2#include <unordered_map>
15template <
typename T,
typename U>
21 template <
typename S
ideKey,
typename S
ideValue>
34 const SideValue&
at(
const SideKey& key)
const {
35 auto it =
map.find(key);
36 if (it ==
map.end()) {
37 throw std::out_of_range(
"Key not found in BidirectionalMap::Side::at");
43 return map.count(key) > 0;
54 typename std::unordered_map<SideKey, SideValue>::const_iterator
begin()
const {
return map.begin(); }
55 typename std::unordered_map<SideKey, SideValue>::const_iterator
end()
const {
return map.end(); }
58 std::unordered_map<SideKey, SideValue>
map;
76 void insert(
const T& key,
const U& value) {
77 auto it_left_key =
left.map.find(key);
78 if (it_left_key !=
left.map.end()) {
79 U old_value = std::move(it_left_key->second);
80 left.map.erase(it_left_key);
81 right.map.erase(old_value);
84 auto it_right_value =
right.map.find(value);
85 if (it_right_value !=
right.map.end()) {
86 T old_key = std::move(it_right_value->second);
87 right.map.erase(it_right_value);
88 left.map.erase(old_key);
91 left.map.insert_or_assign( key, value );
92 right.map.insert_or_assign( value, key );
95 template <
typename KT,
typename KU>
97 auto it_left_key =
left.map.find(key);
98 if (it_left_key !=
left.map.end()) {
99 right.map.erase(it_left_key->second);
100 left.map.erase(it_left_key);
103 auto it_right_value =
right.map.find(value);
104 if (it_right_value !=
right.map.end()) {
105 left.map.erase(it_right_value->second);
106 right.map.erase(it_right_value);
109 left.map.emplace(key, value);
110 right.map.emplace(std::forward<KU>(value), std::forward<KT>(key));
114 auto it_left =
left.map.find(key);
115 if (it_left !=
left.map.end()) {
116 U value = it_left->second;
117 left.map.erase(it_left);
118 right.map.erase(value);
123 auto it_right =
right.map.find(key);
124 if (it_right !=
right.map.end()) {
125 T value = it_right->second;
126 right.map.erase(it_right);
127 left.map.erase(value);
132 left.clear_map_only();
133 right.clear_map_only();
138 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)