libstdc++
flat_map
Go to the documentation of this file.
1// <flat_map> -*- C++ -*-
2
3// Copyright The GNU Toolchain Authors.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file include/flat_map
26 * This is a Standard C++ Library header.
27 */
28
29#ifndef _GLIBCXX_FLAT_MAP
30#define _GLIBCXX_FLAT_MAP 1
31
32#ifdef _GLIBCXX_SYSHDR
33#pragma GCC system_header
34#endif
35
36#define __glibcxx_want_flat_map
37#include <bits/version.h>
38
39#ifdef __cpp_lib_flat_map // >= C++23
40
41#include <compare>
42#include <initializer_list>
43
44#include <exception>
45#include <functional> // not_fn
46#include <optional>
47#include <ranges> // views::zip
48#include <type_traits>
49#include <vector>
50#include <bits/functexcept.h>
51#include <bits/stl_algo.h>
52#include <bits/stl_function.h> // less
53#include <bits/stl_pair.h>
54#include <bits/uses_allocator_args.h> // make_obj_using_allocator
55#include <bits/ranges_algo.h>
56
57namespace std _GLIBCXX_VISIBILITY(default)
58{
59_GLIBCXX_BEGIN_NAMESPACE_VERSION
60
61 template<typename _Key, typename _Tp, typename _Compare,
62 typename _KeyContainer, typename _MappedContainer>
63 class flat_map;
64
65 template<typename _Key, typename _Tp, typename _Compare,
66 typename _KeyContainer, typename _MappedContainer>
67 class flat_multimap;
68
69 template<typename _Key, typename _Tp, typename _Compare,
70 typename _KeyContainer, typename _MappedContainer, bool _Multi>
71 class _Flat_map_impl
72 {
73 static_assert(is_same_v<_Key, typename _KeyContainer::value_type>);
74 static_assert(is_same_v<_Tp, typename _MappedContainer::value_type>);
75
76 static_assert(is_nothrow_swappable_v<_KeyContainer>);
77 static_assert(is_nothrow_swappable_v<_MappedContainer>);
78
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>;
85
86 public:
87 template<bool _Const> struct _Iterator;
88
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;
103
104 private:
105 using __emplace_result_t = __conditional_t<_Multi, iterator, pair<iterator, bool>>;
106
107 public:
108 class value_compare
109 {
110 [[no_unique_address]] key_compare _M_comp;
111 value_compare(key_compare __c) : _M_comp(__c) { }
112
113 public:
114 bool
115 operator()(const_reference __x, const_reference __y) const
116 { return _M_comp(__x.first, __y.first); }
117
118 friend _Flat_map_impl;
119 };
120
121 struct containers
122 {
123 key_container_type keys;
124 mapped_container_type values;
125 };
126
127 private:
128 struct _ClearGuard
129 {
130 containers* _M_cont;
131
132 _ClearGuard(containers& __cont)
133 : _M_cont(std::__addressof(__cont))
134 { }
135
136 ~_ClearGuard()
137 {
138 if (_M_cont)
139 {
140 _M_cont->keys.clear();
141 _M_cont->values.clear();
142 }
143 }
144
145 void
146 _M_disable()
147 { _M_cont = nullptr; }
148 };
149
150 _ClearGuard
151 _M_make_clear_guard()
152 { return _ClearGuard{this->_M_cont}; }
153
154 public:
155 // constructors
156 _Flat_map_impl() : _Flat_map_impl(key_compare()) { }
157
158 explicit
159 _Flat_map_impl(const key_compare& __comp)
160 : _M_cont(), _M_comp(__comp)
161 { }
162
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)
167 {
168 __glibcxx_assert(_M_cont.keys.size() == _M_cont.values.size());
169 _M_sort_uniq();
170 }
171
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)
177 {
178 __glibcxx_assert(_M_cont.keys.size() == _M_cont.values.size());
179 _GLIBCXX_DEBUG_ASSERT(ranges::is_sorted(_M_cont.keys, _M_comp));
180 }
181
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); }
187
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); }
194
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())
198 { }
199
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)
203 { insert_range(std::forward<_Rg>(__rg)); }
204
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)
208 { }
209
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)
214 { }
215
216 // constructors with allocators
217
218 template<__allocator_for<key_container_type, mapped_container_type> _Alloc>
219 explicit
220 _Flat_map_impl(const _Alloc& __a)
221 : _Flat_map_impl(key_compare(), __a)
222 { }
223
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)),
228 _M_comp(__comp)
229 { }
230
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,
234 const _Alloc& __a)
235 : _Flat_map_impl(__key_cont, __mapped_cont, key_compare(), __a)
236 { }
237
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,
242 const _Alloc& __a)
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)),
245 _M_comp(__comp)
246 {
247 __glibcxx_assert(_M_cont.keys.size() == _M_cont.values.size());
248 _M_sort_uniq();
249 }
250
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,
255 const _Alloc& __a)
256 : _Flat_map_impl(__s, __key_cont, __mapped_cont, key_compare(), __a)
257 { }
258
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,
264 const _Alloc& __a)
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)),
267 _M_comp(__comp)
268 {
269 __glibcxx_assert(_M_cont.keys.size() == _M_cont.values.size());
270 _GLIBCXX_DEBUG_ASSERT(ranges::is_sorted(_M_cont.keys, _M_comp));
271 }
272
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)),
277 _M_comp(__x._M_comp)
278 { }
279
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))),
286 _M_comp(__x._M_comp)
287 { }
288
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,
292 const _Alloc& __a)
293 : _Flat_map_impl(std::move(__first), std::move(__last), key_compare(), __a)
294 { }
295
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,
300 const _Alloc& __a)
301 : _Flat_map_impl(__comp, __a)
302 { insert(__first, __last); }
303
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,
308 const _Alloc& __a)
309 : _Flat_map_impl(__s, std::move(__first), std::move(__last), key_compare(), __a)
310 { }
311
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,
317 const _Alloc& __a)
318 : _Flat_map_impl(__comp, __a)
319 { insert(__s, __first, __last); }
320
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,
324 const _Alloc& __a)
325 : _Flat_map_impl(from_range, std::forward<_Rg>(__rg), key_compare(), __a)
326 { }
327
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,
331 const _Alloc& __a)
332 : _Flat_map_impl(__comp, __a)
333 { insert_range(std::forward<_Rg>(__rg)); }
334
335 template<__allocator_for<key_container_type, mapped_container_type> _Alloc>
336 _Flat_map_impl(initializer_list<value_type> __il,
337 const _Alloc& __a)
338 : _Flat_map_impl(__il, key_compare(), __a)
339 { }
340
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,
344 const _Alloc& __a)
345 : _Flat_map_impl(__il.begin(), __il.end(), __comp, __a)
346 { }
347
348 template<__allocator_for<key_container_type, mapped_container_type> _Alloc>
349 _Flat_map_impl(__sorted_t __s,
350 initializer_list<value_type> __il,
351 const _Alloc& __a)
352 : _Flat_map_impl(__s, __il.begin(), __il.end(), key_compare(), __a)
353 { }
354
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,
359 const _Alloc& __a)
360 : _Flat_map_impl(__s, __il.begin(), __il.end(), __comp, __a)
361 { }
362
363 _Derived&
364 operator=(initializer_list<value_type> __il)
365 {
366 clear();
367 insert(__il);
368 return static_cast<_Derived&>(*this);
369 }
370
371 // iterators
372 iterator
373 begin() noexcept
374 { return {this, _M_cont.keys.cbegin()}; }
375
376 const_iterator
377 begin() const noexcept
378 { return {this, _M_cont.keys.cbegin()}; }
379
380 iterator
381 end() noexcept
382 { return {this, _M_cont.keys.cend()}; }
383
384 const_iterator
385 end() const noexcept
386 { return {this, _M_cont.keys.cend()}; }
387
388 reverse_iterator
389 rbegin() noexcept
390 { return reverse_iterator(end()); }
391
392 const_reverse_iterator
393 rbegin() const noexcept
394 { return const_reverse_iterator(end()); }
395
396 reverse_iterator
397 rend() noexcept
398 { return reverse_iterator(begin()); }
399
400 const_reverse_iterator
401 rend() const noexcept
402 { return const_reverse_iterator(begin()); }
403
404 const_iterator
405 cbegin() const noexcept
406 { return {this, _M_cont.keys.cbegin()}; }
407
408 const_iterator
409 cend() const noexcept
410 { return {this, _M_cont.keys.cend()}; }
411
412 const_reverse_iterator
413 crbegin() const noexcept
414 { return const_reverse_iterator(cend()); }
415
416 const_reverse_iterator
417 crend() const noexcept
418 { return const_reverse_iterator(cbegin()); }
419
420 // capacity
421 [[nodiscard]]
422 bool
423 empty() const noexcept
424 { return _M_cont.keys.empty(); }
425
426 size_type
427 size() const noexcept
428 { return _M_cont.keys.size(); }
429
430 size_type
431 max_size() const noexcept
432 { return std::min<size_type>(keys().max_size(), values().max_size()); }
433
434 // element access
435 // operator[] and at defined directly in class flat_map only.
436
437 // modifiers
438 template<typename _Key2, typename... _Args>
439 pair<iterator, bool>
440 _M_try_emplace(optional<const_iterator> __hint, _Key2&& __k, _Args&&... __args)
441 {
442 // TODO: Simplify and audit the hint handling.
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()
447 && (__hint == cbegin()
448 || (__r = !_M_comp(__k, (*__hint)[-1].first))) // k >= hint[-1]
449 && (__hint == cend()
450 || (__s = !_M_comp((*__hint)[0].first, __k)))) // k <= hint[0]
451 {
452 __key_it = _M_cont.keys.begin() + __hint->_M_index;
453 if constexpr (!_Multi)
454 if (__r == 1 && !_M_comp(__key_it[-1], __k)) // k == hint[-1]
455 return {iterator{this, __key_it - 1}, false};
456 }
457 else
458 {
459 auto __first = _M_cont.keys.begin();
460 auto __last = _M_cont.keys.end();
461 if (__r == 1) // k >= hint[-1]
462 __first += __hint->_M_index;
463 else if (__r == 0) // k < __hint[-1]
464 __last = __first + __hint->_M_index;
465 if constexpr (_Multi)
466 {
467 if (__s == 0) // hint[0] < k
468 // Insert before the leftmost equivalent key.
469 __key_it = std::lower_bound(__first, __last, __k, _M_comp);
470 else
471 // Insert after the rightmost equivalent key.
472 __key_it = std::upper_bound(std::make_reverse_iterator(__last),
474 __k, std::not_fn(_M_comp)).base();
475 }
476 else
477 __key_it = std::lower_bound(__first, __last, __k, _M_comp);
478 }
479
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};
483
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());
487 _M_cont.values.emplace(__value_it, std::forward<_Args>(__args)...);
488 __guard._M_disable();
489 return {iterator{this, __key_it}, true};
490 }
491
492 template<typename... _Args>
493 requires is_constructible_v<value_type, _Args...>
494 __emplace_result_t
495 emplace(_Args&&... __args)
496 {
497 value_type __p(std::forward<_Args>(__args)...);
498 auto __r = _M_try_emplace(nullopt, std::move(__p.first), std::move(__p.second));
499 if constexpr (_Multi)
500 return __r.first;
501 else
502 return __r;
503 }
504
505 template<typename... _Args>
506 iterator
507 emplace_hint(const_iterator __position, _Args&&... __args)
508 {
509 value_type __p(std::forward<_Args>(__args)...);
510 return _M_try_emplace(__position, std::move(__p.first), std::move(__p.second)).first;
511 }
512
513 __emplace_result_t
514 insert(const value_type& __x)
515 { return emplace(__x); }
516
517 __emplace_result_t
518 insert(value_type&& __x)
519 { return emplace(std::move(__x)); }
520
521 iterator
522 insert(const_iterator __position, const value_type& __x)
523 { return emplace_hint(__position, __x); }
524
525 iterator
526 insert(const_iterator __position, value_type&& __x)
527 { return emplace_hint(__position, std::move(__x)); }
528
529 template<typename _Arg>
530 requires is_constructible_v<value_type, _Arg>
531 __emplace_result_t
532 insert(_Arg&& __x)
533 { return emplace(std::forward<_Arg>(__x)); }
534
535 template<typename _Arg>
536 requires is_constructible_v<value_type, _Arg>
537 iterator
538 insert(const_iterator __position, _Arg&& __x)
539 { return emplace_hint(__position, std::forward<_Arg>(__x)); }
540
541 private:
542 template<typename _Iter, typename _Sent>
543 void
544 _M_insert(_Iter __first, _Sent __last)
545 {
546 // FIXME: This implementation fails its complexity requirements.
547 // We can't idiomatically implement an efficient version (as in the
548 // disabled code) until ranges::inplace_merge is fixed to dispatch
549 // on iterator concept instead of iterator category.
550#if 0
551 auto __guard = _M_make_clear_guard();
552 auto __n = size();
553 for (; __first != __last; ++__first)
554 {
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));
558 }
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)
563 _M_unique();
564 __guard._M_cont = nullptr;
565#else
566 auto __guard = _M_make_clear_guard();
567 for (; __first != __last; ++__first)
568 {
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));
572 }
573 _M_sort_uniq();
574 __guard._M_disable();
575#endif
576 }
577
578 public:
579 template<__has_input_iter_cat _InputIterator>
580 void
581 insert(_InputIterator __first, _InputIterator __last)
582 { _M_insert(std::move(__first), std::move(__last)); }
583
584 template<__has_input_iter_cat _InputIterator>
585 void
586 insert(__sorted_t, _InputIterator __first, _InputIterator __last)
587 {
588 // FIXME: This implementation fails its complexity requirements; see above.
589 insert(std::move(__first), std::move(__last));
590 }
591
592 template<__detail::__container_compatible_range<value_type> _Rg>
593 void
594 insert_range(_Rg&& __rg)
595 { _M_insert(ranges::begin(__rg), ranges::end(__rg)); }
596
597 void
598 insert(initializer_list<value_type> __il)
599 { insert(__il.begin(), __il.end()); }
600
601 void
602 insert(__sorted_t __s, initializer_list<value_type> __il)
603 { insert(__s, __il.begin(), __il.end()); }
604
605 containers
606 extract() &&
607 {
608 auto __guard = _M_make_clear_guard();
609 return {std::move(_M_cont.keys), std::move(_M_cont.values)};
610 }
611
612 void
613 replace(key_container_type&& __key_cont, mapped_container_type&& __mapped_cont)
614 {
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();
618 _M_cont.keys = std::move(__key_cont);
619 _M_cont.values = std::move(__mapped_cont);
620 __guard._M_disable();
621 }
622
623 // try_emplace and insert_or_assign defined directly in class flat_map only.
624
625 iterator
626 erase(iterator __position)
627 { return erase(static_cast<const_iterator>(__position)); }
628
629 iterator
630 erase(const_iterator __position)
631 {
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};
638 }
639
640 size_type
641 erase(const key_type& __x)
642 { return erase<const key_type&>(__x); }
643
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>)
649 size_type
650 erase(_Key2&& __x)
651 {
652 auto [__first, __last] = equal_range(std::forward<_Key2>(__x));
653 auto __n = __last - __first;
654 erase(__first, __last);
655 return __n;
656 }
657
658 iterator
659 erase(const_iterator __first, const_iterator __last)
660 {
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};
668 }
669
670 void
671 swap(_Derived& __y) noexcept
672 {
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);
676 }
677
678 void
679 clear() noexcept
680 {
681 _M_cont.keys.clear();
682 _M_cont.values.clear();
683 }
684
685 // observers
686 [[nodiscard]]
687 key_compare
688 key_comp() const
689 { return _M_comp; }
690
691 [[nodiscard]]
692 value_compare
693 value_comp() const
694 { return value_compare(_M_comp); }
695
696 [[nodiscard]]
697 const key_container_type&
698 keys() const noexcept
699 { return _M_cont.keys; }
700
701 [[nodiscard]]
702 const mapped_container_type&
703 values() const noexcept
704 { return _M_cont.values; }
705
706 // map operations
707 [[nodiscard]]
708 iterator
709 find(const key_type& __x)
710 { return find<key_type>(__x); }
711
712 [[nodiscard]]
713 const_iterator
714 find(const key_type& __x) const
715 { return find<key_type>(__x); }
716
717 template<typename _Key2>
718 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
719 [[nodiscard]]
720 iterator
721 find(const _Key2& __x)
722 {
723 auto __it = lower_bound(__x);
724 if (__it != end() && !_M_comp(__x, __it->first))
725 return __it;
726 else
727 return end();
728 }
729
730 template<typename _Key2>
731 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
732 [[nodiscard]]
733 const_iterator
734 find(const _Key2& __x) const
735 {
736 auto __it = lower_bound(__x);
737 if (__it != cend() && !_M_comp(__x, __it->first))
738 return __it;
739 else
740 return cend();
741 }
742
743 [[nodiscard]]
744 size_type
745 count(const key_type& __x) const
746 { return count<key_type>(__x); }
747
748 template<typename _Key2>
749 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
750 [[nodiscard]]
751 size_type
752 count(const _Key2& __x) const
753 {
754 if constexpr (!_Multi)
755 return contains<_Key2>(__x);
756 else
757 {
758 auto [__first, __last] = equal_range(__x);
759 return __last - __first;
760 }
761 }
762
763 [[nodiscard]]
764 bool
765 contains(const key_type& __x) const
766 { return contains<key_type>(__x); }
767
768 template<typename _Key2>
769 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
770 [[nodiscard]]
771 bool
772 contains(const _Key2& __x) const
773 { return find(__x) != cend(); }
774
775 [[nodiscard]]
776 iterator
777 lower_bound(const key_type& __x)
778 { return lower_bound<key_type>(__x); }
779
780 [[nodiscard]]
781 const_iterator
782 lower_bound(const key_type& __x) const
783 { return lower_bound<key_type>(__x); }
784
785 template<typename _Key2>
786 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
787 [[nodiscard]]
788 iterator
789 lower_bound(const _Key2& __x)
790 {
791 auto __it = std::lower_bound(_M_cont.keys.begin(), _M_cont.keys.end(),
792 __x, _M_comp);
793 return {this, __it};
794 }
795
796 template<typename _Key2>
797 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
798 [[nodiscard]]
799 const_iterator
800 lower_bound(const _Key2& __x) const
801 {
802 auto __it = std::lower_bound(_M_cont.keys.begin(), _M_cont.keys.end(),
803 __x, _M_comp);
804 return {this, __it};
805 }
806
807 [[nodiscard]]
808 iterator
809 upper_bound(const key_type& __x)
810 { return upper_bound<key_type>(__x); }
811
812 [[nodiscard]]
813 const_iterator
814 upper_bound(const key_type& __x) const
815 { return upper_bound<key_type>(__x); }
816
817 template<typename _Key2>
818 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
819 [[nodiscard]]
820 iterator
821 upper_bound(const _Key2& __x)
822 {
823 auto __it = std::upper_bound(_M_cont.keys.begin(), _M_cont.keys.end(),
824 __x, _M_comp);
825 return {this, __it};
826 }
827
828 template<typename _Key2>
829 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
830 [[nodiscard]]
831 const_iterator
832 upper_bound(const _Key2& __x) const
833 {
834 auto __it = std::upper_bound(_M_cont.keys.begin(), _M_cont.keys.end(),
835 __x, _M_comp);
836 return {this, __it};
837 }
838
839 [[nodiscard]]
840 pair<iterator, iterator>
841 equal_range(const key_type& __x)
842 { return equal_range<key_type>(__x); }
843
844 [[nodiscard]]
845 pair<const_iterator, const_iterator>
846 equal_range(const key_type& __x) const
847 { return equal_range<key_type>(__x); }
848
849 template<typename _Key2>
850 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
851 [[nodiscard]]
852 pair<iterator, iterator>
853 equal_range(const _Key2& __x)
854 {
855 auto [__first, __last] = std::equal_range(_M_cont.keys.begin(),
856 _M_cont.keys.end(),
857 __x, _M_comp);
858 return {{this, __first}, {this, __last}};
859 }
860
861 template<typename _Key2>
862 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
863 [[nodiscard]]
864 pair<const_iterator, const_iterator>
865 equal_range(const _Key2& __x) const
866 {
867 auto [__first, __last] = std::equal_range(_M_cont.keys.begin(),
868 _M_cont.keys.end(),
869 __x, _M_comp);
870 return {{this, __first}, {this, __last}};
871 }
872
873 [[nodiscard]]
874 friend bool
875 operator==(const _Derived& __x, const _Derived& __y)
876 { return std::equal(__x.begin(), __x.end(), __y.begin(), __y.end()); }
877
878 template<typename _Up = value_type>
879 [[nodiscard]]
880 friend __detail::__synth3way_t<_Up>
881 operator<=>(const _Derived& __x, const _Derived& __y)
882 {
883 return std::lexicographical_compare_three_way(__x.begin(), __x.end(),
884 __y.begin(), __y.end(),
885 __detail::__synth3way);
886 }
887
888 friend void
889 swap(_Derived& __x, _Derived& __y) noexcept
890 { return __x.swap(__y); }
891
892 template<typename _Predicate>
893 size_type
894 _M_erase_if(_Predicate __pred)
895 {
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();
902 return __erased;
903 }
904
905 private:
906 containers _M_cont;
907 [[no_unique_address]] _Compare _M_comp;
908
909 void
910 _M_sort_uniq()
911 {
912 auto __zv = views::zip(_M_cont.keys, _M_cont.values);
913 ranges::sort(__zv, value_comp());
914 if constexpr (!_Multi)
915 _M_unique();
916 }
917
918 void
919 _M_unique() requires (!_Multi)
920 {
921 struct __key_equiv
922 {
923 __key_equiv(key_compare __c) : _M_comp(__c) { }
924
925 bool
926 operator()(const_reference __x, const_reference __y) const
927 { return !_M_comp(__x.first, __y.first) && !_M_comp(__y.first, __x.first); }
928
929 [[no_unique_address]] key_compare _M_comp;
930 };
931
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());
937 }
938 };
939
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
944 {
945 using __size_type = typename _Flat_map_impl::size_type;
946
947 public:
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;
954
955 _Iterator() = default;
956
957 _Iterator(_Iterator<!_Const> __it) requires _Const
958 : _M_cont(__it._M_cont), _M_index(__it._M_index)
959 { }
960
961 reference
962 operator*() const noexcept
963 {
964 __glibcxx_assert(_M_index < _M_cont->keys.size());
965 return {_M_cont->keys[_M_index], _M_cont->values[_M_index]};
966 }
967
968 struct pointer
969 {
970 reference __p;
971
972 const reference*
973 operator->() const noexcept
974 { return std::__addressof(__p); }
975 };
976
977 pointer
978 operator->() const
979 { return pointer{operator*()}; }
980
981 reference
982 operator[](difference_type __n) const noexcept
983 { return *(*this + __n); }
984
985 _Iterator&
986 operator++() noexcept
987 {
988 ++_M_index;
989 return *this;
990 }
991
992 _Iterator&
993 operator--() noexcept
994 {
995 --_M_index;
996 return *this;
997 }
998
999 _Iterator
1000 operator++(int) noexcept
1001 {
1002 auto __tmp = *this;
1003 ++*this;
1004 return __tmp;
1005 }
1006
1007 _Iterator
1008 operator--(int) noexcept
1009 {
1010 auto __tmp = *this;
1011 --*this;
1012 return __tmp;
1013 }
1014
1015 _Iterator&
1016 operator+=(difference_type __n) noexcept
1017 {
1018 _M_index += __n;
1019 return *this;
1020 }
1021
1022 _Iterator&
1023 operator-=(difference_type __n) noexcept
1024 {
1025 _M_index -= __n;
1026 return *this;
1027 }
1028
1029 private:
1030 friend _Flat_map_impl;
1031 friend _Iterator<!_Const>;
1032
1033 _Iterator(_Flat_map_impl* __fm, typename key_container_type::const_iterator __it)
1034 requires (!_Const)
1035 : _M_cont(std::__addressof(__fm->_M_cont)), _M_index(__it - __fm->keys().cbegin())
1036 { }
1037
1038 _Iterator(const _Flat_map_impl* __fm, typename key_container_type::const_iterator __it)
1039 requires _Const
1040 : _M_cont(std::__addressof(__fm->_M_cont)), _M_index(__it - __fm->keys().cbegin())
1041 { }
1042
1043 friend _Iterator
1044 operator+(_Iterator __it, difference_type __n) noexcept
1045 {
1046 __it += __n;
1047 return __it;
1048 }
1049
1050 friend _Iterator
1051 operator+(difference_type __n, _Iterator __it) noexcept
1052 {
1053 __it += __n;
1054 return __it;
1055 }
1056
1057 friend _Iterator
1058 operator-(_Iterator __it, difference_type __n) noexcept
1059 {
1060 __it -= __n;
1061 return __it;
1062 }
1063
1064 friend difference_type
1065 operator-(const _Iterator& __x, const _Iterator& __y) noexcept
1066 {
1067 __glibcxx_assert(__x._M_cont == __y._M_cont);
1068 return __x._M_index - __y._M_index;
1069 }
1070
1071 friend bool
1072 operator==(const _Iterator& __x, const _Iterator& __y) noexcept
1073 {
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;
1077 }
1078
1079 friend strong_ordering
1080 operator<=>(const _Iterator& __x, const _Iterator& __y)
1081 {
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;
1085 }
1086
1087 ranges::__maybe_const_t<_Const, containers>* _M_cont = nullptr;
1088 __size_type _M_index = -1;
1089 };
1090
1091 /* Class template flat_map - container adaptor
1092 *
1093 * @ingroup
1094 */
1095 template<typename _Key, typename _Tp, typename _Compare = less<_Key>,
1096 typename _KeyContainer = vector<_Key>,
1097 typename _MappedContainer = vector<_Tp>>
1098 class flat_map
1099 : private _Flat_map_impl<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer, false>
1100 {
1101 using _Impl = _Flat_map_impl<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer, false>;
1102 friend _Impl;
1103
1104 public:
1105 // types
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;
1122
1123 // constructors
1124 using _Impl::_Impl;
1125
1126 // iterators
1127 using _Impl::begin;
1128 using _Impl::end;
1129 using _Impl::rbegin;
1130 using _Impl::rend;
1131
1132 using _Impl::cbegin;
1133 using _Impl::cend;
1134 using _Impl::crbegin;
1135 using _Impl::crend;
1136
1137 // capacity
1138 using _Impl::empty;
1139 using _Impl::size;
1140 using _Impl::max_size;
1141
1142 // element access
1143 mapped_type&
1144 operator[](const key_type& __x)
1145 { return operator[]<const key_type>(__x); }
1146
1147 mapped_type&
1148 operator[](key_type&& __x)
1149 { return operator[]<key_type>(std::move(__x)); }
1150
1151 template<typename _Key2>
1152 requires same_as<remove_cvref_t<_Key2>, _Key> || __transparent_comparator<_Compare>
1153 mapped_type&
1154 operator[](_Key2&& __x)
1155 { return try_emplace(std::forward<_Key2>(__x)).first->second; }
1156
1157 mapped_type&
1158 at(const key_type& __x)
1159 { return at<key_type>(__x); }
1160
1161 const mapped_type&
1162 at(const key_type& __x) const
1163 { return at<key_type>(__x); }
1164
1165 template<typename _Key2>
1166 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
1167 mapped_type&
1168 at(const _Key2& __x)
1169 {
1170 auto __it = this->find(__x);
1171 if (__it == this->end())
1172 __throw_out_of_range("flat_map::at");
1173 return __it->second;
1174 }
1175
1176 template<typename _Key2>
1177 requires same_as<_Key2, _Key> || __transparent_comparator<_Compare>
1178 const mapped_type&
1179 at(const _Key2& __x) const
1180 {
1181 auto __it = this->find(__x);
1182 if (__it == this->end())
1183 __throw_out_of_range("flat_map::at");
1184 return __it->second;
1185 }
1186
1187 // modifiers
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;
1194 using _Impl::erase;
1195 using _Impl::swap;
1196 using _Impl::clear;
1197
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)
1202 { return _Impl::_M_try_emplace(nullopt, __k, std::forward<_Args>(__args)...); }
1203
1204 template<typename... _Args>
1205 requires is_constructible_v<mapped_type, _Args...>
1206 pair<iterator, bool>
1207 try_emplace(key_type&& __k, _Args&&... __args)
1208 {
1209 return _Impl::_M_try_emplace(nullopt, std::move(__k),
1210 std::forward<_Args>(__args)...);
1211 }
1212
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)
1221 {
1222 return _Impl::_M_try_emplace(nullopt, std::forward<_Key2>(__k),
1223 std::forward<_Args>(__args)...);
1224 }
1225
1226 template<typename... _Args>
1227 requires is_constructible_v<mapped_type, _Args...>
1228 iterator
1229 try_emplace(const_iterator __hint, const key_type& __k, _Args&&... __args)
1230 { return _Impl::_M_try_emplace(__hint, __k, std::forward<_Args>(__args)...).first; }
1231
1232 template<typename... _Args>
1233 requires is_constructible_v<mapped_type, _Args...>
1234 iterator
1235 try_emplace(const_iterator __hint, key_type&& __k, _Args&&... __args)
1236 {
1237 return _Impl::_M_try_emplace(__hint, std::move(__k),
1238 std::forward<_Args>(__args)...).first;
1239 }
1240
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...>
1245 iterator
1246 try_emplace(const_iterator __hint, _Key2&& __k, _Args&&... __args)
1247 {
1248 return _Impl::_M_try_emplace(__hint, std::forward<_Key2>(__k),
1249 std::forward<_Args>(__args)...).first;
1250 }
1251
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)
1257 { return insert_or_assign<const key_type&, _Mapped>(__k, std::forward<_Mapped>(__obj)); }
1258
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)
1264 {
1265 return insert_or_assign<key_type, _Mapped>(std::move(__k),
1266 std::forward<_Mapped>(__obj));
1267 }
1268
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)
1276 {
1277 auto __r = _Impl::_M_try_emplace(nullopt, std::forward<_Key2>(__k),
1278 std::forward<_Mapped>(__obj));
1279 if (!__r.second)
1280 __r.first->second = std::forward<_Mapped>(__obj);
1281 return __r;
1282 }
1283
1284 template<typename _Mapped>
1285 requires is_assignable_v<mapped_type&, _Mapped>
1286 && is_constructible_v<mapped_type, _Mapped>
1287 iterator
1288 insert_or_assign(const_iterator __hint, const key_type& __k, _Mapped&& __obj)
1289 {
1290 return insert_or_assign<const key_type&, _Mapped>(__hint, __k,
1291 std::forward<_Mapped>(__obj));
1292 }
1293
1294 template<typename _Mapped>
1295 requires is_assignable_v<mapped_type&, _Mapped>
1296 && is_constructible_v<mapped_type, _Mapped>
1297 iterator
1298 insert_or_assign(const_iterator __hint, key_type&& __k, _Mapped&& __obj)
1299 {
1300 return insert_or_assign<key_type, _Mapped>(__hint, std::move(__k),
1301 std::forward<_Mapped>(__obj));
1302 }
1303
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>
1309 iterator
1310 insert_or_assign(const_iterator __hint, _Key2&& __k, _Mapped&& __obj)
1311 {
1312 auto __r = _Impl::_M_try_emplace(__hint, std::forward<_Key2>(__k),
1313 std::forward<_Mapped>(__obj));
1314 if (!__r.second)
1315 __r.first->second = std::forward<_Mapped>(__obj);
1316 return __r.first;
1317 }
1318
1319 // observers
1320 using _Impl::key_comp;
1321 using _Impl::value_comp;
1322 using _Impl::keys;
1323 using _Impl::values;
1324
1325 // map operations
1326 using _Impl::find;
1327 using _Impl::count;
1328 using _Impl::contains;
1329 using _Impl::lower_bound;
1330 using _Impl::upper_bound;
1331 using _Impl::equal_range;
1332
1333 using _Impl::_M_erase_if;
1334 };
1335
1336 template<typename _KeyContainer, typename _MappedContainer,
1337 __not_allocator_like _Compare = less<typename _KeyContainer::value_type>>
1338 flat_map(_KeyContainer, _MappedContainer, _Compare = _Compare())
1339 -> flat_map<typename _KeyContainer::value_type, typename _MappedContainer::value_type,
1340 _Compare, _KeyContainer, _MappedContainer>;
1341
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,
1346 less<typename _KeyContainer::value_type>, _KeyContainer, _MappedContainer>;
1347
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>;
1353
1354 template<typename _KeyContainer, typename _MappedContainer,
1355 __not_allocator_like _Compare = less<typename _KeyContainer::value_type>>
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>;
1359
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,
1364 less<typename _KeyContainer::value_type>, _KeyContainer, _MappedContainer>;
1365
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>;
1371
1372 template<__has_input_iter_cat _InputIterator,
1373 __not_allocator_like _Compare = less<__iter_key_t<_InputIterator>>>
1374 flat_map(_InputIterator, _InputIterator, _Compare = _Compare())
1375 -> flat_map<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>, _Compare>;
1376
1377 template<__has_input_iter_cat _InputIterator,
1378 __not_allocator_like _Compare = less<__iter_key_t<_InputIterator>>>
1379 flat_map(sorted_unique_t, _InputIterator, _InputIterator, _Compare = _Compare())
1380 -> flat_map<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>, _Compare>;
1381
1382 template<ranges::input_range _Rg,
1383 __not_allocator_like _Compare = less<__detail::__range_key_type<_Rg>>,
1384 __allocator_like _Alloc = allocator<byte>>
1385 flat_map(from_range_t, _Rg&&, _Compare = _Compare(), _Alloc = _Alloc())
1386 -> flat_map<__detail::__range_key_type<_Rg>, __detail::__range_mapped_type<_Rg>,
1387 _Compare,
1389 __alloc_rebind<_Alloc, __detail::__range_key_type<_Rg>>>,
1391 __alloc_rebind<_Alloc, __detail::__range_mapped_type<_Rg>>>>;
1392
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>>>>;
1401
1402 template<typename _Key, typename _Tp, __not_allocator_like _Compare = less<_Key>>
1403 flat_map(initializer_list<pair<_Key, _Tp>>, _Compare = _Compare())
1404 -> flat_map<_Key, _Tp, _Compare>;
1405
1406 template<typename _Key, typename _Tp, __not_allocator_like _Compare = less<_Key>>
1407 flat_map(sorted_unique_t, initializer_list<pair<_Key, _Tp>>, _Compare = _Compare())
1408 -> flat_map<_Key, _Tp, _Compare>;
1409
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>>
1415 { };
1416
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,
1421 _Predicate __pred)
1422 { return __c._M_erase_if(std::move(__pred)); }
1423
1424 /* Class template flat_multimap - container adaptor
1425 *
1426 * @ingroup
1427 */
1428 template<typename _Key, typename _Tp, typename _Compare = less<_Key>,
1429 typename _KeyContainer = vector<_Key>,
1430 typename _MappedContainer = vector<_Tp>>
1431 class flat_multimap
1432 : private _Flat_map_impl<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer, true>
1433 {
1434 using _Impl = _Flat_map_impl<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer, true>;
1435 friend _Impl;
1436
1437 public:
1438 // types
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;
1455
1456 // constructors
1457 using _Impl::_Impl;
1458
1459 // iterators
1460 using _Impl::begin;
1461 using _Impl::end;
1462 using _Impl::rbegin;
1463 using _Impl::rend;
1464
1465 using _Impl::cbegin;
1466 using _Impl::cend;
1467 using _Impl::crbegin;
1468 using _Impl::crend;
1469
1470 // capacity
1471 using _Impl::empty;
1472 using _Impl::size;
1473 using _Impl::max_size;
1474
1475 // modifiers
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;
1482 using _Impl::erase;
1483 using _Impl::swap;
1484 using _Impl::clear;
1485
1486 // observers
1487 using _Impl::key_comp;
1488 using _Impl::value_comp;
1489 using _Impl::keys;
1490 using _Impl::values;
1491
1492 // map operations
1493 using _Impl::find;
1494 using _Impl::count;
1495 using _Impl::contains;
1496 using _Impl::lower_bound;
1497 using _Impl::upper_bound;
1498 using _Impl::equal_range;
1499
1500 using _Impl::_M_erase_if;
1501 };
1502
1503 template<typename _KeyContainer, typename _MappedContainer,
1504 __not_allocator_like _Compare = less<typename _KeyContainer::value_type>>
1505 flat_multimap(_KeyContainer, _MappedContainer, _Compare = _Compare())
1506 -> flat_multimap<typename _KeyContainer::value_type, typename _MappedContainer::value_type,
1507 _Compare, _KeyContainer, _MappedContainer>;
1508
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,
1513 less<typename _KeyContainer::value_type>, _KeyContainer, _MappedContainer>;
1514
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>;
1520
1521 template<typename _KeyContainer, typename _MappedContainer,
1522 __not_allocator_like _Compare = less<typename _KeyContainer::value_type>>
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>;
1526
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,
1531 less<typename _KeyContainer::value_type>, _KeyContainer, _MappedContainer>;
1532
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>;
1538
1539 template<__has_input_iter_cat _InputIterator,
1540 __not_allocator_like _Compare = less<__iter_key_t<_InputIterator>>>
1541 flat_multimap(_InputIterator, _InputIterator, _Compare = _Compare())
1542 -> flat_multimap<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>, _Compare>;
1543
1544 template<__has_input_iter_cat _InputIterator,
1545 __not_allocator_like _Compare = less<__iter_key_t<_InputIterator>>>
1546 flat_multimap(sorted_equivalent_t, _InputIterator, _InputIterator, _Compare = _Compare())
1547 -> flat_multimap<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>, _Compare>;
1548
1549 template<ranges::input_range _Rg,
1550 __not_allocator_like _Compare = less<__detail::__range_key_type<_Rg>>,
1551 __allocator_like _Alloc = allocator<byte>>
1552 flat_multimap(from_range_t, _Rg&&, _Compare = _Compare(), _Alloc = _Alloc())
1553 -> flat_multimap<__detail::__range_key_type<_Rg>, __detail::__range_mapped_type<_Rg>,
1554 _Compare,
1556 __alloc_rebind<_Alloc, __detail::__range_key_type<_Rg>>>,
1558 __alloc_rebind<_Alloc, __detail::__range_mapped_type<_Rg>>>>;
1559
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>>>>;
1568
1569 template<typename _Key, typename _Tp, __not_allocator_like _Compare = less<_Key>>
1570 flat_multimap(initializer_list<pair<_Key, _Tp>>, _Compare = _Compare())
1571 -> flat_multimap<_Key, _Tp, _Compare>;
1572
1573 template<typename _Key, typename _Tp, __not_allocator_like _Compare = less<_Key>>
1574 flat_multimap(sorted_equivalent_t, initializer_list<pair<_Key, _Tp>>, _Compare = _Compare())
1575 -> flat_multimap<_Key, _Tp, _Compare>;
1576
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>,
1580 _Alloc>
1581 : bool_constant<uses_allocator_v<_KeyContainer, _Alloc>
1582 && uses_allocator_v<_MappedContainer, _Alloc>>
1583 { };
1584
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,
1589 _Predicate __pred)
1590 { return __c._M_erase_if(std::move(__pred)); }
1591
1592_GLIBCXX_END_NAMESPACE_VERSION
1593} // namespace std
1594#endif // __cpp_lib_flat_map
1595#endif // _GLIBCXX_FLAT_MAP
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
Definition complex:434
constexpr complex< _Tp > operator-(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x minus y.
Definition complex:404
constexpr complex< _Tp > operator+(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x plus y.
Definition complex:374
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.
Definition move.h:138
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:52
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
_Tp * end(valarray< _Tp > &__va) noexcept
Return an iterator pointing to one past the last element of the valarray.
Definition valarray:1251
_Tp * begin(valarray< _Tp > &__va) noexcept
Return an iterator pointing to the first element of the valarray.
Definition valarray:1229
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.
initializer_list
The standard allocator, as per C++03 [20.4.1].
Definition allocator.h:134
Declare uses_allocator so it can be specialized in <queue> etc.
Definition memoryfwd.h:75
One of the comparison functors.
Struct holding two objects of arbitrary type.
Definition stl_pair.h:304
A standard container which offers fixed time access to individual elements in any order.
Definition stl_vector.h:459