2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP__HASH_TABLE
11 #define _LIBCPP__HASH_TABLE
14 #include <initializer_list>
20 #include <type_traits>
24 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25 #pragma GCC system_header
29 #include <__undef_macros>
32 _LIBCPP_BEGIN_NAMESPACE_STD
34 template <class _Key, class _Tp>
35 struct __hash_value_type;
37 #ifndef _LIBCPP_CXX03_LANG
39 struct __is_hash_value_type_imp : false_type {};
41 template <class _Key, class _Value>
42 struct __is_hash_value_type_imp<__hash_value_type<_Key, _Value>> : true_type {};
44 template <class ..._Args>
45 struct __is_hash_value_type : false_type {};
48 struct __is_hash_value_type<_One> : __is_hash_value_type_imp<typename __uncvref<_One>::type> {};
52 size_t __next_prime(size_t __n);
54 template <class _NodePtr>
55 struct __hash_node_base
57 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
58 typedef __hash_node_base __first_node;
59 typedef typename __rebind_pointer<_NodePtr, __first_node>::type __node_base_pointer;
60 typedef _NodePtr __node_pointer;
62 #if defined(_LIBCPP_ABI_FIX_UNORDERED_NODE_POINTER_UB)
63 typedef __node_base_pointer __next_pointer;
65 typedef typename conditional<
66 is_pointer<__node_pointer>::value,
68 __node_pointer>::type __next_pointer;
71 __next_pointer __next_;
73 _LIBCPP_INLINE_VISIBILITY
74 __next_pointer __ptr() _NOEXCEPT {
75 return static_cast<__next_pointer>(
76 pointer_traits<__node_base_pointer>::pointer_to(*this));
79 _LIBCPP_INLINE_VISIBILITY
80 __node_pointer __upcast() _NOEXCEPT {
81 return static_cast<__node_pointer>(
82 pointer_traits<__node_base_pointer>::pointer_to(*this));
85 _LIBCPP_INLINE_VISIBILITY
86 size_t __hash() const _NOEXCEPT {
87 return static_cast<__node_type const&>(*this).__hash_;
90 _LIBCPP_INLINE_VISIBILITY __hash_node_base() _NOEXCEPT : __next_(nullptr) {}
93 template <class _Tp, class _VoidPtr>
95 : public __hash_node_base
97 typename __rebind_pointer<_VoidPtr, __hash_node<_Tp, _VoidPtr> >::type
100 typedef _Tp __node_value_type;
103 __node_value_type __value_;
106 inline _LIBCPP_INLINE_VISIBILITY
108 __is_hash_power2(size_t __bc)
110 return __bc > 2 && !(__bc & (__bc - 1));
113 inline _LIBCPP_INLINE_VISIBILITY
115 __constrain_hash(size_t __h, size_t __bc)
117 return !(__bc & (__bc - 1)) ? __h & (__bc - 1) :
118 (__h < __bc ? __h : __h % __bc);
121 inline _LIBCPP_INLINE_VISIBILITY
123 __next_hash_pow2(size_t __n)
125 return __n < 2 ? __n : (size_t(1) << (std::numeric_limits<size_t>::digits - __libcpp_clz(__n-1)));
129 template <class _Tp, class _Hash, class _Equal, class _Alloc> class __hash_table;
131 template <class _NodePtr> class _LIBCPP_TEMPLATE_VIS __hash_iterator;
132 template <class _ConstNodePtr> class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
133 template <class _NodePtr> class _LIBCPP_TEMPLATE_VIS __hash_local_iterator;
134 template <class _ConstNodePtr> class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
135 template <class _HashIterator> class _LIBCPP_TEMPLATE_VIS __hash_map_iterator;
136 template <class _HashIterator> class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
139 struct __hash_key_value_types {
140 static_assert(!is_reference<_Tp>::value && !is_const<_Tp>::value, "");
141 typedef _Tp key_type;
142 typedef _Tp __node_value_type;
143 typedef _Tp __container_value_type;
144 static const bool __is_map = false;
146 _LIBCPP_INLINE_VISIBILITY
147 static key_type const& __get_key(_Tp const& __v) {
150 _LIBCPP_INLINE_VISIBILITY
151 static __container_value_type const& __get_value(__node_value_type const& __v) {
154 _LIBCPP_INLINE_VISIBILITY
155 static __container_value_type* __get_ptr(__node_value_type& __n) {
156 return _VSTD::addressof(__n);
158 #ifndef _LIBCPP_CXX03_LANG
159 _LIBCPP_INLINE_VISIBILITY
160 static __container_value_type&& __move(__node_value_type& __v) {
161 return _VSTD::move(__v);
166 template <class _Key, class _Tp>
167 struct __hash_key_value_types<__hash_value_type<_Key, _Tp> > {
168 typedef _Key key_type;
169 typedef _Tp mapped_type;
170 typedef __hash_value_type<_Key, _Tp> __node_value_type;
171 typedef pair<const _Key, _Tp> __container_value_type;
172 typedef __container_value_type __map_value_type;
173 static const bool __is_map = true;
175 _LIBCPP_INLINE_VISIBILITY
176 static key_type const& __get_key(__container_value_type const& __v) {
181 _LIBCPP_INLINE_VISIBILITY
182 static typename enable_if<__is_same_uncvref<_Up, __node_value_type>::value,
183 __container_value_type const&>::type
184 __get_value(_Up& __t) {
185 return __t.__get_value();
189 _LIBCPP_INLINE_VISIBILITY
190 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
191 __container_value_type const&>::type
192 __get_value(_Up& __t) {
196 _LIBCPP_INLINE_VISIBILITY
197 static __container_value_type* __get_ptr(__node_value_type& __n) {
198 return _VSTD::addressof(__n.__get_value());
200 #ifndef _LIBCPP_CXX03_LANG
201 _LIBCPP_INLINE_VISIBILITY
202 static pair<key_type&&, mapped_type&&> __move(__node_value_type& __v) {
209 template <class _Tp, class _AllocPtr, class _KVTypes = __hash_key_value_types<_Tp>,
210 bool = _KVTypes::__is_map>
211 struct __hash_map_pointer_types {};
213 template <class _Tp, class _AllocPtr, class _KVTypes>
214 struct __hash_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
215 typedef typename _KVTypes::__map_value_type _Mv;
216 typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
217 __map_value_type_pointer;
218 typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
219 __const_map_value_type_pointer;
222 template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
223 struct __hash_node_types;
225 template <class _NodePtr, class _Tp, class _VoidPtr>
226 struct __hash_node_types<_NodePtr, __hash_node<_Tp, _VoidPtr> >
227 : public __hash_key_value_types<_Tp>, __hash_map_pointer_types<_Tp, _VoidPtr>
230 typedef __hash_key_value_types<_Tp> __base;
233 typedef ptrdiff_t difference_type;
234 typedef size_t size_type;
236 typedef typename __rebind_pointer<_NodePtr, void>::type __void_pointer;
238 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
239 typedef _NodePtr __node_pointer;
241 typedef __hash_node_base<__node_pointer> __node_base_type;
242 typedef typename __rebind_pointer<_NodePtr, __node_base_type>::type
245 typedef typename __node_base_type::__next_pointer __next_pointer;
247 typedef _Tp __node_value_type;
248 typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
249 __node_value_type_pointer;
250 typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
251 __const_node_value_type_pointer;
254 static_assert(!is_const<__node_type>::value,
255 "_NodePtr should never be a pointer to const");
256 static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
257 "_VoidPtr does not point to unqualified void type");
258 static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
259 _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
262 template <class _HashIterator>
263 struct __hash_node_types_from_iterator;
264 template <class _NodePtr>
265 struct __hash_node_types_from_iterator<__hash_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
266 template <class _NodePtr>
267 struct __hash_node_types_from_iterator<__hash_const_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
268 template <class _NodePtr>
269 struct __hash_node_types_from_iterator<__hash_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
270 template <class _NodePtr>
271 struct __hash_node_types_from_iterator<__hash_const_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
274 template <class _NodeValueTp, class _VoidPtr>
275 struct __make_hash_node_types {
276 typedef __hash_node<_NodeValueTp, _VoidPtr> _NodeTp;
277 typedef typename __rebind_pointer<_VoidPtr, _NodeTp>::type _NodePtr;
278 typedef __hash_node_types<_NodePtr> type;
281 template <class _NodePtr>
282 class _LIBCPP_TEMPLATE_VIS __hash_iterator
284 typedef __hash_node_types<_NodePtr> _NodeTypes;
285 typedef _NodePtr __node_pointer;
286 typedef typename _NodeTypes::__next_pointer __next_pointer;
288 __next_pointer __node_;
291 typedef forward_iterator_tag iterator_category;
292 typedef typename _NodeTypes::__node_value_type value_type;
293 typedef typename _NodeTypes::difference_type difference_type;
294 typedef value_type& reference;
295 typedef typename _NodeTypes::__node_value_type_pointer pointer;
297 _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT : __node_(nullptr) {
298 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
301 #if _LIBCPP_DEBUG_LEVEL >= 2
302 _LIBCPP_INLINE_VISIBILITY
303 __hash_iterator(const __hash_iterator& __i)
304 : __node_(__i.__node_)
306 __get_db()->__iterator_copy(this, &__i);
309 _LIBCPP_INLINE_VISIBILITY
312 __get_db()->__erase_i(this);
315 _LIBCPP_INLINE_VISIBILITY
316 __hash_iterator& operator=(const __hash_iterator& __i)
320 __get_db()->__iterator_copy(this, &__i);
321 __node_ = __i.__node_;
325 #endif // _LIBCPP_DEBUG_LEVEL >= 2
327 _LIBCPP_INLINE_VISIBILITY
328 reference operator*() const {
329 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
330 "Attempted to dereference a non-dereferenceable unordered container iterator");
331 return __node_->__upcast()->__value_;
334 _LIBCPP_INLINE_VISIBILITY
335 pointer operator->() const {
336 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
337 "Attempted to dereference a non-dereferenceable unordered container iterator");
338 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
341 _LIBCPP_INLINE_VISIBILITY
342 __hash_iterator& operator++() {
343 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
344 "Attempted to increment non-incrementable unordered container iterator");
345 __node_ = __node_->__next_;
349 _LIBCPP_INLINE_VISIBILITY
350 __hash_iterator operator++(int)
352 __hash_iterator __t(*this);
357 friend _LIBCPP_INLINE_VISIBILITY
358 bool operator==(const __hash_iterator& __x, const __hash_iterator& __y)
360 return __x.__node_ == __y.__node_;
362 friend _LIBCPP_INLINE_VISIBILITY
363 bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y)
364 {return !(__x == __y);}
367 #if _LIBCPP_DEBUG_LEVEL >= 2
368 _LIBCPP_INLINE_VISIBILITY
369 __hash_iterator(__next_pointer __node, const void* __c) _NOEXCEPT
372 __get_db()->__insert_ic(this, __c);
375 _LIBCPP_INLINE_VISIBILITY
376 __hash_iterator(__next_pointer __node) _NOEXCEPT
380 template <class, class, class, class> friend class __hash_table;
381 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
382 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_iterator;
383 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_map;
384 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_multimap;
387 template <class _NodePtr>
388 class _LIBCPP_TEMPLATE_VIS __hash_const_iterator
390 static_assert(!is_const<typename pointer_traits<_NodePtr>::element_type>::value, "");
391 typedef __hash_node_types<_NodePtr> _NodeTypes;
392 typedef _NodePtr __node_pointer;
393 typedef typename _NodeTypes::__next_pointer __next_pointer;
395 __next_pointer __node_;
398 typedef __hash_iterator<_NodePtr> __non_const_iterator;
400 typedef forward_iterator_tag iterator_category;
401 typedef typename _NodeTypes::__node_value_type value_type;
402 typedef typename _NodeTypes::difference_type difference_type;
403 typedef const value_type& reference;
404 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
407 _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT : __node_(nullptr) {
408 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
411 _LIBCPP_INLINE_VISIBILITY
412 __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT
413 : __node_(__x.__node_)
415 _LIBCPP_DEBUG_MODE(__get_db()->__iterator_copy(this, &__x));
418 #if _LIBCPP_DEBUG_LEVEL >= 2
419 _LIBCPP_INLINE_VISIBILITY
420 __hash_const_iterator(const __hash_const_iterator& __i)
421 : __node_(__i.__node_)
423 __get_db()->__iterator_copy(this, &__i);
426 _LIBCPP_INLINE_VISIBILITY
427 ~__hash_const_iterator()
429 __get_db()->__erase_i(this);
432 _LIBCPP_INLINE_VISIBILITY
433 __hash_const_iterator& operator=(const __hash_const_iterator& __i)
437 __get_db()->__iterator_copy(this, &__i);
438 __node_ = __i.__node_;
442 #endif // _LIBCPP_DEBUG_LEVEL >= 2
444 _LIBCPP_INLINE_VISIBILITY
445 reference operator*() const {
446 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
447 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
448 return __node_->__upcast()->__value_;
450 _LIBCPP_INLINE_VISIBILITY
451 pointer operator->() const {
452 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
453 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
454 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
457 _LIBCPP_INLINE_VISIBILITY
458 __hash_const_iterator& operator++() {
459 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
460 "Attempted to increment non-incrementable unordered container const_iterator");
461 __node_ = __node_->__next_;
465 _LIBCPP_INLINE_VISIBILITY
466 __hash_const_iterator operator++(int)
468 __hash_const_iterator __t(*this);
473 friend _LIBCPP_INLINE_VISIBILITY
474 bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
476 return __x.__node_ == __y.__node_;
478 friend _LIBCPP_INLINE_VISIBILITY
479 bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
480 {return !(__x == __y);}
483 #if _LIBCPP_DEBUG_LEVEL >= 2
484 _LIBCPP_INLINE_VISIBILITY
485 __hash_const_iterator(__next_pointer __node, const void* __c) _NOEXCEPT
488 __get_db()->__insert_ic(this, __c);
491 _LIBCPP_INLINE_VISIBILITY
492 __hash_const_iterator(__next_pointer __node) _NOEXCEPT
496 template <class, class, class, class> friend class __hash_table;
497 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
498 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_map;
499 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_multimap;
502 template <class _NodePtr>
503 class _LIBCPP_TEMPLATE_VIS __hash_local_iterator
505 typedef __hash_node_types<_NodePtr> _NodeTypes;
506 typedef _NodePtr __node_pointer;
507 typedef typename _NodeTypes::__next_pointer __next_pointer;
509 __next_pointer __node_;
511 size_t __bucket_count_;
514 typedef forward_iterator_tag iterator_category;
515 typedef typename _NodeTypes::__node_value_type value_type;
516 typedef typename _NodeTypes::difference_type difference_type;
517 typedef value_type& reference;
518 typedef typename _NodeTypes::__node_value_type_pointer pointer;
520 _LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT : __node_(nullptr) {
521 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
524 #if _LIBCPP_DEBUG_LEVEL >= 2
525 _LIBCPP_INLINE_VISIBILITY
526 __hash_local_iterator(const __hash_local_iterator& __i)
527 : __node_(__i.__node_),
528 __bucket_(__i.__bucket_),
529 __bucket_count_(__i.__bucket_count_)
531 __get_db()->__iterator_copy(this, &__i);
534 _LIBCPP_INLINE_VISIBILITY
535 ~__hash_local_iterator()
537 __get_db()->__erase_i(this);
540 _LIBCPP_INLINE_VISIBILITY
541 __hash_local_iterator& operator=(const __hash_local_iterator& __i)
545 __get_db()->__iterator_copy(this, &__i);
546 __node_ = __i.__node_;
547 __bucket_ = __i.__bucket_;
548 __bucket_count_ = __i.__bucket_count_;
552 #endif // _LIBCPP_DEBUG_LEVEL >= 2
554 _LIBCPP_INLINE_VISIBILITY
555 reference operator*() const {
556 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
557 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
558 return __node_->__upcast()->__value_;
561 _LIBCPP_INLINE_VISIBILITY
562 pointer operator->() const {
563 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
564 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
565 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
568 _LIBCPP_INLINE_VISIBILITY
569 __hash_local_iterator& operator++() {
570 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
571 "Attempted to increment non-incrementable unordered container local_iterator");
572 __node_ = __node_->__next_;
573 if (__node_ != nullptr && __constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
578 _LIBCPP_INLINE_VISIBILITY
579 __hash_local_iterator operator++(int)
581 __hash_local_iterator __t(*this);
586 friend _LIBCPP_INLINE_VISIBILITY
587 bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
589 return __x.__node_ == __y.__node_;
591 friend _LIBCPP_INLINE_VISIBILITY
592 bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
593 {return !(__x == __y);}
596 #if _LIBCPP_DEBUG_LEVEL >= 2
597 _LIBCPP_INLINE_VISIBILITY
598 __hash_local_iterator(__next_pointer __node, size_t __bucket,
599 size_t __bucket_count, const void* __c) _NOEXCEPT
602 __bucket_count_(__bucket_count)
604 __get_db()->__insert_ic(this, __c);
605 if (__node_ != nullptr)
606 __node_ = __node_->__next_;
609 _LIBCPP_INLINE_VISIBILITY
610 __hash_local_iterator(__next_pointer __node, size_t __bucket,
611 size_t __bucket_count) _NOEXCEPT
614 __bucket_count_(__bucket_count)
616 if (__node_ != nullptr)
617 __node_ = __node_->__next_;
620 template <class, class, class, class> friend class __hash_table;
621 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
622 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_iterator;
625 template <class _ConstNodePtr>
626 class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator
628 typedef __hash_node_types<_ConstNodePtr> _NodeTypes;
629 typedef _ConstNodePtr __node_pointer;
630 typedef typename _NodeTypes::__next_pointer __next_pointer;
632 __next_pointer __node_;
634 size_t __bucket_count_;
636 typedef pointer_traits<__node_pointer> __pointer_traits;
637 typedef typename __pointer_traits::element_type __node;
638 typedef typename remove_const<__node>::type __non_const_node;
639 typedef typename __rebind_pointer<__node_pointer, __non_const_node>::type
640 __non_const_node_pointer;
642 typedef __hash_local_iterator<__non_const_node_pointer>
643 __non_const_iterator;
645 typedef forward_iterator_tag iterator_category;
646 typedef typename _NodeTypes::__node_value_type value_type;
647 typedef typename _NodeTypes::difference_type difference_type;
648 typedef const value_type& reference;
649 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
652 _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT : __node_(nullptr) {
653 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
656 _LIBCPP_INLINE_VISIBILITY
657 __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT
658 : __node_(__x.__node_),
659 __bucket_(__x.__bucket_),
660 __bucket_count_(__x.__bucket_count_)
662 _LIBCPP_DEBUG_MODE(__get_db()->__iterator_copy(this, &__x));
665 #if _LIBCPP_DEBUG_LEVEL >= 2
666 _LIBCPP_INLINE_VISIBILITY
667 __hash_const_local_iterator(const __hash_const_local_iterator& __i)
668 : __node_(__i.__node_),
669 __bucket_(__i.__bucket_),
670 __bucket_count_(__i.__bucket_count_)
672 __get_db()->__iterator_copy(this, &__i);
675 _LIBCPP_INLINE_VISIBILITY
676 ~__hash_const_local_iterator()
678 __get_db()->__erase_i(this);
681 _LIBCPP_INLINE_VISIBILITY
682 __hash_const_local_iterator& operator=(const __hash_const_local_iterator& __i)
686 __get_db()->__iterator_copy(this, &__i);
687 __node_ = __i.__node_;
688 __bucket_ = __i.__bucket_;
689 __bucket_count_ = __i.__bucket_count_;
693 #endif // _LIBCPP_DEBUG_LEVEL >= 2
695 _LIBCPP_INLINE_VISIBILITY
696 reference operator*() const {
697 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
698 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
699 return __node_->__upcast()->__value_;
702 _LIBCPP_INLINE_VISIBILITY
703 pointer operator->() const {
704 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
705 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
706 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
709 _LIBCPP_INLINE_VISIBILITY
710 __hash_const_local_iterator& operator++() {
711 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
712 "Attempted to increment non-incrementable unordered container const_local_iterator");
713 __node_ = __node_->__next_;
714 if (__node_ != nullptr && __constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
719 _LIBCPP_INLINE_VISIBILITY
720 __hash_const_local_iterator operator++(int)
722 __hash_const_local_iterator __t(*this);
727 friend _LIBCPP_INLINE_VISIBILITY
728 bool operator==(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
730 return __x.__node_ == __y.__node_;
732 friend _LIBCPP_INLINE_VISIBILITY
733 bool operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
734 {return !(__x == __y);}
737 #if _LIBCPP_DEBUG_LEVEL >= 2
738 _LIBCPP_INLINE_VISIBILITY
739 __hash_const_local_iterator(__next_pointer __node, size_t __bucket,
740 size_t __bucket_count, const void* __c) _NOEXCEPT
743 __bucket_count_(__bucket_count)
745 __get_db()->__insert_ic(this, __c);
746 if (__node_ != nullptr)
747 __node_ = __node_->__next_;
750 _LIBCPP_INLINE_VISIBILITY
751 __hash_const_local_iterator(__next_pointer __node, size_t __bucket,
752 size_t __bucket_count) _NOEXCEPT
755 __bucket_count_(__bucket_count)
757 if (__node_ != nullptr)
758 __node_ = __node_->__next_;
761 template <class, class, class, class> friend class __hash_table;
762 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
765 template <class _Alloc>
766 class __bucket_list_deallocator
768 typedef _Alloc allocator_type;
769 typedef allocator_traits<allocator_type> __alloc_traits;
770 typedef typename __alloc_traits::size_type size_type;
772 __compressed_pair<size_type, allocator_type> __data_;
774 typedef typename __alloc_traits::pointer pointer;
776 _LIBCPP_INLINE_VISIBILITY
777 __bucket_list_deallocator()
778 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
779 : __data_(0, __default_init_tag()) {}
781 _LIBCPP_INLINE_VISIBILITY
782 __bucket_list_deallocator(const allocator_type& __a, size_type __size)
783 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
784 : __data_(__size, __a) {}
786 #ifndef _LIBCPP_CXX03_LANG
787 _LIBCPP_INLINE_VISIBILITY
788 __bucket_list_deallocator(__bucket_list_deallocator&& __x)
789 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
790 : __data_(_VSTD::move(__x.__data_))
796 _LIBCPP_INLINE_VISIBILITY
797 size_type& size() _NOEXCEPT {return __data_.first();}
798 _LIBCPP_INLINE_VISIBILITY
799 size_type size() const _NOEXCEPT {return __data_.first();}
801 _LIBCPP_INLINE_VISIBILITY
802 allocator_type& __alloc() _NOEXCEPT {return __data_.second();}
803 _LIBCPP_INLINE_VISIBILITY
804 const allocator_type& __alloc() const _NOEXCEPT {return __data_.second();}
806 _LIBCPP_INLINE_VISIBILITY
807 void operator()(pointer __p) _NOEXCEPT
809 __alloc_traits::deallocate(__alloc(), __p, size());
813 template <class _Alloc> class __hash_map_node_destructor;
815 template <class _Alloc>
816 class __hash_node_destructor
818 typedef _Alloc allocator_type;
819 typedef allocator_traits<allocator_type> __alloc_traits;
822 typedef typename __alloc_traits::pointer pointer;
824 typedef __hash_node_types<pointer> _NodeTypes;
826 allocator_type& __na_;
829 bool __value_constructed;
831 __hash_node_destructor(__hash_node_destructor const&) = default;
832 __hash_node_destructor& operator=(const __hash_node_destructor&) = delete;
835 _LIBCPP_INLINE_VISIBILITY
836 explicit __hash_node_destructor(allocator_type& __na,
837 bool __constructed = false) _NOEXCEPT
839 __value_constructed(__constructed)
842 _LIBCPP_INLINE_VISIBILITY
843 void operator()(pointer __p) _NOEXCEPT
845 if (__value_constructed)
846 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
848 __alloc_traits::deallocate(__na_, __p, 1);
851 template <class> friend class __hash_map_node_destructor;
854 #if _LIBCPP_STD_VER > 14
855 template <class _NodeType, class _Alloc>
856 struct __generic_container_node_destructor;
858 template <class _Tp, class _VoidPtr, class _Alloc>
859 struct __generic_container_node_destructor<__hash_node<_Tp, _VoidPtr>, _Alloc>
860 : __hash_node_destructor<_Alloc>
862 using __hash_node_destructor<_Alloc>::__hash_node_destructor;
866 template <class _Key, class _Hash, class _Equal>
867 struct __enforce_unordered_container_requirements {
868 #ifndef _LIBCPP_CXX03_LANG
869 static_assert(__check_hash_requirements<_Key, _Hash>::value,
870 "the specified hash does not meet the Hash requirements");
871 static_assert(is_copy_constructible<_Equal>::value,
872 "the specified comparator is required to be copy constructible");
877 template <class _Key, class _Hash, class _Equal>
878 #ifndef _LIBCPP_CXX03_LANG
879 _LIBCPP_DIAGNOSE_WARNING(!__invokable<_Equal const&, _Key const&, _Key const&>::value,
880 "the specified comparator type does not provide a viable const call operator")
881 _LIBCPP_DIAGNOSE_WARNING(!__invokable<_Hash const&, _Key const&>::value,
882 "the specified hash functor does not provide a viable const call operator")
884 typename __enforce_unordered_container_requirements<_Key, _Hash, _Equal>::type
885 __diagnose_unordered_container_requirements(int);
887 // This dummy overload is used so that the compiler won't emit a spurious
888 // "no matching function for call to __diagnose_unordered_xxx" diagnostic
889 // when the overload above causes a hard error.
890 template <class _Key, class _Hash, class _Equal>
891 int __diagnose_unordered_container_requirements(void*);
893 template <class _Tp, class _Hash, class _Equal, class _Alloc>
897 typedef _Tp value_type;
898 typedef _Hash hasher;
899 typedef _Equal key_equal;
900 typedef _Alloc allocator_type;
903 typedef allocator_traits<allocator_type> __alloc_traits;
905 __make_hash_node_types<value_type, typename __alloc_traits::void_pointer>::type
909 typedef typename _NodeTypes::__node_value_type __node_value_type;
910 typedef typename _NodeTypes::__container_value_type __container_value_type;
911 typedef typename _NodeTypes::key_type key_type;
912 typedef value_type& reference;
913 typedef const value_type& const_reference;
914 typedef typename __alloc_traits::pointer pointer;
915 typedef typename __alloc_traits::const_pointer const_pointer;
916 #ifndef _LIBCPP_ABI_FIX_UNORDERED_CONTAINER_SIZE_TYPE
917 typedef typename __alloc_traits::size_type size_type;
919 typedef typename _NodeTypes::size_type size_type;
921 typedef typename _NodeTypes::difference_type difference_type;
925 typedef typename _NodeTypes::__node_type __node;
926 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
927 typedef allocator_traits<__node_allocator> __node_traits;
928 typedef typename _NodeTypes::__void_pointer __void_pointer;
929 typedef typename _NodeTypes::__node_pointer __node_pointer;
930 typedef typename _NodeTypes::__node_pointer __node_const_pointer;
931 typedef typename _NodeTypes::__node_base_type __first_node;
932 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
933 typedef typename _NodeTypes::__next_pointer __next_pointer;
936 // check for sane allocator pointer rebinding semantics. Rebinding the
937 // allocator for a new pointer type should be exactly the same as rebinding
938 // the pointer using 'pointer_traits'.
939 static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
940 "Allocator does not rebind pointers in a sane manner.");
941 typedef typename __rebind_alloc_helper<__node_traits, __first_node>::type
942 __node_base_allocator;
943 typedef allocator_traits<__node_base_allocator> __node_base_traits;
944 static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
945 "Allocator does not rebind pointers in a sane manner.");
949 typedef typename __rebind_alloc_helper<__node_traits, __next_pointer>::type __pointer_allocator;
950 typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter;
951 typedef unique_ptr<__next_pointer[], __bucket_list_deleter> __bucket_list;
952 typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits;
953 typedef typename __bucket_list_deleter::pointer __node_pointer_pointer;
955 // --- Member data begin ---
956 __bucket_list __bucket_list_;
957 __compressed_pair<__first_node, __node_allocator> __p1_;
958 __compressed_pair<size_type, hasher> __p2_;
959 __compressed_pair<float, key_equal> __p3_;
960 // --- Member data end ---
962 _LIBCPP_INLINE_VISIBILITY
963 size_type& size() _NOEXCEPT {return __p2_.first();}
965 _LIBCPP_INLINE_VISIBILITY
966 size_type size() const _NOEXCEPT {return __p2_.first();}
968 _LIBCPP_INLINE_VISIBILITY
969 hasher& hash_function() _NOEXCEPT {return __p2_.second();}
970 _LIBCPP_INLINE_VISIBILITY
971 const hasher& hash_function() const _NOEXCEPT {return __p2_.second();}
973 _LIBCPP_INLINE_VISIBILITY
974 float& max_load_factor() _NOEXCEPT {return __p3_.first();}
975 _LIBCPP_INLINE_VISIBILITY
976 float max_load_factor() const _NOEXCEPT {return __p3_.first();}
978 _LIBCPP_INLINE_VISIBILITY
979 key_equal& key_eq() _NOEXCEPT {return __p3_.second();}
980 _LIBCPP_INLINE_VISIBILITY
981 const key_equal& key_eq() const _NOEXCEPT {return __p3_.second();}
983 _LIBCPP_INLINE_VISIBILITY
984 __node_allocator& __node_alloc() _NOEXCEPT {return __p1_.second();}
985 _LIBCPP_INLINE_VISIBILITY
986 const __node_allocator& __node_alloc() const _NOEXCEPT
987 {return __p1_.second();}
990 typedef __hash_iterator<__node_pointer> iterator;
991 typedef __hash_const_iterator<__node_pointer> const_iterator;
992 typedef __hash_local_iterator<__node_pointer> local_iterator;
993 typedef __hash_const_local_iterator<__node_pointer> const_local_iterator;
995 _LIBCPP_INLINE_VISIBILITY
998 is_nothrow_default_constructible<__bucket_list>::value &&
999 is_nothrow_default_constructible<__first_node>::value &&
1000 is_nothrow_default_constructible<__node_allocator>::value &&
1001 is_nothrow_default_constructible<hasher>::value &&
1002 is_nothrow_default_constructible<key_equal>::value);
1003 _LIBCPP_INLINE_VISIBILITY
1004 __hash_table(const hasher& __hf, const key_equal& __eql);
1005 __hash_table(const hasher& __hf, const key_equal& __eql,
1006 const allocator_type& __a);
1007 explicit __hash_table(const allocator_type& __a);
1008 __hash_table(const __hash_table& __u);
1009 __hash_table(const __hash_table& __u, const allocator_type& __a);
1010 #ifndef _LIBCPP_CXX03_LANG
1011 __hash_table(__hash_table&& __u)
1013 is_nothrow_move_constructible<__bucket_list>::value &&
1014 is_nothrow_move_constructible<__first_node>::value &&
1015 is_nothrow_move_constructible<__node_allocator>::value &&
1016 is_nothrow_move_constructible<hasher>::value &&
1017 is_nothrow_move_constructible<key_equal>::value);
1018 __hash_table(__hash_table&& __u, const allocator_type& __a);
1019 #endif // _LIBCPP_CXX03_LANG
1022 __hash_table& operator=(const __hash_table& __u);
1023 #ifndef _LIBCPP_CXX03_LANG
1024 _LIBCPP_INLINE_VISIBILITY
1025 __hash_table& operator=(__hash_table&& __u)
1027 __node_traits::propagate_on_container_move_assignment::value &&
1028 is_nothrow_move_assignable<__node_allocator>::value &&
1029 is_nothrow_move_assignable<hasher>::value &&
1030 is_nothrow_move_assignable<key_equal>::value);
1032 template <class _InputIterator>
1033 void __assign_unique(_InputIterator __first, _InputIterator __last);
1034 template <class _InputIterator>
1035 void __assign_multi(_InputIterator __first, _InputIterator __last);
1037 _LIBCPP_INLINE_VISIBILITY
1038 size_type max_size() const _NOEXCEPT
1040 return std::min<size_type>(
1041 __node_traits::max_size(__node_alloc()),
1042 numeric_limits<difference_type >::max()
1047 _LIBCPP_INLINE_VISIBILITY
1048 __next_pointer __node_insert_multi_prepare(size_t __cp_hash,
1049 value_type& __cp_val);
1050 _LIBCPP_INLINE_VISIBILITY
1051 void __node_insert_multi_perform(__node_pointer __cp,
1052 __next_pointer __pn) _NOEXCEPT;
1054 _LIBCPP_INLINE_VISIBILITY
1055 __next_pointer __node_insert_unique_prepare(size_t __nd_hash,
1056 value_type& __nd_val);
1057 _LIBCPP_INLINE_VISIBILITY
1058 void __node_insert_unique_perform(__node_pointer __ptr) _NOEXCEPT;
1061 _LIBCPP_INLINE_VISIBILITY
1062 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1063 _LIBCPP_INLINE_VISIBILITY
1064 iterator __node_insert_multi(__node_pointer __nd);
1065 _LIBCPP_INLINE_VISIBILITY
1066 iterator __node_insert_multi(const_iterator __p,
1067 __node_pointer __nd);
1069 #ifndef _LIBCPP_CXX03_LANG
1070 template <class _Key, class ..._Args>
1071 _LIBCPP_INLINE_VISIBILITY
1072 pair<iterator, bool> __emplace_unique_key_args(_Key const& __k, _Args&&... __args);
1074 template <class... _Args>
1075 _LIBCPP_INLINE_VISIBILITY
1076 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
1078 template <class _Pp>
1079 _LIBCPP_INLINE_VISIBILITY
1080 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1081 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1082 __can_extract_key<_Pp, key_type>());
1085 template <class _First, class _Second>
1086 _LIBCPP_INLINE_VISIBILITY
1088 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1089 pair<iterator, bool>
1090 >::type __emplace_unique(_First&& __f, _Second&& __s) {
1091 return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
1092 _VSTD::forward<_Second>(__s));
1095 template <class... _Args>
1096 _LIBCPP_INLINE_VISIBILITY
1097 pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1098 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1101 template <class _Pp>
1102 _LIBCPP_INLINE_VISIBILITY
1103 pair<iterator, bool>
1104 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1105 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1107 template <class _Pp>
1108 _LIBCPP_INLINE_VISIBILITY
1109 pair<iterator, bool>
1110 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1111 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1113 template <class _Pp>
1114 _LIBCPP_INLINE_VISIBILITY
1115 pair<iterator, bool>
1116 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1117 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1120 template <class... _Args>
1121 _LIBCPP_INLINE_VISIBILITY
1122 iterator __emplace_multi(_Args&&... __args);
1123 template <class... _Args>
1124 _LIBCPP_INLINE_VISIBILITY
1125 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
1128 _LIBCPP_INLINE_VISIBILITY
1129 pair<iterator, bool>
1130 __insert_unique(__container_value_type&& __x) {
1131 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), _VSTD::move(__x));
1134 template <class _Pp, class = typename enable_if<
1135 !__is_same_uncvref<_Pp, __container_value_type>::value
1137 _LIBCPP_INLINE_VISIBILITY
1138 pair<iterator, bool> __insert_unique(_Pp&& __x) {
1139 return __emplace_unique(_VSTD::forward<_Pp>(__x));
1142 template <class _Pp>
1143 _LIBCPP_INLINE_VISIBILITY
1144 iterator __insert_multi(_Pp&& __x) {
1145 return __emplace_multi(_VSTD::forward<_Pp>(__x));
1148 template <class _Pp>
1149 _LIBCPP_INLINE_VISIBILITY
1150 iterator __insert_multi(const_iterator __p, _Pp&& __x) {
1151 return __emplace_hint_multi(__p, _VSTD::forward<_Pp>(__x));
1154 #else // !defined(_LIBCPP_CXX03_LANG)
1155 template <class _Key, class _Args>
1156 _LIBCPP_INLINE_VISIBILITY
1157 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1159 iterator __insert_multi(const __container_value_type& __x);
1160 iterator __insert_multi(const_iterator __p, const __container_value_type& __x);
1163 _LIBCPP_INLINE_VISIBILITY
1164 pair<iterator, bool> __insert_unique(const __container_value_type& __x) {
1165 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), __x);
1168 #if _LIBCPP_STD_VER > 14
1169 template <class _NodeHandle, class _InsertReturnType>
1170 _LIBCPP_INLINE_VISIBILITY
1171 _InsertReturnType __node_handle_insert_unique(_NodeHandle&& __nh);
1172 template <class _NodeHandle>
1173 _LIBCPP_INLINE_VISIBILITY
1174 iterator __node_handle_insert_unique(const_iterator __hint,
1175 _NodeHandle&& __nh);
1176 template <class _Table>
1177 _LIBCPP_INLINE_VISIBILITY
1178 void __node_handle_merge_unique(_Table& __source);
1180 template <class _NodeHandle>
1181 _LIBCPP_INLINE_VISIBILITY
1182 iterator __node_handle_insert_multi(_NodeHandle&& __nh);
1183 template <class _NodeHandle>
1184 _LIBCPP_INLINE_VISIBILITY
1185 iterator __node_handle_insert_multi(const_iterator __hint, _NodeHandle&& __nh);
1186 template <class _Table>
1187 _LIBCPP_INLINE_VISIBILITY
1188 void __node_handle_merge_multi(_Table& __source);
1190 template <class _NodeHandle>
1191 _LIBCPP_INLINE_VISIBILITY
1192 _NodeHandle __node_handle_extract(key_type const& __key);
1193 template <class _NodeHandle>
1194 _LIBCPP_INLINE_VISIBILITY
1195 _NodeHandle __node_handle_extract(const_iterator __it);
1198 void clear() _NOEXCEPT;
1199 void rehash(size_type __n);
1200 _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n)
1201 {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));}
1203 _LIBCPP_INLINE_VISIBILITY
1204 size_type bucket_count() const _NOEXCEPT
1206 return __bucket_list_.get_deleter().size();
1209 _LIBCPP_INLINE_VISIBILITY
1210 iterator begin() _NOEXCEPT;
1211 _LIBCPP_INLINE_VISIBILITY
1212 iterator end() _NOEXCEPT;
1213 _LIBCPP_INLINE_VISIBILITY
1214 const_iterator begin() const _NOEXCEPT;
1215 _LIBCPP_INLINE_VISIBILITY
1216 const_iterator end() const _NOEXCEPT;
1218 template <class _Key>
1219 _LIBCPP_INLINE_VISIBILITY
1220 size_type bucket(const _Key& __k) const
1222 _LIBCPP_ASSERT(bucket_count() > 0,
1223 "unordered container::bucket(key) called when bucket_count() == 0");
1224 return __constrain_hash(hash_function()(__k), bucket_count());
1227 template <class _Key>
1228 iterator find(const _Key& __x);
1229 template <class _Key>
1230 const_iterator find(const _Key& __x) const;
1232 typedef __hash_node_destructor<__node_allocator> _Dp;
1233 typedef unique_ptr<__node, _Dp> __node_holder;
1235 iterator erase(const_iterator __p);
1236 iterator erase(const_iterator __first, const_iterator __last);
1237 template <class _Key>
1238 size_type __erase_unique(const _Key& __k);
1239 template <class _Key>
1240 size_type __erase_multi(const _Key& __k);
1241 __node_holder remove(const_iterator __p) _NOEXCEPT;
1243 template <class _Key>
1244 _LIBCPP_INLINE_VISIBILITY
1245 size_type __count_unique(const _Key& __k) const;
1246 template <class _Key>
1247 size_type __count_multi(const _Key& __k) const;
1249 template <class _Key>
1250 pair<iterator, iterator>
1251 __equal_range_unique(const _Key& __k);
1252 template <class _Key>
1253 pair<const_iterator, const_iterator>
1254 __equal_range_unique(const _Key& __k) const;
1256 template <class _Key>
1257 pair<iterator, iterator>
1258 __equal_range_multi(const _Key& __k);
1259 template <class _Key>
1260 pair<const_iterator, const_iterator>
1261 __equal_range_multi(const _Key& __k) const;
1263 void swap(__hash_table& __u)
1264 #if _LIBCPP_STD_VER <= 11
1266 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
1267 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
1268 || __is_nothrow_swappable<__pointer_allocator>::value)
1269 && (!__node_traits::propagate_on_container_swap::value
1270 || __is_nothrow_swappable<__node_allocator>::value)
1273 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value);
1276 _LIBCPP_INLINE_VISIBILITY
1277 size_type max_bucket_count() const _NOEXCEPT
1278 {return max_size(); }
1279 size_type bucket_size(size_type __n) const;
1280 _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT
1282 size_type __bc = bucket_count();
1283 return __bc != 0 ? (float)size() / __bc : 0.f;
1285 _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT
1287 _LIBCPP_ASSERT(__mlf > 0,
1288 "unordered container::max_load_factor(lf) called with lf <= 0");
1289 max_load_factor() = _VSTD::max(__mlf, load_factor());
1292 _LIBCPP_INLINE_VISIBILITY
1294 begin(size_type __n)
1296 _LIBCPP_ASSERT(__n < bucket_count(),
1297 "unordered container::begin(n) called with n >= bucket_count()");
1298 #if _LIBCPP_DEBUG_LEVEL >= 2
1299 return local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1301 return local_iterator(__bucket_list_[__n], __n, bucket_count());
1305 _LIBCPP_INLINE_VISIBILITY
1309 _LIBCPP_ASSERT(__n < bucket_count(),
1310 "unordered container::end(n) called with n >= bucket_count()");
1311 #if _LIBCPP_DEBUG_LEVEL >= 2
1312 return local_iterator(nullptr, __n, bucket_count(), this);
1314 return local_iterator(nullptr, __n, bucket_count());
1318 _LIBCPP_INLINE_VISIBILITY
1319 const_local_iterator
1320 cbegin(size_type __n) const
1322 _LIBCPP_ASSERT(__n < bucket_count(),
1323 "unordered container::cbegin(n) called with n >= bucket_count()");
1324 #if _LIBCPP_DEBUG_LEVEL >= 2
1325 return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1327 return const_local_iterator(__bucket_list_[__n], __n, bucket_count());
1331 _LIBCPP_INLINE_VISIBILITY
1332 const_local_iterator
1333 cend(size_type __n) const
1335 _LIBCPP_ASSERT(__n < bucket_count(),
1336 "unordered container::cend(n) called with n >= bucket_count()");
1337 #if _LIBCPP_DEBUG_LEVEL >= 2
1338 return const_local_iterator(nullptr, __n, bucket_count(), this);
1340 return const_local_iterator(nullptr, __n, bucket_count());
1344 #if _LIBCPP_DEBUG_LEVEL >= 2
1346 bool __dereferenceable(const const_iterator* __i) const;
1347 bool __decrementable(const const_iterator* __i) const;
1348 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1349 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1351 #endif // _LIBCPP_DEBUG_LEVEL >= 2
1354 void __rehash(size_type __n);
1356 #ifndef _LIBCPP_CXX03_LANG
1357 template <class ..._Args>
1358 __node_holder __construct_node(_Args&& ...__args);
1360 template <class _First, class ..._Rest>
1361 __node_holder __construct_node_hash(size_t __hash, _First&& __f, _Rest&&... __rest);
1362 #else // _LIBCPP_CXX03_LANG
1363 __node_holder __construct_node(const __container_value_type& __v);
1364 __node_holder __construct_node_hash(size_t __hash, const __container_value_type& __v);
1368 _LIBCPP_INLINE_VISIBILITY
1369 void __copy_assign_alloc(const __hash_table& __u)
1370 {__copy_assign_alloc(__u, integral_constant<bool,
1371 __node_traits::propagate_on_container_copy_assignment::value>());}
1372 void __copy_assign_alloc(const __hash_table& __u, true_type);
1373 _LIBCPP_INLINE_VISIBILITY
1374 void __copy_assign_alloc(const __hash_table&, false_type) {}
1376 #ifndef _LIBCPP_CXX03_LANG
1377 void __move_assign(__hash_table& __u, false_type);
1378 void __move_assign(__hash_table& __u, true_type)
1380 is_nothrow_move_assignable<__node_allocator>::value &&
1381 is_nothrow_move_assignable<hasher>::value &&
1382 is_nothrow_move_assignable<key_equal>::value);
1383 _LIBCPP_INLINE_VISIBILITY
1384 void __move_assign_alloc(__hash_table& __u)
1386 !__node_traits::propagate_on_container_move_assignment::value ||
1387 (is_nothrow_move_assignable<__pointer_allocator>::value &&
1388 is_nothrow_move_assignable<__node_allocator>::value))
1389 {__move_assign_alloc(__u, integral_constant<bool,
1390 __node_traits::propagate_on_container_move_assignment::value>());}
1391 _LIBCPP_INLINE_VISIBILITY
1392 void __move_assign_alloc(__hash_table& __u, true_type)
1394 is_nothrow_move_assignable<__pointer_allocator>::value &&
1395 is_nothrow_move_assignable<__node_allocator>::value)
1397 __bucket_list_.get_deleter().__alloc() =
1398 _VSTD::move(__u.__bucket_list_.get_deleter().__alloc());
1399 __node_alloc() = _VSTD::move(__u.__node_alloc());
1401 _LIBCPP_INLINE_VISIBILITY
1402 void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
1403 #endif // _LIBCPP_CXX03_LANG
1405 void __deallocate_node(__next_pointer __np) _NOEXCEPT;
1406 __next_pointer __detach() _NOEXCEPT;
1408 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_map;
1409 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_multimap;
1412 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1414 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table()
1416 is_nothrow_default_constructible<__bucket_list>::value &&
1417 is_nothrow_default_constructible<__first_node>::value &&
1418 is_nothrow_default_constructible<__node_allocator>::value &&
1419 is_nothrow_default_constructible<hasher>::value &&
1420 is_nothrow_default_constructible<key_equal>::value)
1421 : __p2_(0, __default_init_tag()),
1422 __p3_(1.0f, __default_init_tag())
1426 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1428 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1429 const key_equal& __eql)
1430 : __bucket_list_(nullptr, __bucket_list_deleter()),
1437 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1438 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1439 const key_equal& __eql,
1440 const allocator_type& __a)
1441 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1442 __p1_(__default_init_tag(), __node_allocator(__a)),
1448 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1449 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a)
1450 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1451 __p1_(__default_init_tag(), __node_allocator(__a)),
1452 __p2_(0, __default_init_tag()),
1453 __p3_(1.0f, __default_init_tag())
1457 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1458 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u)
1459 : __bucket_list_(nullptr,
1460 __bucket_list_deleter(allocator_traits<__pointer_allocator>::
1461 select_on_container_copy_construction(
1462 __u.__bucket_list_.get_deleter().__alloc()), 0)),
1463 __p1_(__default_init_tag(), allocator_traits<__node_allocator>::
1464 select_on_container_copy_construction(__u.__node_alloc())),
1465 __p2_(0, __u.hash_function()),
1470 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1471 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u,
1472 const allocator_type& __a)
1473 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1474 __p1_(__default_init_tag(), __node_allocator(__a)),
1475 __p2_(0, __u.hash_function()),
1480 #ifndef _LIBCPP_CXX03_LANG
1482 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1483 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u)
1485 is_nothrow_move_constructible<__bucket_list>::value &&
1486 is_nothrow_move_constructible<__first_node>::value &&
1487 is_nothrow_move_constructible<__node_allocator>::value &&
1488 is_nothrow_move_constructible<hasher>::value &&
1489 is_nothrow_move_constructible<key_equal>::value)
1490 : __bucket_list_(_VSTD::move(__u.__bucket_list_)),
1491 __p1_(_VSTD::move(__u.__p1_)),
1492 __p2_(_VSTD::move(__u.__p2_)),
1493 __p3_(_VSTD::move(__u.__p3_))
1497 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1498 __p1_.first().__ptr();
1499 __u.__p1_.first().__next_ = nullptr;
1504 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1505 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u,
1506 const allocator_type& __a)
1507 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1508 __p1_(__default_init_tag(), __node_allocator(__a)),
1509 __p2_(0, _VSTD::move(__u.hash_function())),
1510 __p3_(_VSTD::move(__u.__p3_))
1512 if (__a == allocator_type(__u.__node_alloc()))
1514 __bucket_list_.reset(__u.__bucket_list_.release());
1515 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1516 __u.__bucket_list_.get_deleter().size() = 0;
1519 __p1_.first().__next_ = __u.__p1_.first().__next_;
1520 __u.__p1_.first().__next_ = nullptr;
1521 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1522 __p1_.first().__ptr();
1523 size() = __u.size();
1529 #endif // _LIBCPP_CXX03_LANG
1531 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1532 __hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table()
1534 #if defined(_LIBCPP_CXX03_LANG)
1535 static_assert((is_copy_constructible<key_equal>::value),
1536 "Predicate must be copy-constructible.");
1537 static_assert((is_copy_constructible<hasher>::value),
1538 "Hasher must be copy-constructible.");
1541 __deallocate_node(__p1_.first().__next_);
1542 #if _LIBCPP_DEBUG_LEVEL >= 2
1543 __get_db()->__erase_c(this);
1547 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1549 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(
1550 const __hash_table& __u, true_type)
1552 if (__node_alloc() != __u.__node_alloc())
1555 __bucket_list_.reset();
1556 __bucket_list_.get_deleter().size() = 0;
1558 __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
1559 __node_alloc() = __u.__node_alloc();
1562 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1563 __hash_table<_Tp, _Hash, _Equal, _Alloc>&
1564 __hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u)
1568 __copy_assign_alloc(__u);
1569 hash_function() = __u.hash_function();
1570 key_eq() = __u.key_eq();
1571 max_load_factor() = __u.max_load_factor();
1572 __assign_multi(__u.begin(), __u.end());
1577 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1579 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate_node(__next_pointer __np)
1582 __node_allocator& __na = __node_alloc();
1583 while (__np != nullptr)
1585 __next_pointer __next = __np->__next_;
1586 #if _LIBCPP_DEBUG_LEVEL >= 2
1587 __c_node* __c = __get_db()->__find_c_and_lock(this);
1588 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1591 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1592 if (__i->__node_ == __np)
1594 (*__p)->__c_ = nullptr;
1595 if (--__c->end_ != __p)
1596 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1599 __get_db()->unlock();
1601 __node_pointer __real_np = __np->__upcast();
1602 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__real_np->__value_));
1603 __node_traits::deallocate(__na, __real_np, 1);
1608 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1609 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
1610 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
1612 size_type __bc = bucket_count();
1613 for (size_type __i = 0; __i < __bc; ++__i)
1614 __bucket_list_[__i] = nullptr;
1616 __next_pointer __cache = __p1_.first().__next_;
1617 __p1_.first().__next_ = nullptr;
1621 #ifndef _LIBCPP_CXX03_LANG
1623 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1625 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1626 __hash_table& __u, true_type)
1628 is_nothrow_move_assignable<__node_allocator>::value &&
1629 is_nothrow_move_assignable<hasher>::value &&
1630 is_nothrow_move_assignable<key_equal>::value)
1633 __bucket_list_.reset(__u.__bucket_list_.release());
1634 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1635 __u.__bucket_list_.get_deleter().size() = 0;
1636 __move_assign_alloc(__u);
1637 size() = __u.size();
1638 hash_function() = _VSTD::move(__u.hash_function());
1639 max_load_factor() = __u.max_load_factor();
1640 key_eq() = _VSTD::move(__u.key_eq());
1641 __p1_.first().__next_ = __u.__p1_.first().__next_;
1644 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1645 __p1_.first().__ptr();
1646 __u.__p1_.first().__next_ = nullptr;
1649 #if _LIBCPP_DEBUG_LEVEL >= 2
1650 __get_db()->swap(this, &__u);
1654 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1656 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1657 __hash_table& __u, false_type)
1659 if (__node_alloc() == __u.__node_alloc())
1660 __move_assign(__u, true_type());
1663 hash_function() = _VSTD::move(__u.hash_function());
1664 key_eq() = _VSTD::move(__u.key_eq());
1665 max_load_factor() = __u.max_load_factor();
1666 if (bucket_count() != 0)
1668 __next_pointer __cache = __detach();
1669 #ifndef _LIBCPP_NO_EXCEPTIONS
1672 #endif // _LIBCPP_NO_EXCEPTIONS
1673 const_iterator __i = __u.begin();
1674 while (__cache != nullptr && __u.size() != 0)
1676 __cache->__upcast()->__value_ =
1677 _VSTD::move(__u.remove(__i++)->__value_);
1678 __next_pointer __next = __cache->__next_;
1679 __node_insert_multi(__cache->__upcast());
1682 #ifndef _LIBCPP_NO_EXCEPTIONS
1686 __deallocate_node(__cache);
1689 #endif // _LIBCPP_NO_EXCEPTIONS
1690 __deallocate_node(__cache);
1692 const_iterator __i = __u.begin();
1693 while (__u.size() != 0)
1695 __node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__value_));
1696 __node_insert_multi(__h.get());
1702 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1704 __hash_table<_Tp, _Hash, _Equal, _Alloc>&
1705 __hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
1707 __node_traits::propagate_on_container_move_assignment::value &&
1708 is_nothrow_move_assignable<__node_allocator>::value &&
1709 is_nothrow_move_assignable<hasher>::value &&
1710 is_nothrow_move_assignable<key_equal>::value)
1712 __move_assign(__u, integral_constant<bool,
1713 __node_traits::propagate_on_container_move_assignment::value>());
1717 #endif // _LIBCPP_CXX03_LANG
1719 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1720 template <class _InputIterator>
1722 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first,
1723 _InputIterator __last)
1725 typedef iterator_traits<_InputIterator> _ITraits;
1726 typedef typename _ITraits::value_type _ItValueType;
1727 static_assert((is_same<_ItValueType, __container_value_type>::value),
1728 "__assign_unique may only be called with the containers value type");
1730 if (bucket_count() != 0)
1732 __next_pointer __cache = __detach();
1733 #ifndef _LIBCPP_NO_EXCEPTIONS
1736 #endif // _LIBCPP_NO_EXCEPTIONS
1737 for (; __cache != nullptr && __first != __last; ++__first)
1739 __cache->__upcast()->__value_ = *__first;
1740 __next_pointer __next = __cache->__next_;
1741 __node_insert_unique(__cache->__upcast());
1744 #ifndef _LIBCPP_NO_EXCEPTIONS
1748 __deallocate_node(__cache);
1751 #endif // _LIBCPP_NO_EXCEPTIONS
1752 __deallocate_node(__cache);
1754 for (; __first != __last; ++__first)
1755 __insert_unique(*__first);
1758 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1759 template <class _InputIterator>
1761 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
1762 _InputIterator __last)
1764 typedef iterator_traits<_InputIterator> _ITraits;
1765 typedef typename _ITraits::value_type _ItValueType;
1766 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1767 is_same<_ItValueType, __node_value_type>::value),
1768 "__assign_multi may only be called with the containers value type"
1769 " or the nodes value type");
1770 if (bucket_count() != 0)
1772 __next_pointer __cache = __detach();
1773 #ifndef _LIBCPP_NO_EXCEPTIONS
1776 #endif // _LIBCPP_NO_EXCEPTIONS
1777 for (; __cache != nullptr && __first != __last; ++__first)
1779 __cache->__upcast()->__value_ = *__first;
1780 __next_pointer __next = __cache->__next_;
1781 __node_insert_multi(__cache->__upcast());
1784 #ifndef _LIBCPP_NO_EXCEPTIONS
1788 __deallocate_node(__cache);
1791 #endif // _LIBCPP_NO_EXCEPTIONS
1792 __deallocate_node(__cache);
1794 for (; __first != __last; ++__first)
1795 __insert_multi(_NodeTypes::__get_value(*__first));
1798 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1800 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1801 __hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT
1803 #if _LIBCPP_DEBUG_LEVEL >= 2
1804 return iterator(__p1_.first().__next_, this);
1806 return iterator(__p1_.first().__next_);
1810 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1812 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1813 __hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT
1815 #if _LIBCPP_DEBUG_LEVEL >= 2
1816 return iterator(nullptr, this);
1818 return iterator(nullptr);
1822 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1824 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
1825 __hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT
1827 #if _LIBCPP_DEBUG_LEVEL >= 2
1828 return const_iterator(__p1_.first().__next_, this);
1830 return const_iterator(__p1_.first().__next_);
1834 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1836 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
1837 __hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT
1839 #if _LIBCPP_DEBUG_LEVEL >= 2
1840 return const_iterator(nullptr, this);
1842 return const_iterator(nullptr);
1846 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1848 __hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT
1852 __deallocate_node(__p1_.first().__next_);
1853 __p1_.first().__next_ = nullptr;
1854 size_type __bc = bucket_count();
1855 for (size_type __i = 0; __i < __bc; ++__i)
1856 __bucket_list_[__i] = nullptr;
1862 // Prepare the container for an insertion of the value __value with the hash
1863 // __hash. This does a lookup into the container to see if __value is already
1864 // present, and performs a rehash if necessary. Returns a pointer to the
1865 // existing element if it exists, otherwise nullptr.
1867 // Note that this function does forward exceptions if key_eq() throws, and never
1868 // mutates __value or actually inserts into the map.
1869 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1870 _LIBCPP_INLINE_VISIBILITY
1871 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
1872 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique_prepare(
1873 size_t __hash, value_type& __value)
1875 size_type __bc = bucket_count();
1879 size_t __chash = __constrain_hash(__hash, __bc);
1880 __next_pointer __ndptr = __bucket_list_[__chash];
1881 if (__ndptr != nullptr)
1883 for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
1884 __constrain_hash(__ndptr->__hash(), __bc) == __chash;
1885 __ndptr = __ndptr->__next_)
1887 if (key_eq()(__ndptr->__upcast()->__value_, __value))
1892 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1894 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
1895 size_type(ceil(float(size() + 1) / max_load_factor()))));
1900 // Insert the node __nd into the container by pushing it into the right bucket,
1901 // and updating size(). Assumes that __nd->__hash is up-to-date, and that
1902 // rehashing has already occurred and that no element with the same key exists
1904 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1905 _LIBCPP_INLINE_VISIBILITY
1907 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique_perform(
1908 __node_pointer __nd) _NOEXCEPT
1910 size_type __bc = bucket_count();
1911 size_t __chash = __constrain_hash(__nd->__hash(), __bc);
1912 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
1913 __next_pointer __pn = __bucket_list_[__chash];
1914 if (__pn == nullptr)
1916 __pn =__p1_.first().__ptr();
1917 __nd->__next_ = __pn->__next_;
1918 __pn->__next_ = __nd->__ptr();
1919 // fix up __bucket_list_
1920 __bucket_list_[__chash] = __pn;
1921 if (__nd->__next_ != nullptr)
1922 __bucket_list_[__constrain_hash(__nd->__next_->__hash(), __bc)] = __nd->__ptr();
1926 __nd->__next_ = __pn->__next_;
1927 __pn->__next_ = __nd->__ptr();
1932 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1933 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1934 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd)
1936 __nd->__hash_ = hash_function()(__nd->__value_);
1937 __next_pointer __existing_node =
1938 __node_insert_unique_prepare(__nd->__hash(), __nd->__value_);
1940 // Insert the node, unless it already exists in the container.
1941 bool __inserted = false;
1942 if (__existing_node == nullptr)
1944 __node_insert_unique_perform(__nd);
1945 __existing_node = __nd->__ptr();
1948 #if _LIBCPP_DEBUG_LEVEL >= 2
1949 return pair<iterator, bool>(iterator(__existing_node, this), __inserted);
1951 return pair<iterator, bool>(iterator(__existing_node), __inserted);
1955 // Prepare the container for an insertion of the value __cp_val with the hash
1956 // __cp_hash. This does a lookup into the container to see if __cp_value is
1957 // already present, and performs a rehash if necessary. Returns a pointer to the
1958 // last occurance of __cp_val in the map.
1960 // Note that this function does forward exceptions if key_eq() throws, and never
1961 // mutates __value or actually inserts into the map.
1962 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1963 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
1964 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi_prepare(
1965 size_t __cp_hash, value_type& __cp_val)
1967 size_type __bc = bucket_count();
1968 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1970 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
1971 size_type(ceil(float(size() + 1) / max_load_factor()))));
1972 __bc = bucket_count();
1974 size_t __chash = __constrain_hash(__cp_hash, __bc);
1975 __next_pointer __pn = __bucket_list_[__chash];
1976 if (__pn != nullptr)
1978 for (bool __found = false; __pn->__next_ != nullptr &&
1979 __constrain_hash(__pn->__next_->__hash(), __bc) == __chash;
1980 __pn = __pn->__next_)
1982 // __found key_eq() action
1985 // false true set __found to true
1987 if (__found != (__pn->__next_->__hash() == __cp_hash &&
1988 key_eq()(__pn->__next_->__upcast()->__value_, __cp_val)))
2000 // Insert the node __cp into the container after __pn (which is the last node in
2001 // the bucket that compares equal to __cp). Rehashing, and checking for
2002 // uniqueness has already been performed (in __node_insert_multi_prepare), so
2003 // all we need to do is update the bucket and size(). Assumes that __cp->__hash
2005 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2007 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi_perform(
2008 __node_pointer __cp, __next_pointer __pn) _NOEXCEPT
2010 size_type __bc = bucket_count();
2011 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
2012 if (__pn == nullptr)
2014 __pn =__p1_.first().__ptr();
2015 __cp->__next_ = __pn->__next_;
2016 __pn->__next_ = __cp->__ptr();
2017 // fix up __bucket_list_
2018 __bucket_list_[__chash] = __pn;
2019 if (__cp->__next_ != nullptr)
2020 __bucket_list_[__constrain_hash(__cp->__next_->__hash(), __bc)]
2025 __cp->__next_ = __pn->__next_;
2026 __pn->__next_ = __cp->__ptr();
2027 if (__cp->__next_ != nullptr)
2029 size_t __nhash = __constrain_hash(__cp->__next_->__hash(), __bc);
2030 if (__nhash != __chash)
2031 __bucket_list_[__nhash] = __cp->__ptr();
2038 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2039 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2040 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp)
2042 __cp->__hash_ = hash_function()(__cp->__value_);
2043 __next_pointer __pn = __node_insert_multi_prepare(__cp->__hash(), __cp->__value_);
2044 __node_insert_multi_perform(__cp, __pn);
2046 #if _LIBCPP_DEBUG_LEVEL >= 2
2047 return iterator(__cp->__ptr(), this);
2049 return iterator(__cp->__ptr());
2053 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2054 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2055 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(
2056 const_iterator __p, __node_pointer __cp)
2058 #if _LIBCPP_DEBUG_LEVEL >= 2
2059 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2060 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
2061 " referring to this unordered container");
2063 if (__p != end() && key_eq()(*__p, __cp->__value_))
2065 __next_pointer __np = __p.__node_;
2066 __cp->__hash_ = __np->__hash();
2067 size_type __bc = bucket_count();
2068 if (size()+1 > __bc * max_load_factor() || __bc == 0)
2070 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
2071 size_type(ceil(float(size() + 1) / max_load_factor()))));
2072 __bc = bucket_count();
2074 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
2075 __next_pointer __pp = __bucket_list_[__chash];
2076 while (__pp->__next_ != __np)
2077 __pp = __pp->__next_;
2078 __cp->__next_ = __np;
2079 __pp->__next_ = static_cast<__next_pointer>(__cp);
2081 #if _LIBCPP_DEBUG_LEVEL >= 2
2082 return iterator(static_cast<__next_pointer>(__cp), this);
2084 return iterator(static_cast<__next_pointer>(__cp));
2087 return __node_insert_multi(__cp);
2092 #ifndef _LIBCPP_CXX03_LANG
2093 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2094 template <class _Key, class ..._Args>
2095 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
2096 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
2098 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2099 template <class _Key, class _Args>
2100 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
2101 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
2105 size_t __hash = hash_function()(__k);
2106 size_type __bc = bucket_count();
2107 bool __inserted = false;
2108 __next_pointer __nd;
2112 __chash = __constrain_hash(__hash, __bc);
2113 __nd = __bucket_list_[__chash];
2114 if (__nd != nullptr)
2116 for (__nd = __nd->__next_; __nd != nullptr &&
2117 (__nd->__hash() == __hash || __constrain_hash(__nd->__hash(), __bc) == __chash);
2118 __nd = __nd->__next_)
2120 if (key_eq()(__nd->__upcast()->__value_, __k))
2126 #ifndef _LIBCPP_CXX03_LANG
2127 __node_holder __h = __construct_node_hash(__hash, _VSTD::forward<_Args>(__args)...);
2129 __node_holder __h = __construct_node_hash(__hash, __args);
2131 if (size()+1 > __bc * max_load_factor() || __bc == 0)
2133 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
2134 size_type(ceil(float(size() + 1) / max_load_factor()))));
2135 __bc = bucket_count();
2136 __chash = __constrain_hash(__hash, __bc);
2138 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
2139 __next_pointer __pn = __bucket_list_[__chash];
2140 if (__pn == nullptr)
2142 __pn = __p1_.first().__ptr();
2143 __h->__next_ = __pn->__next_;
2144 __pn->__next_ = __h.get()->__ptr();
2145 // fix up __bucket_list_
2146 __bucket_list_[__chash] = __pn;
2147 if (__h->__next_ != nullptr)
2148 __bucket_list_[__constrain_hash(__h->__next_->__hash(), __bc)]
2149 = __h.get()->__ptr();
2153 __h->__next_ = __pn->__next_;
2154 __pn->__next_ = static_cast<__next_pointer>(__h.get());
2156 __nd = static_cast<__next_pointer>(__h.release());
2162 #if _LIBCPP_DEBUG_LEVEL >= 2
2163 return pair<iterator, bool>(iterator(__nd, this), __inserted);
2165 return pair<iterator, bool>(iterator(__nd), __inserted);
2169 #ifndef _LIBCPP_CXX03_LANG
2171 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2172 template <class... _Args>
2173 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
2174 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_impl(_Args&&... __args)
2176 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2177 pair<iterator, bool> __r = __node_insert_unique(__h.get());
2183 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2184 template <class... _Args>
2185 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2186 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args)
2188 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2189 iterator __r = __node_insert_multi(__h.get());
2194 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2195 template <class... _Args>
2196 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2197 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(
2198 const_iterator __p, _Args&&... __args)
2200 #if _LIBCPP_DEBUG_LEVEL >= 2
2201 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2202 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
2203 " referring to this unordered container");
2205 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2206 iterator __r = __node_insert_multi(__p, __h.get());
2211 #else // _LIBCPP_CXX03_LANG
2213 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2214 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2215 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const __container_value_type& __x)
2217 __node_holder __h = __construct_node(__x);
2218 iterator __r = __node_insert_multi(__h.get());
2223 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2224 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2225 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
2226 const __container_value_type& __x)
2228 #if _LIBCPP_DEBUG_LEVEL >= 2
2229 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2230 "unordered container::insert(const_iterator, lvalue) called with an iterator not"
2231 " referring to this unordered container");
2233 __node_holder __h = __construct_node(__x);
2234 iterator __r = __node_insert_multi(__p, __h.get());
2239 #endif // _LIBCPP_CXX03_LANG
2241 #if _LIBCPP_STD_VER > 14
2242 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2243 template <class _NodeHandle, class _InsertReturnType>
2244 _LIBCPP_INLINE_VISIBILITY
2246 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_unique(
2250 return _InsertReturnType{end(), false, _NodeHandle()};
2251 pair<iterator, bool> __result = __node_insert_unique(__nh.__ptr_);
2252 if (__result.second)
2253 __nh.__release_ptr();
2254 return _InsertReturnType{__result.first, __result.second, _VSTD::move(__nh)};
2257 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2258 template <class _NodeHandle>
2259 _LIBCPP_INLINE_VISIBILITY
2260 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2261 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_unique(
2262 const_iterator, _NodeHandle&& __nh)
2266 pair<iterator, bool> __result = __node_insert_unique(__nh.__ptr_);
2267 if (__result.second)
2268 __nh.__release_ptr();
2269 return __result.first;
2272 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2273 template <class _NodeHandle>
2274 _LIBCPP_INLINE_VISIBILITY
2276 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_extract(
2277 key_type const& __key)
2279 iterator __i = find(__key);
2281 return _NodeHandle();
2282 return __node_handle_extract<_NodeHandle>(__i);
2285 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2286 template <class _NodeHandle>
2287 _LIBCPP_INLINE_VISIBILITY
2289 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_extract(
2292 allocator_type __alloc(__node_alloc());
2293 return _NodeHandle(remove(__p).release(), __alloc);
2296 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2297 template <class _Table>
2298 _LIBCPP_INLINE_VISIBILITY
2300 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_merge_unique(
2303 static_assert(is_same<__node, typename _Table::__node>::value, "");
2305 for (typename _Table::iterator __it = __source.begin();
2306 __it != __source.end();)
2308 __node_pointer __src_ptr = __it.__node_->__upcast();
2309 size_t __hash = hash_function()(__src_ptr->__value_);
2310 __next_pointer __existing_node =
2311 __node_insert_unique_prepare(__hash, __src_ptr->__value_);
2312 auto __prev_iter = __it++;
2313 if (__existing_node == nullptr)
2315 (void)__source.remove(__prev_iter).release();
2316 __src_ptr->__hash_ = __hash;
2317 __node_insert_unique_perform(__src_ptr);
2322 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2323 template <class _NodeHandle>
2324 _LIBCPP_INLINE_VISIBILITY
2325 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2326 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_multi(
2331 iterator __result = __node_insert_multi(__nh.__ptr_);
2332 __nh.__release_ptr();
2336 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2337 template <class _NodeHandle>
2338 _LIBCPP_INLINE_VISIBILITY
2339 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2340 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_multi(
2341 const_iterator __hint, _NodeHandle&& __nh)
2345 iterator __result = __node_insert_multi(__hint, __nh.__ptr_);
2346 __nh.__release_ptr();
2350 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2351 template <class _Table>
2352 _LIBCPP_INLINE_VISIBILITY
2354 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_merge_multi(
2357 static_assert(is_same<typename _Table::__node, __node>::value, "");
2359 for (typename _Table::iterator __it = __source.begin();
2360 __it != __source.end();)
2362 __node_pointer __src_ptr = __it.__node_->__upcast();
2363 size_t __src_hash = hash_function()(__src_ptr->__value_);
2364 __next_pointer __pn =
2365 __node_insert_multi_prepare(__src_hash, __src_ptr->__value_);
2366 (void)__source.remove(__it++).release();
2367 __src_ptr->__hash_ = __src_hash;
2368 __node_insert_multi_perform(__src_ptr, __pn);
2371 #endif // _LIBCPP_STD_VER > 14
2373 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2375 __hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
2376 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
2380 else if (__n & (__n - 1))
2381 __n = __next_prime(__n);
2382 size_type __bc = bucket_count();
2385 else if (__n < __bc)
2387 __n = _VSTD::max<size_type>
2390 __is_hash_power2(__bc) ? __next_hash_pow2(size_t(ceil(float(size()) / max_load_factor()))) :
2391 __next_prime(size_t(ceil(float(size()) / max_load_factor())))
2398 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2400 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc)
2402 #if _LIBCPP_DEBUG_LEVEL >= 2
2403 __get_db()->__invalidate_all(this);
2404 #endif // _LIBCPP_DEBUG_LEVEL >= 2
2405 __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
2406 __bucket_list_.reset(__nbc > 0 ?
2407 __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
2408 __bucket_list_.get_deleter().size() = __nbc;
2411 for (size_type __i = 0; __i < __nbc; ++__i)
2412 __bucket_list_[__i] = nullptr;
2413 __next_pointer __pp = __p1_.first().__ptr();
2414 __next_pointer __cp = __pp->__next_;
2415 if (__cp != nullptr)
2417 size_type __chash = __constrain_hash(__cp->__hash(), __nbc);
2418 __bucket_list_[__chash] = __pp;
2419 size_type __phash = __chash;
2420 for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr;
2421 __cp = __pp->__next_)
2423 __chash = __constrain_hash(__cp->__hash(), __nbc);
2424 if (__chash == __phash)
2428 if (__bucket_list_[__chash] == nullptr)
2430 __bucket_list_[__chash] = __pp;
2436 __next_pointer __np = __cp;
2437 for (; __np->__next_ != nullptr &&
2438 key_eq()(__cp->__upcast()->__value_,
2439 __np->__next_->__upcast()->__value_);
2440 __np = __np->__next_)
2442 __pp->__next_ = __np->__next_;
2443 __np->__next_ = __bucket_list_[__chash]->__next_;
2444 __bucket_list_[__chash]->__next_ = __cp;
2453 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2454 template <class _Key>
2455 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2456 __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
2458 size_t __hash = hash_function()(__k);
2459 size_type __bc = bucket_count();
2462 size_t __chash = __constrain_hash(__hash, __bc);
2463 __next_pointer __nd = __bucket_list_[__chash];
2464 if (__nd != nullptr)
2466 for (__nd = __nd->__next_; __nd != nullptr &&
2467 (__nd->__hash() == __hash
2468 || __constrain_hash(__nd->__hash(), __bc) == __chash);
2469 __nd = __nd->__next_)
2471 if ((__nd->__hash() == __hash)
2472 && key_eq()(__nd->__upcast()->__value_, __k))
2473 #if _LIBCPP_DEBUG_LEVEL >= 2
2474 return iterator(__nd, this);
2476 return iterator(__nd);
2484 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2485 template <class _Key>
2486 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
2487 __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
2489 size_t __hash = hash_function()(__k);
2490 size_type __bc = bucket_count();
2493 size_t __chash = __constrain_hash(__hash, __bc);
2494 __next_pointer __nd = __bucket_list_[__chash];
2495 if (__nd != nullptr)
2497 for (__nd = __nd->__next_; __nd != nullptr &&
2498 (__hash == __nd->__hash()
2499 || __constrain_hash(__nd->__hash(), __bc) == __chash);
2500 __nd = __nd->__next_)
2502 if ((__nd->__hash() == __hash)
2503 && key_eq()(__nd->__upcast()->__value_, __k))
2504 #if _LIBCPP_DEBUG_LEVEL >= 2
2505 return const_iterator(__nd, this);
2507 return const_iterator(__nd);
2516 #ifndef _LIBCPP_CXX03_LANG
2518 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2519 template <class ..._Args>
2520 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2521 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args)
2523 static_assert(!__is_hash_value_type<_Args...>::value,
2524 "Construct cannot be called with a hash value type");
2525 __node_allocator& __na = __node_alloc();
2526 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2527 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
2528 __h.get_deleter().__value_constructed = true;
2529 __h->__hash_ = hash_function()(__h->__value_);
2530 __h->__next_ = nullptr;
2534 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2535 template <class _First, class ..._Rest>
2536 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2537 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(
2538 size_t __hash, _First&& __f, _Rest&& ...__rest)
2540 static_assert(!__is_hash_value_type<_First, _Rest...>::value,
2541 "Construct cannot be called with a hash value type");
2542 __node_allocator& __na = __node_alloc();
2543 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2544 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_),
2545 _VSTD::forward<_First>(__f),
2546 _VSTD::forward<_Rest>(__rest)...);
2547 __h.get_deleter().__value_constructed = true;
2548 __h->__hash_ = __hash;
2549 __h->__next_ = nullptr;
2553 #else // _LIBCPP_CXX03_LANG
2555 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2556 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2557 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const __container_value_type& __v)
2559 __node_allocator& __na = __node_alloc();
2560 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2561 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
2562 __h.get_deleter().__value_constructed = true;
2563 __h->__hash_ = hash_function()(__h->__value_);
2564 __h->__next_ = nullptr;
2565 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
2568 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2569 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2570 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(size_t __hash,
2571 const __container_value_type& __v)
2573 __node_allocator& __na = __node_alloc();
2574 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2575 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
2576 __h.get_deleter().__value_constructed = true;
2577 __h->__hash_ = __hash;
2578 __h->__next_ = nullptr;
2579 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
2582 #endif // _LIBCPP_CXX03_LANG
2584 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2585 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2586 __hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p)
2588 __next_pointer __np = __p.__node_;
2589 #if _LIBCPP_DEBUG_LEVEL >= 2
2590 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2591 "unordered container erase(iterator) called with an iterator not"
2592 " referring to this container");
2593 _LIBCPP_ASSERT(__p != end(),
2594 "unordered container erase(iterator) called with a non-dereferenceable iterator");
2595 iterator __r(__np, this);
2604 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2605 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2606 __hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first,
2607 const_iterator __last)
2609 #if _LIBCPP_DEBUG_LEVEL >= 2
2610 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2611 "unodered container::erase(iterator, iterator) called with an iterator not"
2612 " referring to this unodered container");
2613 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
2614 "unodered container::erase(iterator, iterator) called with an iterator not"
2615 " referring to this unodered container");
2617 for (const_iterator __p = __first; __first != __last; __p = __first)
2622 __next_pointer __np = __last.__node_;
2623 #if _LIBCPP_DEBUG_LEVEL >= 2
2624 return iterator (__np, this);
2626 return iterator (__np);
2630 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2631 template <class _Key>
2632 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2633 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k)
2635 iterator __i = find(__k);
2642 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2643 template <class _Key>
2644 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2645 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k)
2648 iterator __i = find(__k);
2651 iterator __e = end();
2656 } while (__i != __e && key_eq()(*__i, __k));
2661 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2662 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2663 __hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT
2666 __next_pointer __cn = __p.__node_;
2667 size_type __bc = bucket_count();
2668 size_t __chash = __constrain_hash(__cn->__hash(), __bc);
2669 // find previous node
2670 __next_pointer __pn = __bucket_list_[__chash];
2671 for (; __pn->__next_ != __cn; __pn = __pn->__next_)
2673 // Fix up __bucket_list_
2674 // if __pn is not in same bucket (before begin is not in same bucket) &&
2675 // if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
2676 if (__pn == __p1_.first().__ptr()
2677 || __constrain_hash(__pn->__hash(), __bc) != __chash)
2679 if (__cn->__next_ == nullptr
2680 || __constrain_hash(__cn->__next_->__hash(), __bc) != __chash)
2681 __bucket_list_[__chash] = nullptr;
2683 // if __cn->__next_ is not in same bucket (nullptr is in same bucket)
2684 if (__cn->__next_ != nullptr)
2686 size_t __nhash = __constrain_hash(__cn->__next_->__hash(), __bc);
2687 if (__nhash != __chash)
2688 __bucket_list_[__nhash] = __pn;
2691 __pn->__next_ = __cn->__next_;
2692 __cn->__next_ = nullptr;
2694 #if _LIBCPP_DEBUG_LEVEL >= 2
2695 __c_node* __c = __get_db()->__find_c_and_lock(this);
2696 for (__i_node** __dp = __c->end_; __dp != __c->beg_; )
2699 iterator* __i = static_cast<iterator*>((*__dp)->__i_);
2700 if (__i->__node_ == __cn)
2702 (*__dp)->__c_ = nullptr;
2703 if (--__c->end_ != __dp)
2704 memmove(__dp, __dp+1, (__c->end_ - __dp)*sizeof(__i_node*));
2707 __get_db()->unlock();
2709 return __node_holder(__cn->__upcast(), _Dp(__node_alloc(), true));
2712 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2713 template <class _Key>
2715 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2716 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const
2718 return static_cast<size_type>(find(__k) != end());
2721 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2722 template <class _Key>
2723 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2724 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const
2727 const_iterator __i = find(__k);
2730 const_iterator __e = end();
2735 } while (__i != __e && key_eq()(*__i, __k));
2740 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2741 template <class _Key>
2742 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2743 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2744 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2747 iterator __i = find(__k);
2751 return pair<iterator, iterator>(__i, __j);
2754 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2755 template <class _Key>
2756 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2757 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2758 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2759 const _Key& __k) const
2761 const_iterator __i = find(__k);
2762 const_iterator __j = __i;
2765 return pair<const_iterator, const_iterator>(__i, __j);
2768 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2769 template <class _Key>
2770 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2771 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2772 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2775 iterator __i = find(__k);
2779 iterator __e = end();
2783 } while (__j != __e && key_eq()(*__j, __k));
2785 return pair<iterator, iterator>(__i, __j);
2788 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2789 template <class _Key>
2790 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2791 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2792 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2793 const _Key& __k) const
2795 const_iterator __i = find(__k);
2796 const_iterator __j = __i;
2799 const_iterator __e = end();
2803 } while (__j != __e && key_eq()(*__j, __k));
2805 return pair<const_iterator, const_iterator>(__i, __j);
2808 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2810 __hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
2811 #if _LIBCPP_STD_VER <= 11
2813 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
2814 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
2815 || __is_nothrow_swappable<__pointer_allocator>::value)
2816 && (!__node_traits::propagate_on_container_swap::value
2817 || __is_nothrow_swappable<__node_allocator>::value)
2820 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value)
2823 _LIBCPP_ASSERT(__node_traits::propagate_on_container_swap::value ||
2824 this->__node_alloc() == __u.__node_alloc(),
2825 "list::swap: Either propagate_on_container_swap must be true"
2826 " or the allocators must compare equal");
2828 __node_pointer_pointer __npp = __bucket_list_.release();
2829 __bucket_list_.reset(__u.__bucket_list_.release());
2830 __u.__bucket_list_.reset(__npp);
2832 _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
2833 __swap_allocator(__bucket_list_.get_deleter().__alloc(),
2834 __u.__bucket_list_.get_deleter().__alloc());
2835 __swap_allocator(__node_alloc(), __u.__node_alloc());
2836 _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
2837 __p2_.swap(__u.__p2_);
2838 __p3_.swap(__u.__p3_);
2840 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
2841 __p1_.first().__ptr();
2843 __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash(), __u.bucket_count())] =
2844 __u.__p1_.first().__ptr();
2845 #if _LIBCPP_DEBUG_LEVEL >= 2
2846 __get_db()->swap(this, &__u);
2850 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2851 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2852 __hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const
2854 _LIBCPP_ASSERT(__n < bucket_count(),
2855 "unordered container::bucket_size(n) called with n >= bucket_count()");
2856 __next_pointer __np = __bucket_list_[__n];
2857 size_type __bc = bucket_count();
2859 if (__np != nullptr)
2861 for (__np = __np->__next_; __np != nullptr &&
2862 __constrain_hash(__np->__hash(), __bc) == __n;
2863 __np = __np->__next_, ++__r)
2869 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2870 inline _LIBCPP_INLINE_VISIBILITY
2872 swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x,
2873 __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
2874 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2879 #if _LIBCPP_DEBUG_LEVEL >= 2
2881 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2883 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const
2885 return __i->__node_ != nullptr;
2888 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2890 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const
2895 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2897 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2902 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2904 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2909 #endif // _LIBCPP_DEBUG_LEVEL >= 2
2911 _LIBCPP_END_NAMESPACE_STD
2915 #endif // _LIBCPP__HASH_TABLE