10#include <unordered_map>
30template <
class Key,
class Value,
class Hash = std::hash<Key>,
class KeyEqual = std::equal_to<Key>>
39 template <
bool throwIfIndexOutOfBounds = false>
41 if constexpr (throwIfIndexOutOfBounds) {
42 if (index > _lastValidListIndex) {
44 "NeighborListsBuffer::getNeighborListRef() index out of bounds: {} > {}", index, _lastValidListIndex);
47 return _neighborLists[index];
56 template <
bool throwIfKeyIsUnknown = false>
58 if constexpr (throwIfKeyIsUnknown) {
59 if (_keyMap.find(key) == _keyMap.end()) {
63 return _neighborLists[_keyMap[key]];
76 if (_lastValidListIndex >= _neighborLists.size()) {
78 _neighborLists.resize(std::max(10ul,
static_cast<size_t>(_neighborLists.size() * _growthFactor)),
79 std::vector<Value>(_defaultListLength));
81 ++_lastValidListIndex;
82 _neighborLists[_lastValidListIndex].clear();
83 return _lastValidListIndex;
96 _keyMap.emplace(key, newIndex);
106 _lastValidListIndex = std::numeric_limits<size_t>::max();
121 void setDefaultListLength(
size_t defaultListLength) { NeighborListsBuffer::_defaultListLength = defaultListLength; }
127 void setGrowthFactor(
double growthFactor) { NeighborListsBuffer::_growthFactor = growthFactor; }
133 std::unordered_map<Key, size_t, Hash, KeyEqual> _keyMap{};
140 std::vector<std::vector<Value>> _neighborLists{};
144 size_t _lastValidListIndex{std::numeric_limits<size_t>::max()};
148 size_t _defaultListLength{10};
152 double _growthFactor{2};
Class for manual memory management of neighbor lists.
Definition: NeighborListsBuffer.h:31
std::vector< Value > & getNeighborListRef(size_t index)
Getter for a reference to a neighbor list by index.
Definition: NeighborListsBuffer.h:40
void setDefaultListLength(size_t defaultListLength)
Set the initial length of new neighbor lists.
Definition: NeighborListsBuffer.h:121
void reserveNeighborLists(size_t n)
Resize the internal buffer so that there are new spare lists.
Definition: NeighborListsBuffer.h:115
std::vector< Value > & getNeighborListRef(const Key &key)
Getter for a reference to a neighbor list by key.
Definition: NeighborListsBuffer.h:57
size_t getNewNeighborList(const Key &key)
Assigns a neighbor list to the given key.
Definition: NeighborListsBuffer.h:94
size_t getNewNeighborList()
Reserves a neighbor list for use.
Definition: NeighborListsBuffer.h:74
void setGrowthFactor(double growthFactor)
Set the growth factor for the internal buffer.
Definition: NeighborListsBuffer.h:127
void clear()
Clears the internal key map and moves _lastValidListIndex to indicate an empty buffer.
Definition: NeighborListsBuffer.h:104
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:63
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:32