EchoMap
2026-07-25 6d3977c
An experimental cross-platform digital signal processing application for sound-source localisation.
Toggle main menu visibility
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
14
namespace
echomap
15
{
16
33
template
<
typename
Key,
typename
Value>
class
BidirectionalUnorderedMapping
34
{
41
std::unordered_map<Key, Value>
forward_map
;
42
49
std::unordered_map<Value, Key>
reverse_map
;
50
51
public
:
52
using
iterator
=
decltype
(
forward_map
)
::iterator
;
53
using
const_iterator
=
decltype
(
forward_map
)
::const_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
<
116
const_iterator
,
117
bool
>
118
emplace
(
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
152
bool
erase_by_key
(
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
175
bool
erase_by_value
(
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
197
const_iterator
find_by_key
(
198
const
Key& key
199
)
const
200
{
201
return
forward_map
.find(key);
202
}
203
210
const_iterator
find_by_value
(
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
std::unordered_map::cbegin
T cbegin(T... args)
cassert
echomap::BidirectionalUnorderedMapping
Maintains a bidirectional mapping between a key and value, allowing fast lookup on both objects.
Definition
BidirectionalUnorderedMapping.hpp:34
echomap::BidirectionalUnorderedMapping::find_by_value
const_iterator find_by_value(const Value &value) const
Locates an entry in the mapping by its value.
Definition
BidirectionalUnorderedMapping.hpp:210
echomap::BidirectionalUnorderedMapping::erase_by_key
bool erase_by_key(const Key &key)
Removes an entry from the mapping by its key.
Definition
BidirectionalUnorderedMapping.hpp:152
echomap::BidirectionalUnorderedMapping::emplace
std::pair< const_iterator, bool > emplace(KeyT &&key, ValueT &&value)
Emplaces an entry to the mapping.
Definition
BidirectionalUnorderedMapping.hpp:118
echomap::BidirectionalUnorderedMapping::erase_by_value
bool erase_by_value(const Value &value)
Removes an entry from the mapping by its value.
Definition
BidirectionalUnorderedMapping.hpp:175
echomap::BidirectionalUnorderedMapping::iterator
decltype(forward_map)::iterator iterator
The bidirectional container non-constant iterator.
Definition
BidirectionalUnorderedMapping.hpp:52
echomap::BidirectionalUnorderedMapping::const_iterator
decltype(forward_map)::const_iterator const_iterator
The bidirectional container constant iterator.
Definition
BidirectionalUnorderedMapping.hpp:53
echomap::BidirectionalUnorderedMapping::clear
void clear() noexcept
Removes all entries from the mapping.
Definition
BidirectionalUnorderedMapping.hpp:90
echomap::BidirectionalUnorderedMapping::forward_map
std::unordered_map< Key, Value > forward_map
The "forward" map, mapping keys to values in the conventional way.
Definition
BidirectionalUnorderedMapping.hpp:41
echomap::BidirectionalUnorderedMapping::find_by_key
const_iterator find_by_key(const Key &key) const
Locates an entry in the mapping by its key.
Definition
BidirectionalUnorderedMapping.hpp:197
echomap::BidirectionalUnorderedMapping::reverse_map
std::unordered_map< Value, Key > reverse_map
The "reverse" map, mapping values to keys.
Definition
BidirectionalUnorderedMapping.hpp:49
std::unordered_map::end
T end(T... args)
std::forward
T forward(T... args)
echomap
The main EchoMap outermost namespace for all non-exported symbols.
Definition
ActionController.hpp:20
std::pair
unordered_map
src
objects
BidirectionalUnorderedMapping.hpp
Generated by
1.17.0