Bridges-C++ 3.5.0-dev2-1-ge3e57bf
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
9using namespace std;
10
11namespace 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
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
213 return BracketHelperConst(*this, row);
214 }
215
222 Grid<E> & gr;
223 int row;
224 public:
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
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:74
This BracketHelperConst is a helper class to get the [] operators to work.
Definition: Grid.h:198
E const & operator[](int col) const
Definition: Grid.h:206
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
E & operator[](int col)
Definition: Grid.h:229
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
void setDimensions(int rows, int cols)
Construct the grid given the dimensions.
Definition: Grid.h:147
Grid()
Definition: Grid.h:88
Grid & operator=(const Grid &g)
Definition: Grid.h:126
BracketHelperConst operator[](int row) const
provides the necessary abstraction to do something = grid[x][y];
Definition: Grid.h:212
Grid(int rows, int cols)
Grid constructor.
Definition: Grid.h:81
int gridSize[2]
Definition: Grid.h:61
virtual ~Grid()
Definition: Grid.h:119
E ** grid
Definition: Grid.h:59
int const * getDimensions()
Get dimenions of the grid.
Definition: Grid.h:164
void set(int row, int col, E val)
Set the grid value for the (row, col) element.
Definition: Grid.h:186
int maxGridSize[2]
Definition: Grid.h:62
Grid(int *size)
Grid constructor given size.
Definition: Grid.h:97
E const & get(int row, int col) const
Get the (row, col) element in the grid.
Definition: Grid.h:174
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