11 #ifndef PARALLEL_CELLULAR_AUTOMATA_GRID_HPP
12 #define PARALLEL_CELLULAR_AUTOMATA_GRID_HPP
44 throw std::invalid_argument(
"Grid constructor has 0 size.");
57 Grid(std::vector<T> grid_,
size_t rows) : grid(grid_), nrows(
rows), ncols(grid.size() /
rows)
61 throw std::invalid_argument(
"Grid cannot have 0 rows.");
86 grid = std::move(other.grid);
97 return Grid(other.nrows, other.ncols);
111 std::ifstream f(filepath);
127 std::cout << e <<
" ";
134 throw std::invalid_argument(
"Invalid filepath");
144 std::ofstream file(filepath);
147 file << nrows <<
" " << ncols << std::endl;
148 for (
const auto elem : grid)
155 throw std::invalid_argument(
"Invalid filepath");
170 return grid[ncols * row + col];
183 return grid[ncols * row + col];
214 for (
size_t i = 0; i < grid.nrows; ++i)
216 for (
size_t j = 0; j < grid.ncols; ++j)
218 os << grid(i, j) <<
" ";
234 return (lhs.nrows == rhs.nrows) && (lhs.ncols == rhs.ncols) &&
235 std::equal(lhs.grid.begin(), lhs.grid.end(), rhs.grid.begin());
246 swap(nrows, other.nrows);
247 swap(ncols, other.ncols);
248 grid.swap(other.grid);
262 Grid(std::vector<T> &&v,
size_t rows,
size_t cols) : grid(std::move(v)), nrows(
rows), ncols(cols)
266 throw std::invalid_argument(
"Empty vector or invalid dimension");
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