1 // Debugging unordered_set/unordered_multiset implementation -*- C++ -*-
3 // Copyright (C) 2003-2018 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_set
26 * This file is a GNU debug extension to the Standard C++ Library.
29 #ifndef _GLIBCXX_DEBUG_UNORDERED_SET
30 #define _GLIBCXX_DEBUG_UNORDERED_SET 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
35 # include <bits/c++0x_warning.h>
37 # include <unordered_set>
39 #include <debug/safe_unordered_container.h>
40 #include <debug/safe_container.h>
41 #include <debug/safe_iterator.h>
42 #include <debug/safe_local_iterator.h>
44 namespace std _GLIBCXX_VISIBILITY(default)
48 /// Class std::unordered_set with safety/checking/debug instrumentation.
49 template<typename _Value,
50 typename _Hash = std::hash<_Value>,
51 typename _Pred = std::equal_to<_Value>,
52 typename _Alloc = std::allocator<_Value> >
54 : public __gnu_debug::_Safe_container<
55 unordered_set<_Value, _Hash, _Pred, _Alloc>, _Alloc,
56 __gnu_debug::_Safe_unordered_container>,
57 public _GLIBCXX_STD_C::unordered_set<_Value, _Hash, _Pred, _Alloc>
59 typedef _GLIBCXX_STD_C::unordered_set<
60 _Value, _Hash, _Pred, _Alloc> _Base;
61 typedef __gnu_debug::_Safe_container<
62 unordered_set, _Alloc, __gnu_debug::_Safe_unordered_container> _Safe;
64 typedef typename _Base::const_iterator _Base_const_iterator;
65 typedef typename _Base::iterator _Base_iterator;
66 typedef typename _Base::const_local_iterator _Base_const_local_iterator;
67 typedef typename _Base::local_iterator _Base_local_iterator;
70 typedef typename _Base::size_type size_type;
71 typedef typename _Base::hasher hasher;
72 typedef typename _Base::key_equal key_equal;
73 typedef typename _Base::allocator_type allocator_type;
75 typedef typename _Base::key_type key_type;
76 typedef typename _Base::value_type value_type;
78 typedef __gnu_debug::_Safe_iterator<
79 _Base_iterator, unordered_set> iterator;
80 typedef __gnu_debug::_Safe_iterator<
81 _Base_const_iterator, unordered_set> const_iterator;
82 typedef __gnu_debug::_Safe_local_iterator<
83 _Base_local_iterator, unordered_set> local_iterator;
84 typedef __gnu_debug::_Safe_local_iterator<
85 _Base_const_local_iterator, unordered_set> const_local_iterator;
87 unordered_set() = default;
90 unordered_set(size_type __n,
91 const hasher& __hf = hasher(),
92 const key_equal& __eql = key_equal(),
93 const allocator_type& __a = allocator_type())
94 : _Base(__n, __hf, __eql, __a) { }
96 template<typename _InputIterator>
97 unordered_set(_InputIterator __first, _InputIterator __last,
99 const hasher& __hf = hasher(),
100 const key_equal& __eql = key_equal(),
101 const allocator_type& __a = allocator_type())
102 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
104 __gnu_debug::__base(__last), __n,
105 __hf, __eql, __a) { }
107 unordered_set(const unordered_set&) = default;
109 unordered_set(const _Base& __x)
112 unordered_set(unordered_set&&) = default;
115 unordered_set(const allocator_type& __a)
118 unordered_set(const unordered_set& __uset,
119 const allocator_type& __a)
120 : _Base(__uset, __a) { }
122 unordered_set(unordered_set&& __uset,
123 const allocator_type& __a)
124 : _Safe(std::move(__uset._M_safe()), __a),
125 _Base(std::move(__uset._M_base()), __a) { }
127 unordered_set(initializer_list<value_type> __l,
129 const hasher& __hf = hasher(),
130 const key_equal& __eql = key_equal(),
131 const allocator_type& __a = allocator_type())
132 : _Base(__l, __n, __hf, __eql, __a) { }
134 unordered_set(size_type __n, const allocator_type& __a)
135 : unordered_set(__n, hasher(), key_equal(), __a)
138 unordered_set(size_type __n, const hasher& __hf,
139 const allocator_type& __a)
140 : unordered_set(__n, __hf, key_equal(), __a)
143 template<typename _InputIterator>
144 unordered_set(_InputIterator __first, _InputIterator __last,
146 const allocator_type& __a)
147 : unordered_set(__first, __last, __n, hasher(), key_equal(), __a)
150 template<typename _InputIterator>
151 unordered_set(_InputIterator __first, _InputIterator __last,
152 size_type __n, const hasher& __hf,
153 const allocator_type& __a)
154 : unordered_set(__first, __last, __n, __hf, key_equal(), __a)
157 unordered_set(initializer_list<value_type> __l,
159 const allocator_type& __a)
160 : unordered_set(__l, __n, hasher(), key_equal(), __a)
163 unordered_set(initializer_list<value_type> __l,
164 size_type __n, const hasher& __hf,
165 const allocator_type& __a)
166 : unordered_set(__l, __n, __hf, key_equal(), __a)
169 ~unordered_set() = default;
172 operator=(const unordered_set&) = default;
175 operator=(unordered_set&&) = default;
178 operator=(initializer_list<value_type> __l)
181 this->_M_invalidate_all();
186 swap(unordered_set& __x)
187 noexcept( noexcept(declval<_Base&>().swap(__x)) )
197 this->_M_invalidate_all();
202 { return iterator(_Base::begin(), this); }
205 begin() const noexcept
206 { return const_iterator(_Base::begin(), this); }
210 { return iterator(_Base::end(), this); }
214 { return const_iterator(_Base::end(), this); }
217 cbegin() const noexcept
218 { return const_iterator(_Base::begin(), this); }
221 cend() const noexcept
222 { return const_iterator(_Base::end(), this); }
228 __glibcxx_check_bucket_index(__b);
229 return local_iterator(_Base::begin(__b), this);
235 __glibcxx_check_bucket_index(__b);
236 return local_iterator(_Base::end(__b), this);
240 begin(size_type __b) const
242 __glibcxx_check_bucket_index(__b);
243 return const_local_iterator(_Base::begin(__b), this);
247 end(size_type __b) const
249 __glibcxx_check_bucket_index(__b);
250 return const_local_iterator(_Base::end(__b), this);
254 cbegin(size_type __b) const
256 __glibcxx_check_bucket_index(__b);
257 return const_local_iterator(_Base::cbegin(__b), this);
261 cend(size_type __b) const
263 __glibcxx_check_bucket_index(__b);
264 return const_local_iterator(_Base::cend(__b), this);
268 bucket_size(size_type __b) const
270 __glibcxx_check_bucket_index(__b);
271 return _Base::bucket_size(__b);
275 max_load_factor() const noexcept
276 { return _Base::max_load_factor(); }
279 max_load_factor(float __f)
281 __glibcxx_check_max_load_factor(__f);
282 _Base::max_load_factor(__f);
285 template<typename... _Args>
286 std::pair<iterator, bool>
287 emplace(_Args&&... __args)
289 size_type __bucket_count = this->bucket_count();
290 std::pair<_Base_iterator, bool> __res
291 = _Base::emplace(std::forward<_Args>(__args)...);
292 _M_check_rehashed(__bucket_count);
293 return std::make_pair(iterator(__res.first, this), __res.second);
296 template<typename... _Args>
298 emplace_hint(const_iterator __hint, _Args&&... __args)
300 __glibcxx_check_insert(__hint);
301 size_type __bucket_count = this->bucket_count();
302 _Base_iterator __it = _Base::emplace_hint(__hint.base(),
303 std::forward<_Args>(__args)...);
304 _M_check_rehashed(__bucket_count);
305 return iterator(__it, this);
308 std::pair<iterator, bool>
309 insert(const value_type& __obj)
311 size_type __bucket_count = this->bucket_count();
312 std::pair<_Base_iterator, bool> __res
313 = _Base::insert(__obj);
314 _M_check_rehashed(__bucket_count);
315 return std::make_pair(iterator(__res.first, this), __res.second);
319 insert(const_iterator __hint, const value_type& __obj)
321 __glibcxx_check_insert(__hint);
322 size_type __bucket_count = this->bucket_count();
323 _Base_iterator __it = _Base::insert(__hint.base(), __obj);
324 _M_check_rehashed(__bucket_count);
325 return iterator(__it, this);
328 std::pair<iterator, bool>
329 insert(value_type&& __obj)
331 size_type __bucket_count = this->bucket_count();
332 std::pair<_Base_iterator, bool> __res
333 = _Base::insert(std::move(__obj));
334 _M_check_rehashed(__bucket_count);
335 return std::make_pair(iterator(__res.first, this), __res.second);
339 insert(const_iterator __hint, value_type&& __obj)
341 __glibcxx_check_insert(__hint);
342 size_type __bucket_count = this->bucket_count();
343 _Base_iterator __it = _Base::insert(__hint.base(), std::move(__obj));
344 _M_check_rehashed(__bucket_count);
345 return iterator(__it, this);
349 insert(std::initializer_list<value_type> __l)
351 size_type __bucket_count = this->bucket_count();
353 _M_check_rehashed(__bucket_count);
356 template<typename _InputIterator>
358 insert(_InputIterator __first, _InputIterator __last)
360 typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
361 __glibcxx_check_valid_range2(__first, __last, __dist);
362 size_type __bucket_count = this->bucket_count();
364 if (__dist.second >= __gnu_debug::__dp_sign)
365 _Base::insert(__gnu_debug::__unsafe(__first),
366 __gnu_debug::__unsafe(__last));
368 _Base::insert(__first, __last);
370 _M_check_rehashed(__bucket_count);
373 #if __cplusplus > 201402L
374 using node_type = typename _Base::node_type;
375 using insert_return_type = _Node_insert_return<iterator, node_type>;
378 extract(const_iterator __position)
380 __glibcxx_check_erase(__position);
381 _Base_const_iterator __victim = __position.base();
382 this->_M_invalidate_if(
383 [__victim](_Base_const_iterator __it) { return __it == __victim; }
385 this->_M_invalidate_local_if(
386 [__victim](_Base_const_local_iterator __it) {
387 return __it._M_curr() == __victim._M_cur;
389 return _Base::extract(__position.base());
393 extract(const key_type& __key)
395 const auto __position = find(__key);
396 if (__position != end())
397 return extract(__position);
402 insert(node_type&& __nh)
404 auto __ret = _Base::insert(std::move(__nh));
405 iterator __pos = iterator(__ret.position, this);
406 return { __pos, __ret.inserted, std::move(__ret.node) };
410 insert(const_iterator __hint, node_type&& __nh)
412 __glibcxx_check_insert(__hint);
413 return iterator(_Base::insert(__hint.base(), std::move(__nh)), this);
420 find(const key_type& __key)
421 { return iterator(_Base::find(__key), this); }
424 find(const key_type& __key) const
425 { return const_iterator(_Base::find(__key), this); }
427 std::pair<iterator, iterator>
428 equal_range(const key_type& __key)
430 std::pair<_Base_iterator, _Base_iterator> __res
431 = _Base::equal_range(__key);
432 return std::make_pair(iterator(__res.first, this),
433 iterator(__res.second, this));
436 std::pair<const_iterator, const_iterator>
437 equal_range(const key_type& __key) const
439 std::pair<_Base_const_iterator, _Base_const_iterator>
440 __res = _Base::equal_range(__key);
441 return std::make_pair(const_iterator(__res.first, this),
442 const_iterator(__res.second, this));
446 erase(const key_type& __key)
449 _Base_iterator __victim(_Base::find(__key));
450 if (__victim != _Base::end())
452 this->_M_invalidate_if(
453 [__victim](_Base_const_iterator __it)
454 { return __it == __victim; });
455 this->_M_invalidate_local_if(
456 [__victim](_Base_const_local_iterator __it)
457 { return __it._M_curr() == __victim._M_cur; });
458 size_type __bucket_count = this->bucket_count();
459 _Base::erase(__victim);
460 _M_check_rehashed(__bucket_count);
467 erase(const_iterator __it)
469 __glibcxx_check_erase(__it);
470 _Base_const_iterator __victim = __it.base();
471 this->_M_invalidate_if(
472 [__victim](_Base_const_iterator __it)
473 { return __it == __victim; });
474 this->_M_invalidate_local_if(
475 [__victim](_Base_const_local_iterator __it)
476 { return __it._M_curr() == __victim._M_cur; });
477 size_type __bucket_count = this->bucket_count();
478 _Base_iterator __next = _Base::erase(__it.base());
479 _M_check_rehashed(__bucket_count);
480 return iterator(__next, this);
485 { return erase(const_iterator(__it)); }
488 erase(const_iterator __first, const_iterator __last)
490 __glibcxx_check_erase_range(__first, __last);
491 for (_Base_const_iterator __tmp = __first.base();
492 __tmp != __last.base(); ++__tmp)
494 _GLIBCXX_DEBUG_VERIFY(__tmp != _Base::end(),
495 _M_message(__gnu_debug::__msg_valid_range)
496 ._M_iterator(__first, "first")
497 ._M_iterator(__last, "last"));
498 this->_M_invalidate_if(
499 [__tmp](_Base_const_iterator __it)
500 { return __it == __tmp; });
501 this->_M_invalidate_local_if(
502 [__tmp](_Base_const_local_iterator __it)
503 { return __it._M_curr() == __tmp._M_cur; });
505 size_type __bucket_count = this->bucket_count();
506 _Base_iterator __next = _Base::erase(__first.base(),
508 _M_check_rehashed(__bucket_count);
509 return iterator(__next, this);
513 _M_base() noexcept { return *this; }
516 _M_base() const noexcept { return *this; }
520 _M_check_rehashed(size_type __prev_count)
522 if (__prev_count != this->bucket_count())
523 this->_M_invalidate_locals();
527 #if __cpp_deduction_guides >= 201606
529 template<typename _InputIterator,
531 hash<typename iterator_traits<_InputIterator>::value_type>,
533 equal_to<typename iterator_traits<_InputIterator>::value_type>,
534 typename _Allocator =
535 allocator<typename iterator_traits<_InputIterator>::value_type>,
536 typename = _RequireInputIter<_InputIterator>,
537 typename = _RequireAllocator<_Allocator>>
538 unordered_set(_InputIterator, _InputIterator,
539 unordered_set<int>::size_type = {},
540 _Hash = _Hash(), _Pred = _Pred(), _Allocator = _Allocator())
541 -> unordered_set<typename iterator_traits<_InputIterator>::value_type,
542 _Hash, _Pred, _Allocator>;
544 template<typename _Tp, typename _Hash = hash<_Tp>,
545 typename _Pred = equal_to<_Tp>,
546 typename _Allocator = allocator<_Tp>,
547 typename = _RequireAllocator<_Allocator>>
548 unordered_set(initializer_list<_Tp>,
549 unordered_set<int>::size_type = {},
550 _Hash = _Hash(), _Pred = _Pred(), _Allocator = _Allocator())
551 -> unordered_set<_Tp, _Hash, _Pred, _Allocator>;
553 template<typename _InputIterator, typename _Allocator,
554 typename = _RequireInputIter<_InputIterator>,
555 typename = _RequireAllocator<_Allocator>>
556 unordered_set(_InputIterator, _InputIterator,
557 unordered_set<int>::size_type, _Allocator)
558 -> unordered_set<typename iterator_traits<_InputIterator>::value_type,
560 typename iterator_traits<_InputIterator>::value_type>,
562 typename iterator_traits<_InputIterator>::value_type>,
565 template<typename _InputIterator, typename _Hash, typename _Allocator,
566 typename = _RequireInputIter<_InputIterator>,
567 typename = _RequireAllocator<_Allocator>>
568 unordered_set(_InputIterator, _InputIterator,
569 unordered_set<int>::size_type,
571 -> unordered_set<typename iterator_traits<_InputIterator>::value_type,
574 typename iterator_traits<_InputIterator>::value_type>,
577 template<typename _Tp, typename _Allocator,
578 typename = _RequireAllocator<_Allocator>>
579 unordered_set(initializer_list<_Tp>,
580 unordered_set<int>::size_type, _Allocator)
581 -> unordered_set<_Tp, hash<_Tp>, equal_to<_Tp>, _Allocator>;
583 template<typename _Tp, typename _Hash, typename _Allocator,
584 typename = _RequireAllocator<_Allocator>>
585 unordered_set(initializer_list<_Tp>,
586 unordered_set<int>::size_type, _Hash, _Allocator)
587 -> unordered_set<_Tp, _Hash, equal_to<_Tp>, _Allocator>;
591 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
593 swap(unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
594 unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
595 noexcept(noexcept(__x.swap(__y)))
598 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
600 operator==(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
601 const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
602 { return __x._M_base() == __y._M_base(); }
604 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
606 operator!=(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
607 const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
608 { return !(__x == __y); }
611 /// Class std::unordered_multiset with safety/checking/debug instrumentation.
612 template<typename _Value,
613 typename _Hash = std::hash<_Value>,
614 typename _Pred = std::equal_to<_Value>,
615 typename _Alloc = std::allocator<_Value> >
616 class unordered_multiset
617 : public __gnu_debug::_Safe_container<
618 unordered_multiset<_Value, _Hash, _Pred, _Alloc>, _Alloc,
619 __gnu_debug::_Safe_unordered_container>,
620 public _GLIBCXX_STD_C::unordered_multiset<_Value, _Hash, _Pred, _Alloc>
622 typedef _GLIBCXX_STD_C::unordered_multiset<
623 _Value, _Hash, _Pred, _Alloc> _Base;
624 typedef __gnu_debug::_Safe_container<unordered_multiset,
625 _Alloc, __gnu_debug::_Safe_unordered_container> _Safe;
626 typedef typename _Base::const_iterator _Base_const_iterator;
627 typedef typename _Base::iterator _Base_iterator;
628 typedef typename _Base::const_local_iterator
629 _Base_const_local_iterator;
630 typedef typename _Base::local_iterator _Base_local_iterator;
633 typedef typename _Base::size_type size_type;
634 typedef typename _Base::hasher hasher;
635 typedef typename _Base::key_equal key_equal;
636 typedef typename _Base::allocator_type allocator_type;
638 typedef typename _Base::key_type key_type;
639 typedef typename _Base::value_type value_type;
641 typedef __gnu_debug::_Safe_iterator<
642 _Base_iterator, unordered_multiset> iterator;
643 typedef __gnu_debug::_Safe_iterator<
644 _Base_const_iterator, unordered_multiset> const_iterator;
645 typedef __gnu_debug::_Safe_local_iterator<
646 _Base_local_iterator, unordered_multiset> local_iterator;
647 typedef __gnu_debug::_Safe_local_iterator<
648 _Base_const_local_iterator, unordered_multiset> const_local_iterator;
650 unordered_multiset() = default;
653 unordered_multiset(size_type __n,
654 const hasher& __hf = hasher(),
655 const key_equal& __eql = key_equal(),
656 const allocator_type& __a = allocator_type())
657 : _Base(__n, __hf, __eql, __a) { }
659 template<typename _InputIterator>
660 unordered_multiset(_InputIterator __first, _InputIterator __last,
662 const hasher& __hf = hasher(),
663 const key_equal& __eql = key_equal(),
664 const allocator_type& __a = allocator_type())
665 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
667 __gnu_debug::__base(__last), __n,
668 __hf, __eql, __a) { }
670 unordered_multiset(const unordered_multiset&) = default;
672 unordered_multiset(const _Base& __x)
675 unordered_multiset(unordered_multiset&&) = default;
678 unordered_multiset(const allocator_type& __a)
681 unordered_multiset(const unordered_multiset& __uset,
682 const allocator_type& __a)
683 : _Base(__uset, __a) { }
685 unordered_multiset(unordered_multiset&& __uset,
686 const allocator_type& __a)
687 : _Safe(std::move(__uset._M_safe()), __a),
688 _Base(std::move(__uset._M_base()), __a) { }
690 unordered_multiset(initializer_list<value_type> __l,
692 const hasher& __hf = hasher(),
693 const key_equal& __eql = key_equal(),
694 const allocator_type& __a = allocator_type())
695 : _Base(__l, __n, __hf, __eql, __a) { }
697 unordered_multiset(size_type __n, const allocator_type& __a)
698 : unordered_multiset(__n, hasher(), key_equal(), __a)
701 unordered_multiset(size_type __n, const hasher& __hf,
702 const allocator_type& __a)
703 : unordered_multiset(__n, __hf, key_equal(), __a)
706 template<typename _InputIterator>
707 unordered_multiset(_InputIterator __first, _InputIterator __last,
709 const allocator_type& __a)
710 : unordered_multiset(__first, __last, __n, hasher(), key_equal(), __a)
713 template<typename _InputIterator>
714 unordered_multiset(_InputIterator __first, _InputIterator __last,
715 size_type __n, const hasher& __hf,
716 const allocator_type& __a)
717 : unordered_multiset(__first, __last, __n, __hf, key_equal(), __a)
720 unordered_multiset(initializer_list<value_type> __l,
722 const allocator_type& __a)
723 : unordered_multiset(__l, __n, hasher(), key_equal(), __a)
726 unordered_multiset(initializer_list<value_type> __l,
727 size_type __n, const hasher& __hf,
728 const allocator_type& __a)
729 : unordered_multiset(__l, __n, __hf, key_equal(), __a)
732 ~unordered_multiset() = default;
735 operator=(const unordered_multiset&) = default;
738 operator=(unordered_multiset&&) = default;
741 operator=(initializer_list<value_type> __l)
743 this->_M_base() = __l;
744 this->_M_invalidate_all();
749 swap(unordered_multiset& __x)
750 noexcept( noexcept(declval<_Base&>().swap(__x)) )
760 this->_M_invalidate_all();
765 { return iterator(_Base::begin(), this); }
768 begin() const noexcept
769 { return const_iterator(_Base::begin(), this); }
773 { return iterator(_Base::end(), this); }
777 { return const_iterator(_Base::end(), this); }
780 cbegin() const noexcept
781 { return const_iterator(_Base::begin(), this); }
784 cend() const noexcept
785 { return const_iterator(_Base::end(), this); }
791 __glibcxx_check_bucket_index(__b);
792 return local_iterator(_Base::begin(__b), this);
798 __glibcxx_check_bucket_index(__b);
799 return local_iterator(_Base::end(__b), this);
803 begin(size_type __b) const
805 __glibcxx_check_bucket_index(__b);
806 return const_local_iterator(_Base::begin(__b), this);
810 end(size_type __b) const
812 __glibcxx_check_bucket_index(__b);
813 return const_local_iterator(_Base::end(__b), this);
817 cbegin(size_type __b) const
819 __glibcxx_check_bucket_index(__b);
820 return const_local_iterator(_Base::cbegin(__b), this);
824 cend(size_type __b) const
826 __glibcxx_check_bucket_index(__b);
827 return const_local_iterator(_Base::cend(__b), this);
831 bucket_size(size_type __b) const
833 __glibcxx_check_bucket_index(__b);
834 return _Base::bucket_size(__b);
838 max_load_factor() const noexcept
839 { return _Base::max_load_factor(); }
842 max_load_factor(float __f)
844 __glibcxx_check_max_load_factor(__f);
845 _Base::max_load_factor(__f);
848 template<typename... _Args>
850 emplace(_Args&&... __args)
852 size_type __bucket_count = this->bucket_count();
854 = _Base::emplace(std::forward<_Args>(__args)...);
855 _M_check_rehashed(__bucket_count);
856 return iterator(__it, this);
859 template<typename... _Args>
861 emplace_hint(const_iterator __hint, _Args&&... __args)
863 __glibcxx_check_insert(__hint);
864 size_type __bucket_count = this->bucket_count();
865 _Base_iterator __it = _Base::emplace_hint(__hint.base(),
866 std::forward<_Args>(__args)...);
867 _M_check_rehashed(__bucket_count);
868 return iterator(__it, this);
872 insert(const value_type& __obj)
874 size_type __bucket_count = this->bucket_count();
875 _Base_iterator __it = _Base::insert(__obj);
876 _M_check_rehashed(__bucket_count);
877 return iterator(__it, this);
881 insert(const_iterator __hint, const value_type& __obj)
883 __glibcxx_check_insert(__hint);
884 size_type __bucket_count = this->bucket_count();
885 _Base_iterator __it = _Base::insert(__hint.base(), __obj);
886 _M_check_rehashed(__bucket_count);
887 return iterator(__it, this);
891 insert(value_type&& __obj)
893 size_type __bucket_count = this->bucket_count();
894 _Base_iterator __it = _Base::insert(std::move(__obj));
895 _M_check_rehashed(__bucket_count);
896 return iterator(__it, this);
900 insert(const_iterator __hint, value_type&& __obj)
902 __glibcxx_check_insert(__hint);
903 size_type __bucket_count = this->bucket_count();
904 _Base_iterator __it = _Base::insert(__hint.base(), std::move(__obj));
905 _M_check_rehashed(__bucket_count);
906 return iterator(__it, this);
910 insert(std::initializer_list<value_type> __l)
912 size_type __bucket_count = this->bucket_count();
914 _M_check_rehashed(__bucket_count);
917 template<typename _InputIterator>
919 insert(_InputIterator __first, _InputIterator __last)
921 typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
922 __glibcxx_check_valid_range2(__first, __last, __dist);
923 size_type __bucket_count = this->bucket_count();
925 if (__dist.second >= __gnu_debug::__dp_sign)
926 _Base::insert(__gnu_debug::__unsafe(__first),
927 __gnu_debug::__unsafe(__last));
929 _Base::insert(__first, __last);
931 _M_check_rehashed(__bucket_count);
934 #if __cplusplus > 201402L
935 using node_type = typename _Base::node_type;
938 extract(const_iterator __position)
940 __glibcxx_check_erase(__position);
941 _Base_const_iterator __victim = __position.base();
942 this->_M_invalidate_if(
943 [__victim](_Base_const_iterator __it) { return __it == __victim; }
945 this->_M_invalidate_local_if(
946 [__victim](_Base_const_local_iterator __it) {
947 return __it._M_curr() == __victim._M_cur;
949 return _Base::extract(__position.base());
953 extract(const key_type& __key)
955 const auto __position = find(__key);
956 if (__position != end())
957 return extract(__position);
962 insert(node_type&& __nh)
963 { return iterator(_Base::insert(std::move(__nh)), this); }
966 insert(const_iterator __hint, node_type&& __nh)
968 __glibcxx_check_insert(__hint);
969 return iterator(_Base::insert(__hint.base(), std::move(__nh)), this);
976 find(const key_type& __key)
977 { return iterator(_Base::find(__key), this); }
980 find(const key_type& __key) const
981 { return const_iterator(_Base::find(__key), this); }
983 std::pair<iterator, iterator>
984 equal_range(const key_type& __key)
986 std::pair<_Base_iterator, _Base_iterator> __res
987 = _Base::equal_range(__key);
988 return std::make_pair(iterator(__res.first, this),
989 iterator(__res.second, this));
992 std::pair<const_iterator, const_iterator>
993 equal_range(const key_type& __key) const
995 std::pair<_Base_const_iterator, _Base_const_iterator>
996 __res = _Base::equal_range(__key);
997 return std::make_pair(const_iterator(__res.first, this),
998 const_iterator(__res.second, this));
1002 erase(const key_type& __key)
1005 std::pair<_Base_iterator, _Base_iterator> __pair =
1006 _Base::equal_range(__key);
1007 for (_Base_iterator __victim = __pair.first; __victim != __pair.second;)
1009 this->_M_invalidate_if([__victim](_Base_const_iterator __it)
1010 { return __it == __victim; });
1011 this->_M_invalidate_local_if(
1012 [__victim](_Base_const_local_iterator __it)
1013 { return __it._M_curr() == __victim._M_cur; });
1014 _Base::erase(__victim++);
1021 erase(const_iterator __it)
1023 __glibcxx_check_erase(__it);
1024 _Base_const_iterator __victim = __it.base();
1025 this->_M_invalidate_if([__victim](_Base_const_iterator __it)
1026 { return __it == __victim; });
1027 this->_M_invalidate_local_if(
1028 [__victim](_Base_const_local_iterator __it)
1029 { return __it._M_curr() == __victim._M_cur; });
1030 return iterator(_Base::erase(__it.base()), this);
1034 erase(iterator __it)
1035 { return erase(const_iterator(__it)); }
1038 erase(const_iterator __first, const_iterator __last)
1040 __glibcxx_check_erase_range(__first, __last);
1041 for (_Base_const_iterator __tmp = __first.base();
1042 __tmp != __last.base(); ++__tmp)
1044 _GLIBCXX_DEBUG_VERIFY(__tmp != _Base::end(),
1045 _M_message(__gnu_debug::__msg_valid_range)
1046 ._M_iterator(__first, "first")
1047 ._M_iterator(__last, "last"));
1048 this->_M_invalidate_if([__tmp](_Base_const_iterator __it)
1049 { return __it == __tmp; });
1050 this->_M_invalidate_local_if(
1051 [__tmp](_Base_const_local_iterator __it)
1052 { return __it._M_curr() == __tmp._M_cur; });
1054 return iterator(_Base::erase(__first.base(),
1055 __last.base()), this);
1059 _M_base() noexcept { return *this; }
1062 _M_base() const noexcept { return *this; }
1066 _M_check_rehashed(size_type __prev_count)
1068 if (__prev_count != this->bucket_count())
1069 this->_M_invalidate_locals();
1073 #if __cpp_deduction_guides >= 201606
1075 template<typename _InputIterator,
1077 hash<typename iterator_traits<_InputIterator>::value_type>,
1079 equal_to<typename iterator_traits<_InputIterator>::value_type>,
1080 typename _Allocator =
1081 allocator<typename iterator_traits<_InputIterator>::value_type>,
1082 typename = _RequireInputIter<_InputIterator>,
1083 typename = _RequireAllocator<_Allocator>>
1084 unordered_multiset(_InputIterator, _InputIterator,
1085 unordered_multiset<int>::size_type = {},
1086 _Hash = _Hash(), _Pred = _Pred(),
1087 _Allocator = _Allocator())
1088 -> unordered_multiset<typename iterator_traits<_InputIterator>::value_type,
1089 _Hash, _Pred, _Allocator>;
1091 template<typename _Tp, typename _Hash = hash<_Tp>,
1092 typename _Pred = equal_to<_Tp>,
1093 typename _Allocator = allocator<_Tp>,
1094 typename = _RequireAllocator<_Allocator>>
1095 unordered_multiset(initializer_list<_Tp>,
1096 unordered_multiset<int>::size_type = {},
1097 _Hash = _Hash(), _Pred = _Pred(),
1098 _Allocator = _Allocator())
1099 -> unordered_multiset<_Tp, _Hash, _Pred, _Allocator>;
1101 template<typename _InputIterator, typename _Allocator,
1102 typename = _RequireInputIter<_InputIterator>,
1103 typename = _RequireAllocator<_Allocator>>
1104 unordered_multiset(_InputIterator, _InputIterator,
1105 unordered_multiset<int>::size_type, _Allocator)
1106 -> unordered_multiset<typename iterator_traits<_InputIterator>::value_type,
1108 iterator_traits<_InputIterator>::value_type>,
1110 iterator_traits<_InputIterator>::value_type>,
1113 template<typename _InputIterator, typename _Hash, typename _Allocator,
1114 typename = _RequireInputIter<_InputIterator>,
1115 typename = _RequireAllocator<_Allocator>>
1116 unordered_multiset(_InputIterator, _InputIterator,
1117 unordered_multiset<int>::size_type,
1119 -> unordered_multiset<typename
1120 iterator_traits<_InputIterator>::value_type,
1124 iterator_traits<_InputIterator>::value_type>,
1127 template<typename _Tp, typename _Allocator,
1128 typename = _RequireAllocator<_Allocator>>
1129 unordered_multiset(initializer_list<_Tp>,
1130 unordered_multiset<int>::size_type, _Allocator)
1131 -> unordered_multiset<_Tp, hash<_Tp>, equal_to<_Tp>, _Allocator>;
1133 template<typename _Tp, typename _Hash, typename _Allocator,
1134 typename = _RequireAllocator<_Allocator>>
1135 unordered_multiset(initializer_list<_Tp>,
1136 unordered_multiset<int>::size_type, _Hash, _Allocator)
1137 -> unordered_multiset<_Tp, _Hash, equal_to<_Tp>, _Allocator>;
1141 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
1143 swap(unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
1144 unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
1145 noexcept(noexcept(__x.swap(__y)))
1148 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
1150 operator==(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
1151 const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
1152 { return __x._M_base() == __y._M_base(); }
1154 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
1156 operator!=(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
1157 const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
1158 { return !(__x == __y); }
1160 } // namespace __debug