Medusa  1.1
Coordinate Free Mehless Method implementation
Grid.hpp
Go to the documentation of this file.
1 #ifndef MEDUSA_BITS_SPATIAL_SEARCH_GRID_HPP_
2 #define MEDUSA_BITS_SPATIAL_SEARCH_GRID_HPP_
3 
9 #include "Grid_fwd.hpp"
12 #include <ostream>
13 
14 namespace mm {
15 
16 template <typename T, int dimension, typename IndexType, typename IndexArrayT>
18  assert_msg(0 <= i && i < dim, "Dimension index %i must be in range [0, %d)", i, dim);
19  return sizes_[i];
20 }
21 
22 template <typename T, int dimension, typename IndexType, typename IndexArrayT>
24  const Grid::IndexArray& index) const {
25  assert_msg(inBounds(index, sizes_), "Index %s is out of bounds %s.", index, sizes_);
26  return data_[linearIndex(index, sizes_)];
27 }
28 
29 template <typename T, int dimension, typename IndexType, typename IndexArrayT>
31  assert_msg(inBounds(index, sizes_), "Index %s is out of bounds %s.", index, sizes_);
32  return data_[linearIndex(index, sizes_)];
33 }
34 
35 template <typename T, int dimension, typename IndexType, typename IndexArrayT>
37  assert_msg(0 <= index && index < size_, "Index %d is out of bounds [0, %d).", index, size_);
38  return data_[index];
39 }
40 
41 template <typename T, int dimension, typename IndexType, typename IndexArrayT>
43  assert_msg(0 <= index && index < size_, "Index %d is out of bounds [0, %d).", index, size_);
44  return data_[index];
45 }
46 
47 template <typename T, int dimension, typename IndexType, typename IndexArrayT>
49  Index result = 1;
50  for (int i = 0; i < dim; ++i) {
51  result *= sizes[i];
52  }
53  return result;
54 }
55 
56 template <typename T, int dimension, typename IndexType, typename IndexArrayT>
58  const Grid::IndexArray& bounds) {
59  for (int i = 0; i < dim; ++i) {
60  if (index[i] < 0 || index[i] >= bounds[i]) return false;
61  }
62  return true;
63 }
64 
65 template <typename T, int dimension, typename IndexType, typename IndexArrayT>
67  const Grid::IndexArray& index, const Grid::IndexArray& bounds) {
68  assert_msg(inBounds(index, bounds), "Index %s is out of bounds %s.", index, bounds);
69  Index result = 0;
70  for (int i = 0; i < dim; ++i) {
71  result *= bounds[i];
72  result += index[i];
73  }
74  return result;
75 }
76 
77 template <typename T, int dimension, typename IndexType, typename IndexArrayT>
79  Index index, const IndexArray& bounds) {
80  IndexArray multi_index;
81  for (int i = dim-1; i >= 0; --i) {
82  multi_index[i] = index % bounds[i];
83  index /= bounds[i];
84  }
85  return multi_index;
86 }
87 
89 template <typename T, int dim, typename I, typename IA>
90 std::ostream& operator<<(std::ostream& os, const Grid<T, dim, I, IA>& grid) {
91  os << dim << "D grid with sizes " << grid.sizes_ << " and " << grid.size_ << " elements, ";
92  os << "using " << mem2str(mem_used(grid.data())) << " of memory.";
93  return os;
94 }
96 
97 } // namespace mm
98 
99 #endif // MEDUSA_BITS_SPATIAL_SEARCH_GRID_HPP_
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< int, dim >::Index
int Index
Type of the index used.
Definition: Grid_fwd.hpp:39
Grid_fwd.hpp
dim
@ dim
Number of elements of this matrix.
Definition: MatrixBaseAddons.hpp:14
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::operator<<
std::ostream & operator<<(std::ostream &os, const Gaussian< S > &b)
Output basic information about given Gaussian RBF.
Definition: Gaussian.hpp:37
mm::Grid::operator[]
const T & operator[](const Index &index) const
Readonly access to the grid.
Definition: Grid.hpp:36
assert_msg
#define assert_msg(cond,...)
Assert with better error reporting.
Definition: assert.hpp:75
mm::Grid
Class representing a simple n-dimensional grid structure, which supports indexing and storing values.
Definition: Grid_fwd.hpp:36
mm::Grid::linearIndex
Index linearIndex(const IndexArray &index) const
Compute linear index from given multi-index.
Definition: Grid_fwd.hpp:80
assert.hpp
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
memutils.hpp
mm::mem_used
std::size_t mem_used(const container_t &v)
Returns number of bytes the container uses in memory.
Definition: memutils.hpp:83
mm::mem2str
std::string mem2str(std::size_t bytes)
Simple function to help format memory amounts for printing.
Definition: memutils.cpp:10