23#ifndef VCL_SPACE_COMPLEX_GRID_HASH_TABLE_GRID_H
24#define VCL_SPACE_COMPLEX_GRID_HASH_TABLE_GRID_H
26#include "abstract_grid.h"
27#include "regular_grid.h"
30#include <unordered_map>
49template<
typename Gr
idType,
typename ValueType,
bool AllowDuplicates = true>
54 HashTableGrid<GridType, ValueType, AllowDuplicates>>
58 "Not allowing duplicates in a Spatial Data Structures means that "
59 "ValueType must implement operator==.");
69 using KeyType = AbsGrid::KeyType;
72 using MapType = std::unordered_multimap<KeyType, ValueType>;
73 using MapValueType = MapType::value_type;
80 using Iterator = std::unordered_multimap<KeyType, ValueType>::iterator;
82 std::unordered_multimap<KeyType, ValueType>::const_iterator;
101 template<
typename ObjIterator>
105 const IntersectsCellFunction& intersects =
nullptr) :
106 AbsGrid(begin, end, intersects)
114 std::ranges::begin(
r),
124 bool empty()
const {
return mMap.empty(); }
134 return mMap.find(
k) == mMap.end();
144 std::set<KeyType> keys;
145 for (
const auto&
p : mMap)
146 keys.insert(
p.first);
157 std::pair<Iterator, Iterator> valuesInCell(
const KeyType&
k)
159 auto p = mMap.equal_range(
k);
160 return std::make_pair(Iterator(
p.first), Iterator(
p.second));
163 std::pair<ConstIterator, ConstIterator> valuesInCell(
const KeyType& k)
const
165 auto p = mMap.equal_range(k);
166 return std::make_pair(ConstIterator(p.first), ConstIterator(p.second));
169 void clear() { mMap.clear(); }
171 bool eraseAllInCell(
const KeyType& k)
173 std::pair<Iterator, Iterator> range = mMap.equal_range(k);
174 if (range != mMap.end()) {
175 mMap.erase(range.first, range.second);
181 void eraseInSphere(
const Sphere<typename GridType::ScalarType>& s)
183 std::vector<ConstIterator> toDel = AbsGrid::valuesInSphere(s);
184 for (
auto& it : toDel)
188 Iterator begin() {
return mMap.begin(); }
190 ConstIterator begin()
const {
return mMap.begin(); }
192 Iterator end() {
return mMap.end(); }
194 ConstIterator end()
const {
return mMap.end(); }
197 bool insertInCell(
const KeyType& k,
const ValueType& v)
199 if constexpr (AllowDuplicates) {
204 auto range = mMap.equal_range(k);
206 for (Iterator ci = range.first; ci != range.second && !found;
208 if (ci->second == v) {
218 bool eraseInCell(
const KeyType& k,
const ValueType& v)
222 std::pair<Iterator, Iterator> range = mMap.equal_range(k);
223 for (Iterator ci = range.first; ci != range.second; ++ci) {
224 if (ci->second == v) {
227 if constexpr (!AllowDuplicates) {
240 typename ScalarType = double,
241 bool AllowDuplicates =
true>
242using HashTableGrid2 =
243 HashTableGrid<RegularGrid2<ScalarType>, ValueType, AllowDuplicates>;
247 typename ScalarType = double,
248 bool AllowDuplicates =
true>
249using HashTableGrid3 =
250 HashTableGrid<RegularGrid3<ScalarType>, ValueType, AllowDuplicates>;
The AbstractGrid class describes a generic Spatial Data Structure organized on a regular grid,...
Definition abstract_grid.h:108
bool insert(const ValueType &v)
Inserts the given element in the AbstractGrid.
Definition abstract_grid.h:183
std::function< bool(const typename GridType::BBoxType &, const RemoveCVRefAndPointer< ValueType > &)> IntersectsCellFunction
The IntersectsCellFunction type is a std::function that takes as input a bounding box and a value,...
Definition abstract_grid.h:119
The HashTableGrid class stores N-Dimensional spatial elements (that could be anything on which it can...
Definition hash_table_grid.h:55
std::set< KeyType > nonEmptyCells() const
Returns an std::set containing the cell coordinates of all the cells that contain at least one elemen...
Definition hash_table_grid.h:142
std::size_t countInCell(const KeyType &k) const
Returns the number of elements contained in the given cell.
Definition hash_table_grid.h:155
bool empty() const
Returns true if the HashTableGrid is empty (no elements in it).
Definition hash_table_grid.h:124
HashTableGrid(ObjIterator begin, ObjIterator end, const IntersectsCellFunction &intersects=nullptr)
Creates an HashTableGrid that contains all the elements that can be iterated from begin to end.
Definition hash_table_grid.h:102
bool cellEmpty(const KeyType &k) const
Returns true if the given cell coordinate does not contain elements in it.
Definition hash_table_grid.h:132
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43