29#ifndef _GLIBCXX_FLAT_MAP
30#define _GLIBCXX_FLAT_MAP 1
33#pragma GCC system_header
36#define __glibcxx_want_flat_map
39#ifdef __cpp_lib_flat_map
57namespace std _GLIBCXX_VISIBILITY(default)
59_GLIBCXX_BEGIN_NAMESPACE_VERSION
61 template<
typename _Key,
typename _Tp,
typename _Compare,
62 typename _KeyContainer,
typename _MappedContainer>
65 template<
typename _Key,
typename _Tp,
typename _Compare,
66 typename _KeyContainer,
typename _MappedContainer>
69 template<
typename _Key,
typename _Tp,
typename _Compare,
70 typename _KeyContainer,
typename _MappedContainer,
bool _Multi>
73 static_assert(is_same_v<_Key, typename _KeyContainer::value_type>);
74 static_assert(is_same_v<_Tp, typename _MappedContainer::value_type>);
76 static_assert(is_nothrow_swappable_v<_KeyContainer>);
77 static_assert(is_nothrow_swappable_v<_MappedContainer>);
79 using _Derived = __conditional_t<_Multi,
80 flat_multimap<_Key, _Tp, _Compare,
81 _KeyContainer, _MappedContainer>,
82 flat_map<_Key, _Tp, _Compare,
83 _KeyContainer, _MappedContainer>>;
84 using __sorted_t = __conditional_t<_Multi, sorted_equivalent_t, sorted_unique_t>;
87 template<
bool _Const>
struct _Iterator;
89 using key_type = _Key;
90 using mapped_type = _Tp;
91 using value_type = pair<key_type, mapped_type>;
92 using key_compare = _Compare;
93 using reference = pair<const key_type&, mapped_type&>;
94 using const_reference = pair<const key_type&, const mapped_type&>;
95 using size_type = size_t;
96 using difference_type = ptrdiff_t;
97 using iterator = _Iterator<false>;
98 using const_iterator = _Iterator<true>;
99 using reverse_iterator = std::reverse_iterator<iterator>;
100 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
101 using key_container_type = _KeyContainer;
102 using mapped_container_type = _MappedContainer;
105 using __emplace_result_t = __conditional_t<_Multi, iterator, pair<iterator, bool>>;
110 [[no_unique_address]] key_compare _M_comp;
111 value_compare(key_compare __c) : _M_comp(__c) { }
115 operator()(const_reference __x, const_reference __y)
const
116 {
return _M_comp(__x.first, __y.first); }
118 friend _Flat_map_impl;
123 key_container_type keys;
124 mapped_container_type values;
132 _ClearGuard(containers& __cont)
140 _M_cont->keys.clear();
141 _M_cont->values.clear();
147 { _M_cont =
nullptr; }
151 _M_make_clear_guard()
152 {
return _ClearGuard{this->_M_cont}; }
156 _Flat_map_impl() : _Flat_map_impl(key_compare()) { }
159 _Flat_map_impl(
const key_compare& __comp)
160 : _M_cont(), _M_comp(__comp)
163 _Flat_map_impl(key_container_type __key_cont,
164 mapped_container_type __mapped_cont,
165 const key_compare& __comp = key_compare())
166 : _M_cont(std::
move(__key_cont), std::
move(__mapped_cont)), _M_comp(__comp)
168 __glibcxx_assert(_M_cont.keys.size() == _M_cont.values.size());
172 _Flat_map_impl(__sorted_t,
173 key_container_type __key_cont,
174 mapped_container_type __mapped_cont,
175 const key_compare& __comp = key_compare())
176 : _M_cont(std::
move(__key_cont), std::
move(__mapped_cont)), _M_comp(__comp)
178 __glibcxx_assert(_M_cont.keys.size() == _M_cont.values.size());
179 _GLIBCXX_DEBUG_ASSERT(ranges::is_sorted(_M_cont.keys, _M_comp));
182 template<__has_input_iter_cat _InputIterator>
183 _Flat_map_impl(_InputIterator __first, _InputIterator __last,
184 const key_compare& __comp = key_compare())
185 : _M_cont(), _M_comp(__comp)
186 { insert(__first, __last); }
188 template<__has_input_iter_cat _InputIterator>
189 _Flat_map_impl(__sorted_t __s,
190 _InputIterator __first, _InputIterator __last,
191 const key_compare& __comp = key_compare())
192 : _M_cont(), _M_comp(__comp)
193 { insert(__s, __first, __last); }
195 template<__detail::__container_compatible_range<value_type> _Rg>
196 _Flat_map_impl(from_range_t, _Rg&& __rg)
197 : _Flat_map_impl(from_range, std::
forward<_Rg>(__rg), key_compare())
200 template<__detail::__container_compatible_range<value_type> _Rg>
201 _Flat_map_impl(from_range_t, _Rg&& __rg,
const key_compare& __comp)
202 : _Flat_map_impl(__comp)
205 _Flat_map_impl(initializer_list<value_type> __il,
206 const key_compare& __comp = key_compare())
207 : _Flat_map_impl(__il.
begin(), __il.
end(), __comp)
210 _Flat_map_impl(__sorted_t __s,
211 initializer_list<value_type> __il,
212 const key_compare& __comp = key_compare())
213 : _Flat_map_impl(__s, __il.
begin(), __il.
end(), __comp)
218 template<__allocator_for<key_container_type, mapped_container_type> _Alloc>
220 _Flat_map_impl(
const _Alloc& __a)
221 : _Flat_map_impl(key_compare(), __a)
224 template<__allocator_for<key_container_type, mapped_container_type> _Alloc>
225 _Flat_map_impl(
const key_compare& __comp,
const _Alloc& __a)
226 : _M_cont(std::make_obj_using_allocator<key_container_type>(__a),
227 std::make_obj_using_allocator<mapped_container_type>(__a)),
231 template<__allocator_for<key_container_type, mapped_container_type> _Alloc>
232 _Flat_map_impl(
const key_container_type& __key_cont,
233 const mapped_container_type& __mapped_cont,
235 : _Flat_map_impl(__key_cont, __mapped_cont, key_compare(), __a)
238 template<__allocator_for<key_container_type, mapped_container_type> _Alloc>
239 _Flat_map_impl(
const key_container_type& __key_cont,
240 const mapped_container_type& __mapped_cont,
241 const key_compare& __comp,
243 : _M_cont(std::make_obj_using_allocator<key_container_type>(__a, __key_cont),
244 std::make_obj_using_allocator<mapped_container_type>(__a, __mapped_cont)),
247 __glibcxx_assert(_M_cont.keys.size() == _M_cont.values.size());
251 template<__allocator_for<key_container_type, mapped_container_type> _Alloc>
252 _Flat_map_impl(__sorted_t __s,
253 const key_container_type& __key_cont,
254 const mapped_container_type& __mapped_cont,
256 : _Flat_map_impl(__s, __key_cont, __mapped_cont, key_compare(), __a)
259 template<__allocator_for<key_container_type, mapped_container_type> _Alloc>
260 _Flat_map_impl(__sorted_t,
261 const key_container_type& __key_cont,
262 const mapped_container_type& __mapped_cont,
263 const key_compare& __comp,
265 : _M_cont(std::make_obj_using_allocator<key_container_type>(__a, __key_cont),
266 std::make_obj_using_allocator<mapped_container_type>(__a, __mapped_cont)),
269 __glibcxx_assert(_M_cont.keys.size() == _M_cont.values.size());
270 _GLIBCXX_DEBUG_ASSERT(ranges::is_sorted(_M_cont.keys, _M_comp));
273 template<__allocator_for<key_container_type, mapped_container_type> _Alloc>
274 _Flat_map_impl(
const _Derived& __x,
const _Alloc& __a)
275 : _M_cont(std::make_obj_using_allocator<key_container_type>(__a, __x._M_cont.keys),
276 std::make_obj_using_allocator<mapped_container_type>(__a, __x._M_cont.values)),
280 template<__allocator_for<key_container_type, mapped_container_type> _Alloc>
281 _Flat_map_impl(_Derived&& __x,
const _Alloc& __a)
282 : _M_cont(std::make_obj_using_allocator<key_container_type>
283 (__a, std::
move(__x._M_cont.keys)),
284 std::make_obj_using_allocator<mapped_container_type>
285 (__a, std::
move(__x._M_cont.values))),
289 template<__has_input_iter_cat _InputIterator,
290 __allocator_for<key_container_type, mapped_container_type> _Alloc>
291 _Flat_map_impl(_InputIterator __first, _InputIterator __last,
293 : _Flat_map_impl(std::
move(__first), std::
move(__last), key_compare(), __a)
296 template<__has_input_iter_cat _InputIterator,
297 __allocator_for<key_container_type, mapped_container_type> _Alloc>
298 _Flat_map_impl(_InputIterator __first, _InputIterator __last,
299 const key_compare& __comp,
301 : _Flat_map_impl(__comp, __a)
302 { insert(__first, __last); }
304 template<__has_input_iter_cat _InputIterator,
305 __allocator_for<key_container_type, mapped_container_type> _Alloc>
306 _Flat_map_impl(__sorted_t __s,
307 _InputIterator __first, _InputIterator __last,
309 : _Flat_map_impl(__s, std::
move(__first), std::
move(__last), key_compare(), __a)
312 template<__has_input_iter_cat _InputIterator,
313 __allocator_for<key_container_type, mapped_container_type> _Alloc>
314 _Flat_map_impl(__sorted_t __s,
315 _InputIterator __first, _InputIterator __last,
316 const key_compare& __comp,
318 : _Flat_map_impl(__comp, __a)
319 { insert(__s, __first, __last); }
321 template<__detail::__container_compatible_range<value_type> _Rg,
322 __allocator_for<key_container_type, mapped_container_type> _Alloc>
323 _Flat_map_impl(from_range_t, _Rg&& __rg,
325 : _Flat_map_impl(from_range, std::
forward<_Rg>(__rg), key_compare(), __a)
328 template<__detail::__container_compatible_range<value_type> _Rg,
329 __allocator_for<key_container_type, mapped_container_type> _Alloc>
330 _Flat_map_impl(from_range_t, _Rg&& __rg,
const key_compare& __comp,
332 : _Flat_map_impl(__comp, __a)
335 template<__allocator_for<key_container_type, mapped_container_type> _Alloc>
336 _Flat_map_impl(initializer_list<value_type> __il,
338 : _Flat_map_impl(__il, key_compare(), __a)
341 template<__allocator_for<key_container_type, mapped_container_type> _Alloc>
342 _Flat_map_impl(initializer_list<value_type> __il,
343 const key_compare& __comp,
345 : _Flat_map_impl(__il.
begin(), __il.
end(), __comp, __a)
348 template<__allocator_for<key_container_type, mapped_container_type> _Alloc>
349 _Flat_map_impl(__sorted_t __s,
350 initializer_list<value_type> __il,
352 : _Flat_map_impl(__s, __il.
begin(), __il.
end(), key_compare(), __a)
355 template<__allocator_for<key_container_type, mapped_container_type> _Alloc>
356 _Flat_map_impl(__sorted_t __s,
357 initializer_list<value_type> __il,
358 const key_compare& __comp,
360 : _Flat_map_impl(__s, __il.
begin(), __il.
end(), __comp, __a)
364 operator=(initializer_list<value_type> __il)
368 return static_cast<_Derived&
>(*this);
374 {
return {
this, _M_cont.keys.cbegin()}; }
377 begin() const noexcept
378 {
return {
this, _M_cont.keys.cbegin()}; }
382 {
return {
this, _M_cont.keys.cend()}; }
386 {
return {
this, _M_cont.keys.cend()}; }
390 {
return reverse_iterator(
end()); }
392 const_reverse_iterator
394 {
return const_reverse_iterator(
end()); }
398 {
return reverse_iterator(
begin()); }
400 const_reverse_iterator
401 rend() const noexcept
402 {
return const_reverse_iterator(
begin()); }
406 {
return {
this, _M_cont.keys.cbegin()}; }
409 cend() const noexcept
410 {
return {
this, _M_cont.keys.cend()}; }
412 const_reverse_iterator
414 {
return const_reverse_iterator(
cend()); }
416 const_reverse_iterator
417 crend() const noexcept
418 {
return const_reverse_iterator(
cbegin()); }
423 empty() const noexcept
424 {
return _M_cont.keys.empty(); }
427 size() const noexcept
428 {
return _M_cont.keys.size(); }
431 max_size() const noexcept
438 template<
typename _Key2,
typename... _Args>
440 _M_try_emplace(optional<const_iterator> __hint, _Key2&& __k, _Args&&... __args)
443 typename key_container_type::iterator __key_it;
444 typename mapped_container_type::iterator __value_it;
445 int __r = -1, __s = -1;
446 if (__hint.has_value()
448 || (__r = !_M_comp(__k, (*__hint)[-1].first)))
450 || (__s = !_M_comp((*__hint)[0].first, __k))))
452 __key_it = _M_cont.keys.begin() + __hint->_M_index;
453 if constexpr (!_Multi)
454 if (__r == 1 && !_M_comp(__key_it[-1], __k))
455 return {iterator{
this, __key_it - 1},
false};
459 auto __first = _M_cont.keys.begin();
460 auto __last = _M_cont.keys.end();
462 __first += __hint->_M_index;
464 __last = __first + __hint->_M_index;
465 if constexpr (_Multi)
469 __key_it = std::lower_bound(__first, __last, __k, _M_comp);
474 __k, std::not_fn(_M_comp)).base();
477 __key_it = std::lower_bound(__first, __last, __k, _M_comp);
480 if constexpr (!_Multi)
481 if (__key_it != _M_cont.keys.end() && !_M_comp(__k, __key_it[0]))
482 return {iterator{
this, __key_it},
false};
484 auto __guard = _M_make_clear_guard();
485 __key_it = _M_cont.keys.insert(__key_it,
std::move(__k));
486 __value_it = _M_cont.values.begin() + (__key_it - _M_cont.keys.begin());
488 __guard._M_disable();
489 return {iterator{
this, __key_it},
true};
492 template<
typename... _Args>
493 requires is_constructible_v<value_type, _Args...>
495 emplace(_Args&&... __args)
499 if constexpr (_Multi)
505 template<
typename... _Args>
507 emplace_hint(const_iterator __position, _Args&&... __args)
514 insert(
const value_type& __x)
515 {
return emplace(__x); }
518 insert(value_type&& __x)
522 insert(const_iterator __position,
const value_type& __x)
523 {
return emplace_hint(__position, __x); }
526 insert(const_iterator __position, value_type&& __x)
527 {
return emplace_hint(__position,
std::move(__x)); }
529 template<
typename _Arg>
530 requires is_constructible_v<value_type, _Arg>
535 template<
typename _Arg>
536 requires is_constructible_v<value_type, _Arg>
538 insert(const_iterator __position, _Arg&& __x)
542 template<
typename _Iter,
typename _Sent>
544 _M_insert(_Iter __first, _Sent __last)
551 auto __guard = _M_make_clear_guard();
553 for (; __first != __last; ++__first)
555 value_type __value = *__first;
556 _M_cont.keys.emplace_back(
std::move(__value.first));
557 _M_cont.values.emplace_back(
std::move(__value.second));
559 auto __zv = views::zip(_M_cont.keys, _M_cont.values);
560 ranges::sort(__zv.begin() + __n, __zv.end(), value_comp());
561 ranges::inplace_merge(__zv.begin(), __zv.begin() + __n, __zv.end(), value_comp());
562 if constexpr (!_Multi)
564 __guard._M_cont =
nullptr;
566 auto __guard = _M_make_clear_guard();
567 for (; __first != __last; ++__first)
569 value_type __value = *__first;
570 _M_cont.keys.emplace_back(
std::move(__value.first));
571 _M_cont.values.emplace_back(
std::move(__value.second));
574 __guard._M_disable();
579 template<__has_input_iter_cat _InputIterator>
581 insert(_InputIterator __first, _InputIterator __last)
584 template<__has_input_iter_cat _InputIterator>
586 insert(__sorted_t, _InputIterator __first, _InputIterator __last)
592 template<__detail::__container_compatible_range<value_type> _Rg>
594 insert_range(_Rg&& __rg)
595 { _M_insert(ranges::begin(__rg), ranges::end(__rg)); }
598 insert(initializer_list<value_type> __il)
599 { insert(__il.begin(), __il.end()); }
602 insert(__sorted_t __s, initializer_list<value_type> __il)
603 { insert(__s, __il.begin(), __il.end()); }
608 auto __guard = _M_make_clear_guard();
613 replace(key_container_type&& __key_cont, mapped_container_type&& __mapped_cont)
615 __glibcxx_assert(__key_cont.size() == __mapped_cont.size());
616 _GLIBCXX_DEBUG_ASSERT(ranges::is_sorted(__key_cont, _M_comp));
617 auto __guard = _M_make_clear_guard();
619 _M_cont.values =
std::move(__mapped_cont);
620 __guard._M_disable();
626 erase(iterator __position)
627 {
return erase(
static_cast<const_iterator
>(__position)); }
630 erase(const_iterator __position)
632 auto __guard = _M_make_clear_guard();
633 auto __idx = __position._M_index;
634 auto __it = _M_cont.keys.erase(_M_cont.keys.begin() + __idx);
635 _M_cont.values.erase(_M_cont.values.begin() + __idx);
636 __guard._M_disable();
637 return iterator{
this, __it};
641 erase(
const key_type& __x)
642 {
return erase<const key_type&>(__x); }
644 template<
typename _Key2>
645 requires same_as<remove_cvref_t<_Key2>, _Key>
646 || (__transparent_comparator<_Compare>
647 && !is_convertible_v<_Key2, iterator>
648 && !is_convertible_v<_Key2, const_iterator>)
653 auto __n = __last - __first;
654 erase(__first, __last);
659 erase(const_iterator __first, const_iterator __last)
661 auto __guard = _M_make_clear_guard();
662 auto __it = _M_cont.keys.erase(_M_cont.keys.begin() + __first._M_index,
663 _M_cont.keys.begin() + __last._M_index);
664 _M_cont.values.erase(_M_cont.values.begin() + __first._M_index,
665 _M_cont.values.begin() + __last._M_index);
666 __guard._M_disable();
667 return iterator{
this, __it};
671 swap(_Derived& __y)
noexcept
673 ranges::swap(_M_comp, __y._M_comp);
674 ranges::swap(_M_cont.keys, __y._M_cont.keys);
675 ranges::swap(_M_cont.values, __y._M_cont.values);
681 _M_cont.keys.clear();
682 _M_cont.values.clear();
694 {
return value_compare(_M_comp); }
697 const key_container_type&
698 keys() const noexcept
699 {
return _M_cont.keys; }
702 const mapped_container_type&
703 values() const noexcept
704 {
return _M_cont.values; }
709 find(
const key_type& __x)
710 {
return find<key_type>(__x); }
714 find(
const key_type& __x)
const
715 {
return find<key_type>(__x); }
717 template<
typename _Key2>
718 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
721 find(
const _Key2& __x)
723 auto __it = lower_bound(__x);
724 if (__it !=
end() && !_M_comp(__x, __it->first))
730 template<
typename _Key2>
731 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
734 find(
const _Key2& __x)
const
736 auto __it = lower_bound(__x);
737 if (__it !=
cend() && !_M_comp(__x, __it->first))
745 count(
const key_type& __x)
const
746 {
return count<key_type>(__x); }
748 template<
typename _Key2>
749 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
752 count(
const _Key2& __x)
const
754 if constexpr (!_Multi)
755 return contains<_Key2>(__x);
758 auto [__first, __last] = equal_range(__x);
759 return __last - __first;
765 contains(
const key_type& __x)
const
766 {
return contains<key_type>(__x); }
768 template<
typename _Key2>
769 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
772 contains(
const _Key2& __x)
const
773 {
return find(__x) !=
cend(); }
777 lower_bound(
const key_type& __x)
778 {
return lower_bound<key_type>(__x); }
782 lower_bound(
const key_type& __x)
const
783 {
return lower_bound<key_type>(__x); }
785 template<
typename _Key2>
786 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
789 lower_bound(
const _Key2& __x)
791 auto __it = std::lower_bound(_M_cont.keys.begin(), _M_cont.keys.end(),
796 template<
typename _Key2>
797 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
800 lower_bound(
const _Key2& __x)
const
802 auto __it = std::lower_bound(_M_cont.keys.begin(), _M_cont.keys.end(),
809 upper_bound(
const key_type& __x)
810 {
return upper_bound<key_type>(__x); }
814 upper_bound(
const key_type& __x)
const
815 {
return upper_bound<key_type>(__x); }
817 template<
typename _Key2>
818 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
821 upper_bound(
const _Key2& __x)
823 auto __it = std::upper_bound(_M_cont.keys.begin(), _M_cont.keys.end(),
828 template<
typename _Key2>
829 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
832 upper_bound(
const _Key2& __x)
const
834 auto __it = std::upper_bound(_M_cont.keys.begin(), _M_cont.keys.end(),
840 pair<iterator, iterator>
841 equal_range(
const key_type& __x)
842 {
return equal_range<key_type>(__x); }
845 pair<const_iterator, const_iterator>
846 equal_range(
const key_type& __x)
const
847 {
return equal_range<key_type>(__x); }
849 template<
typename _Key2>
850 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
852 pair<iterator, iterator>
853 equal_range(
const _Key2& __x)
855 auto [__first, __last] = std::equal_range(_M_cont.keys.begin(),
858 return {{
this, __first}, {
this, __last}};
861 template<
typename _Key2>
862 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
864 pair<const_iterator, const_iterator>
865 equal_range(
const _Key2& __x)
const
867 auto [__first, __last] = std::equal_range(_M_cont.keys.begin(),
870 return {{
this, __first}, {
this, __last}};
875 operator==(
const _Derived& __x,
const _Derived& __y)
876 {
return std::equal(__x.begin(), __x.end(), __y.begin(), __y.end()); }
878 template<
typename _Up = value_type>
880 friend __detail::__synth3way_t<_Up>
881 operator<=>(
const _Derived& __x,
const _Derived& __y)
884 __y.begin(), __y.end(),
885 __detail::__synth3way);
889 swap(_Derived& __x, _Derived& __y)
noexcept
890 {
return __x.swap(__y); }
892 template<
typename _Predicate>
894 _M_erase_if(_Predicate __pred)
896 auto __guard = _M_make_clear_guard();
897 auto __zv = views::zip(_M_cont.keys, _M_cont.values);
898 auto __sr = ranges::remove_if(__zv, __pred);
899 auto __erased = __sr.size();
900 erase(
end() - __erased,
end());
901 __guard._M_disable();
907 [[no_unique_address]] _Compare _M_comp;
912 auto __zv = views::zip(_M_cont.keys, _M_cont.values);
913 ranges::sort(__zv, value_comp());
914 if constexpr (!_Multi)
919 _M_unique()
requires (!_Multi)
923 __key_equiv(key_compare __c) : _M_comp(__c) { }
926 operator()(const_reference __x, const_reference __y)
const
927 {
return !_M_comp(__x.first, __y.first) && !_M_comp(__y.first, __x.first); }
929 [[no_unique_address]] key_compare _M_comp;
932 auto __zv = views::zip(_M_cont.keys, _M_cont.values);
933 auto __it = ranges::unique(__zv, __key_equiv(_M_comp)).begin();
934 auto __n = __it - __zv.begin();
935 _M_cont.keys.erase(_M_cont.keys.begin() + __n, _M_cont.keys.end());
936 _M_cont.values.erase(_M_cont.values.begin() + __n, _M_cont.values.end());
940 template<
typename _Key,
typename _Tp,
typename _Compare,
941 typename _KeyContainer,
typename _MappedContainer,
bool _Multi>
942 template<
bool _Const>
943 class _Flat_map_impl<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer, _Multi>::_Iterator
945 using __size_type =
typename _Flat_map_impl::size_type;
948 using iterator_category = input_iterator_tag;
949 using iterator_concept = random_access_iterator_tag;
950 using value_type = pair<const key_type, mapped_type>;
951 using reference =
pair<
const key_type&,
952 ranges::__maybe_const_t<_Const, mapped_type>&>;
953 using difference_type = ptrdiff_t;
955 _Iterator() =
default;
957 _Iterator(_Iterator<!_Const> __it)
requires _Const
958 : _M_cont(__it._M_cont), _M_index(__it._M_index)
964 __glibcxx_assert(_M_index < _M_cont->keys.size());
965 return {_M_cont->keys[_M_index], _M_cont->values[_M_index]};
973 operator->() const noexcept
982 operator[](difference_type __n)
const noexcept
983 {
return *(*
this + __n); }
986 operator++() noexcept
993 operator--() noexcept
1000 operator++(
int)
noexcept
1008 operator--(
int)
noexcept
1016 operator+=(difference_type __n)
noexcept
1023 operator-=(difference_type __n)
noexcept
1030 friend _Flat_map_impl;
1031 friend _Iterator<!_Const>;
1033 _Iterator(_Flat_map_impl* __fm,
typename key_container_type::const_iterator __it)
1035 : _M_cont(std::
__addressof(__fm->_M_cont)), _M_index(__it - __fm->keys().
cbegin())
1038 _Iterator(
const _Flat_map_impl* __fm,
typename key_container_type::const_iterator __it)
1040 : _M_cont(
std::__addressof(__fm->_M_cont)), _M_index(__it - __fm->keys().cbegin())
1044 operator+(_Iterator __it, difference_type __n)
noexcept
1051 operator+(difference_type __n, _Iterator __it)
noexcept
1058 operator-(_Iterator __it, difference_type __n)
noexcept
1064 friend difference_type
1065 operator-(
const _Iterator& __x,
const _Iterator& __y)
noexcept
1067 __glibcxx_assert(__x._M_cont == __y._M_cont);
1068 return __x._M_index - __y._M_index;
1072 operator==(
const _Iterator& __x,
const _Iterator& __y)
noexcept
1074 __glibcxx_assert(__x._M_cont == __y._M_cont);
1075 __glibcxx_assert((__x._M_index ==
size_t(-1)) == (__y._M_index ==
size_t(-1)));
1076 return __x._M_index == __y._M_index;
1079 friend strong_ordering
1080 operator<=>(
const _Iterator& __x,
const _Iterator& __y)
1082 __glibcxx_assert(__x._M_cont == __y._M_cont);
1083 __glibcxx_assert((__x._M_index ==
size_t(-1)) == (__y._M_index ==
size_t(-1)));
1084 return __x._M_index <=> __y._M_index;
1087 ranges::__maybe_const_t<_Const, containers>* _M_cont =
nullptr;
1088 __size_type _M_index = -1;
1095 template<
typename _Key,
typename _Tp,
typename _Compare = less<_Key>,
1096 typename _KeyContainer = vector<_Key>,
1097 typename _MappedContainer = vector<_Tp>>
1099 :
private _Flat_map_impl<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer, false>
1101 using _Impl = _Flat_map_impl<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer, false>;
1106 using typename _Impl::key_type;
1107 using typename _Impl::mapped_type;
1108 using typename _Impl::value_type;
1109 using typename _Impl::key_compare;
1110 using typename _Impl::reference;
1111 using typename _Impl::const_reference;
1112 using typename _Impl::size_type;
1113 using typename _Impl::difference_type;
1114 using typename _Impl::iterator;
1115 using typename _Impl::const_iterator;
1116 using typename _Impl::reverse_iterator;
1117 using typename _Impl::const_reverse_iterator;
1118 using typename _Impl::key_container_type;
1119 using typename _Impl::mapped_container_type;
1120 using typename _Impl::value_compare;
1121 using typename _Impl::containers;
1129 using _Impl::rbegin;
1132 using _Impl::cbegin;
1134 using _Impl::crbegin;
1140 using _Impl::max_size;
1144 operator[](
const key_type& __x)
1145 {
return operator[]<
const key_type>(__x); }
1148 operator[](key_type&& __x)
1149 {
return operator[]<key_type>(
std::move(__x)); }
1151 template<
typename _Key2>
1152 requires same_as<remove_cvref_t<_Key2>, _Key> || __transparent_comparator<_Compare>
1154 operator[](_Key2&& __x)
1158 at(
const key_type& __x)
1159 {
return at<key_type>(__x); }
1162 at(
const key_type& __x)
const
1163 {
return at<key_type>(__x); }
1165 template<
typename _Key2>
1166 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
1168 at(
const _Key2& __x)
1170 auto __it = this->find(__x);
1171 if (__it == this->
end())
1172 __throw_out_of_range(
"flat_map::at");
1173 return __it->second;
1176 template<
typename _Key2>
1177 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
1179 at(
const _Key2& __x)
const
1181 auto __it = this->find(__x);
1182 if (__it == this->
end())
1183 __throw_out_of_range(
"flat_map::at");
1184 return __it->second;
1188 using _Impl::emplace;
1189 using _Impl::emplace_hint;
1190 using _Impl::insert;
1191 using _Impl::insert_range;
1192 using _Impl::extract;
1193 using _Impl::replace;
1198 template<
typename... _Args>
1199 requires is_constructible_v<mapped_type, _Args...>
1200 pair<iterator, bool>
1201 try_emplace(
const key_type& __k, _Args&&... __args)
1204 template<
typename... _Args>
1205 requires is_constructible_v<mapped_type, _Args...>
1206 pair<iterator, bool>
1207 try_emplace(key_type&& __k, _Args&&... __args)
1209 return _Impl::_M_try_emplace(nullopt,
std::move(__k),
1213 template<
typename _Key2,
typename... _Args>
1214 requires __transparent_comparator<_Compare>
1215 && is_constructible_v<key_type, _Key2>
1216 && is_constructible_v<mapped_type, _Args...>
1217 && (!is_convertible_v<_Key2&&, const_iterator>)
1218 && (!is_convertible_v<_Key2&&, iterator>)
1219 pair<iterator, bool>
1220 try_emplace(_Key2&& __k, _Args&&... __args)
1226 template<
typename... _Args>
1227 requires is_constructible_v<mapped_type, _Args...>
1229 try_emplace(const_iterator __hint,
const key_type& __k, _Args&&... __args)
1232 template<
typename... _Args>
1233 requires is_constructible_v<mapped_type, _Args...>
1235 try_emplace(const_iterator __hint, key_type&& __k, _Args&&... __args)
1237 return _Impl::_M_try_emplace(__hint,
std::move(__k),
1241 template<
typename _Key2,
typename... _Args>
1242 requires __transparent_comparator<_Compare>
1243 && is_constructible_v<key_type, _Key2>
1244 && is_constructible_v<mapped_type, _Args...>
1246 try_emplace(const_iterator __hint, _Key2&& __k, _Args&&... __args)
1252 template<
typename _Mapped>
1253 requires is_assignable_v<mapped_type&, _Mapped>
1254 && is_constructible_v<mapped_type, _Mapped>
1255 pair<iterator, bool>
1256 insert_or_assign(
const key_type& __k, _Mapped&& __obj)
1259 template<
typename _Mapped>
1260 requires is_assignable_v<mapped_type&, _Mapped>
1261 && is_constructible_v<mapped_type, _Mapped>
1262 pair<iterator, bool>
1263 insert_or_assign(key_type&& __k, _Mapped&& __obj)
1265 return insert_or_assign<key_type, _Mapped>(
std::move(__k),
1269 template<
typename _Key2,
typename _Mapped>
1270 requires (same_as<remove_cvref_t<_Key2>, _Key> || __transparent_comparator<_Compare>)
1271 && is_constructible_v<key_type, _Key2>
1272 && is_assignable_v<mapped_type&, _Mapped>
1273 && is_constructible_v<mapped_type, _Mapped>
1274 pair<iterator, bool>
1275 insert_or_assign(_Key2&& __k, _Mapped&& __obj)
1284 template<
typename _Mapped>
1285 requires is_assignable_v<mapped_type&, _Mapped>
1286 && is_constructible_v<mapped_type, _Mapped>
1288 insert_or_assign(const_iterator __hint,
const key_type& __k, _Mapped&& __obj)
1290 return insert_or_assign<const key_type&, _Mapped>(__hint, __k,
1294 template<
typename _Mapped>
1295 requires is_assignable_v<mapped_type&, _Mapped>
1296 && is_constructible_v<mapped_type, _Mapped>
1298 insert_or_assign(const_iterator __hint, key_type&& __k, _Mapped&& __obj)
1300 return insert_or_assign<key_type, _Mapped>(__hint,
std::move(__k),
1304 template<
typename _Key2,
typename _Mapped>
1305 requires (same_as<remove_cvref_t<_Key2>, _Key> || __transparent_comparator<_Compare>)
1306 && is_constructible_v<key_type, _Key2>
1307 && is_assignable_v<mapped_type&, _Mapped>
1308 && is_constructible_v<mapped_type, _Mapped>
1310 insert_or_assign(const_iterator __hint, _Key2&& __k, _Mapped&& __obj)
1320 using _Impl::key_comp;
1321 using _Impl::value_comp;
1323 using _Impl::values;
1328 using _Impl::contains;
1329 using _Impl::lower_bound;
1330 using _Impl::upper_bound;
1331 using _Impl::equal_range;
1333 using _Impl::_M_erase_if;
1336 template<
typename _KeyContainer,
typename _MappedContainer,
1338 flat_map(_KeyContainer, _MappedContainer, _Compare = _Compare())
1339 -> flat_map<
typename _KeyContainer::value_type,
typename _MappedContainer::value_type,
1340 _Compare, _KeyContainer, _MappedContainer>;
1342 template<
typename _KeyContainer,
typename _MappedContainer,
1343 __allocator_for<_KeyContainer, _MappedContainer> _Alloc>
1344 flat_map(_KeyContainer, _MappedContainer, _Alloc)
1345 -> flat_map<
typename _KeyContainer::value_type,
typename _MappedContainer::value_type,
1348 template<
typename _KeyContainer,
typename _MappedContainer, __not_allocator_like _Compare,
1349 __allocator_for<_KeyContainer, _MappedContainer> _Alloc>
1350 flat_map(_KeyContainer, _MappedContainer, _Compare, _Alloc)
1351 -> flat_map<
typename _KeyContainer::value_type,
typename _MappedContainer::value_type,
1352 _Compare, _KeyContainer, _MappedContainer>;
1354 template<
typename _KeyContainer,
typename _MappedContainer,
1356 flat_map(sorted_unique_t, _KeyContainer, _MappedContainer, _Compare = _Compare())
1357 -> flat_map<
typename _KeyContainer::value_type,
typename _MappedContainer::value_type,
1358 _Compare, _KeyContainer, _MappedContainer>;
1360 template<
typename _KeyContainer,
typename _MappedContainer,
1361 __allocator_for<_KeyContainer, _MappedContainer> _Alloc>
1362 flat_map(sorted_unique_t, _KeyContainer, _MappedContainer, _Alloc)
1363 -> flat_map<
typename _KeyContainer::value_type,
typename _MappedContainer::value_type,
1366 template<
typename _KeyContainer,
typename _MappedContainer, __not_allocator_like _Compare,
1367 __allocator_for<_KeyContainer, _MappedContainer> _Alloc>
1368 flat_map(sorted_unique_t, _KeyContainer, _MappedContainer, _Compare, _Alloc)
1369 -> flat_map<
typename _KeyContainer::value_type,
typename _MappedContainer::value_type,
1370 _Compare, _KeyContainer, _MappedContainer>;
1372 template<__has_input_iter_cat _InputIterator,
1374 flat_map(_InputIterator, _InputIterator, _Compare = _Compare())
1375 -> flat_map<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>, _Compare>;
1377 template<__has_input_iter_cat _InputIterator,
1379 flat_map(sorted_unique_t, _InputIterator, _InputIterator, _Compare = _Compare())
1380 -> flat_map<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>, _Compare>;
1382 template<ranges::input_range _Rg,
1385 flat_map(from_range_t, _Rg&&, _Compare = _Compare(), _Alloc = _Alloc())
1386 -> flat_map<__detail::__range_key_type<_Rg>, __detail::__range_mapped_type<_Rg>,
1389 __alloc_rebind<_Alloc, __detail::__range_key_type<_Rg>>>,
1391 __alloc_rebind<_Alloc, __detail::__range_mapped_type<_Rg>>>>;
1393 template<ranges::input_range _Rg, __allocator_like _Alloc>
1394 flat_map(from_range_t, _Rg&&, _Alloc)
1395 -> flat_map<__detail::__range_key_type<_Rg>, __detail::__range_mapped_type<_Rg>,
1398 __alloc_rebind<_Alloc, __detail::__range_key_type<_Rg>>>,
1400 __alloc_rebind<_Alloc, __detail::__range_mapped_type<_Rg>>>>;
1402 template<
typename _Key,
typename _Tp, __not_allocator_like _Compare = less<_Key>>
1404 -> flat_map<_Key, _Tp, _Compare>;
1406 template<
typename _Key,
typename _Tp, __not_allocator_like _Compare = less<_Key>>
1408 -> flat_map<_Key, _Tp, _Compare>;
1410 template<
typename _Key,
typename _Tp,
typename _Compare,
1411 typename _KeyContainer,
typename _MappedContainer,
typename _Alloc>
1412 struct uses_allocator<flat_map<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer>, _Alloc>
1413 : bool_constant<uses_allocator_v<_KeyContainer, _Alloc>
1414 && uses_allocator_v<_MappedContainer, _Alloc>>
1417 template<
typename _Key,
typename _Tp,
typename _Compare,
1418 typename _KeyContainer,
typename _MappedContainer,
typename _Predicate>
1419 typename flat_map<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer>::size_type
1420 erase_if(flat_map<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer>& __c,
1422 {
return __c._M_erase_if(
std::move(__pred)); }
1428 template<
typename _Key,
typename _Tp,
typename _Compare = less<_Key>,
1429 typename _KeyContainer = vector<_Key>,
1430 typename _MappedContainer = vector<_Tp>>
1432 :
private _Flat_map_impl<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer, true>
1434 using _Impl = _Flat_map_impl<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer, true>;
1439 using typename _Impl::key_type;
1440 using typename _Impl::mapped_type;
1441 using typename _Impl::value_type;
1442 using typename _Impl::key_compare;
1443 using typename _Impl::reference;
1444 using typename _Impl::const_reference;
1445 using typename _Impl::size_type;
1446 using typename _Impl::difference_type;
1447 using typename _Impl::iterator;
1448 using typename _Impl::const_iterator;
1449 using typename _Impl::reverse_iterator;
1450 using typename _Impl::const_reverse_iterator;
1451 using typename _Impl::key_container_type;
1452 using typename _Impl::mapped_container_type;
1453 using typename _Impl::value_compare;
1454 using typename _Impl::containers;
1462 using _Impl::rbegin;
1465 using _Impl::cbegin;
1467 using _Impl::crbegin;
1473 using _Impl::max_size;
1476 using _Impl::emplace;
1477 using _Impl::emplace_hint;
1478 using _Impl::insert;
1479 using _Impl::insert_range;
1480 using _Impl::extract;
1481 using _Impl::replace;
1487 using _Impl::key_comp;
1488 using _Impl::value_comp;
1490 using _Impl::values;
1495 using _Impl::contains;
1496 using _Impl::lower_bound;
1497 using _Impl::upper_bound;
1498 using _Impl::equal_range;
1500 using _Impl::_M_erase_if;
1503 template<
typename _KeyContainer,
typename _MappedContainer,
1505 flat_multimap(_KeyContainer, _MappedContainer, _Compare = _Compare())
1506 -> flat_multimap<
typename _KeyContainer::value_type,
typename _MappedContainer::value_type,
1507 _Compare, _KeyContainer, _MappedContainer>;
1509 template<
typename _KeyContainer,
typename _MappedContainer,
1510 __allocator_for<_KeyContainer, _MappedContainer> _Alloc>
1511 flat_multimap(_KeyContainer, _MappedContainer, _Alloc)
1512 -> flat_multimap<
typename _KeyContainer::value_type,
typename _MappedContainer::value_type,
1515 template<
typename _KeyContainer,
typename _MappedContainer, __not_allocator_like _Compare,
1516 __allocator_for<_KeyContainer, _MappedContainer> _Alloc>
1517 flat_multimap(_KeyContainer, _MappedContainer, _Compare, _Alloc)
1518 -> flat_multimap<
typename _KeyContainer::value_type,
typename _MappedContainer::value_type,
1519 _Compare, _KeyContainer, _MappedContainer>;
1521 template<
typename _KeyContainer,
typename _MappedContainer,
1523 flat_multimap(sorted_equivalent_t, _KeyContainer, _MappedContainer, _Compare = _Compare())
1524 -> flat_multimap<
typename _KeyContainer::value_type,
typename _MappedContainer::value_type,
1525 _Compare, _KeyContainer, _MappedContainer>;
1527 template<
typename _KeyContainer,
typename _MappedContainer,
1528 __allocator_for<_KeyContainer, _MappedContainer> _Alloc>
1529 flat_multimap(sorted_equivalent_t, _KeyContainer, _MappedContainer, _Alloc)
1530 -> flat_multimap<
typename _KeyContainer::value_type,
typename _MappedContainer::value_type,
1533 template<
typename _KeyContainer,
typename _MappedContainer, __not_allocator_like _Compare,
1534 __allocator_for<_KeyContainer, _MappedContainer> _Alloc>
1535 flat_multimap(sorted_equivalent_t, _KeyContainer, _MappedContainer, _Compare, _Alloc)
1536 -> flat_multimap<
typename _KeyContainer::value_type,
typename _MappedContainer::value_type,
1537 _Compare, _KeyContainer, _MappedContainer>;
1539 template<__has_input_iter_cat _InputIterator,
1541 flat_multimap(_InputIterator, _InputIterator, _Compare = _Compare())
1542 -> flat_multimap<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>, _Compare>;
1544 template<__has_input_iter_cat _InputIterator,
1546 flat_multimap(sorted_equivalent_t, _InputIterator, _InputIterator, _Compare = _Compare())
1547 -> flat_multimap<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>, _Compare>;
1549 template<ranges::input_range _Rg,
1552 flat_multimap(from_range_t, _Rg&&, _Compare = _Compare(), _Alloc = _Alloc())
1553 -> flat_multimap<__detail::__range_key_type<_Rg>, __detail::__range_mapped_type<_Rg>,
1556 __alloc_rebind<_Alloc, __detail::__range_key_type<_Rg>>>,
1558 __alloc_rebind<_Alloc, __detail::__range_mapped_type<_Rg>>>>;
1560 template<ranges::input_range _Rg, __allocator_like _Alloc>
1561 flat_multimap(from_range_t, _Rg&&, _Alloc)
1562 -> flat_multimap<__detail::__range_key_type<_Rg>, __detail::__range_mapped_type<_Rg>,
1565 __alloc_rebind<_Alloc, __detail::__range_key_type<_Rg>>>,
1567 __alloc_rebind<_Alloc, __detail::__range_mapped_type<_Rg>>>>;
1569 template<
typename _Key,
typename _Tp, __not_allocator_like _Compare = less<_Key>>
1571 -> flat_multimap<_Key, _Tp, _Compare>;
1573 template<
typename _Key,
typename _Tp, __not_allocator_like _Compare = less<_Key>>
1575 -> flat_multimap<_Key, _Tp, _Compare>;
1577 template<
typename _Key,
typename _Tp,
typename _Compare,
1578 typename _KeyContainer,
typename _MappedContainer,
typename _Alloc>
1579 struct uses_allocator<flat_multimap<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer>,
1581 : bool_constant<uses_allocator_v<_KeyContainer, _Alloc>
1582 && uses_allocator_v<_MappedContainer, _Alloc>>
1585 template<
typename _Key,
typename _Tp,
typename _Compare,
1586 typename _KeyContainer,
typename _MappedContainer,
typename _Predicate>
1587 typename flat_multimap<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer>::size_type
1588 erase_if(flat_multimap<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer>& __c,
1590 {
return __c._M_erase_if(
std::move(__pred)); }
1592_GLIBCXX_END_NAMESPACE_VERSION
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
constexpr complex< _Tp > operator-(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x minus y.
constexpr complex< _Tp > operator+(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x plus y.
pair(_T1, _T2) -> pair< _T1, _T2 >
Two pairs are equal iff their members are equal.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
_Tp * end(valarray< _Tp > &__va) noexcept
Return an iterator pointing to one past the last element of the valarray.
_Tp * begin(valarray< _Tp > &__va) noexcept
Return an iterator pointing to the first element of the valarray.
constexpr auto lexicographical_compare_three_way(_InputIter1 __first1, _InputIter1 __last1, _InputIter2 __first2, _InputIter2 __last2, _Comp __comp) -> decltype(__comp(*__first1, *__first2))
Performs dictionary comparison on ranges.
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
constexpr reverse_iterator< _Iterator > make_reverse_iterator(_Iterator __i)
Generator function for reverse_iterator.
ISO C++ entities toplevel namespace is std.
constexpr auto rbegin(_Container &__cont) noexcept(noexcept(__cont.rbegin())) -> decltype(__cont.rbegin())
Return a reverse iterator pointing to the last element of the container.
constexpr auto cend(const _Container &__cont) noexcept(noexcept(std::end(__cont))) -> decltype(std::end(__cont))
Return an iterator pointing to one past the last element of the const container.
constexpr auto empty(const _Container &__cont) noexcept(noexcept(__cont.empty())) -> decltype(__cont.empty())
Return whether a container is empty.
constexpr auto crbegin(const _Container &__cont) noexcept(noexcept(std::rbegin(__cont))) -> decltype(std::rbegin(__cont))
Return a reverse iterator pointing to the last element of the const container.
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
constexpr auto cbegin(const _Container &__cont) noexcept(noexcept(std::begin(__cont))) -> decltype(std::begin(__cont))
Return an iterator pointing to the first element of the const container.
constexpr auto rend(_Container &__cont) noexcept(noexcept(__cont.rend())) -> decltype(__cont.rend())
Return a reverse iterator pointing one past the first element of the container.
constexpr auto crend(const _Container &__cont) noexcept(noexcept(std::rend(__cont))) -> decltype(std::rend(__cont))
Return a reverse iterator pointing one past the first element of the const container.
The standard allocator, as per C++03 [20.4.1].
Declare uses_allocator so it can be specialized in <queue> etc.
One of the comparison functors.
Struct holding two objects of arbitrary type.
A standard container which offers fixed time access to individual elements in any order.