1 // Debugging unordered_map/unordered_multimap implementation -*- C++ -*-
3 // Copyright (C) 2003-2020 Free Software Foundation, Inc.
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)
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.
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.
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/>.
25 /** @file debug/unordered_map
26 * This file is a GNU debug extension to the Standard C++ Library.
29 #ifndef _GLIBCXX_DEBUG_UNORDERED_MAP
30 #define _GLIBCXX_DEBUG_UNORDERED_MAP 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
35 # include <bits/c++0x_warning.h>
37 # include <bits/c++config.h>
38 namespace std _GLIBCXX_VISIBILITY(default) { namespace __debug {
39 template<typename _Key, typename _Tp, typename _Hash, typename _Pred,
42 template<typename _Key, typename _Tp, typename _Hash, typename _Pred,
44 class unordered_multimap;
45 } } // namespace std::__debug
47 # include <unordered_map>
49 #include <debug/safe_unordered_container.h>
50 #include <debug/safe_container.h>
51 #include <debug/safe_iterator.h>
52 #include <debug/safe_local_iterator.h>
54 namespace std _GLIBCXX_VISIBILITY(default)
58 /// Class std::unordered_map with safety/checking/debug instrumentation.
59 template<typename _Key, typename _Tp,
60 typename _Hash = std::hash<_Key>,
61 typename _Pred = std::equal_to<_Key>,
62 typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
64 : public __gnu_debug::_Safe_container<
65 unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>, _Alloc,
66 __gnu_debug::_Safe_unordered_container>,
67 public _GLIBCXX_STD_C::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>
69 typedef _GLIBCXX_STD_C::unordered_map<_Key, _Tp, _Hash,
71 typedef __gnu_debug::_Safe_container<unordered_map,
72 _Alloc, __gnu_debug::_Safe_unordered_container> _Safe;
73 typedef typename _Base::const_iterator _Base_const_iterator;
74 typedef typename _Base::iterator _Base_iterator;
75 typedef typename _Base::const_local_iterator
76 _Base_const_local_iterator;
77 typedef typename _Base::local_iterator _Base_local_iterator;
79 template<typename _ItT, typename _SeqT, typename _CatT>
80 friend class ::__gnu_debug::_Safe_iterator;
81 template<typename _ItT, typename _SeqT>
82 friend class ::__gnu_debug::_Safe_local_iterator;
85 typedef typename _Base::size_type size_type;
86 typedef typename _Base::hasher hasher;
87 typedef typename _Base::key_equal key_equal;
88 typedef typename _Base::allocator_type allocator_type;
90 typedef typename _Base::key_type key_type;
91 typedef typename _Base::value_type value_type;
93 typedef __gnu_debug::_Safe_iterator<
94 _Base_iterator, unordered_map> iterator;
95 typedef __gnu_debug::_Safe_iterator<
96 _Base_const_iterator, unordered_map> const_iterator;
97 typedef __gnu_debug::_Safe_local_iterator<
98 _Base_local_iterator, unordered_map> local_iterator;
99 typedef __gnu_debug::_Safe_local_iterator<
100 _Base_const_local_iterator, unordered_map> const_local_iterator;
102 unordered_map() = default;
105 unordered_map(size_type __n,
106 const hasher& __hf = hasher(),
107 const key_equal& __eql = key_equal(),
108 const allocator_type& __a = allocator_type())
109 : _Base(__n, __hf, __eql, __a) { }
111 template<typename _InputIterator>
112 unordered_map(_InputIterator __first, _InputIterator __last,
114 const hasher& __hf = hasher(),
115 const key_equal& __eql = key_equal(),
116 const allocator_type& __a = allocator_type())
117 : _Base(__gnu_debug::__base(
118 __glibcxx_check_valid_constructor_range(__first, __last)),
119 __gnu_debug::__base(__last), __n,
120 __hf, __eql, __a) { }
122 unordered_map(const unordered_map&) = default;
124 unordered_map(const _Base& __x)
127 unordered_map(unordered_map&&) = default;
130 unordered_map(const allocator_type& __a)
133 unordered_map(const unordered_map& __umap,
134 const allocator_type& __a)
135 : _Base(__umap, __a) { }
137 unordered_map(unordered_map&& __umap,
138 const allocator_type& __a)
139 noexcept( noexcept(_Base(std::move(__umap._M_base()), __a)) )
140 : _Safe(std::move(__umap._M_safe()), __a),
141 _Base(std::move(__umap._M_base()), __a) { }
143 unordered_map(initializer_list<value_type> __l,
145 const hasher& __hf = hasher(),
146 const key_equal& __eql = key_equal(),
147 const allocator_type& __a = allocator_type())
148 : _Base(__l, __n, __hf, __eql, __a) { }
150 unordered_map(size_type __n, const allocator_type& __a)
151 : unordered_map(__n, hasher(), key_equal(), __a)
154 unordered_map(size_type __n,
156 const allocator_type& __a)
157 : unordered_map(__n, __hf, key_equal(), __a)
160 template<typename _InputIterator>
161 unordered_map(_InputIterator __first, _InputIterator __last,
163 const allocator_type& __a)
164 : unordered_map(__first, __last, __n, hasher(), key_equal(), __a)
167 template<typename _InputIterator>
168 unordered_map(_InputIterator __first, _InputIterator __last,
171 const allocator_type& __a)
172 : unordered_map(__first, __last, __n, __hf, key_equal(), __a)
175 unordered_map(initializer_list<value_type> __l,
177 const allocator_type& __a)
178 : unordered_map(__l, __n, hasher(), key_equal(), __a)
181 unordered_map(initializer_list<value_type> __l,
184 const allocator_type& __a)
185 : unordered_map(__l, __n, __hf, key_equal(), __a)
188 ~unordered_map() = default;
191 operator=(const unordered_map&) = default;
194 operator=(unordered_map&&) = default;
197 operator=(initializer_list<value_type> __l)
200 this->_M_invalidate_all();
205 swap(unordered_map& __x)
206 noexcept( noexcept(declval<_Base&>().swap(__x)) )
216 this->_M_invalidate_all();
221 { return { _Base::begin(), this }; }
224 begin() const noexcept
225 { return { _Base::begin(), this }; }
229 { return { _Base::end(), this }; }
233 { return { _Base::end(), this }; }
236 cbegin() const noexcept
237 { return { _Base::cbegin(), this }; }
240 cend() const noexcept
241 { return { _Base::cend(), this }; }
247 __glibcxx_check_bucket_index(__b);
248 return { _Base::begin(__b), this };
254 __glibcxx_check_bucket_index(__b);
255 return { _Base::end(__b), this };
259 begin(size_type __b) const
261 __glibcxx_check_bucket_index(__b);
262 return { _Base::begin(__b), this };
266 end(size_type __b) const
268 __glibcxx_check_bucket_index(__b);
269 return { _Base::end(__b), this };
273 cbegin(size_type __b) const
275 __glibcxx_check_bucket_index(__b);
276 return { _Base::cbegin(__b), this };
280 cend(size_type __b) const
282 __glibcxx_check_bucket_index(__b);
283 return { _Base::cend(__b), this };
287 bucket_size(size_type __b) const
289 __glibcxx_check_bucket_index(__b);
290 return _Base::bucket_size(__b);
294 max_load_factor() const noexcept
295 { return _Base::max_load_factor(); }
298 max_load_factor(float __f)
300 __glibcxx_check_max_load_factor(__f);
301 _Base::max_load_factor(__f);
304 template<typename... _Args>
305 std::pair<iterator, bool>
306 emplace(_Args&&... __args)
308 size_type __bucket_count = this->bucket_count();
309 auto __res = _Base::emplace(std::forward<_Args>(__args)...);
310 _M_check_rehashed(__bucket_count);
311 return { { __res.first, this }, __res.second };
314 template<typename... _Args>
316 emplace_hint(const_iterator __hint, _Args&&... __args)
318 __glibcxx_check_insert(__hint);
319 size_type __bucket_count = this->bucket_count();
320 auto __it = _Base::emplace_hint(__hint.base(),
321 std::forward<_Args>(__args)...);
322 _M_check_rehashed(__bucket_count);
323 return { __it, this };
326 std::pair<iterator, bool>
327 insert(const value_type& __obj)
329 size_type __bucket_count = this->bucket_count();
330 auto __res = _Base::insert(__obj);
331 _M_check_rehashed(__bucket_count);
332 return { { __res.first, this }, __res.second };
335 // _GLIBCXX_RESOLVE_LIB_DEFECTS
336 // 2354. Unnecessary copying when inserting into maps with braced-init
337 std::pair<iterator, bool>
338 insert(value_type&& __x)
340 size_type __bucket_count = this->bucket_count();
341 auto __res = _Base::insert(std::move(__x));
342 _M_check_rehashed(__bucket_count);
343 return { { __res.first, this }, __res.second };
346 template<typename _Pair, typename = typename
347 std::enable_if<std::is_constructible<value_type,
348 _Pair&&>::value>::type>
349 std::pair<iterator, bool>
350 insert(_Pair&& __obj)
352 size_type __bucket_count = this->bucket_count();
353 auto __res = _Base::insert(std::forward<_Pair>(__obj));
354 _M_check_rehashed(__bucket_count);
355 return { { __res.first, this }, __res.second };
359 insert(const_iterator __hint, const value_type& __obj)
361 __glibcxx_check_insert(__hint);
362 size_type __bucket_count = this->bucket_count();
363 auto __it = _Base::insert(__hint.base(), __obj);
364 _M_check_rehashed(__bucket_count);
365 return { __it, this };
368 // _GLIBCXX_RESOLVE_LIB_DEFECTS
369 // 2354. Unnecessary copying when inserting into maps with braced-init
371 insert(const_iterator __hint, value_type&& __x)
373 __glibcxx_check_insert(__hint);
374 size_type __bucket_count = this->bucket_count();
375 auto __it = _Base::insert(__hint.base(), std::move(__x));
376 _M_check_rehashed(__bucket_count);
377 return { __it, this };
380 template<typename _Pair, typename = typename
381 std::enable_if<std::is_constructible<value_type,
382 _Pair&&>::value>::type>
384 insert(const_iterator __hint, _Pair&& __obj)
386 __glibcxx_check_insert(__hint);
387 size_type __bucket_count = this->bucket_count();
388 auto __it = _Base::insert(__hint.base(), std::forward<_Pair>(__obj));
389 _M_check_rehashed(__bucket_count);
390 return { __it, this };
394 insert(std::initializer_list<value_type> __l)
396 size_type __bucket_count = this->bucket_count();
398 _M_check_rehashed(__bucket_count);
401 template<typename _InputIterator>
403 insert(_InputIterator __first, _InputIterator __last)
405 typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
406 __glibcxx_check_valid_range2(__first, __last, __dist);
407 size_type __bucket_count = this->bucket_count();
409 if (__dist.second >= __gnu_debug::__dp_sign)
410 _Base::insert(__gnu_debug::__unsafe(__first),
411 __gnu_debug::__unsafe(__last));
413 _Base::insert(__first, __last);
415 _M_check_rehashed(__bucket_count);
418 #if __cplusplus > 201402L
419 template <typename... _Args>
421 try_emplace(const key_type& __k, _Args&&... __args)
423 auto __res = _Base::try_emplace(__k,
424 std::forward<_Args>(__args)...);
425 return { { __res.first, this }, __res.second };
428 template <typename... _Args>
430 try_emplace(key_type&& __k, _Args&&... __args)
432 auto __res = _Base::try_emplace(std::move(__k),
433 std::forward<_Args>(__args)...);
434 return { { __res.first, this }, __res.second };
437 template <typename... _Args>
439 try_emplace(const_iterator __hint, const key_type& __k,
442 __glibcxx_check_insert(__hint);
443 return { _Base::try_emplace(__hint.base(), __k,
444 std::forward<_Args>(__args)...),
448 template <typename... _Args>
450 try_emplace(const_iterator __hint, key_type&& __k, _Args&&... __args)
452 __glibcxx_check_insert(__hint);
453 return { _Base::try_emplace(__hint.base(), std::move(__k),
454 std::forward<_Args>(__args)...),
458 template <typename _Obj>
460 insert_or_assign(const key_type& __k, _Obj&& __obj)
462 auto __res = _Base::insert_or_assign(__k,
463 std::forward<_Obj>(__obj));
464 return { { __res.first, this }, __res.second };
467 template <typename _Obj>
469 insert_or_assign(key_type&& __k, _Obj&& __obj)
471 auto __res = _Base::insert_or_assign(std::move(__k),
472 std::forward<_Obj>(__obj));
473 return { { __res.first, this }, __res.second };
476 template <typename _Obj>
478 insert_or_assign(const_iterator __hint, const key_type& __k,
481 __glibcxx_check_insert(__hint);
482 return { _Base::insert_or_assign(__hint.base(), __k,
483 std::forward<_Obj>(__obj)),
487 template <typename _Obj>
489 insert_or_assign(const_iterator __hint, key_type&& __k, _Obj&& __obj)
491 __glibcxx_check_insert(__hint);
492 return { _Base::insert_or_assign(__hint.base(), std::move(__k),
493 std::forward<_Obj>(__obj)),
498 #if __cplusplus > 201402L
499 using node_type = typename _Base::node_type;
500 using insert_return_type = _Node_insert_return<iterator, node_type>;
503 extract(const_iterator __position)
505 __glibcxx_check_erase(__position);
506 return _M_extract(__position.base());
510 extract(const key_type& __key)
512 const auto __position = _Base::find(__key);
513 if (__position != _Base::end())
514 return _M_extract(__position);
519 insert(node_type&& __nh)
521 auto __ret = _Base::insert(std::move(__nh));
523 { { __ret.position, this }, __ret.inserted, std::move(__ret.node) };
527 insert(const_iterator __hint, node_type&& __nh)
529 __glibcxx_check_insert(__hint);
530 return { _Base::insert(__hint.base(), std::move(__nh)), this };
537 find(const key_type& __key)
538 { return { _Base::find(__key), this }; }
541 find(const key_type& __key) const
542 { return { _Base::find(__key), this }; }
544 std::pair<iterator, iterator>
545 equal_range(const key_type& __key)
547 auto __res = _Base::equal_range(__key);
548 return { { __res.first, this }, { __res.second, this } };
551 std::pair<const_iterator, const_iterator>
552 equal_range(const key_type& __key) const
554 auto __res = _Base::equal_range(__key);
555 return { { __res.first, this }, { __res.second, this } };
559 erase(const key_type& __key)
562 auto __victim = _Base::find(__key);
563 if (__victim != _Base::end())
572 erase(const_iterator __it)
574 __glibcxx_check_erase(__it);
575 return { _M_erase(__it.base()), this };
581 __glibcxx_check_erase(__it);
582 return { _M_erase(__it.base()), this };
586 erase(const_iterator __first, const_iterator __last)
588 __glibcxx_check_erase_range(__first, __last);
589 for (auto __tmp = __first.base(); __tmp != __last.base(); ++__tmp)
591 _GLIBCXX_DEBUG_VERIFY(__tmp != _Base::cend(),
592 _M_message(__gnu_debug::__msg_valid_range)
593 ._M_iterator(__first, "first")
594 ._M_iterator(__last, "last"));
595 _M_invalidate(__tmp);
598 size_type __bucket_count = this->bucket_count();
599 auto __next = _Base::erase(__first.base(), __last.base());
600 _M_check_rehashed(__bucket_count);
601 return { __next, this };
605 _M_base() noexcept { return *this; }
608 _M_base() const noexcept { return *this; }
612 _M_check_rehashed(size_type __prev_count)
614 if (__prev_count != this->bucket_count())
615 this->_M_invalidate_all();
619 _M_invalidate(_Base_const_iterator __victim)
621 this->_M_invalidate_if(
622 [__victim](_Base_const_iterator __it) { return __it == __victim; });
623 this->_M_invalidate_local_if(
624 [__victim](_Base_const_local_iterator __it)
625 { return __it._M_curr() == __victim._M_cur; });
629 _M_erase(_Base_const_iterator __victim)
631 _M_invalidate(__victim);
632 size_type __bucket_count = this->bucket_count();
633 _Base_iterator __next = _Base::erase(__victim);
634 _M_check_rehashed(__bucket_count);
638 #if __cplusplus > 201402L
640 _M_extract(_Base_const_iterator __victim)
642 _M_invalidate(__victim);
643 return _Base::extract(__victim);
648 #if __cpp_deduction_guides >= 201606
650 template<typename _InputIterator,
651 typename _Hash = hash<__iter_key_t<_InputIterator>>,
652 typename _Pred = equal_to<__iter_key_t<_InputIterator>>,
653 typename _Allocator = allocator<__iter_to_alloc_t<_InputIterator>>,
654 typename = _RequireInputIter<_InputIterator>,
655 typename = _RequireNotAllocatorOrIntegral<_Hash>,
656 typename = _RequireNotAllocator<_Pred>,
657 typename = _RequireAllocator<_Allocator>>
658 unordered_map(_InputIterator, _InputIterator,
659 typename unordered_map<int, int>::size_type = {},
660 _Hash = _Hash(), _Pred = _Pred(), _Allocator = _Allocator())
661 -> unordered_map<__iter_key_t<_InputIterator>,
662 __iter_val_t<_InputIterator>,
663 _Hash, _Pred, _Allocator>;
665 template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
666 typename _Pred = equal_to<_Key>,
667 typename _Allocator = allocator<pair<const _Key, _Tp>>,
668 typename = _RequireNotAllocatorOrIntegral<_Hash>,
669 typename = _RequireNotAllocator<_Pred>,
670 typename = _RequireAllocator<_Allocator>>
671 unordered_map(initializer_list<pair<_Key, _Tp>>,
672 typename unordered_map<int, int>::size_type = {},
673 _Hash = _Hash(), _Pred = _Pred(), _Allocator = _Allocator())
674 -> unordered_map<_Key, _Tp, _Hash, _Pred, _Allocator>;
676 template<typename _InputIterator, typename _Allocator,
677 typename = _RequireInputIter<_InputIterator>,
678 typename = _RequireAllocator<_Allocator>>
679 unordered_map(_InputIterator, _InputIterator,
680 typename unordered_map<int, int>::size_type, _Allocator)
681 -> unordered_map<__iter_key_t<_InputIterator>,
682 __iter_val_t<_InputIterator>,
683 hash<__iter_key_t<_InputIterator>>,
684 equal_to<__iter_key_t<_InputIterator>>,
687 template<typename _InputIterator, typename _Allocator,
688 typename = _RequireInputIter<_InputIterator>,
689 typename = _RequireAllocator<_Allocator>>
690 unordered_map(_InputIterator, _InputIterator, _Allocator)
691 -> unordered_map<__iter_key_t<_InputIterator>,
692 __iter_val_t<_InputIterator>,
693 hash<__iter_key_t<_InputIterator>>,
694 equal_to<__iter_key_t<_InputIterator>>,
697 template<typename _InputIterator, typename _Hash, typename _Allocator,
698 typename = _RequireInputIter<_InputIterator>,
699 typename = _RequireNotAllocatorOrIntegral<_Hash>,
700 typename = _RequireAllocator<_Allocator>>
701 unordered_map(_InputIterator, _InputIterator,
702 typename unordered_map<int, int>::size_type,
704 -> unordered_map<__iter_key_t<_InputIterator>,
705 __iter_val_t<_InputIterator>, _Hash,
706 equal_to<__iter_key_t<_InputIterator>>, _Allocator>;
708 template<typename _Key, typename _Tp, typename _Allocator,
709 typename = _RequireAllocator<_Allocator>>
710 unordered_map(initializer_list<pair<_Key, _Tp>>,
711 typename unordered_map<int, int>::size_type,
713 -> unordered_map<_Key, _Tp, hash<_Key>, equal_to<_Key>, _Allocator>;
715 template<typename _Key, typename _Tp, typename _Allocator,
716 typename = _RequireAllocator<_Allocator>>
717 unordered_map(initializer_list<pair<_Key, _Tp>>, _Allocator)
718 -> unordered_map<_Key, _Tp, hash<_Key>, equal_to<_Key>, _Allocator>;
720 template<typename _Key, typename _Tp, typename _Hash, typename _Allocator,
721 typename = _RequireNotAllocatorOrIntegral<_Hash>,
722 typename = _RequireAllocator<_Allocator>>
723 unordered_map(initializer_list<pair<_Key, _Tp>>,
724 typename unordered_map<int, int>::size_type,
726 -> unordered_map<_Key, _Tp, _Hash, equal_to<_Key>, _Allocator>;
730 template<typename _Key, typename _Tp, typename _Hash,
731 typename _Pred, typename _Alloc>
733 swap(unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
734 unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
735 noexcept(noexcept(__x.swap(__y)))
738 template<typename _Key, typename _Tp, typename _Hash,
739 typename _Pred, typename _Alloc>
741 operator==(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
742 const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
743 { return __x._M_base() == __y._M_base(); }
745 #if __cpp_impl_three_way_comparison < 201907L
746 template<typename _Key, typename _Tp, typename _Hash,
747 typename _Pred, typename _Alloc>
749 operator!=(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
750 const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
751 { return !(__x == __y); }
754 /// Class std::unordered_multimap with safety/checking/debug instrumentation.
755 template<typename _Key, typename _Tp,
756 typename _Hash = std::hash<_Key>,
757 typename _Pred = std::equal_to<_Key>,
758 typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
759 class unordered_multimap
760 : public __gnu_debug::_Safe_container<
761 unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>, _Alloc,
762 __gnu_debug::_Safe_unordered_container>,
763 public _GLIBCXX_STD_C::unordered_multimap<
764 _Key, _Tp, _Hash, _Pred, _Alloc>
766 typedef _GLIBCXX_STD_C::unordered_multimap<_Key, _Tp, _Hash,
767 _Pred, _Alloc> _Base;
768 typedef __gnu_debug::_Safe_container<unordered_multimap,
769 _Alloc, __gnu_debug::_Safe_unordered_container> _Safe;
770 typedef typename _Base::const_iterator _Base_const_iterator;
771 typedef typename _Base::iterator _Base_iterator;
772 typedef typename _Base::const_local_iterator _Base_const_local_iterator;
773 typedef typename _Base::local_iterator _Base_local_iterator;
775 template<typename _ItT, typename _SeqT, typename _CatT>
776 friend class ::__gnu_debug::_Safe_iterator;
777 template<typename _ItT, typename _SeqT>
778 friend class ::__gnu_debug::_Safe_local_iterator;
781 typedef typename _Base::size_type size_type;
782 typedef typename _Base::hasher hasher;
783 typedef typename _Base::key_equal key_equal;
784 typedef typename _Base::allocator_type allocator_type;
786 typedef typename _Base::key_type key_type;
787 typedef typename _Base::value_type value_type;
789 typedef __gnu_debug::_Safe_iterator<
790 _Base_iterator, unordered_multimap> iterator;
791 typedef __gnu_debug::_Safe_iterator<
792 _Base_const_iterator, unordered_multimap> const_iterator;
793 typedef __gnu_debug::_Safe_local_iterator<
794 _Base_local_iterator, unordered_multimap> local_iterator;
795 typedef __gnu_debug::_Safe_local_iterator<
796 _Base_const_local_iterator, unordered_multimap> const_local_iterator;
798 unordered_multimap() = default;
801 unordered_multimap(size_type __n,
802 const hasher& __hf = hasher(),
803 const key_equal& __eql = key_equal(),
804 const allocator_type& __a = allocator_type())
805 : _Base(__n, __hf, __eql, __a) { }
807 template<typename _InputIterator>
808 unordered_multimap(_InputIterator __first, _InputIterator __last,
810 const hasher& __hf = hasher(),
811 const key_equal& __eql = key_equal(),
812 const allocator_type& __a = allocator_type())
813 : _Base(__gnu_debug::__base(
814 __glibcxx_check_valid_constructor_range(__first, __last)),
815 __gnu_debug::__base(__last), __n,
816 __hf, __eql, __a) { }
818 unordered_multimap(const unordered_multimap&) = default;
820 unordered_multimap(const _Base& __x)
823 unordered_multimap(unordered_multimap&&) = default;
826 unordered_multimap(const allocator_type& __a)
829 unordered_multimap(const unordered_multimap& __umap,
830 const allocator_type& __a)
831 : _Base(__umap, __a) { }
833 unordered_multimap(unordered_multimap&& __umap,
834 const allocator_type& __a)
835 noexcept( noexcept(_Base(std::move(__umap._M_base()), __a)) )
836 : _Safe(std::move(__umap._M_safe()), __a),
837 _Base(std::move(__umap._M_base()), __a) { }
839 unordered_multimap(initializer_list<value_type> __l,
841 const hasher& __hf = hasher(),
842 const key_equal& __eql = key_equal(),
843 const allocator_type& __a = allocator_type())
844 : _Base(__l, __n, __hf, __eql, __a) { }
846 unordered_multimap(size_type __n, const allocator_type& __a)
847 : unordered_multimap(__n, hasher(), key_equal(), __a)
850 unordered_multimap(size_type __n, const hasher& __hf,
851 const allocator_type& __a)
852 : unordered_multimap(__n, __hf, key_equal(), __a)
855 template<typename _InputIterator>
856 unordered_multimap(_InputIterator __first, _InputIterator __last,
858 const allocator_type& __a)
859 : unordered_multimap(__first, __last, __n, hasher(), key_equal(), __a)
862 template<typename _InputIterator>
863 unordered_multimap(_InputIterator __first, _InputIterator __last,
864 size_type __n, const hasher& __hf,
865 const allocator_type& __a)
866 : unordered_multimap(__first, __last, __n, __hf, key_equal(), __a)
869 unordered_multimap(initializer_list<value_type> __l,
871 const allocator_type& __a)
872 : unordered_multimap(__l, __n, hasher(), key_equal(), __a)
875 unordered_multimap(initializer_list<value_type> __l,
876 size_type __n, const hasher& __hf,
877 const allocator_type& __a)
878 : unordered_multimap(__l, __n, __hf, key_equal(), __a)
881 ~unordered_multimap() = default;
884 operator=(const unordered_multimap&) = default;
887 operator=(unordered_multimap&&) = default;
890 operator=(initializer_list<value_type> __l)
892 this->_M_base() = __l;
893 this->_M_invalidate_all();
898 swap(unordered_multimap& __x)
899 noexcept( noexcept(declval<_Base&>().swap(__x)) )
909 this->_M_invalidate_all();
914 { return { _Base::begin(), this }; }
917 begin() const noexcept
918 { return { _Base::begin(), this }; }
922 { return { _Base::end(), this }; }
926 { return { _Base::end(), this }; }
929 cbegin() const noexcept
930 { return { _Base::cbegin(), this }; }
933 cend() const noexcept
934 { return { _Base::cend(), this }; }
940 __glibcxx_check_bucket_index(__b);
941 return { _Base::begin(__b), this };
947 __glibcxx_check_bucket_index(__b);
948 return { _Base::end(__b), this };
952 begin(size_type __b) const
954 __glibcxx_check_bucket_index(__b);
955 return { _Base::begin(__b), this };
959 end(size_type __b) const
961 __glibcxx_check_bucket_index(__b);
962 return { _Base::end(__b), this };
966 cbegin(size_type __b) const
968 __glibcxx_check_bucket_index(__b);
969 return { _Base::cbegin(__b), this };
973 cend(size_type __b) const
975 __glibcxx_check_bucket_index(__b);
976 return { _Base::cend(__b), this };
980 bucket_size(size_type __b) const
982 __glibcxx_check_bucket_index(__b);
983 return _Base::bucket_size(__b);
987 max_load_factor() const noexcept
988 { return _Base::max_load_factor(); }
991 max_load_factor(float __f)
993 __glibcxx_check_max_load_factor(__f);
994 _Base::max_load_factor(__f);
997 template<typename... _Args>
999 emplace(_Args&&... __args)
1001 size_type __bucket_count = this->bucket_count();
1002 auto __it = _Base::emplace(std::forward<_Args>(__args)...);
1003 _M_check_rehashed(__bucket_count);
1004 return { __it, this };
1007 template<typename... _Args>
1009 emplace_hint(const_iterator __hint, _Args&&... __args)
1011 __glibcxx_check_insert(__hint);
1012 size_type __bucket_count = this->bucket_count();
1013 auto __it = _Base::emplace_hint(__hint.base(),
1014 std::forward<_Args>(__args)...);
1015 _M_check_rehashed(__bucket_count);
1016 return { __it, this };
1020 insert(const value_type& __obj)
1022 size_type __bucket_count = this->bucket_count();
1023 auto __it = _Base::insert(__obj);
1024 _M_check_rehashed(__bucket_count);
1025 return { __it, this };
1028 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1029 // 2354. Unnecessary copying when inserting into maps with braced-init
1031 insert(value_type&& __x)
1033 size_type __bucket_count = this->bucket_count();
1034 auto __it = _Base::insert(std::move(__x));
1035 _M_check_rehashed(__bucket_count);
1036 return { __it, this };
1040 insert(const_iterator __hint, const value_type& __obj)
1042 __glibcxx_check_insert(__hint);
1043 size_type __bucket_count = this->bucket_count();
1044 auto __it = _Base::insert(__hint.base(), __obj);
1045 _M_check_rehashed(__bucket_count);
1046 return { __it, this };
1049 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1050 // 2354. Unnecessary copying when inserting into maps with braced-init
1052 insert(const_iterator __hint, value_type&& __x)
1054 __glibcxx_check_insert(__hint);
1055 size_type __bucket_count = this->bucket_count();
1056 auto __it = _Base::insert(__hint.base(), std::move(__x));
1057 _M_check_rehashed(__bucket_count);
1058 return { __it, this };
1061 template<typename _Pair, typename = typename
1062 std::enable_if<std::is_constructible<value_type,
1063 _Pair&&>::value>::type>
1065 insert(_Pair&& __obj)
1067 size_type __bucket_count = this->bucket_count();
1068 auto __it = _Base::insert(std::forward<_Pair>(__obj));
1069 _M_check_rehashed(__bucket_count);
1070 return { __it, this };
1073 template<typename _Pair, typename = typename
1074 std::enable_if<std::is_constructible<value_type,
1075 _Pair&&>::value>::type>
1077 insert(const_iterator __hint, _Pair&& __obj)
1079 __glibcxx_check_insert(__hint);
1080 size_type __bucket_count = this->bucket_count();
1081 auto __it = _Base::insert(__hint.base(), std::forward<_Pair>(__obj));
1082 _M_check_rehashed(__bucket_count);
1083 return { __it, this };
1087 insert(std::initializer_list<value_type> __l)
1088 { _Base::insert(__l); }
1090 template<typename _InputIterator>
1092 insert(_InputIterator __first, _InputIterator __last)
1094 typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
1095 __glibcxx_check_valid_range2(__first, __last, __dist);
1096 size_type __bucket_count = this->bucket_count();
1098 if (__dist.second >= __gnu_debug::__dp_sign)
1099 _Base::insert(__gnu_debug::__unsafe(__first),
1100 __gnu_debug::__unsafe(__last));
1102 _Base::insert(__first, __last);
1104 _M_check_rehashed(__bucket_count);
1107 #if __cplusplus > 201402L
1108 using node_type = typename _Base::node_type;
1111 extract(const_iterator __position)
1113 __glibcxx_check_erase(__position);
1114 return _M_extract(__position.base());
1118 extract(const key_type& __key)
1120 const auto __position = _Base::find(__key);
1121 if (__position != _Base::end())
1122 return _M_extract(__position);
1127 insert(node_type&& __nh)
1128 { return { _Base::insert(std::move(__nh)), this }; }
1131 insert(const_iterator __hint, node_type&& __nh)
1133 __glibcxx_check_insert(__hint);
1134 return { _Base::insert(__hint.base(), std::move(__nh)), this };
1141 find(const key_type& __key)
1142 { return { _Base::find(__key), this }; }
1145 find(const key_type& __key) const
1146 { return { _Base::find(__key), this }; }
1148 std::pair<iterator, iterator>
1149 equal_range(const key_type& __key)
1151 auto __res = _Base::equal_range(__key);
1152 return { { __res.first, this }, { __res.second, this } };
1155 std::pair<const_iterator, const_iterator>
1156 equal_range(const key_type& __key) const
1158 auto __res = _Base::equal_range(__key);
1159 return { { __res.first, this }, { __res.second, this } };
1163 erase(const key_type& __key)
1166 size_type __bucket_count = this->bucket_count();
1167 auto __pair = _Base::equal_range(__key);
1168 for (auto __victim = __pair.first; __victim != __pair.second;)
1170 _M_invalidate(__victim);
1171 __victim = _Base::erase(__victim);
1175 _M_check_rehashed(__bucket_count);
1180 erase(const_iterator __it)
1182 __glibcxx_check_erase(__it);
1183 return { _M_erase(__it.base()), this };
1187 erase(iterator __it)
1189 __glibcxx_check_erase(__it);
1190 return { _M_erase(__it.base()), this };
1194 erase(const_iterator __first, const_iterator __last)
1196 __glibcxx_check_erase_range(__first, __last);
1197 for (auto __tmp = __first.base(); __tmp != __last.base(); ++__tmp)
1199 _GLIBCXX_DEBUG_VERIFY(__tmp != _Base::cend(),
1200 _M_message(__gnu_debug::__msg_valid_range)
1201 ._M_iterator(__first, "first")
1202 ._M_iterator(__last, "last"));
1203 _M_invalidate(__tmp);
1206 size_type __bucket_count = this->bucket_count();
1207 auto __next = _Base::erase(__first.base(), __last.base());
1208 _M_check_rehashed(__bucket_count);
1209 return { __next, this };
1213 _M_base() noexcept { return *this; }
1216 _M_base() const noexcept { return *this; }
1220 _M_check_rehashed(size_type __prev_count)
1222 if (__prev_count != this->bucket_count())
1223 this->_M_invalidate_all();
1227 _M_invalidate(_Base_const_iterator __victim)
1229 this->_M_invalidate_if(
1230 [__victim](_Base_const_iterator __it) { return __it == __victim; });
1231 this->_M_invalidate_local_if(
1232 [__victim](_Base_const_local_iterator __it)
1233 { return __it._M_curr() == __victim._M_cur; });
1237 _M_erase(_Base_const_iterator __victim)
1239 _M_invalidate(__victim);
1240 size_type __bucket_count = this->bucket_count();
1241 _Base_iterator __next = _Base::erase(__victim);
1242 _M_check_rehashed(__bucket_count);
1246 #if __cplusplus > 201402L
1248 _M_extract(_Base_const_iterator __victim)
1250 _M_invalidate(__victim);
1251 return _Base::extract(__victim);
1256 #if __cpp_deduction_guides >= 201606
1258 template<typename _InputIterator,
1259 typename _Hash = hash<__iter_key_t<_InputIterator>>,
1260 typename _Pred = equal_to<__iter_key_t<_InputIterator>>,
1261 typename _Allocator = allocator<__iter_to_alloc_t<_InputIterator>>,
1262 typename = _RequireInputIter<_InputIterator>,
1263 typename = _RequireNotAllocatorOrIntegral<_Hash>,
1264 typename = _RequireNotAllocator<_Pred>,
1265 typename = _RequireAllocator<_Allocator>>
1266 unordered_multimap(_InputIterator, _InputIterator,
1267 unordered_multimap<int, int>::size_type = {},
1268 _Hash = _Hash(), _Pred = _Pred(),
1269 _Allocator = _Allocator())
1270 -> unordered_multimap<__iter_key_t<_InputIterator>,
1271 __iter_val_t<_InputIterator>, _Hash, _Pred,
1274 template<typename _Key, typename _Tp, typename _Hash = hash<_Key>,
1275 typename _Pred = equal_to<_Key>,
1276 typename _Allocator = allocator<pair<const _Key, _Tp>>,
1277 typename = _RequireNotAllocatorOrIntegral<_Hash>,
1278 typename = _RequireNotAllocator<_Pred>,
1279 typename = _RequireAllocator<_Allocator>>
1280 unordered_multimap(initializer_list<pair<_Key, _Tp>>,
1281 unordered_multimap<int, int>::size_type = {},
1282 _Hash = _Hash(), _Pred = _Pred(),
1283 _Allocator = _Allocator())
1284 -> unordered_multimap<_Key, _Tp, _Hash, _Pred, _Allocator>;
1286 template<typename _InputIterator, typename _Allocator,
1287 typename = _RequireInputIter<_InputIterator>,
1288 typename = _RequireAllocator<_Allocator>>
1289 unordered_multimap(_InputIterator, _InputIterator,
1290 unordered_multimap<int, int>::size_type, _Allocator)
1291 -> unordered_multimap<__iter_key_t<_InputIterator>,
1292 __iter_val_t<_InputIterator>,
1293 hash<__iter_key_t<_InputIterator>>,
1294 equal_to<__iter_key_t<_InputIterator>>, _Allocator>;
1296 template<typename _InputIterator, typename _Allocator,
1297 typename = _RequireInputIter<_InputIterator>,
1298 typename = _RequireAllocator<_Allocator>>
1299 unordered_multimap(_InputIterator, _InputIterator, _Allocator)
1300 -> unordered_multimap<__iter_key_t<_InputIterator>,
1301 __iter_val_t<_InputIterator>,
1302 hash<__iter_key_t<_InputIterator>>,
1303 equal_to<__iter_key_t<_InputIterator>>, _Allocator>;
1305 template<typename _InputIterator, typename _Hash, typename _Allocator,
1306 typename = _RequireInputIter<_InputIterator>,
1307 typename = _RequireNotAllocatorOrIntegral<_Hash>,
1308 typename = _RequireAllocator<_Allocator>>
1309 unordered_multimap(_InputIterator, _InputIterator,
1310 unordered_multimap<int, int>::size_type, _Hash,
1312 -> unordered_multimap<__iter_key_t<_InputIterator>,
1313 __iter_val_t<_InputIterator>, _Hash,
1314 equal_to<__iter_key_t<_InputIterator>>, _Allocator>;
1316 template<typename _Key, typename _Tp, typename _Allocator,
1317 typename = _RequireAllocator<_Allocator>>
1318 unordered_multimap(initializer_list<pair<_Key, _Tp>>,
1319 unordered_multimap<int, int>::size_type,
1321 -> unordered_multimap<_Key, _Tp, hash<_Key>, equal_to<_Key>, _Allocator>;
1323 template<typename _Key, typename _Tp, typename _Allocator,
1324 typename = _RequireAllocator<_Allocator>>
1325 unordered_multimap(initializer_list<pair<_Key, _Tp>>, _Allocator)
1326 -> unordered_multimap<_Key, _Tp, hash<_Key>, equal_to<_Key>, _Allocator>;
1328 template<typename _Key, typename _Tp, typename _Hash, typename _Allocator,
1329 typename = _RequireNotAllocatorOrIntegral<_Hash>,
1330 typename = _RequireAllocator<_Allocator>>
1331 unordered_multimap(initializer_list<pair<_Key, _Tp>>,
1332 unordered_multimap<int, int>::size_type,
1334 -> unordered_multimap<_Key, _Tp, _Hash, equal_to<_Key>, _Allocator>;
1338 template<typename _Key, typename _Tp, typename _Hash,
1339 typename _Pred, typename _Alloc>
1341 swap(unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
1342 unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
1343 noexcept(noexcept(__x.swap(__y)))
1346 template<typename _Key, typename _Tp, typename _Hash,
1347 typename _Pred, typename _Alloc>
1349 operator==(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
1350 const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
1351 { return __x._M_base() == __y._M_base(); }
1353 #if __cpp_impl_three_way_comparison < 201907L
1354 template<typename _Key, typename _Tp, typename _Hash,
1355 typename _Pred, typename _Alloc>
1357 operator!=(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
1358 const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
1359 { return !(__x == __y); }
1362 } // namespace __debug