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