Bridges-C++  3.1.1
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:
67  virtual const string getDStype() const override {
68  return "Grid";
69  }
70 
77  Grid(int rows, int cols) {
78  setDimensions(rows, cols);
79  }
80 
81  Grid()
82  : Grid(10, 10) {
83  }
84 
85 
86  explicit Grid(int *size)
87  : Grid(size[0], size[1]) {
88  }
89 
90  Grid(const Grid& g)
91  : Grid(g.gridSize[0], g.gridSize[1]) {
92  for (int i = 0; i < gridSize[0]; i++) {
93  for (int j = 0; j < gridSize[1]; j++) {
94  set (i, j, g.get(i, j));
95  }
96  }
97  }
98 
99  virtual ~Grid() {
100  deallocateGrid();
101  }
102 
103  Grid& operator=(const Grid& g) {
104  if (this->gridSize[0] != g.gridSize[0] ||
105  this->gridSize[1] != g.gridSize[1] ) {
106  deallocateGrid();
107  setDimensions(g.gridSize[0], g.gridSize[1]);
108  }
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  return *this;
116  }
117 
118 
119  void setDimensions(int rows, int cols) {
120  gridSize[0] = rows;
121  gridSize[1] = cols;
122  if (rows > maxGridSize[0] || cols > maxGridSize[1]) {
123  cerr << "Grid Maximum Size (1080 x 1920) exceeded!\n"
124  << "Provided Size: " << rows << " x " << cols
125  << ". Aborting" << endl << endl;
126  exit(-1);
127  }
128  allocateGrid ();
129  }
130 
131  int const * getDimensions() {
132  return gridSize;
133  }
134 
135 
136 
137  // get the (row, col) element in the grid
138  E const& get(int row, int col) const {
139  checkRowCol(row, col);
140 
141  return grid[row][col];
142  }
143  // set the (row, col) element in the grid
144  void set(int row, int col, E val) {
145  checkRowCol(row, col);
146 
147  grid[row][col] = val;
148  }
149 
150  //This BracketHelperConst is a helper class to get the [] operators to work.
151  //It is not intended to be used by bridges users.
153  Grid<E> const & gr;
154  int row;
155  public:
156  BracketHelperConst(Grid<E> const & g, int r)
157  : gr(g), row(r)
158  {}
159 
160  E const & operator[] (int col) const {
161  return gr.get(row, col);
162  }
163  };
164 
166  BracketHelperConst operator[] (int row) const {
167  return BracketHelperConst(*this, row);
168  }
169 
170  //This BracketHelper is a helper class to get the [] operators to work.
171  //It is not intended to be used by bridges users.
173  Grid<E> & gr;
174  int row;
175  public:
176  BracketHelper(Grid<E> & g, int r)
177  : gr(g), row(r)
178  {}
179 
180  E & operator[] (int col) {
181  gr.checkRowCol(row, col);
182  return gr.grid[row][col];
183  }
184  };
185 
187  BracketHelper operator[] (int row) {
188  return BracketHelper(*this, row);
189  }
190 
191 
192  }; // end class Grid
193  }
194 } // end - namespace bridges
195 
196 #endif
This is the superclass of all data structure types in BRIDGES.
Definition: DataStructure.h:73
Grid & operator=(const Grid &g)
Definition: Grid.h:103
Grid()
Definition: Grid.h:81
virtual ~Grid()
Definition: Grid.h:99
STL namespace.
E ** grid
Definition: Grid.h:61
BracketHelper(Grid< E > &g, int r)
Definition: Grid.h:176
Grid(const Grid &g)
Definition: Grid.h:90
This is a class in BRIDGES for representing an (n x n) grid.
Definition: Grid.h:31
Grid(int *size)
Definition: Grid.h:86
Grid(int rows, int cols)
Definition: Grid.h:77
virtual const string getDStype() const override
Definition: Grid.h:67
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:156
int const * getDimensions()
Definition: Grid.h:131
void setDimensions(int rows, int cols)
Definition: Grid.h:119
E const & get(int row, int col) const
Definition: Grid.h:138