parallel-cellular-automata
Framework for building parallel cellular automata.
grid.hpp
Go to the documentation of this file.
1 
11 #ifndef PARALLEL_CELLULAR_AUTOMATA_GRID_HPP
12 #define PARALLEL_CELLULAR_AUTOMATA_GRID_HPP
13 
14 #include <exception>
15 #include <fstream>
16 #include <iostream>
17 #include <memory>
18 #include <string>
19 #include <vector>
20 
21 namespace ca
22 {
23 
29 template <typename T>
30 class Grid
31 {
32  public:
40  Grid(size_t rows, size_t cols) : grid(rows * cols), nrows(rows), ncols(cols)
41  {
42  if (!grid.size())
43  {
44  throw std::invalid_argument("Grid constructor has 0 size.");
45  }
46  }
47 
57  Grid(std::vector<T> grid_, size_t rows) : grid(grid_), nrows(rows), ncols(grid.size() / rows)
58  {
59  if (!rows)
60  {
61  throw std::invalid_argument("Grid cannot have 0 rows.");
62  }
63  }
64 
70  Grid(const Grid &other)
71  {
72  nrows = other.nrows;
73  ncols = other.ncols;
74  grid = other.grid;
75  }
76 
82  Grid(Grid &&other)
83  {
84  nrows = other.nrows;
85  ncols = other.ncols;
86  grid = std::move(other.grid);
87  }
88 
95  static Grid newWithSameSize(const Grid &other)
96  {
97  return Grid(other.nrows, other.ncols);
98  }
99 
109  static Grid newFromFile(std::string filepath)
110  {
111  std::ifstream f(filepath);
112  if (f)
113  {
114 
115  unsigned rows, columns;
116  f >> rows;
117  f >> columns;
118  std::vector<T> v;
119  v.reserve(rows * columns);
120  T value;
121  while (f >> value)
122  {
123  v.push_back(value);
124  }
125  for (auto e : v)
126  {
127  std::cout << e << " ";
128  }
129 
130  return Grid(std::move(v), rows, columns);
131  }
132  else
133  {
134  throw std::invalid_argument("Invalid filepath");
135  }
136  }
142  void toFile(std::string filepath)
143  {
144  std::ofstream file(filepath);
145  if (file)
146  {
147  file << nrows << " " << ncols << std::endl;
148  for (const auto elem : grid)
149  {
150  file << elem << " ";
151  }
152  }
153  else
154  {
155  throw std::invalid_argument("Invalid filepath");
156  }
157  }
158 
168  T &operator()(unsigned row, unsigned col)
169  {
170  return grid[ncols * row + col];
171  }
181  T operator()(unsigned row, unsigned col) const
182  {
183  return grid[ncols * row + col];
184  }
185 
191  inline size_t rows() const
192  {
193  return this->nrows;
194  }
200  inline size_t columns() const
201  {
202  return this->ncols;
203  }
212  friend std::ostream &operator<<(std::ostream &os, const Grid &grid)
213  {
214  for (size_t i = 0; i < grid.nrows; ++i)
215  {
216  for (size_t j = 0; j < grid.ncols; ++j)
217  {
218  os << grid(i, j) << " ";
219  }
220 
221  os << std::endl;
222  }
223 
224  return os;
225  }
232  friend bool operator==(const Grid &lhs, const Grid &rhs)
233  {
234  return (lhs.nrows == rhs.nrows) && (lhs.ncols == rhs.ncols) &&
235  std::equal(lhs.grid.begin(), lhs.grid.end(), rhs.grid.begin());
236  }
237 
243  void swap(Grid &other)
244  {
245  using std::swap;
246  swap(nrows, other.nrows);
247  swap(ncols, other.ncols);
248  grid.swap(other.grid);
249  }
250 
256  std::vector<T> &getInnerVector()
257  {
258  return grid;
259  }
260 
261  private:
262  Grid(std::vector<T> &&v, size_t rows, size_t cols) : grid(std::move(v)), nrows(rows), ncols(cols)
263  {
264  if (!(getInnerVector().size() && rows && cols))
265  {
266  throw std::invalid_argument("Empty vector or invalid dimension");
267  }
268  }
273  std::vector<T> grid;
278  size_t nrows;
283  size_t ncols;
284 };
285 
286 } // namespace ca
287 #endif
Grid of the cellular automaton.
Definition: grid.hpp:31
void toFile(std::string filepath)
Writes the grid to a file.
Definition: grid.hpp:142
Grid(const Grid &other)
Construct a new Grid object.
Definition: grid.hpp:70
size_t columns() const
Returns the number of columns of the grid.
Definition: grid.hpp:200
std::vector< T > & getInnerVector()
Get a reference to the vector representing the grid.
Definition: grid.hpp:256
friend bool operator==(const Grid &lhs, const Grid &rhs)
Compare two grids for equality.
Definition: grid.hpp:232
Grid(size_t rows, size_t cols)
Construct a new Grid object.
Definition: grid.hpp:40
Grid(std::vector< T > grid_, size_t rows)
Construct a new Grid object.
Definition: grid.hpp:57
friend std::ostream & operator<<(std::ostream &os, const Grid &grid)
Prints the grid on the stream, with the elements in a row separated by a whitespace and the rows sepa...
Definition: grid.hpp:212
void swap(Grid &other)
Swap the content of the grid with the one of another grid.
Definition: grid.hpp:243
static Grid newFromFile(std::string filepath)
Load a grid from a file.
Definition: grid.hpp:109
Grid(Grid &&other)
Construct a new Grid object.
Definition: grid.hpp:82
T operator()(unsigned row, unsigned col) const
Get the element in position row,col.
Definition: grid.hpp:181
size_t rows() const
Returns the number of rows of the grid.
Definition: grid.hpp:191
T & operator()(unsigned row, unsigned col)
Get the element in position row,col.
Definition: grid.hpp:168
static Grid newWithSameSize(const Grid &other)
Return a grid of the same dimension of the grid passed as argument.
Definition: grid.hpp:95
Namespace of the framework.
Definition: barrier.hpp:20