AutoPas  3.0.0
Loading...
Searching...
No Matches
NeighborListsBuffer.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <cstddef>
10#include <unordered_map>
11#include <vector>
12
14
15namespace autopas {
30template <class Key, class Value, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>>
32 public:
39 template <bool throwIfIndexOutOfBounds = false>
40 std::vector<Value> &getNeighborListRef(size_t index) {
41 if constexpr (throwIfIndexOutOfBounds) {
42 if (index > _lastValidListIndex) {
44 "NeighborListsBuffer::getNeighborListRef() index out of bounds: {} > {}", index, _lastValidListIndex);
45 }
46 }
47 return _neighborLists[index];
48 }
49
56 template <bool throwIfKeyIsUnknown = false>
57 std::vector<Value> &getNeighborListRef(const Key &key) {
58 if constexpr (throwIfKeyIsUnknown) {
59 if (_keyMap.find(key) == _keyMap.end()) {
60 autopas::utils::ExceptionHandler::exception("NeighborListsBuffer::getNeighborListRef() unknown key: {}", key);
61 }
62 }
63 return _neighborLists[_keyMap[key]];
64 }
65
75 // if the buffer is saturated...
76 if (_lastValidListIndex >= _neighborLists.size()) {
77 // ...grow it and initialize all new lists. Make sure to grow to at least 10 lists.
78 _neighborLists.resize(std::max(10ul, static_cast<size_t>(_neighborLists.size() * _growthFactor)),
79 std::vector<Value>(_defaultListLength));
80 }
81 ++_lastValidListIndex;
82 _neighborLists[_lastValidListIndex].clear();
83 return _lastValidListIndex;
84 }
85
94 size_t getNewNeighborList(const Key &key) {
95 const auto newIndex = getNewNeighborList();
96 _keyMap.emplace(key, newIndex);
97 return newIndex;
98 }
99
104 void clear() {
105 _keyMap.clear();
106 _lastValidListIndex = std::numeric_limits<size_t>::max();
107 }
108
115 void reserveNeighborLists(size_t n) { _neighborLists.resize(n, std::vector<Value>(_defaultListLength)); }
116
121 void setDefaultListLength(size_t defaultListLength) { NeighborListsBuffer::_defaultListLength = defaultListLength; }
122
127 void setGrowthFactor(double growthFactor) { NeighborListsBuffer::_growthFactor = growthFactor; }
128
129 private:
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};
153};
154} // namespace autopas
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