blob: 16a0f5eb0fdae0eccb0eab6aaf67f6d8ec689ad6 [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>
Eric Fiselier774c7c52016-02-10 20:46:23 +000020#include <utility>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021
Howard Hinnant66c6f972011-11-29 16:45:27 +000022#include <__undef_min_max>
Saleem Abdulrasoolf1b30c42015-02-13 22:15:32 +000023#include <__undef___deallocate>
Howard Hinnant66c6f972011-11-29 16:45:27 +000024
Eric Fiselierb9536102014-08-10 23:53:08 +000025#include <__debug>
Howard Hinnant8b00e6c2013-08-02 00:26:35 +000026
Howard Hinnant08e17472011-10-17 20:05:10 +000027#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000028#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000029#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000030
31_LIBCPP_BEGIN_NAMESPACE_STD
32
Eric Fiselier2960ae22016-02-11 11:59:44 +000033
34#ifndef _LIBCPP_CXX03_LANG
35template <class _Key, class _Tp>
36union __hash_value_type;
37#else
38template <class _Key, class _Tp>
39struct __hash_value_type;
40#endif
41
42#ifndef _LIBCPP_CXX03_LANG
43template <class _Tp>
44struct __is_hash_value_type_imp : false_type {};
45
46template <class _Key, class _Value>
47struct __is_hash_value_type_imp<__hash_value_type<_Key, _Value>> : true_type {};
48
49template <class ..._Args>
50struct __is_hash_value_type : false_type {};
51
52template <class _One>
53struct __is_hash_value_type<_One> : __is_hash_value_type_imp<typename __uncvref<_One>::type> {};
54#endif
55
Howard Hinnant83eade62013-03-06 23:30:19 +000056_LIBCPP_FUNC_VIS
Howard Hinnant2b1b2d42011-06-14 19:58:17 +000057size_t __next_prime(size_t __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000058
59template <class _NodePtr>
60struct __hash_node_base
61{
62 typedef __hash_node_base __first_node;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063
Howard Hinnantdf85e572011-02-27 18:02:02 +000064 _NodePtr __next_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000065
Howard Hinnant5f2f14c2011-06-04 18:54:24 +000066 _LIBCPP_INLINE_VISIBILITY __hash_node_base() _NOEXCEPT : __next_(nullptr) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000067};
68
69template <class _Tp, class _VoidPtr>
70struct __hash_node
71 : public __hash_node_base
72 <
Eric Fiselier5cf84e02015-12-30 21:52:00 +000073 typename __rebind_pointer<_VoidPtr, __hash_node<_Tp, _VoidPtr> >::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000074 >
75{
Eric Fiselier774c7c52016-02-10 20:46:23 +000076 typedef _Tp __node_value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000077
78 size_t __hash_;
Eric Fiselier774c7c52016-02-10 20:46:23 +000079 __node_value_type __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000080};
81
Howard Hinnant7a445152012-07-06 17:31:14 +000082inline _LIBCPP_INLINE_VISIBILITY
83bool
Eric Fiselier57947ca2015-02-02 21:31:48 +000084__is_hash_power2(size_t __bc)
Howard Hinnant7a445152012-07-06 17:31:14 +000085{
86 return __bc > 2 && !(__bc & (__bc - 1));
87}
88
89inline _LIBCPP_INLINE_VISIBILITY
90size_t
91__constrain_hash(size_t __h, size_t __bc)
92{
93 return !(__bc & (__bc - 1)) ? __h & (__bc - 1) : __h % __bc;
94}
95
96inline _LIBCPP_INLINE_VISIBILITY
97size_t
Eric Fiselier57947ca2015-02-02 21:31:48 +000098__next_hash_pow2(size_t __n)
Howard Hinnant7a445152012-07-06 17:31:14 +000099{
100 return size_t(1) << (std::numeric_limits<size_t>::digits - __clz(__n-1));
101}
102
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +0000103#ifndef _LIBCPP_CXX03_LANG
104struct __extract_key_fail_tag {};
105struct __extract_key_self_tag {};
106struct __extract_key_first_tag {};
107
108template <class _ValTy, class _Key,
109 class _RawValTy = typename __unconstref<_ValTy>::type>
110struct __can_extract_key
111 : conditional<is_same<_RawValTy, _Key>::value, __extract_key_self_tag,
112 __extract_key_fail_tag>::type {};
113
114template <class _Pair, class _Key, class _First, class _Second>
115struct __can_extract_key<_Pair, _Key, pair<_First, _Second>>
116 : conditional<is_same<typename remove_const<_First>::type, _Key>::value,
117 __extract_key_first_tag, __extract_key_fail_tag>::type {};
118#endif
119
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000120template <class _Tp, class _Hash, class _Equal, class _Alloc> class __hash_table;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000121
122template <class _NodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_iterator;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000123template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000124template <class _NodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_local_iterator;
125template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000126template <class _HashIterator> class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
127template <class _HashIterator> class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000128
Eric Fiselier774c7c52016-02-10 20:46:23 +0000129template <class _Tp>
Eric Fiselier66e344f2016-02-20 07:59:16 +0000130struct __hash_key_value_types {
Eric Fiselier774c7c52016-02-10 20:46:23 +0000131 static_assert(!is_reference<_Tp>::value && !is_const<_Tp>::value, "");
132 typedef _Tp key_type;
133 typedef _Tp __node_value_type;
134 typedef _Tp __container_value_type;
135 static const bool __is_map = false;
Eric Fiselier2960ae22016-02-11 11:59:44 +0000136
137 _LIBCPP_INLINE_VISIBILITY
138 static key_type const& __get_key(_Tp const& __v) {
139 return __v;
140 }
141 _LIBCPP_INLINE_VISIBILITY
142 static __container_value_type const& __get_value(__node_value_type const& __v) {
143 return __v;
144 }
145 _LIBCPP_INLINE_VISIBILITY
146 static __container_value_type* __get_ptr(__node_value_type& __n) {
147 return _VSTD::addressof(__n);
148 }
149#ifndef _LIBCPP_CXX03_LANG
150 _LIBCPP_INLINE_VISIBILITY
151 static __container_value_type&& __move(__node_value_type& __v) {
152 return _VSTD::move(__v);
153 }
154#endif
Eric Fiselier774c7c52016-02-10 20:46:23 +0000155};
156
157template <class _Key, class _Tp>
Eric Fiselier66e344f2016-02-20 07:59:16 +0000158struct __hash_key_value_types<__hash_value_type<_Key, _Tp> > {
Eric Fiselier774c7c52016-02-10 20:46:23 +0000159 typedef _Key key_type;
160 typedef _Tp mapped_type;
161 typedef __hash_value_type<_Key, _Tp> __node_value_type;
162 typedef pair<const _Key, _Tp> __container_value_type;
Eric Fiselier2960ae22016-02-11 11:59:44 +0000163 typedef pair<_Key, _Tp> __nc_value_type;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000164 typedef __container_value_type __map_value_type;
165 static const bool __is_map = true;
Eric Fiselier2960ae22016-02-11 11:59:44 +0000166
167 _LIBCPP_INLINE_VISIBILITY
168 static key_type const& __get_key(__container_value_type const& __v) {
169 return __v.first;
170 }
171
172 template <class _Up>
173 _LIBCPP_INLINE_VISIBILITY
174 static typename enable_if<__is_same_uncvref<_Up, __node_value_type>::value,
175 __container_value_type const&>::type
176 __get_value(_Up& __t) {
177 return __t.__cc;
178 }
179
180 template <class _Up>
181 _LIBCPP_INLINE_VISIBILITY
182 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
183 __container_value_type const&>::type
184 __get_value(_Up& __t) {
185 return __t;
186 }
187
188 _LIBCPP_INLINE_VISIBILITY
189 static __container_value_type* __get_ptr(__node_value_type& __n) {
190 return _VSTD::addressof(__n.__cc);
191 }
192#ifndef _LIBCPP_CXX03_LANG
193 _LIBCPP_INLINE_VISIBILITY
194 static __nc_value_type&& __move(__node_value_type& __v) {
195 return _VSTD::move(__v.__nc);
196 }
197#endif
198
Eric Fiselier774c7c52016-02-10 20:46:23 +0000199};
200
Eric Fiselier66e344f2016-02-20 07:59:16 +0000201template <class _Tp, class _AllocPtr, class _KVTypes = __hash_key_value_types<_Tp>,
Eric Fiselier774c7c52016-02-10 20:46:23 +0000202 bool = _KVTypes::__is_map>
Eric Fiselier66e344f2016-02-20 07:59:16 +0000203struct __hash_map_pointer_types {};
Eric Fiselier774c7c52016-02-10 20:46:23 +0000204
205template <class _Tp, class _AllocPtr, class _KVTypes>
Eric Fiselier66e344f2016-02-20 07:59:16 +0000206struct __hash_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
Eric Fiselier774c7c52016-02-10 20:46:23 +0000207 typedef typename _KVTypes::__map_value_type _Mv;
208 typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
209 __map_value_type_pointer;
210 typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
211 __const_map_value_type_pointer;
212};
213
214template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
215struct __hash_node_types;
216
217template <class _NodePtr, class _Tp, class _VoidPtr>
218struct __hash_node_types<_NodePtr, __hash_node<_Tp, _VoidPtr> >
Eric Fiselier66e344f2016-02-20 07:59:16 +0000219 : public __hash_key_value_types<_Tp>, __hash_map_pointer_types<_Tp, _VoidPtr>
Eric Fiselier774c7c52016-02-10 20:46:23 +0000220
221{
Eric Fiselier66e344f2016-02-20 07:59:16 +0000222 typedef __hash_key_value_types<_Tp> __base;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000223
224public:
225 typedef ptrdiff_t difference_type;
226 typedef size_t size_type;
227
228 typedef typename __rebind_pointer<_NodePtr, void>::type __void_pointer;
229
230 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
231 typedef _NodePtr __node_pointer;
232
233 typedef __hash_node_base<__node_pointer> __node_base_type;
234 typedef typename __rebind_pointer<_NodePtr, __node_base_type>::type
235 __node_base_pointer;
236
237 typedef _Tp __node_value_type;
238 typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
239 __node_value_type_pointer;
240 typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
241 __const_node_value_type_pointer;
242private:
243 static_assert(!is_const<__node_type>::value,
244 "_NodePtr should never be a pointer to const");
245 static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
246 "_VoidPtr does not point to unqualified void type");
247 static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
248 _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
249};
250
251
252
253template <class _HashIterator>
254struct __hash_node_types_from_iterator;
255template <class _NodePtr>
256struct __hash_node_types_from_iterator<__hash_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
257template <class _NodePtr>
258struct __hash_node_types_from_iterator<__hash_const_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
259template <class _NodePtr>
260struct __hash_node_types_from_iterator<__hash_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
261template <class _NodePtr>
262struct __hash_node_types_from_iterator<__hash_const_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
263
264
265template <class _NodeValueTp, class _VoidPtr>
266struct __make_hash_node_types {
267 typedef __hash_node<_NodeValueTp, _VoidPtr> _NodeTp;
268 typedef typename __rebind_pointer<_VoidPtr, _NodeTp>::type _NodePtr;
269 typedef __hash_node_types<_NodePtr> type;
270};
271
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000272template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000273class _LIBCPP_TYPE_VIS_ONLY __hash_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000274{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000275 typedef __hash_node_types<_NodePtr> _NodeTypes;
276 typedef _NodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000277
278 __node_pointer __node_;
279
280public:
Eric Fiselier774c7c52016-02-10 20:46:23 +0000281 typedef forward_iterator_tag iterator_category;
282 typedef typename _NodeTypes::__node_value_type value_type;
283 typedef typename _NodeTypes::difference_type difference_type;
284 typedef value_type& reference;
285 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000286
Howard Hinnant39213642013-07-23 22:01:58 +0000287 _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT
Marshall Clow193ef032013-08-07 21:30:44 +0000288#if _LIBCPP_STD_VER > 11
289 : __node_(nullptr)
290#endif
Howard Hinnant39213642013-07-23 22:01:58 +0000291 {
292#if _LIBCPP_DEBUG_LEVEL >= 2
293 __get_db()->__insert_i(this);
294#endif
295 }
296
297#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000298
Howard Hinnant99acc502010-09-21 17:32:39 +0000299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000300 __hash_iterator(const __hash_iterator& __i)
301 : __node_(__i.__node_)
302 {
303 __get_db()->__iterator_copy(this, &__i);
304 }
305
Howard Hinnant99acc502010-09-21 17:32:39 +0000306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000307 ~__hash_iterator()
308 {
309 __get_db()->__erase_i(this);
310 }
311
312 _LIBCPP_INLINE_VISIBILITY
313 __hash_iterator& operator=(const __hash_iterator& __i)
314 {
315 if (this != &__i)
316 {
317 __get_db()->__iterator_copy(this, &__i);
318 __node_ = __i.__node_;
319 }
320 return *this;
321 }
322
323#endif // _LIBCPP_DEBUG_LEVEL >= 2
324
325 _LIBCPP_INLINE_VISIBILITY
326 reference operator*() const
327 {
328#if _LIBCPP_DEBUG_LEVEL >= 2
329 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
330 "Attempted to dereference a non-dereferenceable unordered container iterator");
331#endif
332 return __node_->__value_;
333 }
334 _LIBCPP_INLINE_VISIBILITY
335 pointer operator->() const
336 {
337#if _LIBCPP_DEBUG_LEVEL >= 2
338 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
339 "Attempted to dereference a non-dereferenceable unordered container iterator");
340#endif
341 return pointer_traits<pointer>::pointer_to(__node_->__value_);
342 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000343
Howard Hinnant99acc502010-09-21 17:32:39 +0000344 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000345 __hash_iterator& operator++()
346 {
Howard Hinnant39213642013-07-23 22:01:58 +0000347#if _LIBCPP_DEBUG_LEVEL >= 2
348 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
349 "Attempted to increment non-incrementable unordered container iterator");
350#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000351 __node_ = __node_->__next_;
352 return *this;
353 }
354
Howard Hinnant99acc502010-09-21 17:32:39 +0000355 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000356 __hash_iterator operator++(int)
357 {
358 __hash_iterator __t(*this);
359 ++(*this);
360 return __t;
361 }
362
Howard Hinnant99acc502010-09-21 17:32:39 +0000363 friend _LIBCPP_INLINE_VISIBILITY
364 bool operator==(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000365 {
Howard Hinnant39213642013-07-23 22:01:58 +0000366 return __x.__node_ == __y.__node_;
367 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000368 friend _LIBCPP_INLINE_VISIBILITY
369 bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000370 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371
372private:
Howard Hinnant39213642013-07-23 22:01:58 +0000373#if _LIBCPP_DEBUG_LEVEL >= 2
374 _LIBCPP_INLINE_VISIBILITY
375 __hash_iterator(__node_pointer __node, const void* __c) _NOEXCEPT
376 : __node_(__node)
377 {
378 __get_db()->__insert_ic(this, __c);
379 }
380#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000382 __hash_iterator(__node_pointer __node) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000383 : __node_(__node)
384 {}
Howard Hinnant39213642013-07-23 22:01:58 +0000385#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000386
387 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000388 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator;
389 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
390 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
391 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000392};
393
Eric Fiselier774c7c52016-02-10 20:46:23 +0000394template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000395class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000396{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000397 static_assert(!is_const<typename pointer_traits<_NodePtr>::element_type>::value, "");
398 typedef __hash_node_types<_NodePtr> _NodeTypes;
399 typedef _NodePtr __node_pointer;
Eric Fiselier0493d022016-02-18 00:20:34 +0000400
Eric Fiselier774c7c52016-02-10 20:46:23 +0000401 __node_pointer __node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000402
403public:
Eric Fiselier0493d022016-02-18 00:20:34 +0000404 typedef __hash_iterator<_NodePtr> __non_const_iterator;
405
Eric Fiselier774c7c52016-02-10 20:46:23 +0000406 typedef forward_iterator_tag iterator_category;
407 typedef typename _NodeTypes::__node_value_type value_type;
408 typedef typename _NodeTypes::difference_type difference_type;
409 typedef const value_type& reference;
410 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
411
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000412
Howard Hinnant39213642013-07-23 22:01:58 +0000413 _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT
Marshall Clow193ef032013-08-07 21:30:44 +0000414#if _LIBCPP_STD_VER > 11
415 : __node_(nullptr)
416#endif
Howard Hinnant39213642013-07-23 22:01:58 +0000417 {
418#if _LIBCPP_DEBUG_LEVEL >= 2
419 __get_db()->__insert_i(this);
420#endif
421 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000422 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000423 __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000424 : __node_(__x.__node_)
Howard Hinnant39213642013-07-23 22:01:58 +0000425 {
426#if _LIBCPP_DEBUG_LEVEL >= 2
427 __get_db()->__iterator_copy(this, &__x);
428#endif
429 }
430
431#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000432
Howard Hinnant99acc502010-09-21 17:32:39 +0000433 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000434 __hash_const_iterator(const __hash_const_iterator& __i)
435 : __node_(__i.__node_)
436 {
437 __get_db()->__iterator_copy(this, &__i);
438 }
439
Howard Hinnant99acc502010-09-21 17:32:39 +0000440 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000441 ~__hash_const_iterator()
442 {
443 __get_db()->__erase_i(this);
444 }
445
446 _LIBCPP_INLINE_VISIBILITY
447 __hash_const_iterator& operator=(const __hash_const_iterator& __i)
448 {
449 if (this != &__i)
450 {
451 __get_db()->__iterator_copy(this, &__i);
452 __node_ = __i.__node_;
453 }
454 return *this;
455 }
456
457#endif // _LIBCPP_DEBUG_LEVEL >= 2
458
459 _LIBCPP_INLINE_VISIBILITY
460 reference operator*() const
461 {
462#if _LIBCPP_DEBUG_LEVEL >= 2
463 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
464 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
465#endif
466 return __node_->__value_;
467 }
468 _LIBCPP_INLINE_VISIBILITY
469 pointer operator->() const
470 {
471#if _LIBCPP_DEBUG_LEVEL >= 2
472 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
473 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
474#endif
475 return pointer_traits<pointer>::pointer_to(__node_->__value_);
476 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000477
Howard Hinnant99acc502010-09-21 17:32:39 +0000478 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000479 __hash_const_iterator& operator++()
480 {
Howard Hinnant39213642013-07-23 22:01:58 +0000481#if _LIBCPP_DEBUG_LEVEL >= 2
482 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
483 "Attempted to increment non-incrementable unordered container const_iterator");
484#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000485 __node_ = __node_->__next_;
486 return *this;
487 }
488
Howard Hinnant99acc502010-09-21 17:32:39 +0000489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000490 __hash_const_iterator operator++(int)
491 {
492 __hash_const_iterator __t(*this);
493 ++(*this);
494 return __t;
495 }
496
Howard Hinnant99acc502010-09-21 17:32:39 +0000497 friend _LIBCPP_INLINE_VISIBILITY
498 bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000499 {
Howard Hinnant39213642013-07-23 22:01:58 +0000500 return __x.__node_ == __y.__node_;
501 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000502 friend _LIBCPP_INLINE_VISIBILITY
503 bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000504 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000505
506private:
Howard Hinnant39213642013-07-23 22:01:58 +0000507#if _LIBCPP_DEBUG_LEVEL >= 2
508 _LIBCPP_INLINE_VISIBILITY
509 __hash_const_iterator(__node_pointer __node, const void* __c) _NOEXCEPT
510 : __node_(__node)
511 {
512 __get_db()->__insert_ic(this, __c);
513 }
514#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000515 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000516 __hash_const_iterator(__node_pointer __node) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000517 : __node_(__node)
518 {}
Howard Hinnant39213642013-07-23 22:01:58 +0000519#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000520
521 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000522 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
523 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
524 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000525};
526
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000527template <class _NodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000528class _LIBCPP_TYPE_VIS_ONLY __hash_local_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000529{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000530 typedef __hash_node_types<_NodePtr> _NodeTypes;
531 typedef _NodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000532
533 __node_pointer __node_;
534 size_t __bucket_;
535 size_t __bucket_count_;
536
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000537public:
538 typedef forward_iterator_tag iterator_category;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000539 typedef typename _NodeTypes::__node_value_type value_type;
540 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000541 typedef value_type& reference;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000542 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000543
Howard Hinnant39213642013-07-23 22:01:58 +0000544 _LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT
545 {
546#if _LIBCPP_DEBUG_LEVEL >= 2
547 __get_db()->__insert_i(this);
548#endif
549 }
550
551#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000552
Howard Hinnant99acc502010-09-21 17:32:39 +0000553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000554 __hash_local_iterator(const __hash_local_iterator& __i)
555 : __node_(__i.__node_),
556 __bucket_(__i.__bucket_),
557 __bucket_count_(__i.__bucket_count_)
558 {
559 __get_db()->__iterator_copy(this, &__i);
560 }
561
Howard Hinnant99acc502010-09-21 17:32:39 +0000562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000563 ~__hash_local_iterator()
564 {
565 __get_db()->__erase_i(this);
566 }
567
568 _LIBCPP_INLINE_VISIBILITY
569 __hash_local_iterator& operator=(const __hash_local_iterator& __i)
570 {
571 if (this != &__i)
572 {
573 __get_db()->__iterator_copy(this, &__i);
574 __node_ = __i.__node_;
575 __bucket_ = __i.__bucket_;
576 __bucket_count_ = __i.__bucket_count_;
577 }
578 return *this;
579 }
580
581#endif // _LIBCPP_DEBUG_LEVEL >= 2
582
583 _LIBCPP_INLINE_VISIBILITY
584 reference operator*() const
585 {
586#if _LIBCPP_DEBUG_LEVEL >= 2
587 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
588 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
589#endif
590 return __node_->__value_;
591 }
592 _LIBCPP_INLINE_VISIBILITY
593 pointer operator->() const
594 {
595#if _LIBCPP_DEBUG_LEVEL >= 2
596 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
597 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
598#endif
599 return pointer_traits<pointer>::pointer_to(__node_->__value_);
600 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000601
Howard Hinnant99acc502010-09-21 17:32:39 +0000602 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000603 __hash_local_iterator& operator++()
604 {
Howard Hinnant39213642013-07-23 22:01:58 +0000605#if _LIBCPP_DEBUG_LEVEL >= 2
606 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
607 "Attempted to increment non-incrementable unordered container local_iterator");
608#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000609 __node_ = __node_->__next_;
Howard Hinnant7a445152012-07-06 17:31:14 +0000610 if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000611 __node_ = nullptr;
612 return *this;
613 }
614
Howard Hinnant99acc502010-09-21 17:32:39 +0000615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000616 __hash_local_iterator operator++(int)
617 {
618 __hash_local_iterator __t(*this);
619 ++(*this);
620 return __t;
621 }
622
Howard Hinnant99acc502010-09-21 17:32:39 +0000623 friend _LIBCPP_INLINE_VISIBILITY
624 bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000625 {
Howard Hinnant39213642013-07-23 22:01:58 +0000626 return __x.__node_ == __y.__node_;
627 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000628 friend _LIBCPP_INLINE_VISIBILITY
629 bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000630 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000631
632private:
Howard Hinnant39213642013-07-23 22:01:58 +0000633#if _LIBCPP_DEBUG_LEVEL >= 2
634 _LIBCPP_INLINE_VISIBILITY
635 __hash_local_iterator(__node_pointer __node, size_t __bucket,
636 size_t __bucket_count, const void* __c) _NOEXCEPT
637 : __node_(__node),
638 __bucket_(__bucket),
639 __bucket_count_(__bucket_count)
640 {
641 __get_db()->__insert_ic(this, __c);
642 if (__node_ != nullptr)
643 __node_ = __node_->__next_;
644 }
645#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000646 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000647 __hash_local_iterator(__node_pointer __node, size_t __bucket,
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000648 size_t __bucket_count) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000649 : __node_(__node),
650 __bucket_(__bucket),
651 __bucket_count_(__bucket_count)
652 {
653 if (__node_ != nullptr)
654 __node_ = __node_->__next_;
655 }
Howard Hinnant39213642013-07-23 22:01:58 +0000656#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000657 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000658 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
659 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000660};
661
662template <class _ConstNodePtr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000663class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000664{
Eric Fiselier774c7c52016-02-10 20:46:23 +0000665 typedef __hash_node_types<_ConstNodePtr> _NodeTypes;
666 typedef _ConstNodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000667
668 __node_pointer __node_;
669 size_t __bucket_;
670 size_t __bucket_count_;
671
672 typedef pointer_traits<__node_pointer> __pointer_traits;
673 typedef typename __pointer_traits::element_type __node;
674 typedef typename remove_const<__node>::type __non_const_node;
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000675 typedef typename __rebind_pointer<__node_pointer, __non_const_node>::type
676 __non_const_node_pointer;
Eric Fiselier0493d022016-02-18 00:20:34 +0000677public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000678 typedef __hash_local_iterator<__non_const_node_pointer>
679 __non_const_iterator;
Eric Fiselier0493d022016-02-18 00:20:34 +0000680
Eric Fiselier774c7c52016-02-10 20:46:23 +0000681 typedef forward_iterator_tag iterator_category;
682 typedef typename _NodeTypes::__node_value_type value_type;
683 typedef typename _NodeTypes::difference_type difference_type;
684 typedef const value_type& reference;
685 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000686
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000687
Howard Hinnant39213642013-07-23 22:01:58 +0000688 _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT
689 {
690#if _LIBCPP_DEBUG_LEVEL >= 2
691 __get_db()->__insert_i(this);
692#endif
693 }
694
Howard Hinnant99acc502010-09-21 17:32:39 +0000695 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000696 __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000697 : __node_(__x.__node_),
698 __bucket_(__x.__bucket_),
699 __bucket_count_(__x.__bucket_count_)
Howard Hinnant39213642013-07-23 22:01:58 +0000700 {
701#if _LIBCPP_DEBUG_LEVEL >= 2
702 __get_db()->__iterator_copy(this, &__x);
703#endif
704 }
705
706#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000707
Howard Hinnant99acc502010-09-21 17:32:39 +0000708 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000709 __hash_const_local_iterator(const __hash_const_local_iterator& __i)
710 : __node_(__i.__node_),
711 __bucket_(__i.__bucket_),
712 __bucket_count_(__i.__bucket_count_)
713 {
714 __get_db()->__iterator_copy(this, &__i);
715 }
716
Howard Hinnant99acc502010-09-21 17:32:39 +0000717 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant39213642013-07-23 22:01:58 +0000718 ~__hash_const_local_iterator()
719 {
720 __get_db()->__erase_i(this);
721 }
722
723 _LIBCPP_INLINE_VISIBILITY
724 __hash_const_local_iterator& operator=(const __hash_const_local_iterator& __i)
725 {
726 if (this != &__i)
727 {
728 __get_db()->__iterator_copy(this, &__i);
729 __node_ = __i.__node_;
730 __bucket_ = __i.__bucket_;
731 __bucket_count_ = __i.__bucket_count_;
732 }
733 return *this;
734 }
735
736#endif // _LIBCPP_DEBUG_LEVEL >= 2
737
738 _LIBCPP_INLINE_VISIBILITY
739 reference operator*() const
740 {
741#if _LIBCPP_DEBUG_LEVEL >= 2
742 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
743 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
744#endif
745 return __node_->__value_;
746 }
747 _LIBCPP_INLINE_VISIBILITY
748 pointer operator->() const
749 {
750#if _LIBCPP_DEBUG_LEVEL >= 2
751 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
752 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
753#endif
754 return pointer_traits<pointer>::pointer_to(__node_->__value_);
755 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000756
Howard Hinnant99acc502010-09-21 17:32:39 +0000757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000758 __hash_const_local_iterator& operator++()
759 {
Howard Hinnant39213642013-07-23 22:01:58 +0000760#if _LIBCPP_DEBUG_LEVEL >= 2
761 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
762 "Attempted to increment non-incrementable unordered container const_local_iterator");
763#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000764 __node_ = __node_->__next_;
Howard Hinnant7a445152012-07-06 17:31:14 +0000765 if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000766 __node_ = nullptr;
767 return *this;
768 }
769
Howard Hinnant99acc502010-09-21 17:32:39 +0000770 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000771 __hash_const_local_iterator operator++(int)
772 {
773 __hash_const_local_iterator __t(*this);
774 ++(*this);
775 return __t;
776 }
777
Howard Hinnant99acc502010-09-21 17:32:39 +0000778 friend _LIBCPP_INLINE_VISIBILITY
779 bool operator==(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000780 {
Howard Hinnant39213642013-07-23 22:01:58 +0000781 return __x.__node_ == __y.__node_;
782 }
Howard Hinnant99acc502010-09-21 17:32:39 +0000783 friend _LIBCPP_INLINE_VISIBILITY
784 bool operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnant39213642013-07-23 22:01:58 +0000785 {return !(__x == __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000786
787private:
Howard Hinnant39213642013-07-23 22:01:58 +0000788#if _LIBCPP_DEBUG_LEVEL >= 2
789 _LIBCPP_INLINE_VISIBILITY
790 __hash_const_local_iterator(__node_pointer __node, size_t __bucket,
791 size_t __bucket_count, const void* __c) _NOEXCEPT
792 : __node_(__node),
793 __bucket_(__bucket),
794 __bucket_count_(__bucket_count)
795 {
796 __get_db()->__insert_ic(this, __c);
797 if (__node_ != nullptr)
798 __node_ = __node_->__next_;
799 }
800#else
Howard Hinnant99acc502010-09-21 17:32:39 +0000801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000802 __hash_const_local_iterator(__node_pointer __node, size_t __bucket,
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000803 size_t __bucket_count) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000804 : __node_(__node),
805 __bucket_(__bucket),
806 __bucket_count_(__bucket_count)
807 {
808 if (__node_ != nullptr)
809 __node_ = __node_->__next_;
810 }
Howard Hinnant39213642013-07-23 22:01:58 +0000811#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000812 template <class, class, class, class> friend class __hash_table;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000813 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000814};
815
816template <class _Alloc>
817class __bucket_list_deallocator
818{
819 typedef _Alloc allocator_type;
820 typedef allocator_traits<allocator_type> __alloc_traits;
821 typedef typename __alloc_traits::size_type size_type;
822
823 __compressed_pair<size_type, allocator_type> __data_;
824public:
825 typedef typename __alloc_traits::pointer pointer;
826
Howard Hinnant99acc502010-09-21 17:32:39 +0000827 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000828 __bucket_list_deallocator()
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000829 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000830 : __data_(0) {}
Howard Hinnant99acc502010-09-21 17:32:39 +0000831
832 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000833 __bucket_list_deallocator(const allocator_type& __a, size_type __size)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000834 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000835 : __data_(__size, __a) {}
836
Howard Hinnant73d21a42010-09-04 23:28:19 +0000837#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000838
Howard Hinnant99acc502010-09-21 17:32:39 +0000839 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000840 __bucket_list_deallocator(__bucket_list_deallocator&& __x)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000841 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000842 : __data_(_VSTD::move(__x.__data_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000843 {
844 __x.size() = 0;
845 }
846
Howard Hinnant73d21a42010-09-04 23:28:19 +0000847#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000848
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000849 _LIBCPP_INLINE_VISIBILITY
850 size_type& size() _NOEXCEPT {return __data_.first();}
851 _LIBCPP_INLINE_VISIBILITY
852 size_type size() const _NOEXCEPT {return __data_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000853
Howard Hinnant99acc502010-09-21 17:32:39 +0000854 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000855 allocator_type& __alloc() _NOEXCEPT {return __data_.second();}
856 _LIBCPP_INLINE_VISIBILITY
857 const allocator_type& __alloc() const _NOEXCEPT {return __data_.second();}
858
859 _LIBCPP_INLINE_VISIBILITY
860 void operator()(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000861 {
862 __alloc_traits::deallocate(__alloc(), __p, size());
863 }
864};
865
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000866template <class _Alloc> class __hash_map_node_destructor;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000867
868template <class _Alloc>
869class __hash_node_destructor
870{
871 typedef _Alloc allocator_type;
872 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000873
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000874public:
875 typedef typename __alloc_traits::pointer pointer;
876private:
Eric Fiselier2960ae22016-02-11 11:59:44 +0000877 typedef __hash_node_types<pointer> _NodeTypes;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000878
879 allocator_type& __na_;
880
881 __hash_node_destructor& operator=(const __hash_node_destructor&);
882
883public:
884 bool __value_constructed;
885
Howard Hinnant99acc502010-09-21 17:32:39 +0000886 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant199d0ae2011-07-31 17:04:30 +0000887 explicit __hash_node_destructor(allocator_type& __na,
888 bool __constructed = false) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000889 : __na_(__na),
Howard Hinnant199d0ae2011-07-31 17:04:30 +0000890 __value_constructed(__constructed)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000891 {}
892
Howard Hinnant99acc502010-09-21 17:32:39 +0000893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000894 void operator()(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000895 {
896 if (__value_constructed)
Eric Fiselier2960ae22016-02-11 11:59:44 +0000897 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000898 if (__p)
899 __alloc_traits::deallocate(__na_, __p, 1);
900 }
901
902 template <class> friend class __hash_map_node_destructor;
903};
904
905template <class _Tp, class _Hash, class _Equal, class _Alloc>
906class __hash_table
907{
908public:
909 typedef _Tp value_type;
910 typedef _Hash hasher;
911 typedef _Equal key_equal;
912 typedef _Alloc allocator_type;
913
914private:
915 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000916 typedef typename
917 __make_hash_node_types<value_type, typename __alloc_traits::void_pointer>::type
918 _NodeTypes;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000919public:
Eric Fiselier2960ae22016-02-11 11:59:44 +0000920
921 typedef typename _NodeTypes::__node_value_type __node_value_type;
922 typedef typename _NodeTypes::__container_value_type __container_value_type;
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +0000923 typedef typename _NodeTypes::key_type key_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000924 typedef value_type& reference;
925 typedef const value_type& const_reference;
926 typedef typename __alloc_traits::pointer pointer;
927 typedef typename __alloc_traits::const_pointer const_pointer;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000928#ifndef _LIBCPP_ABI_FIX_UNORDERED_CONTAINER_SIZE_TYPE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000929 typedef typename __alloc_traits::size_type size_type;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000930#else
931 typedef typename _NodeTypes::size_type size_type;
932#endif
933 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000934public:
935 // Create __node
Eric Fiselier774c7c52016-02-10 20:46:23 +0000936
937 typedef typename _NodeTypes::__node_type __node;
Marshall Clow66302c62015-04-07 05:21:38 +0000938 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000939 typedef allocator_traits<__node_allocator> __node_traits;
Eric Fiselier9e9f42e2016-02-11 15:22:37 +0000940 typedef typename _NodeTypes::__void_pointer __void_pointer;
Eric Fiselier774c7c52016-02-10 20:46:23 +0000941 typedef typename _NodeTypes::__node_pointer __node_pointer;
942 typedef typename _NodeTypes::__node_pointer __node_const_pointer;
943 typedef typename _NodeTypes::__node_base_type __first_node;
944 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
945
946private:
947 // check for sane allocator pointer rebinding semantics. Rebinding the
948 // allocator for a new pointer type should be exactly the same as rebinding
949 // the pointer using 'pointer_traits'.
950 static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
951 "Allocator does not rebind pointers in a sane manner.");
952 typedef typename __rebind_alloc_helper<__node_traits, __first_node>::type
953 __node_base_allocator;
954 typedef allocator_traits<__node_base_allocator> __node_base_traits;
955 static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
956 "Allocator does not rebind pointers in a sane manner.");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000957
958private:
959
Marshall Clow66302c62015-04-07 05:21:38 +0000960 typedef typename __rebind_alloc_helper<__node_traits, __node_pointer>::type __pointer_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000961 typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter;
962 typedef unique_ptr<__node_pointer[], __bucket_list_deleter> __bucket_list;
963 typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits;
964 typedef typename __bucket_list_deleter::pointer __node_pointer_pointer;
965
966 // --- Member data begin ---
Eric Fiselier774c7c52016-02-10 20:46:23 +0000967 __bucket_list __bucket_list_;
968 __compressed_pair<__first_node, __node_allocator> __p1_;
969 __compressed_pair<size_type, hasher> __p2_;
970 __compressed_pair<float, key_equal> __p3_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000971 // --- Member data end ---
972
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000973 _LIBCPP_INLINE_VISIBILITY
974 size_type& size() _NOEXCEPT {return __p2_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000975public:
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000976 _LIBCPP_INLINE_VISIBILITY
977 size_type size() const _NOEXCEPT {return __p2_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000978
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000979 _LIBCPP_INLINE_VISIBILITY
980 hasher& hash_function() _NOEXCEPT {return __p2_.second();}
981 _LIBCPP_INLINE_VISIBILITY
982 const hasher& hash_function() const _NOEXCEPT {return __p2_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000983
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000984 _LIBCPP_INLINE_VISIBILITY
985 float& max_load_factor() _NOEXCEPT {return __p3_.first();}
986 _LIBCPP_INLINE_VISIBILITY
987 float max_load_factor() const _NOEXCEPT {return __p3_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000988
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000989 _LIBCPP_INLINE_VISIBILITY
990 key_equal& key_eq() _NOEXCEPT {return __p3_.second();}
991 _LIBCPP_INLINE_VISIBILITY
992 const key_equal& key_eq() const _NOEXCEPT {return __p3_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000993
Howard Hinnant5f2f14c2011-06-04 18:54:24 +0000994 _LIBCPP_INLINE_VISIBILITY
995 __node_allocator& __node_alloc() _NOEXCEPT {return __p1_.second();}
996 _LIBCPP_INLINE_VISIBILITY
997 const __node_allocator& __node_alloc() const _NOEXCEPT
998 {return __p1_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000999
1000public:
1001 typedef __hash_iterator<__node_pointer> iterator;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001002 typedef __hash_const_iterator<__node_pointer> const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001003 typedef __hash_local_iterator<__node_pointer> local_iterator;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001004 typedef __hash_const_local_iterator<__node_pointer> const_local_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001005
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001006 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001007 __hash_table()
1008 _NOEXCEPT_(
1009 is_nothrow_default_constructible<__bucket_list>::value &&
1010 is_nothrow_default_constructible<__first_node>::value &&
1011 is_nothrow_default_constructible<__node_allocator>::value &&
1012 is_nothrow_default_constructible<hasher>::value &&
1013 is_nothrow_default_constructible<key_equal>::value);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001014 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001015 __hash_table(const hasher& __hf, const key_equal& __eql);
1016 __hash_table(const hasher& __hf, const key_equal& __eql,
1017 const allocator_type& __a);
1018 explicit __hash_table(const allocator_type& __a);
1019 __hash_table(const __hash_table& __u);
1020 __hash_table(const __hash_table& __u, const allocator_type& __a);
Eric Fiselier2960ae22016-02-11 11:59:44 +00001021#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001022 __hash_table(__hash_table&& __u)
1023 _NOEXCEPT_(
1024 is_nothrow_move_constructible<__bucket_list>::value &&
1025 is_nothrow_move_constructible<__first_node>::value &&
1026 is_nothrow_move_constructible<__node_allocator>::value &&
1027 is_nothrow_move_constructible<hasher>::value &&
1028 is_nothrow_move_constructible<key_equal>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001029 __hash_table(__hash_table&& __u, const allocator_type& __a);
Eric Fiselier2960ae22016-02-11 11:59:44 +00001030#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001031 ~__hash_table();
1032
1033 __hash_table& operator=(const __hash_table& __u);
Eric Fiselier2960ae22016-02-11 11:59:44 +00001034#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001035 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001036 __hash_table& operator=(__hash_table&& __u)
1037 _NOEXCEPT_(
1038 __node_traits::propagate_on_container_move_assignment::value &&
1039 is_nothrow_move_assignable<__node_allocator>::value &&
1040 is_nothrow_move_assignable<hasher>::value &&
1041 is_nothrow_move_assignable<key_equal>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001042#endif
1043 template <class _InputIterator>
1044 void __assign_unique(_InputIterator __first, _InputIterator __last);
1045 template <class _InputIterator>
1046 void __assign_multi(_InputIterator __first, _InputIterator __last);
1047
Howard Hinnant99acc502010-09-21 17:32:39 +00001048 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001049 size_type max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001050 {
1051 return allocator_traits<__pointer_allocator>::max_size(
1052 __bucket_list_.get_deleter().__alloc());
1053 }
1054
1055 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1056 iterator __node_insert_multi(__node_pointer __nd);
1057 iterator __node_insert_multi(const_iterator __p,
1058 __node_pointer __nd);
1059
Eric Fiselier2960ae22016-02-11 11:59:44 +00001060#ifndef _LIBCPP_CXX03_LANG
1061 template <class _Key, class ..._Args>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +00001062 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001063 pair<iterator, bool> __emplace_unique_key_args(_Key const& __k, _Args&&... __args);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001064
Eric Fiselier2960ae22016-02-11 11:59:44 +00001065 template <class... _Args>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +00001066 _LIBCPP_INLINE_VISIBILITY
1067 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
1068
1069 template <class _Pp>
1070 _LIBCPP_INLINE_VISIBILITY
1071 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1072 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1073 __can_extract_key<_Pp, key_type>());
1074 }
Eric Fiselier2960ae22016-02-11 11:59:44 +00001075 template <class... _Args>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +00001076 _LIBCPP_INLINE_VISIBILITY
1077 pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1078 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1079 }
1080
1081 template <class _Pp>
1082 _LIBCPP_INLINE_VISIBILITY
1083 pair<iterator, bool>
1084 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1085 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1086 }
1087 template <class _Pp>
1088 _LIBCPP_INLINE_VISIBILITY
1089 pair<iterator, bool>
1090 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1091 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1092 }
1093 template <class _Pp>
1094 _LIBCPP_INLINE_VISIBILITY
1095 pair<iterator, bool>
1096 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1097 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1098 }
1099
1100 template <class... _Args>
1101 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001102 iterator __emplace_multi(_Args&&... __args);
1103 template <class... _Args>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +00001104 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001105 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
1106
1107
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001108 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001109 pair<iterator, bool>
1110 __insert_unique(__container_value_type&& __x) {
1111 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), _VSTD::move(__x));
1112 }
1113
1114 template <class _Pp, class = typename enable_if<
1115 !__is_same_uncvref<_Pp, __container_value_type>::value
1116 >::type>
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001117 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2960ae22016-02-11 11:59:44 +00001118 pair<iterator, bool> __insert_unique(_Pp&& __x) {
1119 return __emplace_unique(_VSTD::forward<_Pp>(__x));
1120 }
1121
1122 template <class _Pp>
1123 _LIBCPP_INLINE_VISIBILITY
1124 iterator __insert_multi(_Pp&& __x) {
1125 return __emplace_multi(_VSTD::forward<_Pp>(__x));
1126 }
1127
1128 template <class _Pp>
1129 _LIBCPP_INLINE_VISIBILITY
1130 iterator __insert_multi(const_iterator __p, _Pp&& __x) {
1131 return __emplace_hint_multi(__p, _VSTD::forward<_Pp>(__x));
1132 }
1133
1134#else // !defined(_LIBCPP_CXX03_LANG)
1135 template <class _Key, class _Args>
1136 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1137
1138 iterator __insert_multi(const __container_value_type& __x);
1139 iterator __insert_multi(const_iterator __p, const __container_value_type& __x);
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001140#endif
1141
Eric Fiselier2960ae22016-02-11 11:59:44 +00001142 _LIBCPP_INLINE_VISIBILITY
1143 pair<iterator, bool> __insert_unique(const __container_value_type& __x) {
1144 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), __x);
1145 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001146
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001147 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001148 void rehash(size_type __n);
Howard Hinnant99acc502010-09-21 17:32:39 +00001149 _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001150 {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));}
Howard Hinnant99acc502010-09-21 17:32:39 +00001151
1152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001153 size_type bucket_count() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001154 {
1155 return __bucket_list_.get_deleter().size();
1156 }
1157
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001159 iterator begin() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001161 iterator end() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001162 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001163 const_iterator begin() const _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001165 const_iterator end() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001166
1167 template <class _Key>
Howard Hinnant99acc502010-09-21 17:32:39 +00001168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001169 size_type bucket(const _Key& __k) const
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001170 {
1171 _LIBCPP_ASSERT(bucket_count() > 0,
1172 "unordered container::bucket(key) called when bucket_count() == 0");
1173 return __constrain_hash(hash_function()(__k), bucket_count());
1174 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001175
1176 template <class _Key>
1177 iterator find(const _Key& __x);
1178 template <class _Key>
1179 const_iterator find(const _Key& __x) const;
1180
Howard Hinnant99968442011-11-29 18:15:50 +00001181 typedef __hash_node_destructor<__node_allocator> _Dp;
1182 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001183
1184 iterator erase(const_iterator __p);
1185 iterator erase(const_iterator __first, const_iterator __last);
1186 template <class _Key>
1187 size_type __erase_unique(const _Key& __k);
1188 template <class _Key>
1189 size_type __erase_multi(const _Key& __k);
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001190 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001191
1192 template <class _Key>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001193 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001194 size_type __count_unique(const _Key& __k) const;
1195 template <class _Key>
1196 size_type __count_multi(const _Key& __k) const;
1197
1198 template <class _Key>
1199 pair<iterator, iterator>
1200 __equal_range_unique(const _Key& __k);
1201 template <class _Key>
1202 pair<const_iterator, const_iterator>
1203 __equal_range_unique(const _Key& __k) const;
1204
1205 template <class _Key>
1206 pair<iterator, iterator>
1207 __equal_range_multi(const _Key& __k);
1208 template <class _Key>
1209 pair<const_iterator, const_iterator>
1210 __equal_range_multi(const _Key& __k) const;
1211
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001212 void swap(__hash_table& __u)
Eric Fiselier692177d2015-07-18 20:40:46 +00001213#if _LIBCPP_STD_VER <= 11
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001214 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:56 +00001215 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clow7d914d12015-07-13 20:04:56 +00001216 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
1217 || __is_nothrow_swappable<__pointer_allocator>::value)
1218 && (!__node_traits::propagate_on_container_swap::value
1219 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +00001220 );
Eric Fiselier692177d2015-07-18 20:40:46 +00001221#else
1222 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value);
1223#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001224
Howard Hinnant99acc502010-09-21 17:32:39 +00001225 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001226 size_type max_bucket_count() const _NOEXCEPT
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001227 {return __pointer_alloc_traits::max_size(__bucket_list_.get_deleter().__alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001228 size_type bucket_size(size_type __n) const;
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001229 _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001230 {
1231 size_type __bc = bucket_count();
1232 return __bc != 0 ? (float)size() / __bc : 0.f;
1233 }
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001234 _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001235 {
1236 _LIBCPP_ASSERT(__mlf > 0,
1237 "unordered container::max_load_factor(lf) called with lf <= 0");
1238 max_load_factor() = _VSTD::max(__mlf, load_factor());
1239 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001240
Howard Hinnant39213642013-07-23 22:01:58 +00001241 _LIBCPP_INLINE_VISIBILITY
1242 local_iterator
1243 begin(size_type __n)
1244 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001245 _LIBCPP_ASSERT(__n < bucket_count(),
1246 "unordered container::begin(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001247#if _LIBCPP_DEBUG_LEVEL >= 2
1248 return local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1249#else
1250 return local_iterator(__bucket_list_[__n], __n, bucket_count());
1251#endif
1252 }
1253
1254 _LIBCPP_INLINE_VISIBILITY
1255 local_iterator
1256 end(size_type __n)
1257 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001258 _LIBCPP_ASSERT(__n < bucket_count(),
1259 "unordered container::end(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001260#if _LIBCPP_DEBUG_LEVEL >= 2
1261 return local_iterator(nullptr, __n, bucket_count(), this);
1262#else
1263 return local_iterator(nullptr, __n, bucket_count());
1264#endif
1265 }
1266
1267 _LIBCPP_INLINE_VISIBILITY
1268 const_local_iterator
1269 cbegin(size_type __n) const
1270 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001271 _LIBCPP_ASSERT(__n < bucket_count(),
1272 "unordered container::cbegin(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001273#if _LIBCPP_DEBUG_LEVEL >= 2
1274 return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1275#else
1276 return const_local_iterator(__bucket_list_[__n], __n, bucket_count());
1277#endif
1278 }
1279
1280 _LIBCPP_INLINE_VISIBILITY
1281 const_local_iterator
1282 cend(size_type __n) const
1283 {
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00001284 _LIBCPP_ASSERT(__n < bucket_count(),
1285 "unordered container::cend(n) called with n >= bucket_count()");
Howard Hinnant39213642013-07-23 22:01:58 +00001286#if _LIBCPP_DEBUG_LEVEL >= 2
1287 return const_local_iterator(nullptr, __n, bucket_count(), this);
1288#else
1289 return const_local_iterator(nullptr, __n, bucket_count());
1290#endif
1291 }
1292
1293#if _LIBCPP_DEBUG_LEVEL >= 2
1294
1295 bool __dereferenceable(const const_iterator* __i) const;
1296 bool __decrementable(const const_iterator* __i) const;
1297 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1298 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1299
1300#endif // _LIBCPP_DEBUG_LEVEL >= 2
1301
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001302private:
1303 void __rehash(size_type __n);
1304
Eric Fiselier2960ae22016-02-11 11:59:44 +00001305#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001306 template <class ..._Args>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001307 __node_holder __construct_node(_Args&& ...__args);
1308
1309 template <class _First, class ..._Rest>
1310 __node_holder __construct_node_hash(size_t __hash, _First&& __f, _Rest&&... __rest);
1311#else // _LIBCPP_CXX03_LANG
1312 __node_holder __construct_node(const __container_value_type& __v);
1313 __node_holder __construct_node_hash(size_t __hash, const __container_value_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001314#endif
Eric Fiselier2960ae22016-02-11 11:59:44 +00001315
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001316
Howard Hinnant99acc502010-09-21 17:32:39 +00001317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001318 void __copy_assign_alloc(const __hash_table& __u)
1319 {__copy_assign_alloc(__u, integral_constant<bool,
1320 __node_traits::propagate_on_container_copy_assignment::value>());}
1321 void __copy_assign_alloc(const __hash_table& __u, true_type);
Howard Hinnant99acc502010-09-21 17:32:39 +00001322 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001323 void __copy_assign_alloc(const __hash_table&, false_type) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001324
Eric Fiselier2960ae22016-02-11 11:59:44 +00001325#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326 void __move_assign(__hash_table& __u, false_type);
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001327 void __move_assign(__hash_table& __u, true_type)
1328 _NOEXCEPT_(
1329 is_nothrow_move_assignable<__node_allocator>::value &&
1330 is_nothrow_move_assignable<hasher>::value &&
1331 is_nothrow_move_assignable<key_equal>::value);
1332 _LIBCPP_INLINE_VISIBILITY
1333 void __move_assign_alloc(__hash_table& __u)
1334 _NOEXCEPT_(
1335 !__node_traits::propagate_on_container_move_assignment::value ||
1336 (is_nothrow_move_assignable<__pointer_allocator>::value &&
1337 is_nothrow_move_assignable<__node_allocator>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001338 {__move_assign_alloc(__u, integral_constant<bool,
1339 __node_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant99acc502010-09-21 17:32:39 +00001340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001341 void __move_assign_alloc(__hash_table& __u, true_type)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001342 _NOEXCEPT_(
1343 is_nothrow_move_assignable<__pointer_allocator>::value &&
1344 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001345 {
1346 __bucket_list_.get_deleter().__alloc() =
Howard Hinnant0949eed2011-06-30 21:18:19 +00001347 _VSTD::move(__u.__bucket_list_.get_deleter().__alloc());
1348 __node_alloc() = _VSTD::move(__u.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001349 }
Howard Hinnant99acc502010-09-21 17:32:39 +00001350 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001351 void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
Eric Fiselier2960ae22016-02-11 11:59:44 +00001352#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001353
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001354 void __deallocate(__node_pointer __np) _NOEXCEPT;
1355 __node_pointer __detach() _NOEXCEPT;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001356
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001357 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_map;
1358 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001359};
1360
1361template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001362inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001363__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table()
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001364 _NOEXCEPT_(
1365 is_nothrow_default_constructible<__bucket_list>::value &&
1366 is_nothrow_default_constructible<__first_node>::value &&
Eric Fiselierc8f54c22015-12-16 00:53:04 +00001367 is_nothrow_default_constructible<__node_allocator>::value &&
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001368 is_nothrow_default_constructible<hasher>::value &&
1369 is_nothrow_default_constructible<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001370 : __p2_(0),
1371 __p3_(1.0f)
1372{
1373}
1374
1375template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001376inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001377__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1378 const key_equal& __eql)
1379 : __bucket_list_(nullptr, __bucket_list_deleter()),
1380 __p1_(),
1381 __p2_(0, __hf),
1382 __p3_(1.0f, __eql)
1383{
1384}
1385
1386template <class _Tp, class _Hash, class _Equal, class _Alloc>
1387__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1388 const key_equal& __eql,
1389 const allocator_type& __a)
1390 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1391 __p1_(__node_allocator(__a)),
1392 __p2_(0, __hf),
1393 __p3_(1.0f, __eql)
1394{
1395}
1396
1397template <class _Tp, class _Hash, class _Equal, class _Alloc>
1398__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a)
1399 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1400 __p1_(__node_allocator(__a)),
1401 __p2_(0),
1402 __p3_(1.0f)
1403{
1404}
1405
1406template <class _Tp, class _Hash, class _Equal, class _Alloc>
1407__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u)
1408 : __bucket_list_(nullptr,
1409 __bucket_list_deleter(allocator_traits<__pointer_allocator>::
1410 select_on_container_copy_construction(
1411 __u.__bucket_list_.get_deleter().__alloc()), 0)),
1412 __p1_(allocator_traits<__node_allocator>::
1413 select_on_container_copy_construction(__u.__node_alloc())),
1414 __p2_(0, __u.hash_function()),
1415 __p3_(__u.__p3_)
1416{
1417}
1418
1419template <class _Tp, class _Hash, class _Equal, class _Alloc>
1420__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u,
1421 const allocator_type& __a)
1422 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1423 __p1_(__node_allocator(__a)),
1424 __p2_(0, __u.hash_function()),
1425 __p3_(__u.__p3_)
1426{
1427}
1428
Eric Fiselier2960ae22016-02-11 11:59:44 +00001429#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001430
1431template <class _Tp, class _Hash, class _Equal, class _Alloc>
1432__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001433 _NOEXCEPT_(
1434 is_nothrow_move_constructible<__bucket_list>::value &&
1435 is_nothrow_move_constructible<__first_node>::value &&
Eric Fiselierc8f54c22015-12-16 00:53:04 +00001436 is_nothrow_move_constructible<__node_allocator>::value &&
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001437 is_nothrow_move_constructible<hasher>::value &&
1438 is_nothrow_move_constructible<key_equal>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001439 : __bucket_list_(_VSTD::move(__u.__bucket_list_)),
1440 __p1_(_VSTD::move(__u.__p1_)),
1441 __p2_(_VSTD::move(__u.__p2_)),
1442 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001443{
1444 if (size() > 0)
1445 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001446 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001447 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001448 __u.__p1_.first().__next_ = nullptr;
1449 __u.size() = 0;
1450 }
1451}
1452
1453template <class _Tp, class _Hash, class _Equal, class _Alloc>
1454__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u,
1455 const allocator_type& __a)
1456 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
1457 __p1_(__node_allocator(__a)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001458 __p2_(0, _VSTD::move(__u.hash_function())),
1459 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001460{
1461 if (__a == allocator_type(__u.__node_alloc()))
1462 {
1463 __bucket_list_.reset(__u.__bucket_list_.release());
1464 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1465 __u.__bucket_list_.get_deleter().size() = 0;
1466 if (__u.size() > 0)
1467 {
1468 __p1_.first().__next_ = __u.__p1_.first().__next_;
1469 __u.__p1_.first().__next_ = nullptr;
Howard Hinnant7a445152012-07-06 17:31:14 +00001470 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001471 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001472 size() = __u.size();
1473 __u.size() = 0;
1474 }
1475 }
1476}
1477
Eric Fiselier2960ae22016-02-11 11:59:44 +00001478#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001479
1480template <class _Tp, class _Hash, class _Equal, class _Alloc>
1481__hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table()
1482{
1483 __deallocate(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001484#if _LIBCPP_DEBUG_LEVEL >= 2
1485 __get_db()->__erase_c(this);
1486#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001487}
1488
1489template <class _Tp, class _Hash, class _Equal, class _Alloc>
1490void
1491__hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(
1492 const __hash_table& __u, true_type)
1493{
1494 if (__node_alloc() != __u.__node_alloc())
1495 {
1496 clear();
1497 __bucket_list_.reset();
1498 __bucket_list_.get_deleter().size() = 0;
1499 }
1500 __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
1501 __node_alloc() = __u.__node_alloc();
1502}
1503
1504template <class _Tp, class _Hash, class _Equal, class _Alloc>
1505__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1506__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u)
1507{
1508 if (this != &__u)
1509 {
1510 __copy_assign_alloc(__u);
1511 hash_function() = __u.hash_function();
1512 key_eq() = __u.key_eq();
1513 max_load_factor() = __u.max_load_factor();
1514 __assign_multi(__u.begin(), __u.end());
1515 }
1516 return *this;
1517}
1518
1519template <class _Tp, class _Hash, class _Equal, class _Alloc>
1520void
1521__hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate(__node_pointer __np)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001522 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001523{
1524 __node_allocator& __na = __node_alloc();
1525 while (__np != nullptr)
1526 {
1527 __node_pointer __next = __np->__next_;
Howard Hinnant39213642013-07-23 22:01:58 +00001528#if _LIBCPP_DEBUG_LEVEL >= 2
1529 __c_node* __c = __get_db()->__find_c_and_lock(this);
1530 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1531 {
1532 --__p;
1533 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1534 if (__i->__node_ == __np)
1535 {
1536 (*__p)->__c_ = nullptr;
1537 if (--__c->end_ != __p)
1538 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1539 }
1540 }
1541 __get_db()->unlock();
1542#endif
Eric Fiselier2960ae22016-02-11 11:59:44 +00001543 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__np->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001544 __node_traits::deallocate(__na, __np, 1);
1545 __np = __next;
1546 }
1547}
1548
1549template <class _Tp, class _Hash, class _Equal, class _Alloc>
1550typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_pointer
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001551__hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001552{
1553 size_type __bc = bucket_count();
1554 for (size_type __i = 0; __i < __bc; ++__i)
1555 __bucket_list_[__i] = nullptr;
1556 size() = 0;
1557 __node_pointer __cache = __p1_.first().__next_;
1558 __p1_.first().__next_ = nullptr;
1559 return __cache;
1560}
1561
Eric Fiselier2960ae22016-02-11 11:59:44 +00001562#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001563
1564template <class _Tp, class _Hash, class _Equal, class _Alloc>
1565void
1566__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1567 __hash_table& __u, true_type)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001568 _NOEXCEPT_(
1569 is_nothrow_move_assignable<__node_allocator>::value &&
1570 is_nothrow_move_assignable<hasher>::value &&
1571 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001572{
1573 clear();
1574 __bucket_list_.reset(__u.__bucket_list_.release());
1575 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1576 __u.__bucket_list_.get_deleter().size() = 0;
1577 __move_assign_alloc(__u);
1578 size() = __u.size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001579 hash_function() = _VSTD::move(__u.hash_function());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001580 max_load_factor() = __u.max_load_factor();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001581 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001582 __p1_.first().__next_ = __u.__p1_.first().__next_;
1583 if (size() > 0)
1584 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001585 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001586 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001587 __u.__p1_.first().__next_ = nullptr;
1588 __u.size() = 0;
1589 }
Howard Hinnant39213642013-07-23 22:01:58 +00001590#if _LIBCPP_DEBUG_LEVEL >= 2
1591 __get_db()->swap(this, &__u);
1592#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001593}
1594
1595template <class _Tp, class _Hash, class _Equal, class _Alloc>
1596void
1597__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1598 __hash_table& __u, false_type)
1599{
1600 if (__node_alloc() == __u.__node_alloc())
1601 __move_assign(__u, true_type());
1602 else
1603 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001604 hash_function() = _VSTD::move(__u.hash_function());
1605 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001606 max_load_factor() = __u.max_load_factor();
1607 if (bucket_count() != 0)
1608 {
1609 __node_pointer __cache = __detach();
1610#ifndef _LIBCPP_NO_EXCEPTIONS
1611 try
1612 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001613#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001614 const_iterator __i = __u.begin();
1615 while (__cache != nullptr && __u.size() != 0)
1616 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001617 __cache->__value_ = _VSTD::move(__u.remove(__i++)->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001618 __node_pointer __next = __cache->__next_;
1619 __node_insert_multi(__cache);
1620 __cache = __next;
1621 }
1622#ifndef _LIBCPP_NO_EXCEPTIONS
1623 }
1624 catch (...)
1625 {
1626 __deallocate(__cache);
1627 throw;
1628 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001629#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001630 __deallocate(__cache);
1631 }
1632 const_iterator __i = __u.begin();
1633 while (__u.size() != 0)
1634 {
Eric Fiselier2960ae22016-02-11 11:59:44 +00001635 __node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001636 __node_insert_multi(__h.get());
1637 __h.release();
1638 }
1639 }
1640}
1641
1642template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001643inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001644__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1645__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001646 _NOEXCEPT_(
1647 __node_traits::propagate_on_container_move_assignment::value &&
1648 is_nothrow_move_assignable<__node_allocator>::value &&
1649 is_nothrow_move_assignable<hasher>::value &&
1650 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001651{
1652 __move_assign(__u, integral_constant<bool,
1653 __node_traits::propagate_on_container_move_assignment::value>());
1654 return *this;
1655}
1656
Eric Fiselier2960ae22016-02-11 11:59:44 +00001657#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001658
1659template <class _Tp, class _Hash, class _Equal, class _Alloc>
1660template <class _InputIterator>
1661void
1662__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first,
1663 _InputIterator __last)
1664{
Eric Fiselier2960ae22016-02-11 11:59:44 +00001665 typedef iterator_traits<_InputIterator> _ITraits;
1666 typedef typename _ITraits::value_type _ItValueType;
1667 static_assert((is_same<_ItValueType, __container_value_type>::value),
1668 "__assign_unique may only be called with the containers value type");
1669
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001670 if (bucket_count() != 0)
1671 {
1672 __node_pointer __cache = __detach();
1673#ifndef _LIBCPP_NO_EXCEPTIONS
1674 try
1675 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001676#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001677 for (; __cache != nullptr && __first != __last; ++__first)
1678 {
1679 __cache->__value_ = *__first;
1680 __node_pointer __next = __cache->__next_;
1681 __node_insert_unique(__cache);
1682 __cache = __next;
1683 }
1684#ifndef _LIBCPP_NO_EXCEPTIONS
1685 }
1686 catch (...)
1687 {
1688 __deallocate(__cache);
1689 throw;
1690 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001691#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001692 __deallocate(__cache);
1693 }
1694 for (; __first != __last; ++__first)
1695 __insert_unique(*__first);
1696}
1697
1698template <class _Tp, class _Hash, class _Equal, class _Alloc>
1699template <class _InputIterator>
1700void
1701__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
1702 _InputIterator __last)
1703{
Eric Fiselier2960ae22016-02-11 11:59:44 +00001704 typedef iterator_traits<_InputIterator> _ITraits;
1705 typedef typename _ITraits::value_type _ItValueType;
1706 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1707 is_same<_ItValueType, __node_value_type>::value),
1708 "__assign_multi may only be called with the containers value type"
1709 " or the nodes value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001710 if (bucket_count() != 0)
1711 {
1712 __node_pointer __cache = __detach();
1713#ifndef _LIBCPP_NO_EXCEPTIONS
1714 try
1715 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001716#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001717 for (; __cache != nullptr && __first != __last; ++__first)
1718 {
1719 __cache->__value_ = *__first;
1720 __node_pointer __next = __cache->__next_;
1721 __node_insert_multi(__cache);
1722 __cache = __next;
1723 }
1724#ifndef _LIBCPP_NO_EXCEPTIONS
1725 }
1726 catch (...)
1727 {
1728 __deallocate(__cache);
1729 throw;
1730 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001731#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001732 __deallocate(__cache);
1733 }
1734 for (; __first != __last; ++__first)
Eric Fiselier2960ae22016-02-11 11:59:44 +00001735 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001736}
1737
1738template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001739inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001740typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001741__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001742{
Howard Hinnant39213642013-07-23 22:01:58 +00001743#if _LIBCPP_DEBUG_LEVEL >= 2
1744 return iterator(__p1_.first().__next_, this);
1745#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001746 return iterator(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001747#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001748}
1749
1750template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001751inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001752typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001753__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001754{
Howard Hinnant39213642013-07-23 22:01:58 +00001755#if _LIBCPP_DEBUG_LEVEL >= 2
1756 return iterator(nullptr, this);
1757#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001758 return iterator(nullptr);
Howard Hinnant39213642013-07-23 22:01:58 +00001759#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001760}
1761
1762template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001763inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001764typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001765__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001766{
Howard Hinnant39213642013-07-23 22:01:58 +00001767#if _LIBCPP_DEBUG_LEVEL >= 2
1768 return const_iterator(__p1_.first().__next_, this);
1769#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001770 return const_iterator(__p1_.first().__next_);
Howard Hinnant39213642013-07-23 22:01:58 +00001771#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001772}
1773
1774template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00001775inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001776typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001777__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001778{
Howard Hinnant39213642013-07-23 22:01:58 +00001779#if _LIBCPP_DEBUG_LEVEL >= 2
1780 return const_iterator(nullptr, this);
1781#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001782 return const_iterator(nullptr);
Howard Hinnant39213642013-07-23 22:01:58 +00001783#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001784}
1785
1786template <class _Tp, class _Hash, class _Equal, class _Alloc>
1787void
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00001788__hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001789{
1790 if (size() > 0)
1791 {
1792 __deallocate(__p1_.first().__next_);
1793 __p1_.first().__next_ = nullptr;
1794 size_type __bc = bucket_count();
Howard Hinnant9f66bff2011-07-05 14:14:17 +00001795 for (size_type __i = 0; __i < __bc; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001796 __bucket_list_[__i] = nullptr;
1797 size() = 0;
1798 }
1799}
1800
1801template <class _Tp, class _Hash, class _Equal, class _Alloc>
1802pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1803__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd)
1804{
1805 __nd->__hash_ = hash_function()(__nd->__value_);
1806 size_type __bc = bucket_count();
1807 bool __inserted = false;
1808 __node_pointer __ndptr;
1809 size_t __chash;
1810 if (__bc != 0)
1811 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001812 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001813 __ndptr = __bucket_list_[__chash];
1814 if (__ndptr != nullptr)
1815 {
1816 for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00001817 __constrain_hash(__ndptr->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001818 __ndptr = __ndptr->__next_)
1819 {
1820 if (key_eq()(__ndptr->__value_, __nd->__value_))
1821 goto __done;
1822 }
1823 }
1824 }
1825 {
1826 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1827 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001828 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001829 size_type(ceil(float(size() + 1) / max_load_factor()))));
1830 __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00001831 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001832 }
1833 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
1834 __node_pointer __pn = __bucket_list_[__chash];
1835 if (__pn == nullptr)
1836 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001837 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001838 __nd->__next_ = __pn->__next_;
1839 __pn->__next_ = __nd;
1840 // fix up __bucket_list_
1841 __bucket_list_[__chash] = __pn;
1842 if (__nd->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:14 +00001843 __bucket_list_[__constrain_hash(__nd->__next_->__hash_, __bc)] = __nd;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001844 }
1845 else
1846 {
1847 __nd->__next_ = __pn->__next_;
1848 __pn->__next_ = __nd;
1849 }
1850 __ndptr = __nd;
1851 // increment size
1852 ++size();
1853 __inserted = true;
1854 }
1855__done:
Howard Hinnant39213642013-07-23 22:01:58 +00001856#if _LIBCPP_DEBUG_LEVEL >= 2
1857 return pair<iterator, bool>(iterator(__ndptr, this), __inserted);
1858#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001859 return pair<iterator, bool>(iterator(__ndptr), __inserted);
Howard Hinnant39213642013-07-23 22:01:58 +00001860#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001861}
1862
1863template <class _Tp, class _Hash, class _Equal, class _Alloc>
1864typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1865__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp)
1866{
1867 __cp->__hash_ = hash_function()(__cp->__value_);
1868 size_type __bc = bucket_count();
1869 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1870 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001871 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001872 size_type(ceil(float(size() + 1) / max_load_factor()))));
1873 __bc = bucket_count();
1874 }
Howard Hinnant7a445152012-07-06 17:31:14 +00001875 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001876 __node_pointer __pn = __bucket_list_[__chash];
1877 if (__pn == nullptr)
1878 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001879 __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001880 __cp->__next_ = __pn->__next_;
1881 __pn->__next_ = __cp;
1882 // fix up __bucket_list_
1883 __bucket_list_[__chash] = __pn;
1884 if (__cp->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:14 +00001885 __bucket_list_[__constrain_hash(__cp->__next_->__hash_, __bc)] = __cp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001886 }
1887 else
1888 {
1889 for (bool __found = false; __pn->__next_ != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00001890 __constrain_hash(__pn->__next_->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001891 __pn = __pn->__next_)
Howard Hinnant324bb032010-08-22 00:02:43 +00001892 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001893 // __found key_eq() action
1894 // false false loop
1895 // true true loop
1896 // false true set __found to true
1897 // true false break
1898 if (__found != (__pn->__next_->__hash_ == __cp->__hash_ &&
1899 key_eq()(__pn->__next_->__value_, __cp->__value_)))
1900 {
1901 if (!__found)
1902 __found = true;
1903 else
1904 break;
1905 }
1906 }
1907 __cp->__next_ = __pn->__next_;
1908 __pn->__next_ = __cp;
1909 if (__cp->__next_ != nullptr)
1910 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001911 size_t __nhash = __constrain_hash(__cp->__next_->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001912 if (__nhash != __chash)
1913 __bucket_list_[__nhash] = __cp;
1914 }
1915 }
1916 ++size();
Howard Hinnant39213642013-07-23 22:01:58 +00001917#if _LIBCPP_DEBUG_LEVEL >= 2
1918 return iterator(__cp, this);
1919#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001920 return iterator(__cp);
Howard Hinnant39213642013-07-23 22:01:58 +00001921#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001922}
1923
1924template <class _Tp, class _Hash, class _Equal, class _Alloc>
1925typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1926__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(
1927 const_iterator __p, __node_pointer __cp)
1928{
Howard Hinnant824c1992013-08-02 17:50:49 +00001929#if _LIBCPP_DEBUG_LEVEL >= 2
1930 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1931 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
1932 " referring to this unordered container");
1933#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001934 if (__p != end() && key_eq()(*__p, __cp->__value_))
1935 {
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00001936 __node_pointer __np = __p.__node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001937 __cp->__hash_ = __np->__hash_;
1938 size_type __bc = bucket_count();
1939 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1940 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00001941 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001942 size_type(ceil(float(size() + 1) / max_load_factor()))));
1943 __bc = bucket_count();
1944 }
Howard Hinnant7a445152012-07-06 17:31:14 +00001945 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001946 __node_pointer __pp = __bucket_list_[__chash];
1947 while (__pp->__next_ != __np)
1948 __pp = __pp->__next_;
1949 __cp->__next_ = __np;
1950 __pp->__next_ = __cp;
1951 ++size();
Howard Hinnant39213642013-07-23 22:01:58 +00001952#if _LIBCPP_DEBUG_LEVEL >= 2
1953 return iterator(__cp, this);
1954#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001955 return iterator(__cp);
Howard Hinnant39213642013-07-23 22:01:58 +00001956#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001957 }
1958 return __node_insert_multi(__cp);
1959}
1960
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001961
1962
Eric Fiselier2960ae22016-02-11 11:59:44 +00001963#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001964template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001965template <class _Key, class ..._Args>
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001966_LIBCPP_INLINE_VISIBILITY
1967pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001968__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001969#else
1970template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001971template <class _Key, class _Args>
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001972_LIBCPP_INLINE_VISIBILITY
1973pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselier2960ae22016-02-11 11:59:44 +00001974__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
Eric Fiselierfdae69a2015-06-13 07:18:32 +00001975#endif
1976{
Eric Fiselier2960ae22016-02-11 11:59:44 +00001977
1978 size_t __hash = hash_function()(__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001979 size_type __bc = bucket_count();
1980 bool __inserted = false;
1981 __node_pointer __nd;
1982 size_t __chash;
1983 if (__bc != 0)
1984 {
Howard Hinnant7a445152012-07-06 17:31:14 +00001985 __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001986 __nd = __bucket_list_[__chash];
1987 if (__nd != nullptr)
1988 {
1989 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00001990 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001991 __nd = __nd->__next_)
1992 {
Eric Fiselier2960ae22016-02-11 11:59:44 +00001993 if (key_eq()(__nd->__value_, __k))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001994 goto __done;
1995 }
1996 }
1997 }
1998 {
Eric Fiselier2960ae22016-02-11 11:59:44 +00001999#ifndef _LIBCPP_CXX03_LANG
2000 __node_holder __h = __construct_node_hash(__hash, _VSTD::forward<_Args>(__args)...);
2001#else
2002 __node_holder __h = __construct_node_hash(__hash, __args);
2003#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002004 if (size()+1 > __bc * max_load_factor() || __bc == 0)
2005 {
Eric Fiselier57947ca2015-02-02 21:31:48 +00002006 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002007 size_type(ceil(float(size() + 1) / max_load_factor()))));
2008 __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00002009 __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002010 }
2011 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
2012 __node_pointer __pn = __bucket_list_[__chash];
2013 if (__pn == nullptr)
2014 {
Eric Fiselier9e9f42e2016-02-11 15:22:37 +00002015 __pn = static_cast<__node_pointer>(static_cast<__void_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002016 __h->__next_ = __pn->__next_;
2017 __pn->__next_ = __h.get();
2018 // fix up __bucket_list_
2019 __bucket_list_[__chash] = __pn;
2020 if (__h->__next_ != nullptr)
Howard Hinnant7a445152012-07-06 17:31:14 +00002021 __bucket_list_[__constrain_hash(__h->__next_->__hash_, __bc)] = __h.get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002022 }
2023 else
2024 {
2025 __h->__next_ = __pn->__next_;
2026 __pn->__next_ = __h.get();
2027 }
2028 __nd = __h.release();
2029 // increment size
2030 ++size();
2031 __inserted = true;
2032 }
2033__done:
Howard Hinnant39213642013-07-23 22:01:58 +00002034#if _LIBCPP_DEBUG_LEVEL >= 2
2035 return pair<iterator, bool>(iterator(__nd, this), __inserted);
2036#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002037 return pair<iterator, bool>(iterator(__nd), __inserted);
Howard Hinnant39213642013-07-23 22:01:58 +00002038#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002039}
2040
Eric Fiselier2960ae22016-02-11 11:59:44 +00002041#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002042
2043template <class _Tp, class _Hash, class _Equal, class _Alloc>
2044template <class... _Args>
2045pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Duncan P. N. Exon Smith798ec842016-03-17 20:45:20 +00002046__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_impl(_Args&&... __args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002047{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002048 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002049 pair<iterator, bool> __r = __node_insert_unique(__h.get());
2050 if (__r.second)
2051 __h.release();
2052 return __r;
2053}
2054
2055template <class _Tp, class _Hash, class _Equal, class _Alloc>
2056template <class... _Args>
2057typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2058__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args)
2059{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002060 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002061 iterator __r = __node_insert_multi(__h.get());
2062 __h.release();
2063 return __r;
2064}
2065
2066template <class _Tp, class _Hash, class _Equal, class _Alloc>
2067template <class... _Args>
2068typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2069__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(
2070 const_iterator __p, _Args&&... __args)
2071{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00002072#if _LIBCPP_DEBUG_LEVEL >= 2
2073 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2074 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
2075 " referring to this unordered container");
2076#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00002077 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002078 iterator __r = __node_insert_multi(__p, __h.get());
2079 __h.release();
2080 return __r;
2081}
2082
Eric Fiselier2960ae22016-02-11 11:59:44 +00002083#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002084
2085template <class _Tp, class _Hash, class _Equal, class _Alloc>
2086typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Eric Fiselier2960ae22016-02-11 11:59:44 +00002087__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const __container_value_type& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002088{
2089 __node_holder __h = __construct_node(__x);
2090 iterator __r = __node_insert_multi(__h.get());
2091 __h.release();
2092 return __r;
2093}
2094
2095template <class _Tp, class _Hash, class _Equal, class _Alloc>
2096typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2097__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
Eric Fiselier2960ae22016-02-11 11:59:44 +00002098 const __container_value_type& __x)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002099{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00002100#if _LIBCPP_DEBUG_LEVEL >= 2
2101 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2102 "unordered container::insert(const_iterator, lvalue) called with an iterator not"
2103 " referring to this unordered container");
2104#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002105 __node_holder __h = __construct_node(__x);
2106 iterator __r = __node_insert_multi(__p, __h.get());
2107 __h.release();
2108 return __r;
2109}
2110
Eric Fiselier2960ae22016-02-11 11:59:44 +00002111#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002112
2113template <class _Tp, class _Hash, class _Equal, class _Alloc>
2114void
2115__hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
2116{
Howard Hinnant7a445152012-07-06 17:31:14 +00002117 if (__n == 1)
2118 __n = 2;
2119 else if (__n & (__n - 1))
2120 __n = __next_prime(__n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002121 size_type __bc = bucket_count();
2122 if (__n > __bc)
2123 __rehash(__n);
Howard Hinnant7a445152012-07-06 17:31:14 +00002124 else if (__n < __bc)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002125 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002126 __n = _VSTD::max<size_type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002127 (
2128 __n,
Eric Fiselier57947ca2015-02-02 21:31:48 +00002129 __is_hash_power2(__bc) ? __next_hash_pow2(size_t(ceil(float(size()) / max_load_factor()))) :
2130 __next_prime(size_t(ceil(float(size()) / max_load_factor())))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002131 );
2132 if (__n < __bc)
2133 __rehash(__n);
2134 }
2135}
2136
2137template <class _Tp, class _Hash, class _Equal, class _Alloc>
2138void
2139__hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc)
2140{
Howard Hinnant39213642013-07-23 22:01:58 +00002141#if _LIBCPP_DEBUG_LEVEL >= 2
2142 __get_db()->__invalidate_all(this);
2143#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002144 __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
2145 __bucket_list_.reset(__nbc > 0 ?
2146 __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
2147 __bucket_list_.get_deleter().size() = __nbc;
2148 if (__nbc > 0)
2149 {
2150 for (size_type __i = 0; __i < __nbc; ++__i)
2151 __bucket_list_[__i] = nullptr;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002152 __node_pointer __pp(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002153 __node_pointer __cp = __pp->__next_;
2154 if (__cp != nullptr)
2155 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002156 size_type __chash = __constrain_hash(__cp->__hash_, __nbc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002157 __bucket_list_[__chash] = __pp;
2158 size_type __phash = __chash;
2159 for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr;
2160 __cp = __pp->__next_)
2161 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002162 __chash = __constrain_hash(__cp->__hash_, __nbc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002163 if (__chash == __phash)
2164 __pp = __cp;
2165 else
2166 {
2167 if (__bucket_list_[__chash] == nullptr)
2168 {
2169 __bucket_list_[__chash] = __pp;
2170 __pp = __cp;
2171 __phash = __chash;
2172 }
2173 else
2174 {
2175 __node_pointer __np = __cp;
2176 for (; __np->__next_ != nullptr &&
2177 key_eq()(__cp->__value_, __np->__next_->__value_);
2178 __np = __np->__next_)
2179 ;
2180 __pp->__next_ = __np->__next_;
2181 __np->__next_ = __bucket_list_[__chash]->__next_;
2182 __bucket_list_[__chash]->__next_ = __cp;
Howard Hinnant324bb032010-08-22 00:02:43 +00002183
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002184 }
2185 }
2186 }
2187 }
2188 }
2189}
2190
2191template <class _Tp, class _Hash, class _Equal, class _Alloc>
2192template <class _Key>
2193typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2194__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
2195{
2196 size_t __hash = hash_function()(__k);
2197 size_type __bc = bucket_count();
2198 if (__bc != 0)
2199 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002200 size_t __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002201 __node_pointer __nd = __bucket_list_[__chash];
2202 if (__nd != nullptr)
2203 {
2204 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00002205 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002206 __nd = __nd->__next_)
2207 {
2208 if (key_eq()(__nd->__value_, __k))
Howard Hinnant39213642013-07-23 22:01:58 +00002209#if _LIBCPP_DEBUG_LEVEL >= 2
2210 return iterator(__nd, this);
2211#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002212 return iterator(__nd);
Howard Hinnant39213642013-07-23 22:01:58 +00002213#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002214 }
2215 }
2216 }
2217 return end();
2218}
2219
2220template <class _Tp, class _Hash, class _Equal, class _Alloc>
2221template <class _Key>
2222typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
2223__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
2224{
2225 size_t __hash = hash_function()(__k);
2226 size_type __bc = bucket_count();
2227 if (__bc != 0)
2228 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002229 size_t __chash = __constrain_hash(__hash, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002230 __node_const_pointer __nd = __bucket_list_[__chash];
2231 if (__nd != nullptr)
2232 {
2233 for (__nd = __nd->__next_; __nd != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00002234 __constrain_hash(__nd->__hash_, __bc) == __chash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002235 __nd = __nd->__next_)
2236 {
2237 if (key_eq()(__nd->__value_, __k))
Howard Hinnant39213642013-07-23 22:01:58 +00002238#if _LIBCPP_DEBUG_LEVEL >= 2
2239 return const_iterator(__nd, this);
2240#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002241 return const_iterator(__nd);
Howard Hinnant39213642013-07-23 22:01:58 +00002242#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002243 }
2244 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002245
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002246 }
2247 return end();
2248}
2249
Eric Fiselier2960ae22016-02-11 11:59:44 +00002250#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002251
2252template <class _Tp, class _Hash, class _Equal, class _Alloc>
2253template <class ..._Args>
2254typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2255__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args)
2256{
Eric Fiselier2960ae22016-02-11 11:59:44 +00002257 static_assert(!__is_hash_value_type<_Args...>::value,
2258 "Construct cannot be called with a hash value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002259 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002260 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002261 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002262 __h.get_deleter().__value_constructed = true;
2263 __h->__hash_ = hash_function()(__h->__value_);
2264 __h->__next_ = nullptr;
2265 return __h;
2266}
2267
2268template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier2960ae22016-02-11 11:59:44 +00002269template <class _First, class ..._Rest>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002270typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselier2960ae22016-02-11 11:59:44 +00002271__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(
2272 size_t __hash, _First&& __f, _Rest&& ...__rest)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002273{
Eric Fiselier2960ae22016-02-11 11:59:44 +00002274 static_assert(!__is_hash_value_type<_First, _Rest...>::value,
2275 "Construct cannot be called with a hash value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002276 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002277 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002278 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_),
2279 _VSTD::forward<_First>(__f),
2280 _VSTD::forward<_Rest>(__rest)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002281 __h.get_deleter().__value_constructed = true;
2282 __h->__hash_ = __hash;
2283 __h->__next_ = nullptr;
Howard Hinnant9a894d92013-08-22 18:29:50 +00002284 return __h;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002285}
2286
Eric Fiselier2960ae22016-02-11 11:59:44 +00002287#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002288
2289template <class _Tp, class _Hash, class _Equal, class _Alloc>
2290typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselier2960ae22016-02-11 11:59:44 +00002291__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002292{
2293 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002294 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002295 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002296 __h.get_deleter().__value_constructed = true;
2297 __h->__hash_ = hash_function()(__h->__value_);
2298 __h->__next_ = nullptr;
Dimitry Andric89663502015-08-19 06:43:33 +00002299 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002300}
2301
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002302template <class _Tp, class _Hash, class _Equal, class _Alloc>
2303typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselier2960ae22016-02-11 11:59:44 +00002304__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(size_t __hash,
2305 const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002306{
2307 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002308 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselier2960ae22016-02-11 11:59:44 +00002309 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002310 __h.get_deleter().__value_constructed = true;
2311 __h->__hash_ = __hash;
2312 __h->__next_ = nullptr;
Dimitry Andric89663502015-08-19 06:43:33 +00002313 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002314}
2315
Eric Fiselier2960ae22016-02-11 11:59:44 +00002316#endif // _LIBCPP_CXX03_LANG
2317
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002318template <class _Tp, class _Hash, class _Equal, class _Alloc>
2319typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2320__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p)
2321{
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002322 __node_pointer __np = __p.__node_;
Howard Hinnant39213642013-07-23 22:01:58 +00002323#if _LIBCPP_DEBUG_LEVEL >= 2
2324 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2325 "unordered container erase(iterator) called with an iterator not"
2326 " referring to this container");
2327 _LIBCPP_ASSERT(__p != end(),
2328 "unordered container erase(iterator) called with a non-dereferenceable iterator");
2329 iterator __r(__np, this);
2330#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002331 iterator __r(__np);
Howard Hinnant39213642013-07-23 22:01:58 +00002332#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002333 ++__r;
2334 remove(__p);
2335 return __r;
2336}
2337
2338template <class _Tp, class _Hash, class _Equal, class _Alloc>
2339typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2340__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first,
2341 const_iterator __last)
2342{
Howard Hinnant39213642013-07-23 22:01:58 +00002343#if _LIBCPP_DEBUG_LEVEL >= 2
2344 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2345 "unodered container::erase(iterator, iterator) called with an iterator not"
2346 " referring to this unodered container");
2347 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
2348 "unodered container::erase(iterator, iterator) called with an iterator not"
2349 " referring to this unodered container");
2350#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002351 for (const_iterator __p = __first; __first != __last; __p = __first)
2352 {
2353 ++__first;
2354 erase(__p);
2355 }
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002356 __node_pointer __np = __last.__node_;
Howard Hinnant39213642013-07-23 22:01:58 +00002357#if _LIBCPP_DEBUG_LEVEL >= 2
2358 return iterator (__np, this);
2359#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002360 return iterator (__np);
Howard Hinnant39213642013-07-23 22:01:58 +00002361#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002362}
2363
2364template <class _Tp, class _Hash, class _Equal, class _Alloc>
2365template <class _Key>
2366typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2367__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k)
2368{
2369 iterator __i = find(__k);
2370 if (__i == end())
2371 return 0;
2372 erase(__i);
2373 return 1;
2374}
2375
2376template <class _Tp, class _Hash, class _Equal, class _Alloc>
2377template <class _Key>
2378typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2379__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k)
2380{
2381 size_type __r = 0;
2382 iterator __i = find(__k);
2383 if (__i != end())
2384 {
2385 iterator __e = end();
2386 do
2387 {
2388 erase(__i++);
2389 ++__r;
2390 } while (__i != __e && key_eq()(*__i, __k));
2391 }
2392 return __r;
2393}
2394
2395template <class _Tp, class _Hash, class _Equal, class _Alloc>
2396typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002397__hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002398{
2399 // current node
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002400 __node_pointer __cn = __p.__node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002401 size_type __bc = bucket_count();
Howard Hinnant7a445152012-07-06 17:31:14 +00002402 size_t __chash = __constrain_hash(__cn->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002403 // find previous node
2404 __node_pointer __pn = __bucket_list_[__chash];
2405 for (; __pn->__next_ != __cn; __pn = __pn->__next_)
2406 ;
2407 // Fix up __bucket_list_
2408 // if __pn is not in same bucket (before begin is not in same bucket) &&
2409 // if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002410 if (__pn == static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()))
2411 || __constrain_hash(__pn->__hash_, __bc) != __chash)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002412 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002413 if (__cn->__next_ == nullptr || __constrain_hash(__cn->__next_->__hash_, __bc) != __chash)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002414 __bucket_list_[__chash] = nullptr;
2415 }
2416 // if __cn->__next_ is not in same bucket (nullptr is in same bucket)
2417 if (__cn->__next_ != nullptr)
2418 {
Howard Hinnant7a445152012-07-06 17:31:14 +00002419 size_t __nhash = __constrain_hash(__cn->__next_->__hash_, __bc);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002420 if (__nhash != __chash)
2421 __bucket_list_[__nhash] = __pn;
2422 }
2423 // remove __cn
2424 __pn->__next_ = __cn->__next_;
2425 __cn->__next_ = nullptr;
2426 --size();
Howard Hinnant39213642013-07-23 22:01:58 +00002427#if _LIBCPP_DEBUG_LEVEL >= 2
2428 __c_node* __c = __get_db()->__find_c_and_lock(this);
2429 for (__i_node** __p = __c->end_; __p != __c->beg_; )
2430 {
2431 --__p;
2432 iterator* __i = static_cast<iterator*>((*__p)->__i_);
2433 if (__i->__node_ == __cn)
2434 {
2435 (*__p)->__c_ = nullptr;
2436 if (--__c->end_ != __p)
2437 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
2438 }
2439 }
2440 __get_db()->unlock();
2441#endif
Howard Hinnant99968442011-11-29 18:15:50 +00002442 return __node_holder(__cn, _Dp(__node_alloc(), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002443}
2444
2445template <class _Tp, class _Hash, class _Equal, class _Alloc>
2446template <class _Key>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00002447inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002448typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2449__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const
2450{
2451 return static_cast<size_type>(find(__k) != end());
2452}
2453
2454template <class _Tp, class _Hash, class _Equal, class _Alloc>
2455template <class _Key>
2456typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2457__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const
2458{
2459 size_type __r = 0;
2460 const_iterator __i = find(__k);
2461 if (__i != end())
2462 {
2463 const_iterator __e = end();
2464 do
2465 {
2466 ++__i;
2467 ++__r;
2468 } while (__i != __e && key_eq()(*__i, __k));
2469 }
2470 return __r;
2471}
2472
2473template <class _Tp, class _Hash, class _Equal, class _Alloc>
2474template <class _Key>
2475pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2476 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2477__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2478 const _Key& __k)
2479{
2480 iterator __i = find(__k);
2481 iterator __j = __i;
2482 if (__i != end())
2483 ++__j;
2484 return pair<iterator, iterator>(__i, __j);
2485}
2486
2487template <class _Tp, class _Hash, class _Equal, class _Alloc>
2488template <class _Key>
2489pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2490 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2491__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2492 const _Key& __k) const
2493{
2494 const_iterator __i = find(__k);
2495 const_iterator __j = __i;
2496 if (__i != end())
2497 ++__j;
2498 return pair<const_iterator, const_iterator>(__i, __j);
2499}
2500
2501template <class _Tp, class _Hash, class _Equal, class _Alloc>
2502template <class _Key>
2503pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2504 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2505__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2506 const _Key& __k)
2507{
2508 iterator __i = find(__k);
2509 iterator __j = __i;
2510 if (__i != end())
2511 {
2512 iterator __e = end();
2513 do
2514 {
2515 ++__j;
2516 } while (__j != __e && key_eq()(*__j, __k));
2517 }
2518 return pair<iterator, iterator>(__i, __j);
2519}
2520
2521template <class _Tp, class _Hash, class _Equal, class _Alloc>
2522template <class _Key>
2523pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2524 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2525__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2526 const _Key& __k) const
2527{
2528 const_iterator __i = find(__k);
2529 const_iterator __j = __i;
2530 if (__i != end())
2531 {
2532 const_iterator __e = end();
2533 do
2534 {
2535 ++__j;
2536 } while (__j != __e && key_eq()(*__j, __k));
2537 }
2538 return pair<const_iterator, const_iterator>(__i, __j);
2539}
2540
2541template <class _Tp, class _Hash, class _Equal, class _Alloc>
2542void
2543__hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
Eric Fiselier692177d2015-07-18 20:40:46 +00002544#if _LIBCPP_STD_VER <= 11
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002545 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:56 +00002546 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clow7d914d12015-07-13 20:04:56 +00002547 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
2548 || __is_nothrow_swappable<__pointer_allocator>::value)
2549 && (!__node_traits::propagate_on_container_swap::value
2550 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +00002551 )
Eric Fiselier692177d2015-07-18 20:40:46 +00002552#else
2553 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value)
2554#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002555{
2556 {
2557 __node_pointer_pointer __npp = __bucket_list_.release();
2558 __bucket_list_.reset(__u.__bucket_list_.release());
2559 __u.__bucket_list_.reset(__npp);
2560 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00002561 _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
Marshall Clow7d914d12015-07-13 20:04:56 +00002562 __swap_allocator(__bucket_list_.get_deleter().__alloc(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002563 __u.__bucket_list_.get_deleter().__alloc());
Marshall Clow7d914d12015-07-13 20:04:56 +00002564 __swap_allocator(__node_alloc(), __u.__node_alloc());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002565 _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002566 __p2_.swap(__u.__p2_);
2567 __p3_.swap(__u.__p3_);
2568 if (size() > 0)
Howard Hinnant7a445152012-07-06 17:31:14 +00002569 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002570 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002571 if (__u.size() > 0)
Howard Hinnant7a445152012-07-06 17:31:14 +00002572 __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash_, __u.bucket_count())] =
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +00002573 static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__u.__p1_.first()));
Howard Hinnant39213642013-07-23 22:01:58 +00002574#if _LIBCPP_DEBUG_LEVEL >= 2
2575 __get_db()->swap(this, &__u);
2576#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002577}
2578
2579template <class _Tp, class _Hash, class _Equal, class _Alloc>
2580typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2581__hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const
2582{
Howard Hinnant0bb0a7c2013-07-29 19:05:47 +00002583 _LIBCPP_ASSERT(__n < bucket_count(),
2584 "unordered container::bucket_size(n) called with n >= bucket_count()");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002585 __node_const_pointer __np = __bucket_list_[__n];
2586 size_type __bc = bucket_count();
2587 size_type __r = 0;
2588 if (__np != nullptr)
2589 {
2590 for (__np = __np->__next_; __np != nullptr &&
Howard Hinnant7a445152012-07-06 17:31:14 +00002591 __constrain_hash(__np->__hash_, __bc) == __n;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002592 __np = __np->__next_, ++__r)
2593 ;
2594 }
2595 return __r;
2596}
2597
Howard Hinnant5f2f14c2011-06-04 18:54:24 +00002598template <class _Tp, class _Hash, class _Equal, class _Alloc>
2599inline _LIBCPP_INLINE_VISIBILITY
2600void
2601swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x,
2602 __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
2603 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2604{
2605 __x.swap(__y);
2606}
2607
Howard Hinnant39213642013-07-23 22:01:58 +00002608#if _LIBCPP_DEBUG_LEVEL >= 2
2609
2610template <class _Tp, class _Hash, class _Equal, class _Alloc>
2611bool
2612__hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const
2613{
2614 return __i->__node_ != nullptr;
2615}
2616
2617template <class _Tp, class _Hash, class _Equal, class _Alloc>
2618bool
2619__hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const
2620{
2621 return false;
2622}
2623
2624template <class _Tp, class _Hash, class _Equal, class _Alloc>
2625bool
2626__hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2627{
2628 return false;
2629}
2630
2631template <class _Tp, class _Hash, class _Equal, class _Alloc>
2632bool
2633__hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2634{
2635 return false;
2636}
2637
2638#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002639_LIBCPP_END_NAMESPACE_STD
2640
2641#endif // _LIBCPP__HASH_TABLE