34#include <unordered_map>
46template <typename ItemType_ = unsigned long, ItemType_ ErasedValue_ = std::numeric_limits<ItemType_>::max(),
47 size_t CleanupThreshold = 8192>
50 enum { ErasedValue = ErasedValue_ };
52 using Iterator =
typename std::vector<ItemType>::const_iterator;
55 size_t erasedCounter = 0;
56 std::vector<ItemType> items;
57 std::unordered_map<ItemType, size_t> indexes;
65 for (
auto value : items) {
66 if (value != ErasedValue) {
87 for (
size_t i = 0; i < N; ++i) {
94 size_t size()
const {
return indexes.size(); }
95 bool empty()
const {
return indexes.empty(); }
104 assert(value != ErasedValue &&
"#bad_idea");
105 assert(!indexes.count(value) &&
"Items must be unique.");
106 indexes[value] = items.size();
107 items.push_back(value);
114 for (
auto value : items)
115 if (value != ErasedValue)
return value;
116 assert(
false &&
"Queue is empty.");
126 if (empty())
return false;
129 auto it = indexes.find(value);
130 if (it == indexes.end())
return false;
132 const auto idx = it->second;
133 assert(items[idx] != ErasedValue &&
"The item was already erased?!");
137 items[idx] = ErasedValue;
138 if (++erasedCounter == CleanupThreshold) cleanup();
A queue-type container (push back, pop front), but with fast deletion of arbitrary items (possibly in...
Definition CFastFindAndEraseQueue.hpp:48
CFastFindAndEraseQueue()=default
Default construct.
Iterator end() const
Definition CFastFindAndEraseQueue.hpp:97
CFastFindAndEraseQueue(size_t N)
Construct and initialize with [0,N) range.
Definition CFastFindAndEraseQueue.hpp:85
ItemType front() const
Get the item in front of the queue.
Definition CFastFindAndEraseQueue.hpp:113
typename std::vector< ItemType >::const_iterator Iterator
Definition CFastFindAndEraseQueue.hpp:52
ItemType_ ItemType
Definition CFastFindAndEraseQueue.hpp:51
bool empty() const
Definition CFastFindAndEraseQueue.hpp:95
bool findAndErase(const ItemType value)
Main event, look for an item and erase it if present.
Definition CFastFindAndEraseQueue.hpp:125
Iterator begin() const
Definition CFastFindAndEraseQueue.hpp:96
size_t size() const
Definition CFastFindAndEraseQueue.hpp:94
void push_back(ItemType value)
Push back an item, that should be unique.
Definition CFastFindAndEraseQueue.hpp:103