EchoMap 2026-07-25 6d3977c
An experimental cross-platform digital signal processing application for sound-source localisation.
Loading...
Searching...
No Matches
echomap::BidirectionalUnorderedMapping< Key, Value > Class Template Reference

Maintains a bidirectional mapping between a key and value, allowing fast lookup on both objects. More...

#include <BidirectionalUnorderedMapping.hpp>

Public Types

using iterator = decltype(forward_map)::iterator
 The bidirectional container non-constant iterator.
using const_iterator = decltype(forward_map)::const_iterator
 The bidirectional container constant iterator.

Public Member Functions

const_iterator begin () const noexcept
const_iterator cbegin () const noexcept
const_iterator end () const noexcept
const_iterator cend () const noexcept
bool empty () const noexcept
std::size_t size () const noexcept
void clear () noexcept
 Removes all entries from the mapping.
template<typename KeyT, typename ValueT>
std::pair< const_iterator, bool > emplace (KeyT &&key, ValueT &&value)
 Emplaces an entry to the mapping.
bool erase_by_key (const Key &key)
 Removes an entry from the mapping by its key.
bool erase_by_value (const Value &value)
 Removes an entry from the mapping by its value.
const_iterator find_by_key (const Key &key) const
 Locates an entry in the mapping by its key.
const_iterator find_by_value (const Value &value) const
 Locates an entry in the mapping by its value.

Private Attributes

std::unordered_map< Key, Value > forward_map
 The "forward" map, mapping keys to values in the conventional way.
std::unordered_map< Value, Key > reverse_map
 The "reverse" map, mapping values to keys.

Detailed Description

template<typename Key, typename Value>
class echomap::BidirectionalUnorderedMapping< Key, Value >

Maintains a bidirectional mapping between a key and value, allowing fast lookup on both objects.

Template Parameters
KeyThe first key type
ValueThe second key type, colloquially the "value".

The templated types should typically be small (such as a stable integral ID), since their values are duplicated internally across two STL unordered hashing containers.

To guarantee implementation invariants, the bidirectional mapping container does not expose mutable access to its members. Mutation of the iterators of the underlying storage containers can be done exclusively through the public interface.

Definition at line 33 of file BidirectionalUnorderedMapping.hpp.

Member Typedef Documentation

◆ const_iterator

template<typename Key, typename Value>
using echomap::BidirectionalUnorderedMapping< Key, Value >::const_iterator = decltype(forward_map)::const_iterator

The bidirectional container constant iterator.

Definition at line 53 of file BidirectionalUnorderedMapping.hpp.

◆ iterator

template<typename Key, typename Value>
using echomap::BidirectionalUnorderedMapping< Key, Value >::iterator = decltype(forward_map)::iterator

The bidirectional container non-constant iterator.

Definition at line 52 of file BidirectionalUnorderedMapping.hpp.

Member Function Documentation

◆ begin()

template<typename Key, typename Value>
const_iterator echomap::BidirectionalUnorderedMapping< Key, Value >::begin ( ) const
inlinenodiscardnoexcept

Definition at line 55 of file BidirectionalUnorderedMapping.hpp.

56 {
57 return forward_map.begin();
58 }
std::unordered_map< Key, Value > forward_map
The "forward" map, mapping keys to values in the conventional way.

◆ cbegin()

template<typename Key, typename Value>
const_iterator echomap::BidirectionalUnorderedMapping< Key, Value >::cbegin ( ) const
inlinenodiscardnoexcept

Definition at line 60 of file BidirectionalUnorderedMapping.hpp.

61 {
62 return forward_map.cbegin();
63 }

◆ cend()

template<typename Key, typename Value>
const_iterator echomap::BidirectionalUnorderedMapping< Key, Value >::cend ( ) const
inlinenodiscardnoexcept

Definition at line 70 of file BidirectionalUnorderedMapping.hpp.

71 {
72 return forward_map.cend();
73 }

◆ clear()

template<typename Key, typename Value>
void echomap::BidirectionalUnorderedMapping< Key, Value >::clear ( )
inlinenoexcept

Removes all entries from the mapping.

Postcondition
All internal containers are cleared.

Definition at line 90 of file BidirectionalUnorderedMapping.hpp.

91 {
92 forward_map.clear();
93 reverse_map.clear();
94
95 assert(forward_map.empty());
96 assert(reverse_map.empty());
97 }
Maintains a bidirectional mapping between a key and value, allowing fast lookup on both objects.
std::unordered_map< Value, Key > reverse_map
The "reverse" map, mapping values to keys.

