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>
22 #include <__undef___deallocate>
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 #pragma GCC system_header
30 _LIBCPP_BEGIN_NAMESPACE_STD
33 size_t __next_prime(size_t __n);
35 template <class _NodePtr>
36 struct __hash_node_base
38 typedef __hash_node_base __first_node;
42 _LIBCPP_INLINE_VISIBILITY __hash_node_base() _NOEXCEPT : __next_(nullptr) {}
45 template <class _Tp, class _VoidPtr>
47 : public __hash_node_base
49 typename __rebind_pointer<_VoidPtr, __hash_node<_Tp, _VoidPtr> >::type
52 typedef _Tp value_type;
58 inline _LIBCPP_INLINE_VISIBILITY
60 __is_hash_power2(size_t __bc)
62 return __bc > 2 && !(__bc & (__bc - 1));
65 inline _LIBCPP_INLINE_VISIBILITY
67 __constrain_hash(size_t __h, size_t __bc)
69 return !(__bc & (__bc - 1)) ? __h & (__bc - 1) : __h % __bc;
72 inline _LIBCPP_INLINE_VISIBILITY
74 __next_hash_pow2(size_t __n)
76 return size_t(1) << (std::numeric_limits<size_t>::digits - __clz(__n-1));
79 template <class _Tp, class _Hash, class _Equal, class _Alloc> class __hash_table;
80 template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator;
81 template <class _HashIterator> class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
82 template <class _HashIterator> class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
84 template <class _NodePtr>
85 class _LIBCPP_TYPE_VIS_ONLY __hash_iterator
87 typedef _NodePtr __node_pointer;
89 __node_pointer __node_;
92 typedef forward_iterator_tag iterator_category;
93 typedef typename pointer_traits<__node_pointer>::element_type::value_type value_type;
94 typedef typename pointer_traits<__node_pointer>::difference_type difference_type;
95 typedef value_type& reference;
96 typedef typename __rebind_pointer<__node_pointer, value_type>::type pointer;
98 _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT
99 #if _LIBCPP_STD_VER > 11
103 #if _LIBCPP_DEBUG_LEVEL >= 2
104 __get_db()->__insert_i(this);
108 #if _LIBCPP_DEBUG_LEVEL >= 2
110 _LIBCPP_INLINE_VISIBILITY
111 __hash_iterator(const __hash_iterator& __i)
112 : __node_(__i.__node_)
114 __get_db()->__iterator_copy(this, &__i);
117 _LIBCPP_INLINE_VISIBILITY
120 __get_db()->__erase_i(this);
123 _LIBCPP_INLINE_VISIBILITY
124 __hash_iterator& operator=(const __hash_iterator& __i)
128 __get_db()->__iterator_copy(this, &__i);
129 __node_ = __i.__node_;
134 #endif // _LIBCPP_DEBUG_LEVEL >= 2
136 _LIBCPP_INLINE_VISIBILITY
137 reference operator*() const
139 #if _LIBCPP_DEBUG_LEVEL >= 2
140 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
141 "Attempted to dereference a non-dereferenceable unordered container iterator");
143 return __node_->__value_;
145 _LIBCPP_INLINE_VISIBILITY
146 pointer operator->() const
148 #if _LIBCPP_DEBUG_LEVEL >= 2
149 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
150 "Attempted to dereference a non-dereferenceable unordered container iterator");
152 return pointer_traits<pointer>::pointer_to(__node_->__value_);
155 _LIBCPP_INLINE_VISIBILITY
156 __hash_iterator& operator++()
158 #if _LIBCPP_DEBUG_LEVEL >= 2
159 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
160 "Attempted to increment non-incrementable unordered container iterator");
162 __node_ = __node_->__next_;
166 _LIBCPP_INLINE_VISIBILITY
167 __hash_iterator operator++(int)
169 __hash_iterator __t(*this);
174 friend _LIBCPP_INLINE_VISIBILITY
175 bool operator==(const __hash_iterator& __x, const __hash_iterator& __y)
177 return __x.__node_ == __y.__node_;
179 friend _LIBCPP_INLINE_VISIBILITY
180 bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y)
181 {return !(__x == __y);}
184 #if _LIBCPP_DEBUG_LEVEL >= 2
185 _LIBCPP_INLINE_VISIBILITY
186 __hash_iterator(__node_pointer __node, const void* __c) _NOEXCEPT
189 __get_db()->__insert_ic(this, __c);
192 _LIBCPP_INLINE_VISIBILITY
193 __hash_iterator(__node_pointer __node) _NOEXCEPT
198 template <class, class, class, class> friend class __hash_table;
199 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator;
200 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
201 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
202 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
205 template <class _ConstNodePtr>
206 class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator
208 typedef _ConstNodePtr __node_pointer;
210 __node_pointer __node_;
212 typedef typename remove_const<
213 typename pointer_traits<__node_pointer>::element_type
217 typedef forward_iterator_tag iterator_category;
218 typedef typename __node::value_type value_type;
219 typedef typename pointer_traits<__node_pointer>::difference_type difference_type;
220 typedef const value_type& reference;
221 typedef typename __rebind_pointer<__node_pointer, const value_type>::type pointer;
222 typedef typename __rebind_pointer<__node_pointer, __node>::type __non_const_node_pointer;
223 typedef __hash_iterator<__non_const_node_pointer> __non_const_iterator;
225 _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT
226 #if _LIBCPP_STD_VER > 11
230 #if _LIBCPP_DEBUG_LEVEL >= 2
231 __get_db()->__insert_i(this);
234 _LIBCPP_INLINE_VISIBILITY
235 __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT
236 : __node_(__x.__node_)
238 #if _LIBCPP_DEBUG_LEVEL >= 2
239 __get_db()->__iterator_copy(this, &__x);
243 #if _LIBCPP_DEBUG_LEVEL >= 2
245 _LIBCPP_INLINE_VISIBILITY
246 __hash_const_iterator(const __hash_const_iterator& __i)
247 : __node_(__i.__node_)
249 __get_db()->__iterator_copy(this, &__i);
252 _LIBCPP_INLINE_VISIBILITY
253 ~__hash_const_iterator()
255 __get_db()->__erase_i(this);
258 _LIBCPP_INLINE_VISIBILITY
259 __hash_const_iterator& operator=(const __hash_const_iterator& __i)
263 __get_db()->__iterator_copy(this, &__i);
264 __node_ = __i.__node_;
269 #endif // _LIBCPP_DEBUG_LEVEL >= 2
271 _LIBCPP_INLINE_VISIBILITY
272 reference operator*() const
274 #if _LIBCPP_DEBUG_LEVEL >= 2
275 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
276 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
278 return __node_->__value_;
280 _LIBCPP_INLINE_VISIBILITY
281 pointer operator->() const
283 #if _LIBCPP_DEBUG_LEVEL >= 2
284 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
285 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
287 return pointer_traits<pointer>::pointer_to(__node_->__value_);
290 _LIBCPP_INLINE_VISIBILITY
291 __hash_const_iterator& operator++()
293 #if _LIBCPP_DEBUG_LEVEL >= 2
294 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
295 "Attempted to increment non-incrementable unordered container const_iterator");
297 __node_ = __node_->__next_;
301 _LIBCPP_INLINE_VISIBILITY
302 __hash_const_iterator operator++(int)
304 __hash_const_iterator __t(*this);
309 friend _LIBCPP_INLINE_VISIBILITY
310 bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
312 return __x.__node_ == __y.__node_;
314 friend _LIBCPP_INLINE_VISIBILITY
315 bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
316 {return !(__x == __y);}
319 #if _LIBCPP_DEBUG_LEVEL >= 2
320 _LIBCPP_INLINE_VISIBILITY
321 __hash_const_iterator(__node_pointer __node, const void* __c) _NOEXCEPT
324 __get_db()->__insert_ic(this, __c);
327 _LIBCPP_INLINE_VISIBILITY
328 __hash_const_iterator(__node_pointer __node) _NOEXCEPT
333 template <class, class, class, class> friend class __hash_table;
334 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
335 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
336 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
339 template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
341 template <class _NodePtr>
342 class _LIBCPP_TYPE_VIS_ONLY __hash_local_iterator
344 typedef _NodePtr __node_pointer;
346 __node_pointer __node_;
348 size_t __bucket_count_;
350 typedef pointer_traits<__node_pointer> __pointer_traits;
352 typedef forward_iterator_tag iterator_category;
353 typedef typename __pointer_traits::element_type::value_type value_type;
354 typedef typename __pointer_traits::difference_type difference_type;
355 typedef value_type& reference;
356 typedef typename __rebind_pointer<__node_pointer, value_type>::type pointer;
358 _LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT
360 #if _LIBCPP_DEBUG_LEVEL >= 2
361 __get_db()->__insert_i(this);
365 #if _LIBCPP_DEBUG_LEVEL >= 2
367 _LIBCPP_INLINE_VISIBILITY
368 __hash_local_iterator(const __hash_local_iterator& __i)
369 : __node_(__i.__node_),
370 __bucket_(__i.__bucket_),
371 __bucket_count_(__i.__bucket_count_)
373 __get_db()->__iterator_copy(this, &__i);
376 _LIBCPP_INLINE_VISIBILITY
377 ~__hash_local_iterator()
379 __get_db()->__erase_i(this);
382 _LIBCPP_INLINE_VISIBILITY
383 __hash_local_iterator& operator=(const __hash_local_iterator& __i)
387 __get_db()->__iterator_copy(this, &__i);
388 __node_ = __i.__node_;
389 __bucket_ = __i.__bucket_;
390 __bucket_count_ = __i.__bucket_count_;
395 #endif // _LIBCPP_DEBUG_LEVEL >= 2
397 _LIBCPP_INLINE_VISIBILITY
398 reference operator*() const
400 #if _LIBCPP_DEBUG_LEVEL >= 2
401 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
402 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
404 return __node_->__value_;
406 _LIBCPP_INLINE_VISIBILITY
407 pointer operator->() const
409 #if _LIBCPP_DEBUG_LEVEL >= 2
410 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
411 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
413 return pointer_traits<pointer>::pointer_to(__node_->__value_);
416 _LIBCPP_INLINE_VISIBILITY
417 __hash_local_iterator& operator++()
419 #if _LIBCPP_DEBUG_LEVEL >= 2
420 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
421 "Attempted to increment non-incrementable unordered container local_iterator");
423 __node_ = __node_->__next_;
424 if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_)
429 _LIBCPP_INLINE_VISIBILITY
430 __hash_local_iterator operator++(int)
432 __hash_local_iterator __t(*this);
437 friend _LIBCPP_INLINE_VISIBILITY
438 bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
440 return __x.__node_ == __y.__node_;
442 friend _LIBCPP_INLINE_VISIBILITY
443 bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
444 {return !(__x == __y);}
447 #if _LIBCPP_DEBUG_LEVEL >= 2
448 _LIBCPP_INLINE_VISIBILITY
449 __hash_local_iterator(__node_pointer __node, size_t __bucket,
450 size_t __bucket_count, const void* __c) _NOEXCEPT
453 __bucket_count_(__bucket_count)
455 __get_db()->__insert_ic(this, __c);
456 if (__node_ != nullptr)
457 __node_ = __node_->__next_;
460 _LIBCPP_INLINE_VISIBILITY
461 __hash_local_iterator(__node_pointer __node, size_t __bucket,
462 size_t __bucket_count) _NOEXCEPT
465 __bucket_count_(__bucket_count)
467 if (__node_ != nullptr)
468 __node_ = __node_->__next_;
471 template <class, class, class, class> friend class __hash_table;
472 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
473 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
476 template <class _ConstNodePtr>
477 class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator
479 typedef _ConstNodePtr __node_pointer;
481 __node_pointer __node_;
483 size_t __bucket_count_;
485 typedef pointer_traits<__node_pointer> __pointer_traits;
486 typedef typename __pointer_traits::element_type __node;
487 typedef typename remove_const<__node>::type __non_const_node;
488 typedef typename __rebind_pointer<__node_pointer, __non_const_node>::type
489 __non_const_node_pointer;
491 typedef __hash_local_iterator<__non_const_node_pointer>
492 __non_const_iterator;
494 typedef forward_iterator_tag iterator_category;
495 typedef typename remove_const<
496 typename __pointer_traits::element_type::value_type
498 typedef typename __pointer_traits::difference_type difference_type;
499 typedef const value_type& reference;
500 typedef typename __rebind_pointer<__node_pointer, const value_type>::type
504 _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT
506 #if _LIBCPP_DEBUG_LEVEL >= 2
507 __get_db()->__insert_i(this);
511 _LIBCPP_INLINE_VISIBILITY
512 __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT
513 : __node_(__x.__node_),
514 __bucket_(__x.__bucket_),
515 __bucket_count_(__x.__bucket_count_)
517 #if _LIBCPP_DEBUG_LEVEL >= 2
518 __get_db()->__iterator_copy(this, &__x);
522 #if _LIBCPP_DEBUG_LEVEL >= 2
524 _LIBCPP_INLINE_VISIBILITY
525 __hash_const_local_iterator(const __hash_const_local_iterator& __i)
526 : __node_(__i.__node_),
527 __bucket_(__i.__bucket_),
528 __bucket_count_(__i.__bucket_count_)
530 __get_db()->__iterator_copy(this, &__i);
533 _LIBCPP_INLINE_VISIBILITY
534 ~__hash_const_local_iterator()
536 __get_db()->__erase_i(this);
539 _LIBCPP_INLINE_VISIBILITY
540 __hash_const_local_iterator& operator=(const __hash_const_local_iterator& __i)
544 __get_db()->__iterator_copy(this, &__i);
545 __node_ = __i.__node_;
546 __bucket_ = __i.__bucket_;
547 __bucket_count_ = __i.__bucket_count_;
552 #endif // _LIBCPP_DEBUG_LEVEL >= 2
554 _LIBCPP_INLINE_VISIBILITY
555 reference operator*() const
557 #if _LIBCPP_DEBUG_LEVEL >= 2
558 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
559 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
561 return __node_->__value_;
563 _LIBCPP_INLINE_VISIBILITY
564 pointer operator->() const
566 #if _LIBCPP_DEBUG_LEVEL >= 2
567 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
568 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
570 return pointer_traits<pointer>::pointer_to(__node_->__value_);
573 _LIBCPP_INLINE_VISIBILITY
574 __hash_const_local_iterator& operator++()
576 #if _LIBCPP_DEBUG_LEVEL >= 2
577 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
578 "Attempted to increment non-incrementable unordered container const_local_iterator");
580 __node_ = __node_->__next_;
581 if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_)
586 _LIBCPP_INLINE_VISIBILITY
587 __hash_const_local_iterator operator++(int)
589 __hash_const_local_iterator __t(*this);
594 friend _LIBCPP_INLINE_VISIBILITY
595 bool operator==(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
597 return __x.__node_ == __y.__node_;
599 friend _LIBCPP_INLINE_VISIBILITY
600 bool operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
601 {return !(__x == __y);}
604 #if _LIBCPP_DEBUG_LEVEL >= 2
605 _LIBCPP_INLINE_VISIBILITY
606 __hash_const_local_iterator(__node_pointer __node, size_t __bucket,
607 size_t __bucket_count, const void* __c) _NOEXCEPT
610 __bucket_count_(__bucket_count)
612 __get_db()->__insert_ic(this, __c);
613 if (__node_ != nullptr)
614 __node_ = __node_->__next_;
617 _LIBCPP_INLINE_VISIBILITY
618 __hash_const_local_iterator(__node_pointer __node, size_t __bucket,
619 size_t __bucket_count) _NOEXCEPT
622 __bucket_count_(__bucket_count)
624 if (__node_ != nullptr)
625 __node_ = __node_->__next_;
628 template <class, class, class, class> friend class __hash_table;
629 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
632 template <class _Alloc>
633 class __bucket_list_deallocator
635 typedef _Alloc allocator_type;
636 typedef allocator_traits<allocator_type> __alloc_traits;
637 typedef typename __alloc_traits::size_type size_type;
639 __compressed_pair<size_type, allocator_type> __data_;
641 typedef typename __alloc_traits::pointer pointer;
643 _LIBCPP_INLINE_VISIBILITY
644 __bucket_list_deallocator()
645 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
648 _LIBCPP_INLINE_VISIBILITY
649 __bucket_list_deallocator(const allocator_type& __a, size_type __size)
650 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
651 : __data_(__size, __a) {}
653 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
655 _LIBCPP_INLINE_VISIBILITY
656 __bucket_list_deallocator(__bucket_list_deallocator&& __x)
657 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
658 : __data_(_VSTD::move(__x.__data_))
663 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
665 _LIBCPP_INLINE_VISIBILITY
666 size_type& size() _NOEXCEPT {return __data_.first();}
667 _LIBCPP_INLINE_VISIBILITY
668 size_type size() const _NOEXCEPT {return __data_.first();}
670 _LIBCPP_INLINE_VISIBILITY
671 allocator_type& __alloc() _NOEXCEPT {return __data_.second();}
672 _LIBCPP_INLINE_VISIBILITY
673 const allocator_type& __alloc() const _NOEXCEPT {return __data_.second();}
675 _LIBCPP_INLINE_VISIBILITY
676 void operator()(pointer __p) _NOEXCEPT
678 __alloc_traits::deallocate(__alloc(), __p, size());
682 template <class _Alloc> class __hash_map_node_destructor;
684 template <class _Alloc>
685 class __hash_node_destructor
687 typedef _Alloc allocator_type;
688 typedef allocator_traits<allocator_type> __alloc_traits;
689 typedef typename __alloc_traits::value_type::value_type value_type;
691 typedef typename __alloc_traits::pointer pointer;
694 allocator_type& __na_;
696 __hash_node_destructor& operator=(const __hash_node_destructor&);
699 bool __value_constructed;
701 _LIBCPP_INLINE_VISIBILITY
702 explicit __hash_node_destructor(allocator_type& __na,
703 bool __constructed = false) _NOEXCEPT
705 __value_constructed(__constructed)
708 _LIBCPP_INLINE_VISIBILITY
709 void operator()(pointer __p) _NOEXCEPT
711 if (__value_constructed)
712 __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_));
714 __alloc_traits::deallocate(__na_, __p, 1);
717 template <class> friend class __hash_map_node_destructor;
720 template <class _Tp, class _Hash, class _Equal, class _Alloc>
724 typedef _Tp value_type;
725 typedef _Hash hasher;
726 typedef _Equal key_equal;
727 typedef _Alloc allocator_type;
730 typedef allocator_traits<allocator_type> __alloc_traits;
732 typedef value_type& reference;
733 typedef const value_type& const_reference;
734 typedef typename __alloc_traits::pointer pointer;
735 typedef typename __alloc_traits::const_pointer const_pointer;
736 typedef typename __alloc_traits::size_type size_type;
737 typedef typename __alloc_traits::difference_type difference_type;
740 typedef __hash_node<value_type, typename __alloc_traits::void_pointer> __node;
741 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
742 typedef allocator_traits<__node_allocator> __node_traits;
743 typedef typename __node_traits::pointer __node_pointer;
744 typedef typename __node_traits::pointer __node_const_pointer;
745 typedef __hash_node_base<__node_pointer> __first_node;
746 typedef typename __rebind_pointer<__node_pointer, __first_node>::type
751 typedef typename __rebind_alloc_helper<__node_traits, __node_pointer>::type __pointer_allocator;
752 typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter;
753 typedef unique_ptr<__node_pointer[], __bucket_list_deleter> __bucket_list;
754 typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits;
755 typedef typename __bucket_list_deleter::pointer __node_pointer_pointer;
757 // --- Member data begin ---
758 __bucket_list __bucket_list_;
759 __compressed_pair<__first_node, __node_allocator> __p1_;
760 __compressed_pair<size_type, hasher> __p2_;
761 __compressed_pair<float, key_equal> __p3_;
762 // --- Member data end ---
764 _LIBCPP_INLINE_VISIBILITY
765 size_type& size() _NOEXCEPT {return __p2_.first();}
767 _LIBCPP_INLINE_VISIBILITY
768 size_type size() const _NOEXCEPT {return __p2_.first();}
770 _LIBCPP_INLINE_VISIBILITY
771 hasher& hash_function() _NOEXCEPT {return __p2_.second();}
772 _LIBCPP_INLINE_VISIBILITY
773 const hasher& hash_function() const _NOEXCEPT {return __p2_.second();}
775 _LIBCPP_INLINE_VISIBILITY
776 float& max_load_factor() _NOEXCEPT {return __p3_.first();}
777 _LIBCPP_INLINE_VISIBILITY
778 float max_load_factor() const _NOEXCEPT {return __p3_.first();}
780 _LIBCPP_INLINE_VISIBILITY
781 key_equal& key_eq() _NOEXCEPT {return __p3_.second();}
782 _LIBCPP_INLINE_VISIBILITY
783 const key_equal& key_eq() const _NOEXCEPT {return __p3_.second();}
785 _LIBCPP_INLINE_VISIBILITY
786 __node_allocator& __node_alloc() _NOEXCEPT {return __p1_.second();}
787 _LIBCPP_INLINE_VISIBILITY
788 const __node_allocator& __node_alloc() const _NOEXCEPT
789 {return __p1_.second();}
792 typedef __hash_iterator<__node_pointer> iterator;
793 typedef __hash_const_iterator<__node_pointer> const_iterator;
794 typedef __hash_local_iterator<__node_pointer> local_iterator;
795 typedef __hash_const_local_iterator<__node_pointer> const_local_iterator;
797 _LIBCPP_INLINE_VISIBILITY
800 is_nothrow_default_constructible<__bucket_list>::value &&
801 is_nothrow_default_constructible<__first_node>::value &&
802 is_nothrow_default_constructible<__node_allocator>::value &&
803 is_nothrow_default_constructible<hasher>::value &&
804 is_nothrow_default_constructible<key_equal>::value);
805 _LIBCPP_INLINE_VISIBILITY
806 __hash_table(const hasher& __hf, const key_equal& __eql);
807 __hash_table(const hasher& __hf, const key_equal& __eql,
808 const allocator_type& __a);
809 explicit __hash_table(const allocator_type& __a);
810 __hash_table(const __hash_table& __u);
811 __hash_table(const __hash_table& __u, const allocator_type& __a);
812 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
813 __hash_table(__hash_table&& __u)
815 is_nothrow_move_constructible<__bucket_list>::value &&
816 is_nothrow_move_constructible<__first_node>::value &&
817 is_nothrow_move_constructible<__node_allocator>::value &&
818 is_nothrow_move_constructible<hasher>::value &&
819 is_nothrow_move_constructible<key_equal>::value);
820 __hash_table(__hash_table&& __u, const allocator_type& __a);
821 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
824 __hash_table& operator=(const __hash_table& __u);
825 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
826 _LIBCPP_INLINE_VISIBILITY
827 __hash_table& operator=(__hash_table&& __u)
829 __node_traits::propagate_on_container_move_assignment::value &&
830 is_nothrow_move_assignable<__node_allocator>::value &&
831 is_nothrow_move_assignable<hasher>::value &&
832 is_nothrow_move_assignable<key_equal>::value);
834 template <class _InputIterator>
835 void __assign_unique(_InputIterator __first, _InputIterator __last);
836 template <class _InputIterator>
837 void __assign_multi(_InputIterator __first, _InputIterator __last);
839 _LIBCPP_INLINE_VISIBILITY
840 size_type max_size() const _NOEXCEPT
842 return allocator_traits<__pointer_allocator>::max_size(
843 __bucket_list_.get_deleter().__alloc());
846 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
847 iterator __node_insert_multi(__node_pointer __nd);
848 iterator __node_insert_multi(const_iterator __p,
849 __node_pointer __nd);
851 #if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
852 template <class... _Args>
853 pair<iterator, bool> __emplace_unique(_Args&&... __args);
854 template <class... _Args>
855 iterator __emplace_multi(_Args&&... __args);
856 template <class... _Args>
857 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
858 #endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
860 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
861 template <class _ValueTp>
862 _LIBCPP_INLINE_VISIBILITY
863 pair<iterator, bool> __insert_unique_value(_ValueTp&& __x);
865 _LIBCPP_INLINE_VISIBILITY
866 pair<iterator, bool> __insert_unique_value(const value_type& __x);
869 pair<iterator, bool> __insert_unique(const value_type& __x);
871 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
872 pair<iterator, bool> __insert_unique(value_type&& __x);
874 pair<iterator, bool> __insert_unique(_Pp&& __x);
875 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
877 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
879 iterator __insert_multi(_Pp&& __x);
881 iterator __insert_multi(const_iterator __p, _Pp&& __x);
882 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
883 iterator __insert_multi(const value_type& __x);
884 iterator __insert_multi(const_iterator __p, const value_type& __x);
885 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
887 void clear() _NOEXCEPT;
888 void rehash(size_type __n);
889 _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n)
890 {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));}
892 _LIBCPP_INLINE_VISIBILITY
893 size_type bucket_count() const _NOEXCEPT
895 return __bucket_list_.get_deleter().size();
898 _LIBCPP_INLINE_VISIBILITY
899 iterator begin() _NOEXCEPT;
900 _LIBCPP_INLINE_VISIBILITY
901 iterator end() _NOEXCEPT;
902 _LIBCPP_INLINE_VISIBILITY
903 const_iterator begin() const _NOEXCEPT;
904 _LIBCPP_INLINE_VISIBILITY
905 const_iterator end() const _NOEXCEPT;
907 template <class _Key>
908 _LIBCPP_INLINE_VISIBILITY
909 size_type bucket(const _Key& __k) const
911 _LIBCPP_ASSERT(bucket_count() > 0,
912 "unordered container::bucket(key) called when bucket_count() == 0");
913 return __constrain_hash(hash_function()(__k), bucket_count());
916 template <class _Key>
917 iterator find(const _Key& __x);
918 template <class _Key>
919 const_iterator find(const _Key& __x) const;
921 typedef __hash_node_destructor<__node_allocator> _Dp;
922 typedef unique_ptr<__node, _Dp> __node_holder;
924 iterator erase(const_iterator __p);
925 iterator erase(const_iterator __first, const_iterator __last);
926 template <class _Key>
927 size_type __erase_unique(const _Key& __k);
928 template <class _Key>
929 size_type __erase_multi(const _Key& __k);
930 __node_holder remove(const_iterator __p) _NOEXCEPT;
932 template <class _Key>
933 _LIBCPP_INLINE_VISIBILITY
934 size_type __count_unique(const _Key& __k) const;
935 template <class _Key>
936 size_type __count_multi(const _Key& __k) const;
938 template <class _Key>
939 pair<iterator, iterator>
940 __equal_range_unique(const _Key& __k);
941 template <class _Key>
942 pair<const_iterator, const_iterator>
943 __equal_range_unique(const _Key& __k) const;
945 template <class _Key>
946 pair<iterator, iterator>
947 __equal_range_multi(const _Key& __k);
948 template <class _Key>
949 pair<const_iterator, const_iterator>
950 __equal_range_multi(const _Key& __k) const;
952 void swap(__hash_table& __u)
953 #if _LIBCPP_STD_VER <= 11
955 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
956 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
957 || __is_nothrow_swappable<__pointer_allocator>::value)
958 && (!__node_traits::propagate_on_container_swap::value
959 || __is_nothrow_swappable<__node_allocator>::value)
962 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value);
965 _LIBCPP_INLINE_VISIBILITY
966 size_type max_bucket_count() const _NOEXCEPT
967 {return __pointer_alloc_traits::max_size(__bucket_list_.get_deleter().__alloc());}
968 size_type bucket_size(size_type __n) const;
969 _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT
971 size_type __bc = bucket_count();
972 return __bc != 0 ? (float)size() / __bc : 0.f;
974 _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT
976 _LIBCPP_ASSERT(__mlf > 0,
977 "unordered container::max_load_factor(lf) called with lf <= 0");
978 max_load_factor() = _VSTD::max(__mlf, load_factor());
981 _LIBCPP_INLINE_VISIBILITY
985 _LIBCPP_ASSERT(__n < bucket_count(),
986 "unordered container::begin(n) called with n >= bucket_count()");
987 #if _LIBCPP_DEBUG_LEVEL >= 2
988 return local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
990 return local_iterator(__bucket_list_[__n], __n, bucket_count());
994 _LIBCPP_INLINE_VISIBILITY
998 _LIBCPP_ASSERT(__n < bucket_count(),
999 "unordered container::end(n) called with n >= bucket_count()");
1000 #if _LIBCPP_DEBUG_LEVEL >= 2
1001 return local_iterator(nullptr, __n, bucket_count(), this);
1003 return local_iterator(nullptr, __n, bucket_count());
1007 _LIBCPP_INLINE_VISIBILITY
1008 const_local_iterator
1009 cbegin(size_type __n) const
1011 _LIBCPP_ASSERT(__n < bucket_count(),
1012 "unordered container::cbegin(n) called with n >= bucket_count()");
1013 #if _LIBCPP_DEBUG_LEVEL >= 2
1014 return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1016 return const_local_iterator(__bucket_list_[__n], __n, bucket_count());
1020 _LIBCPP_INLINE_VISIBILITY
1021 const_local_iterator
1022 cend(size_type __n) const
1024 _LIBCPP_ASSERT(__n < bucket_count(),
1025 "unordered container::cend(n) called with n >= bucket_count()");
1026 #if _LIBCPP_DEBUG_LEVEL >= 2
1027 return const_local_iterator(nullptr, __n, bucket_count(), this);
1029 return const_local_iterator(nullptr, __n, bucket_count());
1033 #if _LIBCPP_DEBUG_LEVEL >= 2
1035 bool __dereferenceable(const const_iterator* __i) const;
1036 bool __decrementable(const const_iterator* __i) const;
1037 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1038 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1040 #endif // _LIBCPP_DEBUG_LEVEL >= 2
1043 void __rehash(size_type __n);
1045 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1046 #ifndef _LIBCPP_HAS_NO_VARIADICS
1047 template <class ..._Args>
1048 __node_holder __construct_node(_Args&& ...__args);
1049 #endif // _LIBCPP_HAS_NO_VARIADICS
1050 __node_holder __construct_node(value_type&& __v, size_t __hash);
1051 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1052 __node_holder __construct_node(const value_type& __v);
1054 __node_holder __construct_node(const value_type& __v, size_t __hash);
1056 _LIBCPP_INLINE_VISIBILITY
1057 void __copy_assign_alloc(const __hash_table& __u)
1058 {__copy_assign_alloc(__u, integral_constant<bool,
1059 __node_traits::propagate_on_container_copy_assignment::value>());}
1060 void __copy_assign_alloc(const __hash_table& __u, true_type);
1061 _LIBCPP_INLINE_VISIBILITY
1062 void __copy_assign_alloc(const __hash_table&, false_type) {}
1064 void __move_assign(__hash_table& __u, false_type);
1065 void __move_assign(__hash_table& __u, true_type)
1067 is_nothrow_move_assignable<__node_allocator>::value &&
1068 is_nothrow_move_assignable<hasher>::value &&
1069 is_nothrow_move_assignable<key_equal>::value);
1070 _LIBCPP_INLINE_VISIBILITY
1071 void __move_assign_alloc(__hash_table& __u)
1073 !__node_traits::propagate_on_container_move_assignment::value ||
1074 (is_nothrow_move_assignable<__pointer_allocator>::value &&
1075 is_nothrow_move_assignable<__node_allocator>::value))
1076 {__move_assign_alloc(__u, integral_constant<bool,
1077 __node_traits::propagate_on_container_move_assignment::value>());}
1078 _LIBCPP_INLINE_VISIBILITY
1079 void __move_assign_alloc(__hash_table& __u, true_type)
1081 is_nothrow_move_assignable<__pointer_allocator>::value &&
1082 is_nothrow_move_assignable<__node_allocator>::value)
1084 __bucket_list_.get_deleter().__alloc() =
1085 _VSTD::move(__u.__bucket_list_.get_deleter().__alloc());
1086 __node_alloc() = _VSTD::move(__u.__node_alloc());
1088 _LIBCPP_INLINE_VISIBILITY
1089 void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
1091 void __deallocate(__node_pointer __np) _NOEXCEPT;
1092 __node_pointer __detach() _NOEXCEPT;
1094 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
1095 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
1098 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1100 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table()
1102 is_nothrow_default_constructible<__bucket_list>::value &&
1103 is_nothrow_default_constructible<__first_node>::value &&
1104 is_nothrow_default_constructible<__node_allocator>::value &&
1105 is_nothrow_default_constructible<hasher>::value &&
1106 is_nothrow_default_constructible<key_equal>::value)
1112 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1114 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1115 const key_equal& __eql)
1116 : __bucket_list_(nullptr, __bucket_list_deleter()),
1123 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1124 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1125 const key_equal& __eql,
1126 const allocator_type& __a)
1127 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1128 __p1_(__node_allocator(__a)),
1134 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1135 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a)
1136 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1137 __p1_(__node_allocator(__a)),
1143 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1144 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u)
1145 : __bucket_list_(nullptr,
1146 __bucket_list_deleter(allocator_traits<__pointer_allocator>::
1147 select_on_container_copy_construction(
1148 __u.__bucket_list_.get_deleter().__alloc()), 0)),
1149 __p1_(allocator_traits<__node_allocator>::
1150 select_on_container_copy_construction(__u.__node_alloc())),
1151 __p2_(0, __u.hash_function()),
1156 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1157 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u,
1158 const allocator_type& __a)
1159 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1160 __p1_(__node_allocator(__a)),
1161 __p2_(0, __u.hash_function()),
1166 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1168 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1169 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u)
1171 is_nothrow_move_constructible<__bucket_list>::value &&
1172 is_nothrow_move_constructible<__first_node>::value &&
1173 is_nothrow_move_constructible<__node_allocator>::value &&
1174 is_nothrow_move_constructible<hasher>::value &&
1175 is_nothrow_move_constructible<key_equal>::value)
1176 : __bucket_list_(_VSTD::move(__u.__bucket_list_)),
1177 __p1_(_VSTD::move(__u.__p1_)),
1178 __p2_(_VSTD::move(__u.__p2_)),
1179 __p3_(_VSTD::move(__u.__p3_))
1183 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
1184 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
1185 __u.__p1_.first().__next_ = nullptr;
1190 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1191 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u,
1192 const allocator_type& __a)
1193 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1194 __p1_(__node_allocator(__a)),
1195 __p2_(0, _VSTD::move(__u.hash_function())),
1196 __p3_(_VSTD::move(__u.__p3_))
1198 if (__a == allocator_type(__u.__node_alloc()))
1200 __bucket_list_.reset(__u.__bucket_list_.release());
1201 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1202 __u.__bucket_list_.get_deleter().size() = 0;
1205 __p1_.first().__next_ = __u.__p1_.first().__next_;
1206 __u.__p1_.first().__next_ = nullptr;
1207 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
1208 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
1209 size() = __u.size();
1215 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1217 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1218 __hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table()
1220 __deallocate(__p1_.first().__next_);
1221 #if _LIBCPP_DEBUG_LEVEL >= 2
1222 __get_db()->__erase_c(this);
1226 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1228 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(
1229 const __hash_table& __u, true_type)
1231 if (__node_alloc() != __u.__node_alloc())
1234 __bucket_list_.reset();
1235 __bucket_list_.get_deleter().size() = 0;
1237 __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
1238 __node_alloc() = __u.__node_alloc();
1241 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1242 __hash_table<_Tp, _Hash, _Equal, _Alloc>&
1243 __hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u)
1247 __copy_assign_alloc(__u);
1248 hash_function() = __u.hash_function();
1249 key_eq() = __u.key_eq();
1250 max_load_factor() = __u.max_load_factor();
1251 __assign_multi(__u.begin(), __u.end());
1256 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1258 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate(__node_pointer __np)
1261 __node_allocator& __na = __node_alloc();
1262 while (__np != nullptr)
1264 __node_pointer __next = __np->__next_;
1265 #if _LIBCPP_DEBUG_LEVEL >= 2
1266 __c_node* __c = __get_db()->__find_c_and_lock(this);
1267 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1270 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1271 if (__i->__node_ == __np)
1273 (*__p)->__c_ = nullptr;
1274 if (--__c->end_ != __p)
1275 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1278 __get_db()->unlock();
1280 __node_traits::destroy(__na, _VSTD::addressof(__np->__value_));
1281 __node_traits::deallocate(__na, __np, 1);
1286 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1287 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_pointer
1288 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
1290 size_type __bc = bucket_count();
1291 for (size_type __i = 0; __i < __bc; ++__i)
1292 __bucket_list_[__i] = nullptr;
1294 __node_pointer __cache = __p1_.first().__next_;
1295 __p1_.first().__next_ = nullptr;
1299 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1301 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1303 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1304 __hash_table& __u, true_type)
1306 is_nothrow_move_assignable<__node_allocator>::value &&
1307 is_nothrow_move_assignable<hasher>::value &&
1308 is_nothrow_move_assignable<key_equal>::value)
1311 __bucket_list_.reset(__u.__bucket_list_.release());
1312 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1313 __u.__bucket_list_.get_deleter().size() = 0;
1314 __move_assign_alloc(__u);
1315 size() = __u.size();
1316 hash_function() = _VSTD::move(__u.hash_function());
1317 max_load_factor() = __u.max_load_factor();
1318 key_eq() = _VSTD::move(__u.key_eq());
1319 __p1_.first().__next_ = __u.__p1_.first().__next_;
1322 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
1323 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
1324 __u.__p1_.first().__next_ = nullptr;
1327 #if _LIBCPP_DEBUG_LEVEL >= 2
1328 __get_db()->swap(this, &__u);
1332 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1334 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1335 __hash_table& __u, false_type)
1337 if (__node_alloc() == __u.__node_alloc())
1338 __move_assign(__u, true_type());
1341 hash_function() = _VSTD::move(__u.hash_function());
1342 key_eq() = _VSTD::move(__u.key_eq());
1343 max_load_factor() = __u.max_load_factor();
1344 if (bucket_count() != 0)
1346 __node_pointer __cache = __detach();
1347 #ifndef _LIBCPP_NO_EXCEPTIONS
1350 #endif // _LIBCPP_NO_EXCEPTIONS
1351 const_iterator __i = __u.begin();
1352 while (__cache != nullptr && __u.size() != 0)
1354 __cache->__value_ = _VSTD::move(__u.remove(__i++)->__value_);
1355 __node_pointer __next = __cache->__next_;
1356 __node_insert_multi(__cache);
1359 #ifndef _LIBCPP_NO_EXCEPTIONS
1363 __deallocate(__cache);
1366 #endif // _LIBCPP_NO_EXCEPTIONS
1367 __deallocate(__cache);
1369 const_iterator __i = __u.begin();
1370 while (__u.size() != 0)
1373 __construct_node(_VSTD::move(__u.remove(__i++)->__value_));
1374 __node_insert_multi(__h.get());
1380 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1382 __hash_table<_Tp, _Hash, _Equal, _Alloc>&
1383 __hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
1385 __node_traits::propagate_on_container_move_assignment::value &&
1386 is_nothrow_move_assignable<__node_allocator>::value &&
1387 is_nothrow_move_assignable<hasher>::value &&
1388 is_nothrow_move_assignable<key_equal>::value)
1390 __move_assign(__u, integral_constant<bool,
1391 __node_traits::propagate_on_container_move_assignment::value>());
1395 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1397 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1398 template <class _InputIterator>
1400 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first,
1401 _InputIterator __last)
1403 if (bucket_count() != 0)
1405 __node_pointer __cache = __detach();
1406 #ifndef _LIBCPP_NO_EXCEPTIONS
1409 #endif // _LIBCPP_NO_EXCEPTIONS
1410 for (; __cache != nullptr && __first != __last; ++__first)
1412 __cache->__value_ = *__first;
1413 __node_pointer __next = __cache->__next_;
1414 __node_insert_unique(__cache);
1417 #ifndef _LIBCPP_NO_EXCEPTIONS
1421 __deallocate(__cache);
1424 #endif // _LIBCPP_NO_EXCEPTIONS
1425 __deallocate(__cache);
1427 for (; __first != __last; ++__first)
1428 __insert_unique(*__first);
1431 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1432 template <class _InputIterator>
1434 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
1435 _InputIterator __last)
1437 if (bucket_count() != 0)
1439 __node_pointer __cache = __detach();
1440 #ifndef _LIBCPP_NO_EXCEPTIONS
1443 #endif // _LIBCPP_NO_EXCEPTIONS
1444 for (; __cache != nullptr && __first != __last; ++__first)
1446 __cache->__value_ = *__first;
1447 __node_pointer __next = __cache->__next_;
1448 __node_insert_multi(__cache);
1451 #ifndef _LIBCPP_NO_EXCEPTIONS
1455 __deallocate(__cache);
1458 #endif // _LIBCPP_NO_EXCEPTIONS
1459 __deallocate(__cache);
1461 for (; __first != __last; ++__first)
1462 __insert_multi(*__first);
1465 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1467 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1468 __hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT
1470 #if _LIBCPP_DEBUG_LEVEL >= 2
1471 return iterator(__p1_.first().__next_, this);
1473 return iterator(__p1_.first().__next_);
1477 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1479 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1480 __hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT
1482 #if _LIBCPP_DEBUG_LEVEL >= 2
1483 return iterator(nullptr, this);
1485 return iterator(nullptr);
1489 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1491 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
1492 __hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT
1494 #if _LIBCPP_DEBUG_LEVEL >= 2
1495 return const_iterator(__p1_.first().__next_, this);
1497 return const_iterator(__p1_.first().__next_);
1501 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1503 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
1504 __hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT
1506 #if _LIBCPP_DEBUG_LEVEL >= 2
1507 return const_iterator(nullptr, this);
1509 return const_iterator(nullptr);
1513 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1515 __hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT
1519 __deallocate(__p1_.first().__next_);
1520 __p1_.first().__next_ = nullptr;
1521 size_type __bc = bucket_count();
1522 for (size_type __i = 0; __i < __bc; ++__i)
1523 __bucket_list_[__i] = nullptr;
1528 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1529 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1530 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd)
1532 __nd->__hash_ = hash_function()(__nd->__value_);
1533 size_type __bc = bucket_count();
1534 bool __inserted = false;
1535 __node_pointer __ndptr;
1539 __chash = __constrain_hash(__nd->__hash_, __bc);
1540 __ndptr = __bucket_list_[__chash];
1541 if (__ndptr != nullptr)
1543 for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
1544 __constrain_hash(__ndptr->__hash_, __bc) == __chash;
1545 __ndptr = __ndptr->__next_)
1547 if (key_eq()(__ndptr->__value_, __nd->__value_))
1553 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1555 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
1556 size_type(ceil(float(size() + 1) / max_load_factor()))));
1557 __bc = bucket_count();
1558 __chash = __constrain_hash(__nd->__hash_, __bc);
1560 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
1561 __node_pointer __pn = __bucket_list_[__chash];
1562 if (__pn == nullptr)
1564 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
1565 __nd->__next_ = __pn->__next_;
1566 __pn->__next_ = __nd;
1567 // fix up __bucket_list_
1568 __bucket_list_[__chash] = __pn;
1569 if (__nd->__next_ != nullptr)
1570 __bucket_list_[__constrain_hash(__nd->__next_->__hash_, __bc)] = __nd;
1574 __nd->__next_ = __pn->__next_;
1575 __pn->__next_ = __nd;
1583 #if _LIBCPP_DEBUG_LEVEL >= 2
1584 return pair<iterator, bool>(iterator(__ndptr, this), __inserted);
1586 return pair<iterator, bool>(iterator(__ndptr), __inserted);
1590 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1591 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1592 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp)
1594 __cp->__hash_ = hash_function()(__cp->__value_);
1595 size_type __bc = bucket_count();
1596 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1598 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
1599 size_type(ceil(float(size() + 1) / max_load_factor()))));
1600 __bc = bucket_count();
1602 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
1603 __node_pointer __pn = __bucket_list_[__chash];
1604 if (__pn == nullptr)
1606 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
1607 __cp->__next_ = __pn->__next_;
1608 __pn->__next_ = __cp;
1609 // fix up __bucket_list_
1610 __bucket_list_[__chash] = __pn;
1611 if (__cp->__next_ != nullptr)
1612 __bucket_list_[__constrain_hash(__cp->__next_->__hash_, __bc)] = __cp;
1616 for (bool __found = false; __pn->__next_ != nullptr &&
1617 __constrain_hash(__pn->__next_->__hash_, __bc) == __chash;
1618 __pn = __pn->__next_)
1620 // __found key_eq() action
1623 // false true set __found to true
1625 if (__found != (__pn->__next_->__hash_ == __cp->__hash_ &&
1626 key_eq()(__pn->__next_->__value_, __cp->__value_)))
1634 __cp->__next_ = __pn->__next_;
1635 __pn->__next_ = __cp;
1636 if (__cp->__next_ != nullptr)
1638 size_t __nhash = __constrain_hash(__cp->__next_->__hash_, __bc);
1639 if (__nhash != __chash)
1640 __bucket_list_[__nhash] = __cp;
1644 #if _LIBCPP_DEBUG_LEVEL >= 2
1645 return iterator(__cp, this);
1647 return iterator(__cp);
1651 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1652 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1653 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(
1654 const_iterator __p, __node_pointer __cp)
1656 #if _LIBCPP_DEBUG_LEVEL >= 2
1657 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1658 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
1659 " referring to this unordered container");
1661 if (__p != end() && key_eq()(*__p, __cp->__value_))
1663 __node_pointer __np = __p.__node_;
1664 __cp->__hash_ = __np->__hash_;
1665 size_type __bc = bucket_count();
1666 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1668 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
1669 size_type(ceil(float(size() + 1) / max_load_factor()))));
1670 __bc = bucket_count();
1672 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
1673 __node_pointer __pp = __bucket_list_[__chash];
1674 while (__pp->__next_ != __np)
1675 __pp = __pp->__next_;
1676 __cp->__next_ = __np;
1677 __pp->__next_ = __cp;
1679 #if _LIBCPP_DEBUG_LEVEL >= 2
1680 return iterator(__cp, this);
1682 return iterator(__cp);
1685 return __node_insert_multi(__cp);
1688 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1689 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1690 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique(const value_type& __x)
1692 return __insert_unique_value(__x);
1696 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1697 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1698 template <class _ValueTp>
1699 _LIBCPP_INLINE_VISIBILITY
1700 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1701 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique_value(_ValueTp&& __x)
1703 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1704 _LIBCPP_INLINE_VISIBILITY
1705 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1706 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique_value(const value_type& __x)
1709 #if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1710 typedef const value_type& _ValueTp;
1712 size_t __hash = hash_function()(__x);
1713 size_type __bc = bucket_count();
1714 bool __inserted = false;
1715 __node_pointer __nd;
1719 __chash = __constrain_hash(__hash, __bc);
1720 __nd = __bucket_list_[__chash];
1721 if (__nd != nullptr)
1723 for (__nd = __nd->__next_; __nd != nullptr &&
1724 __constrain_hash(__nd->__hash_, __bc) == __chash;
1725 __nd = __nd->__next_)
1727 if (key_eq()(__nd->__value_, __x))
1733 __node_holder __h = __construct_node(_VSTD::forward<_ValueTp>(__x), __hash);
1734 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1736 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
1737 size_type(ceil(float(size() + 1) / max_load_factor()))));
1738 __bc = bucket_count();
1739 __chash = __constrain_hash(__hash, __bc);
1741 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
1742 __node_pointer __pn = __bucket_list_[__chash];
1743 if (__pn == nullptr)
1745 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
1746 __h->__next_ = __pn->__next_;
1747 __pn->__next_ = __h.get();
1748 // fix up __bucket_list_
1749 __bucket_list_[__chash] = __pn;
1750 if (__h->__next_ != nullptr)
1751 __bucket_list_[__constrain_hash(__h->__next_->__hash_, __bc)] = __h.get();
1755 __h->__next_ = __pn->__next_;
1756 __pn->__next_ = __h.get();
1758 __nd = __h.release();
1764 #if _LIBCPP_DEBUG_LEVEL >= 2
1765 return pair<iterator, bool>(iterator(__nd, this), __inserted);
1767 return pair<iterator, bool>(iterator(__nd), __inserted);
1771 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1772 #ifndef _LIBCPP_HAS_NO_VARIADICS
1774 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1775 template <class... _Args>
1776 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1777 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique(_Args&&... __args)
1779 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
1780 pair<iterator, bool> __r = __node_insert_unique(__h.get());
1786 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1787 template <class... _Args>
1788 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1789 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args)
1791 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
1792 iterator __r = __node_insert_multi(__h.get());
1797 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1798 template <class... _Args>
1799 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1800 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(
1801 const_iterator __p, _Args&&... __args)
1803 #if _LIBCPP_DEBUG_LEVEL >= 2
1804 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1805 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
1806 " referring to this unordered container");
1808 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
1809 iterator __r = __node_insert_multi(__p, __h.get());
1814 #endif // _LIBCPP_HAS_NO_VARIADICS
1816 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1817 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1818 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique(value_type&& __x)
1820 return __insert_unique_value(_VSTD::move(__x));
1823 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1824 template <class _Pp>
1825 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1826 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique(_Pp&& __x)
1828 __node_holder __h = __construct_node(_VSTD::forward<_Pp>(__x));
1829 pair<iterator, bool> __r = __node_insert_unique(__h.get());
1835 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1837 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1839 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1840 template <class _Pp>
1841 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1842 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(_Pp&& __x)
1844 __node_holder __h = __construct_node(_VSTD::forward<_Pp>(__x));
1845 iterator __r = __node_insert_multi(__h.get());
1850 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1851 template <class _Pp>
1852 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1853 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
1856 #if _LIBCPP_DEBUG_LEVEL >= 2
1857 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1858 "unordered container::insert(const_iterator, rvalue) called with an iterator not"
1859 " referring to this unordered container");
1861 __node_holder __h = __construct_node(_VSTD::forward<_Pp>(__x));
1862 iterator __r = __node_insert_multi(__p, __h.get());
1867 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1869 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1870 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1871 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const value_type& __x)
1873 __node_holder __h = __construct_node(__x);
1874 iterator __r = __node_insert_multi(__h.get());
1879 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1880 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1881 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
1882 const value_type& __x)
1884 #if _LIBCPP_DEBUG_LEVEL >= 2
1885 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1886 "unordered container::insert(const_iterator, lvalue) called with an iterator not"
1887 " referring to this unordered container");
1889 __node_holder __h = __construct_node(__x);
1890 iterator __r = __node_insert_multi(__p, __h.get());
1895 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1897 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1899 __hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
1903 else if (__n & (__n - 1))
1904 __n = __next_prime(__n);
1905 size_type __bc = bucket_count();
1908 else if (__n < __bc)
1910 __n = _VSTD::max<size_type>
1913 __is_hash_power2(__bc) ? __next_hash_pow2(size_t(ceil(float(size()) / max_load_factor()))) :
1914 __next_prime(size_t(ceil(float(size()) / max_load_factor())))
1921 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1923 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc)
1925 #if _LIBCPP_DEBUG_LEVEL >= 2
1926 __get_db()->__invalidate_all(this);
1927 #endif // _LIBCPP_DEBUG_LEVEL >= 2
1928 __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
1929 __bucket_list_.reset(__nbc > 0 ?
1930 __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
1931 __bucket_list_.get_deleter().size() = __nbc;
1934 for (size_type __i = 0; __i < __nbc; ++__i)
1935 __bucket_list_[__i] = nullptr;
1936 __node_pointer __pp(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())));
1937 __node_pointer __cp = __pp->__next_;
1938 if (__cp != nullptr)
1940 size_type __chash = __constrain_hash(__cp->__hash_, __nbc);
1941 __bucket_list_[__chash] = __pp;
1942 size_type __phash = __chash;
1943 for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr;
1944 __cp = __pp->__next_)
1946 __chash = __constrain_hash(__cp->__hash_, __nbc);
1947 if (__chash == __phash)
1951 if (__bucket_list_[__chash] == nullptr)
1953 __bucket_list_[__chash] = __pp;
1959 __node_pointer __np = __cp;
1960 for (; __np->__next_ != nullptr &&
1961 key_eq()(__cp->__value_, __np->__next_->__value_);
1962 __np = __np->__next_)
1964 __pp->__next_ = __np->__next_;
1965 __np->__next_ = __bucket_list_[__chash]->__next_;
1966 __bucket_list_[__chash]->__next_ = __cp;
1975 template <class _Tp, class _Hash, class _Equal, class _Alloc>
1976 template <class _Key>
1977 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1978 __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
1980 size_t __hash = hash_function()(__k);
1981 size_type __bc = bucket_count();
1984 size_t __chash = __constrain_hash(__hash, __bc);
1985 __node_pointer __nd = __bucket_list_[__chash];
1986 if (__nd != nullptr)
1988 for (__nd = __nd->__next_; __nd != nullptr &&
1989 __constrain_hash(__nd->__hash_, __bc) == __chash;
1990 __nd = __nd->__next_)
1992 if (key_eq()(__nd->__value_, __k))
1993 #if _LIBCPP_DEBUG_LEVEL >= 2
1994 return iterator(__nd, this);
1996 return iterator(__nd);
2004 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2005 template <class _Key>
2006 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
2007 __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
2009 size_t __hash = hash_function()(__k);
2010 size_type __bc = bucket_count();
2013 size_t __chash = __constrain_hash(__hash, __bc);
2014 __node_const_pointer __nd = __bucket_list_[__chash];
2015 if (__nd != nullptr)
2017 for (__nd = __nd->__next_; __nd != nullptr &&
2018 __constrain_hash(__nd->__hash_, __bc) == __chash;
2019 __nd = __nd->__next_)
2021 if (key_eq()(__nd->__value_, __k))
2022 #if _LIBCPP_DEBUG_LEVEL >= 2
2023 return const_iterator(__nd, this);
2025 return const_iterator(__nd);
2034 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2035 #ifndef _LIBCPP_HAS_NO_VARIADICS
2037 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2038 template <class ..._Args>
2039 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2040 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args)
2042 __node_allocator& __na = __node_alloc();
2043 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2044 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), _VSTD::forward<_Args>(__args)...);
2045 __h.get_deleter().__value_constructed = true;
2046 __h->__hash_ = hash_function()(__h->__value_);
2047 __h->__next_ = nullptr;
2051 #endif // _LIBCPP_HAS_NO_VARIADICS
2053 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2054 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2055 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(value_type&& __v,
2058 __node_allocator& __na = __node_alloc();
2059 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2060 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), _VSTD::move(__v));
2061 __h.get_deleter().__value_constructed = true;
2062 __h->__hash_ = __hash;
2063 __h->__next_ = nullptr;
2067 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2069 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2070 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2071 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const value_type& __v)
2073 __node_allocator& __na = __node_alloc();
2074 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2075 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), __v);
2076 __h.get_deleter().__value_constructed = true;
2077 __h->__hash_ = hash_function()(__h->__value_);
2078 __h->__next_ = nullptr;
2079 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
2082 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2084 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2085 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2086 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const value_type& __v,
2089 __node_allocator& __na = __node_alloc();
2090 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2091 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), __v);
2092 __h.get_deleter().__value_constructed = true;
2093 __h->__hash_ = __hash;
2094 __h->__next_ = nullptr;
2095 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
2098 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2099 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2100 __hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p)
2102 __node_pointer __np = __p.__node_;
2103 #if _LIBCPP_DEBUG_LEVEL >= 2
2104 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2105 "unordered container erase(iterator) called with an iterator not"
2106 " referring to this container");
2107 _LIBCPP_ASSERT(__p != end(),
2108 "unordered container erase(iterator) called with a non-dereferenceable iterator");
2109 iterator __r(__np, this);
2118 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2119 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2120 __hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first,
2121 const_iterator __last)
2123 #if _LIBCPP_DEBUG_LEVEL >= 2
2124 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2125 "unodered container::erase(iterator, iterator) called with an iterator not"
2126 " referring to this unodered container");
2127 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
2128 "unodered container::erase(iterator, iterator) called with an iterator not"
2129 " referring to this unodered container");
2131 for (const_iterator __p = __first; __first != __last; __p = __first)
2136 __node_pointer __np = __last.__node_;
2137 #if _LIBCPP_DEBUG_LEVEL >= 2
2138 return iterator (__np, this);
2140 return iterator (__np);
2144 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2145 template <class _Key>
2146 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2147 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k)
2149 iterator __i = find(__k);
2156 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2157 template <class _Key>
2158 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2159 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k)
2162 iterator __i = find(__k);
2165 iterator __e = end();
2170 } while (__i != __e && key_eq()(*__i, __k));
2175 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2176 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2177 __hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT
2180 __node_pointer __cn = __p.__node_;
2181 size_type __bc = bucket_count();
2182 size_t __chash = __constrain_hash(__cn->__hash_, __bc);
2183 // find previous node
2184 __node_pointer __pn = __bucket_list_[__chash];
2185 for (; __pn->__next_ != __cn; __pn = __pn->__next_)
2187 // Fix up __bucket_list_
2188 // if __pn is not in same bucket (before begin is not in same bucket) &&
2189 // if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
2190 if (__pn == static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()))
2191 || __constrain_hash(__pn->__hash_, __bc) != __chash)
2193 if (__cn->__next_ == nullptr || __constrain_hash(__cn->__next_->__hash_, __bc) != __chash)
2194 __bucket_list_[__chash] = nullptr;
2196 // if __cn->__next_ is not in same bucket (nullptr is in same bucket)
2197 if (__cn->__next_ != nullptr)
2199 size_t __nhash = __constrain_hash(__cn->__next_->__hash_, __bc);
2200 if (__nhash != __chash)
2201 __bucket_list_[__nhash] = __pn;
2204 __pn->__next_ = __cn->__next_;
2205 __cn->__next_ = nullptr;
2207 #if _LIBCPP_DEBUG_LEVEL >= 2
2208 __c_node* __c = __get_db()->__find_c_and_lock(this);
2209 for (__i_node** __p = __c->end_; __p != __c->beg_; )
2212 iterator* __i = static_cast<iterator*>((*__p)->__i_);
2213 if (__i->__node_ == __cn)
2215 (*__p)->__c_ = nullptr;
2216 if (--__c->end_ != __p)
2217 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2220 __get_db()->unlock();
2222 return __node_holder(__cn, _Dp(__node_alloc(), true));
2225 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2226 template <class _Key>
2228 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2229 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const
2231 return static_cast<size_type>(find(__k) != end());
2234 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2235 template <class _Key>
2236 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2237 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const
2240 const_iterator __i = find(__k);
2243 const_iterator __e = end();
2248 } while (__i != __e && key_eq()(*__i, __k));
2253 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2254 template <class _Key>
2255 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2256 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2257 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2260 iterator __i = find(__k);
2264 return pair<iterator, iterator>(__i, __j);
2267 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2268 template <class _Key>
2269 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2270 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2271 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2272 const _Key& __k) const
2274 const_iterator __i = find(__k);
2275 const_iterator __j = __i;
2278 return pair<const_iterator, const_iterator>(__i, __j);
2281 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2282 template <class _Key>
2283 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2284 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2285 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2288 iterator __i = find(__k);
2292 iterator __e = end();
2296 } while (__j != __e && key_eq()(*__j, __k));
2298 return pair<iterator, iterator>(__i, __j);
2301 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2302 template <class _Key>
2303 pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2304 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2305 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2306 const _Key& __k) const
2308 const_iterator __i = find(__k);
2309 const_iterator __j = __i;
2312 const_iterator __e = end();
2316 } while (__j != __e && key_eq()(*__j, __k));
2318 return pair<const_iterator, const_iterator>(__i, __j);
2321 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2323 __hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
2324 #if _LIBCPP_STD_VER <= 11
2326 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
2327 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
2328 || __is_nothrow_swappable<__pointer_allocator>::value)
2329 && (!__node_traits::propagate_on_container_swap::value
2330 || __is_nothrow_swappable<__node_allocator>::value)
2333 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value)
2337 __node_pointer_pointer __npp = __bucket_list_.release();
2338 __bucket_list_.reset(__u.__bucket_list_.release());
2339 __u.__bucket_list_.reset(__npp);
2341 _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
2342 __swap_allocator(__bucket_list_.get_deleter().__alloc(),
2343 __u.__bucket_list_.get_deleter().__alloc());
2344 __swap_allocator(__node_alloc(), __u.__node_alloc());
2345 _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
2346 __p2_.swap(__u.__p2_);
2347 __p3_.swap(__u.__p3_);
2349 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
2350 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
2352 __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash_, __u.bucket_count())] =
2353 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__u.__p1_.first()));
2354 #if _LIBCPP_DEBUG_LEVEL >= 2
2355 __get_db()->swap(this, &__u);
2359 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2360 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2361 __hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const
2363 _LIBCPP_ASSERT(__n < bucket_count(),
2364 "unordered container::bucket_size(n) called with n >= bucket_count()");
2365 __node_const_pointer __np = __bucket_list_[__n];
2366 size_type __bc = bucket_count();
2368 if (__np != nullptr)
2370 for (__np = __np->__next_; __np != nullptr &&
2371 __constrain_hash(__np->__hash_, __bc) == __n;
2372 __np = __np->__next_, ++__r)
2378 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2379 inline _LIBCPP_INLINE_VISIBILITY
2381 swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x,
2382 __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
2383 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2388 #if _LIBCPP_DEBUG_LEVEL >= 2
2390 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2392 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const
2394 return __i->__node_ != nullptr;
2397 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2399 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const
2404 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2406 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2411 template <class _Tp, class _Hash, class _Equal, class _Alloc>
2413 __hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2418 #endif // _LIBCPP_DEBUG_LEVEL >= 2
2419 _LIBCPP_END_NAMESPACE_STD
2421 #endif // _LIBCPP__HASH_TABLE