parallel-cellular-automata
Framework for building parallel cellular automata.
Public Member Functions | Static Public Member Functions | Friends | List of all members
ca::Grid< T > Class Template Reference

Grid of the cellular automaton. More...

#include <grid.hpp>

Public Member Functions

 Grid (size_t rows, size_t cols)
 Construct a new Grid object. More...
 
 Grid (std::vector< T > grid_, size_t rows)
 Construct a new Grid object. More...
 
 Grid (const Grid &other)
 Construct a new Grid object. More...
 
 Grid (Grid &&other)
 Construct a new Grid object. More...
 
void toFile (std::string filepath)
 Writes the grid to a file. More...
 
T & operator() (unsigned row, unsigned col)
 Get the element in position row,col. More...
 
operator() (unsigned row, unsigned col) const
 Get the element in position row,col. More...
 
size_t rows () const
 Returns the number of rows of the grid. More...
 
size_t columns () const
 Returns the number of columns of the grid. More...
 
void swap (Grid &other)
 Swap the content of the grid with the one of another grid. More...
 
std::vector< T > & getInnerVector ()
 Get a reference to the vector representing the grid. More...
 

Static Public Member Functions

static Grid newWithSameSize (const Grid &other)
 Return a grid of the same dimension of the grid passed as argument. More...
 
static Grid newFromFile (std::string filepath)
 Load a grid from a file. More...
 

Friends

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 separated by a newline. More...
 
bool operator== (const Grid &lhs, const Grid &rhs)
 Compare two grids for equality. More...
 

Detailed Description

template<typename T>
class ca::Grid< T >

Grid of the cellular automaton.

Template Parameters
Ttype of the cell.

Constructor & Destructor Documentation

◆ Grid() [1/4]

template<typename T >
ca::Grid< T >::Grid ( size_t  rows,
size_t  cols 
)
inline

Construct a new Grid object.

Parameters
rowsnumber of rows of the grid.
colsnumber of columns of the grid.
Exceptions
invalid_argumentif either rows or cols are zero.

◆ Grid() [2/4]

template<typename T >
ca::Grid< T >::Grid ( std::vector< T >  grid_,
size_t  rows 
)
inline

Construct a new Grid object.

Parameters
grid_vector containing the elements of the grid.
rowsnumber of rows of the grid.
Note
the number of columns will be calculated as grid_.size() / rows.
Precondition
rows >= 1 && rows
rows divides grid_.size()

◆ Grid() [3/4]

template<typename T >
ca::Grid< T >::Grid ( const Grid< T > &  other)
inline

Construct a new Grid object.

Parameters
othergrid to copy.

◆ Grid() [4/4]

template<typename T >
ca::Grid< T >::Grid ( Grid< T > &&  other)
inline

Construct a new Grid object.

Parameters
othergrid to copy.

Member Function Documentation

◆ columns()

template<typename T >
size_t ca::Grid< T >::columns ( ) const
inline

Returns the number of columns of the grid.

Returns
size_t number of columns.

◆ getInnerVector()

template<typename T >
std::vector<T>& ca::Grid< T >::getInnerVector ( )
inline

Get a reference to the vector representing the grid.

Returns
std::vector<T>& vector representing the grid.

◆ newFromFile()

template<typename T >
static Grid ca::Grid< T >::newFromFile ( std::string  filepath)
inlinestatic

Load a grid from a file.

The file must contain the number of rows, the number of columns and then the elements of the grid.

Parameters
filepathpath to the file.
Returns
Grid grid initialized with the content of the file.

◆ newWithSameSize()

template<typename T >
static Grid ca::Grid< T >::newWithSameSize ( const Grid< T > &  other)
inlinestatic

Return a grid of the same dimension of the grid passed as argument.

Parameters
otherGrid whose dimension has to be taken.
Returns
Grid new grid of same dimension.

◆ operator()() [1/2]

template<typename T >
T& ca::Grid< T >::operator() ( unsigned  row,
unsigned  col 
)
inline

Get the element in position row,col.

Parameters
rowrow number.
colcolumn number.
Precondition
0 <= rows < this->nrows
0 <= col < this->ncols
Returns
T& reference to the element in position row,col

◆ operator()() [2/2]

template<typename T >
T ca::Grid< T >::operator() ( unsigned  row,
unsigned  col 
) const
inline

Get the element in position row,col.

Parameters
rowrow number.
colcolumn number.
Precondition
0 <= rows < this->nrows
0 <= col < this->ncols
Returns
T& reference to the element in position row,col

◆ rows()

template<typename T >
size_t ca::Grid< T >::rows ( ) const
inline

Returns the number of rows of the grid.

Returns
size_t number of rows.

◆ swap()

template<typename T >
void ca::Grid< T >::swap ( Grid< T > &  other)
inline

Swap the content of the grid with the one of another grid.

Parameters
othergrid to swap.

◆ toFile()

template<typename T >
void ca::Grid< T >::toFile ( std::string  filepath)
inline

Writes the grid to a file.

Parameters
filepathpath of the file.

Friends And Related Function Documentation

◆ operator<<

template<typename T >
std::ostream& operator<< ( std::ostream &  os,
const Grid< T > &  grid 
)
friend

Prints the grid on the stream, with the elements in a row separated by a whitespace and the rows separated by a newline.

Parameters
osoutput stream on which to print.
gridgrid to print.
Returns
std::ostream&

◆ operator==

template<typename T >
bool operator== ( const Grid< T > &  lhs,
const Grid< T > &  rhs 
)
friend

Compare two grids for equality.

Returns
true if the grids are equal.
false if the grids differ.

The documentation for this class was generated from the following file: