Medusa  1.1
Coordinate Free Mehless Method implementation
Grid_fwd.hpp
Go to the documentation of this file.
1 #ifndef MEDUSA_BITS_SPATIAL_SEARCH_GRID_FWD_HPP_
2 #define MEDUSA_BITS_SPATIAL_SEARCH_GRID_FWD_HPP_
3 
4 
13 #include <medusa/Config.hpp>
14 #include <array>
15 #include <vector>
16 #include <iosfwd>
17 
18 namespace mm {
19 
34 template <typename T, int dimension, typename IndexType = int,
35  typename IndexArrayT = std::array<IndexType, dimension>>
36 class Grid {
37  public:
38  typedef T value_type;
39  typedef IndexType Index;
40  enum { dim = dimension };
41  typedef IndexArrayT IndexArray;
42 
43  private:
45  IndexType size_;
46  std::vector<T> data_;
47 
48  public:
52  Grid(const IndexArray& sizes, const T& value) :
53  sizes_(sizes), size_(computeSize(sizes)), data_(size_, value) {}
54 
56  IndexArray sizes() const { return sizes_; }
58  inline Index size(int i) const;
60  Index size() const { return size_; }
61 
63  const std::vector<T>& data() const { return data_; }
65  std::vector<T>& data() { return data_; }
66 
68  inline const T& operator()(const IndexArray& index) const;
69 
71  inline T& operator()(const IndexArray& index);
72 
74  inline const T& operator[](const Index& index) const;
75 
77  inline T& operator[](const Index& index);
78 
80  Index linearIndex(const IndexArray& index) const { return linearIndex(index, sizes_); }
82  IndexArray multiIndex(const Index& index) const { return multiIndex(index, sizes_); }
83 
85  bool inBounds(const IndexArray& index) const { return inBounds(index, sizes_); }
86 
87  private:
89  static Index computeSize(const IndexArray& sizes);
90 
92  static bool inBounds(const IndexArray& index, const IndexArray& bounds);
93 
95  static Index linearIndex(const IndexArray& index, const IndexArray& bounds);
97  static IndexArray multiIndex(Index index, const IndexArray& bounds);
98 
99  public:
101  template <typename U, int D, typename I, typename IA>
102  friend std::ostream& operator<<(std::ostream& os, const Grid<U, D, I, IA>& grid);
103 };
104 
105 } // namespace mm
106 
107 #endif // MEDUSA_BITS_SPATIAL_SEARCH_GRID_FWD_HPP_
mm::Grid::Grid
Grid(const IndexArray &sizes, const T &value)
Construct a grid with given sizes initialized to value.
Definition: Grid_fwd.hpp:52
mm
Root namespace for the whole library.
Definition: Gaussian.hpp:14
mm::Grid::operator()
const T & operator()(const IndexArray &index) const
Readonly access to the grid.
Definition: Grid.hpp:23
mm::Grid::multiIndex
IndexArray multiIndex(const Index &index) const
Compute multi-index from given linear index.
Definition: Grid_fwd.hpp:82
mm::Grid::Index
IndexType Index
Type of the index used.
Definition: Grid_fwd.hpp:39
mm::Grid::size
Index size() const
Get total number of elements.
Definition: Grid_fwd.hpp:60
mm::Grid::computeSize
static Index computeSize(const IndexArray &sizes)
Compute the number of elements.
Definition: Grid.hpp:48
mm::Grid::operator[]
const T & operator[](const Index &index) const
Readonly access to the grid.
Definition: Grid.hpp:36
mm::Grid::operator<<
friend std::ostream & operator<<(std::ostream &os, const Grid< U, D, I, IA > &grid)
Output some information about given grid.
mm::Grid::data_
std::vector< T > data_
Data stored in the grid.
Definition: Grid_fwd.hpp:46
Config.hpp
mm::Grid
Class representing a simple n-dimensional grid structure, which supports indexing and storing values.
Definition: Grid_fwd.hpp:36
mm::Grid::data
std::vector< T > & data()
Get read-write access to linear data.
Definition: Grid_fwd.hpp:65
mm::Grid::linearIndex
Index linearIndex(const IndexArray &index) const
Compute linear index from given multi-index.
Definition: Grid_fwd.hpp:80
mm::Grid::sizes_
IndexArray sizes_
Boundary of the grid in all dimensions.
Definition: Grid_fwd.hpp:44
mm::Grid::data
const std::vector< T > & data() const
Get read-only access to linear data.
Definition: Grid_fwd.hpp:63
mm::Grid::IndexArray
IndexArrayT IndexArray
Multiindex type.
Definition: Grid_fwd.hpp:41
mm::Grid::inBounds
bool inBounds(const IndexArray &index) const
Check if given index in in bounds.
Definition: Grid_fwd.hpp:85
mm::Grid::size_
IndexType size_
Total number of grid cells.
Definition: Grid_fwd.hpp:45
mm::Grid::value_type
T value_type
Type of the values stored in the grid.
Definition: Grid_fwd.hpp:38
mm::Grid::dim
@ dim
Dimensionality of the domain.
Definition: Grid_fwd.hpp:40
mm::Grid::sizes
IndexArray sizes() const
Get grid sizes.
Definition: Grid_fwd.hpp:56
mm::Grid::Grid
Grid(const IndexArray &sizes)
Construct a zero initialized grid with given sizes.
Definition: Grid_fwd.hpp:50