EchoMap 2026-07-25 6d3977c
An experimental cross-platform digital signal processing application for sound-source localisation.
Loading...
Searching...
No Matches
BidirectionalUnorderedMapping.hpp
Go to the documentation of this file.
1
7
8#ifndef ECHOMAP_BIDIRECTIONALUNORDEREDMAPPING_HPP
9#define ECHOMAP_BIDIRECTIONALUNORDEREDMAPPING_HPP
10
11#include <cassert>
12#include <unordered_map>
13
14namespace echomap
15{
16
33template <typename Key, typename Value> class BidirectionalUnorderedMapping
34{
42
50
51public:
52 using iterator = decltype(forward_map)::iterator;
54
55 [[nodiscard]] const_iterator begin() const noexcept
56 {
57 return forward_map.begin();
58 }
59
60 [[nodiscard]] const_iterator cbegin() const noexcept
61 {
62 return forward_map.cbegin();
63 }
64
65 [[nodiscard]] const_iterator end() const noexcept
66 {
67 return forward_map.end();
68 }
69
70 [[nodiscard]] const_iterator cend() const noexcept
71 {
72 return forward_map.cend();
73 }
74
75 [[nodiscard]] bool empty() const noexcept
76 {
77 return forward_map.empty();
78 }
79
80 [[nodiscard]] std::size_t size() const noexcept
81 {
82 return forward_map.size();
83 }
84
90 void clear() noexcept
91 {
92 forward_map.clear();
93 reverse_map.clear();
94
95 assert(forward_map.empty());
96 assert(reverse_map.empty());
97 }
98
112 template <
113 typename KeyT,
114 typename ValueT>
115 std::pair<
117 bool>
119 KeyT&& key,
120 ValueT&& value
121 )
122 {
123 auto forward_insertion = forward_map.emplace(std::forward<KeyT>(key), std::forward<ValueT>(value));
124 if (!forward_insertion.second)
125 return {forward_insertion.first, false};
126
127 try {
128 const auto& inserted_key = forward_insertion.first->first;
129 // ReSharper disable once CppTooWideScopeInitStatement - Intentionally wide for readability.
130 const auto& inserted_value = forward_insertion.first->second;
131
132 if (!reverse_map.emplace(inserted_value, inserted_key).second) {
133 forward_map.erase(forward_insertion.first);
134 return {forward_map.end(), false};
135 }
136 } catch (...) {
137 forward_map.erase(forward_insertion.first);
138 throw;
139 }
140
141 assert(forward_map.size() == reverse_map.size());
142 return {forward_insertion.first, true};
143 }
144
153 const Key& key
154 )
155 {
156 const auto itr = forward_map.find(key);
157 if (itr == forward_map.end())
158 return false;
159
160 reverse_map.erase(itr->second);
161 forward_map.erase(itr);
162
163 assert(forward_map.size() == reverse_map.size());
164
165 return true;
166 }
167
176 const Value& value
177 )
178 {
179 const auto itr = reverse_map.find(value);
180 if (itr == reverse_map.end())
181 return false;
182
183 forward_map.erase(itr->second);
184 reverse_map.erase(itr);
185
186 assert(forward_map.size() == reverse_map.size());
187
188 return true;
189 }
190
198 const Key& key
199 ) const
200 {
201 return forward_map.find(key);
202 }
203
211 const Value& value
212 ) const
213 {
214 const auto reverse_map_it = reverse_map.find(value);
215 if (reverse_map_it == reverse_map.end())
216 return forward_map.end();
217
218 const auto forward_map_it = forward_map.find(reverse_map_it->second);
219 assert(forward_map_it != forward_map.end()); // Verify class invariant.
220
221 return forward_map_it;
222 }
223};
224
225} // namespace echomap
226
227#endif // ECHOMAP_BIDIRECTIONALUNORDEREDMAPPING_HPP
T cbegin(T... args)
Maintains a bidirectional mapping between a key and value, allowing fast lookup on both objects.
const_iterator find_by_value(const Value &value) const
Locates an entry in the mapping by its value.
bool erase_by_key(const Key &key)
Removes an entry from the mapping by its key.
std::pair< const_iterator, bool > emplace(KeyT &&key, ValueT &&value)
Emplaces an entry to the mapping.
bool erase_by_value(const Value &value)
Removes an entry from the mapping by its value.
decltype(forward_map)::iterator iterator
The bidirectional container non-constant iterator.
decltype(forward_map)::const_iterator const_iterator
The bidirectional container constant iterator.
void clear() noexcept
Removes all entries from the mapping.
std::unordered_map< Key, Value > forward_map
The "forward" map, mapping keys to values in the conventional way.
const_iterator find_by_key(const Key &key) const
Locates an entry in the mapping by its key.
std::unordered_map< Value, Key > reverse_map
The "reverse" map, mapping values to keys.
T end(T... args)
T forward(T... args)
The main EchoMap outermost namespace for all non-exported symbols.