Medusa  1.1
Coordinate Free Mehless Method implementation
mm::Grid< T, dimension, IndexType, IndexArrayT > Class Template Reference

#include <Grid_fwd.hpp>

Detailed Description

template<typename T, int dimension, typename IndexType = int, typename IndexArrayT = std::array<IndexType, dimension>>
class mm::Grid< T, dimension, IndexType, IndexArrayT >

Class representing a simple n-dimensional grid structure, which supports indexing and storing values.

The values are stored in row-major order.

Template Parameters
TType of the values stored in the grid.
dimensionDimension of the grid.
IndexTypeType used for indexing.

Usage example:

Grid<double, 4> grid({2, 2, 2, 2});
grid({0, 1, 0, 0}) = -3;
grid({0, 0, 1, 0}) = 2;
EXPECT_EQ(2*2*2*2, grid.size());
EXPECT_EQ((std::array<int, 4>({2, 2, 2, 2})), grid.sizes());
EXPECT_EQ(2, grid.size(2));
EXPECT_EQ(-3, grid({0, 1, 0, 0}));
EXPECT_EQ(2, grid({0, 0, 1, 0}));
std::cout << grid << std::endl;
See also
KDGrid

Definition at line 36 of file Grid_fwd.hpp.

Public Member Functions

 Grid (const IndexArray &sizes)
 Construct a zero initialized grid with given sizes. More...
 
 Grid (const IndexArray &sizes, const T &value)
 Construct a grid with given sizes initialized to value. More...
 
IndexArray sizes () const
 Get grid sizes. More...
 
Index size (int i) const
 Get side in i-th dimension. More...
 
Index size () const
 Get total number of elements. More...
 
const std::vector< T > & data () const
 Get read-only access to linear data. More...
 
std::vector< T > & data ()
 Get read-write access to linear data. More...
 
const T & operator() (const IndexArray &index) const
 Readonly access to the grid. More...
 
T & operator() (const IndexArray &index)
 Read-write access to the grid. More...
 
const T & operator[] (const Index &index) const
 Readonly access to the grid. More...
 
T & operator[] (const Index &index)
 Read-write access to the grid. More...
 
Index linearIndex (const IndexArray &index) const
 Compute linear index from given multi-index. More...
 
IndexArray multiIndex (const Index &index) const
 Compute multi-index from given linear index. More...
 
bool inBounds (const IndexArray &index) const
 Check if given index in in bounds. More...
 

Public Types

enum  { dim = dimension }
 
typedef T value_type
 Type of the values stored in the grid. More...
 
typedef IndexType Index
 Type of the index used. More...
 
typedef IndexArrayT IndexArray
 Multiindex type. More...
 

Friends

template<typename U , int D, typename I , typename IA >
std::ostream & operator<< (std::ostream &os, const Grid< U, D, I, IA > &grid)
 Output some information about given grid. More...
 

Static Private Member Functions

static Index computeSize (const IndexArray &sizes)
 Compute the number of elements. More...
 
static bool inBounds (const IndexArray &index, const IndexArray &bounds)
 Check if index is in given bounds. More...
 
static Index linearIndex (const IndexArray &index, const IndexArray &bounds)
 Compute the linear index with respect to given bounds. More...
 
static IndexArray multiIndex (Index index, const IndexArray &bounds)
 Compute the multi-index with respect to given bounds. More...
 

Private Attributes

IndexArray sizes_
 Boundary of the grid in all dimensions. More...
 
IndexType size_
 Total number of grid cells. More...
 
std::vector< T > data_
 Data stored in the grid. More...
 

Member Enumeration Documentation

◆ anonymous enum

template<typename T , int dimension, typename IndexType = int, typename IndexArrayT = std::array<IndexType, dimension>>
anonymous enum
Enumerator
dim 

Dimensionality of the domain.

Definition at line 40 of file Grid_fwd.hpp.

Constructor & Destructor Documentation

◆ Grid() [1/2]

template<typename T , int dimension, typename IndexType = int, typename IndexArrayT = std::array<IndexType, dimension>>
mm::Grid< T, dimension, IndexType, IndexArrayT >::Grid ( const IndexArray sizes)
inline

Construct a zero initialized grid with given sizes.

Definition at line 50 of file Grid_fwd.hpp.

◆ Grid() [2/2]

template<typename T , int dimension, typename IndexType = int, typename IndexArrayT = std::array<IndexType, dimension>>
mm::Grid< T, dimension, IndexType, IndexArrayT >::Grid ( const IndexArray sizes,
const T &  value 
)
inline

Construct a grid with given sizes initialized to value.

Definition at line 52 of file Grid_fwd.hpp.

Member Function Documentation

◆ computeSize()

template<typename T , int dimension, typename IndexType , typename IndexArrayT >
IndexType mm::Grid< T, dimension, IndexType, IndexArrayT >::computeSize ( const IndexArray sizes)
staticprivate

Compute the number of elements.

Definition at line 48 of file Grid.hpp.

◆ data() [1/2]

template<typename T , int dimension, typename IndexType = int, typename IndexArrayT = std::array<IndexType, dimension>>
std::vector<T>& mm::Grid< T, dimension, IndexType, IndexArrayT >::data ( )
inline

Get read-write access to linear data.

Definition at line 65 of file Grid_fwd.hpp.

◆ data() [2/2]

template<typename T , int dimension, typename IndexType = int, typename IndexArrayT = std::array<IndexType, dimension>>
const std::vector<T>& mm::Grid< T, dimension, IndexType, IndexArrayT >::data ( ) const
inline

Get read-only access to linear data.

Definition at line 63 of file Grid_fwd.hpp.

◆ inBounds() [1/2]

