1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4//                     The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP__HASH_TABLE
12#define _LIBCPP__HASH_TABLE
13
14#include <__config>
15#include <initializer_list>
16#include <memory>
17#include <iterator>
18#include <algorithm>
19#include <cmath>
20#include <utility>
21
22#include <__undef_min_max>
23#include <__undef___deallocate>
24
25#include <__debug>
26
27#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28#pragma GCC system_header
29#endif
30
31_LIBCPP_BEGIN_NAMESPACE_STD
32
33
34#ifndef _LIBCPP_CXX03_LANG
35template <class _Key, class _Tp>
36union __hash_value_type;
37#else
38template <class _Key, class _Tp>
39struct __hash_value_type;
40#endif
41
42#ifndef _LIBCPP_CXX03_LANG
43template <class _Tp>
44struct __is_hash_value_type_imp : false_type {};
45
46template <class _Key, class _Value>
47struct __is_hash_value_type_imp<__hash_value_type<_Key, _Value>> : true_type {};
48
49template <class ..._Args>
50struct __is_hash_value_type : false_type {};
51
52template <class _One>
53struct __is_hash_value_type<_One> : __is_hash_value_type_imp<typename __uncvref<_One>::type> {};
54#endif
55
56_LIBCPP_FUNC_VIS
57size_t __next_prime(size_t __n);
58
59template <class _NodePtr>
60struct __hash_node_base
61{
62    typedef typename pointer_traits<_NodePtr>::element_type __node_type;
63    typedef __hash_node_base __first_node;
64    typedef typename __rebind_pointer<_NodePtr, __first_node>::type __node_base_pointer;
65    typedef _NodePtr __node_pointer;
66
67#if defined(_LIBCPP_ABI_FIX_UNORDERED_NODE_POINTER_UB)
68  typedef __node_base_pointer __next_pointer;
69#else
70  typedef typename conditional<
71      is_pointer<__node_pointer>::value,
72      __node_base_pointer,
73      __node_pointer>::type   __next_pointer;
74#endif
75
76    __next_pointer    __next_;
77
78    _LIBCPP_INLINE_VISIBILITY
79    __next_pointer __ptr() _NOEXCEPT {
80        return static_cast<__next_pointer>(
81            pointer_traits<__node_base_pointer>::pointer_to(*this));
82    }
83
84    _LIBCPP_INLINE_VISIBILITY
85    __node_pointer __upcast() _NOEXCEPT {
86        return static_cast<__node_pointer>(
87            pointer_traits<__node_base_pointer>::pointer_to(*this));
88    }
89
90    _LIBCPP_INLINE_VISIBILITY
91    size_t __hash() const _NOEXCEPT {
92        return static_cast<__node_type const&>(*this).__hash_;
93    }
94
95    _LIBCPP_INLINE_VISIBILITY __hash_node_base() _NOEXCEPT : __next_(nullptr) {}
96};
97
98template <class _Tp, class _VoidPtr>
99struct __hash_node
100    : public __hash_node_base
101             <
102                 typename __rebind_pointer<_VoidPtr, __hash_node<_Tp, _VoidPtr> >::type
103             >
104{
105    typedef _Tp __node_value_type;
106
107    size_t            __hash_;
108    __node_value_type __value_;
109};
110
111inline _LIBCPP_INLINE_VISIBILITY
112bool
113__is_hash_power2(size_t __bc)
114{
115    return __bc > 2 && !(__bc & (__bc - 1));
116}
117
118inline _LIBCPP_INLINE_VISIBILITY
119size_t
120__constrain_hash(size_t __h, size_t __bc)
121{
122    return !(__bc & (__bc - 1)) ? __h & (__bc - 1) :
123        (__h < __bc ? __h : __h % __bc);
124}
125
126inline _LIBCPP_INLINE_VISIBILITY
127size_t
128__next_hash_pow2(size_t __n)
129{
130    return size_t(1) << (std::numeric_limits<size_t>::digits - __clz(__n-1));
131}
132
133
134template <class _Tp, class _Hash, class _Equal, class _Alloc> class __hash_table;
135
136template <class _NodePtr>      class _LIBCPP_TYPE_VIS_ONLY __hash_iterator;
137template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator;
138template <class _NodePtr>      class _LIBCPP_TYPE_VIS_ONLY __hash_local_iterator;
139template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
140template <class _HashIterator> class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
141template <class _HashIterator> class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
142
143template <class _Tp>
144struct __hash_key_value_types {
145  static_assert(!is_reference<_Tp>::value && !is_const<_Tp>::value, "");
146  typedef _Tp key_type;
147  typedef _Tp __node_value_type;
148  typedef _Tp __container_value_type;
149  static const bool __is_map = false;
150
151  _LIBCPP_INLINE_VISIBILITY
152  static key_type const& __get_key(_Tp const& __v) {
153    return __v;
154  }
155  _LIBCPP_INLINE_VISIBILITY
156  static __container_value_type const& __get_value(__node_value_type const& __v) {
157    return __v;
158  }
159  _LIBCPP_INLINE_VISIBILITY
160  static __container_value_type* __get_ptr(__node_value_type& __n) {
161    return _VSTD::addressof(__n);
162  }
163#ifndef _LIBCPP_CXX03_LANG
164  _LIBCPP_INLINE_VISIBILITY
165  static  __container_value_type&& __move(__node_value_type& __v) {
166    return _VSTD::move(__v);
167  }
168#endif
169};
170
171template <class _Key, class _Tp>
172struct __hash_key_value_types<__hash_value_type<_Key, _Tp> > {
173  typedef _Key                                         key_type;
174  typedef _Tp                                          mapped_type;
175  typedef __hash_value_type<_Key, _Tp>                 __node_value_type;
176  typedef pair<const _Key, _Tp>                        __container_value_type;
177  typedef pair<_Key, _Tp>                              __nc_value_type;
178  typedef __container_value_type                       __map_value_type;
179  static const bool __is_map = true;
180
181  _LIBCPP_INLINE_VISIBILITY
182  static key_type const& __get_key(__container_value_type const& __v) {
183    return __v.first;
184  }
185
186  template <class _Up>
187  _LIBCPP_INLINE_VISIBILITY
188  static typename enable_if<__is_same_uncvref<_Up, __node_value_type>::value,
189      __container_value_type const&>::type
190  __get_value(_Up& __t) {
191    return __t.__cc;
192  }
193
194  template <class _Up>
195  _LIBCPP_INLINE_VISIBILITY
196  static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
197      __container_value_type const&>::type
198  __get_value(_Up& __t) {
199    return __t;
200  }
201
202  _LIBCPP_INLINE_VISIBILITY
203  static __container_value_type* __get_ptr(__node_value_type& __n) {
204    return _VSTD::addressof(__n.__cc);
205  }
206#ifndef _LIBCPP_CXX03_LANG
207  _LIBCPP_INLINE_VISIBILITY
208  static __nc_value_type&& __move(__node_value_type& __v) {
209    return _VSTD::move(__v.__nc);
210  }
211#endif
212
213};
214
215template <class _Tp, class _AllocPtr, class _KVTypes = __hash_key_value_types<_Tp>,
216          bool = _KVTypes::__is_map>
217struct __hash_map_pointer_types {};
218
219template <class _Tp, class _AllocPtr, class _KVTypes>
220struct __hash_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
221  typedef typename _KVTypes::__map_value_type   _Mv;
222  typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
223                                                       __map_value_type_pointer;
224  typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
225                                                 __const_map_value_type_pointer;
226};
227
228template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
229struct __hash_node_types;
230
231template <class _NodePtr, class _Tp, class _VoidPtr>
232struct __hash_node_types<_NodePtr, __hash_node<_Tp, _VoidPtr> >
233    : public __hash_key_value_types<_Tp>, __hash_map_pointer_types<_Tp, _VoidPtr>
234
235{
236  typedef __hash_key_value_types<_Tp>           __base;
237
238public:
239  typedef ptrdiff_t difference_type;
240  typedef size_t size_type;
241
242  typedef typename __rebind_pointer<_NodePtr, void>::type       __void_pointer;
243
244  typedef typename pointer_traits<_NodePtr>::element_type       __node_type;
245  typedef _NodePtr                                              __node_pointer;
246
247  typedef __hash_node_base<__node_pointer>                      __node_base_type;
248  typedef typename __rebind_pointer<_NodePtr, __node_base_type>::type
249                                                             __node_base_pointer;
250
251  typedef typename __node_base_type::__next_pointer          __next_pointer;
252
253  typedef _Tp                                                 __node_value_type;
254  typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
255                                                      __node_value_type_pointer;
256  typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
257                                                __const_node_value_type_pointer;
258
259private:
260    static_assert(!is_const<__node_type>::value,
261                "_NodePtr should never be a pointer to const");
262    static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
263                  "_VoidPtr does not point to unqualified void type");
264    static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
265                          _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
266};
267
268template <class _HashIterator>
269struct __hash_node_types_from_iterator;
270template <class _NodePtr>
271struct __hash_node_types_from_iterator<__hash_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
272template <class _NodePtr>
273struct __hash_node_types_from_iterator<__hash_const_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
274template <class _NodePtr>
275struct __hash_node_types_from_iterator<__hash_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
276template <class _NodePtr>
277struct __hash_node_types_from_iterator<__hash_const_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
278
279
280template <class _NodeValueTp, class _VoidPtr>
281struct __make_hash_node_types {
282  typedef __hash_node<_NodeValueTp, _VoidPtr> _NodeTp;
283  typedef typename __rebind_pointer<_VoidPtr, _NodeTp>::type _NodePtr;
284  typedef __hash_node_types<_NodePtr> type;
285};
286
287template <class _NodePtr>
288class _LIBCPP_TYPE_VIS_ONLY __hash_iterator
289{
290    typedef __hash_node_types<_NodePtr> _NodeTypes;
291    typedef _NodePtr                            __node_pointer;
292    typedef typename _NodeTypes::__next_pointer __next_pointer;
293
294    __next_pointer            __node_;
295
296public:
297    typedef forward_iterator_tag                           iterator_category;
298    typedef typename _NodeTypes::__node_value_type         value_type;
299    typedef typename _NodeTypes::difference_type           difference_type;
300    typedef value_type&                                    reference;
301    typedef typename _NodeTypes::__node_value_type_pointer pointer;
302
303    _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT : __node_(nullptr) {
304        _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
305    }
306
307#if _LIBCPP_DEBUG_LEVEL >= 2
308    _LIBCPP_INLINE_VISIBILITY
309    __hash_iterator(const __hash_iterator& __i)
310        : __node_(__i.__node_)
311    {
312        __get_db()->__iterator_copy(this, &__i);
313    }
314
315    _LIBCPP_INLINE_VISIBILITY
316    ~__hash_iterator()
317    {
318        __get_db()->__erase_i(this);
319    }
320
321    _LIBCPP_INLINE_VISIBILITY
322    __hash_iterator& operator=(const __hash_iterator& __i)
323    {
324        if (this != &__i)
325        {
326            __get_db()->__iterator_copy(this, &__i);
327            __node_ = __i.__node_;
328        }
329        return *this;
330    }
331#endif  // _LIBCPP_DEBUG_LEVEL >= 2
332
333    _LIBCPP_INLINE_VISIBILITY
334    reference operator*() const {
335        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
336                             "Attempted to dereference a non-dereferenceable unordered container iterator");
337        return __node_->__upcast()->__value_;
338    }
339
340    _LIBCPP_INLINE_VISIBILITY
341    pointer operator->() const {
342        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
343                           "Attempted to dereference a non-dereferenceable unordered container iterator");
344        return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
345    }
346
347    _LIBCPP_INLINE_VISIBILITY
348    __hash_iterator& operator++() {
349        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
350                       "Attempted to increment non-incrementable unordered container iterator");
351        __node_ = __node_->__next_;
352        return *this;
353    }
354
355    _LIBCPP_INLINE_VISIBILITY
356    __hash_iterator operator++(int)
357    {
358        __hash_iterator __t(*this);
359        ++(*this);
360        return __t;
361    }
362
363    friend _LIBCPP_INLINE_VISIBILITY
364    bool operator==(const __hash_iterator& __x, const __hash_iterator& __y)
365    {
366        return __x.__node_ == __y.__node_;
367    }
368    friend _LIBCPP_INLINE_VISIBILITY
369    bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y)
370        {return !(__x == __y);}
371
372private:
373#if _LIBCPP_DEBUG_LEVEL >= 2
374    _LIBCPP_INLINE_VISIBILITY
375    __hash_iterator(__next_pointer __node, const void* __c) _NOEXCEPT
376        : __node_(__node)
377        {
378            __get_db()->__insert_ic(this, __c);
379        }
380#else
381    _LIBCPP_INLINE_VISIBILITY
382    __hash_iterator(__next_pointer __node) _NOEXCEPT
383        : __node_(__node)
384        {}
385#endif
386    template <class, class, class, class> friend class __hash_table;
387    template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator;
388    template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
389    template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
390    template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
391};
392
393template <class _NodePtr>
394class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator
395{
396    static_assert(!is_const<typename pointer_traits<_NodePtr>::element_type>::value, "");
397    typedef __hash_node_types<_NodePtr> _NodeTypes;
398    typedef _NodePtr                            __node_pointer;
399    typedef typename _NodeTypes::__next_pointer __next_pointer;
400
401    __next_pointer __node_;
402
403public:
404    typedef __hash_iterator<_NodePtr> __non_const_iterator;
405
406    typedef forward_iterator_tag                                 iterator_category;
407    typedef typename _NodeTypes::__node_value_type               value_type;
408    typedef typename _NodeTypes::difference_type                 difference_type;
409    typedef const value_type&                                    reference;
410    typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
411
412
413    _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT : __node_(nullptr) {
414        _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
415    }
416
417    _LIBCPP_INLINE_VISIBILITY
418    __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT
419        : __node_(__x.__node_)
420    {
421        _LIBCPP_DEBUG_MODE(__get_db()->__iterator_copy(this, &__x));
422    }
423
424#if _LIBCPP_DEBUG_LEVEL >= 2
425    _LIBCPP_INLINE_VISIBILITY
426    __hash_const_iterator(const __hash_const_iterator& __i)
427        : __node_(__i.__node_)
428    {
429        __get_db()->__iterator_copy(this, &__i);
430    }
431
432    _LIBCPP_INLINE_VISIBILITY
433    ~__hash_const_iterator()
434    {
435        __get_db()->__erase_i(this);
436    }
437
438    _LIBCPP_INLINE_VISIBILITY
439    __hash_const_iterator& operator=(const __hash_const_iterator& __i)
440    {
441        if (this != &__i)
442        {
443            __get_db()->__iterator_copy(this, &__i);
444            __node_ = __i.__node_;
445        }
446        return *this;
447    }
448#endif  // _LIBCPP_DEBUG_LEVEL >= 2
449
450    _LIBCPP_INLINE_VISIBILITY
451    reference operator*() const {
452        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
453                           "Attempted to dereference a non-dereferenceable unordered container const_iterator");
454        return __node_->__upcast()->__value_;
455    }
456    _LIBCPP_INLINE_VISIBILITY
457    pointer operator->() const {
458        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
459                           "Attempted to dereference a non-dereferenceable unordered container const_iterator");
460        return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
461    }
462
463    _LIBCPP_INLINE_VISIBILITY
464    __hash_const_iterator& operator++() {
465        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
466                             "Attempted to increment non-incrementable unordered container const_iterator");
467        __node_ = __node_->__next_;
468        return *this;
469    }
470
471    _LIBCPP_INLINE_VISIBILITY
472    __hash_const_iterator operator++(int)
473    {
474        __hash_const_iterator __t(*this);
475        ++(*this);
476        return __t;
477    }
478
479    friend _LIBCPP_INLINE_VISIBILITY
480    bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
481    {
482        return __x.__node_ == __y.__node_;
483    }
484    friend _LIBCPP_INLINE_VISIBILITY
485    bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
486        {return !(__x == __y);}
487
488private:
489#if _LIBCPP_DEBUG_LEVEL >= 2
490    _LIBCPP_INLINE_VISIBILITY
491    __hash_const_iterator(__next_pointer __node, const void* __c) _NOEXCEPT
492        : __node_(__node)
493        {
494            __get_db()->__insert_ic(this, __c);
495        }
496#else
497    _LIBCPP_INLINE_VISIBILITY
498    __hash_const_iterator(__next_pointer __node) _NOEXCEPT
499        : __node_(__node)
500        {}
501#endif
502    template <class, class, class, class> friend class __hash_table;
503    template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
504    template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
505    template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
506};
507
508template <class _NodePtr>
509class _LIBCPP_TYPE_VIS_ONLY __hash_local_iterator
510{
511    typedef __hash_node_types<_NodePtr> _NodeTypes;
512    typedef _NodePtr                            __node_pointer;
513    typedef typename _NodeTypes::__next_pointer __next_pointer;
514
515    __next_pointer         __node_;
516    size_t                 __bucket_;
517    size_t                 __bucket_count_;
518
519public:
520    typedef forward_iterator_tag                                iterator_category;
521    typedef typename _NodeTypes::__node_value_type              value_type;
522    typedef typename _NodeTypes::difference_type                difference_type;
523    typedef value_type&                                         reference;
524    typedef typename _NodeTypes::__node_value_type_pointer      pointer;
525
526    _LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT : __node_(nullptr) {
527        _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
528    }
529
530#if _LIBCPP_DEBUG_LEVEL >= 2
531    _LIBCPP_INLINE_VISIBILITY
532    __hash_local_iterator(const __hash_local_iterator& __i)
533        : __node_(__i.__node_),
534          __bucket_(__i.__bucket_),
535          __bucket_count_(__i.__bucket_count_)
536    {
537        __get_db()->__iterator_copy(this, &__i);
538    }
539
540    _LIBCPP_INLINE_VISIBILITY
541    ~__hash_local_iterator()
542    {
543        __get_db()->__erase_i(this);
544    }
545
546    _LIBCPP_INLINE_VISIBILITY
547    __hash_local_iterator& operator=(const __hash_local_iterator& __i)
548    {
549        if (this != &__i)
550        {
551            __get_db()->__iterator_copy(this, &__i);
552            __node_ = __i.__node_;
553            __bucket_ = __i.__bucket_;
554            __bucket_count_ = __i.__bucket_count_;
555        }
556        return *this;
557    }
558#endif  // _LIBCPP_DEBUG_LEVEL >= 2
559
560    _LIBCPP_INLINE_VISIBILITY
561    reference operator*() const {
562        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
563                           "Attempted to dereference a non-dereferenceable unordered container local_iterator");
564        return __node_->__upcast()->__value_;
565    }
566
567    _LIBCPP_INLINE_VISIBILITY
568    pointer operator->() const {
569        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
570                             "Attempted to dereference a non-dereferenceable unordered container local_iterator");
571        return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
572    }
573
574    _LIBCPP_INLINE_VISIBILITY
575    __hash_local_iterator& operator++() {
576        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
577                       "Attempted to increment non-incrementable unordered container local_iterator");
578        __node_ = __node_->__next_;
579        if (__node_ != nullptr && __constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
580            __node_ = nullptr;
581        return *this;
582    }
583
584    _LIBCPP_INLINE_VISIBILITY
585    __hash_local_iterator operator++(int)
586    {
587        __hash_local_iterator __t(*this);
588        ++(*this);
589        return __t;
590    }
591
592    friend _LIBCPP_INLINE_VISIBILITY
593    bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
594    {
595        return __x.__node_ == __y.__node_;
596    }
597    friend _LIBCPP_INLINE_VISIBILITY
598    bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
599        {return !(__x == __y);}
600
601private:
602#if _LIBCPP_DEBUG_LEVEL >= 2
603    _LIBCPP_INLINE_VISIBILITY
604    __hash_local_iterator(__next_pointer __node, size_t __bucket,
605                          size_t __bucket_count, const void* __c) _NOEXCEPT
606        : __node_(__node),
607          __bucket_(__bucket),
608          __bucket_count_(__bucket_count)
609        {
610            __get_db()->__insert_ic(this, __c);
611            if (__node_ != nullptr)
612                __node_ = __node_->__next_;
613        }
614#else
615    _LIBCPP_INLINE_VISIBILITY
616    __hash_local_iterator(__next_pointer __node, size_t __bucket,
617                          size_t __bucket_count) _NOEXCEPT
618        : __node_(__node),
619          __bucket_(__bucket),
620          __bucket_count_(__bucket_count)
621        {
622            if (__node_ != nullptr)
623                __node_ = __node_->__next_;
624        }
625#endif
626    template <class, class, class, class> friend class __hash_table;
627    template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
628    template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
629};
630
631template <class _ConstNodePtr>
632class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator
633{
634    typedef __hash_node_types<_ConstNodePtr> _NodeTypes;
635    typedef _ConstNodePtr                       __node_pointer;
636    typedef typename _NodeTypes::__next_pointer __next_pointer;
637
638    __next_pointer         __node_;
639    size_t                 __bucket_;
640    size_t                 __bucket_count_;
641
642    typedef pointer_traits<__node_pointer>          __pointer_traits;
643    typedef typename __pointer_traits::element_type __node;
644    typedef typename remove_const<__node>::type     __non_const_node;
645    typedef typename __rebind_pointer<__node_pointer, __non_const_node>::type
646        __non_const_node_pointer;
647public:
648    typedef __hash_local_iterator<__non_const_node_pointer>
649                                                    __non_const_iterator;
650
651    typedef forward_iterator_tag                                 iterator_category;
652    typedef typename _NodeTypes::__node_value_type               value_type;
653    typedef typename _NodeTypes::difference_type                 difference_type;
654    typedef const value_type&                                    reference;
655    typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
656
657
658    _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT : __node_(nullptr) {
659        _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
660    }
661
662    _LIBCPP_INLINE_VISIBILITY
663    __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT
664        : __node_(__x.__node_),
665          __bucket_(__x.__bucket_),
666          __bucket_count_(__x.__bucket_count_)
667    {
668        _LIBCPP_DEBUG_MODE(__get_db()->__iterator_copy(this, &__x));
669    }
670
671#if _LIBCPP_DEBUG_LEVEL >= 2
672    _LIBCPP_INLINE_VISIBILITY
673    __hash_const_local_iterator(const __hash_const_local_iterator& __i)
674        : __node_(__i.__node_),
675          __bucket_(__i.__bucket_),
676          __bucket_count_(__i.__bucket_count_)
677    {
678        __get_db()->__iterator_copy(this, &__i);
679    }
680
681    _LIBCPP_INLINE_VISIBILITY
682    ~__hash_const_local_iterator()
683    {
684        __get_db()->__erase_i(this);
685    }
686
687    _LIBCPP_INLINE_VISIBILITY
688    __hash_const_local_iterator& operator=(const __hash_const_local_iterator& __i)
689    {
690        if (this != &__i)
691        {
692            __get_db()->__iterator_copy(this, &__i);
693            __node_ = __i.__node_;
694            __bucket_ = __i.__bucket_;
695            __bucket_count_ = __i.__bucket_count_;
696        }
697        return *this;
698    }
699#endif  // _LIBCPP_DEBUG_LEVEL >= 2
700
701    _LIBCPP_INLINE_VISIBILITY
702    reference operator*() const {
703        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
704                           "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
705        return __node_->__upcast()->__value_;
706    }
707
708    _LIBCPP_INLINE_VISIBILITY
709    pointer operator->() const {
710        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
711                           "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
712        return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
713    }
714
715    _LIBCPP_INLINE_VISIBILITY
716    __hash_const_local_iterator& operator++() {
717        _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
718                       "Attempted to increment non-incrementable unordered container const_local_iterator");
719        __node_ = __node_->__next_;
720        if (__node_ != nullptr && __constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
721            __node_ = nullptr;
722        return *this;
723    }
724
725    _LIBCPP_INLINE_VISIBILITY
726    __hash_const_local_iterator operator++(int)
727    {
728        __hash_const_local_iterator __t(*this);
729        ++(*this);
730        return __t;
731    }
732
733    friend _LIBCPP_INLINE_VISIBILITY
734    bool operator==(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
735    {
736        return __x.__node_ == __y.__node_;
737    }
738    friend _LIBCPP_INLINE_VISIBILITY
739    bool operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
740        {return !(__x == __y);}
741
742private:
743#if _LIBCPP_DEBUG_LEVEL >= 2
744    _LIBCPP_INLINE_VISIBILITY
745    __hash_const_local_iterator(__next_pointer __node, size_t __bucket,
746                                size_t __bucket_count, const void* __c) _NOEXCEPT
747        : __node_(__node),
748          __bucket_(__bucket),
749          __bucket_count_(__bucket_count)
750        {
751            __get_db()->__insert_ic(this, __c);
752            if (__node_ != nullptr)
753                __node_ = __node_->__next_;
754        }
755#else
756    _LIBCPP_INLINE_VISIBILITY
757    __hash_const_local_iterator(__next_pointer __node, size_t __bucket,
758                                size_t __bucket_count) _NOEXCEPT
759        : __node_(__node),
760          __bucket_(__bucket),
761          __bucket_count_(__bucket_count)
762        {
763            if (__node_ != nullptr)
764                __node_ = __node_->__next_;
765        }
766#endif
767    template <class, class, class, class> friend class __hash_table;
768    template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
769};
770
771template <class _Alloc>
772class __bucket_list_deallocator
773{
774    typedef _Alloc                                          allocator_type;
775    typedef allocator_traits<allocator_type>                __alloc_traits;
776    typedef typename __alloc_traits::size_type              size_type;
777
778    __compressed_pair<size_type, allocator_type> __data_;
779public:
780    typedef typename __alloc_traits::pointer pointer;
781
782    _LIBCPP_INLINE_VISIBILITY
783    __bucket_list_deallocator()
784        _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
785        : __data_(0) {}
786
787    _LIBCPP_INLINE_VISIBILITY
788    __bucket_list_deallocator(const allocator_type& __a, size_type __size)
789        _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
790        : __data_(__size, __a) {}
791
792#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
793
794    _LIBCPP_INLINE_VISIBILITY
795    __bucket_list_deallocator(__bucket_list_deallocator&& __x)
796        _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
797        : __data_(_VSTD::move(__x.__data_))
798    {
799        __x.size() = 0;
800    }
801
802#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
803
804    _LIBCPP_INLINE_VISIBILITY
805    size_type& size() _NOEXCEPT {return __data_.first();}
806    _LIBCPP_INLINE_VISIBILITY
807    size_type  size() const _NOEXCEPT {return __data_.first();}
808
809    _LIBCPP_INLINE_VISIBILITY
810    allocator_type& __alloc() _NOEXCEPT {return __data_.second();}
811    _LIBCPP_INLINE_VISIBILITY
812    const allocator_type& __alloc() const _NOEXCEPT {return __data_.second();}
813
814    _LIBCPP_INLINE_VISIBILITY
815    void operator()(pointer __p) _NOEXCEPT
816    {
817        __alloc_traits::deallocate(__alloc(), __p, size());
818    }
819};
820
821template <class _Alloc> class __hash_map_node_destructor;
822
823template <class _Alloc>
824class __hash_node_destructor
825{
826    typedef _Alloc                                          allocator_type;
827    typedef allocator_traits<allocator_type>                __alloc_traits;
828
829public:
830    typedef typename __alloc_traits::pointer                pointer;
831private:
832    typedef __hash_node_types<pointer> _NodeTypes;
833
834    allocator_type& __na_;
835
836    __hash_node_destructor& operator=(const __hash_node_destructor&);
837
838public:
839    bool __value_constructed;
840
841    _LIBCPP_INLINE_VISIBILITY
842    explicit __hash_node_destructor(allocator_type& __na,
843                                    bool __constructed = false) _NOEXCEPT
844        : __na_(__na),
845          __value_constructed(__constructed)
846        {}
847
848    _LIBCPP_INLINE_VISIBILITY
849    void operator()(pointer __p) _NOEXCEPT
850    {
851        if (__value_constructed)
852            __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
853        if (__p)
854            __alloc_traits::deallocate(__na_, __p, 1);
855    }
856
857    template <class> friend class __hash_map_node_destructor;
858};
859
860template <class _Tp, class _Hash, class _Equal, class _Alloc>
861class __hash_table
862{
863public:
864    typedef _Tp    value_type;
865    typedef _Hash  hasher;
866    typedef _Equal key_equal;
867    typedef _Alloc allocator_type;
868
869private:
870    typedef allocator_traits<allocator_type> __alloc_traits;
871    typedef typename
872      __make_hash_node_types<value_type, typename __alloc_traits::void_pointer>::type
873                                                                     _NodeTypes;
874public:
875
876    typedef typename _NodeTypes::__node_value_type           __node_value_type;
877    typedef typename _NodeTypes::__container_value_type      __container_value_type;
878    typedef typename _NodeTypes::key_type                    key_type;
879    typedef value_type&                              reference;
880    typedef const value_type&                        const_reference;
881    typedef typename __alloc_traits::pointer         pointer;
882    typedef typename __alloc_traits::const_pointer   const_pointer;
883#ifndef _LIBCPP_ABI_FIX_UNORDERED_CONTAINER_SIZE_TYPE
884    typedef typename __alloc_traits::size_type       size_type;
885#else
886    typedef typename _NodeTypes::size_type           size_type;
887#endif
888    typedef typename _NodeTypes::difference_type     difference_type;
889public:
890    // Create __node
891
892    typedef typename _NodeTypes::__node_type __node;
893    typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
894    typedef allocator_traits<__node_allocator>       __node_traits;
895    typedef typename _NodeTypes::__void_pointer      __void_pointer;
896    typedef typename _NodeTypes::__node_pointer      __node_pointer;
897    typedef typename _NodeTypes::__node_pointer      __node_const_pointer;
898    typedef typename _NodeTypes::__node_base_type    __first_node;
899    typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
900    typedef typename _NodeTypes::__next_pointer      __next_pointer;
901
902private:
903    // check for sane allocator pointer rebinding semantics. Rebinding the
904    // allocator for a new pointer type should be exactly the same as rebinding
905    // the pointer using 'pointer_traits'.
906    static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
907                  "Allocator does not rebind pointers in a sane manner.");
908    typedef typename __rebind_alloc_helper<__node_traits, __first_node>::type
909        __node_base_allocator;
910    typedef allocator_traits<__node_base_allocator> __node_base_traits;
911    static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
912                 "Allocator does not rebind pointers in a sane manner.");
913
914private:
915
916    typedef typename __rebind_alloc_helper<__node_traits, __next_pointer>::type __pointer_allocator;
917    typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter;
918    typedef unique_ptr<__next_pointer[], __bucket_list_deleter> __bucket_list;
919    typedef allocator_traits<__pointer_allocator>          __pointer_alloc_traits;
920    typedef typename __bucket_list_deleter::pointer       __node_pointer_pointer;
921
922    // --- Member data begin ---
923    __bucket_list                                         __bucket_list_;
924    __compressed_pair<__first_node, __node_allocator>     __p1_;
925    __compressed_pair<size_type, hasher>                  __p2_;
926    __compressed_pair<float, key_equal>                   __p3_;
927    // --- Member data end ---
928
929    _LIBCPP_INLINE_VISIBILITY
930    size_type& size() _NOEXCEPT {return __p2_.first();}
931public:
932    _LIBCPP_INLINE_VISIBILITY
933    size_type  size() const _NOEXCEPT {return __p2_.first();}
934
935    _LIBCPP_INLINE_VISIBILITY
936    hasher& hash_function() _NOEXCEPT {return __p2_.second();}
937    _LIBCPP_INLINE_VISIBILITY
938    const hasher& hash_function() const _NOEXCEPT {return __p2_.second();}
939
940    _LIBCPP_INLINE_VISIBILITY
941    float& max_load_factor() _NOEXCEPT {return __p3_.first();}
942    _LIBCPP_INLINE_VISIBILITY
943    float  max_load_factor() const _NOEXCEPT {return __p3_.first();}
944
945    _LIBCPP_INLINE_VISIBILITY
946    key_equal& key_eq() _NOEXCEPT {return __p3_.second();}
947    _LIBCPP_INLINE_VISIBILITY
948    const key_equal& key_eq() const _NOEXCEPT {return __p3_.second();}
949
950    _LIBCPP_INLINE_VISIBILITY
951    __node_allocator& __node_alloc() _NOEXCEPT {return __p1_.second();}
952    _LIBCPP_INLINE_VISIBILITY
953    const __node_allocator& __node_alloc() const _NOEXCEPT
954        {return __p1_.second();}
955
956public:
957    typedef __hash_iterator<__node_pointer>                   iterator;
958    typedef __hash_const_iterator<__node_pointer>             const_iterator;
959    typedef __hash_local_iterator<__node_pointer>             local_iterator;
960    typedef __hash_const_local_iterator<__node_pointer>       const_local_iterator;
961
962    _LIBCPP_INLINE_VISIBILITY
963    __hash_table()
964        _NOEXCEPT_(
965            is_nothrow_default_constructible<__bucket_list>::value &&
966            is_nothrow_default_constructible<__first_node>::value &&
967            is_nothrow_default_constructible<__node_allocator>::value &&
968            is_nothrow_default_constructible<hasher>::value &&
969            is_nothrow_default_constructible<key_equal>::value);
970    _LIBCPP_INLINE_VISIBILITY
971    __hash_table(const hasher& __hf, const key_equal& __eql);
972    __hash_table(const hasher& __hf, const key_equal& __eql,
973                 const allocator_type& __a);
974    explicit __hash_table(const allocator_type& __a);
975    __hash_table(const __hash_table& __u);
976    __hash_table(const __hash_table& __u, const allocator_type& __a);
977#ifndef _LIBCPP_CXX03_LANG
978    __hash_table(__hash_table&& __u)
979        _NOEXCEPT_(
980            is_nothrow_move_constructible<__bucket_list>::value &&
981            is_nothrow_move_constructible<__first_node>::value &&
982            is_nothrow_move_constructible<__node_allocator>::value &&
983            is_nothrow_move_constructible<hasher>::value &&
984            is_nothrow_move_constructible<key_equal>::value);
985    __hash_table(__hash_table&& __u, const allocator_type& __a);
986#endif  // _LIBCPP_CXX03_LANG
987    ~__hash_table();
988
989    __hash_table& operator=(const __hash_table& __u);
990#ifndef _LIBCPP_CXX03_LANG
991    _LIBCPP_INLINE_VISIBILITY
992    __hash_table& operator=(__hash_table&& __u)
993        _NOEXCEPT_(
994            __node_traits::propagate_on_container_move_assignment::value &&
995            is_nothrow_move_assignable<__node_allocator>::value &&
996            is_nothrow_move_assignable<hasher>::value &&
997            is_nothrow_move_assignable<key_equal>::value);
998#endif
999    template <class _InputIterator>
1000        void __assign_unique(_InputIterator __first, _InputIterator __last);
1001    template <class _InputIterator>
1002        void __assign_multi(_InputIterator __first, _InputIterator __last);
1003
1004    _LIBCPP_INLINE_VISIBILITY
1005    size_type max_size() const _NOEXCEPT
1006    {
1007        return allocator_traits<__pointer_allocator>::max_size(
1008            __bucket_list_.get_deleter().__alloc());
1009    }
1010
1011    pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1012    iterator             __node_insert_multi(__node_pointer __nd);
1013    iterator             __node_insert_multi(const_iterator __p,
1014                                             __node_pointer __nd);
1015
1016#ifndef _LIBCPP_CXX03_LANG
1017    template <class _Key, class ..._Args>
1018    _LIBCPP_INLINE_VISIBILITY
1019    pair<iterator, bool> __emplace_unique_key_args(_Key const& __k, _Args&&... __args);
1020
1021    template <class... _Args>
1022    _LIBCPP_INLINE_VISIBILITY
1023    pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
1024
1025    template <class _Pp>
1026    _LIBCPP_INLINE_VISIBILITY
1027    pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1028      return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1029                                          __can_extract_key<_Pp, key_type>());
1030    }
1031
1032    template <class _First, class _Second>
1033    _LIBCPP_INLINE_VISIBILITY
1034    typename enable_if<
1035        __can_extract_map_key<_First, key_type, __container_value_type>::value,
1036        pair<iterator, bool>
1037    >::type __emplace_unique(_First&& __f, _Second&& __s) {
1038        return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
1039                                              _VSTD::forward<_Second>(__s));
1040    }
1041
1042    template <class... _Args>
1043    _LIBCPP_INLINE_VISIBILITY
1044    pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1045      return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1046    }
1047
1048    template <class _Pp>
1049    _LIBCPP_INLINE_VISIBILITY
1050    pair<iterator, bool>
1051    __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1052      return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1053    }
1054    template <class _Pp>
1055    _LIBCPP_INLINE_VISIBILITY
1056    pair<iterator, bool>
1057    __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1058      return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1059    }
1060    template <class _Pp>
1061    _LIBCPP_INLINE_VISIBILITY
1062    pair<iterator, bool>
1063    __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1064      return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1065    }
1066
1067    template <class... _Args>
1068    _LIBCPP_INLINE_VISIBILITY
1069    iterator __emplace_multi(_Args&&... __args);
1070    template <class... _Args>
1071    _LIBCPP_INLINE_VISIBILITY
1072    iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
1073
1074
1075    _LIBCPP_INLINE_VISIBILITY
1076    pair<iterator, bool>
1077    __insert_unique(__container_value_type&& __x) {
1078      return __emplace_unique_key_args(_NodeTypes::__get_key(__x), _VSTD::move(__x));
1079    }
1080
1081    template <class _Pp, class = typename enable_if<
1082            !__is_same_uncvref<_Pp, __container_value_type>::value
1083        >::type>
1084    _LIBCPP_INLINE_VISIBILITY
1085    pair<iterator, bool> __insert_unique(_Pp&& __x) {
1086      return __emplace_unique(_VSTD::forward<_Pp>(__x));
1087    }
1088
1089    template <class _Pp>
1090    _LIBCPP_INLINE_VISIBILITY
1091    iterator __insert_multi(_Pp&& __x) {
1092      return __emplace_multi(_VSTD::forward<_Pp>(__x));
1093    }
1094
1095    template <class _Pp>
1096    _LIBCPP_INLINE_VISIBILITY
1097    iterator __insert_multi(const_iterator __p, _Pp&& __x) {
1098        return __emplace_hint_multi(__p, _VSTD::forward<_Pp>(__x));
1099    }
1100
1101#else  // !defined(_LIBCPP_CXX03_LANG)
1102    template <class _Key, class _Args>
1103    _LIBCPP_INLINE_VISIBILITY
1104    pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1105
1106    iterator __insert_multi(const __container_value_type& __x);
1107    iterator __insert_multi(const_iterator __p, const __container_value_type& __x);
1108#endif
1109
1110    _LIBCPP_INLINE_VISIBILITY
1111    pair<iterator, bool> __insert_unique(const __container_value_type& __x) {
1112        return __emplace_unique_key_args(_NodeTypes::__get_key(__x), __x);
1113    }
1114
1115    void clear() _NOEXCEPT;
1116    void rehash(size_type __n);
1117    _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n)
1118        {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));}
1119
1120    _LIBCPP_INLINE_VISIBILITY
1121    size_type bucket_count() const _NOEXCEPT
1122    {
1123        return __bucket_list_.get_deleter().size();
1124    }
1125
1126    _LIBCPP_INLINE_VISIBILITY
1127    iterator       begin() _NOEXCEPT;
1128    _LIBCPP_INLINE_VISIBILITY
1129    iterator       end() _NOEXCEPT;
1130    _LIBCPP_INLINE_VISIBILITY
1131    const_iterator begin() const _NOEXCEPT;
1132    _LIBCPP_INLINE_VISIBILITY
1133    const_iterator end() const _NOEXCEPT;
1134
1135    template <class _Key>
1136        _LIBCPP_INLINE_VISIBILITY
1137        size_type bucket(const _Key& __k) const
1138        {
1139            _LIBCPP_ASSERT(bucket_count() > 0,
1140                "unordered container::bucket(key) called when bucket_count() == 0");
1141            return __constrain_hash(hash_function()(__k), bucket_count());
1142        }
1143
1144    template <class _Key>
1145        iterator       find(const _Key& __x);
1146    template <class _Key>
1147        const_iterator find(const _Key& __x) const;
1148
1149    typedef __hash_node_destructor<__node_allocator> _Dp;
1150    typedef unique_ptr<__node, _Dp> __node_holder;
1151
1152    iterator erase(const_iterator __p);
1153    iterator erase(const_iterator __first, const_iterator __last);
1154    template <class _Key>
1155        size_type __erase_unique(const _Key& __k);
1156    template <class _Key>
1157        size_type __erase_multi(const _Key& __k);
1158    __node_holder remove(const_iterator __p) _NOEXCEPT;
1159
1160    template <class _Key>
1161        _LIBCPP_INLINE_VISIBILITY
1162        size_type __count_unique(const _Key& __k) const;
1163    template <class _Key>
1164        size_type __count_multi(const _Key& __k) const;
1165
1166    template <class _Key>
1167        pair<iterator, iterator>
1168        __equal_range_unique(const _Key& __k);
1169    template <class _Key>
1170        pair<const_iterator, const_iterator>
1171        __equal_range_unique(const _Key& __k) const;
1172
1173    template <class _Key>
1174        pair<iterator, iterator>
1175        __equal_range_multi(const _Key& __k);
1176    template <class _Key>
1177        pair<const_iterator, const_iterator>
1178        __equal_range_multi(const _Key& __k) const;
1179
1180    void swap(__hash_table& __u)
1181#if _LIBCPP_STD_VER <= 11
1182        _NOEXCEPT_(
1183            __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
1184            && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
1185                  || __is_nothrow_swappable<__pointer_allocator>::value)
1186            && (!__node_traits::propagate_on_container_swap::value
1187                  || __is_nothrow_swappable<__node_allocator>::value)
1188            );
1189#else
1190     _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value);
1191#endif
1192
1193    _LIBCPP_INLINE_VISIBILITY
1194    size_type max_bucket_count() const _NOEXCEPT
1195        {return __pointer_alloc_traits::max_size(__bucket_list_.get_deleter().__alloc());}
1196    size_type bucket_size(size_type __n) const;
1197    _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT
1198    {
1199        size_type __bc = bucket_count();
1200        return __bc != 0 ? (float)size() / __bc : 0.f;
1201    }
1202    _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT
1203    {
1204        _LIBCPP_ASSERT(__mlf > 0,
1205            "unordered container::max_load_factor(lf) called with lf <= 0");
1206        max_load_factor() = _VSTD::max(__mlf, load_factor());
1207    }
1208
1209    _LIBCPP_INLINE_VISIBILITY
1210    local_iterator
1211    begin(size_type __n)
1212    {
1213        _LIBCPP_ASSERT(__n < bucket_count(),
1214            "unordered container::begin(n) called with n >= bucket_count()");
1215#if _LIBCPP_DEBUG_LEVEL >= 2
1216        return local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1217#else
1218        return local_iterator(__bucket_list_[__n], __n, bucket_count());
1219#endif
1220    }
1221
1222    _LIBCPP_INLINE_VISIBILITY
1223    local_iterator
1224    end(size_type __n)
1225    {
1226        _LIBCPP_ASSERT(__n < bucket_count(),
1227            "unordered container::end(n) called with n >= bucket_count()");
1228#if _LIBCPP_DEBUG_LEVEL >= 2
1229        return local_iterator(nullptr, __n, bucket_count(), this);
1230#else
1231        return local_iterator(nullptr, __n, bucket_count());
1232#endif
1233    }
1234
1235    _LIBCPP_INLINE_VISIBILITY
1236    const_local_iterator
1237    cbegin(size_type __n) const
1238    {
1239        _LIBCPP_ASSERT(__n < bucket_count(),
1240            "unordered container::cbegin(n) called with n >= bucket_count()");
1241#if _LIBCPP_DEBUG_LEVEL >= 2
1242        return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1243#else
1244        return const_local_iterator(__bucket_list_[__n], __n, bucket_count());
1245#endif
1246    }
1247
1248    _LIBCPP_INLINE_VISIBILITY
1249    const_local_iterator
1250    cend(size_type __n) const
1251    {
1252        _LIBCPP_ASSERT(__n < bucket_count(),
1253            "unordered container::cend(n) called with n >= bucket_count()");
1254#if _LIBCPP_DEBUG_LEVEL >= 2
1255        return const_local_iterator(nullptr, __n, bucket_count(), this);
1256#else
1257        return const_local_iterator(nullptr, __n, bucket_count());
1258#endif
1259    }
1260
1261#if _LIBCPP_DEBUG_LEVEL >= 2
1262
1263    bool __dereferenceable(const const_iterator* __i) const;
1264    bool __decrementable(const const_iterator* __i) const;
1265    bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1266    bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1267
1268#endif  // _LIBCPP_DEBUG_LEVEL >= 2
1269
1270private:
1271    void __rehash(size_type __n);
1272
1273#ifndef _LIBCPP_CXX03_LANG
1274    template <class ..._Args>
1275    __node_holder __construct_node(_Args&& ...__args);
1276
1277    template <class _First, class ..._Rest>
1278    __node_holder __construct_node_hash(size_t __hash, _First&& __f, _Rest&&... __rest);
1279#else // _LIBCPP_CXX03_LANG
1280    __node_holder __construct_node(const __container_value_type& __v);
1281    __node_holder __construct_node_hash(size_t __hash, const __container_value_type& __v);
1282#endif
1283
1284
1285    _LIBCPP_INLINE_VISIBILITY
1286    void __copy_assign_alloc(const __hash_table& __u)
1287        {__copy_assign_alloc(__u, integral_constant<bool,
1288             __node_traits::propagate_on_container_copy_assignment::value>());}
1289    void __copy_assign_alloc(const __hash_table& __u, true_type);
1290    _LIBCPP_INLINE_VISIBILITY
1291        void __copy_assign_alloc(const __hash_table&, false_type) {}
1292
1293#ifndef _LIBCPP_CXX03_LANG
1294    void __move_assign(__hash_table& __u, false_type);
1295    void __move_assign(__hash_table& __u, true_type)
1296        _NOEXCEPT_(
1297            is_nothrow_move_assignable<__node_allocator>::value &&
1298            is_nothrow_move_assignable<hasher>::value &&
1299            is_nothrow_move_assignable<key_equal>::value);
1300    _LIBCPP_INLINE_VISIBILITY
1301    void __move_assign_alloc(__hash_table& __u)
1302        _NOEXCEPT_(
1303            !__node_traits::propagate_on_container_move_assignment::value ||
1304            (is_nothrow_move_assignable<__pointer_allocator>::value &&
1305             is_nothrow_move_assignable<__node_allocator>::value))
1306        {__move_assign_alloc(__u, integral_constant<bool,
1307             __node_traits::propagate_on_container_move_assignment::value>());}
1308    _LIBCPP_INLINE_VISIBILITY
1309    void __move_assign_alloc(__hash_table& __u, true_type)
1310        _NOEXCEPT_(
1311            is_nothrow_move_assignable<__pointer_allocator>::value &&
1312            is_nothrow_move_assignable<__node_allocator>::value)
1313    {
1314        __bucket_list_.get_deleter().__alloc() =
1315                _VSTD::move(__u.__bucket_list_.get_deleter().__alloc());
1316        __node_alloc() = _VSTD::move(__u.__node_alloc());
1317    }
1318    _LIBCPP_INLINE_VISIBILITY
1319        void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
1320#endif // _LIBCPP_CXX03_LANG
1321
1322    void __deallocate(__next_pointer __np) _NOEXCEPT;
1323    __next_pointer __detach() _NOEXCEPT;
1324
1325    template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
1326    template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
1327};
1328
1329template <class _Tp, class _Hash, class _Equal, class _Alloc>
1330inline
1331__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table()
1332    _NOEXCEPT_(
1333        is_nothrow_default_constructible<__bucket_list>::value &&
1334        is_nothrow_default_constructible<__first_node>::value &&
1335        is_nothrow_default_constructible<__node_allocator>::value &&
1336        is_nothrow_default_constructible<hasher>::value &&
1337        is_nothrow_default_constructible<key_equal>::value)
1338    : __p2_(0),
1339      __p3_(1.0f)
1340{
1341}
1342
1343template <class _Tp, class _Hash, class _Equal, class _Alloc>
1344inline
1345__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1346                                                       const key_equal& __eql)
1347    : __bucket_list_(nullptr, __bucket_list_deleter()),
1348      __p1_(),
1349      __p2_(0, __hf),
1350      __p3_(1.0f, __eql)
1351{
1352}
1353
1354template <class _Tp, class _Hash, class _Equal, class _Alloc>
1355__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1356                                                       const key_equal& __eql,
1357                                                       const allocator_type& __a)
1358    : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1359      __p1_(__node_allocator(__a)),
1360      __p2_(0, __hf),
1361      __p3_(1.0f, __eql)
1362{
1363}
1364
1365template <class _Tp, class _Hash, class _Equal, class _Alloc>
1366__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a)
1367    : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1368      __p1_(__node_allocator(__a)),
1369      __p2_(0),
1370      __p3_(1.0f)
1371{
1372}
1373
1374template <class _Tp, class _Hash, class _Equal, class _Alloc>
1375__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u)
1376    : __bucket_list_(nullptr,
1377          __bucket_list_deleter(allocator_traits<__pointer_allocator>::
1378              select_on_container_copy_construction(
1379                  __u.__bucket_list_.get_deleter().__alloc()), 0)),
1380      __p1_(allocator_traits<__node_allocator>::
1381          select_on_container_copy_construction(__u.__node_alloc())),
1382      __p2_(0, __u.hash_function()),
1383      __p3_(__u.__p3_)
1384{
1385}
1386
1387template <class _Tp, class _Hash, class _Equal, class _Alloc>
1388__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u,
1389                                                       const allocator_type& __a)
1390    : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1391      __p1_(__node_allocator(__a)),
1392      __p2_(0, __u.hash_function()),
1393      __p3_(__u.__p3_)
1394{
1395}
1396
1397#ifndef _LIBCPP_CXX03_LANG
1398
1399template <class _Tp, class _Hash, class _Equal, class _Alloc>
1400__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u)
1401        _NOEXCEPT_(
1402            is_nothrow_move_constructible<__bucket_list>::value &&
1403            is_nothrow_move_constructible<__first_node>::value &&
1404            is_nothrow_move_constructible<__node_allocator>::value &&
1405            is_nothrow_move_constructible<hasher>::value &&
1406            is_nothrow_move_constructible<key_equal>::value)
1407    : __bucket_list_(_VSTD::move(__u.__bucket_list_)),
1408      __p1_(_VSTD::move(__u.__p1_)),
1409      __p2_(_VSTD::move(__u.__p2_)),
1410      __p3_(_VSTD::move(__u.__p3_))
1411{
1412    if (size() > 0)
1413    {
1414        __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1415            __p1_.first().__ptr();
1416        __u.__p1_.first().__next_ = nullptr;
1417        __u.size() = 0;
1418    }
1419}
1420
1421template <class _Tp, class _Hash, class _Equal, class _Alloc>
1422__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u,
1423                                                       const allocator_type& __a)
1424    : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1425      __p1_(__node_allocator(__a)),
1426      __p2_(0, _VSTD::move(__u.hash_function())),
1427      __p3_(_VSTD::move(__u.__p3_))
1428{
1429    if (__a == allocator_type(__u.__node_alloc()))
1430    {
1431        __bucket_list_.reset(__u.__bucket_list_.release());
1432        __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1433        __u.__bucket_list_.get_deleter().size() = 0;
1434        if (__u.size() > 0)
1435        {
1436            __p1_.first().__next_ = __u.__p1_.first().__next_;
1437            __u.__p1_.first().__next_ = nullptr;
1438            __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1439                __p1_.first().__ptr();
1440            size() = __u.size();
1441            __u.size() = 0;
1442        }
1443    }
1444}
1445
1446#endif  // _LIBCPP_CXX03_LANG
1447
1448template <class _Tp, class _Hash, class _Equal, class _Alloc>
1449__hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table()
1450{
1451    static_assert((is_copy_constructible<key_equal>::value),
1452                 "Predicate must be copy-constructible.");
1453    static_assert((is_copy_constructible<hasher>::value),
1454                 "Hasher must be copy-constructible.");
1455    __deallocate(__p1_.first().__next_);
1456#if _LIBCPP_DEBUG_LEVEL >= 2
1457    __get_db()->__erase_c(this);
1458#endif
1459}
1460
1461template <class _Tp, class _Hash, class _Equal, class _Alloc>
1462void
1463__hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(
1464        const __hash_table& __u, true_type)
1465{
1466    if (__node_alloc() != __u.__node_alloc())
1467    {
1468        clear();
1469        __bucket_list_.reset();
1470        __bucket_list_.get_deleter().size() = 0;
1471    }
1472    __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
1473    __node_alloc() = __u.__node_alloc();
1474}
1475
1476template <class _Tp, class _Hash, class _Equal, class _Alloc>
1477__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1478__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u)
1479{
1480    if (this != &__u)
1481    {
1482        __copy_assign_alloc(__u);
1483        hash_function() = __u.hash_function();
1484        key_eq() = __u.key_eq();
1485        max_load_factor() = __u.max_load_factor();
1486        __assign_multi(__u.begin(), __u.end());
1487    }
1488    return *this;
1489}
1490
1491template <class _Tp, class _Hash, class _Equal, class _Alloc>
1492void
1493__hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate(__next_pointer __np)
1494    _NOEXCEPT
1495{
1496    __node_allocator& __na = __node_alloc();
1497    while (__np != nullptr)
1498    {
1499        __next_pointer __next = __np->__next_;
1500#if _LIBCPP_DEBUG_LEVEL >= 2
1501        __c_node* __c = __get_db()->__find_c_and_lock(this);
1502        for (__i_node** __p = __c->end_; __p != __c->beg_; )
1503        {
1504            --__p;
1505            iterator* __i = static_cast<iterator*>((*__p)->__i_);
1506            if (__i->__node_ == __np)
1507            {
1508                (*__p)->__c_ = nullptr;
1509                if (--__c->end_ != __p)
1510                    memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1511            }
1512        }
1513        __get_db()->unlock();
1514#endif
1515        __node_pointer __real_np = __np->__upcast();
1516        __node_traits::destroy(__na, _NodeTypes::__get_ptr(__real_np->__value_));
1517        __node_traits::deallocate(__na, __real_np, 1);
1518        __np = __next;
1519    }
1520}
1521
1522template <class _Tp, class _Hash, class _Equal, class _Alloc>
1523typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
1524__hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
1525{
1526    size_type __bc = bucket_count();
1527    for (size_type __i = 0; __i < __bc; ++__i)
1528        __bucket_list_[__i] = nullptr;
1529    size() = 0;
1530    __next_pointer __cache = __p1_.first().__next_;
1531    __p1_.first().__next_ = nullptr;
1532    return __cache;
1533}
1534
1535#ifndef _LIBCPP_CXX03_LANG
1536
1537template <class _Tp, class _Hash, class _Equal, class _Alloc>
1538void
1539__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1540        __hash_table& __u, true_type)
1541    _NOEXCEPT_(
1542        is_nothrow_move_assignable<__node_allocator>::value &&
1543        is_nothrow_move_assignable<hasher>::value &&
1544        is_nothrow_move_assignable<key_equal>::value)
1545{
1546    clear();
1547    __bucket_list_.reset(__u.__bucket_list_.release());
1548    __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1549    __u.__bucket_list_.get_deleter().size() = 0;
1550    __move_assign_alloc(__u);
1551    size() = __u.size();
1552    hash_function() = _VSTD::move(__u.hash_function());
1553    max_load_factor() = __u.max_load_factor();
1554    key_eq() = _VSTD::move(__u.key_eq());
1555    __p1_.first().__next_ = __u.__p1_.first().__next_;
1556    if (size() > 0)
1557    {
1558        __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1559            __p1_.first().__ptr();
1560        __u.__p1_.first().__next_ = nullptr;
1561        __u.size() = 0;
1562    }
1563#if _LIBCPP_DEBUG_LEVEL >= 2
1564    __get_db()->swap(this, &__u);
1565#endif
1566}
1567
1568template <class _Tp, class _Hash, class _Equal, class _Alloc>
1569void
1570__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1571        __hash_table& __u, false_type)
1572{
1573    if (__node_alloc() == __u.__node_alloc())
1574        __move_assign(__u, true_type());
1575    else
1576    {
1577        hash_function() = _VSTD::move(__u.hash_function());
1578        key_eq() = _VSTD::move(__u.key_eq());
1579        max_load_factor() = __u.max_load_factor();
1580        if (bucket_count() != 0)
1581        {
1582            __next_pointer __cache = __detach();
1583#ifndef _LIBCPP_NO_EXCEPTIONS
1584            try
1585            {
1586#endif  // _LIBCPP_NO_EXCEPTIONS
1587                const_iterator __i = __u.begin();
1588                while (__cache != nullptr && __u.size() != 0)
1589                {
1590                    __cache->__upcast()->__value_ =
1591                        _VSTD::move(__u.remove(__i++)->__value_);
1592                    __next_pointer __next = __cache->__next_;
1593                    __node_insert_multi(__cache->__upcast());
1594                    __cache = __next;
1595                }
1596#ifndef _LIBCPP_NO_EXCEPTIONS
1597            }
1598            catch (...)
1599            {
1600                __deallocate(__cache);
1601                throw;
1602            }
1603#endif  // _LIBCPP_NO_EXCEPTIONS
1604            __deallocate(__cache);
1605        }
1606        const_iterator __i = __u.begin();
1607        while (__u.size() != 0)
1608        {
1609            __node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__value_));
1610            __node_insert_multi(__h.get());
1611            __h.release();
1612        }
1613    }
1614}
1615
1616template <class _Tp, class _Hash, class _Equal, class _Alloc>
1617inline
1618__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1619__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
1620    _NOEXCEPT_(
1621        __node_traits::propagate_on_container_move_assignment::value &&
1622        is_nothrow_move_assignable<__node_allocator>::value &&
1623        is_nothrow_move_assignable<hasher>::value &&
1624        is_nothrow_move_assignable<key_equal>::value)
1625{
1626    __move_assign(__u, integral_constant<bool,
1627                  __node_traits::propagate_on_container_move_assignment::value>());
1628    return *this;
1629}
1630
1631#endif  // _LIBCPP_CXX03_LANG
1632
1633template <class _Tp, class _Hash, class _Equal, class _Alloc>
1634template <class _InputIterator>
1635void
1636__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first,
1637                                                          _InputIterator __last)
1638{
1639    typedef iterator_traits<_InputIterator> _ITraits;
1640    typedef typename _ITraits::value_type _ItValueType;
1641    static_assert((is_same<_ItValueType, __container_value_type>::value),
1642                  "__assign_unique may only be called with the containers value type");
1643
1644    if (bucket_count() != 0)
1645    {
1646        __next_pointer __cache = __detach();
1647#ifndef _LIBCPP_NO_EXCEPTIONS
1648        try
1649        {
1650#endif  // _LIBCPP_NO_EXCEPTIONS
1651            for (; __cache != nullptr && __first != __last; ++__first)
1652            {
1653                __cache->__upcast()->__value_ = *__first;
1654                __next_pointer __next = __cache->__next_;
1655                __node_insert_unique(__cache->__upcast());
1656                __cache = __next;
1657            }
1658#ifndef _LIBCPP_NO_EXCEPTIONS
1659        }
1660        catch (...)
1661        {
1662            __deallocate(__cache);
1663            throw;
1664        }
1665#endif  // _LIBCPP_NO_EXCEPTIONS
1666        __deallocate(__cache);
1667    }
1668    for (; __first != __last; ++__first)
1669        __insert_unique(*__first);
1670}
1671
1672template <class _Tp, class _Hash, class _Equal, class _Alloc>
1673template <class _InputIterator>
1674void
1675__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
1676                                                         _InputIterator __last)
1677{
1678    typedef iterator_traits<_InputIterator> _ITraits;
1679    typedef typename _ITraits::value_type _ItValueType;
1680    static_assert((is_same<_ItValueType, __container_value_type>::value ||
1681                  is_same<_ItValueType, __node_value_type>::value),
1682                  "__assign_multi may only be called with the containers value type"
1683                  " or the nodes value type");
1684    if (bucket_count() != 0)
1685    {
1686        __next_pointer __cache = __detach();
1687#ifndef _LIBCPP_NO_EXCEPTIONS
1688        try
1689        {
1690#endif  // _LIBCPP_NO_EXCEPTIONS
1691            for (; __cache != nullptr && __first != __last; ++__first)
1692            {
1693                __cache->__upcast()->__value_ = *__first;
1694                __next_pointer __next = __cache->__next_;
1695                __node_insert_multi(__cache->__upcast());
1696                __cache = __next;
1697            }
1698#ifndef _LIBCPP_NO_EXCEPTIONS
1699        }
1700        catch (...)
1701        {
1702            __deallocate(__cache);
1703            throw;
1704        }
1705#endif  // _LIBCPP_NO_EXCEPTIONS
1706        __deallocate(__cache);
1707    }
1708    for (; __first != __last; ++__first)
1709        __insert_multi(_NodeTypes::__get_value(*__first));
1710}
1711
1712template <class _Tp, class _Hash, class _Equal, class _Alloc>
1713inline
1714typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1715__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT
1716{
1717#if _LIBCPP_DEBUG_LEVEL >= 2
1718    return iterator(__p1_.first().__next_, this);
1719#else
1720    return iterator(__p1_.first().__next_);
1721#endif
1722}
1723
1724template <class _Tp, class _Hash, class _Equal, class _Alloc>
1725inline
1726typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1727__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT
1728{
1729#if _LIBCPP_DEBUG_LEVEL >= 2
1730    return iterator(nullptr, this);
1731#else
1732    return iterator(nullptr);
1733#endif
1734}
1735
1736template <class _Tp, class _Hash, class _Equal, class _Alloc>
1737inline
1738typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
1739__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT
1740{
1741#if _LIBCPP_DEBUG_LEVEL >= 2
1742    return const_iterator(__p1_.first().__next_, this);
1743#else
1744    return const_iterator(__p1_.first().__next_);
1745#endif
1746}
1747
1748template <class _Tp, class _Hash, class _Equal, class _Alloc>
1749inline
1750typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
1751__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT
1752{
1753#if _LIBCPP_DEBUG_LEVEL >= 2
1754    return const_iterator(nullptr, this);
1755#else
1756    return const_iterator(nullptr);
1757#endif
1758}
1759
1760template <class _Tp, class _Hash, class _Equal, class _Alloc>
1761void
1762__hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT
1763{
1764    if (size() > 0)
1765    {
1766        __deallocate(__p1_.first().__next_);
1767        __p1_.first().__next_ = nullptr;
1768        size_type __bc = bucket_count();
1769        for (size_type __i = 0; __i < __bc; ++__i)
1770            __bucket_list_[__i] = nullptr;
1771        size() = 0;
1772    }
1773}
1774
1775template <class _Tp, class _Hash, class _Equal, class _Alloc>
1776pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1777__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd)
1778{
1779    __nd->__hash_ = hash_function()(__nd->__value_);
1780    size_type __bc = bucket_count();
1781    bool __inserted = false;
1782    __next_pointer __ndptr;
1783    size_t __chash;
1784    if (__bc != 0)
1785    {
1786        __chash = __constrain_hash(__nd->__hash_, __bc);
1787        __ndptr = __bucket_list_[__chash];
1788        if (__ndptr != nullptr)
1789        {
1790            for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
1791                                             __constrain_hash(__ndptr->__hash(), __bc) == __chash;
1792                                                     __ndptr = __ndptr->__next_)
1793            {
1794                if (key_eq()(__ndptr->__upcast()->__value_, __nd->__value_))
1795                    goto __done;
1796            }
1797        }
1798    }
1799    {
1800        if (size()+1 > __bc * max_load_factor() || __bc == 0)
1801        {
1802            rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
1803                           size_type(ceil(float(size() + 1) / max_load_factor()))));
1804            __bc = bucket_count();
1805            __chash = __constrain_hash(__nd->__hash_, __bc);
1806        }
1807        // insert_after __bucket_list_[__chash], or __first_node if bucket is null
1808        __next_pointer __pn = __bucket_list_[__chash];
1809        if (__pn == nullptr)
1810        {
1811            __pn =__p1_.first().__ptr();
1812            __nd->__next_ = __pn->__next_;
1813            __pn->__next_ = __nd->__ptr();
1814            // fix up __bucket_list_
1815            __bucket_list_[__chash] = __pn;
1816            if (__nd->__next_ != nullptr)
1817                __bucket_list_[__constrain_hash(__nd->__next_->__hash(), __bc)] = __nd->__ptr();
1818        }
1819        else
1820        {
1821            __nd->__next_ = __pn->__next_;
1822            __pn->__next_ = __nd->__ptr();
1823        }
1824        __ndptr = __nd->__ptr();
1825        // increment size
1826        ++size();
1827        __inserted = true;
1828    }
1829__done:
1830#if _LIBCPP_DEBUG_LEVEL >= 2
1831    return pair<iterator, bool>(iterator(__ndptr, this), __inserted);
1832#else
1833    return pair<iterator, bool>(iterator(__ndptr), __inserted);
1834#endif
1835}
1836
1837template <class _Tp, class _Hash, class _Equal, class _Alloc>
1838typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1839__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp)
1840{
1841    __cp->__hash_ = hash_function()(__cp->__value_);
1842    size_type __bc = bucket_count();
1843    if (size()+1 > __bc * max_load_factor() || __bc == 0)
1844    {
1845        rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
1846                       size_type(ceil(float(size() + 1) / max_load_factor()))));
1847        __bc = bucket_count();
1848    }
1849    size_t __chash = __constrain_hash(__cp->__hash_, __bc);
1850    __next_pointer __pn = __bucket_list_[__chash];
1851    if (__pn == nullptr)
1852    {
1853        __pn =__p1_.first().__ptr();
1854        __cp->__next_ = __pn->__next_;
1855        __pn->__next_ = __cp->__ptr();
1856        // fix up __bucket_list_
1857        __bucket_list_[__chash] = __pn;
1858        if (__cp->__next_ != nullptr)
1859            __bucket_list_[__constrain_hash(__cp->__next_->__hash(), __bc)]
1860                = __cp->__ptr();
1861    }
1862    else
1863    {
1864        for (bool __found = false; __pn->__next_ != nullptr &&
1865                                   __constrain_hash(__pn->__next_->__hash(), __bc) == __chash;
1866                                                           __pn = __pn->__next_)
1867        {
1868            //      __found    key_eq()     action
1869            //      false       false       loop
1870            //      true        true        loop
1871            //      false       true        set __found to true
1872            //      true        false       break
1873            if (__found != (__pn->__next_->__hash() == __cp->__hash_ &&
1874                            key_eq()(__pn->__next_->__upcast()->__value_, __cp->__value_)))
1875            {
1876                if (!__found)
1877                    __found = true;
1878                else
1879                    break;
1880            }
1881        }
1882        __cp->__next_ = __pn->__next_;
1883        __pn->__next_ = __cp->__ptr();
1884        if (__cp->__next_ != nullptr)
1885        {
1886            size_t __nhash = __constrain_hash(__cp->__next_->__hash(), __bc);
1887            if (__nhash != __chash)
1888                __bucket_list_[__nhash] = __cp->__ptr();
1889        }
1890    }
1891    ++size();
1892#if _LIBCPP_DEBUG_LEVEL >= 2
1893    return iterator(__cp->__ptr(), this);
1894#else
1895    return iterator(__cp->__ptr());
1896#endif
1897}
1898
1899template <class _Tp, class _Hash, class _Equal, class _Alloc>
1900typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1901__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(
1902        const_iterator __p, __node_pointer __cp)
1903{
1904#if _LIBCPP_DEBUG_LEVEL >= 2
1905    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1906        "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
1907        " referring to this unordered container");
1908#endif
1909    if (__p != end() && key_eq()(*__p, __cp->__value_))
1910    {
1911        __next_pointer __np = __p.__node_;
1912        __cp->__hash_ = __np->__hash();
1913        size_type __bc = bucket_count();
1914        if (size()+1 > __bc * max_load_factor() || __bc == 0)
1915        {
1916            rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
1917                           size_type(ceil(float(size() + 1) / max_load_factor()))));
1918            __bc = bucket_count();
1919        }
1920        size_t __chash = __constrain_hash(__cp->__hash_, __bc);
1921        __next_pointer __pp = __bucket_list_[__chash];
1922        while (__pp->__next_ != __np)
1923            __pp = __pp->__next_;
1924        __cp->__next_ = __np;
1925        __pp->__next_ = static_cast<__next_pointer>(__cp);
1926        ++size();
1927#if _LIBCPP_DEBUG_LEVEL >= 2
1928        return iterator(static_cast<__next_pointer>(__cp), this);
1929#else
1930        return iterator(static_cast<__next_pointer>(__cp));
1931#endif
1932    }
1933    return __node_insert_multi(__cp);
1934}
1935
1936
1937
1938#ifndef _LIBCPP_CXX03_LANG
1939template <class _Tp, class _Hash, class _Equal, class _Alloc>
1940template <class _Key, class ..._Args>
1941pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1942__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
1943#else
1944template <class _Tp, class _Hash, class _Equal, class _Alloc>
1945template <class _Key, class _Args>
1946pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1947__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
1948#endif
1949{
1950
1951    size_t __hash = hash_function()(__k);
1952    size_type __bc = bucket_count();
1953    bool __inserted = false;
1954    __next_pointer __nd;
1955    size_t __chash;
1956    if (__bc != 0)
1957    {
1958        __chash = __constrain_hash(__hash, __bc);
1959        __nd = __bucket_list_[__chash];
1960        if (__nd != nullptr)
1961        {
1962            for (__nd = __nd->__next_; __nd != nullptr &&
1963                (__nd->__hash() == __hash || __constrain_hash(__nd->__hash(), __bc) == __chash);
1964                                                           __nd = __nd->__next_)
1965            {
1966                if (key_eq()(__nd->__upcast()->__value_, __k))
1967                    goto __done;
1968            }
1969        }
1970    }
1971    {
1972#ifndef _LIBCPP_CXX03_LANG
1973        __node_holder __h = __construct_node_hash(__hash, _VSTD::forward<_Args>(__args)...);
1974#else
1975        __node_holder __h = __construct_node_hash(__hash, __args);
1976#endif
1977        if (size()+1 > __bc * max_load_factor() || __bc == 0)
1978        {
1979            rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
1980                           size_type(ceil(float(size() + 1) / max_load_factor()))));
1981            __bc = bucket_count();
1982            __chash = __constrain_hash(__hash, __bc);
1983        }
1984        // insert_after __bucket_list_[__chash], or __first_node if bucket is null
1985        __next_pointer __pn = __bucket_list_[__chash];
1986        if (__pn == nullptr)
1987        {
1988            __pn = __p1_.first().__ptr();
1989            __h->__next_ = __pn->__next_;
1990            __pn->__next_ = __h.get()->__ptr();
1991            // fix up __bucket_list_
1992            __bucket_list_[__chash] = __pn;
1993            if (__h->__next_ != nullptr)
1994                __bucket_list_[__constrain_hash(__h->__next_->__hash(), __bc)]
1995                    = __h.get()->__ptr();
1996        }
1997        else
1998        {
1999            __h->__next_ = __pn->__next_;
2000            __pn->__next_ = static_cast<__next_pointer>(__h.get());
2001        }
2002        __nd = static_cast<__next_pointer>(__h.release());
2003        // increment size
2004        ++size();
2005        __inserted = true;
2006    }
2007__done:
2008#if _LIBCPP_DEBUG_LEVEL >= 2
2009    return pair<iterator, bool>(iterator(__nd, this), __inserted);
2010#else
2011    return pair<iterator, bool>(iterator(__nd), __inserted);
2012#endif
2013}
2014
2015#ifndef _LIBCPP_CXX03_LANG
2016
2017template <class _Tp, class _Hash, class _Equal, class _Alloc>
2018template <class... _Args>
2019pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
2020__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_impl(_Args&&... __args)
2021{
2022    __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2023    pair<iterator, bool> __r = __node_insert_unique(__h.get());
2024    if (__r.second)
2025        __h.release();
2026    return __r;
2027}
2028
2029template <class _Tp, class _Hash, class _Equal, class _Alloc>
2030template <class... _Args>
2031typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2032__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args)
2033{
2034    __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2035    iterator __r = __node_insert_multi(__h.get());
2036    __h.release();
2037    return __r;
2038}
2039
2040template <class _Tp, class _Hash, class _Equal, class _Alloc>
2041template <class... _Args>
2042typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2043__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(
2044        const_iterator __p, _Args&&... __args)
2045{
2046#if _LIBCPP_DEBUG_LEVEL >= 2
2047    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2048        "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
2049        " referring to this unordered container");
2050#endif
2051    __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2052    iterator __r = __node_insert_multi(__p, __h.get());
2053    __h.release();
2054    return __r;
2055}
2056
2057#else // _LIBCPP_CXX03_LANG
2058
2059template <class _Tp, class _Hash, class _Equal, class _Alloc>
2060typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2061__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const __container_value_type& __x)
2062{
2063    __node_holder __h = __construct_node(__x);
2064    iterator __r = __node_insert_multi(__h.get());
2065    __h.release();
2066    return __r;
2067}
2068
2069template <class _Tp, class _Hash, class _Equal, class _Alloc>
2070typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2071__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
2072                                                         const __container_value_type& __x)
2073{
2074#if _LIBCPP_DEBUG_LEVEL >= 2
2075    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2076        "unordered container::insert(const_iterator, lvalue) called with an iterator not"
2077        " referring to this unordered container");
2078#endif
2079    __node_holder __h = __construct_node(__x);
2080    iterator __r = __node_insert_multi(__p, __h.get());
2081    __h.release();
2082    return __r;
2083}
2084
2085#endif  // _LIBCPP_CXX03_LANG
2086
2087template <class _Tp, class _Hash, class _Equal, class _Alloc>
2088void
2089__hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
2090{
2091    if (__n == 1)
2092        __n = 2;
2093    else if (__n & (__n - 1))
2094        __n = __next_prime(__n);
2095    size_type __bc = bucket_count();
2096    if (__n > __bc)
2097        __rehash(__n);
2098    else if (__n < __bc)
2099    {
2100        __n = _VSTD::max<size_type>
2101              (
2102                  __n,
2103                  __is_hash_power2(__bc) ? __next_hash_pow2(size_t(ceil(float(size()) / max_load_factor()))) :
2104                                           __next_prime(size_t(ceil(float(size()) / max_load_factor())))
2105              );
2106        if (__n < __bc)
2107            __rehash(__n);
2108    }
2109}
2110
2111template <class _Tp, class _Hash, class _Equal, class _Alloc>
2112void
2113__hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc)
2114{
2115#if _LIBCPP_DEBUG_LEVEL >= 2
2116    __get_db()->__invalidate_all(this);
2117#endif  // _LIBCPP_DEBUG_LEVEL >= 2
2118    __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
2119    __bucket_list_.reset(__nbc > 0 ?
2120                      __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
2121    __bucket_list_.get_deleter().size() = __nbc;
2122    if (__nbc > 0)
2123    {
2124        for (size_type __i = 0; __i < __nbc; ++__i)
2125            __bucket_list_[__i] = nullptr;
2126        __next_pointer __pp = __p1_.first().__ptr();
2127        __next_pointer __cp = __pp->__next_;
2128        if (__cp != nullptr)
2129        {
2130            size_type __chash = __constrain_hash(__cp->__hash(), __nbc);
2131            __bucket_list_[__chash] = __pp;
2132            size_type __phash = __chash;
2133            for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr;
2134                                                           __cp = __pp->__next_)
2135            {
2136                __chash = __constrain_hash(__cp->__hash(), __nbc);
2137                if (__chash == __phash)
2138                    __pp = __cp;
2139                else
2140                {
2141                    if (__bucket_list_[__chash] == nullptr)
2142                    {
2143                        __bucket_list_[__chash] = __pp;
2144                        __pp = __cp;
2145                        __phash = __chash;
2146                    }
2147                    else
2148                    {
2149                        __next_pointer __np = __cp;
2150                        for (; __np->__next_ != nullptr &&
2151                               key_eq()(__cp->__upcast()->__value_,
2152                                        __np->__next_->__upcast()->__value_);
2153                                                           __np = __np->__next_)
2154                            ;
2155                        __pp->__next_ = __np->__next_;
2156                        __np->__next_ = __bucket_list_[__chash]->__next_;
2157                        __bucket_list_[__chash]->__next_ = __cp;
2158
2159                    }
2160                }
2161            }
2162        }
2163    }
2164}
2165
2166template <class _Tp, class _Hash, class _Equal, class _Alloc>
2167template <class _Key>
2168typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2169__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
2170{
2171    size_t __hash = hash_function()(__k);
2172    size_type __bc = bucket_count();
2173    if (__bc != 0)
2174    {
2175        size_t __chash = __constrain_hash(__hash, __bc);
2176        __next_pointer __nd = __bucket_list_[__chash];
2177        if (__nd != nullptr)
2178        {
2179            for (__nd = __nd->__next_; __nd != nullptr &&
2180                (__nd->__hash() == __hash
2181                  || __constrain_hash(__nd->__hash(), __bc) == __chash);
2182                                                           __nd = __nd->__next_)
2183            {
2184                if ((__nd->__hash() == __hash)
2185                    && key_eq()(__nd->__upcast()->__value_, __k))
2186#if _LIBCPP_DEBUG_LEVEL >= 2
2187                    return iterator(__nd, this);
2188#else
2189                    return iterator(__nd);
2190#endif
2191            }
2192        }
2193    }
2194    return end();
2195}
2196
2197template <class _Tp, class _Hash, class _Equal, class _Alloc>
2198template <class _Key>
2199typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
2200__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
2201{
2202    size_t __hash = hash_function()(__k);
2203    size_type __bc = bucket_count();
2204    if (__bc != 0)
2205    {
2206        size_t __chash = __constrain_hash(__hash, __bc);
2207        __next_pointer __nd = __bucket_list_[__chash];
2208        if (__nd != nullptr)
2209        {
2210            for (__nd = __nd->__next_; __nd != nullptr &&
2211                (__hash == __nd->__hash()
2212                    || __constrain_hash(__nd->__hash(), __bc) == __chash);
2213                                                           __nd = __nd->__next_)
2214            {
2215                if ((__nd->__hash() == __hash)
2216                    && key_eq()(__nd->__upcast()->__value_, __k))
2217#if _LIBCPP_DEBUG_LEVEL >= 2
2218                    return const_iterator(__nd, this);
2219#else
2220                    return const_iterator(__nd);
2221#endif
2222            }
2223        }
2224
2225    }
2226    return end();
2227}
2228
2229#ifndef _LIBCPP_CXX03_LANG
2230
2231template <class _Tp, class _Hash, class _Equal, class _Alloc>
2232template <class ..._Args>
2233typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2234__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args)
2235{
2236    static_assert(!__is_hash_value_type<_Args...>::value,
2237                  "Construct cannot be called with a hash value type");
2238    __node_allocator& __na = __node_alloc();
2239    __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2240    __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
2241    __h.get_deleter().__value_constructed = true;
2242    __h->__hash_ = hash_function()(__h->__value_);
2243    __h->__next_ = nullptr;
2244    return __h;
2245}
2246
2247template <class _Tp, class _Hash, class _Equal, class _Alloc>
2248template <class _First, class ..._Rest>
2249typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2250__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(
2251    size_t __hash, _First&& __f, _Rest&& ...__rest)
2252{
2253    static_assert(!__is_hash_value_type<_First, _Rest...>::value,
2254                  "Construct cannot be called with a hash value type");
2255    __node_allocator& __na = __node_alloc();
2256    __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2257    __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_),
2258                             _VSTD::forward<_First>(__f),
2259                             _VSTD::forward<_Rest>(__rest)...);
2260    __h.get_deleter().__value_constructed = true;
2261    __h->__hash_ = __hash;
2262    __h->__next_ = nullptr;
2263    return __h;
2264}
2265
2266#else  // _LIBCPP_CXX03_LANG
2267
2268template <class _Tp, class _Hash, class _Equal, class _Alloc>
2269typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2270__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const __container_value_type& __v)
2271{
2272    __node_allocator& __na = __node_alloc();
2273    __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2274    __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
2275    __h.get_deleter().__value_constructed = true;
2276    __h->__hash_ = hash_function()(__h->__value_);
2277    __h->__next_ = nullptr;
2278    return _LIBCPP_EXPLICIT_MOVE(__h);  // explicitly moved for C++03
2279}
2280
2281template <class _Tp, class _Hash, class _Equal, class _Alloc>
2282typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2283__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(size_t __hash,
2284                                                                const __container_value_type& __v)
2285{
2286    __node_allocator& __na = __node_alloc();
2287    __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2288    __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
2289    __h.get_deleter().__value_constructed = true;
2290    __h->__hash_ = __hash;
2291    __h->__next_ = nullptr;
2292    return _LIBCPP_EXPLICIT_MOVE(__h);  // explicitly moved for C++03
2293}
2294
2295#endif  // _LIBCPP_CXX03_LANG
2296
2297template <class _Tp, class _Hash, class _Equal, class _Alloc>
2298typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2299__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p)
2300{
2301    __next_pointer __np = __p.__node_;
2302#if _LIBCPP_DEBUG_LEVEL >= 2
2303    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2304        "unordered container erase(iterator) called with an iterator not"
2305        " referring to this container");
2306    _LIBCPP_ASSERT(__p != end(),
2307        "unordered container erase(iterator) called with a non-dereferenceable iterator");
2308    iterator __r(__np, this);
2309#else
2310    iterator __r(__np);
2311#endif
2312    ++__r;
2313    remove(__p);
2314    return __r;
2315}
2316
2317template <class _Tp, class _Hash, class _Equal, class _Alloc>
2318typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2319__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first,
2320                                                const_iterator __last)
2321{
2322#if _LIBCPP_DEBUG_LEVEL >= 2
2323    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2324        "unodered container::erase(iterator, iterator) called with an iterator not"
2325        " referring to this unodered container");
2326    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
2327        "unodered container::erase(iterator, iterator) called with an iterator not"
2328        " referring to this unodered container");
2329#endif
2330    for (const_iterator __p = __first; __first != __last; __p = __first)
2331    {
2332        ++__first;
2333        erase(__p);
2334    }
2335    __next_pointer __np = __last.__node_;
2336#if _LIBCPP_DEBUG_LEVEL >= 2
2337    return iterator (__np, this);
2338#else
2339    return iterator (__np);
2340#endif
2341}
2342
2343template <class _Tp, class _Hash, class _Equal, class _Alloc>
2344template <class _Key>
2345typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2346__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k)
2347{
2348    iterator __i = find(__k);
2349    if (__i == end())
2350        return 0;
2351    erase(__i);
2352    return 1;
2353}
2354
2355template <class _Tp, class _Hash, class _Equal, class _Alloc>
2356template <class _Key>
2357typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2358__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k)
2359{
2360    size_type __r = 0;
2361    iterator __i = find(__k);
2362    if (__i != end())
2363    {
2364        iterator __e = end();
2365        do
2366        {
2367            erase(__i++);
2368            ++__r;
2369        } while (__i != __e && key_eq()(*__i, __k));
2370    }
2371    return __r;
2372}
2373
2374template <class _Tp, class _Hash, class _Equal, class _Alloc>
2375typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2376__hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT
2377{
2378    // current node
2379    __next_pointer __cn = __p.__node_;
2380    size_type __bc = bucket_count();
2381    size_t __chash = __constrain_hash(__cn->__hash(), __bc);
2382    // find previous node
2383    __next_pointer __pn = __bucket_list_[__chash];
2384    for (; __pn->__next_ != __cn; __pn = __pn->__next_)
2385        ;
2386    // Fix up __bucket_list_
2387        // if __pn is not in same bucket (before begin is not in same bucket) &&
2388        //    if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
2389    if (__pn == __p1_.first().__ptr()
2390            || __constrain_hash(__pn->__hash(), __bc) != __chash)
2391    {
2392        if (__cn->__next_ == nullptr
2393            || __constrain_hash(__cn->__next_->__hash(), __bc) != __chash)
2394            __bucket_list_[__chash] = nullptr;
2395    }
2396        // if __cn->__next_ is not in same bucket (nullptr is in same bucket)
2397    if (__cn->__next_ != nullptr)
2398    {
2399        size_t __nhash = __constrain_hash(__cn->__next_->__hash(), __bc);
2400        if (__nhash != __chash)
2401            __bucket_list_[__nhash] = __pn;
2402    }
2403    // remove __cn
2404    __pn->__next_ = __cn->__next_;
2405    __cn->__next_ = nullptr;
2406    --size();
2407#if _LIBCPP_DEBUG_LEVEL >= 2
2408    __c_node* __c = __get_db()->__find_c_and_lock(this);
2409    for (__i_node** __p = __c->end_; __p != __c->beg_; )
2410    {
2411        --__p;
2412        iterator* __i = static_cast<iterator*>((*__p)->__i_);
2413        if (__i->__node_ == __cn)
2414        {
2415            (*__p)->__c_ = nullptr;
2416            if (--__c->end_ != __p)
2417                memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2418        }
2419    }
2420    __get_db()->unlock();
2421#endif
2422    return __node_holder(__cn->__upcast(), _Dp(__node_alloc(), true));
2423}
2424
2425template <class _Tp, class _Hash, class _Equal, class _Alloc>
2426template <class _Key>
2427inline
2428typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2429__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const
2430{
2431    return static_cast<size_type>(find(__k) != end());
2432}
2433
2434template <class _Tp, class _Hash, class _Equal, class _Alloc>
2435template <class _Key>
2436typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2437__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const
2438{
2439    size_type __r = 0;
2440    const_iterator __i = find(__k);
2441    if (__i != end())
2442    {
2443        const_iterator __e = end();
2444        do
2445        {
2446            ++__i;
2447            ++__r;
2448        } while (__i != __e && key_eq()(*__i, __k));
2449    }
2450    return __r;
2451}
2452
2453template <class _Tp, class _Hash, class _Equal, class _Alloc>
2454template <class _Key>
2455pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2456     typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2457__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2458        const _Key& __k)
2459{
2460    iterator __i = find(__k);
2461    iterator __j = __i;
2462    if (__i != end())
2463        ++__j;
2464    return pair<iterator, iterator>(__i, __j);
2465}
2466
2467template <class _Tp, class _Hash, class _Equal, class _Alloc>
2468template <class _Key>
2469pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2470     typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2471__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2472        const _Key& __k) const
2473{
2474    const_iterator __i = find(__k);
2475    const_iterator __j = __i;
2476    if (__i != end())
2477        ++__j;
2478    return pair<const_iterator, const_iterator>(__i, __j);
2479}
2480
2481template <class _Tp, class _Hash, class _Equal, class _Alloc>
2482template <class _Key>
2483pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2484     typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2485__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2486        const _Key& __k)
2487{
2488    iterator __i = find(__k);
2489    iterator __j = __i;
2490    if (__i != end())
2491    {
2492        iterator __e = end();
2493        do
2494        {
2495            ++__j;
2496        } while (__j != __e && key_eq()(*__j, __k));
2497    }
2498    return pair<iterator, iterator>(__i, __j);
2499}
2500
2501template <class _Tp, class _Hash, class _Equal, class _Alloc>
2502template <class _Key>
2503pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2504     typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2505__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2506        const _Key& __k) const
2507{
2508    const_iterator __i = find(__k);
2509    const_iterator __j = __i;
2510    if (__i != end())
2511    {
2512        const_iterator __e = end();
2513        do
2514        {
2515            ++__j;
2516        } while (__j != __e && key_eq()(*__j, __k));
2517    }
2518    return pair<const_iterator, const_iterator>(__i, __j);
2519}
2520
2521template <class _Tp, class _Hash, class _Equal, class _Alloc>
2522void
2523__hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
2524#if _LIBCPP_STD_VER <= 11
2525    _NOEXCEPT_(
2526        __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
2527        && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
2528              || __is_nothrow_swappable<__pointer_allocator>::value)
2529        && (!__node_traits::propagate_on_container_swap::value
2530              || __is_nothrow_swappable<__node_allocator>::value)
2531            )
2532#else
2533  _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value)
2534#endif
2535{
2536    {
2537    __node_pointer_pointer __npp = __bucket_list_.release();
2538    __bucket_list_.reset(__u.__bucket_list_.release());
2539    __u.__bucket_list_.reset(__npp);
2540    }
2541    _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
2542    __swap_allocator(__bucket_list_.get_deleter().__alloc(),
2543             __u.__bucket_list_.get_deleter().__alloc());
2544    __swap_allocator(__node_alloc(), __u.__node_alloc());
2545    _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
2546    __p2_.swap(__u.__p2_);
2547    __p3_.swap(__u.__p3_);
2548    if (size() > 0)
2549        __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
2550            __p1_.first().__ptr();
2551    if (__u.size() > 0)
2552        __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash(), __u.bucket_count())] =
2553            __u.__p1_.first().__ptr();
2554#if _LIBCPP_DEBUG_LEVEL >= 2
2555    __get_db()->swap(this, &__u);
2556#endif
2557}
2558
2559template <class _Tp, class _Hash, class _Equal, class _Alloc>
2560typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2561__hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const
2562{
2563    _LIBCPP_ASSERT(__n < bucket_count(),
2564        "unordered container::bucket_size(n) called with n >= bucket_count()");
2565    __next_pointer __np = __bucket_list_[__n];
2566    size_type __bc = bucket_count();
2567    size_type __r = 0;
2568    if (__np != nullptr)
2569    {
2570        for (__np = __np->__next_; __np != nullptr &&
2571                                   __constrain_hash(__np->__hash(), __bc) == __n;
2572                                                    __np = __np->__next_, ++__r)
2573            ;
2574    }
2575    return __r;
2576}
2577
2578template <class _Tp, class _Hash, class _Equal, class _Alloc>
2579inline _LIBCPP_INLINE_VISIBILITY
2580void
2581swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x,
2582     __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
2583    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2584{
2585    __x.swap(__y);
2586}
2587
2588#if _LIBCPP_DEBUG_LEVEL >= 2
2589
2590template <class _Tp, class _Hash, class _Equal, class _Alloc>
2591bool
2592__hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const
2593{
2594    return __i->__node_ != nullptr;
2595}
2596
2597template <class _Tp, class _Hash, class _Equal, class _Alloc>
2598bool
2599__hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const
2600{
2601    return false;
2602}
2603
2604template <class _Tp, class _Hash, class _Equal, class _Alloc>
2605bool
2606__hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2607{
2608    return false;
2609}
2610
2611template <class _Tp, class _Hash, class _Equal, class _Alloc>
2612bool
2613__hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2614{
2615    return false;
2616}
2617
2618#endif  // _LIBCPP_DEBUG_LEVEL >= 2
2619_LIBCPP_END_NAMESPACE_STD
2620
2621#endif  // _LIBCPP__HASH_TABLE
2622