Documentation 6b54b78464
Loading...
Searching...
No Matches
CFastFindAndEraseQueue.hpp
Go to the documentation of this file.
1/*!
2 * \file CFastFindAndEraseQueue.hpp
3 * \brief A queue-type container (push back, pop front), but with
4 * fast deletion of arbitrary items (possibly in the middle).
5 * \author P. Gomes
6 * \version 8.5.0 "Harrier"
7 *
8 * SU2 Project Website: https://su2code.github.io
9 *
10 * The SU2 Project is maintained by the SU2 Foundation
11 * (http://su2foundation.org)
12 *
13 * Copyright 2012-2026, SU2 Contributors (cf. AUTHORS.md)
14 *
15 * SU2 is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
19 *
20 * SU2 is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with SU2. If not, see <http://www.gnu.org/licenses/>.
27 */
28#pragma once
29
30#include <cassert>
31#include <stdlib.h>
32#include <limits>
33#include <vector>
34#include <unordered_map>
35
36/*!
37 * \class CFastFindAndEraseQueue
38 * \ingroup Containers
39 * \brief A queue-type container (push back, pop front), but with
40 * fast deletion of arbitrary items (possibly in the middle).
41 * \param[in] ItemType_ - Type of the stored items.
42 * \param[in] ErasedValue_ - Value used to mark items for erasure.
43 * \param[in] CleanupThreshold - Number of marked items that triggers full cleanup.
44 * \note It would not be a good idea to use non-trivial item types.
45 */
46template <typename ItemType_ = unsigned long, ItemType_ ErasedValue_ = std::numeric_limits<ItemType_>::max(),
47 size_t CleanupThreshold = 8192>
49 public:
50 enum { ErasedValue = ErasedValue_ };
51 using ItemType = ItemType_;
52 using Iterator = typename std::vector<ItemType>::const_iterator;
53
54 private:
55 size_t erasedCounter = 0; /*!< \brief How many items have been marked since last cleanup. */
56 std::vector<ItemType> items; /*!< \brief The stored items. */
57 std::unordered_map<ItemType, size_t> indexes; /*!< \brief Map items to their location. */
58
59 /*!
60 * \brief Cleanup, shifts non-erased items forward, re-mapping them.
61 * \note This is the performance hotspot (due to the re-mapping).
62 */
63 void cleanup() {
64 size_t idx = 0;
65 for (auto value : items) {
66 if (value != ErasedValue) {
67 items[idx] = value;
68 indexes[value] = idx;
69 ++idx;
70 }
71 }
72 items.resize(idx);
73 erasedCounter = 0;
74 }
75
76 public:
77 /*!
78 * \brief Default construct.
79 */
81
82 /*!
83 * \brief Construct and initialize with [0,N) range.
84 */
86 items.resize(N);
87 for (size_t i = 0; i < N; ++i) {
88 items[i] = i;
89 indexes[i] = i;
90 }
91 }
92
93 /*--- Wrapper methods for stl-type operations. ---*/
94 size_t size() const { return indexes.size(); }
95 bool empty() const { return indexes.empty(); }
96 Iterator begin() const { return items.cbegin(); }
97 Iterator end() const { return items.cend(); }
98
99 /*!
100 * \brief Push back an item, that should be unique.
101 * \param[in] value - The new item.
102 */
103 void push_back(ItemType value) {
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);
108 }
109
110 /*!
111 * \brief Get the item in front of the queue.
112 */
113 ItemType front() const {
114 for (auto value : items)
115 if (value != ErasedValue) return value;
116 assert(false && "Queue is empty.");
117 return ErasedValue;
118 }
119
120 /*!
121 * \brief Main event, look for an item and erase it if present.
122 * \param[in] value - Item to look for.
123 * \return True, if something was erased, false otherwise.
124 */
125 bool findAndErase(const ItemType value) {
126 if (empty()) return false;
127
128 /*--- Get the item position. ---*/
129 auto it = indexes.find(value);
130 if (it == indexes.end()) return false; // value is not here
131
132 const auto idx = it->second;
133 assert(items[idx] != ErasedValue && "The item was already erased?!");
134
135 /*--- Erase and cleanup items. ---*/
136 indexes.erase(it);
137 items[idx] = ErasedValue;
138 if (++erasedCounter == CleanupThreshold) cleanup();
139
140 return true;
141 }
142};
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