2#include <unordered_map>
6template <
typename T,
typename U>
12 template <
typename S
ideKey,
typename S
ideValue>
25 const SideValue&
at(
const SideKey& key)
const {
26 auto it =
map.find(key);
27 if (it ==
map.end()) {
28 throw std::out_of_range(
"Key not found in BidirectionalMap::Side::at");
34 return map.count(key) > 0;
45 typename std::unordered_map<SideKey, SideValue>::const_iterator
begin()
const {
return map.begin(); }
46 typename std::unordered_map<SideKey, SideValue>::const_iterator
end()
const {
return map.end(); }
49 std::unordered_map<SideKey, SideValue>
map;
67 void insert(
const T& key,
const U& value) {
68 auto it_left_key =
left.map.find(key);
69 if (it_left_key !=
left.map.end()) {
70 U old_value = std::move(it_left_key->second);
71 left.map.erase(it_left_key);
72 right.map.erase(old_value);
75 auto it_right_value =
right.map.find(value);
76 if (it_right_value !=
right.map.end()) {
77 T old_key = std::move(it_right_value->second);
78 right.map.erase(it_right_value);
79 left.map.erase(old_key);
82 left.map.insert_or_assign( key, value );
83 right.map.insert_or_assign( value, key );
86 template <
typename KT,
typename KU>
88 auto it_left_key =
left.map.find(key);
89 if (it_left_key !=
left.map.end()) {
90 right.map.erase(it_left_key->second);
91 left.map.erase(it_left_key);
94 auto it_right_value =
right.map.find(value);
95 if (it_right_value !=
right.map.end()) {
96 left.map.erase(it_right_value->second);
97 right.map.erase(it_right_value);
100 left.map.emplace(key, value);
101 right.map.emplace(std::forward<KU>(value), std::forward<KT>(key));
105 auto it_left =
left.map.find(key);
106 if (it_left !=
left.map.end()) {
107 U value = it_left->second;
108 left.map.erase(it_left);
109 right.map.erase(value);
114 auto it_right =
right.map.find(key);
115 if (it_right !=
right.map.end()) {
116 T value = it_right->second;
117 right.map.erase(it_right);
118 left.map.erase(value);
123 left.clear_map_only();
124 right.clear_map_only();
129 right.map.reserve(n);
const SideValue & at(const SideKey &key) const
Side()=default
Default constructor.
bool contains(const SideKey &key) const
std::unordered_map< T, U > map
std::unordered_map< SideKey, SideValue >::const_iterator end() const
std::unordered_map< SideKey, SideValue >::const_iterator begin() const
LeftSideType & get_left()
void emplace(KT &&key, KU &&value)
Side< U, T > RightSideType
Side< T, U > LeftSideType
void insert(const T &key, const U &value)
const LeftSideType & get_left() const
void remove_right(const U &key)
RightSideType & get_right()
BidirectionalMap()=default
const RightSideType & get_right() const
void remove_left(const T &key)