27 using key_type = remove_cvref_t<Key>;
28 using mapped_type = remove_cvref_t<T>;
29 using value_type = std::pair<key_type, mapped_type>;
30 using reference = value_type&;
31 using const_reference =
const value_type&;
34 using _data_type = std::vector<value_type>;
45 using _map_type = Map<key_type, size_type>;
46 using _inverted_map_type = Map<mapped_type, size_type>;
50 _inverted_map_type _invertedMap;
53 for (size_type i = from; i != _data.size(); ++i) {
54 _map[_data[i].first] = i;
55 _invertedMap[_data[i].second] = i;
59 template<
class UK,
class U>
61 return _insert(std::make_pair(std::forward<UK>(key), std::forward<U>(value)));
66 const key_type key = value.first;
67 const mapped_type mappedValue = value.second;
69 if (contains(key) && _data[_map.at(key)].second == mappedValue) {
return _data[_map.at(key)].second; }
72 _data.push_back(std::forward<U>(value));
75 size_t minIdx = _data.size() - 1;
77 size_t valueIdx = _map.at(key);
80 minIdx = std::min(minIdx, valueIdx);
83 _invertedMap.erase(_data[valueIdx].second);
86 _data.erase(std::next(_data.begin(), valueIdx));
92 if (contains_value(mappedValue)) {
93 size_t keyIdx = _invertedMap.at(mappedValue);
96 minIdx = std::min(keyIdx, minIdx);
99 _map.erase(_data[keyIdx].first);
102 _data.erase(std::next(_data.begin(), keyIdx));
105 _invertedMap.erase(mappedValue);
108 _update_indexes(minIdx);
110 return _data[_map.at(key)].second;
115 if (!contains(key)) {
return; }
117 size_t elementOffset = _map.at(key);
119 const iterator& itr = std::next(_data.begin(), elementOffset);
120 mapped_type value = itr->second;
122 _map.erase(std::forward<UK>(key));
123 _invertedMap.erase(value);
125 _update_indexes(elementOffset);
130 if (!contains_value(value)) {
return; }
132 size_t elementOffset = _invertedMap.at(value);
134 const iterator& itr = std::next(_data.begin(), elementOffset);
135 key_type key = itr->first;
137 _invertedMap.erase(std::forward<U>(value));
140 _update_indexes(elementOffset);
146 mstd_assert(contains(key),
"Key '{}' not found", key);
148 else {
mstd_assert(contains(key),
"Key not found"); }
149 return _data.at(_map.at(std::forward<UK>(key))).second;
155 mstd_assert(contains(key),
"Key '{}' not found", key);
157 else {
mstd_assert(contains(key),
"Key not found"); }
158 return _data.at(_map.at(std::forward<UK>(key))).second;
164 mstd_assert(contains_value(value),
"Value '{}' not found", value);
166 else {
mstd_assert(contains_value(value),
"Value not found"); }
167 return _data.at(_invertedMap.at(std::forward<U>(value))).first;
173 mstd_assert(contains_value(value),
"Value '{}' not found", value);
175 else {
mstd_assert(contains_value(value),
"Value not found"); }
176 return _data.at(_invertedMap.at(std::forward<U>(value))).first;
182 return _map.contains(std::forward<UK>(key));
184 return _map.find(std::forward<UK>(key)) != _map.end();
191 return _invertedMap.contains(std::forward<U>(value));
193 return _invertedMap.find(std::forward<U>(value)) != _invertedMap.end();
199 if (!contains(key)) {
return _emplace(std::forward<UK>(key), mapped_type()); }
200 return _at(std::forward<UK>(key));
205 auto it = _map.find(std::forward<UK>(key));
206 return it != _map.end() ? std::next(_data.begin(), it->second) : _data.end();
211 auto it = _map.find(std::forward<UK>(key));
212 return it != _map.end() ? std::next(_data.cbegin(), it->second) : _data.cend();
217 auto it = _invertedMap.find(std::forward<U>(value));
218 return it != _invertedMap.end() ? std::next(_data.begin(), it->second) : _data.end();
223 auto it = _invertedMap.find(std::forward<U>(value));
224 return it != _invertedMap.end() ? std::next(_data.cbegin(), it->second) : _data.cend();
230 _MSTD_CONSTEXPR20 bimap(std::initializer_list<value_type> init) { insert(init.begin(), init.end()); }
236 template<mstd::iterator_of<value_type> Iter>
238 template<
class Iter, std::enable_if_t<is_iterator_of_v<Iter, value_type>,
bool> =
true>
249 _MSTD_CONSTEXPR20 mapped_type& emplace(
const key_type& key,
const mapped_type& value) {
return _emplace(key, value); }
252 return _emplace(std::move(key), std::move(value));
260 template<mstd::iterator_of<value_type> Iter>
262 template<
class Iter, std::enable_if_t<is_iterator_of_v<Iter, value_type>,
bool> =
true>
265 for (Iter iter = begin; iter != end; ++iter) { insert(*iter); }
280 [[nodiscard]]
_MSTD_CONSTEXPR20 const mapped_type& at(
const key_type& key)
const {
return _at(key); }
282 [[nodiscard]]
_MSTD_CONSTEXPR20 const mapped_type& at(key_type&& key)
const {
return _at(std::move(key)); }
284 [[nodiscard]]
_MSTD_CONSTEXPR20 key_type& at_value(
const mapped_type& value) {
return _at_value(value); }
286 [[nodiscard]]
_MSTD_CONSTEXPR20 key_type& at_value(mapped_type&& value) {
return _at_value(std::move(value)); }
288 [[nodiscard]]
_MSTD_CONSTEXPR20 const key_type& at_value(
const mapped_type& value)
const {
return _at_value(value); }
291 return _at_value(std::move(value));
298 [[nodiscard]]
_MSTD_CONSTEXPR20 bool contains(
const key_type& key)
const {
return _contains(key); }
300 [[nodiscard]]
_MSTD_CONSTEXPR20 bool contains(key_type&& key)
const {
return _contains(std::move(key)); }
302 [[nodiscard]]
_MSTD_CONSTEXPR20 bool contains_value(
const mapped_type& value)
const {
return _contains_value(value); }
305 return _contains_value(std::move(value));
312 [[nodiscard]]
_MSTD_CONSTEXPR20 const_iterator find(
const key_type& key)
const {
return _find(key); }
314 [[nodiscard]]
_MSTD_CONSTEXPR20 const_iterator find(key_type&& key)
const {
return _find(std::move(key)); }
316 [[nodiscard]]
_MSTD_CONSTEXPR20 iterator find_value(
const mapped_type& value) {
return _find_value(value); }
318 [[nodiscard]]
_MSTD_CONSTEXPR20 iterator find_value(mapped_type&& value) {
return _find_value(std::move(value)); }
320 [[nodiscard]]
_MSTD_CONSTEXPR20 const_iterator find_value(
const mapped_type& value)
const {
return _find_value(value); }
323 return _find_value(std::move(value));
329 _invertedMap.clear();
348 [[nodiscard]]
_MSTD_CONSTEXPR20 const_reverse_iterator rbegin()
const {
return _data.crbegin(); }
352 [[nodiscard]]
_MSTD_CONSTEXPR20 const_reverse_iterator crbegin()
const {
return _data.crbegin(); }
357 if _MSTD_CONSTEXPR17 (std::is_default_constructible_v<mapped_type>) {
return _at_with_construct(key); }
358 else {
return _at(key); }
362 if _MSTD_CONSTEXPR17 (std::is_default_constructible_v<mapped_type>) {
return _at_with_construct(std::move(key)); }
363 else {
return _at(std::move(key)); }
366 [[nodiscard]]
_MSTD_CONSTEXPR20 const mapped_type& operator[](
const key_type& key)
const {
return _at(key); }
368 [[nodiscard]]
_MSTD_CONSTEXPR20 const mapped_type& operator[](key_type&& key)
const {
return _at(std::move(key)); }
371 return _map == other._map && _invertedMap == other._invertedMap && _data == other._data;