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 std::__debug_db_erase_c(this); 1516} 1517 1518template <class _Tp, class _Hash, class _Equal, class _Alloc> 1519void 1520__hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc( 1521 const __hash_table& __u, true_type) 1522{ 1523 if (__node_alloc() != __u.__node_alloc()) 1524 { 1525 clear(); 1526 __bucket_list_.reset(); 1527 __bucket_list_.get_deleter().size() = 0; 1528 } 1529 __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc(); 1530 __node_alloc() = __u.__node_alloc(); 1531} 1532 1533template <class _Tp, class _Hash, class _Equal, class _Alloc> 1534__hash_table<_Tp, _Hash, _Equal, _Alloc>& 1535__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u) 1536{ 1537 if (this != _VSTD::addressof(__u)) 1538 { 1539 __copy_assign_alloc(__u); 1540 hash_function() = __u.hash_function(); 1541 key_eq() = __u.key_eq(); 1542 max_load_factor() = __u.max_load_factor(); 1543 __assign_multi(__u.begin(), __u.end()); 1544 } 1545 return *this; 1546} 1547 1548template <class _Tp, class _Hash, class _Equal, class _Alloc> 1549void 1550__hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate_node(__next_pointer __np) 1551 _NOEXCEPT 1552{ 1553 __node_allocator& __na = __node_alloc(); 1554 while (__np != nullptr) 1555 { 1556 __next_pointer __next = __np->__next_; 1557#if _LIBCPP_DEBUG_LEVEL == 2 1558 __c_node* __c = __get_db()->__find_c_and_lock(this); 1559 for (__i_node** __p = __c->end_; __p != __c->beg_; ) 1560 { 1561 --__p; 1562 iterator* __i = static_cast<iterator*>((*__p)->__i_); 1563 if (__i->__node_ == __np) 1564 { 1565 (*__p)->__c_ = nullptr; 1566 if (--__c->end_ != __p) 1567 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*)); 1568 } 1569 } 1570 __get_db()->unlock(); 1571#endif 1572 __node_pointer __real_np = __np->__upcast(); 1573 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__real_np->__value_)); 1574 __node_traits::deallocate(__na, __real_np, 1); 1575 __np = __next; 1576 } 1577} 1578 1579template <class _Tp, class _Hash, class _Equal, class _Alloc> 1580typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer 1581__hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT 1582{ 1583 size_type __bc = bucket_count(); 1584 for (size_type __i = 0; __i < __bc; ++__i) 1585 __bucket_list_[__i] = nullptr; 1586 size() = 0; 1587 __next_pointer __cache = __p1_.first().__next_; 1588 __p1_.first().__next_ = nullptr; 1589 return __cache; 1590} 1591 1592template <class _Tp, class _Hash, class _Equal, class _Alloc> 1593void 1594__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign( 1595 __hash_table& __u, true_type) 1596 _NOEXCEPT_( 1597 is_nothrow_move_assignable<__node_allocator>::value && 1598 is_nothrow_move_assignable<hasher>::value && 1599 is_nothrow_move_assignable<key_equal>::value) 1600{ 1601 clear(); 1602 __bucket_list_.reset(__u.__bucket_list_.release()); 1603 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size(); 1604 __u.__bucket_list_.get_deleter().size() = 0; 1605 __move_assign_alloc(__u); 1606 size() = __u.size(); 1607 hash_function() = _VSTD::move(__u.hash_function()); 1608 max_load_factor() = __u.max_load_factor(); 1609 key_eq() = _VSTD::move(__u.key_eq()); 1610 __p1_.first().__next_ = __u.__p1_.first().__next_; 1611 if (size() > 0) 1612 { 1613 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] = 1614 __p1_.first().__ptr(); 1615 __u.__p1_.first().__next_ = nullptr; 1616 __u.size() = 0; 1617 } 1618 std::__debug_db_swap(this, std::addressof(__u)); 1619} 1620 1621template <class _Tp, class _Hash, class _Equal, class _Alloc> 1622void 1623__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign( 1624 __hash_table& __u, false_type) 1625{ 1626 if (__node_alloc() == __u.__node_alloc()) 1627 __move_assign(__u, true_type()); 1628 else 1629 { 1630 hash_function() = _VSTD::move(__u.hash_function()); 1631 key_eq() = _VSTD::move(__u.key_eq()); 1632 max_load_factor() = __u.max_load_factor(); 1633 if (bucket_count() != 0) 1634 { 1635 __next_pointer __cache = __detach(); 1636#ifndef _LIBCPP_NO_EXCEPTIONS 1637 try 1638 { 1639#endif // _LIBCPP_NO_EXCEPTIONS 1640 const_iterator __i = __u.begin(); 1641 while (__cache != nullptr && __u.size() != 0) 1642 { 1643 __cache->__upcast()->__value_ = 1644 _VSTD::move(__u.remove(__i++)->__value_); 1645 __next_pointer __next = __cache->__next_; 1646 __node_insert_multi(__cache->__upcast()); 1647 __cache = __next; 1648 } 1649#ifndef _LIBCPP_NO_EXCEPTIONS 1650 } 1651 catch (...) 1652 { 1653 __deallocate_node(__cache); 1654 throw; 1655 } 1656#endif // _LIBCPP_NO_EXCEPTIONS 1657 __deallocate_node(__cache); 1658 } 1659 const_iterator __i = __u.begin(); 1660 while (__u.size() != 0) 1661 { 1662 __node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__value_)); 1663 __node_insert_multi(__h.get()); 1664 __h.release(); 1665 } 1666 } 1667} 1668 1669template <class _Tp, class _Hash, class _Equal, class _Alloc> 1670inline 1671__hash_table<_Tp, _Hash, _Equal, _Alloc>& 1672__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u) 1673 _NOEXCEPT_( 1674 __node_traits::propagate_on_container_move_assignment::value && 1675 is_nothrow_move_assignable<__node_allocator>::value && 1676 is_nothrow_move_assignable<hasher>::value && 1677 is_nothrow_move_assignable<key_equal>::value) 1678{ 1679 __move_assign(__u, integral_constant<bool, 1680 __node_traits::propagate_on_container_move_assignment::value>()); 1681 return *this; 1682} 1683 1684template <class _Tp, class _Hash, class _Equal, class _Alloc> 1685template <class _InputIterator> 1686void 1687__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first, 1688 _InputIterator __last) 1689{ 1690 typedef iterator_traits<_InputIterator> _ITraits; 1691 typedef typename _ITraits::value_type _ItValueType; 1692 static_assert((is_same<_ItValueType, __container_value_type>::value), 1693 "__assign_unique may only be called with the containers value type"); 1694 1695 if (bucket_count() != 0) 1696 { 1697 __next_pointer __cache = __detach(); 1698#ifndef _LIBCPP_NO_EXCEPTIONS 1699 try 1700 { 1701#endif // _LIBCPP_NO_EXCEPTIONS 1702 for (; __cache != nullptr && __first != __last; ++__first) 1703 { 1704 __cache->__upcast()->__value_ = *__first; 1705 __next_pointer __next = __cache->__next_; 1706 __node_insert_unique(__cache->__upcast()); 1707 __cache = __next; 1708 } 1709#ifndef _LIBCPP_NO_EXCEPTIONS 1710 } 1711 catch (...) 1712 { 1713 __deallocate_node(__cache); 1714 throw; 1715 } 1716#endif // _LIBCPP_NO_EXCEPTIONS 1717 __deallocate_node(__cache); 1718 } 1719 for (; __first != __last; ++__first) 1720 __insert_unique(*__first); 1721} 1722 1723template <class _Tp, class _Hash, class _Equal, class _Alloc> 1724template <class _InputIterator> 1725void 1726__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first, 1727 _InputIterator __last) 1728{ 1729 typedef iterator_traits<_InputIterator> _ITraits; 1730 typedef typename _ITraits::value_type _ItValueType; 1731 static_assert((is_same<_ItValueType, __container_value_type>::value || 1732 is_same<_ItValueType, __node_value_type>::value), 1733 "__assign_multi may only be called with the containers value type" 1734 " or the nodes value type"); 1735 if (bucket_count() != 0) 1736 { 1737 __next_pointer __cache = __detach(); 1738#ifndef _LIBCPP_NO_EXCEPTIONS 1739 try 1740 { 1741#endif // _LIBCPP_NO_EXCEPTIONS 1742 for (; __cache != nullptr && __first != __last; ++__first) 1743 { 1744 __cache->__upcast()->__value_ = *__first; 1745 __next_pointer __next = __cache->__next_; 1746 __node_insert_multi(__cache->__upcast()); 1747 __cache = __next; 1748 } 1749#ifndef _LIBCPP_NO_EXCEPTIONS 1750 } 1751 catch (...) 1752 { 1753 __deallocate_node(__cache); 1754 throw; 1755 } 1756#endif // _LIBCPP_NO_EXCEPTIONS 1757 __deallocate_node(__cache); 1758 } 1759 for (; __first != __last; ++__first) 1760 __insert_multi(_NodeTypes::__get_value(*__first)); 1761} 1762 1763template <class _Tp, class _Hash, class _Equal, class _Alloc> 1764inline 1765typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator 1766__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT 1767{ 1768#if _LIBCPP_DEBUG_LEVEL == 2 1769 return iterator(__p1_.first().__next_, this); 1770#else 1771 return iterator(__p1_.first().__next_); 1772#endif 1773} 1774 1775template <class _Tp, class _Hash, class _Equal, class _Alloc> 1776inline 1777typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator 1778__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT 1779{ 1780#if _LIBCPP_DEBUG_LEVEL == 2 1781 return iterator(nullptr, this); 1782#else 1783 return iterator(nullptr); 1784#endif 1785} 1786 1787template <class _Tp, class _Hash, class _Equal, class _Alloc> 1788inline 1789typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator 1790__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT 1791{ 1792#if _LIBCPP_DEBUG_LEVEL == 2 1793 return const_iterator(__p1_.first().__next_, this); 1794#else 1795 return const_iterator(__p1_.first().__next_); 1796#endif 1797} 1798 1799template <class _Tp, class _Hash, class _Equal, class _Alloc> 1800inline 1801typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator 1802__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT 1803{ 1804#if _LIBCPP_DEBUG_LEVEL == 2 1805 return const_iterator(nullptr, this); 1806#else 1807 return const_iterator(nullptr); 1808#endif 1809} 1810 1811template <class _Tp, class _Hash, class _Equal, class _Alloc> 1812void 1813__hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT 1814{ 1815 if (size() > 0) 1816 { 1817 __deallocate_node(__p1_.first().__next_); 1818 __p1_.first().__next_ = nullptr; 1819 size_type __bc = bucket_count(); 1820 for (size_type __i = 0; __i < __bc; ++__i) 1821 __bucket_list_[__i] = nullptr; 1822 size() = 0; 1823 } 1824} 1825 1826 1827// Prepare the container for an insertion of the value __value with the hash 1828// __hash. This does a lookup into the container to see if __value is already 1829// present, and performs a rehash if necessary. Returns a pointer to the 1830// existing element if it exists, otherwise nullptr. 1831// 1832// Note that this function does forward exceptions if key_eq() throws, and never 1833// mutates __value or actually inserts into the map. 1834template <class _Tp, class _Hash, class _Equal, class _Alloc> 1835_LIBCPP_INLINE_VISIBILITY 1836typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer 1837__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique_prepare( 1838 size_t __hash, value_type& __value) 1839{ 1840 size_type __bc = bucket_count(); 1841 1842 if (__bc != 0) 1843 { 1844 size_t __chash = __constrain_hash(__hash, __bc); 1845 __next_pointer __ndptr = __bucket_list_[__chash]; 1846 if (__ndptr != nullptr) 1847 { 1848 for (__ndptr = __ndptr->__next_; __ndptr != nullptr && 1849 __constrain_hash(__ndptr->__hash(), __bc) == __chash; 1850 __ndptr = __ndptr->__next_) 1851 { 1852 if (key_eq()(__ndptr->__upcast()->__value_, __value)) 1853 return __ndptr; 1854 } 1855 } 1856 } 1857 if (size()+1 > __bc * max_load_factor() || __bc == 0) 1858 { 1859 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc), 1860 size_type(ceil(float(size() + 1) / max_load_factor())))); 1861 } 1862 return nullptr; 1863} 1864 1865// Insert the node __nd into the container by pushing it into the right bucket, 1866// and updating size(). Assumes that __nd->__hash is up-to-date, and that 1867// rehashing has already occurred and that no element with the same key exists 1868// in the map. 1869template <class _Tp, class _Hash, class _Equal, class _Alloc> 1870_LIBCPP_INLINE_VISIBILITY 1871void 1872__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique_perform( 1873 __node_pointer __nd) _NOEXCEPT 1874{ 1875 size_type __bc = bucket_count(); 1876 size_t __chash = __constrain_hash(__nd->__hash(), __bc); 1877 // insert_after __bucket_list_[__chash], or __first_node if bucket is null 1878 __next_pointer __pn = __bucket_list_[__chash]; 1879 if (__pn == nullptr) 1880 { 1881 __pn =__p1_.first().__ptr(); 1882 __nd->__next_ = __pn->__next_; 1883 __pn->__next_ = __nd->__ptr(); 1884 // fix up __bucket_list_ 1885 __bucket_list_[__chash] = __pn; 1886 if (__nd->__next_ != nullptr) 1887 __bucket_list_[__constrain_hash(__nd->__next_->__hash(), __bc)] = __nd->__ptr(); 1888 } 1889 else 1890 { 1891 __nd->__next_ = __pn->__next_; 1892 __pn->__next_ = __nd->__ptr(); 1893 } 1894 ++size(); 1895} 1896 1897template <class _Tp, class _Hash, class _Equal, class _Alloc> 1898pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool> 1899__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd) 1900{ 1901 __nd->__hash_ = hash_function()(__nd->__value_); 1902 __next_pointer __existing_node = 1903 __node_insert_unique_prepare(__nd->__hash(), __nd->__value_); 1904 1905 // Insert the node, unless it already exists in the container. 1906 bool __inserted = false; 1907 if (__existing_node == nullptr) 1908 { 1909 __node_insert_unique_perform(__nd); 1910 __existing_node = __nd->__ptr(); 1911 __inserted = true; 1912 } 1913#if _LIBCPP_DEBUG_LEVEL == 2 1914 return pair<iterator, bool>(iterator(__existing_node, this), __inserted); 1915#else 1916 return pair<iterator, bool>(iterator(__existing_node), __inserted); 1917#endif 1918} 1919 1920// Prepare the container for an insertion of the value __cp_val with the hash 1921// __cp_hash. This does a lookup into the container to see if __cp_value is 1922// already present, and performs a rehash if necessary. Returns a pointer to the 1923// last occurrence of __cp_val in the map. 1924// 1925// Note that this function does forward exceptions if key_eq() throws, and never 1926// mutates __value or actually inserts into the map. 1927template <class _Tp, class _Hash, class _Equal, class _Alloc> 1928typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer 1929__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi_prepare( 1930 size_t __cp_hash, value_type& __cp_val) 1931{ 1932 size_type __bc = bucket_count(); 1933 if (size()+1 > __bc * max_load_factor() || __bc == 0) 1934 { 1935 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc), 1936 size_type(ceil(float(size() + 1) / max_load_factor())))); 1937 __bc = bucket_count(); 1938 } 1939 size_t __chash = __constrain_hash(__cp_hash, __bc); 1940 __next_pointer __pn = __bucket_list_[__chash]; 1941 if (__pn != nullptr) 1942 { 1943 for (bool __found = false; __pn->__next_ != nullptr && 1944 __constrain_hash(__pn->__next_->__hash(), __bc) == __chash; 1945 __pn = __pn->__next_) 1946 { 1947 // __found key_eq() action 1948 // false false loop 1949 // true true loop 1950 // false true set __found to true 1951 // true false break 1952 if (__found != (__pn->__next_->__hash() == __cp_hash && 1953 key_eq()(__pn->__next_->__upcast()->__value_, __cp_val))) 1954 { 1955 if (!__found) 1956 __found = true; 1957 else 1958 break; 1959 } 1960 } 1961 } 1962 return __pn; 1963} 1964 1965// Insert the node __cp into the container after __pn (which is the last node in 1966// the bucket that compares equal to __cp). Rehashing, and checking for 1967// uniqueness has already been performed (in __node_insert_multi_prepare), so 1968// all we need to do is update the bucket and size(). Assumes that __cp->__hash 1969// is up-to-date. 1970template <class _Tp, class _Hash, class _Equal, class _Alloc> 1971void 1972__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi_perform( 1973 __node_pointer __cp, __next_pointer __pn) _NOEXCEPT 1974{ 1975 size_type __bc = bucket_count(); 1976 size_t __chash = __constrain_hash(__cp->__hash_, __bc); 1977 if (__pn == nullptr) 1978 { 1979 __pn =__p1_.first().__ptr(); 1980 __cp->__next_ = __pn->__next_; 1981 __pn->__next_ = __cp->__ptr(); 1982 // fix up __bucket_list_ 1983 __bucket_list_[__chash] = __pn; 1984 if (__cp->__next_ != nullptr) 1985 __bucket_list_[__constrain_hash(__cp->__next_->__hash(), __bc)] 1986 = __cp->__ptr(); 1987 } 1988 else 1989 { 1990 __cp->__next_ = __pn->__next_; 1991 __pn->__next_ = __cp->__ptr(); 1992 if (__cp->__next_ != nullptr) 1993 { 1994 size_t __nhash = __constrain_hash(__cp->__next_->__hash(), __bc); 1995 if (__nhash != __chash) 1996 __bucket_list_[__nhash] = __cp->__ptr(); 1997 } 1998 } 1999 ++size(); 2000} 2001 2002 2003template <class _Tp, class _Hash, class _Equal, class _Alloc> 2004typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator 2005__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp) 2006{ 2007 __cp->__hash_ = hash_function()(__cp->__value_); 2008 __next_pointer __pn = __node_insert_multi_prepare(__cp->__hash(), __cp->__value_); 2009 __node_insert_multi_perform(__cp, __pn); 2010 2011#if _LIBCPP_DEBUG_LEVEL == 2 2012 return iterator(__cp->__ptr(), this); 2013#else 2014 return iterator(__cp->__ptr()); 2015#endif 2016} 2017 2018template <class _Tp, class _Hash, class _Equal, class _Alloc> 2019typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator 2020__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi( 2021 const_iterator __p, __node_pointer __cp) 2022{ 2023 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this, 2024 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not" 2025 " referring to this unordered container"); 2026 if (__p != end() && key_eq()(*__p, __cp->__value_)) 2027 { 2028 __next_pointer __np = __p.__node_; 2029 __cp->__hash_ = __np->__hash(); 2030 size_type __bc = bucket_count(); 2031 if (size()+1 > __bc * max_load_factor() || __bc == 0) 2032 { 2033 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc), 2034 size_type(ceil(float(size() + 1) / max_load_factor())))); 2035 __bc = bucket_count(); 2036 } 2037 size_t __chash = __constrain_hash(__cp->__hash_, __bc); 2038 __next_pointer __pp = __bucket_list_[__chash]; 2039 while (__pp->__next_ != __np) 2040 __pp = __pp->__next_; 2041 __cp->__next_ = __np; 2042 __pp->__next_ = static_cast<__next_pointer>(__cp); 2043 ++size(); 2044#if _LIBCPP_DEBUG_LEVEL == 2 2045 return iterator(static_cast<__next_pointer>(__cp), this); 2046#else 2047 return iterator(static_cast<__next_pointer>(__cp)); 2048#endif 2049 } 2050 return __node_insert_multi(__cp); 2051} 2052 2053 2054 2055template <class _Tp, class _Hash, class _Equal, class _Alloc> 2056template <class _Key, class ..._Args> 2057pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool> 2058__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args) 2059{ 2060 2061 size_t __hash = hash_function()(__k); 2062 size_type __bc = bucket_count(); 2063 bool __inserted = false; 2064 __next_pointer __nd; 2065 size_t __chash; 2066 if (__bc != 0) 2067 { 2068 __chash = __constrain_hash(__hash, __bc); 2069 __nd = __bucket_list_[__chash]; 2070 if (__nd != nullptr) 2071 { 2072 for (__nd = __nd->__next_; __nd != nullptr && 2073 (__nd->__hash() == __hash || __constrain_hash(__nd->__hash(), __bc) == __chash); 2074 __nd = __nd->__next_) 2075 { 2076 if (key_eq()(__nd->__upcast()->__value_, __k)) 2077 goto __done; 2078 } 2079 } 2080 } 2081 { 2082 __node_holder __h = __construct_node_hash(__hash, _VSTD::forward<_Args>(__args)...); 2083 if (size()+1 > __bc * max_load_factor() || __bc == 0) 2084 { 2085 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc), 2086 size_type(ceil(float(size() + 1) / max_load_factor())))); 2087 __bc = bucket_count(); 2088 __chash = __constrain_hash(__hash, __bc); 2089 } 2090 // insert_after __bucket_list_[__chash], or __first_node if bucket is null 2091 __next_pointer __pn = __bucket_list_[__chash]; 2092 if (__pn == nullptr) 2093 { 2094 __pn = __p1_.first().__ptr(); 2095 __h->__next_ = __pn->__next_; 2096 __pn->__next_ = __h.get()->__ptr(); 2097 // fix up __bucket_list_ 2098 __bucket_list_[__chash] = __pn; 2099 if (__h->__next_ != nullptr) 2100 __bucket_list_[__constrain_hash(__h->__next_->__hash(), __bc)] 2101 = __h.get()->__ptr(); 2102 } 2103 else 2104 { 2105 __h->__next_ = __pn->__next_; 2106 __pn->__next_ = static_cast<__next_pointer>(__h.get()); 2107 } 2108 __nd = static_cast<__next_pointer>(__h.release()); 2109 // increment size 2110 ++size(); 2111 __inserted = true; 2112 } 2113__done: 2114#if _LIBCPP_DEBUG_LEVEL == 2 2115 return pair<iterator, bool>(iterator(__nd, this), __inserted); 2116#else 2117 return pair<iterator, bool>(iterator(__nd), __inserted); 2118#endif 2119} 2120 2121template <class _Tp, class _Hash, class _Equal, class _Alloc> 2122template <class... _Args> 2123pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool> 2124__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_impl(_Args&&... __args) 2125{ 2126 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); 2127 pair<iterator, bool> __r = __node_insert_unique(__h.get()); 2128 if (__r.second) 2129 __h.release(); 2130 return __r; 2131} 2132 2133template <class _Tp, class _Hash, class _Equal, class _Alloc> 2134template <class... _Args> 2135typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator 2136__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args) 2137{ 2138 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); 2139 iterator __r = __node_insert_multi(__h.get()); 2140 __h.release(); 2141 return __r; 2142} 2143 2144template <class _Tp, class _Hash, class _Equal, class _Alloc> 2145template <class... _Args> 2146typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator 2147__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi( 2148 const_iterator __p, _Args&&... __args) 2149{ 2150 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this, 2151 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not" 2152 " referring to this unordered container"); 2153 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); 2154 iterator __r = __node_insert_multi(__p, __h.get()); 2155 __h.release(); 2156 return __r; 2157} 2158 2159#if _LIBCPP_STD_VER > 14 2160template <class _Tp, class _Hash, class _Equal, class _Alloc> 2161template <class _NodeHandle, class _InsertReturnType> 2162_LIBCPP_INLINE_VISIBILITY 2163_InsertReturnType 2164__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_unique( 2165 _NodeHandle&& __nh) 2166{ 2167 if (__nh.empty()) 2168 return _InsertReturnType{end(), false, _NodeHandle()}; 2169 pair<iterator, bool> __result = __node_insert_unique(__nh.__ptr_); 2170 if (__result.second) 2171 __nh.__release_ptr(); 2172 return _InsertReturnType{__result.first, __result.second, _VSTD::move(__nh)}; 2173} 2174 2175template <class _Tp, class _Hash, class _Equal, class _Alloc> 2176template <class _NodeHandle> 2177_LIBCPP_INLINE_VISIBILITY 2178typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator 2179__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_unique( 2180 const_iterator, _NodeHandle&& __nh) 2181{ 2182 if (__nh.empty()) 2183 return end(); 2184 pair<iterator, bool> __result = __node_insert_unique(__nh.__ptr_); 2185 if (__result.second) 2186 __nh.__release_ptr(); 2187 return __result.first; 2188} 2189 2190template <class _Tp, class _Hash, class _Equal, class _Alloc> 2191template <class _NodeHandle> 2192_LIBCPP_INLINE_VISIBILITY 2193_NodeHandle 2194__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_extract( 2195 key_type const& __key) 2196{ 2197 iterator __i = find(__key); 2198 if (__i == end()) 2199 return _NodeHandle(); 2200 return __node_handle_extract<_NodeHandle>(__i); 2201} 2202 2203template <class _Tp, class _Hash, class _Equal, class _Alloc> 2204template <class _NodeHandle> 2205_LIBCPP_INLINE_VISIBILITY 2206_NodeHandle 2207__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_extract( 2208 const_iterator __p) 2209{ 2210 allocator_type __alloc(__node_alloc()); 2211 return _NodeHandle(remove(__p).release(), __alloc); 2212} 2213 2214template <class _Tp, class _Hash, class _Equal, class _Alloc> 2215template <class _Table> 2216_LIBCPP_INLINE_VISIBILITY 2217void 2218__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_merge_unique( 2219 _Table& __source) 2220{ 2221 static_assert(is_same<__node, typename _Table::__node>::value, ""); 2222 2223 for (typename _Table::iterator __it = __source.begin(); 2224 __it != __source.end();) 2225 { 2226 __node_pointer __src_ptr = __it.__node_->__upcast(); 2227 size_t __hash = hash_function()(__src_ptr->__value_); 2228 __next_pointer __existing_node = 2229 __node_insert_unique_prepare(__hash, __src_ptr->__value_); 2230 auto __prev_iter = __it++; 2231 if (__existing_node == nullptr) 2232 { 2233 (void)__source.remove(__prev_iter).release(); 2234 __src_ptr->__hash_ = __hash; 2235 __node_insert_unique_perform(__src_ptr); 2236 } 2237 } 2238} 2239 2240template <class _Tp, class _Hash, class _Equal, class _Alloc> 2241template <class _NodeHandle> 2242_LIBCPP_INLINE_VISIBILITY 2243typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator 2244__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_multi( 2245 _NodeHandle&& __nh) 2246{ 2247 if (__nh.empty()) 2248 return end(); 2249 iterator __result = __node_insert_multi(__nh.__ptr_); 2250 __nh.__release_ptr(); 2251 return __result; 2252} 2253 2254template <class _Tp, class _Hash, class _Equal, class _Alloc> 2255template <class _NodeHandle> 2256_LIBCPP_INLINE_VISIBILITY 2257typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator 2258__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_multi( 2259 const_iterator __hint, _NodeHandle&& __nh) 2260{ 2261 if (__nh.empty()) 2262 return end(); 2263 iterator __result = __node_insert_multi(__hint, __nh.__ptr_); 2264 __nh.__release_ptr(); 2265 return __result; 2266} 2267 2268template <class _Tp, class _Hash, class _Equal, class _Alloc> 2269template <class _Table> 2270_LIBCPP_INLINE_VISIBILITY 2271void 2272__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_merge_multi( 2273 _Table& __source) 2274{ 2275 static_assert(is_same<typename _Table::__node, __node>::value, ""); 2276 2277 for (typename _Table::iterator __it = __source.begin(); 2278 __it != __source.end();) 2279 { 2280 __node_pointer __src_ptr = __it.__node_->__upcast(); 2281 size_t __src_hash = hash_function()(__src_ptr->__value_); 2282 __next_pointer __pn = 2283 __node_insert_multi_prepare(__src_hash, __src_ptr->__value_); 2284 (void)__source.remove(__it++).release(); 2285 __src_ptr->__hash_ = __src_hash; 2286 __node_insert_multi_perform(__src_ptr, __pn); 2287 } 2288} 2289#endif // _LIBCPP_STD_VER > 14 2290 2291template <class _Tp, class _Hash, class _Equal, class _Alloc> 2292void 2293__hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n) 2294_LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK 2295{ 2296 if (__n == 1) 2297 __n = 2; 2298 else if (__n & (__n - 1)) 2299 __n = __next_prime(__n); 2300 size_type __bc = bucket_count(); 2301 if (__n > __bc) 2302 __rehash(__n); 2303 else if (__n < __bc) 2304 { 2305 __n = _VSTD::max<size_type> 2306 ( 2307 __n, 2308 __is_hash_power2(__bc) ? __next_hash_pow2(size_t(ceil(float(size()) / max_load_factor()))) : 2309 __next_prime(size_t(ceil(float(size()) / max_load_factor()))) 2310 ); 2311 if (__n < __bc) 2312 __rehash(__n); 2313 } 2314} 2315 2316template <class _Tp, class _Hash, class _Equal, class _Alloc> 2317void 2318__hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc) 2319{ 2320 std::__debug_db_invalidate_all(this); 2321 __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc(); 2322 __bucket_list_.reset(__nbc > 0 ? 2323 __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr); 2324 __bucket_list_.get_deleter().size() = __nbc; 2325 if (__nbc > 0) 2326 { 2327 for (size_type __i = 0; __i < __nbc; ++__i) 2328 __bucket_list_[__i] = nullptr; 2329 __next_pointer __pp = __p1_.first().__ptr(); 2330 __next_pointer __cp = __pp->__next_; 2331 if (__cp != nullptr) 2332 { 2333 size_type __chash = __constrain_hash(__cp->__hash(), __nbc); 2334 __bucket_list_[__chash] = __pp; 2335 size_type __phash = __chash; 2336 for (__pp = __cp, void(), __cp = __cp->__next_; __cp != nullptr; 2337 __cp = __pp->__next_) 2338 { 2339 __chash = __constrain_hash(__cp->__hash(), __nbc); 2340 if (__chash == __phash) 2341 __pp = __cp; 2342 else 2343 { 2344 if (__bucket_list_[__chash] == nullptr) 2345 { 2346 __bucket_list_[__chash] = __pp; 2347 __pp = __cp; 2348 __phash = __chash; 2349 } 2350 else 2351 { 2352 __next_pointer __np = __cp; 2353 for (; __np->__next_ != nullptr && 2354 key_eq()(__cp->__upcast()->__value_, 2355 __np->__next_->__upcast()->__value_); 2356 __np = __np->__next_) 2357 ; 2358 __pp->__next_ = __np->__next_; 2359 __np->__next_ = __bucket_list_[__chash]->__next_; 2360 __bucket_list_[__chash]->__next_ = __cp; 2361 2362 } 2363 } 2364 } 2365 } 2366 } 2367} 2368 2369template <class _Tp, class _Hash, class _Equal, class _Alloc> 2370template <class _Key> 2371typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator 2372__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) 2373{ 2374 size_t __hash = hash_function()(__k); 2375 size_type __bc = bucket_count(); 2376 if (__bc != 0) 2377 { 2378 size_t __chash = __constrain_hash(__hash, __bc); 2379 __next_pointer __nd = __bucket_list_[__chash]; 2380 if (__nd != nullptr) 2381 { 2382 for (__nd = __nd->__next_; __nd != nullptr && 2383 (__nd->__hash() == __hash 2384 || __constrain_hash(__nd->__hash(), __bc) == __chash); 2385 __nd = __nd->__next_) 2386 { 2387 if ((__nd->__hash() == __hash) 2388 && key_eq()(__nd->__upcast()->__value_, __k)) 2389#if _LIBCPP_DEBUG_LEVEL == 2 2390 return iterator(__nd, this); 2391#else 2392 return iterator(__nd); 2393#endif 2394 } 2395 } 2396 } 2397 return end(); 2398} 2399 2400template <class _Tp, class _Hash, class _Equal, class _Alloc> 2401template <class _Key> 2402typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator 2403__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const 2404{ 2405 size_t __hash = hash_function()(__k); 2406 size_type __bc = bucket_count(); 2407 if (__bc != 0) 2408 { 2409 size_t __chash = __constrain_hash(__hash, __bc); 2410 __next_pointer __nd = __bucket_list_[__chash]; 2411 if (__nd != nullptr) 2412 { 2413 for (__nd = __nd->__next_; __nd != nullptr && 2414 (__hash == __nd->__hash() 2415 || __constrain_hash(__nd->__hash(), __bc) == __chash); 2416 __nd = __nd->__next_) 2417 { 2418 if ((__nd->__hash() == __hash) 2419 && key_eq()(__nd->__upcast()->__value_, __k)) 2420#if _LIBCPP_DEBUG_LEVEL == 2 2421 return const_iterator(__nd, this); 2422#else 2423 return const_iterator(__nd); 2424#endif 2425 } 2426 } 2427 2428 } 2429 return end(); 2430} 2431 2432template <class _Tp, class _Hash, class _Equal, class _Alloc> 2433template <class ..._Args> 2434typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder 2435__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args) 2436{ 2437 static_assert(!__is_hash_value_type<_Args...>::value, 2438 "Construct cannot be called with a hash value type"); 2439 __node_allocator& __na = __node_alloc(); 2440 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na)); 2441 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...); 2442 __h.get_deleter().__value_constructed = true; 2443 __h->__hash_ = hash_function()(__h->__value_); 2444 __h->__next_ = nullptr; 2445 return __h; 2446} 2447 2448template <class _Tp, class _Hash, class _Equal, class _Alloc> 2449template <class _First, class ..._Rest> 2450typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder 2451__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash( 2452 size_t __hash, _First&& __f, _Rest&& ...__rest) 2453{ 2454 static_assert(!__is_hash_value_type<_First, _Rest...>::value, 2455 "Construct cannot be called with a hash value type"); 2456 __node_allocator& __na = __node_alloc(); 2457 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na)); 2458 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), 2459 _VSTD::forward<_First>(__f), 2460 _VSTD::forward<_Rest>(__rest)...); 2461 __h.get_deleter().__value_constructed = true; 2462 __h->__hash_ = __hash; 2463 __h->__next_ = nullptr; 2464 return __h; 2465} 2466 2467template <class _Tp, class _Hash, class _Equal, class _Alloc> 2468typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator 2469__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p) 2470{ 2471 __next_pointer __np = __p.__node_; 2472 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this, 2473 "unordered container erase(iterator) called with an iterator not" 2474 " referring to this container"); 2475 _LIBCPP_ASSERT(__p != end(), 2476 "unordered container erase(iterator) called with a non-dereferenceable iterator"); 2477#if _LIBCPP_DEBUG_LEVEL == 2 2478 iterator __r(__np, this); 2479#else 2480 iterator __r(__np); 2481#endif 2482 ++__r; 2483 remove(__p); 2484 return __r; 2485} 2486 2487template <class _Tp, class _Hash, class _Equal, class _Alloc> 2488typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator 2489__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first, 2490 const_iterator __last) 2491{ 2492 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__first)) == this, 2493 "unordered container::erase(iterator, iterator) called with an iterator not" 2494 " referring to this container"); 2495 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__last)) == this, 2496 "unordered container::erase(iterator, iterator) called with an iterator not" 2497 " referring to this container"); 2498 for (const_iterator __p = __first; __first != __last; __p = __first) 2499 { 2500 ++__first; 2501 erase(__p); 2502 } 2503 __next_pointer __np = __last.__node_; 2504#if _LIBCPP_DEBUG_LEVEL == 2 2505 return iterator (__np, this); 2506#else 2507 return iterator (__np); 2508#endif 2509} 2510 2511template <class _Tp, class _Hash, class _Equal, class _Alloc> 2512template <class _Key> 2513typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type 2514__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k) 2515{ 2516 iterator __i = find(__k); 2517 if (__i == end()) 2518 return 0; 2519 erase(__i); 2520 return 1; 2521} 2522 2523template <class _Tp, class _Hash, class _Equal, class _Alloc> 2524template <class _Key> 2525typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type 2526__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k) 2527{ 2528 size_type __r = 0; 2529 iterator __i = find(__k); 2530 if (__i != end()) 2531 { 2532 iterator __e = end(); 2533 do 2534 { 2535 erase(__i++); 2536 ++__r; 2537 } while (__i != __e && key_eq()(*__i, __k)); 2538 } 2539 return __r; 2540} 2541 2542template <class _Tp, class _Hash, class _Equal, class _Alloc> 2543typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder 2544__hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT 2545{ 2546 // current node 2547 __next_pointer __cn = __p.__node_; 2548 size_type __bc = bucket_count(); 2549 size_t __chash = __constrain_hash(__cn->__hash(), __bc); 2550 // find previous node 2551 __next_pointer __pn = __bucket_list_[__chash]; 2552 for (; __pn->__next_ != __cn; __pn = __pn->__next_) 2553 ; 2554 // Fix up __bucket_list_ 2555 // if __pn is not in same bucket (before begin is not in same bucket) && 2556 // if __cn->__next_ is not in same bucket (nullptr is not in same bucket) 2557 if (__pn == __p1_.first().__ptr() 2558 || __constrain_hash(__pn->__hash(), __bc) != __chash) 2559 { 2560 if (__cn->__next_ == nullptr 2561 || __constrain_hash(__cn->__next_->__hash(), __bc) != __chash) 2562 __bucket_list_[__chash] = nullptr; 2563 } 2564 // if __cn->__next_ is not in same bucket (nullptr is in same bucket) 2565 if (__cn->__next_ != nullptr) 2566 { 2567 size_t __nhash = __constrain_hash(__cn->__next_->__hash(), __bc); 2568 if (__nhash != __chash) 2569 __bucket_list_[__nhash] = __pn; 2570 } 2571 // remove __cn 2572 __pn->__next_ = __cn->__next_; 2573 __cn->__next_ = nullptr; 2574 --size(); 2575#if _LIBCPP_DEBUG_LEVEL == 2 2576 __c_node* __c = __get_db()->__find_c_and_lock(this); 2577 for (__i_node** __dp = __c->end_; __dp != __c->beg_; ) 2578 { 2579 --__dp; 2580 iterator* __i = static_cast<iterator*>((*__dp)->__i_); 2581 if (__i->__node_ == __cn) 2582 { 2583 (*__dp)->__c_ = nullptr; 2584 if (--__c->end_ != __dp) 2585 _VSTD::memmove(__dp, __dp+1, (__c->end_ - __dp)*sizeof(__i_node*)); 2586 } 2587 } 2588 __get_db()->unlock(); 2589#endif 2590 return __node_holder(__cn->__upcast(), _Dp(__node_alloc(), true)); 2591} 2592 2593template <class _Tp, class _Hash, class _Equal, class _Alloc> 2594template <class _Key> 2595inline 2596typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type 2597__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const 2598{ 2599 return static_cast<size_type>(find(__k) != end()); 2600} 2601 2602template <class _Tp, class _Hash, class _Equal, class _Alloc> 2603template <class _Key> 2604typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type 2605__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const 2606{ 2607 size_type __r = 0; 2608 const_iterator __i = find(__k); 2609 if (__i != end()) 2610 { 2611 const_iterator __e = end(); 2612 do 2613 { 2614 ++__i; 2615 ++__r; 2616 } while (__i != __e && key_eq()(*__i, __k)); 2617 } 2618 return __r; 2619} 2620 2621template <class _Tp, class _Hash, class _Equal, class _Alloc> 2622template <class _Key> 2623pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, 2624 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator> 2625__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique( 2626 const _Key& __k) 2627{ 2628 iterator __i = find(__k); 2629 iterator __j = __i; 2630 if (__i != end()) 2631 ++__j; 2632 return pair<iterator, iterator>(__i, __j); 2633} 2634 2635template <class _Tp, class _Hash, class _Equal, class _Alloc> 2636template <class _Key> 2637pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator, 2638 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator> 2639__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique( 2640 const _Key& __k) const 2641{ 2642 const_iterator __i = find(__k); 2643 const_iterator __j = __i; 2644 if (__i != end()) 2645 ++__j; 2646 return pair<const_iterator, const_iterator>(__i, __j); 2647} 2648 2649template <class _Tp, class _Hash, class _Equal, class _Alloc> 2650template <class _Key> 2651pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, 2652 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator> 2653__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi( 2654 const _Key& __k) 2655{ 2656 iterator __i = find(__k); 2657 iterator __j = __i; 2658 if (__i != end()) 2659 { 2660 iterator __e = end(); 2661 do 2662 { 2663 ++__j; 2664 } while (__j != __e && key_eq()(*__j, __k)); 2665 } 2666 return pair<iterator, iterator>(__i, __j); 2667} 2668 2669template <class _Tp, class _Hash, class _Equal, class _Alloc> 2670template <class _Key> 2671pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator, 2672 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator> 2673__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi( 2674 const _Key& __k) const 2675{ 2676 const_iterator __i = find(__k); 2677 const_iterator __j = __i; 2678 if (__i != end()) 2679 { 2680 const_iterator __e = end(); 2681 do 2682 { 2683 ++__j; 2684 } while (__j != __e && key_eq()(*__j, __k)); 2685 } 2686 return pair<const_iterator, const_iterator>(__i, __j); 2687} 2688 2689template <class _Tp, class _Hash, class _Equal, class _Alloc> 2690void 2691__hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u) 2692#if _LIBCPP_STD_VER <= 11 2693 _NOEXCEPT_( 2694 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value 2695 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value 2696 || __is_nothrow_swappable<__pointer_allocator>::value) 2697 && (!__node_traits::propagate_on_container_swap::value 2698 || __is_nothrow_swappable<__node_allocator>::value) 2699 ) 2700#else 2701 _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value) 2702#endif 2703{ 2704 _LIBCPP_ASSERT(__node_traits::propagate_on_container_swap::value || 2705 this->__node_alloc() == __u.__node_alloc(), 2706 "list::swap: Either propagate_on_container_swap must be true" 2707 " or the allocators must compare equal"); 2708 { 2709 __node_pointer_pointer __npp = __bucket_list_.release(); 2710 __bucket_list_.reset(__u.__bucket_list_.release()); 2711 __u.__bucket_list_.reset(__npp); 2712 } 2713 _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size()); 2714 _VSTD::__swap_allocator(__bucket_list_.get_deleter().__alloc(), 2715 __u.__bucket_list_.get_deleter().__alloc()); 2716 _VSTD::__swap_allocator(__node_alloc(), __u.__node_alloc()); 2717 _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_); 2718 __p2_.swap(__u.__p2_); 2719 __p3_.swap(__u.__p3_); 2720 if (size() > 0) 2721 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] = 2722 __p1_.first().__ptr(); 2723 if (__u.size() > 0) 2724 __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash(), __u.bucket_count())] = 2725 __u.__p1_.first().__ptr(); 2726 std::__debug_db_swap(this, std::addressof(__u)); 2727} 2728 2729template <class _Tp, class _Hash, class _Equal, class _Alloc> 2730typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type 2731__hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const 2732{ 2733 _LIBCPP_ASSERT(__n < bucket_count(), 2734 "unordered container::bucket_size(n) called with n >= bucket_count()"); 2735 __next_pointer __np = __bucket_list_[__n]; 2736 size_type __bc = bucket_count(); 2737 size_type __r = 0; 2738 if (__np != nullptr) 2739 { 2740 for (__np = __np->__next_; __np != nullptr && 2741 __constrain_hash(__np->__hash(), __bc) == __n; 2742 __np = __np->__next_, (void) ++__r) 2743 ; 2744 } 2745 return __r; 2746} 2747 2748template <class _Tp, class _Hash, class _Equal, class _Alloc> 2749inline _LIBCPP_INLINE_VISIBILITY 2750void 2751swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x, 2752 __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y) 2753 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) 2754{ 2755 __x.swap(__y); 2756} 2757 2758#if _LIBCPP_DEBUG_LEVEL == 2 2759 2760template <class _Tp, class _Hash, class _Equal, class _Alloc> 2761bool 2762__hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const 2763{ 2764 return __i->__node_ != nullptr; 2765} 2766 2767template <class _Tp, class _Hash, class _Equal, class _Alloc> 2768bool 2769__hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const 2770{ 2771 return false; 2772} 2773 2774template <class _Tp, class _Hash, class _Equal, class _Alloc> 2775bool 2776__hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const 2777{ 2778 return false; 2779} 2780 2781template <class _Tp, class _Hash, class _Equal, class _Alloc> 2782bool 2783__hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const 2784{ 2785 return false; 2786} 2787 2788#endif // _LIBCPP_DEBUG_LEVEL == 2 2789 2790_LIBCPP_END_NAMESPACE_STD 2791 2792_LIBCPP_POP_MACROS 2793 2794#endif // _LIBCPP___HASH_TABLE 2795