llvm-project
2044 строки · 84.1 Кб
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___HASH_TABLE
11#define _LIBCPP___HASH_TABLE
12
13#include <__algorithm/max.h>
14#include <__algorithm/min.h>
15#include <__assert>
16#include <__bit/countl.h>
17#include <__config>
18#include <__functional/hash.h>
19#include <__functional/invoke.h>
20#include <__iterator/iterator_traits.h>
21#include <__memory/addressof.h>
22#include <__memory/allocator_traits.h>
23#include <__memory/compressed_pair.h>
24#include <__memory/construct_at.h>
25#include <__memory/pointer_traits.h>
26#include <__memory/swap_allocator.h>
27#include <__memory/unique_ptr.h>
28#include <__type_traits/can_extract_key.h>
29#include <__type_traits/conditional.h>
30#include <__type_traits/is_const.h>
31#include <__type_traits/is_constructible.h>
32#include <__type_traits/is_nothrow_assignable.h>
33#include <__type_traits/is_nothrow_constructible.h>
34#include <__type_traits/is_pointer.h>
35#include <__type_traits/is_reference.h>
36#include <__type_traits/is_swappable.h>
37#include <__type_traits/remove_const.h>
38#include <__type_traits/remove_cvref.h>
39#include <__utility/forward.h>
40#include <__utility/move.h>
41#include <__utility/pair.h>
42#include <__utility/swap.h>
43#include <cmath>
44#include <cstring>
45#include <initializer_list>
46#include <new> // __launder
47
48#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
49# pragma GCC system_header
50#endif
51
52_LIBCPP_PUSH_MACROS
53#include <__undef_macros>
54
55_LIBCPP_BEGIN_NAMESPACE_STD
56
57template <class _Key, class _Tp>
58struct __hash_value_type;
59
60template <class _Tp>
61struct __is_hash_value_type_imp : false_type {};
62
63template <class _Key, class _Value>
64struct __is_hash_value_type_imp<__hash_value_type<_Key, _Value> > : true_type {};
65
66template <class... _Args>
67struct __is_hash_value_type : false_type {};
68
69template <class _One>
70struct __is_hash_value_type<_One> : __is_hash_value_type_imp<__remove_cvref_t<_One> > {};
71
72_LIBCPP_EXPORTED_FROM_ABI size_t __next_prime(size_t __n);
73
74template <class _NodePtr>
75struct __hash_node_base {
76typedef typename pointer_traits<_NodePtr>::element_type __node_type;
77typedef __hash_node_base __first_node;
78typedef __rebind_pointer_t<_NodePtr, __first_node> __node_base_pointer;
79typedef _NodePtr __node_pointer;
80
81#if defined(_LIBCPP_ABI_FIX_UNORDERED_NODE_POINTER_UB)
82typedef __node_base_pointer __next_pointer;
83#else
84typedef __conditional_t<is_pointer<__node_pointer>::value, __node_base_pointer, __node_pointer> __next_pointer;
85#endif
86
87__next_pointer __next_;
88
89_LIBCPP_HIDE_FROM_ABI __next_pointer __ptr() _NOEXCEPT {
90return static_cast<__next_pointer>(pointer_traits<__node_base_pointer>::pointer_to(*this));
91}
92
93_LIBCPP_HIDE_FROM_ABI __node_pointer __upcast() _NOEXCEPT {
94return static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(*this));
95}
96
97_LIBCPP_HIDE_FROM_ABI size_t __hash() const _NOEXCEPT { return static_cast<__node_type const&>(*this).__hash_; }
98
99_LIBCPP_HIDE_FROM_ABI __hash_node_base() _NOEXCEPT : __next_(nullptr) {}
100_LIBCPP_HIDE_FROM_ABI explicit __hash_node_base(__next_pointer __next) _NOEXCEPT : __next_(__next) {}
101};
102
103template <class _Tp, class _VoidPtr>
104struct __hash_node : public __hash_node_base< __rebind_pointer_t<_VoidPtr, __hash_node<_Tp, _VoidPtr> > > {
105typedef _Tp __node_value_type;
106using _Base = __hash_node_base<__rebind_pointer_t<_VoidPtr, __hash_node<_Tp, _VoidPtr> > >;
107using __next_pointer = typename _Base::__next_pointer;
108
109size_t __hash_;
110
111// We allow starting the lifetime of nodes without initializing the value held by the node,
112// since that is handled by the hash table itself in order to be allocator-aware.
113#ifndef _LIBCPP_CXX03_LANG
114
115private:
116union {
117_Tp __value_;
118};
119
120public:
121_LIBCPP_HIDE_FROM_ABI _Tp& __get_value() { return __value_; }
122#else
123
124private:
125_ALIGNAS_TYPE(_Tp) char __buffer_[sizeof(_Tp)];
126
127public:
128_LIBCPP_HIDE_FROM_ABI _Tp& __get_value() { return *std::__launder(reinterpret_cast<_Tp*>(&__buffer_)); }
129#endif
130
131_LIBCPP_HIDE_FROM_ABI explicit __hash_node(__next_pointer __next, size_t __hash) : _Base(__next), __hash_(__hash) {}
132_LIBCPP_HIDE_FROM_ABI ~__hash_node() {}
133};
134
135inline _LIBCPP_HIDE_FROM_ABI bool __is_hash_power2(size_t __bc) { return __bc > 2 && !(__bc & (__bc - 1)); }
136
137inline _LIBCPP_HIDE_FROM_ABI size_t __constrain_hash(size_t __h, size_t __bc) {
138return !(__bc & (__bc - 1)) ? __h & (__bc - 1) : (__h < __bc ? __h : __h % __bc);
139}
140
141inline _LIBCPP_HIDE_FROM_ABI size_t __next_hash_pow2(size_t __n) {
142return __n < 2 ? __n : (size_t(1) << (numeric_limits<size_t>::digits - __libcpp_clz(__n - 1)));
143}
144
145template <class _Tp, class _Hash, class _Equal, class _Alloc>
146class __hash_table;
147
148template <class _NodePtr>
149class _LIBCPP_TEMPLATE_VIS __hash_iterator;
150template <class _ConstNodePtr>
151class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
152template <class _NodePtr>
153class _LIBCPP_TEMPLATE_VIS __hash_local_iterator;
154template <class _ConstNodePtr>
155class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
156template <class _HashIterator>
157class _LIBCPP_TEMPLATE_VIS __hash_map_iterator;
158template <class _HashIterator>
159class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
160
161template <class _Tp>
162struct __hash_key_value_types {
163static_assert(!is_reference<_Tp>::value && !is_const<_Tp>::value, "");
164typedef _Tp key_type;
165typedef _Tp __node_value_type;
166typedef _Tp __container_value_type;
167static const bool __is_map = false;
168
169_LIBCPP_HIDE_FROM_ABI static key_type const& __get_key(_Tp const& __v) { return __v; }
170_LIBCPP_HIDE_FROM_ABI static __container_value_type const& __get_value(__node_value_type const& __v) { return __v; }
171_LIBCPP_HIDE_FROM_ABI static __container_value_type* __get_ptr(__node_value_type& __n) { return std::addressof(__n); }
172_LIBCPP_HIDE_FROM_ABI static __container_value_type&& __move(__node_value_type& __v) { return std::move(__v); }
173};
174
175template <class _Key, class _Tp>
176struct __hash_key_value_types<__hash_value_type<_Key, _Tp> > {
177typedef _Key key_type;
178typedef _Tp mapped_type;
179typedef __hash_value_type<_Key, _Tp> __node_value_type;
180typedef pair<const _Key, _Tp> __container_value_type;
181typedef __container_value_type __map_value_type;
182static const bool __is_map = true;
183
184_LIBCPP_HIDE_FROM_ABI static key_type const& __get_key(__container_value_type const& __v) { return __v.first; }
185
186template <class _Up, __enable_if_t<__is_same_uncvref<_Up, __node_value_type>::value, int> = 0>
187_LIBCPP_HIDE_FROM_ABI static __container_value_type const& __get_value(_Up& __t) {
188return __t.__get_value();
189}
190
191template <class _Up, __enable_if_t<__is_same_uncvref<_Up, __container_value_type>::value, int> = 0>
192_LIBCPP_HIDE_FROM_ABI static __container_value_type const& __get_value(_Up& __t) {
193return __t;
194}
195
196_LIBCPP_HIDE_FROM_ABI static __container_value_type* __get_ptr(__node_value_type& __n) {
197return std::addressof(__n.__get_value());
198}
199_LIBCPP_HIDE_FROM_ABI static pair<key_type&&, mapped_type&&> __move(__node_value_type& __v) { return __v.__move(); }
200};
201
202template <class _Tp, class _AllocPtr, class _KVTypes = __hash_key_value_types<_Tp>, bool = _KVTypes::__is_map>
203struct __hash_map_pointer_types {};
204
205template <class _Tp, class _AllocPtr, class _KVTypes>
206struct __hash_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
207typedef typename _KVTypes::__map_value_type _Mv;
208typedef __rebind_pointer_t<_AllocPtr, _Mv> __map_value_type_pointer;
209typedef __rebind_pointer_t<_AllocPtr, const _Mv> __const_map_value_type_pointer;
210};
211
212template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
213struct __hash_node_types;
214
215template <class _NodePtr, class _Tp, class _VoidPtr>
216struct __hash_node_types<_NodePtr, __hash_node<_Tp, _VoidPtr> >
217: public __hash_key_value_types<_Tp>,
218__hash_map_pointer_types<_Tp, _VoidPtr>
219
220{
221typedef __hash_key_value_types<_Tp> __base;
222
223public:
224typedef ptrdiff_t difference_type;
225typedef size_t size_type;
226
227typedef __rebind_pointer_t<_NodePtr, void> __void_pointer;
228
229typedef typename pointer_traits<_NodePtr>::element_type __node_type;
230typedef _NodePtr __node_pointer;
231
232typedef __hash_node_base<__node_pointer> __node_base_type;
233typedef __rebind_pointer_t<_NodePtr, __node_base_type> __node_base_pointer;
234
235typedef typename __node_base_type::__next_pointer __next_pointer;
236
237typedef _Tp __node_value_type;
238typedef __rebind_pointer_t<_VoidPtr, __node_value_type> __node_value_type_pointer;
239typedef __rebind_pointer_t<_VoidPtr, const __node_value_type> __const_node_value_type_pointer;
240
241private:
242static_assert(!is_const<__node_type>::value, "_NodePtr should never be a pointer to const");
243static_assert(is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value,
244"_VoidPtr does not point to unqualified void type");
245static_assert(is_same<__rebind_pointer_t<_VoidPtr, __node_type>, _NodePtr>::value,
246"_VoidPtr does not rebind to _NodePtr.");
247};
248
249template <class _HashIterator>
250struct __hash_node_types_from_iterator;
251template <class _NodePtr>
252struct __hash_node_types_from_iterator<__hash_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
253template <class _NodePtr>
254struct __hash_node_types_from_iterator<__hash_const_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
255template <class _NodePtr>
256struct __hash_node_types_from_iterator<__hash_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
257template <class _NodePtr>
258struct __hash_node_types_from_iterator<__hash_const_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
259
260template <class _NodeValueTp, class _VoidPtr>
261struct __make_hash_node_types {
262typedef __hash_node<_NodeValueTp, _VoidPtr> _NodeTp;
263typedef __rebind_pointer_t<_VoidPtr, _NodeTp> _NodePtr;
264typedef __hash_node_types<_NodePtr> type;
265};
266
267template <class _NodePtr>
268class _LIBCPP_TEMPLATE_VIS __hash_iterator {
269typedef __hash_node_types<_NodePtr> _NodeTypes;
270typedef _NodePtr __node_pointer;
271typedef typename _NodeTypes::__next_pointer __next_pointer;
272
273__next_pointer __node_;
274
275public:
276typedef forward_iterator_tag iterator_category;
277typedef typename _NodeTypes::__node_value_type value_type;
278typedef typename _NodeTypes::difference_type difference_type;
279typedef value_type& reference;
280typedef typename _NodeTypes::__node_value_type_pointer pointer;
281
282_LIBCPP_HIDE_FROM_ABI __hash_iterator() _NOEXCEPT : __node_(nullptr) {}
283
284_LIBCPP_HIDE_FROM_ABI reference operator*() const {
285_LIBCPP_ASSERT_NON_NULL(
286__node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container iterator");
287return __node_->__upcast()->__get_value();
288}
289
290_LIBCPP_HIDE_FROM_ABI pointer operator->() const {
291_LIBCPP_ASSERT_NON_NULL(
292__node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container iterator");
293return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__get_value());
294}
295
296_LIBCPP_HIDE_FROM_ABI __hash_iterator& operator++() {
297_LIBCPP_ASSERT_NON_NULL(
298__node_ != nullptr, "Attempted to increment a non-incrementable unordered container iterator");
299__node_ = __node_->__next_;
300return *this;
301}
302
303_LIBCPP_HIDE_FROM_ABI __hash_iterator operator++(int) {
304__hash_iterator __t(*this);
305++(*this);
306return __t;
307}
308
309friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __hash_iterator& __x, const __hash_iterator& __y) {
310return __x.__node_ == __y.__node_;
311}
312friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y) {
313return !(__x == __y);
314}
315
316private:
317_LIBCPP_HIDE_FROM_ABI explicit __hash_iterator(__next_pointer __node) _NOEXCEPT : __node_(__node) {}
318
319template <class, class, class, class>
320friend class __hash_table;
321template <class>
322friend class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
323template <class>
324friend class _LIBCPP_TEMPLATE_VIS __hash_map_iterator;
325template <class, class, class, class, class>
326friend class _LIBCPP_TEMPLATE_VIS unordered_map;
327template <class, class, class, class, class>
328friend class _LIBCPP_TEMPLATE_VIS unordered_multimap;
329};
330
331template <class _NodePtr>
332class _LIBCPP_TEMPLATE_VIS __hash_const_iterator {
333static_assert(!is_const<typename pointer_traits<_NodePtr>::element_type>::value, "");
334typedef __hash_node_types<_NodePtr> _NodeTypes;
335typedef _NodePtr __node_pointer;
336typedef typename _NodeTypes::__next_pointer __next_pointer;
337
338__next_pointer __node_;
339
340public:
341typedef __hash_iterator<_NodePtr> __non_const_iterator;
342
343typedef forward_iterator_tag iterator_category;
344typedef typename _NodeTypes::__node_value_type value_type;
345typedef typename _NodeTypes::difference_type difference_type;
346typedef const value_type& reference;
347typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
348
349_LIBCPP_HIDE_FROM_ABI __hash_const_iterator() _NOEXCEPT : __node_(nullptr) {}
350
351_LIBCPP_HIDE_FROM_ABI __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT : __node_(__x.__node_) {}
352
353_LIBCPP_HIDE_FROM_ABI reference operator*() const {
354_LIBCPP_ASSERT_NON_NULL(
355__node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container const_iterator");
356return __node_->__upcast()->__get_value();
357}
358_LIBCPP_HIDE_FROM_ABI pointer operator->() const {
359_LIBCPP_ASSERT_NON_NULL(
360__node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container const_iterator");
361return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__get_value());
362}
363
364_LIBCPP_HIDE_FROM_ABI __hash_const_iterator& operator++() {
365_LIBCPP_ASSERT_NON_NULL(
366__node_ != nullptr, "Attempted to increment a non-incrementable unordered container const_iterator");
367__node_ = __node_->__next_;
368return *this;
369}
370
371_LIBCPP_HIDE_FROM_ABI __hash_const_iterator operator++(int) {
372__hash_const_iterator __t(*this);
373++(*this);
374return __t;
375}
376
377friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y) {
378return __x.__node_ == __y.__node_;
379}
380friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y) {
381return !(__x == __y);
382}
383
384private:
385_LIBCPP_HIDE_FROM_ABI explicit __hash_const_iterator(__next_pointer __node) _NOEXCEPT : __node_(__node) {}
386
387template <class, class, class, class>
388friend class __hash_table;
389template <class>
390friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
391template <class, class, class, class, class>
392friend class _LIBCPP_TEMPLATE_VIS unordered_map;
393template <class, class, class, class, class>
394friend class _LIBCPP_TEMPLATE_VIS unordered_multimap;
395};
396
397template <class _NodePtr>
398class _LIBCPP_TEMPLATE_VIS __hash_local_iterator {
399typedef __hash_node_types<_NodePtr> _NodeTypes;
400typedef _NodePtr __node_pointer;
401typedef typename _NodeTypes::__next_pointer __next_pointer;
402
403__next_pointer __node_;
404size_t __bucket_;
405size_t __bucket_count_;
406
407public:
408typedef forward_iterator_tag iterator_category;
409typedef typename _NodeTypes::__node_value_type value_type;
410typedef typename _NodeTypes::difference_type difference_type;
411typedef value_type& reference;
412typedef typename _NodeTypes::__node_value_type_pointer pointer;
413
414_LIBCPP_HIDE_FROM_ABI __hash_local_iterator() _NOEXCEPT : __node_(nullptr) {}
415
416_LIBCPP_HIDE_FROM_ABI reference operator*() const {
417_LIBCPP_ASSERT_NON_NULL(
418__node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container local_iterator");
419return __node_->__upcast()->__get_value();
420}
421
422_LIBCPP_HIDE_FROM_ABI pointer operator->() const {
423_LIBCPP_ASSERT_NON_NULL(
424__node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container local_iterator");
425return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__get_value());
426}
427
428_LIBCPP_HIDE_FROM_ABI __hash_local_iterator& operator++() {
429_LIBCPP_ASSERT_NON_NULL(
430__node_ != nullptr, "Attempted to increment a non-incrementable unordered container local_iterator");
431__node_ = __node_->__next_;
432if (__node_ != nullptr && std::__constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
433__node_ = nullptr;
434return *this;
435}
436
437_LIBCPP_HIDE_FROM_ABI __hash_local_iterator operator++(int) {
438__hash_local_iterator __t(*this);
439++(*this);
440return __t;
441}
442
443friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y) {
444return __x.__node_ == __y.__node_;
445}
446friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y) {
447return !(__x == __y);
448}
449
450private:
451_LIBCPP_HIDE_FROM_ABI explicit __hash_local_iterator(
452__next_pointer __node, size_t __bucket, size_t __bucket_count) _NOEXCEPT
453: __node_(__node),
454__bucket_(__bucket),
455__bucket_count_(__bucket_count) {
456if (__node_ != nullptr)
457__node_ = __node_->__next_;
458}
459
460template <class, class, class, class>
461friend class __hash_table;
462template <class>
463friend class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
464template <class>
465friend class _LIBCPP_TEMPLATE_VIS __hash_map_iterator;
466};
467
468template <class _ConstNodePtr>
469class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator {
470typedef __hash_node_types<_ConstNodePtr> _NodeTypes;
471typedef _ConstNodePtr __node_pointer;
472typedef typename _NodeTypes::__next_pointer __next_pointer;
473
474__next_pointer __node_;
475size_t __bucket_;
476size_t __bucket_count_;
477
478typedef pointer_traits<__node_pointer> __pointer_traits;
479typedef typename __pointer_traits::element_type __node;
480typedef __remove_const_t<__node> __non_const_node;
481typedef __rebind_pointer_t<__node_pointer, __non_const_node> __non_const_node_pointer;
482
483public:
484typedef __hash_local_iterator<__non_const_node_pointer> __non_const_iterator;
485
486typedef forward_iterator_tag iterator_category;
487typedef typename _NodeTypes::__node_value_type value_type;
488typedef typename _NodeTypes::difference_type difference_type;
489typedef const value_type& reference;
490typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
491
492_LIBCPP_HIDE_FROM_ABI __hash_const_local_iterator() _NOEXCEPT : __node_(nullptr) {}
493
494_LIBCPP_HIDE_FROM_ABI __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT
495: __node_(__x.__node_),
496__bucket_(__x.__bucket_),
497__bucket_count_(__x.__bucket_count_) {}
498
499_LIBCPP_HIDE_FROM_ABI reference operator*() const {
500_LIBCPP_ASSERT_NON_NULL(
501__node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
502return __node_->__upcast()->__get_value();
503}
504
505_LIBCPP_HIDE_FROM_ABI pointer operator->() const {
506_LIBCPP_ASSERT_NON_NULL(
507__node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
508return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__get_value());
509}
510
511_LIBCPP_HIDE_FROM_ABI __hash_const_local_iterator& operator++() {
512_LIBCPP_ASSERT_NON_NULL(
513__node_ != nullptr, "Attempted to increment a non-incrementable unordered container const_local_iterator");
514__node_ = __node_->__next_;
515if (__node_ != nullptr && std::__constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
516__node_ = nullptr;
517return *this;
518}
519
520_LIBCPP_HIDE_FROM_ABI __hash_const_local_iterator operator++(int) {
521__hash_const_local_iterator __t(*this);
522++(*this);
523return __t;
524}
525
526friend _LIBCPP_HIDE_FROM_ABI bool
527operator==(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y) {
528return __x.__node_ == __y.__node_;
529}
530friend _LIBCPP_HIDE_FROM_ABI bool
531operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y) {
532return !(__x == __y);
533}
534
535private:
536_LIBCPP_HIDE_FROM_ABI explicit __hash_const_local_iterator(
537__next_pointer __node_ptr, size_t __bucket, size_t __bucket_count) _NOEXCEPT
538: __node_(__node_ptr),
539__bucket_(__bucket),
540__bucket_count_(__bucket_count) {
541if (__node_ != nullptr)
542__node_ = __node_->__next_;
543}
544
545template <class, class, class, class>
546friend class __hash_table;
547template <class>
548friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
549};
550
551template <class _Alloc>
552class __bucket_list_deallocator {
553typedef _Alloc allocator_type;
554typedef allocator_traits<allocator_type> __alloc_traits;
555typedef typename __alloc_traits::size_type size_type;
556
557__compressed_pair<size_type, allocator_type> __data_;
558
559public:
560typedef typename __alloc_traits::pointer pointer;
561
562_LIBCPP_HIDE_FROM_ABI __bucket_list_deallocator() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
563: __data_(0, __default_init_tag()) {}
564
565_LIBCPP_HIDE_FROM_ABI __bucket_list_deallocator(const allocator_type& __a, size_type __size)
566_NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
567: __data_(__size, __a) {}
568
569_LIBCPP_HIDE_FROM_ABI __bucket_list_deallocator(__bucket_list_deallocator&& __x)
570_NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
571: __data_(std::move(__x.__data_)) {
572__x.size() = 0;
573}
574
575_LIBCPP_HIDE_FROM_ABI size_type& size() _NOEXCEPT { return __data_.first(); }
576_LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __data_.first(); }
577
578_LIBCPP_HIDE_FROM_ABI allocator_type& __alloc() _NOEXCEPT { return __data_.second(); }
579_LIBCPP_HIDE_FROM_ABI const allocator_type& __alloc() const _NOEXCEPT { return __data_.second(); }
580
581_LIBCPP_HIDE_FROM_ABI void operator()(pointer __p) _NOEXCEPT { __alloc_traits::deallocate(__alloc(), __p, size()); }
582};
583
584template <class _Alloc>
585class __hash_map_node_destructor;
586
587template <class _Alloc>
588class __hash_node_destructor {
589typedef _Alloc allocator_type;
590typedef allocator_traits<allocator_type> __alloc_traits;
591
592public:
593typedef typename __alloc_traits::pointer pointer;
594
595private:
596typedef __hash_node_types<pointer> _NodeTypes;
597
598allocator_type& __na_;
599
600public:
601bool __value_constructed;
602
603_LIBCPP_HIDE_FROM_ABI __hash_node_destructor(__hash_node_destructor const&) = default;
604_LIBCPP_HIDE_FROM_ABI __hash_node_destructor& operator=(const __hash_node_destructor&) = delete;
605
606_LIBCPP_HIDE_FROM_ABI explicit __hash_node_destructor(allocator_type& __na, bool __constructed = false) _NOEXCEPT
607: __na_(__na),
608__value_constructed(__constructed) {}
609
610_LIBCPP_HIDE_FROM_ABI void operator()(pointer __p) _NOEXCEPT {
611if (__value_constructed) {
612__alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__get_value()));
613std::__destroy_at(std::addressof(*__p));
614}
615if (__p)
616__alloc_traits::deallocate(__na_, __p, 1);
617}
618
619template <class>
620friend class __hash_map_node_destructor;
621};
622
623#if _LIBCPP_STD_VER >= 17
624template <class _NodeType, class _Alloc>
625struct __generic_container_node_destructor;
626
627template <class _Tp, class _VoidPtr, class _Alloc>
628struct __generic_container_node_destructor<__hash_node<_Tp, _VoidPtr>, _Alloc> : __hash_node_destructor<_Alloc> {
629using __hash_node_destructor<_Alloc>::__hash_node_destructor;
630};
631#endif
632
633template <class _Key, class _Hash, class _Equal>
634struct __enforce_unordered_container_requirements {
635#ifndef _LIBCPP_CXX03_LANG
636static_assert(__check_hash_requirements<_Key, _Hash>::value,
637"the specified hash does not meet the Hash requirements");
638static_assert(is_copy_constructible<_Equal>::value, "the specified comparator is required to be copy constructible");
639#endif
640typedef int type;
641};
642
643template <class _Key, class _Hash, class _Equal>
644#ifndef _LIBCPP_CXX03_LANG
645_LIBCPP_DIAGNOSE_WARNING(!__invokable<_Equal const&, _Key const&, _Key const&>::value,
646"the specified comparator type does not provide a viable const call operator")
647_LIBCPP_DIAGNOSE_WARNING(!__invokable<_Hash const&, _Key const&>::value,
648"the specified hash functor does not provide a viable const call operator")
649#endif
650typename __enforce_unordered_container_requirements<_Key, _Hash, _Equal>::type
651__diagnose_unordered_container_requirements(int);
652
653// This dummy overload is used so that the compiler won't emit a spurious
654// "no matching function for call to __diagnose_unordered_xxx" diagnostic
655// when the overload above causes a hard error.
656template <class _Key, class _Hash, class _Equal>
657int __diagnose_unordered_container_requirements(void*);
658
659template <class _Tp, class _Hash, class _Equal, class _Alloc>
660class __hash_table {
661public:
662typedef _Tp value_type;
663typedef _Hash hasher;
664typedef _Equal key_equal;
665typedef _Alloc allocator_type;
666
667private:
668typedef allocator_traits<allocator_type> __alloc_traits;
669typedef typename __make_hash_node_types<value_type, typename __alloc_traits::void_pointer>::type _NodeTypes;
670
671public:
672typedef typename _NodeTypes::__node_value_type __node_value_type;
673typedef typename _NodeTypes::__container_value_type __container_value_type;
674typedef typename _NodeTypes::key_type key_type;
675typedef value_type& reference;
676typedef const value_type& const_reference;
677typedef typename __alloc_traits::pointer pointer;
678typedef typename __alloc_traits::const_pointer const_pointer;
679#ifndef _LIBCPP_ABI_FIX_UNORDERED_CONTAINER_SIZE_TYPE
680typedef typename __alloc_traits::size_type size_type;
681#else
682typedef typename _NodeTypes::size_type size_type;
683#endif
684typedef typename _NodeTypes::difference_type difference_type;
685
686public:
687// Create __node
688
689typedef typename _NodeTypes::__node_type __node;
690typedef __rebind_alloc<__alloc_traits, __node> __node_allocator;
691typedef allocator_traits<__node_allocator> __node_traits;
692typedef typename _NodeTypes::__void_pointer __void_pointer;
693typedef typename _NodeTypes::__node_pointer __node_pointer;
694typedef typename _NodeTypes::__node_pointer __node_const_pointer;
695typedef typename _NodeTypes::__node_base_type __first_node;
696typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
697typedef typename _NodeTypes::__next_pointer __next_pointer;
698
699private:
700// check for sane allocator pointer rebinding semantics. Rebinding the
701// allocator for a new pointer type should be exactly the same as rebinding
702// the pointer using 'pointer_traits'.
703static_assert(is_same<__node_pointer, typename __node_traits::pointer>::value,
704"Allocator does not rebind pointers in a sane manner.");
705typedef __rebind_alloc<__node_traits, __first_node> __node_base_allocator;
706typedef allocator_traits<__node_base_allocator> __node_base_traits;
707static_assert(is_same<__node_base_pointer, typename __node_base_traits::pointer>::value,
708"Allocator does not rebind pointers in a sane manner.");
709
710private:
711typedef __rebind_alloc<__node_traits, __next_pointer> __pointer_allocator;
712typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter;
713typedef unique_ptr<__next_pointer[], __bucket_list_deleter> __bucket_list;
714typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits;
715typedef typename __bucket_list_deleter::pointer __node_pointer_pointer;
716
717// --- Member data begin ---
718__bucket_list __bucket_list_;
719__compressed_pair<__first_node, __node_allocator> __p1_;
720__compressed_pair<size_type, hasher> __p2_;
721__compressed_pair<float, key_equal> __p3_;
722// --- Member data end ---
723
724_LIBCPP_HIDE_FROM_ABI size_type& size() _NOEXCEPT { return __p2_.first(); }
725
726public:
727_LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __p2_.first(); }
728
729_LIBCPP_HIDE_FROM_ABI hasher& hash_function() _NOEXCEPT { return __p2_.second(); }
730_LIBCPP_HIDE_FROM_ABI const hasher& hash_function() const _NOEXCEPT { return __p2_.second(); }
731
732_LIBCPP_HIDE_FROM_ABI float& max_load_factor() _NOEXCEPT { return __p3_.first(); }
733_LIBCPP_HIDE_FROM_ABI float max_load_factor() const _NOEXCEPT { return __p3_.first(); }
734
735_LIBCPP_HIDE_FROM_ABI key_equal& key_eq() _NOEXCEPT { return __p3_.second(); }
736_LIBCPP_HIDE_FROM_ABI const key_equal& key_eq() const _NOEXCEPT { return __p3_.second(); }
737
738_LIBCPP_HIDE_FROM_ABI __node_allocator& __node_alloc() _NOEXCEPT { return __p1_.second(); }
739_LIBCPP_HIDE_FROM_ABI const __node_allocator& __node_alloc() const _NOEXCEPT { return __p1_.second(); }
740
741public:
742typedef __hash_iterator<__node_pointer> iterator;
743typedef __hash_const_iterator<__node_pointer> const_iterator;
744typedef __hash_local_iterator<__node_pointer> local_iterator;
745typedef __hash_const_local_iterator<__node_pointer> const_local_iterator;
746
747_LIBCPP_HIDE_FROM_ABI __hash_table() _NOEXCEPT_(
748is_nothrow_default_constructible<__bucket_list>::value&& is_nothrow_default_constructible<__first_node>::value&&
749is_nothrow_default_constructible<__node_allocator>::value&& is_nothrow_default_constructible<hasher>::value&&
750is_nothrow_default_constructible<key_equal>::value);
751_LIBCPP_HIDE_FROM_ABI __hash_table(const hasher& __hf, const key_equal& __eql);
752_LIBCPP_HIDE_FROM_ABI __hash_table(const hasher& __hf, const key_equal& __eql, const allocator_type& __a);
753_LIBCPP_HIDE_FROM_ABI explicit __hash_table(const allocator_type& __a);
754_LIBCPP_HIDE_FROM_ABI __hash_table(const __hash_table& __u);
755_LIBCPP_HIDE_FROM_ABI __hash_table(const __hash_table& __u, const allocator_type& __a);
756_LIBCPP_HIDE_FROM_ABI __hash_table(__hash_table&& __u) _NOEXCEPT_(
757is_nothrow_move_constructible<__bucket_list>::value&& is_nothrow_move_constructible<__first_node>::value&&
758is_nothrow_move_constructible<__node_allocator>::value&& is_nothrow_move_constructible<hasher>::value&&
759is_nothrow_move_constructible<key_equal>::value);
760_LIBCPP_HIDE_FROM_ABI __hash_table(__hash_table&& __u, const allocator_type& __a);
761_LIBCPP_HIDE_FROM_ABI ~__hash_table();
762
763_LIBCPP_HIDE_FROM_ABI __hash_table& operator=(const __hash_table& __u);
764_LIBCPP_HIDE_FROM_ABI __hash_table& operator=(__hash_table&& __u)
765_NOEXCEPT_(__node_traits::propagate_on_container_move_assignment::value&&
766is_nothrow_move_assignable<__node_allocator>::value&& is_nothrow_move_assignable<hasher>::value&&
767is_nothrow_move_assignable<key_equal>::value);
768template <class _InputIterator>
769_LIBCPP_HIDE_FROM_ABI void __assign_unique(_InputIterator __first, _InputIterator __last);
770template <class _InputIterator>
771_LIBCPP_HIDE_FROM_ABI void __assign_multi(_InputIterator __first, _InputIterator __last);
772
773_LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
774return std::min<size_type>(__node_traits::max_size(__node_alloc()), numeric_limits<difference_type >::max());
775}
776
777private:
778_LIBCPP_HIDE_FROM_ABI __next_pointer __node_insert_multi_prepare(size_t __cp_hash, value_type& __cp_val);
779_LIBCPP_HIDE_FROM_ABI void __node_insert_multi_perform(__node_pointer __cp, __next_pointer __pn) _NOEXCEPT;
780
781_LIBCPP_HIDE_FROM_ABI __next_pointer __node_insert_unique_prepare(size_t __nd_hash, value_type& __nd_val);
782_LIBCPP_HIDE_FROM_ABI void __node_insert_unique_perform(__node_pointer __ptr) _NOEXCEPT;
783
784public:
785_LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
786_LIBCPP_HIDE_FROM_ABI iterator __node_insert_multi(__node_pointer __nd);
787_LIBCPP_HIDE_FROM_ABI iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
788
789template <class _Key, class... _Args>
790_LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique_key_args(_Key const& __k, _Args&&... __args);
791
792template <class... _Args>
793_LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
794
795template <class _Pp>
796_LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique(_Pp&& __x) {
797return __emplace_unique_extract_key(std::forward<_Pp>(__x), __can_extract_key<_Pp, key_type>());
798}
799
800template <class _First,
801class _Second,
802__enable_if_t<__can_extract_map_key<_First, key_type, __container_value_type>::value, int> = 0>
803_LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique(_First&& __f, _Second&& __s) {
804return __emplace_unique_key_args(__f, std::forward<_First>(__f), std::forward<_Second>(__s));
805}
806
807template <class... _Args>
808_LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique(_Args&&... __args) {
809return __emplace_unique_impl(std::forward<_Args>(__args)...);
810}
811
812template <class _Pp>
813_LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
814return __emplace_unique_impl(std::forward<_Pp>(__x));
815}
816template <class _Pp>
817_LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
818return __emplace_unique_key_args(__x, std::forward<_Pp>(__x));
819}
820template <class _Pp>
821_LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
822return __emplace_unique_key_args(__x.first, std::forward<_Pp>(__x));
823}
824
825template <class... _Args>
826_LIBCPP_HIDE_FROM_ABI iterator __emplace_multi(_Args&&... __args);
827template <class... _Args>
828_LIBCPP_HIDE_FROM_ABI iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
829
830_LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __insert_unique(__container_value_type&& __x) {
831return __emplace_unique_key_args(_NodeTypes::__get_key(__x), std::move(__x));
832}
833
834template <class _Pp, __enable_if_t<!__is_same_uncvref<_Pp, __container_value_type>::value, int> = 0>
835_LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __insert_unique(_Pp&& __x) {
836return __emplace_unique(std::forward<_Pp>(__x));
837}
838
839template <class _Pp>
840_LIBCPP_HIDE_FROM_ABI iterator __insert_multi(_Pp&& __x) {
841return __emplace_multi(std::forward<_Pp>(__x));
842}
843
844template <class _Pp>
845_LIBCPP_HIDE_FROM_ABI iterator __insert_multi(const_iterator __p, _Pp&& __x) {
846return __emplace_hint_multi(__p, std::forward<_Pp>(__x));
847}
848
849_LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __insert_unique(const __container_value_type& __x) {
850return __emplace_unique_key_args(_NodeTypes::__get_key(__x), __x);
851}
852
853#if _LIBCPP_STD_VER >= 17
854template <class _NodeHandle, class _InsertReturnType>
855_LIBCPP_HIDE_FROM_ABI _InsertReturnType __node_handle_insert_unique(_NodeHandle&& __nh);
856template <class _NodeHandle>
857_LIBCPP_HIDE_FROM_ABI iterator __node_handle_insert_unique(const_iterator __hint, _NodeHandle&& __nh);
858template <class _Table>
859_LIBCPP_HIDE_FROM_ABI void __node_handle_merge_unique(_Table& __source);
860
861template <class _NodeHandle>
862_LIBCPP_HIDE_FROM_ABI iterator __node_handle_insert_multi(_NodeHandle&& __nh);
863template <class _NodeHandle>
864_LIBCPP_HIDE_FROM_ABI iterator __node_handle_insert_multi(const_iterator __hint, _NodeHandle&& __nh);
865template <class _Table>
866_LIBCPP_HIDE_FROM_ABI void __node_handle_merge_multi(_Table& __source);
867
868template <class _NodeHandle>
869_LIBCPP_HIDE_FROM_ABI _NodeHandle __node_handle_extract(key_type const& __key);
870template <class _NodeHandle>
871_LIBCPP_HIDE_FROM_ABI _NodeHandle __node_handle_extract(const_iterator __it);
872#endif
873
874_LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT;
875_LIBCPP_HIDE_FROM_ABI void __rehash_unique(size_type __n) { __rehash<true>(__n); }
876_LIBCPP_HIDE_FROM_ABI void __rehash_multi(size_type __n) { __rehash<false>(__n); }
877_LIBCPP_HIDE_FROM_ABI void __reserve_unique(size_type __n) {
878__rehash_unique(static_cast<size_type>(std::ceil(__n / max_load_factor())));
879}
880_LIBCPP_HIDE_FROM_ABI void __reserve_multi(size_type __n) {
881__rehash_multi(static_cast<size_type>(std::ceil(__n / max_load_factor())));
882}
883
884_LIBCPP_HIDE_FROM_ABI size_type bucket_count() const _NOEXCEPT { return __bucket_list_.get_deleter().size(); }
885
886_LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT;
887_LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT;
888_LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT;
889_LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT;
890
891template <class _Key>
892_LIBCPP_HIDE_FROM_ABI size_type bucket(const _Key& __k) const {
893_LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
894bucket_count() > 0, "unordered container::bucket(key) called when bucket_count() == 0");
895return std::__constrain_hash(hash_function()(__k), bucket_count());
896}
897
898template <class _Key>
899_LIBCPP_HIDE_FROM_ABI iterator find(const _Key& __x);
900template <class _Key>
901_LIBCPP_HIDE_FROM_ABI const_iterator find(const _Key& __x) const;
902
903typedef __hash_node_destructor<__node_allocator> _Dp;
904typedef unique_ptr<__node, _Dp> __node_holder;
905
906_LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __p);
907_LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __first, const_iterator __last);
908template <class _Key>
909_LIBCPP_HIDE_FROM_ABI size_type __erase_unique(const _Key& __k);
910template <class _Key>
911_LIBCPP_HIDE_FROM_ABI size_type __erase_multi(const _Key& __k);
912_LIBCPP_HIDE_FROM_ABI __node_holder remove(const_iterator __p) _NOEXCEPT;
913
914template <class _Key>
915_LIBCPP_HIDE_FROM_ABI size_type __count_unique(const _Key& __k) const;
916template <class _Key>
917_LIBCPP_HIDE_FROM_ABI size_type __count_multi(const _Key& __k) const;
918
919template <class _Key>
920_LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> __equal_range_unique(const _Key& __k);
921template <class _Key>
922_LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> __equal_range_unique(const _Key& __k) const;
923
924template <class _Key>
925_LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> __equal_range_multi(const _Key& __k);
926template <class _Key>
927_LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> __equal_range_multi(const _Key& __k) const;
928
929_LIBCPP_HIDE_FROM_ABI void swap(__hash_table& __u)
930#if _LIBCPP_STD_VER <= 11
931_NOEXCEPT_(__is_nothrow_swappable_v<hasher>&& __is_nothrow_swappable_v<key_equal> &&
932(!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value ||
933__is_nothrow_swappable_v<__pointer_allocator>) &&
934(!__node_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<__node_allocator>));
935#else
936_NOEXCEPT_(__is_nothrow_swappable_v<hasher>&& __is_nothrow_swappable_v<key_equal>);
937#endif
938
939_LIBCPP_HIDE_FROM_ABI size_type max_bucket_count() const _NOEXCEPT { return max_size(); }
940_LIBCPP_HIDE_FROM_ABI size_type bucket_size(size_type __n) const;
941_LIBCPP_HIDE_FROM_ABI float load_factor() const _NOEXCEPT {
942size_type __bc = bucket_count();
943return __bc != 0 ? (float)size() / __bc : 0.f;
944}
945_LIBCPP_HIDE_FROM_ABI void max_load_factor(float __mlf) _NOEXCEPT {
946// While passing a non-positive load factor is undefined behavior, in practice the result will be benign (the
947// call will be equivalent to `max_load_factor(load_factor())`, which is also the case for passing a valid value
948// less than the current `load_factor`).
949_LIBCPP_ASSERT_PEDANTIC(__mlf > 0, "unordered container::max_load_factor(lf) called with lf <= 0");
950max_load_factor() = std::max(__mlf, load_factor());
951}
952
953_LIBCPP_HIDE_FROM_ABI local_iterator begin(size_type __n) {
954_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
955__n < bucket_count(), "unordered container::begin(n) called with n >= bucket_count()");
956return local_iterator(__bucket_list_[__n], __n, bucket_count());
957}
958
959_LIBCPP_HIDE_FROM_ABI local_iterator end(size_type __n) {
960_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
961__n < bucket_count(), "unordered container::end(n) called with n >= bucket_count()");
962return local_iterator(nullptr, __n, bucket_count());
963}
964
965_LIBCPP_HIDE_FROM_ABI const_local_iterator cbegin(size_type __n) const {
966_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
967__n < bucket_count(), "unordered container::cbegin(n) called with n >= bucket_count()");
968return const_local_iterator(__bucket_list_[__n], __n, bucket_count());
969}
970
971_LIBCPP_HIDE_FROM_ABI const_local_iterator cend(size_type __n) const {
972_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
973__n < bucket_count(), "unordered container::cend(n) called with n >= bucket_count()");
974return const_local_iterator(nullptr, __n, bucket_count());
975}
976
977private:
978template <bool _UniqueKeys>
979_LIBCPP_HIDE_FROM_ABI void __rehash(size_type __n);
980template <bool _UniqueKeys>
981_LIBCPP_HIDE_FROM_ABI void __do_rehash(size_type __n);
982
983template <class... _Args>
984_LIBCPP_HIDE_FROM_ABI __node_holder __construct_node(_Args&&... __args);
985
986template <class _First, class... _Rest>
987_LIBCPP_HIDE_FROM_ABI __node_holder __construct_node_hash(size_t __hash, _First&& __f, _Rest&&... __rest);
988
989_LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __hash_table& __u) {
990__copy_assign_alloc(__u, integral_constant<bool, __node_traits::propagate_on_container_copy_assignment::value>());
991}
992_LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __hash_table& __u, true_type);
993_LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __hash_table&, false_type) {}
994
995_LIBCPP_HIDE_FROM_ABI void __move_assign(__hash_table& __u, false_type);
996_LIBCPP_HIDE_FROM_ABI void __move_assign(__hash_table& __u, true_type)
997_NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value&& is_nothrow_move_assignable<hasher>::value&&
998is_nothrow_move_assignable<key_equal>::value);
999_LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__hash_table& __u) _NOEXCEPT_(
1000!__node_traits::propagate_on_container_move_assignment::value ||
1001(is_nothrow_move_assignable<__pointer_allocator>::value && is_nothrow_move_assignable<__node_allocator>::value)) {
1002__move_assign_alloc(__u, integral_constant<bool, __node_traits::propagate_on_container_move_assignment::value>());
1003}
1004_LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__hash_table& __u, true_type) _NOEXCEPT_(
1005is_nothrow_move_assignable<__pointer_allocator>::value&& is_nothrow_move_assignable<__node_allocator>::value) {
1006__bucket_list_.get_deleter().__alloc() = std::move(__u.__bucket_list_.get_deleter().__alloc());
1007__node_alloc() = std::move(__u.__node_alloc());
1008}
1009_LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
1010
1011_LIBCPP_HIDE_FROM_ABI void __deallocate_node(__next_pointer __np) _NOEXCEPT;
1012_LIBCPP_HIDE_FROM_ABI __next_pointer __detach() _NOEXCEPT;
1013
1014template <class, class, class, class, class>
1015friend class _LIBCPP_TEMPLATE_VIS unordered_map;
1016template <class, class, class, class, class>
1017friend class _LIBCPP_TEMPLATE_VIS unordered_multimap;
1018};
1019
1020template <class _Tp, class _Hash, class _Equal, class _Alloc>
1021inline __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table() _NOEXCEPT_(
1022is_nothrow_default_constructible<__bucket_list>::value&& is_nothrow_default_constructible<__first_node>::value&&
1023is_nothrow_default_constructible<__node_allocator>::value&& is_nothrow_default_constructible<hasher>::value&&
1024is_nothrow_default_constructible<key_equal>::value)
1025: __p2_(0, __default_init_tag()), __p3_(1.0f, __default_init_tag()) {}
1026
1027template <class _Tp, class _Hash, class _Equal, class _Alloc>
1028inline __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf, const key_equal& __eql)
1029: __bucket_list_(nullptr, __bucket_list_deleter()), __p1_(), __p2_(0, __hf), __p3_(1.0f, __eql) {}
1030
1031template <class _Tp, class _Hash, class _Equal, class _Alloc>
1032__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(
1033const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
1034: __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1035__p1_(__default_init_tag(), __node_allocator(__a)),
1036__p2_(0, __hf),
1037__p3_(1.0f, __eql) {}
1038
1039template <class _Tp, class _Hash, class _Equal, class _Alloc>
1040__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a)
1041: __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1042__p1_(__default_init_tag(), __node_allocator(__a)),
1043__p2_(0, __default_init_tag()),
1044__p3_(1.0f, __default_init_tag()) {}
1045
1046template <class _Tp, class _Hash, class _Equal, class _Alloc>
1047__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u)
1048: __bucket_list_(nullptr,
1049__bucket_list_deleter(allocator_traits<__pointer_allocator>::select_on_container_copy_construction(
1050__u.__bucket_list_.get_deleter().__alloc()),
10510)),
1052__p1_(__default_init_tag(),
1053allocator_traits<__node_allocator>::select_on_container_copy_construction(__u.__node_alloc())),
1054__p2_(0, __u.hash_function()),
1055__p3_(__u.__p3_) {}
1056
1057template <class _Tp, class _Hash, class _Equal, class _Alloc>
1058__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u, const allocator_type& __a)
1059: __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1060__p1_(__default_init_tag(), __node_allocator(__a)),
1061__p2_(0, __u.hash_function()),
1062__p3_(__u.__p3_) {}
1063
1064template <class _Tp, class _Hash, class _Equal, class _Alloc>
1065__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u) _NOEXCEPT_(
1066is_nothrow_move_constructible<__bucket_list>::value&& is_nothrow_move_constructible<__first_node>::value&&
1067is_nothrow_move_constructible<__node_allocator>::value&& is_nothrow_move_constructible<hasher>::value&&
1068is_nothrow_move_constructible<key_equal>::value)
1069: __bucket_list_(std::move(__u.__bucket_list_)),
1070__p1_(std::move(__u.__p1_)),
1071__p2_(std::move(__u.__p2_)),
1072__p3_(std::move(__u.__p3_)) {
1073if (size() > 0) {
1074__bucket_list_[std::__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] = __p1_.first().__ptr();
1075__u.__p1_.first().__next_ = nullptr;
1076__u.size() = 0;
1077}
1078}
1079
1080template <class _Tp, class _Hash, class _Equal, class _Alloc>
1081__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u, const allocator_type& __a)
1082: __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1083__p1_(__default_init_tag(), __node_allocator(__a)),
1084__p2_(0, std::move(__u.hash_function())),
1085__p3_(std::move(__u.__p3_)) {
1086if (__a == allocator_type(__u.__node_alloc())) {
1087__bucket_list_.reset(__u.__bucket_list_.release());
1088__bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1089__u.__bucket_list_.get_deleter().size() = 0;
1090if (__u.size() > 0) {
1091__p1_.first().__next_ = __u.__p1_.first().__next_;
1092__u.__p1_.first().__next_ = nullptr;
1093__bucket_list_[std::__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] = __p1_.first().__ptr();
1094size() = __u.size();
1095__u.size() = 0;
1096}
1097}
1098}
1099
1100template <class _Tp, class _Hash, class _Equal, class _Alloc>
1101__hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table() {
1102#if defined(_LIBCPP_CXX03_LANG)
1103static_assert(is_copy_constructible<key_equal>::value, "Predicate must be copy-constructible.");
1104static_assert(is_copy_constructible<hasher>::value, "Hasher must be copy-constructible.");
1105#endif
1106
1107__deallocate_node(__p1_.first().__next_);
1108}
1109
1110template <class _Tp, class _Hash, class _Equal, class _Alloc>
1111void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(const __hash_table& __u, true_type) {
1112if (__node_alloc() != __u.__node_alloc()) {
1113clear();
1114__bucket_list_.reset();
1115__bucket_list_.get_deleter().size() = 0;
1116}
1117__bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
1118__node_alloc() = __u.__node_alloc();
1119}
1120
1121template <class _Tp, class _Hash, class _Equal, class _Alloc>
1122__hash_table<_Tp, _Hash, _Equal, _Alloc>& __hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u) {
1123if (this != std::addressof(__u)) {
1124__copy_assign_alloc(__u);
1125hash_function() = __u.hash_function();
1126key_eq() = __u.key_eq();
1127max_load_factor() = __u.max_load_factor();
1128__assign_multi(__u.begin(), __u.end());
1129}
1130return *this;
1131}
1132
1133template <class _Tp, class _Hash, class _Equal, class _Alloc>
1134void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate_node(__next_pointer __np) _NOEXCEPT {
1135__node_allocator& __na = __node_alloc();
1136while (__np != nullptr) {
1137__next_pointer __next = __np->__next_;
1138__node_pointer __real_np = __np->__upcast();
1139__node_traits::destroy(__na, _NodeTypes::__get_ptr(__real_np->__get_value()));
1140std::__destroy_at(std::addressof(*__real_np));
1141__node_traits::deallocate(__na, __real_np, 1);
1142__np = __next;
1143}
1144}
1145
1146template <class _Tp, class _Hash, class _Equal, class _Alloc>
1147typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
1148__hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT {
1149size_type __bc = bucket_count();
1150for (size_type __i = 0; __i < __bc; ++__i)
1151__bucket_list_[__i] = nullptr;
1152size() = 0;
1153__next_pointer __cache = __p1_.first().__next_;
1154__p1_.first().__next_ = nullptr;
1155return __cache;
1156}
1157
1158template <class _Tp, class _Hash, class _Equal, class _Alloc>
1159void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(__hash_table& __u, true_type)
1160_NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value&& is_nothrow_move_assignable<hasher>::value&&
1161is_nothrow_move_assignable<key_equal>::value) {
1162clear();
1163__bucket_list_.reset(__u.__bucket_list_.release());
1164__bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1165__u.__bucket_list_.get_deleter().size() = 0;
1166__move_assign_alloc(__u);
1167size() = __u.size();
1168hash_function() = std::move(__u.hash_function());
1169max_load_factor() = __u.max_load_factor();
1170key_eq() = std::move(__u.key_eq());
1171__p1_.first().__next_ = __u.__p1_.first().__next_;
1172if (size() > 0) {
1173__bucket_list_[std::__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] = __p1_.first().__ptr();
1174__u.__p1_.first().__next_ = nullptr;
1175__u.size() = 0;
1176}
1177}
1178
1179template <class _Tp, class _Hash, class _Equal, class _Alloc>
1180void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(__hash_table& __u, false_type) {
1181if (__node_alloc() == __u.__node_alloc())
1182__move_assign(__u, true_type());
1183else {
1184hash_function() = std::move(__u.hash_function());
1185key_eq() = std::move(__u.key_eq());
1186max_load_factor() = __u.max_load_factor();
1187if (bucket_count() != 0) {
1188__next_pointer __cache = __detach();
1189#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1190try {
1191#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1192const_iterator __i = __u.begin();
1193while (__cache != nullptr && __u.size() != 0) {
1194__cache->__upcast()->__get_value() = std::move(__u.remove(__i++)->__get_value());
1195__next_pointer __next = __cache->__next_;
1196__node_insert_multi(__cache->__upcast());
1197__cache = __next;
1198}
1199#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1200} catch (...) {
1201__deallocate_node(__cache);
1202throw;
1203}
1204#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1205__deallocate_node(__cache);
1206}
1207const_iterator __i = __u.begin();
1208while (__u.size() != 0) {
1209__node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__get_value()));
1210__node_insert_multi(__h.get());
1211__h.release();
1212}
1213}
1214}
1215
1216template <class _Tp, class _Hash, class _Equal, class _Alloc>
1217inline __hash_table<_Tp, _Hash, _Equal, _Alloc>&
1218__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u) _NOEXCEPT_(
1219__node_traits::propagate_on_container_move_assignment::value&& is_nothrow_move_assignable<__node_allocator>::value&&
1220is_nothrow_move_assignable<hasher>::value&& is_nothrow_move_assignable<key_equal>::value) {
1221__move_assign(__u, integral_constant<bool, __node_traits::propagate_on_container_move_assignment::value>());
1222return *this;
1223}
1224
1225template <class _Tp, class _Hash, class _Equal, class _Alloc>
1226template <class _InputIterator>
1227void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first, _InputIterator __last) {
1228typedef iterator_traits<_InputIterator> _ITraits;
1229typedef typename _ITraits::value_type _ItValueType;
1230static_assert(is_same<_ItValueType, __container_value_type>::value,
1231"__assign_unique may only be called with the containers value type");
1232
1233if (bucket_count() != 0) {
1234__next_pointer __cache = __detach();
1235#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1236try {
1237#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1238for (; __cache != nullptr && __first != __last; ++__first) {
1239__cache->__upcast()->__get_value() = *__first;
1240__next_pointer __next = __cache->__next_;
1241__node_insert_unique(__cache->__upcast());
1242__cache = __next;
1243}
1244#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1245} catch (...) {
1246__deallocate_node(__cache);
1247throw;
1248}
1249#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1250__deallocate_node(__cache);
1251}
1252for (; __first != __last; ++__first)
1253__insert_unique(*__first);
1254}
1255
1256template <class _Tp, class _Hash, class _Equal, class _Alloc>
1257template <class _InputIterator>
1258void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first, _InputIterator __last) {
1259typedef iterator_traits<_InputIterator> _ITraits;
1260typedef typename _ITraits::value_type _ItValueType;
1261static_assert(
1262(is_same<_ItValueType, __container_value_type>::value || is_same<_ItValueType, __node_value_type>::value),
1263"__assign_multi may only be called with the containers value type"
1264" or the nodes value type");
1265if (bucket_count() != 0) {
1266__next_pointer __cache = __detach();
1267#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1268try {
1269#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1270for (; __cache != nullptr && __first != __last; ++__first) {
1271__cache->__upcast()->__get_value() = *__first;
1272__next_pointer __next = __cache->__next_;
1273__node_insert_multi(__cache->__upcast());
1274__cache = __next;
1275}
1276#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1277} catch (...) {
1278__deallocate_node(__cache);
1279throw;
1280}
1281#endif // _LIBCPP_HAS_NO_EXCEPTIONS
1282__deallocate_node(__cache);
1283}
1284for (; __first != __last; ++__first)
1285__insert_multi(_NodeTypes::__get_value(*__first));
1286}
1287
1288template <class _Tp, class _Hash, class _Equal, class _Alloc>
1289inline typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1290__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT {
1291return iterator(__p1_.first().__next_);
1292}
1293
1294template <class _Tp, class _Hash, class _Equal, class _Alloc>
1295inline typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1296__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT {
1297return iterator(nullptr);
1298}
1299
1300template <class _Tp, class _Hash, class _Equal, class _Alloc>
1301inline typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
1302__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT {
1303return const_iterator(__p1_.first().__next_);
1304}
1305
1306template <class _Tp, class _Hash, class _Equal, class _Alloc>
1307inline typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
1308__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT {
1309return const_iterator(nullptr);
1310}
1311
1312template <class _Tp, class _Hash, class _Equal, class _Alloc>
1313void __hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT {
1314if (size() > 0) {
1315__deallocate_node(__p1_.first().__next_);
1316__p1_.first().__next_ = nullptr;
1317size_type __bc = bucket_count();
1318for (size_type __i = 0; __i < __bc; ++__i)
1319__bucket_list_[__i] = nullptr;
1320size() = 0;
1321}
1322}
1323
1324// Prepare the container for an insertion of the value __value with the hash
1325// __hash. This does a lookup into the container to see if __value is already
1326// present, and performs a rehash if necessary. Returns a pointer to the
1327// existing element if it exists, otherwise nullptr.
1328//
1329// Note that this function does forward exceptions if key_eq() throws, and never
1330// mutates __value or actually inserts into the map.
1331template <class _Tp, class _Hash, class _Equal, class _Alloc>
1332_LIBCPP_HIDE_FROM_ABI typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
1333__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique_prepare(size_t __hash, value_type& __value) {
1334size_type __bc = bucket_count();
1335
1336if (__bc != 0) {
1337size_t __chash = std::__constrain_hash(__hash, __bc);
1338__next_pointer __ndptr = __bucket_list_[__chash];
1339if (__ndptr != nullptr) {
1340for (__ndptr = __ndptr->__next_;
1341__ndptr != nullptr &&
1342(__ndptr->__hash() == __hash || std::__constrain_hash(__ndptr->__hash(), __bc) == __chash);
1343__ndptr = __ndptr->__next_) {
1344if ((__ndptr->__hash() == __hash) && key_eq()(__ndptr->__upcast()->__get_value(), __value))
1345return __ndptr;
1346}
1347}
1348}
1349if (size() + 1 > __bc * max_load_factor() || __bc == 0) {
1350__rehash_unique(std::max<size_type>(
13512 * __bc + !std::__is_hash_power2(__bc), size_type(std::ceil(float(size() + 1) / max_load_factor()))));
1352}
1353return nullptr;
1354}
1355
1356// Insert the node __nd into the container by pushing it into the right bucket,
1357// and updating size(). Assumes that __nd->__hash is up-to-date, and that
1358// rehashing has already occurred and that no element with the same key exists
1359// in the map.
1360template <class _Tp, class _Hash, class _Equal, class _Alloc>
1361_LIBCPP_HIDE_FROM_ABI void
1362__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique_perform(__node_pointer __nd) _NOEXCEPT {
1363size_type __bc = bucket_count();
1364size_t __chash = std::__constrain_hash(__nd->__hash(), __bc);
1365// insert_after __bucket_list_[__chash], or __first_node if bucket is null
1366__next_pointer __pn = __bucket_list_[__chash];
1367if (__pn == nullptr) {
1368__pn = __p1_.first().__ptr();
1369__nd->__next_ = __pn->__next_;
1370__pn->__next_ = __nd->__ptr();
1371// fix up __bucket_list_
1372__bucket_list_[__chash] = __pn;
1373if (__nd->__next_ != nullptr)
1374__bucket_list_[std::__constrain_hash(__nd->__next_->__hash(), __bc)] = __nd->__ptr();
1375} else {
1376__nd->__next_ = __pn->__next_;
1377__pn->__next_ = __nd->__ptr();
1378}
1379++size();
1380}
1381
1382template <class _Tp, class _Hash, class _Equal, class _Alloc>
1383pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1384__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd) {
1385__nd->__hash_ = hash_function()(__nd->__get_value());
1386__next_pointer __existing_node = __node_insert_unique_prepare(__nd->__hash(), __nd->__get_value());
1387
1388// Insert the node, unless it already exists in the container.
1389bool __inserted = false;
1390if (__existing_node == nullptr) {
1391__node_insert_unique_perform(__nd);
1392__existing_node = __nd->__ptr();
1393__inserted = true;
1394}
1395return pair<iterator, bool>(iterator(__existing_node), __inserted);
1396}
1397
1398// Prepare the container for an insertion of the value __cp_val with the hash
1399// __cp_hash. This does a lookup into the container to see if __cp_value is
1400// already present, and performs a rehash if necessary. Returns a pointer to the
1401// last occurrence of __cp_val in the map.
1402//
1403// Note that this function does forward exceptions if key_eq() throws, and never
1404// mutates __value or actually inserts into the map.
1405template <class _Tp, class _Hash, class _Equal, class _Alloc>
1406typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
1407__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi_prepare(size_t __cp_hash, value_type& __cp_val) {
1408size_type __bc = bucket_count();
1409if (size() + 1 > __bc * max_load_factor() || __bc == 0) {
1410__rehash_multi(std::max<size_type>(
14112 * __bc + !std::__is_hash_power2(__bc), size_type(std::ceil(float(size() + 1) / max_load_factor()))));
1412__bc = bucket_count();
1413}
1414size_t __chash = std::__constrain_hash(__cp_hash, __bc);
1415__next_pointer __pn = __bucket_list_[__chash];
1416if (__pn != nullptr) {
1417for (bool __found = false;
1418__pn->__next_ != nullptr && std::__constrain_hash(__pn->__next_->__hash(), __bc) == __chash;
1419__pn = __pn->__next_) {
1420// __found key_eq() action
1421// false false loop
1422// true true loop
1423// false true set __found to true
1424// true false break
1425if (__found !=
1426(__pn->__next_->__hash() == __cp_hash && key_eq()(__pn->__next_->__upcast()->__get_value(), __cp_val))) {
1427if (!__found)
1428__found = true;
1429else
1430break;
1431}
1432}
1433}
1434return __pn;
1435}
1436
1437// Insert the node __cp into the container after __pn (which is the last node in
1438// the bucket that compares equal to __cp). Rehashing, and checking for
1439// uniqueness has already been performed (in __node_insert_multi_prepare), so
1440// all we need to do is update the bucket and size(). Assumes that __cp->__hash
1441// is up-to-date.
1442template <class _Tp, class _Hash, class _Equal, class _Alloc>
1443void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi_perform(
1444__node_pointer __cp, __next_pointer __pn) _NOEXCEPT {
1445size_type __bc = bucket_count();
1446size_t __chash = std::__constrain_hash(__cp->__hash_, __bc);
1447if (__pn == nullptr) {
1448__pn = __p1_.first().__ptr();
1449__cp->__next_ = __pn->__next_;
1450__pn->__next_ = __cp->__ptr();
1451// fix up __bucket_list_
1452__bucket_list_[__chash] = __pn;
1453if (__cp->__next_ != nullptr)
1454__bucket_list_[std::__constrain_hash(__cp->__next_->__hash(), __bc)] = __cp->__ptr();
1455} else {
1456__cp->__next_ = __pn->__next_;
1457__pn->__next_ = __cp->__ptr();
1458if (__cp->__next_ != nullptr) {
1459size_t __nhash = std::__constrain_hash(__cp->__next_->__hash(), __bc);
1460if (__nhash != __chash)
1461__bucket_list_[__nhash] = __cp->__ptr();
1462}
1463}
1464++size();
1465}
1466
1467template <class _Tp, class _Hash, class _Equal, class _Alloc>
1468typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1469__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp) {
1470__cp->__hash_ = hash_function()(__cp->__get_value());
1471__next_pointer __pn = __node_insert_multi_prepare(__cp->__hash(), __cp->__get_value());
1472__node_insert_multi_perform(__cp, __pn);
1473
1474return iterator(__cp->__ptr());
1475}
1476
1477template <class _Tp, class _Hash, class _Equal, class _Alloc>
1478typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1479__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(const_iterator __p, __node_pointer __cp) {
1480if (__p != end() && key_eq()(*__p, __cp->__get_value())) {
1481__next_pointer __np = __p.__node_;
1482__cp->__hash_ = __np->__hash();
1483size_type __bc = bucket_count();
1484if (size() + 1 > __bc * max_load_factor() || __bc == 0) {
1485__rehash_multi(std::max<size_type>(
14862 * __bc + !std::__is_hash_power2(__bc), size_type(std::ceil(float(size() + 1) / max_load_factor()))));
1487__bc = bucket_count();
1488}
1489size_t __chash = std::__constrain_hash(__cp->__hash_, __bc);
1490__next_pointer __pp = __bucket_list_[__chash];
1491while (__pp->__next_ != __np)
1492__pp = __pp->__next_;
1493__cp->__next_ = __np;
1494__pp->__next_ = static_cast<__next_pointer>(__cp);
1495++size();
1496return iterator(static_cast<__next_pointer>(__cp));
1497}
1498return __node_insert_multi(__cp);
1499}
1500
1501template <class _Tp, class _Hash, class _Equal, class _Alloc>
1502template <class _Key, class... _Args>
1503pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1504__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args) {
1505size_t __hash = hash_function()(__k);
1506size_type __bc = bucket_count();
1507bool __inserted = false;
1508__next_pointer __nd;
1509size_t __chash;
1510if (__bc != 0) {
1511__chash = std::__constrain_hash(__hash, __bc);
1512__nd = __bucket_list_[__chash];
1513if (__nd != nullptr) {
1514for (__nd = __nd->__next_;
1515__nd != nullptr && (__nd->__hash() == __hash || std::__constrain_hash(__nd->__hash(), __bc) == __chash);
1516__nd = __nd->__next_) {
1517if ((__nd->__hash() == __hash) && key_eq()(__nd->__upcast()->__get_value(), __k))
1518goto __done;
1519}
1520}
1521}
1522{
1523__node_holder __h = __construct_node_hash(__hash, std::forward<_Args>(__args)...);
1524if (size() + 1 > __bc * max_load_factor() || __bc == 0) {
1525__rehash_unique(std::max<size_type>(
15262 * __bc + !std::__is_hash_power2(__bc), size_type(std::ceil(float(size() + 1) / max_load_factor()))));
1527__bc = bucket_count();
1528__chash = std::__constrain_hash(__hash, __bc);
1529}
1530// insert_after __bucket_list_[__chash], or __first_node if bucket is null
1531__next_pointer __pn = __bucket_list_[__chash];
1532if (__pn == nullptr) {
1533__pn = __p1_.first().__ptr();
1534__h->__next_ = __pn->__next_;
1535__pn->__next_ = __h.get()->__ptr();
1536// fix up __bucket_list_
1537__bucket_list_[__chash] = __pn;
1538if (__h->__next_ != nullptr)
1539__bucket_list_[std::__constrain_hash(__h->__next_->__hash(), __bc)] = __h.get()->__ptr();
1540} else {
1541__h->__next_ = __pn->__next_;
1542__pn->__next_ = static_cast<__next_pointer>(__h.get());
1543}
1544__nd = static_cast<__next_pointer>(__h.release());
1545// increment size
1546++size();
1547__inserted = true;
1548}
1549__done:
1550return pair<iterator, bool>(iterator(__nd), __inserted);
1551}
1552
1553template <class _Tp, class _Hash, class _Equal, class _Alloc>
1554template <class... _Args>
1555pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1556__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_impl(_Args&&... __args) {
1557__node_holder __h = __construct_node(std::forward<_Args>(__args)...);
1558pair<iterator, bool> __r = __node_insert_unique(__h.get());
1559if (__r.second)
1560__h.release();
1561return __r;
1562}
1563
1564template <class _Tp, class _Hash, class _Equal, class _Alloc>
1565template <class... _Args>
1566typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1567__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args) {
1568__node_holder __h = __construct_node(std::forward<_Args>(__args)...);
1569iterator __r = __node_insert_multi(__h.get());
1570__h.release();
1571return __r;
1572}
1573
1574template <class _Tp, class _Hash, class _Equal, class _Alloc>
1575template <class... _Args>
1576typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1577__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(const_iterator __p, _Args&&... __args) {
1578__node_holder __h = __construct_node(std::forward<_Args>(__args)...);
1579iterator __r = __node_insert_multi(__p, __h.get());
1580__h.release();
1581return __r;
1582}
1583
1584#if _LIBCPP_STD_VER >= 17
1585template <class _Tp, class _Hash, class _Equal, class _Alloc>
1586template <class _NodeHandle, class _InsertReturnType>
1587_LIBCPP_HIDE_FROM_ABI _InsertReturnType
1588__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_unique(_NodeHandle&& __nh) {
1589if (__nh.empty())
1590return _InsertReturnType{end(), false, _NodeHandle()};
1591pair<iterator, bool> __result = __node_insert_unique(__nh.__ptr_);
1592if (__result.second)
1593__nh.__release_ptr();
1594return _InsertReturnType{__result.first, __result.second, std::move(__nh)};
1595}
1596
1597template <class _Tp, class _Hash, class _Equal, class _Alloc>
1598template <class _NodeHandle>
1599_LIBCPP_HIDE_FROM_ABI typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1600__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_unique(const_iterator, _NodeHandle&& __nh) {
1601if (__nh.empty())
1602return end();
1603pair<iterator, bool> __result = __node_insert_unique(__nh.__ptr_);
1604if (__result.second)
1605__nh.__release_ptr();
1606return __result.first;
1607}
1608
1609template <class _Tp, class _Hash, class _Equal, class _Alloc>
1610template <class _NodeHandle>
1611_LIBCPP_HIDE_FROM_ABI _NodeHandle
1612__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_extract(key_type const& __key) {
1613iterator __i = find(__key);
1614if (__i == end())
1615return _NodeHandle();
1616return __node_handle_extract<_NodeHandle>(__i);
1617}
1618
1619template <class _Tp, class _Hash, class _Equal, class _Alloc>
1620template <class _NodeHandle>
1621_LIBCPP_HIDE_FROM_ABI _NodeHandle __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_extract(const_iterator __p) {
1622allocator_type __alloc(__node_alloc());
1623return _NodeHandle(remove(__p).release(), __alloc);
1624}
1625
1626template <class _Tp, class _Hash, class _Equal, class _Alloc>
1627template <class _Table>
1628_LIBCPP_HIDE_FROM_ABI void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_merge_unique(_Table& __source) {
1629static_assert(is_same<__node, typename _Table::__node>::value, "");
1630
1631for (typename _Table::iterator __it = __source.begin(); __it != __source.end();) {
1632__node_pointer __src_ptr = __it.__node_->__upcast();
1633size_t __hash = hash_function()(__src_ptr->__get_value());
1634__next_pointer __existing_node = __node_insert_unique_prepare(__hash, __src_ptr->__get_value());
1635auto __prev_iter = __it++;
1636if (__existing_node == nullptr) {
1637(void)__source.remove(__prev_iter).release();
1638__src_ptr->__hash_ = __hash;
1639__node_insert_unique_perform(__src_ptr);
1640}
1641}
1642}
1643
1644template <class _Tp, class _Hash, class _Equal, class _Alloc>
1645template <class _NodeHandle>
1646_LIBCPP_HIDE_FROM_ABI typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1647__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_multi(_NodeHandle&& __nh) {
1648if (__nh.empty())
1649return end();
1650iterator __result = __node_insert_multi(__nh.__ptr_);
1651__nh.__release_ptr();
1652return __result;
1653}
1654
1655template <class _Tp, class _Hash, class _Equal, class _Alloc>
1656template <class _NodeHandle>
1657_LIBCPP_HIDE_FROM_ABI typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1658__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_multi(const_iterator __hint, _NodeHandle&& __nh) {
1659if (__nh.empty())
1660return end();
1661iterator __result = __node_insert_multi(__hint, __nh.__ptr_);
1662__nh.__release_ptr();
1663return __result;
1664}
1665
1666template <class _Tp, class _Hash, class _Equal, class _Alloc>
1667template <class _Table>
1668_LIBCPP_HIDE_FROM_ABI void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_merge_multi(_Table& __source) {
1669static_assert(is_same<typename _Table::__node, __node>::value, "");
1670
1671for (typename _Table::iterator __it = __source.begin(); __it != __source.end();) {
1672__node_pointer __src_ptr = __it.__node_->__upcast();
1673size_t __src_hash = hash_function()(__src_ptr->__get_value());
1674__next_pointer __pn = __node_insert_multi_prepare(__src_hash, __src_ptr->__get_value());
1675(void)__source.remove(__it++).release();
1676__src_ptr->__hash_ = __src_hash;
1677__node_insert_multi_perform(__src_ptr, __pn);
1678}
1679}
1680#endif // _LIBCPP_STD_VER >= 17
1681
1682template <class _Tp, class _Hash, class _Equal, class _Alloc>
1683template <bool _UniqueKeys>
1684void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __n) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK {
1685if (__n == 1)
1686__n = 2;
1687else if (__n & (__n - 1))
1688__n = std::__next_prime(__n);
1689size_type __bc = bucket_count();
1690if (__n > __bc)
1691__do_rehash<_UniqueKeys>(__n);
1692else if (__n < __bc) {
1693__n = std::max<size_type>(
1694__n,
1695std::__is_hash_power2(__bc) ? std::__next_hash_pow2(size_t(std::ceil(float(size()) / max_load_factor())))
1696: std::__next_prime(size_t(std::ceil(float(size()) / max_load_factor()))));
1697if (__n < __bc)
1698__do_rehash<_UniqueKeys>(__n);
1699}
1700}
1701
1702template <class _Tp, class _Hash, class _Equal, class _Alloc>
1703template <bool _UniqueKeys>
1704void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__do_rehash(size_type __nbc) {
1705__pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
1706__bucket_list_.reset(__nbc > 0 ? __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
1707__bucket_list_.get_deleter().size() = __nbc;
1708if (__nbc > 0) {
1709for (size_type __i = 0; __i < __nbc; ++__i)
1710__bucket_list_[__i] = nullptr;
1711__next_pointer __pp = __p1_.first().__ptr();
1712__next_pointer __cp = __pp->__next_;
1713if (__cp != nullptr) {
1714size_type __chash = std::__constrain_hash(__cp->__hash(), __nbc);
1715__bucket_list_[__chash] = __pp;
1716size_type __phash = __chash;
1717for (__pp = __cp, void(), __cp = __cp->__next_; __cp != nullptr; __cp = __pp->__next_) {
1718__chash = std::__constrain_hash(__cp->__hash(), __nbc);
1719if (__chash == __phash)
1720__pp = __cp;
1721else {
1722if (__bucket_list_[__chash] == nullptr) {
1723__bucket_list_[__chash] = __pp;
1724__pp = __cp;
1725__phash = __chash;
1726} else {
1727__next_pointer __np = __cp;
1728if _LIBCPP_CONSTEXPR_SINCE_CXX17 (!_UniqueKeys) {
1729for (; __np->__next_ != nullptr &&
1730key_eq()(__cp->__upcast()->__get_value(), __np->__next_->__upcast()->__get_value());
1731__np = __np->__next_)
1732;
1733}
1734__pp->__next_ = __np->__next_;
1735__np->__next_ = __bucket_list_[__chash]->__next_;
1736__bucket_list_[__chash]->__next_ = __cp;
1737}
1738}
1739}
1740}
1741}
1742}
1743
1744template <class _Tp, class _Hash, class _Equal, class _Alloc>
1745template <class _Key>
1746typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1747__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) {
1748size_t __hash = hash_function()(__k);
1749size_type __bc = bucket_count();
1750if (__bc != 0) {
1751size_t __chash = std::__constrain_hash(__hash, __bc);
1752__next_pointer __nd = __bucket_list_[__chash];
1753if (__nd != nullptr) {
1754for (__nd = __nd->__next_;
1755__nd != nullptr && (__nd->__hash() == __hash || std::__constrain_hash(__nd->__hash(), __bc) == __chash);
1756__nd = __nd->__next_) {
1757if ((__nd->__hash() == __hash) && key_eq()(__nd->__upcast()->__get_value(), __k))
1758return iterator(__nd);
1759}
1760}
1761}
1762return end();
1763}
1764
1765template <class _Tp, class _Hash, class _Equal, class _Alloc>
1766template <class _Key>
1767typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
1768__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const {
1769size_t __hash = hash_function()(__k);
1770size_type __bc = bucket_count();
1771if (__bc != 0) {
1772size_t __chash = std::__constrain_hash(__hash, __bc);
1773__next_pointer __nd = __bucket_list_[__chash];
1774if (__nd != nullptr) {
1775for (__nd = __nd->__next_;
1776__nd != nullptr && (__hash == __nd->__hash() || std::__constrain_hash(__nd->__hash(), __bc) == __chash);
1777__nd = __nd->__next_) {
1778if ((__nd->__hash() == __hash) && key_eq()(__nd->__upcast()->__get_value(), __k))
1779return const_iterator(__nd);
1780}
1781}
1782}
1783return end();
1784}
1785
1786template <class _Tp, class _Hash, class _Equal, class _Alloc>
1787template <class... _Args>
1788typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
1789__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&&... __args) {
1790static_assert(!__is_hash_value_type<_Args...>::value, "Construct cannot be called with a hash value type");
1791__node_allocator& __na = __node_alloc();
1792__node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
1793
1794// Begin the lifetime of the node itself. Note that this doesn't begin the lifetime of the value
1795// held inside the node, since we need to use the allocator's construct() method for that.
1796//
1797// We don't use the allocator's construct() method to construct the node itself since the
1798// Cpp17FooInsertable named requirements don't require the allocator's construct() method
1799// to work on anything other than the value_type.
1800std::__construct_at(std::addressof(*__h), /* next = */ nullptr, /* hash = */ 0);
1801
1802// Now construct the value_type using the allocator's construct() method.
1803__node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__get_value()), std::forward<_Args>(__args)...);
1804__h.get_deleter().__value_constructed = true;
1805
1806__h->__hash_ = hash_function()(__h->__get_value());
1807return __h;
1808}
1809
1810template <class _Tp, class _Hash, class _Equal, class _Alloc>
1811template <class _First, class... _Rest>
1812typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
1813__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(size_t __hash, _First&& __f, _Rest&&... __rest) {
1814static_assert(!__is_hash_value_type<_First, _Rest...>::value, "Construct cannot be called with a hash value type");
1815__node_allocator& __na = __node_alloc();
1816__node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
1817std::__construct_at(std::addressof(*__h), /* next = */ nullptr, /* hash = */ __hash);
1818__node_traits::construct(
1819__na, _NodeTypes::__get_ptr(__h->__get_value()), std::forward<_First>(__f), std::forward<_Rest>(__rest)...);
1820__h.get_deleter().__value_constructed = true;
1821return __h;
1822}
1823
1824template <class _Tp, class _Hash, class _Equal, class _Alloc>
1825typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1826__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p) {
1827__next_pointer __np = __p.__node_;
1828_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1829__p != end(), "unordered container::erase(iterator) called with a non-dereferenceable iterator");
1830iterator __r(__np);
1831++__r;
1832remove(__p);
1833return __r;
1834}
1835
1836template <class _Tp, class _Hash, class _Equal, class _Alloc>
1837typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1838__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first, const_iterator __last) {
1839for (const_iterator __p = __first; __first != __last; __p = __first) {
1840++__first;
1841erase(__p);
1842}
1843__next_pointer __np = __last.__node_;
1844return iterator(__np);
1845}
1846
1847template <class _Tp, class _Hash, class _Equal, class _Alloc>
1848template <class _Key>
1849typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
1850__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k) {
1851iterator __i = find(__k);
1852if (__i == end())
1853return 0;
1854erase(__i);
1855return 1;
1856}
1857
1858template <class _Tp, class _Hash, class _Equal, class _Alloc>
1859template <class _Key>
1860typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
1861__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k) {
1862size_type __r = 0;
1863iterator __i = find(__k);
1864if (__i != end()) {
1865iterator __e = end();
1866do {
1867erase(__i++);
1868++__r;
1869} while (__i != __e && key_eq()(*__i, __k));
1870}
1871return __r;
1872}
1873
1874template <class _Tp, class _Hash, class _Equal, class _Alloc>
1875typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
1876__hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT {
1877// current node
1878__next_pointer __cn = __p.__node_;
1879size_type __bc = bucket_count();
1880size_t __chash = std::__constrain_hash(__cn->__hash(), __bc);
1881// find previous node
1882__next_pointer __pn = __bucket_list_[__chash];
1883for (; __pn->__next_ != __cn; __pn = __pn->__next_)
1884;
1885// Fix up __bucket_list_
1886// if __pn is not in same bucket (before begin is not in same bucket) &&
1887// if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
1888if (__pn == __p1_.first().__ptr() || std::__constrain_hash(__pn->__hash(), __bc) != __chash) {
1889if (__cn->__next_ == nullptr || std::__constrain_hash(__cn->__next_->__hash(), __bc) != __chash)
1890__bucket_list_[__chash] = nullptr;
1891}
1892// if __cn->__next_ is not in same bucket (nullptr is in same bucket)
1893if (__cn->__next_ != nullptr) {
1894size_t __nhash = std::__constrain_hash(__cn->__next_->__hash(), __bc);
1895if (__nhash != __chash)
1896__bucket_list_[__nhash] = __pn;
1897}
1898// remove __cn
1899__pn->__next_ = __cn->__next_;
1900__cn->__next_ = nullptr;
1901--size();
1902return __node_holder(__cn->__upcast(), _Dp(__node_alloc(), true));
1903}
1904
1905template <class _Tp, class _Hash, class _Equal, class _Alloc>
1906template <class _Key>
1907inline typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
1908__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const {
1909return static_cast<size_type>(find(__k) != end());
1910}
1911
1912template <class _Tp, class _Hash, class _Equal, class _Alloc>
1913template <class _Key>
1914typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
1915__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const {
1916size_type __r = 0;
1917const_iterator __i = find(__k);
1918if (__i != end()) {
1919const_iterator __e = end();
1920do {
1921++__i;
1922++__r;
1923} while (__i != __e && key_eq()(*__i, __k));
1924}
1925return __r;
1926}
1927
1928template <class _Tp, class _Hash, class _Equal, class _Alloc>
1929template <class _Key>
1930pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
1931typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
1932__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(const _Key& __k) {
1933iterator __i = find(__k);
1934iterator __j = __i;
1935if (__i != end())
1936++__j;
1937return pair<iterator, iterator>(__i, __j);
1938}
1939
1940template <class _Tp, class _Hash, class _Equal, class _Alloc>
1941template <class _Key>
1942pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
1943typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
1944__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(const _Key& __k) const {
1945const_iterator __i = find(__k);
1946const_iterator __j = __i;
1947if (__i != end())
1948++__j;
1949return pair<const_iterator, const_iterator>(__i, __j);
1950}
1951
1952template <class _Tp, class _Hash, class _Equal, class _Alloc>
1953template <class _Key>
1954pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
1955typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
1956__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(const _Key& __k) {
1957iterator __i = find(__k);
1958iterator __j = __i;
1959if (__i != end()) {
1960iterator __e = end();
1961do {
1962++__j;
1963} while (__j != __e && key_eq()(*__j, __k));
1964}
1965return pair<iterator, iterator>(__i, __j);
1966}
1967
1968template <class _Tp, class _Hash, class _Equal, class _Alloc>
1969template <class _Key>
1970pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
1971typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
1972__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(const _Key& __k) const {
1973const_iterator __i = find(__k);
1974const_iterator __j = __i;
1975if (__i != end()) {
1976const_iterator __e = end();
1977do {
1978++__j;
1979} while (__j != __e && key_eq()(*__j, __k));
1980}
1981return pair<const_iterator, const_iterator>(__i, __j);
1982}
1983
1984template <class _Tp, class _Hash, class _Equal, class _Alloc>
1985void __hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
1986#if _LIBCPP_STD_VER <= 11
1987_NOEXCEPT_(__is_nothrow_swappable_v<hasher>&& __is_nothrow_swappable_v<key_equal> &&
1988(!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value ||
1989__is_nothrow_swappable_v<__pointer_allocator>) &&
1990(!__node_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<__node_allocator>))
1991#else
1992_NOEXCEPT_(__is_nothrow_swappable_v<hasher>&& __is_nothrow_swappable_v<key_equal>)
1993#endif
1994{
1995_LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
1996__node_traits::propagate_on_container_swap::value || this->__node_alloc() == __u.__node_alloc(),
1997"unordered container::swap: Either propagate_on_container_swap "
1998"must be true or the allocators must compare equal");
1999{
2000__node_pointer_pointer __npp = __bucket_list_.release();
2001__bucket_list_.reset(__u.__bucket_list_.release());
2002__u.__bucket_list_.reset(__npp);
2003}
2004std::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
2005std::__swap_allocator(__bucket_list_.get_deleter().__alloc(), __u.__bucket_list_.get_deleter().__alloc());
2006std::__swap_allocator(__node_alloc(), __u.__node_alloc());
2007std::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
2008__p2_.swap(__u.__p2_);
2009__p3_.swap(__u.__p3_);
2010if (size() > 0)
2011__bucket_list_[std::__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] = __p1_.first().__ptr();
2012if (__u.size() > 0)
2013__u.__bucket_list_[std::__constrain_hash(__u.__p1_.first().__next_->__hash(), __u.bucket_count())] =
2014__u.__p1_.first().__ptr();
2015}
2016
2017template <class _Tp, class _Hash, class _Equal, class _Alloc>
2018typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2019__hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const {
2020_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
2021__n < bucket_count(), "unordered container::bucket_size(n) called with n >= bucket_count()");
2022__next_pointer __np = __bucket_list_[__n];
2023size_type __bc = bucket_count();
2024size_type __r = 0;
2025if (__np != nullptr) {
2026for (__np = __np->__next_; __np != nullptr && std::__constrain_hash(__np->__hash(), __bc) == __n;
2027__np = __np->__next_, (void)++__r)
2028;
2029}
2030return __r;
2031}
2032
2033template <class _Tp, class _Hash, class _Equal, class _Alloc>
2034inline _LIBCPP_HIDE_FROM_ABI void
2035swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x, __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
2036_NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
2037__x.swap(__y);
2038}
2039
2040_LIBCPP_END_NAMESPACE_STD
2041
2042_LIBCPP_POP_MACROS
2043
2044#endif // _LIBCPP___HASH_TABLE
2045