2 //===----------------------------------------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP__HASH_TABLE
12 #define _LIBCPP__HASH_TABLE
15 #include <initializer_list>
21 #include <__undef_min_max>
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 #pragma GCC system_header
29 _LIBCPP_BEGIN_NAMESPACE_STD
32 size_t __next_prime(size_t __n);
34 template <class _NodePtr>
35 struct __hash_node_base
37 typedef __hash_node_base __first_node;
41 _LIBCPP_INLINE_VISIBILITY __hash_node_base() _NOEXCEPT : __next_(nullptr) {}
44 template <class _Tp, class _VoidPtr>
46 : public __hash_node_base
48 typename pointer_traits<_VoidPtr>::template
49 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
50 rebind<__hash_node<_Tp, _VoidPtr> >
52 rebind<__hash_node<_Tp, _VoidPtr> >::other
56 typedef _Tp value_type;
62 inline _LIBCPP_INLINE_VISIBILITY
64 __is_power2(size_t __bc)
66 return __bc > 2 && !(__bc & (__bc - 1));
69 inline _LIBCPP_INLINE_VISIBILITY
71 __constrain_hash(size_t __h, size_t __bc)
73 return !(__bc & (__bc - 1)) ? __h & (__bc - 1) : __h % __bc;
76 inline _LIBCPP_INLINE_VISIBILITY
78 __next_pow2(size_t __n)
80 return size_t(1) << (std::numeric_limits<size_t>::digits - __clz(__n-1));
83 template <class _Tp, class _Hash, class _Equal, class _Alloc> class __hash_table;
84 template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator;
85 template <class _HashIterator> class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
86 template <class _HashIterator> class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
87 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
88 class _LIBCPP_TYPE_VIS_ONLY unordered_map;
90 template <class _NodePtr>
91 class _LIBCPP_TYPE_VIS_ONLY __hash_iterator
93 typedef _NodePtr __node_pointer;
95 __node_pointer __node_;
98 typedef forward_iterator_tag iterator_category;
99 typedef typename pointer_traits<__node_pointer>::element_type::value_type value_type;
100 typedef typename pointer_traits<__node_pointer>::difference_type difference_type;
101 typedef value_type& reference;
102 typedef typename pointer_traits<__node_pointer>::template
103 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
106 rebind<value_type>::other
110 _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT
111 #if _LIBCPP_STD_VER > 11
115 #if _LIBCPP_DEBUG_LEVEL >= 2
116 __get_db()->__insert_i(this);
120 #if _LIBCPP_DEBUG_LEVEL >= 2
122 _LIBCPP_INLINE_VISIBILITY
123 __hash_iterator(const __hash_iterator& __i)
124 : __node_(__i.__node_)
126 __get_db()->__iterator_copy(this, &__i);
129 _LIBCPP_INLINE_VISIBILITY
132 __get_db()->__erase_i(this);
135 _LIBCPP_INLINE_VISIBILITY
136 __hash_iterator& operator=(const __hash_iterator& __i)
140 __get_db()->__iterator_copy(this, &__i);
141 __node_ = __i.__node_;
146 #endif // _LIBCPP_DEBUG_LEVEL >= 2
148 _LIBCPP_INLINE_VISIBILITY
149 reference operator*() const
151 #if _LIBCPP_DEBUG_LEVEL >= 2
152 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
153 "Attempted to dereference a non-dereferenceable unordered container iterator");
155 return __node_->__value_;
157 _LIBCPP_INLINE_VISIBILITY
158 pointer operator->() const
160 #if _LIBCPP_DEBUG_LEVEL >= 2
161 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
162 "Attempted to dereference a non-dereferenceable unordered container iterator");
164 return pointer_traits<pointer>::pointer_to(__node_->__value_);
167 _LIBCPP_INLINE_VISIBILITY
168 __hash_iterator& operator++()
170 #if _LIBCPP_DEBUG_LEVEL >= 2
171 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
172 "Attempted to increment non-incrementable unordered container iterator");
174 __node_ = __node_->__next_;
178 _LIBCPP_INLINE_VISIBILITY
179 __hash_iterator operator++(int)
181 __hash_iterator __t(*this);
186 friend _LIBCPP_INLINE_VISIBILITY
187 bool operator==(const __hash_iterator& __x, const __hash_iterator& __y)
189 return __x.__node_ == __y.__node_;
191 friend _LIBCPP_INLINE_VISIBILITY
192 bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y)
193 {return !(__x == __y);}
196 #if _LIBCPP_DEBUG_LEVEL >= 2
197 _LIBCPP_INLINE_VISIBILITY
198 __hash_iterator(__node_pointer __node, const void* __c) _NOEXCEPT
201 __get_db()->__insert_ic(this, __c);
204 _LIBCPP_INLINE_VISIBILITY
205 __hash_iterator(__node_pointer __node) _NOEXCEPT
210 template <class, class, class, class> friend class __hash_table;
211 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator;
212 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
213 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
214 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
217 template <class _ConstNodePtr>
218 class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator
220 typedef _ConstNodePtr __node_pointer;
222 __node_pointer __node_;
224 typedef typename remove_const<
225 typename pointer_traits<__node_pointer>::element_type
229 typedef forward_iterator_tag iterator_category;
230 typedef typename __node::value_type value_type;
231 typedef typename pointer_traits<__node_pointer>::difference_type difference_type;
232 typedef const value_type& reference;
233 typedef typename pointer_traits<__node_pointer>::template
234 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
235 rebind<const value_type>
237 rebind<const value_type>::other
240 typedef typename pointer_traits<__node_pointer>::template
241 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
244 rebind<__node>::other
246 __non_const_node_pointer;
247 typedef __hash_iterator<__non_const_node_pointer> __non_const_iterator;
249 _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT
250 #if _LIBCPP_STD_VER > 11
254 #if _LIBCPP_DEBUG_LEVEL >= 2
255 __get_db()->__insert_i(this);
258 _LIBCPP_INLINE_VISIBILITY
259 __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT
260 : __node_(__x.__node_)
262 #if _LIBCPP_DEBUG_LEVEL >= 2
263 __get_db()->__iterator_copy(this, &__x);
267 #if _LIBCPP_DEBUG_LEVEL >= 2
269 _LIBCPP_INLINE_VISIBILITY
270 __hash_const_iterator(const __hash_const_iterator& __i)
271 : __node_(__i.__node_)
273 __get_db()->__iterator_copy(this, &__i);
276 _LIBCPP_INLINE_VISIBILITY
277 ~__hash_const_iterator()
279 __get_db()->__erase_i(this);
282 _LIBCPP_INLINE_VISIBILITY
283 __hash_const_iterator& operator=(const __hash_const_iterator& __i)
287 __get_db()->__iterator_copy(this, &__i);
288 __node_ = __i.__node_;
293 #endif // _LIBCPP_DEBUG_LEVEL >= 2
295 _LIBCPP_INLINE_VISIBILITY
296 reference operator*() const
298 #if _LIBCPP_DEBUG_LEVEL >= 2
299 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
300 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
302 return __node_->__value_;
304 _LIBCPP_INLINE_VISIBILITY
305 pointer operator->() const
307 #if _LIBCPP_DEBUG_LEVEL >= 2
308 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
309 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
311 return pointer_traits<pointer>::pointer_to(__node_->__value_);
314 _LIBCPP_INLINE_VISIBILITY
315 __hash_const_iterator& operator++()
317 #if _LIBCPP_DEBUG_LEVEL >= 2
318 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
319 "Attempted to increment non-incrementable unordered container const_iterator");
321 __node_ = __node_->__next_;
325 _LIBCPP_INLINE_VISIBILITY
326 __hash_const_iterator operator++(int)
328 __hash_const_iterator __t(*this);
333 friend _LIBCPP_INLINE_VISIBILITY
334 bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
336 return __x.__node_ == __y.__node_;
338 friend _LIBCPP_INLINE_VISIBILITY
339 bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
340 {return !(__x == __y);}
343 #if _LIBCPP_DEBUG_LEVEL >= 2
344 _LIBCPP_INLINE_VISIBILITY
345 __hash_const_iterator(__node_pointer __node, const void* __c) _NOEXCEPT
348 __get_db()->__insert_ic(this, __c);
351 _LIBCPP_INLINE_VISIBILITY
352 __hash_const_iterator(__node_pointer __node) _NOEXCEPT
357 template <class, class, class, class> friend class __hash_table;
358 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
359 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
360 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
363 template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
365 template <class _NodePtr>
366 class _LIBCPP_TYPE_VIS_ONLY __hash_local_iterator
368 typedef _NodePtr __node_pointer;
370 __node_pointer __node_;
372 size_t __bucket_count_;
374 typedef pointer_traits<__node_pointer> __pointer_traits;
376 typedef forward_iterator_tag iterator_category;
377 typedef typename __pointer_traits::element_type::value_type value_type;
378 typedef typename __pointer_traits::difference_type difference_type;
379 typedef value_type& reference;
380 typedef typename __pointer_traits::template
381 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
384 rebind<value_type>::other
388 _LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT
390 #if _LIBCPP_DEBUG_LEVEL >= 2
391 __get_db()->__insert_i(this);
395 #if _LIBCPP_DEBUG_LEVEL >= 2
397 _LIBCPP_INLINE_VISIBILITY
398 __hash_local_iterator(const __hash_local_iterator& __i)
399 : __node_(__i.__node_),
400 __bucket_(__i.__bucket_),
401 __bucket_count_(__i.__bucket_count_)
403 __get_db()->__iterator_copy(this, &__i);
406 _LIBCPP_INLINE_VISIBILITY
407 ~__hash_local_iterator()
409 __get_db()->__erase_i(this);
412 _LIBCPP_INLINE_VISIBILITY
413 __hash_local_iterator& operator=(const __hash_local_iterator& __i)
417 __get_db()->__iterator_copy(this, &__i);
418 __node_ = __i.__node_;
419 __bucket_ = __i.__bucket_;
420 __bucket_count_ = __i.__bucket_count_;
425 #endif // _LIBCPP_DEBUG_LEVEL >= 2
427 _LIBCPP_INLINE_VISIBILITY
428 reference operator*() const
430 #if _LIBCPP_DEBUG_LEVEL >= 2
431 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
432 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
434 return __node_->__value_;
436 _LIBCPP_INLINE_VISIBILITY
437 pointer operator->() const
439 #if _LIBCPP_DEBUG_LEVEL >= 2
440 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
441 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
443 return pointer_traits<pointer>::pointer_to(__node_->__value_);
446 _LIBCPP_INLINE_VISIBILITY
447 __hash_local_iterator& operator++()
449 #if _LIBCPP_DEBUG_LEVEL >= 2
450 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
451 "Attempted to increment non-incrementable unordered container local_iterator");
453 __node_ = __node_->__next_;
454 if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_)
459 _LIBCPP_INLINE_VISIBILITY
460 __hash_local_iterator operator++(int)
462 __hash_local_iterator __t(*this);
467 friend _LIBCPP_INLINE_VISIBILITY
468 bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
470 return __x.__node_ == __y.__node_;
472 friend _LIBCPP_INLINE_VISIBILITY
473 bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
474 {return !(__x == __y);}
477 #if _LIBCPP_DEBUG_LEVEL >= 2
478 _LIBCPP_INLINE_VISIBILITY
479 __hash_local_iterator(__node_pointer __node, size_t __bucket,
480 size_t __bucket_count, const void* __c) _NOEXCEPT
483 __bucket_count_(__bucket_count)
485 __get_db()->__insert_ic(this, __c);
486 if (__node_ != nullptr)
487 __node_ = __node_->__next_;
490 _LIBCPP_INLINE_VISIBILITY
491 __hash_local_iterator(__node_pointer __node, size_t __bucket,
492 size_t __bucket_count) _NOEXCEPT
495 __bucket_count_(__bucket_count)
497 if (__node_ != nullptr)
498 __node_ = __node_->__next_;
501 template <class, class, class, class> friend class __hash_table;
502 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
503 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
506 template <class _ConstNodePtr>
507 class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator
509 typedef _ConstNodePtr __node_pointer;
511 __node_pointer __node_;
513 size_t __bucket_count_;
515 typedef pointer_traits<__node_pointer> __pointer_traits;
516 typedef typename __pointer_traits::element_type __node;
517 typedef typename remove_const<__node>::type __non_const_node;
518 typedef typename pointer_traits<__node_pointer>::template
519 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
520 rebind<__non_const_node>
522 rebind<__non_const_node>::other
524 __non_const_node_pointer;
525 typedef __hash_local_iterator<__non_const_node_pointer>
526 __non_const_iterator;
528 typedef forward_iterator_tag iterator_category;
529 typedef typename remove_const<
530 typename __pointer_traits::element_type::value_type
532 typedef typename __pointer_traits::difference_type difference_type;
533 typedef const value_type& reference;
534 typedef typename __pointer_traits::template
535 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
536 rebind<const value_type>
538 rebind<const value_type>::other
542 _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT
544 #if _LIBCPP_DEBUG_LEVEL >= 2
545 __get_db()->__insert_i(this);
549 _LIBCPP_INLINE_VISIBILITY
550 __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT
551 : __node_(__x.__node_),
552 __bucket_(__x.__bucket_),
553 __bucket_count_(__x.__bucket_count_)
555 #if _LIBCPP_DEBUG_LEVEL >= 2
556 __get_db()->__iterator_copy(this, &__x);
560 #if _LIBCPP_DEBUG_LEVEL >= 2
562 _LIBCPP_INLINE_VISIBILITY
563 __hash_const_local_iterator(const __hash_const_local_iterator& __i)
564 : __node_(__i.__node_),
565 __bucket_(__i.__bucket_),
566 __bucket_count_(__i.__bucket_count_)
568 __get_db()->__iterator_copy(this, &__i);
571 _LIBCPP_INLINE_VISIBILITY
572 ~__hash_const_local_iterator()
574 __get_db()->__erase_i(this);
577 _LIBCPP_INLINE_VISIBILITY
578 __hash_const_local_iterator& operator=(const __hash_const_local_iterator& __i)
582 __get_db()->__iterator_copy(this, &__i);
583 __node_ = __i.__node_;
584 __bucket_ = __i.__bucket_;
585 __bucket_count_ = __i.__bucket_count_;
590 #endif // _LIBCPP_DEBUG_LEVEL >= 2
592 _LIBCPP_INLINE_VISIBILITY
593 reference operator*() const
595 #if _LIBCPP_DEBUG_LEVEL >= 2
596 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
597 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
599 return __node_->__value_;
601 _LIBCPP_INLINE_VISIBILITY
602 pointer operator->() const
604 #if _LIBCPP_DEBUG_LEVEL >= 2
605 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
606 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
608 return pointer_traits<pointer>::pointer_to(__node_->__value_);
611 _LIBCPP_INLINE_VISIBILITY
612 __hash_const_local_iterator& operator++()
614 #if _LIBCPP_DEBUG_LEVEL >= 2
615 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
616 "Attempted to increment non-incrementable unordered container const_local_iterator");
618 __node_ = __node_->__next_;
619 if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_)
624 _LIBCPP_INLINE_VISIBILITY
625 __hash_const_local_iterator operator++(int)
627 __hash_const_local_iterator __t(*this);
632 friend _LIBCPP_INLINE_VISIBILITY
633 bool operator==(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
635 return __x.__node_ == __y.__node_;
637 friend _LIBCPP_INLINE_VISIBILITY
638 bool operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
639 {return !(__x == __y);}
642 #if _LIBCPP_DEBUG_LEVEL >= 2
643 _LIBCPP_INLINE_VISIBILITY
644 __hash_const_local_iterator(__node_pointer __node, size_t __bucket,
645 size_t __bucket_count, const void* __c) _NOEXCEPT
648 __bucket_count_(__bucket_count)
650 __get_db()->__insert_ic(this, __c);
651 if (__node_ != nullptr)
652 __node_ = __node_->__next_;
655 _LIBCPP_INLINE_VISIBILITY
656 __hash_const_local_iterator(__node_pointer __node, size_t __bucket,
657 size_t __bucket_count) _NOEXCEPT
660 __bucket_count_(__bucket_count)
662 if (__node_ != nullptr)
663 __node_ = __node_->__next_;
666 template <class, class, class, class> friend class __hash_table;
667 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
670 template <class _Alloc>
671 class __bucket_list_deallocator
673 typedef _Alloc allocator_type;
674 typedef allocator_traits<allocator_type> __alloc_traits;
675 typedef typename __alloc_traits::size_type size_type;
677 __compressed_pair<size_type, allocator_type> __data_;
679 typedef typename __alloc_traits::pointer pointer;
681 _LIBCPP_INLINE_VISIBILITY
682 __bucket_list_deallocator()
683 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
686 _LIBCPP_INLINE_VISIBILITY
687 __bucket_list_deallocator(const allocator_type& __a, size_type __size)
688 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
689 : __data_(__size, __a) {}
691 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
693 _LIBCPP_INLINE_VISIBILITY
694 __bucket_list_deallocator(__bucket_list_deallocator&& __x)
695 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
696 : __data_(_VSTD::move(__x.__data_))
701 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
703 _LIBCPP_INLINE_VISIBILITY
704 size_type& size() _NOEXCEPT {return __data_.first();}
705 _LIBCPP_INLINE_VISIBILITY
706 size_type size() const _NOEXCEPT {return __data_.first();}
708 _LIBCPP_INLINE_VISIBILITY
709 allocator_type& __alloc() _NOEXCEPT {return __data_.second();}
710 _LIBCPP_INLINE_VISIBILITY
711 const allocator_type& __alloc() const _NOEXCEPT {return __data_.second();}
713 _LIBCPP_INLINE_VISIBILITY
714 void operator()(pointer __p) _NOEXCEPT
716 __alloc_traits::deallocate(__alloc(), __p, size());
720 template <class _Alloc> class __hash_map_node_destructor;
722 template <class _Alloc>
723 class __hash_node_destructor
725 typedef _Alloc allocator_type;
726 typedef allocator_traits<allocator_type> __alloc_traits;
727 typedef typename __alloc_traits::value_type::value_type value_type;
729 typedef typename __alloc_traits::pointer pointer;
732 allocator_type& __na_;
734 __hash_node_destructor& operator=(const __hash_node_destructor&);
737 bool __value_constructed;
739 _LIBCPP_INLINE_VISIBILITY
740 explicit __hash_node_destructor(allocator_type& __na,
741 bool __constructed = false) _NOEXCEPT
743 __value_constructed(__constructed)
746 _LIBCPP_INLINE_VISIBILITY
747 void operator()(pointer __p) _NOEXCEPT
749 if (__value_constructed)
750 __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_));
752 __alloc_traits::deallocate(__na_, __p, 1);
755 template <class> friend class __hash_map_node_destructor;
758 template <class _Tp, class _Hash, class _Equal, class _Alloc>
762 typedef _Tp value_type;
763 typedef _Hash hasher;
764 typedef _Equal key_equal;
765 typedef _Alloc allocator_type;
768 typedef allocator_traits<allocator_type> __alloc_traits;
770 typedef value_type& reference;
771 typedef const value_type& const_reference;
772 typedef typename __alloc_traits::pointer pointer;
773 typedef typename __alloc_traits::const_pointer const_pointer;
774 typedef typename __alloc_traits::size_type size_type;
775 typedef typename __alloc_traits::difference_type difference_type;
778 typedef __hash_node<value_type, typename __alloc_traits::void_pointer> __node;
779 typedef typename __alloc_traits::template
780 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
783 rebind_alloc<__node>::other
786 typedef allocator_traits<__node_allocator> __node_traits;
787 typedef typename __node_traits::pointer __node_pointer;
788 typedef typename __node_traits::pointer __node_const_pointer;
789 typedef __hash_node_base<__node_pointer> __first_node;
790 typedef typename pointer_traits<__node_pointer>::template
791 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
794 rebind<__first_node>::other
800 typedef typename __node_traits::template
801 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
802 rebind_alloc<__node_pointer>
804 rebind_alloc<__node_pointer>::other
807 typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter;
808 typedef unique_ptr<__node_pointer[], __bucket_list_deleter> __bucket_list;
809 typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits;
810 typedef typename __bucket_list_deleter::pointer __node_pointer_pointer;
812 // --- Member data begin ---
813 __bucket_list __bucket_list_;
814 __compressed_pair<__first_node, __node_allocator> __p1_;
815 __compressed_pair<size_type, hasher> __p2_;
816 __compressed_pair<float, key_equal> __p3_;
817 // --- Member data end ---
819 _LIBCPP_INLINE_VISIBILITY
820 size_type& size() _NOEXCEPT {return __p2_.first();}
822 _LIBCPP_INLINE_VISIBILITY
823 size_type size() const _NOEXCEPT {return __p2_.first();}
825 _LIBCPP_INLINE_VISIBILITY
826 hasher& hash_function() _NOEXCEPT {return __p2_.second();}
827 _LIBCPP_INLINE_VISIBILITY
828 const hasher& hash_function() const _NOEXCEPT {return __p2_.second();}
830 _LIBCPP_INLINE_VISIBILITY
831 float& max_load_factor() _NOEXCEPT {return __p3_.first();}
832 _LIBCPP_INLINE_VISIBILITY
833 float max_load_factor() const _NOEXCEPT {return __p3_.first();}
835 _LIBCPP_INLINE_VISIBILITY
836 key_equal& key_eq() _NOEXCEPT {return __p3_.second();}
837 _LIBCPP_INLINE_VISIBILITY
838 const key_equal& key_eq() const _NOEXCEPT {return __p3_.second();}
840 _LIBCPP_INLINE_VISIBILITY
841 __node_allocator& __node_alloc() _NOEXCEPT {return __p1_.second();}
842 _LIBCPP_INLINE_VISIBILITY
843 const __node_allocator& __node_alloc() const _NOEXCEPT
844 {return __p1_.second();}
847 typedef __hash_iterator<__node_pointer> iterator;
848 typedef __hash_const_iterator<__node_pointer> const_iterator;
849 typedef __hash_local_iterator<__node_pointer> local_iterator;
850 typedef __hash_const_local_iterator<__node_pointer> const_local_iterator;
854 is_nothrow_default_constructible<__bucket_list>::value &&
855 is_nothrow_default_constructible<__first_node>::value &&
856 is_nothrow_default_constructible<__node_allocator>::value &&
857 is_nothrow_default_constructible<hasher>::value &&
858 is_nothrow_default_constructible<key_equal>::value);
859 __hash_table(const hasher& __hf, const key_equal& __eql);
860 __hash_table(const hasher& __hf, const key_equal& __eql,
861 const allocator_type& __a);
862 explicit __hash_table(const allocator_type& __a);
863 __hash_table(const __hash_table& __u);
864 __hash_table(const __hash_table& __u, const allocator_type& __a);
865 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
866 __hash_table(__hash_table&& __u)
868 is_nothrow_move_constructible<__bucket_list>::value &&
869 is_nothrow_move_constructible<__first_node>::value &&
870 is_nothrow_move_constructible<__node_allocator>::value &&
871 is_nothrow_move_constructible<hasher>::value &&
872 is_nothrow_move_constructible<key_equal>::value);
873 __hash_table(__hash_table&& __u, const allocator_type& __a);
874 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
877 __hash_table& operator=(const __hash_table& __u);
878 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
879 __hash_table& operator=(__hash_table&& __u)
881 __node_traits::propagate_on_container_move_assignment::value &&
882 is_nothrow_move_assignable<__node_allocator>::value &&
883 is_nothrow_move_assignable<hasher>::value &&
884 is_nothrow_move_assignable<key_equal>::value);
886 template <class _InputIterator>
887 void __assign_unique(_InputIterator __first, _InputIterator __last);
888 template <class _InputIterator>
889 void __assign_multi(_InputIterator __first, _InputIterator __last);
891 _LIBCPP_INLINE_VISIBILITY
892 size_type max_size() const _NOEXCEPT
894 return allocator_traits<__pointer_allocator>::max_size(
895 __bucket_list_.get_deleter().__alloc());
898 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
899 iterator __node_insert_multi(__node_pointer __nd);
900 iterator __node_insert_multi(const_iterator __p,
901 __node_pointer __nd);
903 #if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
904 template <class... _Args>
905 pair<iterator, bool> __emplace_unique(_Args&&... __args);
906 template <class... _Args>
907 iterator __emplace_multi(_Args&&... __args);
908 template <class... _Args>
909 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
910 #endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
912 pair<iterator, bool> __insert_unique(const value_type& __x);
914 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
916 pair<iterator, bool> __insert_unique(_Pp&& __x);
917 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
919 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
921 iterator __insert_multi(_Pp&& __x);
923 iterator __insert_multi(const_iterator __p, _Pp&& __x);
924 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
925 iterator __insert_multi(const value_type& __x);
926 iterator __insert_multi(const_iterator __p, const value_type& __x);
927 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
929 void clear() _NOEXCEPT;
930 void rehash(size_type __n);
931 _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n)
932 {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));}
934 _LIBCPP_INLINE_VISIBILITY
935 size_type bucket_count() const _NOEXCEPT
937 return __bucket_list_.get_deleter().size();
940 iterator begin() _NOEXCEPT;
941 iterator end() _NOEXCEPT;
942 const_iterator begin() const _NOEXCEPT;
943 const_iterator end() const _NOEXCEPT;
945 template <class _Key>
946 _LIBCPP_INLINE_VISIBILITY
947 size_type bucket(const _Key& __k) const
949 _LIBCPP_ASSERT(bucket_count() > 0,
950 "unordered container::bucket(key) called when bucket_count() == 0");
951 return __constrain_hash(hash_function()(__k), bucket_count());
954 template <class _Key>
955 iterator find(const _Key& __x);
956 template <class _Key>
957 const_iterator find(const _Key& __x) const;
959 typedef __hash_node_destructor<__node_allocator> _Dp;
960 typedef unique_ptr<__node, _Dp> __node_holder;
962 iterator erase(const_iterator __p);
963 iterator erase(const_iterator __first, const_iterator __last);
964 template <class _Key>
965 size_type __erase_unique(const _Key& __k);
966 template <class _Key>
967 size_type __erase_multi(const _Key& __k);
968 __node_holder remove(const_iterator __p) _NOEXCEPT;
970 template <class _Key>
971 size_type __count_unique(const _Key& __k) const;
972 template <class _Key>
973 size_type __count_multi(const _Key& __k) const;
975 template <class _Key>
976 pair<iterator, iterator>
977 __equal_range_unique(const _Key& __k);
978 template <class _Key>
979 pair<const_iterator, const_iterator>
980 __equal_range_unique(const _Key& __k) const;
982 template <class _Key>
983 pair<iterator, iterator>
984 __equal_range_multi(const _Key& __k);
985 template <class _Key>
986 pair<const_iterator, const_iterator>
987 __equal_range_multi(const _Key& __k) const;
989 void swap(__hash_table& __u)
991 (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value ||
992 __is_nothrow_swappable<__pointer_allocator>::value) &&
993 (!__node_traits::propagate_on_container_swap::value ||
994 __is_nothrow_swappable<__node_allocator>::value) &&
995 __is_nothrow_swappable<hasher>::value &&
996 __is_nothrow_swappable<key_equal>::value);
998 _LIBCPP_INLINE_VISIBILITY
999 size_type max_bucket_count() const _NOEXCEPT
1000 {return __pointer_alloc_traits::max_size(__bucket_list_.get_deleter().__alloc());}
1001 size_type bucket_size(size_type __n) const;
1002 _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT
1004 size_type __bc = bucket_count();
1005 return __bc != 0 ? (float)size() / __bc : 0.f;
1007 _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT
1009 _LIBCPP_ASSERT(__mlf > 0,
1010 "unordered container::max_load_factor(lf) called with lf <= 0");
1011 max_load_factor() = _VSTD::max(__mlf, load_factor());
1014 _LIBCPP_INLINE_VISIBILITY
1016 begin(size_type __n)
1018 _LIBCPP_ASSERT(__n < bucket_count(),
1019 "unordered container::begin(n) called with n >= bucket_count()");
1020 #if _LIBCPP_DEBUG_LEVEL >= 2
1021 return local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1023 return local_iterator(__bucket_list_[__n], __n, bucket_count());
1027 _LIBCPP_INLINE_VISIBILITY
1031 _LIBCPP_ASSERT(__n < bucket_count(),
1032 "unordered container::end(n) called with n >= bucket_count()");
1033 #if _LIBCPP_DEBUG_LEVEL >= 2
1034 return local_iterator(nullptr, __n, bucket_count(), this);
1036 return local_iterator(nullptr, __n, bucket_count());
1040 _LIBCPP_INLINE_VISIBILITY
1041 const_local_iterator
1042 cbegin(size_type __n) const
1044 _LIBCPP_ASSERT(__n < bucket_count(),
1045 "unordered container::cbegin(n) called with n >= bucket_count()");
1046 #if _LIBCPP_DEBUG_LEVEL >= 2
1047 return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1049 return const_local_iterator(__bucket_list_[__n], __n, bucket_count());
1053 _LIBCPP_INLINE_VISIBILITY
1054 const_local_iterator
1055 cend(size_type __n) const
1057 _LIBCPP_ASSERT(__n < bucket_count(),
1058 "unordered container::cend(n) called with n >= bucket_count()");
1059 #if _LIBCPP_DEBUG_LEVEL >= 2
1060 return const_local_iterator(nullptr, __n, bucket_count(), this);
1062 return const_local_iterator(nullptr, __n, bucket_count());
1066 #if _LIBCPP_DEBUG_LEVEL >= 2
1068 bool __dereferenceable(const const_iterator* __i) const;
1069 bool __decrementable(const const_iterator* __i) const;
1070 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1071 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1073 #endif // _LIBCPP_DEBUG_LEVEL >= 2
1076 void __rehash(size_type __n);
1078 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1079 #ifndef _LIBCPP_HAS_NO_VARIADICS
1080 template <class ..._Args>
1081 __node_holder __construct_node(_Args&& ...__args);
1082 #endif // _LIBCPP_HAS_NO_VARIADICS
1083 __node_holder __construct_node(value_type&& __v, size_t __hash);
1084 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1085 __node_holder __construct_node(const value_type& __v);
1087 __node_holder __construct_node(const value_type& __v, size_t __hash);
1089 _LIBCPP_INLINE_VISIBILITY
1090 void __copy_assign_alloc(const __hash_table& __u)
1091 {__copy_assign_alloc(__u, integral_constant<bool,
1092 __node_traits::propagate_on_container_copy_assignment::value>());}
1093 void __copy_assign_alloc(const __hash_table& __u, true_type);
1094 _LIBCPP_INLINE_VISIBILITY
1095 void __copy_assign_alloc(const __hash_table&, false_type) {}
1097 void __move_assign(__hash_table& __u, false_type);
1098 void __move_assign(__hash_table& __u, true_type)
1100 is_nothrow_move_assignable<__node_allocator>::value &&
1101 is_nothrow_move_assignable<hasher>::value &&
1102 is_nothrow_move_assignable<key_equal>::value);
1103 _LIBCPP_INLINE_VISIBILITY
1104 void __move_assign_alloc(__hash_table& __u)
1106 !__node_traits::propagate_on_container_move_assignment::value ||
1107 (is_nothrow_move_assignable<__pointer_allocator>::value &&
1108 is_nothrow_move_assignable<__node_allocator>::value))
1109 {__move_assign_alloc(__u, integral_constant<bool,
1110 __node_traits::propagate_on_container_move_assignment::value>());}
1111 _LIBCPP_INLINE_VISIBILITY
1112 void __move_assign_alloc(__hash_table& __u, true_type)
1114 is_nothrow_move_assignable<__pointer_allocator>::value &&
1115 is_nothrow_move_assignable<__node_allocator>::value)
1117 __bucket_list_.get_deleter().__alloc() =
1118 _VSTD::move(__u.__bucket_list_.get_deleter().__alloc());
1119 __node_alloc() = _VSTD::move(__u.__node_alloc());
1121 _LIBCPP_INLINE_VISIBILITY
1122 void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
1124 template <class _Ap>
1125 _LIBCPP_INLINE_VISIBILITY
1128 __swap_alloc(_Ap& __x, _Ap& __y)
1130 !allocator_traits<_Ap>::propagate_on_container_swap::value ||
1131 __is_nothrow_swappable<_Ap>::value)
1133 __swap_alloc(__x, __y,
1134 integral_constant<bool,
1135 allocator_traits<_Ap>::propagate_on_container_swap::value
1139 template <class _Ap>
1140 _LIBCPP_INLINE_VISIBILITY
1143 __swap_alloc(_Ap& __x, _Ap& __y, true_type)
1144 _NOEXCEPT_(__is_nothrow_swappable<_Ap>::value)
1150 template <class _Ap>
1151 _LIBCPP_INLINE_VISIBILITY
1154 __swap_alloc(_Ap&, _Ap&, false_type) _NOEXCEPT {}
1156 void __deallocate(__node_pointer __np) _NOEXCEPT;
1157 __node_pointer __detach() _NOEXCEPT;
1159 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
1160 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
1163 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1164 inline _LIBCPP_INLINE_VISIBILITY
1165 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table()
1167 is_nothrow_default_constructible<__bucket_list>::value &&
1168 is_nothrow_default_constructible<__first_node>::value &&
1169 is_nothrow_default_constructible<hasher>::value &&
1170 is_nothrow_default_constructible<key_equal>::value)
1176 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1177 inline _LIBCPP_INLINE_VISIBILITY
1178 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1179 const key_equal& __eql)
1180 : __bucket_list_(nullptr, __bucket_list_deleter()),
1187 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1188 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1189 const key_equal& __eql,
1190 const allocator_type& __a)
1191 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1192 __p1_(__node_allocator(__a)),
1198 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1199 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a)
1200 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1201 __p1_(__node_allocator(__a)),
1207 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1208 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u)
1209 : __bucket_list_(nullptr,
1210 __bucket_list_deleter(allocator_traits<__pointer_allocator>::
1211 select_on_container_copy_construction(
1212 __u.__bucket_list_.get_deleter().__alloc()), 0)),
1213 __p1_(allocator_traits<__node_allocator>::
1214 select_on_container_copy_construction(__u.__node_alloc())),
1215 __p2_(0, __u.hash_function()),
1220 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1221 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u,
1222 const allocator_type& __a)
1223 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1224 __p1_(__node_allocator(__a)),
1225 __p2_(0, __u.hash_function()),
1230 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1232 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1233 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u)
1235 is_nothrow_move_constructible<__bucket_list>::value &&
1236 is_nothrow_move_constructible<__first_node>::value &&
1237 is_nothrow_move_constructible<hasher>::value &&
1238 is_nothrow_move_constructible<key_equal>::value)
1239 : __bucket_list_(_VSTD::move(__u.__bucket_list_)),
1240 __p1_(_VSTD::move(__u.__p1_)),
1241 __p2_(_VSTD::move(__u.__p2_)),
1242 __p3_(_VSTD::move(__u.__p3_))
1246 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
1247 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
1248 __u.__p1_.first().__next_ = nullptr;
1253 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1254 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u,
1255 const allocator_type& __a)
1256 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1257 __p1_(__node_allocator(__a)),
1258 __p2_(0, _VSTD::move(__u.hash_function())),
1259 __p3_(_VSTD::move(__u.__p3_))
1261 if (__a == allocator_type(__u.__node_alloc()))
1263 __bucket_list_.reset(__u.__bucket_list_.release());
1264 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1265 __u.__bucket_list_.get_deleter().size() = 0;
1268 __p1_.first().__next_ = __u.__p1_.first().__next_;
1269 __u.__p1_.first().__next_ = nullptr;
1270 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
1271 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
1272 size() = __u.size();
1278 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1280 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1281 __hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table()
1283 __deallocate(__p1_.first().__next_);
1284 #if _LIBCPP_DEBUG_LEVEL >= 2
1285 __get_db()->__erase_c(this);
1289 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1291 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(
1292 const __hash_table& __u, true_type)
1294 if (__node_alloc() != __u.__node_alloc())
1297 __bucket_list_.reset();
1298 __bucket_list_.get_deleter().size() = 0;
1300 __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
1301 __node_alloc() = __u.__node_alloc();
1304 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1305 __hash_table<_Tp, _Hash, _Equal, _Alloc>&
1306 __hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u)
1310 __copy_assign_alloc(__u);
1311 hash_function() = __u.hash_function();
1312 key_eq() = __u.key_eq();
1313 max_load_factor() = __u.max_load_factor();
1314 __assign_multi(__u.begin(), __u.end());
1319 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1321 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate(__node_pointer __np)
1324 __node_allocator& __na = __node_alloc();
1325 while (__np != nullptr)
1327 __node_pointer __next = __np->__next_;
1328 #if _LIBCPP_DEBUG_LEVEL >= 2
1329 __c_node* __c = __get_db()->__find_c_and_lock(this);
1330 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1333 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1334 if (__i->__node_ == __np)
1336 (*__p)->__c_ = nullptr;
1337 if (--__c->end_ != __p)
1338 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1341 __get_db()->unlock();
1343 __node_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1344 __node_traits::deallocate(__na, __np, 1);
1349 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1350 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_pointer
1351 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
1353 size_type __bc = bucket_count();
1354 for (size_type __i = 0; __i < __bc; ++__i)
1355 __bucket_list_[__i] = nullptr;
1357 __node_pointer __cache = __p1_.first().__next_;
1358 __p1_.first().__next_ = nullptr;
1362 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1364 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1366 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1367 __hash_table& __u, true_type)
1369 is_nothrow_move_assignable<__node_allocator>::value &&
1370 is_nothrow_move_assignable<hasher>::value &&
1371 is_nothrow_move_assignable<key_equal>::value)
1374 __bucket_list_.reset(__u.__bucket_list_.release());
1375 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1376 __u.__bucket_list_.get_deleter().size() = 0;
1377 __move_assign_alloc(__u);
1378 size() = __u.size();
1379 hash_function() = _VSTD::move(__u.hash_function());
1380 max_load_factor() = __u.max_load_factor();
1381 key_eq() = _VSTD::move(__u.key_eq());
1382 __p1_.first().__next_ = __u.__p1_.first().__next_;
1385 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
1386 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
1387 __u.__p1_.first().__next_ = nullptr;
1390 #if _LIBCPP_DEBUG_LEVEL >= 2
1391 __get_db()->swap(this, &__u);
1395 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1397 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1398 __hash_table& __u, false_type)
1400 if (__node_alloc() == __u.__node_alloc())
1401 __move_assign(__u, true_type());
1404 hash_function() = _VSTD::move(__u.hash_function());
1405 key_eq() = _VSTD::move(__u.key_eq());
1406 max_load_factor() = __u.max_load_factor();
1407 if (bucket_count() != 0)
1409 __node_pointer __cache = __detach();
1410 #ifndef _LIBCPP_NO_EXCEPTIONS
1413 #endif // _LIBCPP_NO_EXCEPTIONS
1414 const_iterator __i = __u.begin();
1415 while (__cache != nullptr && __u.size() != 0)
1417 __cache->__value_ = _VSTD::move(__u.remove(__i++)->__value_);
1418 __node_pointer __next = __cache->__next_;
1419 __node_insert_multi(__cache);
1422 #ifndef _LIBCPP_NO_EXCEPTIONS
1426 __deallocate(__cache);
1429 #endif // _LIBCPP_NO_EXCEPTIONS
1430 __deallocate(__cache);
1432 const_iterator __i = __u.begin();
1433 while (__u.size() != 0)
1436 __construct_node(_VSTD::move(__u.remove(__i++)->__value_));
1437 __node_insert_multi(__h.get());
1443 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1444 inline _LIBCPP_INLINE_VISIBILITY
1445 __hash_table<_Tp, _Hash, _Equal, _Alloc>&
1446 __hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
1448 __node_traits::propagate_on_container_move_assignment::value &&
1449 is_nothrow_move_assignable<__node_allocator>::value &&
1450 is_nothrow_move_assignable<hasher>::value &&
1451 is_nothrow_move_assignable<key_equal>::value)
1453 __move_assign(__u, integral_constant<bool,
1454 __node_traits::propagate_on_container_move_assignment::value>());
1458 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1460 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1461 template <class _InputIterator>
1463 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first,
1464 _InputIterator __last)
1466 if (bucket_count() != 0)
1468 __node_pointer __cache = __detach();
1469 #ifndef _LIBCPP_NO_EXCEPTIONS
1472 #endif // _LIBCPP_NO_EXCEPTIONS
1473 for (; __cache != nullptr && __first != __last; ++__first)
1475 __cache->__value_ = *__first;
1476 __node_pointer __next = __cache->__next_;
1477 __node_insert_unique(__cache);
1480 #ifndef _LIBCPP_NO_EXCEPTIONS
1484 __deallocate(__cache);
1487 #endif // _LIBCPP_NO_EXCEPTIONS
1488 __deallocate(__cache);
1490 for (; __first != __last; ++__first)
1491 __insert_unique(*__first);
1494 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1495 template <class _InputIterator>
1497 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
1498 _InputIterator __last)
1500 if (bucket_count() != 0)
1502 __node_pointer __cache = __detach();
1503 #ifndef _LIBCPP_NO_EXCEPTIONS
1506 #endif // _LIBCPP_NO_EXCEPTIONS
1507 for (; __cache != nullptr && __first != __last; ++__first)
1509 __cache->__value_ = *__first;
1510 __node_pointer __next = __cache->__next_;
1511 __node_insert_multi(__cache);
1514 #ifndef _LIBCPP_NO_EXCEPTIONS
1518 __deallocate(__cache);
1521 #endif // _LIBCPP_NO_EXCEPTIONS
1522 __deallocate(__cache);
1524 for (; __first != __last; ++__first)
1525 __insert_multi(*__first);
1528 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1529 inline _LIBCPP_INLINE_VISIBILITY
1530 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1531 __hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT
1533 #if _LIBCPP_DEBUG_LEVEL >= 2
1534 return iterator(__p1_.first().__next_, this);
1536 return iterator(__p1_.first().__next_);
1540 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1541 inline _LIBCPP_INLINE_VISIBILITY
1542 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1543 __hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT
1545 #if _LIBCPP_DEBUG_LEVEL >= 2
1546 return iterator(nullptr, this);
1548 return iterator(nullptr);
1552 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1553 inline _LIBCPP_INLINE_VISIBILITY
1554 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
1555 __hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT
1557 #if _LIBCPP_DEBUG_LEVEL >= 2
1558 return const_iterator(__p1_.first().__next_, this);
1560 return const_iterator(__p1_.first().__next_);
1564 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1565 inline _LIBCPP_INLINE_VISIBILITY
1566 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
1567 __hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT
1569 #if _LIBCPP_DEBUG_LEVEL >= 2
1570 return const_iterator(nullptr, this);
1572 return const_iterator(nullptr);
1576 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1578 __hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT
1582 __deallocate(__p1_.first().__next_);
1583 __p1_.first().__next_ = nullptr;
1584 size_type __bc = bucket_count();
1585 for (size_type __i = 0; __i < __bc; ++__i)
1586 __bucket_list_[__i] = nullptr;
1591 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1592 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1593 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd)
1595 __nd->__hash_ = hash_function()(__nd->__value_);
1596 size_type __bc = bucket_count();
1597 bool __inserted = false;
1598 __node_pointer __ndptr;
1602 __chash = __constrain_hash(__nd->__hash_, __bc);
1603 __ndptr = __bucket_list_[__chash];
1604 if (__ndptr != nullptr)
1606 for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
1607 __constrain_hash(__ndptr->__hash_, __bc) == __chash;
1608 __ndptr = __ndptr->__next_)
1610 if (key_eq()(__ndptr->__value_, __nd->__value_))
1616 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1618 rehash(_VSTD::max<size_type>(2 * __bc + !__is_power2(__bc),
1619 size_type(ceil(float(size() + 1) / max_load_factor()))));
1620 __bc = bucket_count();
1621 __chash = __constrain_hash(__nd->__hash_, __bc);
1623 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
1624 __node_pointer __pn = __bucket_list_[__chash];
1625 if (__pn == nullptr)
1627 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
1628 __nd->__next_ = __pn->__next_;
1629 __pn->__next_ = __nd;
1630 // fix up __bucket_list_
1631 __bucket_list_[__chash] = __pn;
1632 if (__nd->__next_ != nullptr)
1633 __bucket_list_[__constrain_hash(__nd->__next_->__hash_, __bc)] = __nd;
1637 __nd->__next_ = __pn->__next_;
1638 __pn->__next_ = __nd;
1646 #if _LIBCPP_DEBUG_LEVEL >= 2
1647 return pair<iterator, bool>(iterator(__ndptr, this), __inserted);
1649 return pair<iterator, bool>(iterator(__ndptr), __inserted);
1653 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1654 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1655 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp)
1657 __cp->__hash_ = hash_function()(__cp->__value_);
1658 size_type __bc = bucket_count();
1659 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1661 rehash(_VSTD::max<size_type>(2 * __bc + !__is_power2(__bc),
1662 size_type(ceil(float(size() + 1) / max_load_factor()))));
1663 __bc = bucket_count();
1665 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
1666 __node_pointer __pn = __bucket_list_[__chash];
1667 if (__pn == nullptr)
1669 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
1670 __cp->__next_ = __pn->__next_;
1671 __pn->__next_ = __cp;
1672 // fix up __bucket_list_
1673 __bucket_list_[__chash] = __pn;
1674 if (__cp->__next_ != nullptr)
1675 __bucket_list_[__constrain_hash(__cp->__next_->__hash_, __bc)] = __cp;
1679 for (bool __found = false; __pn->__next_ != nullptr &&
1680 __constrain_hash(__pn->__next_->__hash_, __bc) == __chash;
1681 __pn = __pn->__next_)
1683 // __found key_eq() action
1686 // false true set __found to true
1688 if (__found != (__pn->__next_->__hash_ == __cp->__hash_ &&
1689 key_eq()(__pn->__next_->__value_, __cp->__value_)))
1697 __cp->__next_ = __pn->__next_;
1698 __pn->__next_ = __cp;
1699 if (__cp->__next_ != nullptr)
1701 size_t __nhash = __constrain_hash(__cp->__next_->__hash_, __bc);
1702 if (__nhash != __chash)
1703 __bucket_list_[__nhash] = __cp;
1707 #if _LIBCPP_DEBUG_LEVEL >= 2
1708 return iterator(__cp, this);
1710 return iterator(__cp);
1714 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1715 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1716 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(
1717 const_iterator __p, __node_pointer __cp)
1719 #if _LIBCPP_DEBUG_LEVEL >= 2
1720 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1721 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
1722 " referring to this unordered container");
1724 if (__p != end() && key_eq()(*__p, __cp->__value_))
1726 __node_pointer __np = __p.__node_;
1727 __cp->__hash_ = __np->__hash_;
1728 size_type __bc = bucket_count();
1729 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1731 rehash(_VSTD::max<size_type>(2 * __bc + !__is_power2(__bc),
1732 size_type(ceil(float(size() + 1) / max_load_factor()))));
1733 __bc = bucket_count();
1735 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
1736 __node_pointer __pp = __bucket_list_[__chash];
1737 while (__pp->__next_ != __np)
1738 __pp = __pp->__next_;
1739 __cp->__next_ = __np;
1740 __pp->__next_ = __cp;
1742 #if _LIBCPP_DEBUG_LEVEL >= 2
1743 return iterator(__cp, this);
1745 return iterator(__cp);
1748 return __node_insert_multi(__cp);
1751 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1752 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1753 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique(const value_type& __x)
1755 size_t __hash = hash_function()(__x);
1756 size_type __bc = bucket_count();
1757 bool __inserted = false;
1758 __node_pointer __nd;
1762 __chash = __constrain_hash(__hash, __bc);
1763 __nd = __bucket_list_[__chash];
1764 if (__nd != nullptr)
1766 for (__nd = __nd->__next_; __nd != nullptr &&
1767 __constrain_hash(__nd->__hash_, __bc) == __chash;
1768 __nd = __nd->__next_)
1770 if (key_eq()(__nd->__value_, __x))
1776 __node_holder __h = __construct_node(__x, __hash);
1777 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1779 rehash(_VSTD::max<size_type>(2 * __bc + !__is_power2(__bc),
1780 size_type(ceil(float(size() + 1) / max_load_factor()))));
1781 __bc = bucket_count();
1782 __chash = __constrain_hash(__hash, __bc);
1784 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
1785 __node_pointer __pn = __bucket_list_[__chash];
1786 if (__pn == nullptr)
1788 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
1789 __h->__next_ = __pn->__next_;
1790 __pn->__next_ = __h.get();
1791 // fix up __bucket_list_
1792 __bucket_list_[__chash] = __pn;
1793 if (__h->__next_ != nullptr)
1794 __bucket_list_[__constrain_hash(__h->__next_->__hash_, __bc)] = __h.get();
1798 __h->__next_ = __pn->__next_;
1799 __pn->__next_ = __h.get();
1801 __nd = __h.release();
1807 #if _LIBCPP_DEBUG_LEVEL >= 2
1808 return pair<iterator, bool>(iterator(__nd, this), __inserted);
1810 return pair<iterator, bool>(iterator(__nd), __inserted);
1814 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1815 #ifndef _LIBCPP_HAS_NO_VARIADICS
1817 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1818 template <class... _Args>
1819 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1820 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique(_Args&&... __args)
1822 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
1823 pair<iterator, bool> __r = __node_insert_unique(__h.get());
1829 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1830 template <class... _Args>
1831 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1832 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args)
1834 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
1835 iterator __r = __node_insert_multi(__h.get());
1840 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1841 template <class... _Args>
1842 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1843 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(
1844 const_iterator __p, _Args&&... __args)
1846 #if _LIBCPP_DEBUG_LEVEL >= 2
1847 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1848 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
1849 " referring to this unordered container");
1851 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
1852 iterator __r = __node_insert_multi(__p, __h.get());
1857 #endif // _LIBCPP_HAS_NO_VARIADICS
1859 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1860 template <class _Pp>
1861 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1862 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique(_Pp&& __x)
1864 __node_holder __h = __construct_node(_VSTD::forward<_Pp>(__x));
1865 pair<iterator, bool> __r = __node_insert_unique(__h.get());
1871 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1873 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1875 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1876 template <class _Pp>
1877 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1878 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(_Pp&& __x)
1880 __node_holder __h = __construct_node(_VSTD::forward<_Pp>(__x));
1881 iterator __r = __node_insert_multi(__h.get());
1886 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1887 template <class _Pp>
1888 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1889 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
1892 #if _LIBCPP_DEBUG_LEVEL >= 2
1893 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1894 "unordered container::insert(const_iterator, rvalue) called with an iterator not"
1895 " referring to this unordered container");
1897 __node_holder __h = __construct_node(_VSTD::forward<_Pp>(__x));
1898 iterator __r = __node_insert_multi(__p, __h.get());
1903 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1905 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1906 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1907 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const value_type& __x)
1909 __node_holder __h = __construct_node(__x);
1910 iterator __r = __node_insert_multi(__h.get());
1915 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1916 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1917 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
1918 const value_type& __x)
1920 #if _LIBCPP_DEBUG_LEVEL >= 2
1921 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1922 "unordered container::insert(const_iterator, lvalue) called with an iterator not"
1923 " referring to this unordered container");
1925 __node_holder __h = __construct_node(__x);
1926 iterator __r = __node_insert_multi(__p, __h.get());
1931 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1933 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1935 __hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
1939 else if (__n & (__n - 1))
1940 __n = __next_prime(__n);
1941 size_type __bc = bucket_count();
1944 else if (__n < __bc)
1946 __n = _VSTD::max<size_type>
1949 __is_power2(__bc) ? __next_pow2(size_t(ceil(float(size()) / max_load_factor()))) :
1950 __next_prime(size_t(ceil(float(size()) / max_load_factor())))
1957 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1959 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc)
1961 #if _LIBCPP_DEBUG_LEVEL >= 2
1962 __get_db()->__invalidate_all(this);
1963 #endif // _LIBCPP_DEBUG_LEVEL >= 2
1964 __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
1965 __bucket_list_.reset(__nbc > 0 ?
1966 __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
1967 __bucket_list_.get_deleter().size() = __nbc;
1970 for (size_type __i = 0; __i < __nbc; ++__i)
1971 __bucket_list_[__i] = nullptr;
1972 __node_pointer __pp(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())));
1973 __node_pointer __cp = __pp->__next_;
1974 if (__cp != nullptr)
1976 size_type __chash = __constrain_hash(__cp->__hash_, __nbc);
1977 __bucket_list_[__chash] = __pp;
1978 size_type __phash = __chash;
1979 for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr;
1980 __cp = __pp->__next_)
1982 __chash = __constrain_hash(__cp->__hash_, __nbc);
1983 if (__chash == __phash)
1987 if (__bucket_list_[__chash] == nullptr)
1989 __bucket_list_[__chash] = __pp;
1995 __node_pointer __np = __cp;
1996 for (; __np->__next_ != nullptr &&
1997 key_eq()(__cp->__value_, __np->__next_->__value_);
1998 __np = __np->__next_)
2000 __pp->__next_ = __np->__next_;
2001 __np->__next_ = __bucket_list_[__chash]->__next_;
2002 __bucket_list_[__chash]->__next_ = __cp;
2011 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2012 template <class _Key>
2013 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2014 __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
2016 size_t __hash = hash_function()(__k);
2017 size_type __bc = bucket_count();
2020 size_t __chash = __constrain_hash(__hash, __bc);
2021 __node_pointer __nd = __bucket_list_[__chash];
2022 if (__nd != nullptr)
2024 for (__nd = __nd->__next_; __nd != nullptr &&
2025 __constrain_hash(__nd->__hash_, __bc) == __chash;
2026 __nd = __nd->__next_)
2028 if (key_eq()(__nd->__value_, __k))
2029 #if _LIBCPP_DEBUG_LEVEL >= 2
2030 return iterator(__nd, this);
2032 return iterator(__nd);
2040 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2041 template <class _Key>
2042 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
2043 __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
2045 size_t __hash = hash_function()(__k);
2046 size_type __bc = bucket_count();
2049 size_t __chash = __constrain_hash(__hash, __bc);
2050 __node_const_pointer __nd = __bucket_list_[__chash];
2051 if (__nd != nullptr)
2053 for (__nd = __nd->__next_; __nd != nullptr &&
2054 __constrain_hash(__nd->__hash_, __bc) == __chash;
2055 __nd = __nd->__next_)
2057 if (key_eq()(__nd->__value_, __k))
2058 #if _LIBCPP_DEBUG_LEVEL >= 2
2059 return const_iterator(__nd, this);
2061 return const_iterator(__nd);
2070 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2071 #ifndef _LIBCPP_HAS_NO_VARIADICS
2073 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2074 template <class ..._Args>
2075 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2076 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args)
2078 __node_allocator& __na = __node_alloc();
2079 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2080 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), _VSTD::forward<_Args>(__args)...);
2081 __h.get_deleter().__value_constructed = true;
2082 __h->__hash_ = hash_function()(__h->__value_);
2083 __h->__next_ = nullptr;
2087 #endif // _LIBCPP_HAS_NO_VARIADICS
2089 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2090 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2091 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(value_type&& __v,
2094 __node_allocator& __na = __node_alloc();
2095 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2096 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), _VSTD::move(__v));
2097 __h.get_deleter().__value_constructed = true;
2098 __h->__hash_ = __hash;
2099 __h->__next_ = nullptr;
2103 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2105 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2106 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2107 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const value_type& __v)
2109 __node_allocator& __na = __node_alloc();
2110 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2111 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), __v);
2112 __h.get_deleter().__value_constructed = true;
2113 __h->__hash_ = hash_function()(__h->__value_);
2114 __h->__next_ = nullptr;
2115 return _VSTD::move(__h); // explicitly moved for C++03
2118 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2120 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2121 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2122 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const value_type& __v,
2125 __node_allocator& __na = __node_alloc();
2126 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2127 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), __v);
2128 __h.get_deleter().__value_constructed = true;
2129 __h->__hash_ = __hash;
2130 __h->__next_ = nullptr;
2131 return _VSTD::move(__h); // explicitly moved for C++03
2134 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2135 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2136 __hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p)
2138 __node_pointer __np = __p.__node_;
2139 #if _LIBCPP_DEBUG_LEVEL >= 2
2140 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2141 "unordered container erase(iterator) called with an iterator not"
2142 " referring to this container");
2143 _LIBCPP_ASSERT(__p != end(),
2144 "unordered container erase(iterator) called with a non-dereferenceable iterator");
2145 iterator __r(__np, this);
2154 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2155 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2156 __hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first,
2157 const_iterator __last)
2159 #if _LIBCPP_DEBUG_LEVEL >= 2
2160 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2161 "unodered container::erase(iterator, iterator) called with an iterator not"
2162 " referring to this unodered container");
2163 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
2164 "unodered container::erase(iterator, iterator) called with an iterator not"
2165 " referring to this unodered container");
2167 for (const_iterator __p = __first; __first != __last; __p = __first)
2172 __node_pointer __np = __last.__node_;
2173 #if _LIBCPP_DEBUG_LEVEL >= 2
2174 return iterator (__np, this);
2176 return iterator (__np);
2180 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2181 template <class _Key>
2182 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2183 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k)
2185 iterator __i = find(__k);
2192 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2193 template <class _Key>
2194 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2195 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k)
2198 iterator __i = find(__k);
2201 iterator __e = end();
2206 } while (__i != __e && key_eq()(*__i, __k));
2211 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2212 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2213 __hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT
2216 __node_pointer __cn = __p.__node_;
2217 size_type __bc = bucket_count();
2218 size_t __chash = __constrain_hash(__cn->__hash_, __bc);
2219 // find previous node
2220 __node_pointer __pn = __bucket_list_[__chash];
2221 for (; __pn->__next_ != __cn; __pn = __pn->__next_)
2223 // Fix up __bucket_list_
2224 // if __pn is not in same bucket (before begin is not in same bucket) &&
2225 // if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
2226 if (__pn == static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()))
2227 || __constrain_hash(__pn->__hash_, __bc) != __chash)
2229 if (__cn->__next_ == nullptr || __constrain_hash(__cn->__next_->__hash_, __bc) != __chash)
2230 __bucket_list_[__chash] = nullptr;
2232 // if __cn->__next_ is not in same bucket (nullptr is in same bucket)
2233 if (__cn->__next_ != nullptr)
2235 size_t __nhash = __constrain_hash(__cn->__next_->__hash_, __bc);
2236 if (__nhash != __chash)
2237 __bucket_list_[__nhash] = __pn;
2240 __pn->__next_ = __cn->__next_;
2241 __cn->__next_ = nullptr;
2243 #if _LIBCPP_DEBUG_LEVEL >= 2
2244 __c_node* __c = __get_db()->__find_c_and_lock(this);
2245 for (__i_node** __p = __c->end_; __p != __c->beg_; )
2248 iterator* __i = static_cast<iterator*>((*__p)->__i_);
2249 if (__i->__node_ == __cn)
2251 (*__p)->__c_ = nullptr;
2252 if (--__c->end_ != __p)
2253 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2256 __get_db()->unlock();
2258 return __node_holder(__cn, _Dp(__node_alloc(), true));
2261 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2262 template <class _Key>
2263 inline _LIBCPP_INLINE_VISIBILITY
2264 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2265 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const
2267 return static_cast<size_type>(find(__k) != end());
2270 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2271 template <class _Key>
2272 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2273 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const
2276 const_iterator __i = find(__k);
2279 const_iterator __e = end();
2284 } while (__i != __e && key_eq()(*__i, __k));
2289 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2290 template <class _Key>
2291 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2292 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2293 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2296 iterator __i = find(__k);
2300 return pair<iterator, iterator>(__i, __j);
2303 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2304 template <class _Key>
2305 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2306 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2307 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2308 const _Key& __k) const
2310 const_iterator __i = find(__k);
2311 const_iterator __j = __i;
2314 return pair<const_iterator, const_iterator>(__i, __j);
2317 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2318 template <class _Key>
2319 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2320 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2321 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2324 iterator __i = find(__k);
2328 iterator __e = end();
2332 } while (__j != __e && key_eq()(*__j, __k));
2334 return pair<iterator, iterator>(__i, __j);
2337 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2338 template <class _Key>
2339 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2340 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2341 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2342 const _Key& __k) const
2344 const_iterator __i = find(__k);
2345 const_iterator __j = __i;
2348 const_iterator __e = end();
2352 } while (__j != __e && key_eq()(*__j, __k));
2354 return pair<const_iterator, const_iterator>(__i, __j);
2357 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2359 __hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
2361 (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value ||
2362 __is_nothrow_swappable<__pointer_allocator>::value) &&
2363 (!__node_traits::propagate_on_container_swap::value ||
2364 __is_nothrow_swappable<__node_allocator>::value) &&
2365 __is_nothrow_swappable<hasher>::value &&
2366 __is_nothrow_swappable<key_equal>::value)
2369 __node_pointer_pointer __npp = __bucket_list_.release();
2370 __bucket_list_.reset(__u.__bucket_list_.release());
2371 __u.__bucket_list_.reset(__npp);
2373 _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
2374 __swap_alloc(__bucket_list_.get_deleter().__alloc(),
2375 __u.__bucket_list_.get_deleter().__alloc());
2376 __swap_alloc(__node_alloc(), __u.__node_alloc());
2377 _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
2378 __p2_.swap(__u.__p2_);
2379 __p3_.swap(__u.__p3_);
2381 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
2382 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
2384 __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash_, __u.bucket_count())] =
2385 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__u.__p1_.first()));
2386 #if _LIBCPP_DEBUG_LEVEL >= 2
2387 __get_db()->swap(this, &__u);
2391 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2392 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2393 __hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const
2395 _LIBCPP_ASSERT(__n < bucket_count(),
2396 "unordered container::bucket_size(n) called with n >= bucket_count()");
2397 __node_const_pointer __np = __bucket_list_[__n];
2398 size_type __bc = bucket_count();
2400 if (__np != nullptr)
2402 for (__np = __np->__next_; __np != nullptr &&
2403 __constrain_hash(__np->__hash_, __bc) == __n;
2404 __np = __np->__next_, ++__r)
2410 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2411 inline _LIBCPP_INLINE_VISIBILITY
2413 swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x,
2414 __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
2415 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2420 #if _LIBCPP_DEBUG_LEVEL >= 2
2422 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2424 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const
2426 return __i->__node_ != nullptr;
2429 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2431 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const
2436 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2438 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2443 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2445 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2450 #endif // _LIBCPP_DEBUG_LEVEL >= 2
2451 _LIBCPP_END_NAMESPACE_STD
2453 #endif // _LIBCPP__HASH_TABLE