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