Fleet  0.0.9
Inference in the LOT
Vector2D.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include <assert.h>
5 #include "IO.h"
6 
13 template<typename T>
14 struct Vector2D {
15  int xsize = 0;
16  int ysize = 0;
17 
18  std::vector<T> value;
19 
20  Vector2D() { }
21 
22  Vector2D(int x, int y) {
23  resize(x,y);
24  }
25 
26  Vector2D(int x, int y, T b) {
27  resize(x,y);
28  fill(b);
29  }
30 
31  bool inbounds(int x, int y) {
32  return (x>=0 and x<xsize and y>=0 and y<ysize);
33  }
34 
35  void fill(T x){
36  std::fill(value.begin(), value.end(), x);
37  }
38 
39  void resize(const int x, const int y) {
40  xsize=x; ysize=y;
41  value.resize(x*y);
42  }
43 
44  void reserve(const int x, const int y) {
45  xsize=x; ysize=y;
46  value.reserve(x*y);
47  }
48 
49  void insert_y(const int y_at, T b) {
50  // insert a new row at y -- note this is a slow operation I'm sure
51  if(y_at >= ysize+1) return; // nothing happens
52  auto nxt = Vector2D<T>(xsize, ysize+1);
53  for(int x=0;x<nxt.xsize;x++) {
54  for(int y=0;y<nxt.ysize;y++) {
55  if(y < y_at) nxt.at(x,y) = this->at(x,y);
56  else if(y == y_at) nxt.at(x,y) = b;
57  else nxt.at(x,y) = this->at(x,y-1);
58  }
59  }
60 
61  this->operator=(std::move(nxt)); // set to myself
62  }
63  void insert_x(const int x_at, T b) {
64  // insert a new row at y -- note this is a slow operation I'm sure
65  if(x_at >= xsize+1) return; // nothing happens
66  auto nxt = Vector2D<T>(xsize+1, ysize);
67  for(int x=0;x<nxt.xsize;x++) {
68  for(int y=0;y<nxt.ysize;y++) {
69  if(x < x_at) nxt.at(x,y) = this->at(x,y);
70  else if(x == x_at) nxt.at(x,y) = b;
71  else nxt.at(x,y) = this->at(x-1,y);
72  }
73  }
74 
75  this->operator=(std::move(nxt)); // set to myself
76  }
77  void delete_y(const int y_at) {
78  if(ysize == 0 or y_at >= ysize or y_at < 0) return;
79  // insert a new row at y -- note this is a slow operation I'm sure
80  auto nxt = Vector2D<T>(xsize, ysize-1);
81  for(int x=0;x<nxt.xsize;x++) { // TODO Can speed up by moving the if outside the y loop
82  for(int y=0;y<ysize;y++) {
83  if(y < y_at) nxt.at(x,y) = this->at(x,y);
84  else if(y > y_at) nxt.at(x,y-1) = this->at(x,y);
85  }
86  }
87 
88  this->operator=(std::move(nxt)); // set to myself
89  }
90  void delete_x(const int x_at) {
91  if(xsize == 0 or x_at >= xsize or x_at < 0) return;
92  // insert a new row at y -- note this is a slow operation I'm sure
93  auto nxt = Vector2D<T>(xsize-1, ysize);
94  for(int x=0;x<xsize;x++) {
95  for(int y=0;y<nxt.ysize;y++) {
96  if(x < x_at) nxt.at(x,y) = this->at(x,y);
97  else if(x > x_at) nxt.at(x-1,y) = this->at(x,y);
98  }
99  }
100 
101  this->operator=(std::move(nxt)); // set to myself
102  }
103 
104 
105  const T& at(const int x, const int y) const {
106  if constexpr (std::is_same<T,bool>::value) { assert(false && "*** Golly you can't have references in std::vector<bool>."); }
107  else {
108  return value.at(x*ysize + y);
109  }
110  }
111 
112  T& at(const int x, const int y) {
113  if constexpr (std::is_same<T,bool>::value) { assert(false && "*** Golly you can't have references in std::vector<bool>."); }
114  else {
115  return value.at(x*ysize + y);
116  }
117  }
118 
119  T& operator()(const int x, const int y) const {
120  if constexpr (std::is_same<T,bool>::value) { assert(false && "*** Golly you can't have references in std::vector<bool>."); }
121  else {
122  return value.at(x*ysize + y);
123  }
124  }
125 
126  const T& operator()(const int x, const int y) {
127  return value.at(x*ysize + y);
128  }
129 
130 
131  // get and Set here are used without references (letting us to 2D vectors of bools)
132  void set(const int x, const int y, const T& val) {
133  value.at(x*ysize + y) = val;
134  }
135  void set(const int x, const int y, const T&& val) {
136  value.at(x*ysize + y) = val;
137  }
138  T get(const int x, const int y) {
139  return value.at(x*ysize+y);
140  }
141 
142  template<typename X>
143  void operator[](X x) {
144  CERR "**** Cannot use [] with Vector2d, use .at()" ENDL;
145  throw YouShouldNotBeHereError();
146  }
147 
148  std::string string() {
149  std::string out = "[";
150  for(int y=0;y<ysize;y++) {
151  out += "[";
152  for(int x=0;x<xsize;x++) {
153  out += str(at(x,y)) + ",";
154  }
155  if(ysize > 0) out.pop_back();
156  out += "],";
157  }
158  if(xsize > 0) out.pop_back(); // remove last comma
159  return out;
160  }
161 };
162 
171 //
172 //template<>
173 //struct Vector2D<bool> : Vector2D<unsigned char> {
174 // using Super = Vector2D<unsigned char>;
175 // using Super::Super;
176 //
177 //};
void delete_y(const int y_at)
Definition: Vector2D.h:77
Vector2D(int x, int y)
Definition: Vector2D.h:22
void operator[](X x)
Definition: Vector2D.h:143
int xsize
Definition: Vector2D.h:15
T & operator()(const int x, const int y) const
Definition: Vector2D.h:119
void reserve(const int x, const int y)
Definition: Vector2D.h:44
void fill(T x)
Definition: Vector2D.h:35
Definition: Errors.h:18
std::vector< T > value
Definition: Vector2D.h:18
std::string string()
Definition: Vector2D.h:148
std::string str(BindingTree *t)
Definition: BindingTree.h:195
const T & operator()(const int x, const int y)
Definition: Vector2D.h:126
#define CERR
Definition: IO.h:23
void insert_y(const int y_at, T b)
Definition: Vector2D.h:49
void delete_x(const int x_at)
Definition: Vector2D.h:90
Just a little wrapper to allow vectors to be handled as 2D arrays, which simplifie some stuff in Gram...
Definition: Vector2D.h:14
void insert_x(const int x_at, T b)
Definition: Vector2D.h:63
#define ENDL
Definition: IO.h:21
bool inbounds(int x, int y)
Definition: Vector2D.h:31
Vector2D(int x, int y, T b)
Definition: Vector2D.h:26
const T & at(const int x, const int y) const
Definition: Vector2D.h:105
Vector2D()
Definition: Vector2D.h:20
int ysize
Definition: Vector2D.h:16
T & at(const int x, const int y)
Definition: Vector2D.h:112
void resize(const int x, const int y)
Definition: Vector2D.h:39