Bridges-C++  3.2.0
Bridges(C++API)
Grid.h
Go to the documentation of this file.
1 #ifndef GRID_H
2 
3 #define GRID_H
4 
5 #include "DataStructure.h"
6 #include <vector>
7 #include "base64.h"
8 
9 using namespace std;
10 
11 namespace bridges {
12  namespace datastructure {
30  template <typename E>
31  class Grid : public DataStructure {
32 
33  private:
34  void allocateGrid() {
35  int rows = gridSize[0];
36  int cols = gridSize[1];
37  grid = new E*[rows];
38  for (int j = 0; j < rows; j++) {
39  grid[j] = new E[cols];
40  }
41  }
42 
43  void deallocateGrid() {
44  if (grid) {
45  for (int i = 0; i < gridSize[0]; i++) {
46  delete[] grid[i];
47  }
48  delete[] grid;
49  }
50  grid = nullptr;
51  }
52 
53  void checkRowCol(int row, int col) const {
54  if (row < 0 || col < 0 || row >= gridSize[0] || col >= gridSize[1])
55  throw "invalid location in Grid";
56 
57  }
58 
59 
60  protected:
61  E **grid = nullptr;
62 
63  int gridSize[2];
64  int maxGridSize[2] = {1080, 1920};
65 
66  public:
71  virtual const string getDStype() const override {
72  return "Grid";
73  }
74 
83  Grid(int rows, int cols) {
84  setDimensions(rows, cols);
85  }
86 
90  Grid()
91  : Grid(10, 10) {
92  }
93 
94 
100  explicit Grid(int *size)
101  : Grid(size[0], size[1]) {
102  }
103 
110  Grid(const Grid& g)
111  : Grid(g.gridSize[0], g.gridSize[1]) {
112  for (int i = 0; i < gridSize[0]; i++) {
113  for (int j = 0; j < gridSize[1]; j++) {
114  set (i, j, g.get(i, j));
115  }
116  }
117  }
118 
122  virtual ~Grid() {
123  deallocateGrid();
124  }
125 
129  Grid& operator=(const Grid& g) {
130  if (this->gridSize[0] != g.gridSize[0] ||
131  this->gridSize[1] != g.gridSize[1] ) {
132  deallocateGrid();
133  setDimensions(g.gridSize[0], g.gridSize[1]);
134  }
135  for (int i = 0; i < gridSize[0]; i++) {
136  for (int j = 0; j < gridSize[1]; j++) {
137  set (i, j, g.get(i, j));
138  }
139  }
140 
141  return *this;
142  }
143 
144 
151  void setDimensions(int rows, int cols) {
152  gridSize[0] = rows;
153  gridSize[1] = cols;
154  if (rows > maxGridSize[0] || cols > maxGridSize[1]) {
155  cerr << "Grid Maximum Size (1080 x 1920) exceeded!\n"
156  << "Provided Size: " << rows << " x " << cols
157  << ". Aborting" << endl << endl;
158  exit(-1);
159  }
160  allocateGrid ();
161  }
162 
168  int const * getDimensions() {
169  return gridSize;
170  }
171 
172 
173 
180  E const& get(int row, int col) const {
181  checkRowCol(row, col);
182 
183  return grid[row][col];
184  }
185  // set the (row, col) element in the grid
192  void set(int row, int col, E val) {
193  checkRowCol(row, col);
194 
195  grid[row][col] = val;
196  }
197 
205  Grid<E> const & gr;
206  int row;
207  public:
208  BracketHelperConst(Grid<E> const & g, int r)
209  : gr(g), row(r)
210  {}
211 
212  E const & operator[] (int col) const {
213  return gr.get(row, col);
214  }
215  };
216 
218  BracketHelperConst operator[] (int row) const {
219  return BracketHelperConst(*this, row);
220  }
221 
228  Grid<E> & gr;
229  int row;
230  public:
231  BracketHelper(Grid<E> & g, int r)
232  : gr(g), row(r)
233  {}
234 
235  E & operator[] (int col) {
236  gr.checkRowCol(row, col);
237  return gr.grid[row][col];
238  }
239  };
240 
242  BracketHelper operator[] (int row) {
243  return BracketHelper(*this, row);
244  }
245 
246 
247  }; // end class Grid
248  }
249 } // end - namespace bridges
250 
251 #endif
This BracketHelperConst is a helper class to get the [] operators to work.
Definition: Grid.h:204
This is the superclass of all data structure types in BRIDGES.
Definition: DataStructure.h:73
Grid & operator=(const Grid &g)
Definition: Grid.h:129
Grid()
Definition: Grid.h:90
virtual ~Grid()
Definition: Grid.h:122
STL namespace.
E ** grid
Definition: Grid.h:61
BracketHelper(Grid< E > &g, int r)
Definition: Grid.h:231
This BracketHelper is a helper class to get the [] operators to work. It is not intended to be used b...
Definition: Grid.h:227
Grid(const Grid &g)
Grid constructor given an input grid.
Definition: Grid.h:110
This is a class in BRIDGES for representing an (n x n) grid.
Definition: Grid.h:31
Grid(int *size)
Grid constructor given size.
Definition: Grid.h:100
Grid(int rows, int cols)
Grid constructor.
Definition: Grid.h:83
virtual const string getDStype() const override
Return the data structure type.
Definition: Grid.h:71
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
int gridSize[2]
Definition: Grid.h:63
BracketHelperConst(Grid< E > const &g, int r)
Definition: Grid.h:208
int const * getDimensions()
Get dimenions of the grid.
Definition: Grid.h:168
void setDimensions(int rows, int cols)
Construct the grid given the dimensions.
Definition: Grid.h:151
E const & get(int row, int col) const
Get the (row, col) element in the grid.
Definition: Grid.h:180