◆ emplace()

template<typename Key, typename Value>
template<typename KeyT, typename ValueT>
std::pair< const_iterator, bool > echomap::BidirectionalUnorderedMapping< Key, Value >::emplace ( KeyT && key,
ValueT && value )
inline

Emplaces an entry to the mapping.

Template Parameters
KeyTType of the forward key
ValueTType of the forward value
Parameters
keyKey from the forward perspective
valueValue from the forward perspective
Returns
  1. Constant iterator to the inserted element, or the end iterator if the insertion failed.
  2. Did an insertion take place?

Definition at line 118 of file BidirectionalUnorderedMapping.hpp.

122 {
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 }

◆ empty()

template<typename Key, typename Value>
bool echomap::BidirectionalUnorderedMapping< Key, Value >::empty ( ) const
inlinenodiscardnoexcept

Definition at line 75 of file BidirectionalUnorderedMapping.hpp.

76 {
77 return forward_map.empty();
78 }

◆ end()

template<typename Key, typename Value>
const_iterator echomap::BidirectionalUnorderedMapping< Key, Value >::end ( ) const
inlinenodiscardnoexcept

Definition at line 65 of file BidirectionalUnorderedMapping.hpp.

66 {
67 return forward_map.end();
68 }

◆ erase_by_key()

template<typename Key, typename Value>
bool echomap::BidirectionalUnorderedMapping< Key, Value >::erase_by_key ( const Key & key)
inline

Removes an entry from the mapping by its key.

Parameters
keyThe key of the entry to remove.
Returns
Was an element removed?
Postcondition
All internal containers meet the size invariant.

Definition at line 152 of file BidirectionalUnorderedMapping.hpp.

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 }

◆ erase_by_value()

template<typename Key, typename Value>
bool echomap::BidirectionalUnorderedMapping< Key, Value >::erase_by_value ( const Value & value)
inline

Removes an entry from the mapping by its value.

Parameters
valueThe value of the entry to remove.
Returns
Was an element removed?
Postcondition
All internal containers meet the size invariant.

Definition at line 175 of file BidirectionalUnorderedMapping.hpp.

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 }

◆ find_by_key()

template<typename Key, typename Value>
const_iterator echomap::BidirectionalUnorderedMapping< Key, Value >::find_by_key ( const Key & key) const
inline

Locates an entry in the mapping by its key.

Parameters
keyThe key of the entry to locate.
Returns
A constant iterator to the located element, or the end iterator of the container if no element was found.

Definition at line 197 of file BidirectionalUnorderedMapping.hpp.

200 {
201 return forward_map.find(key);
202 }

◆ find_by_value()

template<typename Key, typename Value>
const_iterator echomap::BidirectionalUnorderedMapping< Key, Value >::find_by_value ( const Value & value) const
inline

Locates an entry in the mapping by its value.

Parameters
valueThe value of the entry to locate.
Returns
A constant iterator to the located element, or the end iterator of the container if no element was found.

Definition at line 210 of file BidirectionalUnorderedMapping.hpp.

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 }

◆ size()

template<typename Key, typename Value>
std::size_t echomap::BidirectionalUnorderedMapping< Key, Value >::size ( ) const
inlinenodiscardnoexcept

Definition at line 80 of file BidirectionalUnorderedMapping.hpp.

81 {
82 return forward_map.size();
83 }

Member Data Documentation

◆ forward_map

template<typename Key, typename Value>
std::unordered_map<Key, Value> echomap::BidirectionalUnorderedMapping< Key, Value >::forward_map
private

The "forward" map, mapping keys to values in the conventional way.

Invariant
Every K-V entry in the forward map relates to a corresponding V-K entry in the reverse map.
The forward map contains no excess elements than those in the reverse map.

Definition at line 41 of file BidirectionalUnorderedMapping.hpp.

◆ reverse_map

template<typename Key, typename Value>
std::unordered_map<Value, Key> echomap::BidirectionalUnorderedMapping< Key, Value >::reverse_map
private

The "reverse" map, mapping values to keys.

Invariant
Every V-K entry in the reverse map relates to a corresponding K-V entry in the forward map.
The reverse map contains no excess elements than those in the forward map.

Definition at line 49 of file BidirectionalUnorderedMapping.hpp.


The documentation for this class was generated from the following file: