#ifndef MEDUSA_BITS_SPATIAL_SEARCH_GRID_HPP_ #define MEDUSA_BITS_SPATIAL_SEARCH_GRID_HPP_ /** * @file * Implementation of Grid. */ #include "Grid_fwd.hpp" #include #include #include namespace mm { template IndexType Grid::size(int i) const { assert_msg(0 <= i && i < dim, "Dimension index %i must be in range [0, %d)", i, dim); return sizes_[i]; } template const T& Grid::operator()( const Grid::IndexArray& index) const { assert_msg(inBounds(index, sizes_), "Index %s is out of bounds %s.", index, sizes_); return data_[linearIndex(index, sizes_)]; } template T& Grid::operator()(const Grid::IndexArray& index) { assert_msg(inBounds(index, sizes_), "Index %s is out of bounds %s.", index, sizes_); return data_[linearIndex(index, sizes_)]; } template const T& Grid::operator[](const Index& index) const { assert_msg(0 <= index && index < size_, "Index %d is out of bounds [0, %d).", index, size_); return data_[index]; } template T& Grid::operator[](const Index& index) { assert_msg(0 <= index && index < size_, "Index %d is out of bounds [0, %d).", index, size_); return data_[index]; } template IndexType Grid::computeSize(const Grid::IndexArray& sizes) { Index result = 1; for (int i = 0; i < dim; ++i) { result *= sizes[i]; } return result; } template bool Grid::inBounds(const Grid::IndexArray& index, const Grid::IndexArray& bounds) { for (int i = 0; i < dim; ++i) { if (index[i] < 0 || index[i] >= bounds[i]) return false; } return true; } template IndexType Grid::linearIndex( const Grid::IndexArray& index, const Grid::IndexArray& bounds) { assert_msg(inBounds(index, bounds), "Index %s is out of bounds %s.", index, bounds); Index result = 0; for (int i = 0; i < dim; ++i) { result *= bounds[i]; result += index[i]; } return result; } template IndexArrayT Grid::multiIndex( Index index, const IndexArray& bounds) { IndexArray multi_index; for (int i = dim-1; i >= 0; --i) { multi_index[i] = index % bounds[i]; index /= bounds[i]; } return multi_index; } /// @cond template std::ostream& operator<<(std::ostream& os, const Grid& grid) { os << dim << "D grid with sizes " << grid.sizes_ << " and " << grid.size_ << " elements, "; os << "using " << mem2str(mem_used(grid.data())) << " of memory."; return os; } /// @endcond } // namespace mm #endif // MEDUSA_BITS_SPATIAL_SEARCH_GRID_HPP_