template<typename T , int dimension, typename IndexType = int, typename IndexArrayT = std::array<IndexType, dimension>>
bool mm::Grid< T, dimension, IndexType, IndexArrayT >::inBounds ( const IndexArray index) const
inline

Check if given index in in bounds.

Definition at line 85 of file Grid_fwd.hpp.

◆ inBounds() [2/2]

template<typename T , int dimension, typename IndexType , typename IndexArrayT >
bool mm::Grid< T, dimension, IndexType, IndexArrayT >::inBounds ( const IndexArray index,
const IndexArray bounds 
)
staticprivate

Check if index is in given bounds.

Definition at line 57 of file Grid.hpp.

◆ linearIndex() [1/2]

template<typename T , int dimension, typename IndexType = int, typename IndexArrayT = std::array<IndexType, dimension>>
Index mm::Grid< T, dimension, IndexType, IndexArrayT >::linearIndex ( const IndexArray index) const
inline

Compute linear index from given multi-index.

See also
multiIndex

Definition at line 80 of file Grid_fwd.hpp.

◆ linearIndex() [2/2]

template<typename T , int dimension, typename IndexType , typename IndexArrayT >
IndexType mm::Grid< T, dimension, IndexType, IndexArrayT >::linearIndex ( const IndexArray index,
const IndexArray bounds 
)
staticprivate

Compute the linear index with respect to given bounds.

Definition at line 66 of file Grid.hpp.

◆ multiIndex() [1/2]

template<typename T , int dimension, typename IndexType = int, typename IndexArrayT = std::array<IndexType, dimension>>
IndexArray mm::Grid< T, dimension, IndexType, IndexArrayT >::multiIndex ( const Index index) const
inline

Compute multi-index from given linear index.

See also
linearIndex

Definition at line 82 of file Grid_fwd.hpp.

◆ multiIndex() [2/2]

template<typename T , int dimension, typename IndexType , typename IndexArrayT >
IndexArrayT mm::Grid< T, dimension, IndexType, IndexArrayT >::multiIndex ( Index  index,
const IndexArray bounds 
)
staticprivate

Compute the multi-index with respect to given bounds.

Definition at line 78 of file Grid.hpp.

◆ operator()() [1/2]

template<typename T , int dimension, typename IndexType , typename IndexArrayT >
T & mm::Grid< T, dimension, IndexType, IndexArrayT >::operator() ( const IndexArray index)
inline

Read-write access to the grid.

Exceptions
Assertionfails if index if out of bounds.

Definition at line 30 of file Grid.hpp.

◆ operator()() [2/2]

template<typename T , int dimension, typename IndexType , typename IndexArrayT >
const T & mm::Grid< T, dimension, IndexType, IndexArrayT >::operator() ( const IndexArray index) const
inline

Readonly access to the grid.

Exceptions
Assertionfails if index if out of bounds.

Definition at line 23 of file Grid.hpp.

◆ operator[]() [1/2]

template<typename T , int dimension, typename IndexType , typename IndexArrayT >
T & mm::Grid< T, dimension, IndexType, IndexArrayT >::operator[] ( const Index index)
inline

Read-write access to the grid.

Exceptions
Assertionfails if index if out of bounds.

Definition at line 42 of file Grid.hpp.

◆ operator[]() [2/2]

template<typename T , int dimension, typename IndexType , typename IndexArrayT >
const T & mm::Grid< T, dimension, IndexType, IndexArrayT >::operator[] ( const Index index) const
inline

Readonly access to the grid.

Exceptions
Assertionfails if index if out of bounds.

Definition at line 36 of file Grid.hpp.

◆ size() [1/2]

template<typename T , int dimension, typename IndexType = int, typename IndexArrayT = std::array<IndexType, dimension>>
Index mm::Grid< T, dimension, IndexType, IndexArrayT >::size ( ) const
inline

Get total number of elements.

Definition at line 60 of file Grid_fwd.hpp.

◆ size() [2/2]

template<typename T , int dimension, typename IndexType , typename IndexArrayT >
IndexType mm::Grid< T, dimension, IndexType, IndexArrayT >::size ( int  i) const
inline

Get side in i-th dimension.

Definition at line 17 of file Grid.hpp.

◆ sizes()

template<typename T , int dimension, typename IndexType = int, typename IndexArrayT = std::array<IndexType, dimension>>
IndexArray mm::Grid< T, dimension, IndexType, IndexArrayT >::sizes ( ) const
inline

Get grid sizes.

Definition at line 56 of file Grid_fwd.hpp.

Friends And Related Function Documentation

◆ operator<<

template<typename T , int dimension, typename IndexType = int, typename IndexArrayT = std::array<IndexType, dimension>>
template<typename U , int D, typename I , typename IA >
std::ostream& operator<< ( std::ostream &  os,
const Grid< U, D, I, IA > &  grid 
)
friend

Output some information about given grid.

Member Data Documentation

◆ data_

template<typename T , int dimension, typename IndexType = int, typename IndexArrayT = std::array<IndexType, dimension>>
std::vector<T> mm::Grid< T, dimension, IndexType, IndexArrayT >::data_
private

Data stored in the grid.

Definition at line 46 of file Grid_fwd.hpp.

◆ size_

template<typename T , int dimension, typename IndexType = int, typename IndexArrayT = std::array<IndexType, dimension>>
IndexType mm::Grid< T, dimension, IndexType, IndexArrayT >::size_
private

Total number of grid cells.

Definition at line 45 of file Grid_fwd.hpp.

◆ sizes_

template<typename T , int dimension, typename IndexType = int, typename IndexArrayT = std::array<IndexType, dimension>>
IndexArray mm::Grid< T, dimension, IndexType, IndexArrayT >::sizes_
private

Boundary of the grid in all dimensions.

Definition at line 44 of file Grid_fwd.hpp.


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