blob: 7b0fc412bd3b8350e9c30da8e9a7a9851a6b21f1 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
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
Howard Hinnant66c6f972011-11-29 16:45:27 +000021#include <__undef_min_max>
22
Howard Hinnant8b00e6c2013-08-02 00:26:35 +000023#ifdef _LIBCPP_DEBUG2
24# include <__debug>
25#else
26# define _LIBCPP_ASSERT(x, m) ((void)0)
27#endif
28
Howard Hinnant08e17472011-10-17 20:05:10 +000029#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000030#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000031#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000032
33_LIBCPP_BEGIN_NAMESPACE_STD
34
Howard Hinnant83eade62013-03-06 23:30:19 +000035_LIBCPP_FUNC_VIS
Howard Hinnant2b1b2d42011-06-14 19:58:17 +000036size_t __next_prime(size_t __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000037
38template <class _NodePtr>
39struct __hash_node_base
40{
41 typedef __hash_node_base __first_node;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000042
Howard Hinnantdf85e572011-02-27 18:02:02 +000043 _NodePtr __next_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000044
Howard Hinnant5f2f14c2011-06-04 18:54:24 +000045 _LIBCPP_INLINE_VISIBILITY __hash_node_base() _NOEXCEPT : __next_(nullptr) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000046};
47
48template <class _Tp, class _VoidPtr>
49struct __hash_node
50 : public __hash_node_base
51 <
52 typename pointer_traits<_VoidPtr>::template
53#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
54 rebind<__hash_node<_Tp, _VoidPtr> >
55#else
56 rebind<__hash_node<_Tp, _VoidPtr> >::other
57#endif
58 >
59{
60 typedef _Tp value_type;
61
62 size_t __hash_;
63 value_type __value_;
64};
65
Howard Hinnant7a445152012-07-06 17:31:14 +000066inline _LIBCPP_INLINE_VISIBILITY
67bool
68__is_power2(size_t __bc)
69{
70 return __bc > 2 && !(__bc & (__bc - 1));
71}
72
73inline _LIBCPP_INLINE_VISIBILITY
74size_t
75__constrain_hash(size_t __h, size_t __bc)
76{
77 return !(__bc & (__bc - 1)) ? __h & (__bc - 1) : __h % __bc;
78}
79
80inline _LIBCPP_INLINE_VISIBILITY
81size_t
82__next_pow2(size_t __n)
83{
84 return size_t(1) << (std::numeric_limits<size_t>::digits - __clz(__n-1));
85}
86
Howard Hinnant2b1b2d42011-06-14 19:58:17 +000087template <class _Tp, class _Hash, class _Equal, class _Alloc> class __hash_table;
Howard Hinnant83eade62013-03-06 23:30:19 +000088template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS __hash_const_iterator;
89template <class _HashIterator> class _LIBCPP_TYPE_VIS __hash_map_iterator;
90template <class _HashIterator> class _LIBCPP_TYPE_VIS __hash_map_const_iterator;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +000091template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
Howard Hinnant83eade62013-03-06 23:30:19 +000092 class _LIBCPP_TYPE_VIS unordered_map;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000093
94template <class _NodePtr>
Howard Hinnant83eade62013-03-06 23:30:19 +000095class _LIBCPP_TYPE_VIS __hash_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000096{
97 typedef _NodePtr __node_pointer;
98
99 __node_pointer __node_;
100
101public:
102 typedef forward_iterator_tag iterator_category;
103 typedef typename pointer_traits<__node_pointer>::element_type::value_type value_type;
104 typedef typename pointer_traits<__node_pointer>::difference_type difference_type;
105 typedef value_type& reference;
106 typedef typename pointer_traits<__node_pointer>::template
107#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
108 rebind<value_type>
109#else
110 rebind<value_type>::other
111#endif
112 pointer;
113
Howard Hinnant39213642013-07-23 22:01:58 +0000114 _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT
115 {
116#if _LIBCPP_DEBUG_LEVEL >= 2
117 __get_db()->__insert_i(this);
118#endif
119 }
120
121#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000122
Howard Hinnant99acc502010-09-21 17:32:39 +0000123 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000124 __hash_iterator(const __hash_iterator& __i)
125 : __node_(__i.__node_)
126 {
127 __get_db()->__iterator_copy(this, &__i);
128 }
129
Howard Hinnant99acc502010-09-21 17:32:39 +0000130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000131 ~__hash_iterator()
132 {
133 __get_db()->__erase_i(this);
134 }
135
136 _LIBCPP_INLINE_VISIBILITY
137 __hash_iterator& operator=(const __hash_iterator& __i)
138 {
139 if (this != &__i)
140 {
141 __get_db()->__iterator_copy(this, &__i);
142 __node_ = __i.__node_;
143 }
144 return *this;
145 }
146
147#endif // _LIBCPP_DEBUG_LEVEL >= 2
148
149 _LIBCPP_INLINE_VISIBILITY
150 reference operator*() const
151 {
152#if _LIBCPP_DEBUG_LEVEL >= 2
153 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
154 "Attempted to dereference a non-dereferenceable unordered container iterator");
155#endif
156 return __node_->__value_;
157 }
158 _LIBCPP_INLINE_VISIBILITY
159 pointer operator->() const
160 {
161#if _LIBCPP_DEBUG_LEVEL >= 2
162 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
163 "Attempted to dereference a non-dereferenceable unordered container iterator");
164#endif
165 return pointer_traits<pointer>::pointer_to(__node_->__value_);
166 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000167
Howard Hinnant99acc502010-09-21 17:32:39 +0000168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000169 __hash_iterator& operator++()
170 {
Howard Hinnant39213642013-07-23 22:01:58 +0000171#if _LIBCPP_DEBUG_LEVEL >= 2
172 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
173 "Attempted to increment non-incrementable unordered container iterator");
174#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000175 __node_ = __node_->__next_;
176 return *this;
177 }
178
Howard Hinnant99acc502010-09-21 17:32:39 +0000179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000180 __hash_iterator operator++(int)
181 {
182 __hash_iterator __t(*this);
183 ++(*this);
184 return __t;
185 }
186
Howard Hinnant99acc502010-09-21 17:32:39 +0000187 friend _LIBCPP_INLINE_VISIBILITY
188 bool operator==(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000189 {
Howard Hinnant39213642013-07-23 22:01:58 +0000190 return __x.__node_ == __y.__node_;
191 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000192 friend _LIBCPP_INLINE_VISIBILITY
193 bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000194 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000195
196private:
Howard Hinnant39213642013-07-23 22:01:58 +0000197#if _LIBCPP_DEBUG_LEVEL >= 2
198 _LIBCPP_INLINE_VISIBILITY
199 __hash_iterator(__node_pointer __node, const void* __c) _NOEXCEPT
200 : __node_(__node)
201 {
202 __get_db()->__insert_ic(this, __c);
203 }
204#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000205 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000206 __hash_iterator(__node_pointer __node) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000207 : __node_(__node)
208 {}
Howard Hinnant39213642013-07-23 22:01:58 +0000209#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000210
211 template <class, class, class, class> friend class __hash_table;
Howard Hinnant83eade62013-03-06 23:30:19 +0000212 template <class> friend class _LIBCPP_TYPE_VIS __hash_const_iterator;
213 template <class> friend class _LIBCPP_TYPE_VIS __hash_map_iterator;
214 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS unordered_map;
215 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000216};
217
218template <class _ConstNodePtr>
Howard Hinnant83eade62013-03-06 23:30:19 +0000219class _LIBCPP_TYPE_VIS __hash_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000220{
221 typedef _ConstNodePtr __node_pointer;
222
223 __node_pointer __node_;
224
225 typedef typename remove_const<
226 typename pointer_traits<__node_pointer>::element_type
227 >::type __node;
228
229public:
230 typedef forward_iterator_tag iterator_category;
231 typedef typename __node::value_type value_type;
232 typedef typename pointer_traits<__node_pointer>::difference_type difference_type;
233 typedef const value_type& reference;
234 typedef typename pointer_traits<__node_pointer>::template
235#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
236 rebind<const value_type>
237#else
238 rebind<const value_type>::other
239#endif
240 pointer;
241 typedef typename pointer_traits<__node_pointer>::template
242#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
243 rebind<__node>
244#else
245 rebind<__node>::other
246#endif
247 __non_const_node_pointer;
248 typedef __hash_iterator<__non_const_node_pointer> __non_const_iterator;
249
Howard Hinnant39213642013-07-23 22:01:58 +0000250 _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT
251 {
252#if _LIBCPP_DEBUG_LEVEL >= 2
253 __get_db()->__insert_i(this);
254#endif
255 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000256 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000257 __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000258 : __node_(__x.__node_)
Howard Hinnant39213642013-07-23 22:01:58 +0000259 {
260#if _LIBCPP_DEBUG_LEVEL >= 2
261 __get_db()->__iterator_copy(this, &__x);
262#endif
263 }
264
265#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000266
Howard Hinnant99acc502010-09-21 17:32:39 +0000267 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000268 __hash_const_iterator(const __hash_const_iterator& __i)
269 : __node_(__i.__node_)
270 {
271 __get_db()->__iterator_copy(this, &__i);
272 }
273
Howard Hinnant99acc502010-09-21 17:32:39 +0000274 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000275 ~__hash_const_iterator()
276 {
277 __get_db()->__erase_i(this);
278 }
279
280 _LIBCPP_INLINE_VISIBILITY
281 __hash_const_iterator& operator=(const __hash_const_iterator& __i)
282 {
283 if (this != &__i)
284 {
285 __get_db()->__iterator_copy(this, &__i);
286 __node_ = __i.__node_;
287 }
288 return *this;
289 }
290
291#endif // _LIBCPP_DEBUG_LEVEL >= 2
292
293 _LIBCPP_INLINE_VISIBILITY
294 reference operator*() const
295 {
296#if _LIBCPP_DEBUG_LEVEL >= 2
297 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
298 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
299#endif
300 return __node_->__value_;
301 }
302 _LIBCPP_INLINE_VISIBILITY
303 pointer operator->() const
304 {
305#if _LIBCPP_DEBUG_LEVEL >= 2
306 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
307 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
308#endif
309 return pointer_traits<pointer>::pointer_to(__node_->__value_);
310 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000311
Howard Hinnant99acc502010-09-21 17:32:39 +0000312 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000313 __hash_const_iterator& operator++()
314 {
Howard Hinnant39213642013-07-23 22:01:58 +0000315#if _LIBCPP_DEBUG_LEVEL >= 2
316 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
317 "Attempted to increment non-incrementable unordered container const_iterator");
318#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000319 __node_ = __node_->__next_;
320 return *this;
321 }
322
Howard Hinnant99acc502010-09-21 17:32:39 +0000323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000324 __hash_const_iterator operator++(int)
325 {
326 __hash_const_iterator __t(*this);
327 ++(*this);
328 return __t;
329 }
330
Howard Hinnant99acc502010-09-21 17:32:39 +0000331 friend _LIBCPP_INLINE_VISIBILITY
332 bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000333 {
Howard Hinnant39213642013-07-23 22:01:58 +0000334 return __x.__node_ == __y.__node_;
335 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000336 friend _LIBCPP_INLINE_VISIBILITY
337 bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000338 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000339
340private:
Howard Hinnant39213642013-07-23 22:01:58 +0000341#if _LIBCPP_DEBUG_LEVEL >= 2
342 _LIBCPP_INLINE_VISIBILITY
343 __hash_const_iterator(__node_pointer __node, const void* __c) _NOEXCEPT
344 : __node_(__node)
345 {
346 __get_db()->__insert_ic(this, __c);
347 }
348#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000350 __hash_const_iterator(__node_pointer __node) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000351 : __node_(__node)
352 {}
Howard Hinnant39213642013-07-23 22:01:58 +0000353#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000354
355 template <class, class, class, class> friend class __hash_table;
Howard Hinnant83eade62013-03-06 23:30:19 +0000356 template <class> friend class _LIBCPP_TYPE_VIS __hash_map_const_iterator;
357 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS unordered_map;
358 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000359};
360
Howard Hinnant83eade62013-03-06 23:30:19 +0000361template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS __hash_const_local_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000362
363template <class _NodePtr>
Howard Hinnant83eade62013-03-06 23:30:19 +0000364class _LIBCPP_TYPE_VIS __hash_local_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000365{
366 typedef _NodePtr __node_pointer;
367
368 __node_pointer __node_;
369 size_t __bucket_;
370 size_t __bucket_count_;
371
372 typedef pointer_traits<__node_pointer> __pointer_traits;
373public:
374 typedef forward_iterator_tag iterator_category;
375 typedef typename __pointer_traits::element_type::value_type value_type;
376 typedef typename __pointer_traits::difference_type difference_type;
377 typedef value_type& reference;
378 typedef typename __pointer_traits::template
379#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
380 rebind<value_type>
381#else
382 rebind<value_type>::other
383#endif
384 pointer;
385
Howard Hinnant39213642013-07-23 22:01:58 +0000386 _LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT
387 {
388#if _LIBCPP_DEBUG_LEVEL >= 2
389 __get_db()->__insert_i(this);
390#endif
391 }
392
393#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000394
Howard Hinnant99acc502010-09-21 17:32:39 +0000395 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000396 __hash_local_iterator(const __hash_local_iterator& __i)
397 : __node_(__i.__node_),
398 __bucket_(__i.__bucket_),
399 __bucket_count_(__i.__bucket_count_)
400 {
401 __get_db()->__iterator_copy(this, &__i);
402 }
403
Howard Hinnant99acc502010-09-21 17:32:39 +0000404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000405 ~__hash_local_iterator()
406 {
407 __get_db()->__erase_i(this);
408 }
409
410 _LIBCPP_INLINE_VISIBILITY
411 __hash_local_iterator& operator=(const __hash_local_iterator& __i)
412 {
413 if (this != &__i)
414 {
415 __get_db()->__iterator_copy(this, &__i);
416 __node_ = __i.__node_;
417 __bucket_ = __i.__bucket_;
418 __bucket_count_ = __i.__bucket_count_;
419 }
420 return *this;
421 }
422
423#endif // _LIBCPP_DEBUG_LEVEL >= 2
424
425 _LIBCPP_INLINE_VISIBILITY
426 reference operator*() const
427 {
428#if _LIBCPP_DEBUG_LEVEL >= 2
429 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
430 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
431#endif
432 return __node_->__value_;
433 }
434 _LIBCPP_INLINE_VISIBILITY
435 pointer operator->() const
436 {
437#if _LIBCPP_DEBUG_LEVEL >= 2
438 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
439 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
440#endif
441 return pointer_traits<pointer>::pointer_to(__node_->__value_);
442 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000443
Howard Hinnant99acc502010-09-21 17:32:39 +0000444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000445 __hash_local_iterator& operator++()
446 {
Howard Hinnant39213642013-07-23 22:01:58 +0000447#if _LIBCPP_DEBUG_LEVEL >= 2
448 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
449 "Attempted to increment non-incrementable unordered container local_iterator");
450#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000451 __node_ = __node_->__next_;
Howard Hinnant7a445152012-07-06 17:31:14 +0000452 if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000453 __node_ = nullptr;
454 return *this;
455 }
456
Howard Hinnant99acc502010-09-21 17:32:39 +0000457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000458 __hash_local_iterator operator++(int)
459 {
460 __hash_local_iterator __t(*this);
461 ++(*this);
462 return __t;
463 }
464
Howard Hinnant99acc502010-09-21 17:32:39 +0000465 friend _LIBCPP_INLINE_VISIBILITY
466 bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000467 {
Howard Hinnant39213642013-07-23 22:01:58 +0000468 return __x.__node_ == __y.__node_;
469 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000470 friend _LIBCPP_INLINE_VISIBILITY
471 bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000472 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000473
474private:
Howard Hinnant39213642013-07-23 22:01:58 +0000475#if _LIBCPP_DEBUG_LEVEL >= 2
476 _LIBCPP_INLINE_VISIBILITY
477 __hash_local_iterator(__node_pointer __node, size_t __bucket,
478 size_t __bucket_count, const void* __c) _NOEXCEPT
479 : __node_(__node),
480 __bucket_(__bucket),
481 __bucket_count_(__bucket_count)
482 {
483 __get_db()->__insert_ic(this, __c);
484 if (__node_ != nullptr)
485 __node_ = __node_->__next_;
486 }
487#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000488 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000489 __hash_local_iterator(__node_pointer __node, size_t __bucket,
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000490 size_t __bucket_count) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000491 : __node_(__node),
492 __bucket_(__bucket),
493 __bucket_count_(__bucket_count)
494 {
495 if (__node_ != nullptr)
496 __node_ = __node_->__next_;
497 }
Howard Hinnant39213642013-07-23 22:01:58 +0000498#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000499 template <class, class, class, class> friend class __hash_table;
Howard Hinnant83eade62013-03-06 23:30:19 +0000500 template <class> friend class _LIBCPP_TYPE_VIS __hash_const_local_iterator;
501 template <class> friend class _LIBCPP_TYPE_VIS __hash_map_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000502};
503
504template <class _ConstNodePtr>
Howard Hinnant83eade62013-03-06 23:30:19 +0000505class _LIBCPP_TYPE_VIS __hash_const_local_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000506{
507 typedef _ConstNodePtr __node_pointer;
508
509 __node_pointer __node_;
510 size_t __bucket_;
511 size_t __bucket_count_;
512
513 typedef pointer_traits<__node_pointer> __pointer_traits;
514 typedef typename __pointer_traits::element_type __node;
515 typedef typename remove_const<__node>::type __non_const_node;
516 typedef typename pointer_traits<__node_pointer>::template
517#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
518 rebind<__non_const_node>
519#else
520 rebind<__non_const_node>::other
521#endif
522 __non_const_node_pointer;
523 typedef __hash_local_iterator<__non_const_node_pointer>
524 __non_const_iterator;
525public:
526 typedef forward_iterator_tag iterator_category;
527 typedef typename remove_const<
528 typename __pointer_traits::element_type::value_type
529 >::type value_type;
530 typedef typename __pointer_traits::difference_type difference_type;
531 typedef const value_type& reference;
532 typedef typename __pointer_traits::template
533#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
534 rebind<const value_type>
535#else
536 rebind<const value_type>::other
537#endif
538 pointer;
539
Howard Hinnant39213642013-07-23 22:01:58 +0000540 _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT
541 {
542#if _LIBCPP_DEBUG_LEVEL >= 2
543 __get_db()->__insert_i(this);
544#endif
545 }
546
Howard Hinnant99acc502010-09-21 17:32:39 +0000547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000548 __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000549 : __node_(__x.__node_),
550 __bucket_(__x.__bucket_),
551 __bucket_count_(__x.__bucket_count_)
Howard Hinnant39213642013-07-23 22:01:58 +0000552 {
553#if _LIBCPP_DEBUG_LEVEL >= 2
554 __get_db()->__iterator_copy(this, &__x);
555#endif
556 }
557
558#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000559
Howard Hinnant99acc502010-09-21 17:32:39 +0000560 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000561 __hash_const_local_iterator(const __hash_const_local_iterator& __i)
562 : __node_(__i.__node_),
563 __bucket_(__i.__bucket_),
564 __bucket_count_(__i.__bucket_count_)
565 {
566 __get_db()->__iterator_copy(this, &__i);
567 }
568
Howard Hinnant99acc502010-09-21 17:32:39 +0000569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000570 ~__hash_const_local_iterator()
571 {
572 __get_db()->__erase_i(this);
573 }
574
575 _LIBCPP_INLINE_VISIBILITY
576 __hash_const_local_iterator& operator=(const __hash_const_local_iterator& __i)
577 {
578 if (this != &__i)
579 {
580 __get_db()->__iterator_copy(this, &__i);
581 __node_ = __i.__node_;
582 __bucket_ = __i.__bucket_;
583 __bucket_count_ = __i.__bucket_count_;
584 }
585 return *this;
586 }
587
588#endif // _LIBCPP_DEBUG_LEVEL >= 2
589
590 _LIBCPP_INLINE_VISIBILITY
591 reference operator*() const
592 {
593#if _LIBCPP_DEBUG_LEVEL >= 2
594 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
595 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
596#endif
597 return __node_->__value_;
598 }
599 _LIBCPP_INLINE_VISIBILITY
600 pointer operator->() const
601 {
602#if _LIBCPP_DEBUG_LEVEL >= 2
603 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
604 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
605#endif
606 return pointer_traits<pointer>::pointer_to(__node_->__value_);
607 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000608
Howard Hinnant99acc502010-09-21 17:32:39 +0000609 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000610 __hash_const_local_iterator& operator++()
611 {
Howard Hinnant39213642013-07-23 22:01:58 +0000612#if _LIBCPP_DEBUG_LEVEL >= 2
613 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
614 "Attempted to increment non-incrementable unordered container const_local_iterator");
615#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000616 __node_ = __node_->__next_;
Howard Hinnant7a445152012-07-06 17:31:14 +0000617 if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000618 __node_ = nullptr;
619 return *this;
620 }
621
Howard Hinnant99acc502010-09-21 17:32:39 +0000622 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000623 __hash_const_local_iterator operator++(int)
624 {
625 __hash_const_local_iterator __t(*this);
626 ++(*this);
627 return __t;
628 }
629
Howard Hinnant99acc502010-09-21 17:32:39 +0000630 friend _LIBCPP_INLINE_VISIBILITY
631 bool operator==(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000632 {
Howard Hinnant39213642013-07-23 22:01:58 +0000633 return __x.__node_ == __y.__node_;
634 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000635 friend _LIBCPP_INLINE_VISIBILITY
636 bool operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000637 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000638
639private:
Howard Hinnant39213642013-07-23 22:01:58 +0000640#if _LIBCPP_DEBUG_LEVEL >= 2
641 _LIBCPP_INLINE_VISIBILITY
642 __hash_const_local_iterator(__node_pointer __node, size_t __bucket,
643 size_t __bucket_count, const void* __c) _NOEXCEPT
644 : __node_(__node),
645 __bucket_(__bucket),
646 __bucket_count_(__bucket_count)
647 {
648 __get_db()->__insert_ic(this, __c);
649 if (__node_ != nullptr)
650 __node_ = __node_->__next_;
651 }
652#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000654 __hash_const_local_iterator(__node_pointer __node, size_t __bucket,
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000655 size_t __bucket_count) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000656 : __node_(__node),
657 __bucket_(__bucket),
658 __bucket_count_(__bucket_count)
659 {
660 if (__node_ != nullptr)
661 __node_ = __node_->__next_;
662 }
Howard Hinnant39213642013-07-23 22:01:58 +0000663#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000664 template <class, class, class, class> friend class __hash_table;
Howard Hinnant83eade62013-03-06 23:30:19 +0000665 template <class> friend class _LIBCPP_TYPE_VIS __hash_map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000666};
667
668template <class _Alloc>
669class __bucket_list_deallocator
670{
671 typedef _Alloc allocator_type;
672 typedef allocator_traits<allocator_type> __alloc_traits;
673 typedef typename __alloc_traits::size_type size_type;
674
675 __compressed_pair<size_type, allocator_type> __data_;
676public:
677 typedef typename __alloc_traits::pointer pointer;
678
Howard Hinnant99acc502010-09-21 17:32:39 +0000679 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000680 __bucket_list_deallocator()
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000681 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000682 : __data_(0) {}
Howard Hinnant99acc502010-09-21 17:32:39 +0000683
684 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000685 __bucket_list_deallocator(const allocator_type& __a, size_type __size)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000686 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000687 : __data_(__size, __a) {}
688
Howard Hinnant73d21a42010-09-04 23:28:19 +0000689#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000690
Howard Hinnant99acc502010-09-21 17:32:39 +0000691 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000692 __bucket_list_deallocator(__bucket_list_deallocator&& __x)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000693 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000694 : __data_(_VSTD::move(__x.__data_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000695 {
696 __x.size() = 0;
697 }
698
Howard Hinnant73d21a42010-09-04 23:28:19 +0000699#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000700
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000701 _LIBCPP_INLINE_VISIBILITY
702 size_type& size() _NOEXCEPT {return __data_.first();}
703 _LIBCPP_INLINE_VISIBILITY
704 size_type size() const _NOEXCEPT {return __data_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000705
Howard Hinnant99acc502010-09-21 17:32:39 +0000706 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000707 allocator_type& __alloc() _NOEXCEPT {return __data_.second();}
708 _LIBCPP_INLINE_VISIBILITY
709 const allocator_type& __alloc() const _NOEXCEPT {return __data_.second();}
710
711 _LIBCPP_INLINE_VISIBILITY
712 void operator()(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000713 {
714 __alloc_traits::deallocate(__alloc(), __p, size());
715 }
716};
717
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000718template <class _Alloc> class __hash_map_node_destructor;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000719
720template <class _Alloc>
721class __hash_node_destructor
722{
723 typedef _Alloc allocator_type;
724 typedef allocator_traits<allocator_type> __alloc_traits;
725 typedef typename __alloc_traits::value_type::value_type value_type;
726public:
727 typedef typename __alloc_traits::pointer pointer;
728private:
729
730 allocator_type& __na_;
731
732 __hash_node_destructor& operator=(const __hash_node_destructor&);
733
734public:
735 bool __value_constructed;
736
Howard Hinnant99acc502010-09-21 17:32:39 +0000737 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant199d0ae2011-07-31 17:04:30 +0000738 explicit __hash_node_destructor(allocator_type& __na,
739 bool __constructed = false) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000740 : __na_(__na),
Howard Hinnant199d0ae2011-07-31 17:04:30 +0000741 __value_constructed(__constructed)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000742 {}
743
Howard Hinnant99acc502010-09-21 17:32:39 +0000744 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000745 void operator()(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000746 {
747 if (__value_constructed)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000748 __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000749 if (__p)
750 __alloc_traits::deallocate(__na_, __p, 1);
751 }
752
753 template <class> friend class __hash_map_node_destructor;
754};
755
756template <class _Tp, class _Hash, class _Equal, class _Alloc>
757class __hash_table
758{
759public:
760 typedef _Tp value_type;
761 typedef _Hash hasher;
762 typedef _Equal key_equal;
763 typedef _Alloc allocator_type;
764
765private:
766 typedef allocator_traits<allocator_type> __alloc_traits;
767public:
768 typedef value_type& reference;
769 typedef const value_type& const_reference;
770 typedef typename __alloc_traits::pointer pointer;
771 typedef typename __alloc_traits::const_pointer const_pointer;
772 typedef typename __alloc_traits::size_type size_type;
773 typedef typename __alloc_traits::difference_type difference_type;
774public:
775 // Create __node
776 typedef __hash_node<value_type, typename __alloc_traits::void_pointer> __node;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000777 typedef typename __alloc_traits::template
778#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
779 rebind_alloc<__node>
780#else
781 rebind_alloc<__node>::other
782#endif
783 __node_allocator;
784 typedef allocator_traits<__node_allocator> __node_traits;
785 typedef typename __node_traits::pointer __node_pointer;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000786 typedef typename __node_traits::pointer __node_const_pointer;
Howard Hinnantf8880d02011-12-12 17:26:24 +0000787 typedef __hash_node_base<__node_pointer> __first_node;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000788 typedef typename pointer_traits<__node_pointer>::template
789#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
790 rebind<__first_node>
791#else
792 rebind<__first_node>::other
793#endif
794 __node_base_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000795
796private:
797
798 typedef typename __node_traits::template
799#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
800 rebind_alloc<__node_pointer>
801#else
802 rebind_alloc<__node_pointer>::other
803#endif
804 __pointer_allocator;
805 typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter;
806 typedef unique_ptr<__node_pointer[], __bucket_list_deleter> __bucket_list;
807 typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits;
808 typedef typename __bucket_list_deleter::pointer __node_pointer_pointer;
809
810 // --- Member data begin ---
811 __bucket_list __bucket_list_;
812 __compressed_pair<__first_node, __node_allocator> __p1_;
813 __compressed_pair<size_type, hasher> __p2_;
814 __compressed_pair<float, key_equal> __p3_;
815 // --- Member data end ---
816
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000817 _LIBCPP_INLINE_VISIBILITY
818 size_type& size() _NOEXCEPT {return __p2_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000819public:
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000820 _LIBCPP_INLINE_VISIBILITY
821 size_type size() const _NOEXCEPT {return __p2_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000822
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000823 _LIBCPP_INLINE_VISIBILITY
824 hasher& hash_function() _NOEXCEPT {return __p2_.second();}
825 _LIBCPP_INLINE_VISIBILITY
826 const hasher& hash_function() const _NOEXCEPT {return __p2_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000827
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000828 _LIBCPP_INLINE_VISIBILITY
829 float& max_load_factor() _NOEXCEPT {return __p3_.first();}
830 _LIBCPP_INLINE_VISIBILITY
831 float max_load_factor() const _NOEXCEPT {return __p3_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000832
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000833 _LIBCPP_INLINE_VISIBILITY
834 key_equal& key_eq() _NOEXCEPT {return __p3_.second();}
835 _LIBCPP_INLINE_VISIBILITY
836 const key_equal& key_eq() const _NOEXCEPT {return __p3_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000837
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000838 _LIBCPP_INLINE_VISIBILITY
839 __node_allocator& __node_alloc() _NOEXCEPT {return __p1_.second();}
840 _LIBCPP_INLINE_VISIBILITY
841 const __node_allocator& __node_alloc() const _NOEXCEPT
842 {return __p1_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000843
844public:
845 typedef __hash_iterator<__node_pointer> iterator;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000846 typedef __hash_const_iterator<__node_pointer> const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000847 typedef __hash_local_iterator<__node_pointer> local_iterator;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000848 typedef __hash_const_local_iterator<__node_pointer> const_local_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000849
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000850 __hash_table()
851 _NOEXCEPT_(
852 is_nothrow_default_constructible<__bucket_list>::value &&
853 is_nothrow_default_constructible<__first_node>::value &&
854 is_nothrow_default_constructible<__node_allocator>::value &&
855 is_nothrow_default_constructible<hasher>::value &&
856 is_nothrow_default_constructible<key_equal>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000857 __hash_table(const hasher& __hf, const key_equal& __eql);
858 __hash_table(const hasher& __hf, const key_equal& __eql,
859 const allocator_type& __a);
860 explicit __hash_table(const allocator_type& __a);
861 __hash_table(const __hash_table& __u);
862 __hash_table(const __hash_table& __u, const allocator_type& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000863#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000864 __hash_table(__hash_table&& __u)
865 _NOEXCEPT_(
866 is_nothrow_move_constructible<__bucket_list>::value &&
867 is_nothrow_move_constructible<__first_node>::value &&
868 is_nothrow_move_constructible<__node_allocator>::value &&
869 is_nothrow_move_constructible<hasher>::value &&
870 is_nothrow_move_constructible<key_equal>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000871 __hash_table(__hash_table&& __u, const allocator_type& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000872#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000873 ~__hash_table();
874
875 __hash_table& operator=(const __hash_table& __u);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000876#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000877 __hash_table& operator=(__hash_table&& __u)
878 _NOEXCEPT_(
879 __node_traits::propagate_on_container_move_assignment::value &&
880 is_nothrow_move_assignable<__node_allocator>::value &&
881 is_nothrow_move_assignable<hasher>::value &&
882 is_nothrow_move_assignable<key_equal>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000883#endif
884 template <class _InputIterator>
885 void __assign_unique(_InputIterator __first, _InputIterator __last);
886 template <class _InputIterator>
887 void __assign_multi(_InputIterator __first, _InputIterator __last);
888
Howard Hinnant99acc502010-09-21 17:32:39 +0000889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000890 size_type max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000891 {
892 return allocator_traits<__pointer_allocator>::max_size(
893 __bucket_list_.get_deleter().__alloc());
894 }
895
896 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
897 iterator __node_insert_multi(__node_pointer __nd);
898 iterator __node_insert_multi(const_iterator __p,
899 __node_pointer __nd);
900
Howard Hinnant73d21a42010-09-04 23:28:19 +0000901#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000902 template <class... _Args>
903 pair<iterator, bool> __emplace_unique(_Args&&... __args);
904 template <class... _Args>
905 iterator __emplace_multi(_Args&&... __args);
906 template <class... _Args>
907 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000908#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000909
910 pair<iterator, bool> __insert_unique(const value_type& __x);
911
Howard Hinnant73d21a42010-09-04 23:28:19 +0000912#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +0000913 template <class _Pp>
914 pair<iterator, bool> __insert_unique(_Pp&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000915#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000916
Howard Hinnant73d21a42010-09-04 23:28:19 +0000917#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +0000918 template <class _Pp>
919 iterator __insert_multi(_Pp&& __x);
920 template <class _Pp>
921 iterator __insert_multi(const_iterator __p, _Pp&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000922#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000923 iterator __insert_multi(const value_type& __x);
924 iterator __insert_multi(const_iterator __p, const value_type& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000925#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000926
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000927 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000928 void rehash(size_type __n);
Howard Hinnant99acc502010-09-21 17:32:39 +0000929 _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000930 {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));}
Howard Hinnant99acc502010-09-21 17:32:39 +0000931
932 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000933 size_type bucket_count() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000934 {
935 return __bucket_list_.get_deleter().size();
936 }
937
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000938 iterator begin() _NOEXCEPT;
939 iterator end() _NOEXCEPT;
940 const_iterator begin() const _NOEXCEPT;
941 const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000942
943 template <class _Key>
Howard Hinnant99acc502010-09-21 17:32:39 +0000944 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000945 size_type bucket(const _Key& __k) const
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +0000946 {
947 _LIBCPP_ASSERT(bucket_count() > 0,
948 "unordered container::bucket(key) called when bucket_count() == 0");
949 return __constrain_hash(hash_function()(__k), bucket_count());
950 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000951
952 template <class _Key>
953 iterator find(const _Key& __x);
954 template <class _Key>
955 const_iterator find(const _Key& __x) const;
956
Howard Hinnant99968442011-11-29 18:15:50 +0000957 typedef __hash_node_destructor<__node_allocator> _Dp;
958 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000959
960 iterator erase(const_iterator __p);
961 iterator erase(const_iterator __first, const_iterator __last);
962 template <class _Key>
963 size_type __erase_unique(const _Key& __k);
964 template <class _Key>
965 size_type __erase_multi(const _Key& __k);
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000966 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000967
968 template <class _Key>
969 size_type __count_unique(const _Key& __k) const;
970 template <class _Key>
971 size_type __count_multi(const _Key& __k) const;
972
973 template <class _Key>
974 pair<iterator, iterator>
975 __equal_range_unique(const _Key& __k);
976 template <class _Key>
977 pair<const_iterator, const_iterator>
978 __equal_range_unique(const _Key& __k) const;
979
980 template <class _Key>
981 pair<iterator, iterator>
982 __equal_range_multi(const _Key& __k);
983 template <class _Key>
984 pair<const_iterator, const_iterator>
985 __equal_range_multi(const _Key& __k) const;
986
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000987 void swap(__hash_table& __u)
988 _NOEXCEPT_(
989 (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value ||
990 __is_nothrow_swappable<__pointer_allocator>::value) &&
991 (!__node_traits::propagate_on_container_swap::value ||
992 __is_nothrow_swappable<__node_allocator>::value) &&
993 __is_nothrow_swappable<hasher>::value &&
994 __is_nothrow_swappable<key_equal>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000995
Howard Hinnant99acc502010-09-21 17:32:39 +0000996 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000997 size_type max_bucket_count() const _NOEXCEPT
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +0000998 {return __pointer_alloc_traits::max_size(__bucket_list_.get_deleter().__alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000999 size_type bucket_size(size_type __n) const;
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001000 _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001001 {
1002 size_type __bc = bucket_count();
1003 return __bc != 0 ? (float)size() / __bc : 0.f;
1004 }
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001005 _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001006 {
1007 _LIBCPP_ASSERT(__mlf > 0,
1008 "unordered container::max_load_factor(lf) called with lf <= 0");
1009 max_load_factor() = _VSTD::max(__mlf, load_factor());
1010 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001011
Howard Hinnant39213642013-07-23 22:01:58 +00001012 _LIBCPP_INLINE_VISIBILITY
1013 local_iterator
1014 begin(size_type __n)
1015 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001016 _LIBCPP_ASSERT(__n < bucket_count(),
1017 "unordered container::begin(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001018#if _LIBCPP_DEBUG_LEVEL >= 2
1019 return local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1020#else
1021 return local_iterator(__bucket_list_[__n], __n, bucket_count());
1022#endif
1023 }
1024
1025 _LIBCPP_INLINE_VISIBILITY
1026 local_iterator
1027 end(size_type __n)
1028 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001029 _LIBCPP_ASSERT(__n < bucket_count(),
1030 "unordered container::end(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001031#if _LIBCPP_DEBUG_LEVEL >= 2
1032 return local_iterator(nullptr, __n, bucket_count(), this);
1033#else
1034 return local_iterator(nullptr, __n, bucket_count());
1035#endif
1036 }
1037
1038 _LIBCPP_INLINE_VISIBILITY
1039 const_local_iterator
1040 cbegin(size_type __n) const
1041 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001042 _LIBCPP_ASSERT(__n < bucket_count(),
1043 "unordered container::cbegin(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001044#if _LIBCPP_DEBUG_LEVEL >= 2
1045 return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1046#else
1047 return const_local_iterator(__bucket_list_[__n], __n, bucket_count());
1048#endif
1049 }
1050
1051 _LIBCPP_INLINE_VISIBILITY
1052 const_local_iterator
1053 cend(size_type __n) const
1054 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001055 _LIBCPP_ASSERT(__n < bucket_count(),
1056 "unordered container::cend(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001057#if _LIBCPP_DEBUG_LEVEL >= 2
1058 return const_local_iterator(nullptr, __n, bucket_count(), this);
1059#else
1060 return const_local_iterator(nullptr, __n, bucket_count());
1061#endif
1062 }
1063
1064#if _LIBCPP_DEBUG_LEVEL >= 2
1065
1066 bool __dereferenceable(const const_iterator* __i) const;
1067 bool __decrementable(const const_iterator* __i) const;
1068 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1069 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1070
1071#endif // _LIBCPP_DEBUG_LEVEL >= 2
1072
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001073private:
1074 void __rehash(size_type __n);
1075
Howard Hinnant73d21a42010-09-04 23:28:19 +00001076#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1077#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001078 template <class ..._Args>
1079 __node_holder __construct_node(_Args&& ...__args);
Howard Hinnantbfd55302010-09-04 23:46:48 +00001080#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001081 __node_holder __construct_node(value_type&& __v, size_t __hash);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001082#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001083 __node_holder __construct_node(const value_type& __v);
1084#endif
1085 __node_holder __construct_node(const value_type& __v, size_t __hash);
1086
Howard Hinnant99acc502010-09-21 17:32:39 +00001087 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001088 void __copy_assign_alloc(const __hash_table& __u)
1089 {__copy_assign_alloc(__u, integral_constant<bool,
1090 __node_traits::propagate_on_container_copy_assignment::value>());}
1091 void __copy_assign_alloc(const __hash_table& __u, true_type);
Howard Hinnant99acc502010-09-21 17:32:39 +00001092 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001093 void __copy_assign_alloc(const __hash_table&, false_type) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001094
1095 void __move_assign(__hash_table& __u, false_type);
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001096 void __move_assign(__hash_table& __u, true_type)
1097 _NOEXCEPT_(
1098 is_nothrow_move_assignable<__node_allocator>::value &&
1099 is_nothrow_move_assignable<hasher>::value &&
1100 is_nothrow_move_assignable<key_equal>::value);
1101 _LIBCPP_INLINE_VISIBILITY
1102 void __move_assign_alloc(__hash_table& __u)
1103 _NOEXCEPT_(
1104 !__node_traits::propagate_on_container_move_assignment::value ||
1105 (is_nothrow_move_assignable<__pointer_allocator>::value &&
1106 is_nothrow_move_assignable<__node_allocator>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001107 {__move_assign_alloc(__u, integral_constant<bool,
1108 __node_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant99acc502010-09-21 17:32:39 +00001109 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001110 void __move_assign_alloc(__hash_table& __u, true_type)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001111 _NOEXCEPT_(
1112 is_nothrow_move_assignable<__pointer_allocator>::value &&
1113 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001114 {
1115 __bucket_list_.get_deleter().__alloc() =
Howard Hinnant0949eed2011-06-30 21:18:19 +00001116 _VSTD::move(__u.__bucket_list_.get_deleter().__alloc());
1117 __node_alloc() = _VSTD::move(__u.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001118 }
Howard Hinnant99acc502010-09-21 17:32:39 +00001119 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001120 void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001121
Howard Hinnant99968442011-11-29 18:15:50 +00001122 template <class _Ap>
Howard Hinnant99acc502010-09-21 17:32:39 +00001123 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001124 static
1125 void
Howard Hinnant99968442011-11-29 18:15:50 +00001126 __swap_alloc(_Ap& __x, _Ap& __y)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001127 _NOEXCEPT_(
Howard Hinnant99968442011-11-29 18:15:50 +00001128 !allocator_traits<_Ap>::propagate_on_container_swap::value ||
1129 __is_nothrow_swappable<_Ap>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001130 {
1131 __swap_alloc(__x, __y,
1132 integral_constant<bool,
Howard Hinnant99968442011-11-29 18:15:50 +00001133 allocator_traits<_Ap>::propagate_on_container_swap::value
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001134 >());
1135 }
1136
Howard Hinnant99968442011-11-29 18:15:50 +00001137 template <class _Ap>
Howard Hinnant99acc502010-09-21 17:32:39 +00001138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001139 static
1140 void
Howard Hinnant99968442011-11-29 18:15:50 +00001141 __swap_alloc(_Ap& __x, _Ap& __y, true_type)
1142 _NOEXCEPT_(__is_nothrow_swappable<_Ap>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001143 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001144 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001145 swap(__x, __y);
1146 }
1147
Howard Hinnant99968442011-11-29 18:15:50 +00001148 template <class _Ap>
Howard Hinnant99acc502010-09-21 17:32:39 +00001149 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001150 static
1151 void
Howard Hinnantec3773c2011-12-01 20:21:04 +00001152 __swap_alloc(_Ap&, _Ap&, false_type) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001153
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001154 void __deallocate(__node_pointer __np) _NOEXCEPT;
1155 __node_pointer __detach() _NOEXCEPT;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001156
1157 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS unordered_map;
1158 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001159};
1160
1161template <class _Tp, class _Hash, class _Equal, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001162inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001163__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table()
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001164 _NOEXCEPT_(
1165 is_nothrow_default_constructible<__bucket_list>::value &&
1166 is_nothrow_default_constructible<__first_node>::value &&
1167 is_nothrow_default_constructible<hasher>::value &&
1168 is_nothrow_default_constructible<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001169 : __p2_(0),
1170 __p3_(1.0f)
1171{
1172}
1173
1174template <class _Tp, class _Hash, class _Equal, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001175inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001176__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1177 const key_equal& __eql)
1178 : __bucket_list_(nullptr, __bucket_list_deleter()),
1179 __p1_(),
1180 __p2_(0, __hf),
1181 __p3_(1.0f, __eql)
1182{
1183}
1184
1185template <class _Tp, class _Hash, class _Equal, class _Alloc>
1186__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1187 const key_equal& __eql,
1188 const allocator_type& __a)
1189 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1190 __p1_(__node_allocator(__a)),
1191 __p2_(0, __hf),
1192 __p3_(1.0f, __eql)
1193{
1194}
1195
1196template <class _Tp, class _Hash, class _Equal, class _Alloc>
1197__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a)
1198 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1199 __p1_(__node_allocator(__a)),
1200 __p2_(0),
1201 __p3_(1.0f)
1202{
1203}
1204
1205template <class _Tp, class _Hash, class _Equal, class _Alloc>
1206__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u)
1207 : __bucket_list_(nullptr,
1208 __bucket_list_deleter(allocator_traits<__pointer_allocator>::
1209 select_on_container_copy_construction(
1210 __u.__bucket_list_.get_deleter().__alloc()), 0)),
1211 __p1_(allocator_traits<__node_allocator>::
1212 select_on_container_copy_construction(__u.__node_alloc())),
1213 __p2_(0, __u.hash_function()),
1214 __p3_(__u.__p3_)
1215{
1216}
1217
1218template <class _Tp, class _Hash, class _Equal, class _Alloc>
1219__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u,
1220 const allocator_type& __a)
1221 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1222 __p1_(__node_allocator(__a)),
1223 __p2_(0, __u.hash_function()),
1224 __p3_(__u.__p3_)
1225{
1226}
1227
Howard Hinnant73d21a42010-09-04 23:28:19 +00001228#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001229
1230template <class _Tp, class _Hash, class _Equal, class _Alloc>
1231__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001232 _NOEXCEPT_(
1233 is_nothrow_move_constructible<__bucket_list>::value &&
1234 is_nothrow_move_constructible<__first_node>::value &&
1235 is_nothrow_move_constructible<hasher>::value &&
1236 is_nothrow_move_constructible<key_equal>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001237 : __bucket_list_(_VSTD::move(__u.__bucket_list_)),
1238 __p1_(_VSTD::move(__u.__p1_)),
1239 __p2_(_VSTD::move(__u.__p2_)),
1240 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001241{
1242 if (size() > 0)
1243 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001244 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001245 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001246 __u.__p1_.first().__next_ = nullptr;
1247 __u.size() = 0;
1248 }
1249}
1250
1251template <class _Tp, class _Hash, class _Equal, class _Alloc>
1252__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u,
1253 const allocator_type& __a)
1254 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1255 __p1_(__node_allocator(__a)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001256 __p2_(0, _VSTD::move(__u.hash_function())),
1257 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001258{
1259 if (__a == allocator_type(__u.__node_alloc()))
1260 {
1261 __bucket_list_.reset(__u.__bucket_list_.release());
1262 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1263 __u.__bucket_list_.get_deleter().size() = 0;
1264 if (__u.size() > 0)
1265 {
1266 __p1_.first().__next_ = __u.__p1_.first().__next_;
1267 __u.__p1_.first().__next_ = nullptr;
Howard Hinnant7a445152012-07-06 17:31:14 +00001268 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001269 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001270 size() = __u.size();
1271 __u.size() = 0;
1272 }
1273 }
1274}
1275
Howard Hinnant73d21a42010-09-04 23:28:19 +00001276#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001277
1278template <class _Tp, class _Hash, class _Equal, class _Alloc>
1279__hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table()
1280{
1281 __deallocate(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001282#if _LIBCPP_DEBUG_LEVEL >= 2
1283 __get_db()->__erase_c(this);
1284#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001285}
1286
1287template <class _Tp, class _Hash, class _Equal, class _Alloc>
1288void
1289__hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(
1290 const __hash_table& __u, true_type)
1291{
1292 if (__node_alloc() != __u.__node_alloc())
1293 {
1294 clear();
1295 __bucket_list_.reset();
1296 __bucket_list_.get_deleter().size() = 0;
1297 }
1298 __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
1299 __node_alloc() = __u.__node_alloc();
1300}
1301
1302template <class _Tp, class _Hash, class _Equal, class _Alloc>
1303__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1304__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u)
1305{
1306 if (this != &__u)
1307 {
1308 __copy_assign_alloc(__u);
1309 hash_function() = __u.hash_function();
1310 key_eq() = __u.key_eq();
1311 max_load_factor() = __u.max_load_factor();
1312 __assign_multi(__u.begin(), __u.end());
1313 }
1314 return *this;
1315}
1316
1317template <class _Tp, class _Hash, class _Equal, class _Alloc>
1318void
1319__hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate(__node_pointer __np)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001320 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001321{
1322 __node_allocator& __na = __node_alloc();
1323 while (__np != nullptr)
1324 {
1325 __node_pointer __next = __np->__next_;
Howard Hinnant39213642013-07-23 22:01:58 +00001326#if _LIBCPP_DEBUG_LEVEL >= 2
1327 __c_node* __c = __get_db()->__find_c_and_lock(this);
1328 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1329 {
1330 --__p;
1331 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1332 if (__i->__node_ == __np)
1333 {
1334 (*__p)->__c_ = nullptr;
1335 if (--__c->end_ != __p)
1336 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1337 }
1338 }
1339 __get_db()->unlock();
1340#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001341 __node_traits::destroy(__na, _VSTD::addressof(__np->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001342 __node_traits::deallocate(__na, __np, 1);
1343 __np = __next;
1344 }
1345}
1346
1347template <class _Tp, class _Hash, class _Equal, class _Alloc>
1348typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_pointer
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001349__hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001350{
1351 size_type __bc = bucket_count();
1352 for (size_type __i = 0; __i < __bc; ++__i)
1353 __bucket_list_[__i] = nullptr;
1354 size() = 0;
1355 __node_pointer __cache = __p1_.first().__next_;
1356 __p1_.first().__next_ = nullptr;
1357 return __cache;
1358}
1359
Howard Hinnant73d21a42010-09-04 23:28:19 +00001360#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001361
1362template <class _Tp, class _Hash, class _Equal, class _Alloc>
1363void
1364__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1365 __hash_table& __u, true_type)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001366 _NOEXCEPT_(
1367 is_nothrow_move_assignable<__node_allocator>::value &&
1368 is_nothrow_move_assignable<hasher>::value &&
1369 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001370{
1371 clear();
1372 __bucket_list_.reset(__u.__bucket_list_.release());
1373 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1374 __u.__bucket_list_.get_deleter().size() = 0;
1375 __move_assign_alloc(__u);
1376 size() = __u.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001377 hash_function() = _VSTD::move(__u.hash_function());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001378 max_load_factor() = __u.max_load_factor();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001379 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001380 __p1_.first().__next_ = __u.__p1_.first().__next_;
1381 if (size() > 0)
1382 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001383 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001384 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001385 __u.__p1_.first().__next_ = nullptr;
1386 __u.size() = 0;
1387 }
Howard Hinnant39213642013-07-23 22:01:58 +00001388#if _LIBCPP_DEBUG_LEVEL >= 2
1389 __get_db()->swap(this, &__u);
1390#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001391}
1392
1393template <class _Tp, class _Hash, class _Equal, class _Alloc>
1394void
1395__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1396 __hash_table& __u, false_type)
1397{
1398 if (__node_alloc() == __u.__node_alloc())
1399 __move_assign(__u, true_type());
1400 else
1401 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001402 hash_function() = _VSTD::move(__u.hash_function());
1403 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001404 max_load_factor() = __u.max_load_factor();
1405 if (bucket_count() != 0)
1406 {
1407 __node_pointer __cache = __detach();
1408#ifndef _LIBCPP_NO_EXCEPTIONS
1409 try
1410 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001411#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001412 const_iterator __i = __u.begin();
1413 while (__cache != nullptr && __u.size() != 0)
1414 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001415 __cache->__value_ = _VSTD::move(__u.remove(__i++)->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001416 __node_pointer __next = __cache->__next_;
1417 __node_insert_multi(__cache);
1418 __cache = __next;
1419 }
1420#ifndef _LIBCPP_NO_EXCEPTIONS
1421 }
1422 catch (...)
1423 {
1424 __deallocate(__cache);
1425 throw;
1426 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001427#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001428 __deallocate(__cache);
1429 }
1430 const_iterator __i = __u.begin();
1431 while (__u.size() != 0)
1432 {
1433 __node_holder __h =
Howard Hinnant0949eed2011-06-30 21:18:19 +00001434 __construct_node(_VSTD::move(__u.remove(__i++)->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001435 __node_insert_multi(__h.get());
1436 __h.release();
1437 }
1438 }
1439}
1440
1441template <class _Tp, class _Hash, class _Equal, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001442inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001443__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1444__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001445 _NOEXCEPT_(
1446 __node_traits::propagate_on_container_move_assignment::value &&
1447 is_nothrow_move_assignable<__node_allocator>::value &&
1448 is_nothrow_move_assignable<hasher>::value &&
1449 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001450{
1451 __move_assign(__u, integral_constant<bool,
1452 __node_traits::propagate_on_container_move_assignment::value>());
1453 return *this;
1454}
1455
Howard Hinnant73d21a42010-09-04 23:28:19 +00001456#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001457
1458template <class _Tp, class _Hash, class _Equal, class _Alloc>
1459template <class _InputIterator>
1460void
1461__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first,
1462 _InputIterator __last)
1463{
1464 if (bucket_count() != 0)
1465 {
1466 __node_pointer __cache = __detach();
1467#ifndef _LIBCPP_NO_EXCEPTIONS
1468 try
1469 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001470#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001471 for (; __cache != nullptr && __first != __last; ++__first)
1472 {
1473 __cache->__value_ = *__first;
1474 __node_pointer __next = __cache->__next_;
1475 __node_insert_unique(__cache);
1476 __cache = __next;
1477 }
1478#ifndef _LIBCPP_NO_EXCEPTIONS
1479 }
1480 catch (...)
1481 {
1482 __deallocate(__cache);
1483 throw;
1484 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001485#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001486 __deallocate(__cache);
1487 }
1488 for (; __first != __last; ++__first)
1489 __insert_unique(*__first);
1490}
1491
1492template <class _Tp, class _Hash, class _Equal, class _Alloc>
1493template <class _InputIterator>
1494void
1495__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
1496 _InputIterator __last)
1497{
1498 if (bucket_count() != 0)
1499 {
1500 __node_pointer __cache = __detach();
1501#ifndef _LIBCPP_NO_EXCEPTIONS
1502 try
1503 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001504#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001505 for (; __cache != nullptr && __first != __last; ++__first)
1506 {
1507 __cache->__value_ = *__first;
1508 __node_pointer __next = __cache->__next_;
1509 __node_insert_multi(__cache);
1510 __cache = __next;
1511 }
1512#ifndef _LIBCPP_NO_EXCEPTIONS
1513 }
1514 catch (...)
1515 {
1516 __deallocate(__cache);
1517 throw;
1518 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001519#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001520 __deallocate(__cache);
1521 }
1522 for (; __first != __last; ++__first)
1523 __insert_multi(*__first);
1524}
1525
1526template <class _Tp, class _Hash, class _Equal, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001527inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001528typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001529__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530{
Howard Hinnant39213642013-07-23 22:01:58 +00001531#if _LIBCPP_DEBUG_LEVEL >= 2
1532 return iterator(__p1_.first().__next_, this);
1533#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001534 return iterator(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001535#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001536}
1537
1538template <class _Tp, class _Hash, class _Equal, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001539inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001540typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001541__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001542{
Howard Hinnant39213642013-07-23 22:01:58 +00001543#if _LIBCPP_DEBUG_LEVEL >= 2
1544 return iterator(nullptr, this);
1545#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001546 return iterator(nullptr);
Howard Hinnant39213642013-07-23 22:01:58 +00001547#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001548}
1549
1550template <class _Tp, class _Hash, class _Equal, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001551inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001552typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001553__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001554{
Howard Hinnant39213642013-07-23 22:01:58 +00001555#if _LIBCPP_DEBUG_LEVEL >= 2
1556 return const_iterator(__p1_.first().__next_, this);
1557#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001558 return const_iterator(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001559#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001560}
1561
1562template <class _Tp, class _Hash, class _Equal, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001563inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001564typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001565__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001566{
Howard Hinnant39213642013-07-23 22:01:58 +00001567#if _LIBCPP_DEBUG_LEVEL >= 2
1568 return const_iterator(nullptr, this);
1569#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001570 return const_iterator(nullptr);
Howard Hinnant39213642013-07-23 22:01:58 +00001571#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001572}
1573
1574template <class _Tp, class _Hash, class _Equal, class _Alloc>
1575void
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001576__hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001577{
1578 if (size() > 0)
1579 {
1580 __deallocate(__p1_.first().__next_);
1581 __p1_.first().__next_ = nullptr;
1582 size_type __bc = bucket_count();
Howard Hinnant9f66bff2011-07-05 14:14:17 +00001583 for (size_type __i = 0; __i < __bc; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001584 __bucket_list_[__i] = nullptr;
1585 size() = 0;
1586 }
1587}
1588
1589template <class _Tp, class _Hash, class _Equal, class _Alloc>
1590pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1591__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd)
1592{
1593 __nd->__hash_ = hash_function()(__nd->__value_);
1594 size_type __bc = bucket_count();
1595 bool __inserted = false;
1596 __node_pointer __ndptr;
1597 size_t __chash;
1598 if (__bc != 0)
1599 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001600 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001601 __ndptr = __bucket_list_[__chash];
1602 if (__ndptr != nullptr)
1603 {
1604 for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00001605 __constrain_hash(__ndptr->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001606 __ndptr = __ndptr->__next_)
1607 {
1608 if (key_eq()(__ndptr->__value_, __nd->__value_))
1609 goto __done;
1610 }
1611 }
1612 }
1613 {
1614 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1615 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001616 rehash(_VSTD::max<size_type>(2 * __bc + !__is_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001617 size_type(ceil(float(size() + 1) / max_load_factor()))));
1618 __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00001619 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001620 }
1621 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
1622 __node_pointer __pn = __bucket_list_[__chash];
1623 if (__pn == nullptr)
1624 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001625 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001626 __nd->__next_ = __pn->__next_;
1627 __pn->__next_ = __nd;
1628 // fix up __bucket_list_
1629 __bucket_list_[__chash] = __pn;
1630 if (__nd->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:14 +00001631 __bucket_list_[__constrain_hash(__nd->__next_->__hash_, __bc)] = __nd;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001632 }
1633 else
1634 {
1635 __nd->__next_ = __pn->__next_;
1636 __pn->__next_ = __nd;
1637 }
1638 __ndptr = __nd;
1639 // increment size
1640 ++size();
1641 __inserted = true;
1642 }
1643__done:
Howard Hinnant39213642013-07-23 22:01:58 +00001644#if _LIBCPP_DEBUG_LEVEL >= 2
1645 return pair<iterator, bool>(iterator(__ndptr, this), __inserted);
1646#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001647 return pair<iterator, bool>(iterator(__ndptr), __inserted);
Howard Hinnant39213642013-07-23 22:01:58 +00001648#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001649}
1650
1651template <class _Tp, class _Hash, class _Equal, class _Alloc>
1652typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1653__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp)
1654{
1655 __cp->__hash_ = hash_function()(__cp->__value_);
1656 size_type __bc = bucket_count();
1657 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1658 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001659 rehash(_VSTD::max<size_type>(2 * __bc + !__is_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001660 size_type(ceil(float(size() + 1) / max_load_factor()))));
1661 __bc = bucket_count();
1662 }
Howard Hinnant7a445152012-07-06 17:31:14 +00001663 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001664 __node_pointer __pn = __bucket_list_[__chash];
1665 if (__pn == nullptr)
1666 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001667 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001668 __cp->__next_ = __pn->__next_;
1669 __pn->__next_ = __cp;
1670 // fix up __bucket_list_
1671 __bucket_list_[__chash] = __pn;
1672 if (__cp->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:14 +00001673 __bucket_list_[__constrain_hash(__cp->__next_->__hash_, __bc)] = __cp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001674 }
1675 else
1676 {
1677 for (bool __found = false; __pn->__next_ != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00001678 __constrain_hash(__pn->__next_->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001679 __pn = __pn->__next_)
Howard Hinnant324bb032010-08-22 00:02:43 +00001680 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001681 // __found key_eq() action
1682 // false false loop
1683 // true true loop
1684 // false true set __found to true
1685 // true false break
1686 if (__found != (__pn->__next_->__hash_ == __cp->__hash_ &&
1687 key_eq()(__pn->__next_->__value_, __cp->__value_)))
1688 {
1689 if (!__found)
1690 __found = true;
1691 else
1692 break;
1693 }
1694 }
1695 __cp->__next_ = __pn->__next_;
1696 __pn->__next_ = __cp;
1697 if (__cp->__next_ != nullptr)
1698 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001699 size_t __nhash = __constrain_hash(__cp->__next_->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001700 if (__nhash != __chash)
1701 __bucket_list_[__nhash] = __cp;
1702 }
1703 }
1704 ++size();
Howard Hinnant39213642013-07-23 22:01:58 +00001705#if _LIBCPP_DEBUG_LEVEL >= 2
1706 return iterator(__cp, this);
1707#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001708 return iterator(__cp);
Howard Hinnant39213642013-07-23 22:01:58 +00001709#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001710}
1711
1712template <class _Tp, class _Hash, class _Equal, class _Alloc>
1713typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1714__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(
1715 const_iterator __p, __node_pointer __cp)
1716{
Howard Hinnant824c1992013-08-02 17:50:49 +00001717#if _LIBCPP_DEBUG_LEVEL >= 2
1718 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1719 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
1720 " referring to this unordered container");
1721#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001722 if (__p != end() && key_eq()(*__p, __cp->__value_))
1723 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001724 __node_pointer __np = __p.__node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001725 __cp->__hash_ = __np->__hash_;
1726 size_type __bc = bucket_count();
1727 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1728 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001729 rehash(_VSTD::max<size_type>(2 * __bc + !__is_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001730 size_type(ceil(float(size() + 1) / max_load_factor()))));
1731 __bc = bucket_count();
1732 }
Howard Hinnant7a445152012-07-06 17:31:14 +00001733 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001734 __node_pointer __pp = __bucket_list_[__chash];
1735 while (__pp->__next_ != __np)
1736 __pp = __pp->__next_;
1737 __cp->__next_ = __np;
1738 __pp->__next_ = __cp;
1739 ++size();
Howard Hinnant39213642013-07-23 22:01:58 +00001740#if _LIBCPP_DEBUG_LEVEL >= 2
1741 return iterator(__cp, this);
1742#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001743 return iterator(__cp);
Howard Hinnant39213642013-07-23 22:01:58 +00001744#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001745 }
1746 return __node_insert_multi(__cp);
1747}
1748
1749template <class _Tp, class _Hash, class _Equal, class _Alloc>
1750pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1751__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique(const value_type& __x)
1752{
1753 size_t __hash = hash_function()(__x);
1754 size_type __bc = bucket_count();
1755 bool __inserted = false;
1756 __node_pointer __nd;
1757 size_t __chash;
1758 if (__bc != 0)
1759 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001760 __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001761 __nd = __bucket_list_[__chash];
1762 if (__nd != nullptr)
1763 {
1764 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00001765 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001766 __nd = __nd->__next_)
1767 {
1768 if (key_eq()(__nd->__value_, __x))
1769 goto __done;
1770 }
1771 }
1772 }
1773 {
1774 __node_holder __h = __construct_node(__x, __hash);
1775 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1776 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001777 rehash(_VSTD::max<size_type>(2 * __bc + !__is_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001778 size_type(ceil(float(size() + 1) / max_load_factor()))));
1779 __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00001780 __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001781 }
1782 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
1783 __node_pointer __pn = __bucket_list_[__chash];
1784 if (__pn == nullptr)
1785 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001786 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001787 __h->__next_ = __pn->__next_;
1788 __pn->__next_ = __h.get();
1789 // fix up __bucket_list_
1790 __bucket_list_[__chash] = __pn;
1791 if (__h->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:14 +00001792 __bucket_list_[__constrain_hash(__h->__next_->__hash_, __bc)] = __h.get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001793 }
1794 else
1795 {
1796 __h->__next_ = __pn->__next_;
1797 __pn->__next_ = __h.get();
1798 }
1799 __nd = __h.release();
1800 // increment size
1801 ++size();
1802 __inserted = true;
1803 }
1804__done:
Howard Hinnant39213642013-07-23 22:01:58 +00001805#if _LIBCPP_DEBUG_LEVEL >= 2
1806 return pair<iterator, bool>(iterator(__nd, this), __inserted);
1807#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001808 return pair<iterator, bool>(iterator(__nd), __inserted);
Howard Hinnant39213642013-07-23 22:01:58 +00001809#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001810}
1811
Howard Hinnant73d21a42010-09-04 23:28:19 +00001812#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1813#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001814
1815template <class _Tp, class _Hash, class _Equal, class _Alloc>
1816template <class... _Args>
1817pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1818__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique(_Args&&... __args)
1819{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001820 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001821 pair<iterator, bool> __r = __node_insert_unique(__h.get());
1822 if (__r.second)
1823 __h.release();
1824 return __r;
1825}
1826
1827template <class _Tp, class _Hash, class _Equal, class _Alloc>
1828template <class... _Args>
1829typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1830__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args)
1831{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001832 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001833 iterator __r = __node_insert_multi(__h.get());
1834 __h.release();
1835 return __r;
1836}
1837
1838template <class _Tp, class _Hash, class _Equal, class _Alloc>
1839template <class... _Args>
1840typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1841__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(
1842 const_iterator __p, _Args&&... __args)
1843{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001844#if _LIBCPP_DEBUG_LEVEL >= 2
1845 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1846 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
1847 " referring to this unordered container");
1848#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001849 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001850 iterator __r = __node_insert_multi(__p, __h.get());
1851 __h.release();
1852 return __r;
1853}
1854
Howard Hinnant73d21a42010-09-04 23:28:19 +00001855#endif // _LIBCPP_HAS_NO_VARIADICS
1856
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001857template <class _Tp, class _Hash, class _Equal, class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001858template <class _Pp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001859pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Howard Hinnant99968442011-11-29 18:15:50 +00001860__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique(_Pp&& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001861{
Howard Hinnant99968442011-11-29 18:15:50 +00001862 __node_holder __h = __construct_node(_VSTD::forward<_Pp>(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001863 pair<iterator, bool> __r = __node_insert_unique(__h.get());
1864 if (__r.second)
1865 __h.release();
1866 return __r;
1867}
1868
Howard Hinnant73d21a42010-09-04 23:28:19 +00001869#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001870
Howard Hinnant73d21a42010-09-04 23:28:19 +00001871#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001872
1873template <class _Tp, class _Hash, class _Equal, class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001874template <class _Pp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001875typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant99968442011-11-29 18:15:50 +00001876__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(_Pp&& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001877{
Howard Hinnant99968442011-11-29 18:15:50 +00001878 __node_holder __h = __construct_node(_VSTD::forward<_Pp>(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001879 iterator __r = __node_insert_multi(__h.get());
1880 __h.release();
1881 return __r;
1882}
1883
1884template <class _Tp, class _Hash, class _Equal, class _Alloc>
Howard Hinnant99968442011-11-29 18:15:50 +00001885template <class _Pp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001886typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1887__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
Howard Hinnant99968442011-11-29 18:15:50 +00001888 _Pp&& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001889{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001890#if _LIBCPP_DEBUG_LEVEL >= 2
1891 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1892 "unordered container::insert(const_iterator, rvalue) called with an iterator not"
1893 " referring to this unordered container");
1894#endif
Howard Hinnant99968442011-11-29 18:15:50 +00001895 __node_holder __h = __construct_node(_VSTD::forward<_Pp>(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001896 iterator __r = __node_insert_multi(__p, __h.get());
1897 __h.release();
1898 return __r;
1899}
1900
Howard Hinnant73d21a42010-09-04 23:28:19 +00001901#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001902
1903template <class _Tp, class _Hash, class _Equal, class _Alloc>
1904typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1905__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const value_type& __x)
1906{
1907 __node_holder __h = __construct_node(__x);
1908 iterator __r = __node_insert_multi(__h.get());
1909 __h.release();
1910 return __r;
1911}
1912
1913template <class _Tp, class _Hash, class _Equal, class _Alloc>
1914typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1915__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
1916 const value_type& __x)
1917{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001918#if _LIBCPP_DEBUG_LEVEL >= 2
1919 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1920 "unordered container::insert(const_iterator, lvalue) called with an iterator not"
1921 " referring to this unordered container");
1922#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001923 __node_holder __h = __construct_node(__x);
1924 iterator __r = __node_insert_multi(__p, __h.get());
1925 __h.release();
1926 return __r;
1927}
1928
Howard Hinnant73d21a42010-09-04 23:28:19 +00001929#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001930
1931template <class _Tp, class _Hash, class _Equal, class _Alloc>
1932void
1933__hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
1934{
Howard Hinnant7a445152012-07-06 17:31:14 +00001935 if (__n == 1)
1936 __n = 2;
1937 else if (__n & (__n - 1))
1938 __n = __next_prime(__n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001939 size_type __bc = bucket_count();
1940 if (__n > __bc)
1941 __rehash(__n);
Howard Hinnant7a445152012-07-06 17:31:14 +00001942 else if (__n < __bc)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001943 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001944 __n = _VSTD::max<size_type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001945 (
1946 __n,
Howard Hinnant7a445152012-07-06 17:31:14 +00001947 __is_power2(__bc) ? __next_pow2(size_t(ceil(float(size()) / max_load_factor()))) :
1948 __next_prime(size_t(ceil(float(size()) / max_load_factor())))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001949 );
1950 if (__n < __bc)
1951 __rehash(__n);
1952 }
1953}
1954
1955template <class _Tp, class _Hash, class _Equal, class _Alloc>
1956void
1957__hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc)
1958{
Howard Hinnant39213642013-07-23 22:01:58 +00001959#if _LIBCPP_DEBUG_LEVEL >= 2
1960 __get_db()->__invalidate_all(this);
1961#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001962 __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
1963 __bucket_list_.reset(__nbc > 0 ?
1964 __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
1965 __bucket_list_.get_deleter().size() = __nbc;
1966 if (__nbc > 0)
1967 {
1968 for (size_type __i = 0; __i < __nbc; ++__i)
1969 __bucket_list_[__i] = nullptr;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001970 __node_pointer __pp(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001971 __node_pointer __cp = __pp->__next_;
1972 if (__cp != nullptr)
1973 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001974 size_type __chash = __constrain_hash(__cp->__hash_, __nbc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001975 __bucket_list_[__chash] = __pp;
1976 size_type __phash = __chash;
1977 for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr;
1978 __cp = __pp->__next_)
1979 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001980 __chash = __constrain_hash(__cp->__hash_, __nbc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001981 if (__chash == __phash)
1982 __pp = __cp;
1983 else
1984 {
1985 if (__bucket_list_[__chash] == nullptr)
1986 {
1987 __bucket_list_[__chash] = __pp;
1988 __pp = __cp;
1989 __phash = __chash;
1990 }
1991 else
1992 {
1993 __node_pointer __np = __cp;
1994 for (; __np->__next_ != nullptr &&
1995 key_eq()(__cp->__value_, __np->__next_->__value_);
1996 __np = __np->__next_)
1997 ;
1998 __pp->__next_ = __np->__next_;
1999 __np->__next_ = __bucket_list_[__chash]->__next_;
2000 __bucket_list_[__chash]->__next_ = __cp;
Howard Hinnant324bb032010-08-22 00:02:43 +00002001
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002002 }
2003 }
2004 }
2005 }
2006 }
2007}
2008
2009template <class _Tp, class _Hash, class _Equal, class _Alloc>
2010template <class _Key>
2011typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2012__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
2013{
2014 size_t __hash = hash_function()(__k);
2015 size_type __bc = bucket_count();
2016 if (__bc != 0)
2017 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002018 size_t __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002019 __node_pointer __nd = __bucket_list_[__chash];
2020 if (__nd != nullptr)
2021 {
2022 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00002023 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002024 __nd = __nd->__next_)
2025 {
2026 if (key_eq()(__nd->__value_, __k))
Howard Hinnant39213642013-07-23 22:01:58 +00002027#if _LIBCPP_DEBUG_LEVEL >= 2
2028 return iterator(__nd, this);
2029#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002030 return iterator(__nd);
Howard Hinnant39213642013-07-23 22:01:58 +00002031#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002032 }
2033 }
2034 }
2035 return end();
2036}
2037
2038template <class _Tp, class _Hash, class _Equal, class _Alloc>
2039template <class _Key>
2040typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
2041__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
2042{
2043 size_t __hash = hash_function()(__k);
2044 size_type __bc = bucket_count();
2045 if (__bc != 0)
2046 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002047 size_t __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002048 __node_const_pointer __nd = __bucket_list_[__chash];
2049 if (__nd != nullptr)
2050 {
2051 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00002052 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002053 __nd = __nd->__next_)
2054 {
2055 if (key_eq()(__nd->__value_, __k))
Howard Hinnant39213642013-07-23 22:01:58 +00002056#if _LIBCPP_DEBUG_LEVEL >= 2
2057 return const_iterator(__nd, this);
2058#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002059 return const_iterator(__nd);
Howard Hinnant39213642013-07-23 22:01:58 +00002060#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002061 }
2062 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002063
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002064 }
2065 return end();
2066}
2067
Howard Hinnant73d21a42010-09-04 23:28:19 +00002068#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2069#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002070
2071template <class _Tp, class _Hash, class _Equal, class _Alloc>
2072template <class ..._Args>
2073typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2074__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args)
2075{
2076 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002077 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Howard Hinnant0949eed2011-06-30 21:18:19 +00002078 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002079 __h.get_deleter().__value_constructed = true;
2080 __h->__hash_ = hash_function()(__h->__value_);
2081 __h->__next_ = nullptr;
2082 return __h;
2083}
2084
Howard Hinnant73d21a42010-09-04 23:28:19 +00002085#endif // _LIBCPP_HAS_NO_VARIADICS
2086
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002087template <class _Tp, class _Hash, class _Equal, class _Alloc>
2088typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2089__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(value_type&& __v,
2090 size_t __hash)
2091{
2092 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002093 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Howard Hinnant0949eed2011-06-30 21:18:19 +00002094 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), _VSTD::move(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002095 __h.get_deleter().__value_constructed = true;
2096 __h->__hash_ = __hash;
2097 __h->__next_ = nullptr;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002098 return _VSTD::move(__h);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002099}
2100
Howard Hinnant73d21a42010-09-04 23:28:19 +00002101#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002102
2103template <class _Tp, class _Hash, class _Equal, class _Alloc>
2104typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2105__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const value_type& __v)
2106{
2107 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002108 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Howard Hinnant0949eed2011-06-30 21:18:19 +00002109 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002110 __h.get_deleter().__value_constructed = true;
2111 __h->__hash_ = hash_function()(__h->__value_);
2112 __h->__next_ = nullptr;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002113 return _VSTD::move(__h);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002114}
2115
Howard Hinnant73d21a42010-09-04 23:28:19 +00002116#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002117
2118template <class _Tp, class _Hash, class _Equal, class _Alloc>
2119typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2120__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const value_type& __v,
2121 size_t __hash)
2122{
2123 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002124 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Howard Hinnant0949eed2011-06-30 21:18:19 +00002125 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002126 __h.get_deleter().__value_constructed = true;
2127 __h->__hash_ = __hash;
2128 __h->__next_ = nullptr;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002129 return _VSTD::move(__h);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002130}
2131
2132template <class _Tp, class _Hash, class _Equal, class _Alloc>
2133typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2134__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p)
2135{
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002136 __node_pointer __np = __p.__node_;
Howard Hinnant39213642013-07-23 22:01:58 +00002137#if _LIBCPP_DEBUG_LEVEL >= 2
2138 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2139 "unordered container erase(iterator) called with an iterator not"
2140 " referring to this container");
2141 _LIBCPP_ASSERT(__p != end(),
2142 "unordered container erase(iterator) called with a non-dereferenceable iterator");
2143 iterator __r(__np, this);
2144#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002145 iterator __r(__np);
Howard Hinnant39213642013-07-23 22:01:58 +00002146#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002147 ++__r;
2148 remove(__p);
2149 return __r;
2150}
2151
2152template <class _Tp, class _Hash, class _Equal, class _Alloc>
2153typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2154__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first,
2155 const_iterator __last)
2156{
Howard Hinnant39213642013-07-23 22:01:58 +00002157#if _LIBCPP_DEBUG_LEVEL >= 2
2158 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2159 "unodered container::erase(iterator, iterator) called with an iterator not"
2160 " referring to this unodered container");
2161 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
2162 "unodered container::erase(iterator, iterator) called with an iterator not"
2163 " referring to this unodered container");
2164#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002165 for (const_iterator __p = __first; __first != __last; __p = __first)
2166 {
2167 ++__first;
2168 erase(__p);
2169 }
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002170 __node_pointer __np = __last.__node_;
Howard Hinnant39213642013-07-23 22:01:58 +00002171#if _LIBCPP_DEBUG_LEVEL >= 2
2172 return iterator (__np, this);
2173#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002174 return iterator (__np);
Howard Hinnant39213642013-07-23 22:01:58 +00002175#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002176}
2177
2178template <class _Tp, class _Hash, class _Equal, class _Alloc>
2179template <class _Key>
2180typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2181__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k)
2182{
2183 iterator __i = find(__k);
2184 if (__i == end())
2185 return 0;
2186 erase(__i);
2187 return 1;
2188}
2189
2190template <class _Tp, class _Hash, class _Equal, class _Alloc>
2191template <class _Key>
2192typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2193__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k)
2194{
2195 size_type __r = 0;
2196 iterator __i = find(__k);
2197 if (__i != end())
2198 {
2199 iterator __e = end();
2200 do
2201 {
2202 erase(__i++);
2203 ++__r;
2204 } while (__i != __e && key_eq()(*__i, __k));
2205 }
2206 return __r;
2207}
2208
2209template <class _Tp, class _Hash, class _Equal, class _Alloc>
2210typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002211__hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002212{
2213 // current node
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002214 __node_pointer __cn = __p.__node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002215 size_type __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00002216 size_t __chash = __constrain_hash(__cn->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002217 // find previous node
2218 __node_pointer __pn = __bucket_list_[__chash];
2219 for (; __pn->__next_ != __cn; __pn = __pn->__next_)
2220 ;
2221 // Fix up __bucket_list_
2222 // if __pn is not in same bucket (before begin is not in same bucket) &&
2223 // if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002224 if (__pn == static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()))
2225 || __constrain_hash(__pn->__hash_, __bc) != __chash)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002226 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002227 if (__cn->__next_ == nullptr || __constrain_hash(__cn->__next_->__hash_, __bc) != __chash)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002228 __bucket_list_[__chash] = nullptr;
2229 }
2230 // if __cn->__next_ is not in same bucket (nullptr is in same bucket)
2231 if (__cn->__next_ != nullptr)
2232 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002233 size_t __nhash = __constrain_hash(__cn->__next_->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002234 if (__nhash != __chash)
2235 __bucket_list_[__nhash] = __pn;
2236 }
2237 // remove __cn
2238 __pn->__next_ = __cn->__next_;
2239 __cn->__next_ = nullptr;
2240 --size();
Howard Hinnant39213642013-07-23 22:01:58 +00002241#if _LIBCPP_DEBUG_LEVEL >= 2
2242 __c_node* __c = __get_db()->__find_c_and_lock(this);
2243 for (__i_node** __p = __c->end_; __p != __c->beg_; )
2244 {
2245 --__p;
2246 iterator* __i = static_cast<iterator*>((*__p)->__i_);
2247 if (__i->__node_ == __cn)
2248 {
2249 (*__p)->__c_ = nullptr;
2250 if (--__c->end_ != __p)
2251 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2252 }
2253 }
2254 __get_db()->unlock();
2255#endif
Howard Hinnant99968442011-11-29 18:15:50 +00002256 return __node_holder(__cn, _Dp(__node_alloc(), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002257}
2258
2259template <class _Tp, class _Hash, class _Equal, class _Alloc>
2260template <class _Key>
Howard Hinnant99acc502010-09-21 17:32:39 +00002261inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002262typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2263__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const
2264{
2265 return static_cast<size_type>(find(__k) != end());
2266}
2267
2268template <class _Tp, class _Hash, class _Equal, class _Alloc>
2269template <class _Key>
2270typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2271__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const
2272{
2273 size_type __r = 0;
2274 const_iterator __i = find(__k);
2275 if (__i != end())
2276 {
2277 const_iterator __e = end();
2278 do
2279 {
2280 ++__i;
2281 ++__r;
2282 } while (__i != __e && key_eq()(*__i, __k));
2283 }
2284 return __r;
2285}
2286
2287template <class _Tp, class _Hash, class _Equal, class _Alloc>
2288template <class _Key>
2289pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2290 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2291__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2292 const _Key& __k)
2293{
2294 iterator __i = find(__k);
2295 iterator __j = __i;
2296 if (__i != end())
2297 ++__j;
2298 return pair<iterator, iterator>(__i, __j);
2299}
2300
2301template <class _Tp, class _Hash, class _Equal, class _Alloc>
2302template <class _Key>
2303pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2304 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2305__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2306 const _Key& __k) const
2307{
2308 const_iterator __i = find(__k);
2309 const_iterator __j = __i;
2310 if (__i != end())
2311 ++__j;
2312 return pair<const_iterator, const_iterator>(__i, __j);
2313}
2314
2315template <class _Tp, class _Hash, class _Equal, class _Alloc>
2316template <class _Key>
2317pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2318 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2319__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2320 const _Key& __k)
2321{
2322 iterator __i = find(__k);
2323 iterator __j = __i;
2324 if (__i != end())
2325 {
2326 iterator __e = end();
2327 do
2328 {
2329 ++__j;
2330 } while (__j != __e && key_eq()(*__j, __k));
2331 }
2332 return pair<iterator, iterator>(__i, __j);
2333}
2334
2335template <class _Tp, class _Hash, class _Equal, class _Alloc>
2336template <class _Key>
2337pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2338 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2339__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2340 const _Key& __k) const
2341{
2342 const_iterator __i = find(__k);
2343 const_iterator __j = __i;
2344 if (__i != end())
2345 {
2346 const_iterator __e = end();
2347 do
2348 {
2349 ++__j;
2350 } while (__j != __e && key_eq()(*__j, __k));
2351 }
2352 return pair<const_iterator, const_iterator>(__i, __j);
2353}
2354
2355template <class _Tp, class _Hash, class _Equal, class _Alloc>
2356void
2357__hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002358 _NOEXCEPT_(
2359 (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value ||
2360 __is_nothrow_swappable<__pointer_allocator>::value) &&
2361 (!__node_traits::propagate_on_container_swap::value ||
2362 __is_nothrow_swappable<__node_allocator>::value) &&
2363 __is_nothrow_swappable<hasher>::value &&
2364 __is_nothrow_swappable<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002365{
2366 {
2367 __node_pointer_pointer __npp = __bucket_list_.release();
2368 __bucket_list_.reset(__u.__bucket_list_.release());
2369 __u.__bucket_list_.reset(__npp);
2370 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002371 _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002372 __swap_alloc(__bucket_list_.get_deleter().__alloc(),
2373 __u.__bucket_list_.get_deleter().__alloc());
2374 __swap_alloc(__node_alloc(), __u.__node_alloc());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002375 _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002376 __p2_.swap(__u.__p2_);
2377 __p3_.swap(__u.__p3_);
2378 if (size() > 0)
Howard Hinnant7a445152012-07-06 17:31:14 +00002379 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002380 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002381 if (__u.size() > 0)
Howard Hinnant7a445152012-07-06 17:31:14 +00002382 __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash_, __u.bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002383 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__u.__p1_.first()));
Howard Hinnant39213642013-07-23 22:01:58 +00002384#if _LIBCPP_DEBUG_LEVEL >= 2
2385 __get_db()->swap(this, &__u);
2386#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002387}
2388
2389template <class _Tp, class _Hash, class _Equal, class _Alloc>
2390typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2391__hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const
2392{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00002393 _LIBCPP_ASSERT(__n < bucket_count(),
2394 "unordered container::bucket_size(n) called with n >= bucket_count()");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002395 __node_const_pointer __np = __bucket_list_[__n];
2396 size_type __bc = bucket_count();
2397 size_type __r = 0;
2398 if (__np != nullptr)
2399 {
2400 for (__np = __np->__next_; __np != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00002401 __constrain_hash(__np->__hash_, __bc) == __n;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002402 __np = __np->__next_, ++__r)
2403 ;
2404 }
2405 return __r;
2406}
2407
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002408template <class _Tp, class _Hash, class _Equal, class _Alloc>
2409inline _LIBCPP_INLINE_VISIBILITY
2410void
2411swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x,
2412 __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
2413 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2414{
2415 __x.swap(__y);
2416}
2417
Howard Hinnant39213642013-07-23 22:01:58 +00002418#if _LIBCPP_DEBUG_LEVEL >= 2
2419
2420template <class _Tp, class _Hash, class _Equal, class _Alloc>
2421bool
2422__hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const
2423{
2424 return __i->__node_ != nullptr;
2425}
2426
2427template <class _Tp, class _Hash, class _Equal, class _Alloc>
2428bool
2429__hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const
2430{
2431 return false;
2432}
2433
2434template <class _Tp, class _Hash, class _Equal, class _Alloc>
2435bool
2436__hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2437{
2438 return false;
2439}
2440
2441template <class _Tp, class _Hash, class _Equal, class _Alloc>
2442bool
2443__hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2444{
2445 return false;
2446}
2447
2448#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002449_LIBCPP_END_NAMESPACE_STD
2450
2451#endif // _LIBCPP__HASH_TABLE