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