Bridges-C++  3.4.4
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 {
29  template <typename E>
30  class Grid : public DataStructure {
31 
32  private:
33  void allocateGrid() {
34  int rows = gridSize[0];
35  int cols = gridSize[1];
36  grid = new E*[rows];
37  for (int j = 0; j < rows; j++) {
38  grid[j] = new E[cols];
39  }
40  }
41 
42  void deallocateGrid() {
43  if (grid) {
44  for (int i = 0; i < gridSize[0]; i++) {
45  delete[] grid[i];
46  }
47  delete[] grid;
48  }
49  grid = nullptr;
50  }
51 
52  void checkRowCol(int row, int col) const {
53  if (row < 0 || col < 0 || row >= gridSize[0] || col >= gridSize[1])
54  throw "invalid location in Grid";
55 
56  }
57 
58  protected:
59  E **grid = nullptr;
60 
61  int gridSize[2];
62  int maxGridSize[2] = {1080, 1920};
63 
64  public:
69  virtual const string getDStype() const override {
70  return "Grid";
71  }
72 
81  Grid(int rows, int cols) {
82  setDimensions(rows, cols);
83  }
84 
88  Grid()
89  : Grid(10, 10) {
90  }
91 
97  explicit Grid(int *size)
98  : Grid(size[0], size[1]) {
99  }
100 
107  Grid(const Grid& g)
108  : Grid(g.gridSize[0], g.gridSize[1]) {
109  for (int i = 0; i < gridSize[0]; i++) {
110  for (int j = 0; j < gridSize[1]; j++) {
111  set (i, j, g.get(i, j));
112  }
113  }
114  }
115 
119  virtual ~Grid() {
120  deallocateGrid();
121  }
122 
126  Grid& operator=(const Grid& g) {
127  if (this->gridSize[0] != g.gridSize[0] ||
128  this->gridSize[1] != g.gridSize[1] ) {
129  deallocateGrid();
130  setDimensions(g.gridSize[0], g.gridSize[1]);
131  }
132  for (int i = 0; i < gridSize[0]; i++) {
133  for (int j = 0; j < gridSize[1]; j++) {
134  set (i, j, g.get(i, j));
135  }
136  }
137 
138  return *this;
139  }
140 
147  void setDimensions(int rows, int cols) {
148  gridSize[0] = rows;
149  gridSize[1] = cols;
150  if (rows > maxGridSize[0] || cols > maxGridSize[1]) {
151  cerr << "Grid Maximum Size (1080 x 1920) exceeded!\n"
152  << "Provided Size: " << rows << " x " << cols
153  << ". Aborting" << endl << endl;
154  exit(-1);
155  }
156  allocateGrid ();
157  }
158 
164  int const * getDimensions() {
165  return gridSize;
166  }
167 
174  E const& get(int row, int col) const {
175  checkRowCol(row, col);
176 
177  return grid[row][col];
178  }
179  // set the (row, col) element in the grid
186  void set(int row, int col, E val) {
187  checkRowCol(row, col);
188 
189  grid[row][col] = val;
190  }
191 
199  Grid<E> const & gr;
200  int row;
201  public:
202  BracketHelperConst(Grid<E> const & g, int r)
203  : gr(g), row(r)
204  {}
205 
206  E const & operator[] (int col) const {
207  return gr.get(row, col);
208  }
209  };
210 
212  BracketHelperConst operator[] (int row) const {
213  return BracketHelperConst(*this, row);
214  }
215 
222  Grid<E> & gr;
223  int row;
224  public:
225  BracketHelper(Grid<E> & g, int r)
226  : gr(g), row(r)
227  {}
228 
229  E & operator[] (int col) {
230  gr.checkRowCol(row, col);
231  return gr.grid[row][col];
232  }
233  };
234 
236  BracketHelper operator[] (int row) {
237  return BracketHelper(*this, row);
238  }
239 
240  }; // end class Grid
241  }
242 } // end - namespace bridges
243 
244 #endif
This is the superclass of all data structure types in BRIDGES.
Definition: DataStructure.h:73
This BracketHelperConst is a helper class to get the [] operators to work.
Definition: Grid.h:198
BracketHelperConst(Grid< E > const &g, int r)
Definition: Grid.h:202
This BracketHelper is a helper class to get the [] operators to work. It is not intended to be used b...
Definition: Grid.h:221
BracketHelper(Grid< E > &g, int r)
Definition: Grid.h:225
This is a class in BRIDGES for representing an (n x n) grid.
Definition: Grid.h:30
virtual const string getDStype() const override
Return the data structure type.
Definition: Grid.h:69
E const & get(int row, int col) const
Get the (row, col) element in the grid.
Definition: Grid.h:174
void setDimensions(int rows, int cols)
Construct the grid given the dimensions.
Definition: Grid.h:147
Grid & operator=(const Grid &g)
Definition: Grid.h:126
Grid()
Definition: Grid.h:88
int const * getDimensions()
Get dimenions of the grid.
Definition: Grid.h:164
Grid(int rows, int cols)
Grid constructor.
Definition: Grid.h:81
virtual ~Grid()
Definition: Grid.h:119
E ** grid
Definition: Grid.h:59
void set(int row, int col, E val)
Set the grid value for the (row, col) element.
Definition: Grid.h:186
Grid(int *size)
Grid constructor given size.
Definition: Grid.h:97
Grid(const Grid &g)
Grid constructor given an input grid.
Definition: Grid.h:107
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4