Where Online Learning is simpler!
The C and C++ Include Header Files
/usr/include/c++/11/bits/hashtable_policy.h
$ cat -n /usr/include/c++/11/bits/hashtable_policy.h 1 // Internal policy header for unordered_set and unordered_map -*- C++ -*- 2 3 // Copyright (C) 2010-2021 Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the 7 // terms of the GNU General Public License as published by the 8 // Free Software Foundation; either version 3, or (at your option) 9 // any later version. 10 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 16 // Under Section 7 of GPL version 3, you are granted additional 17 // permissions described in the GCC Runtime Library Exception, version 18 // 3.1, as published by the Free Software Foundation. 19 20 // You should have received a copy of the GNU General Public License and 21 // a copy of the GCC Runtime Library Exception along with this program; 22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 //
. 24 25 /** @file bits/hashtable_policy.h 26 * This is an internal header file, included by other library headers. 27 * Do not attempt to use it directly. 28 * @headername{unordered_map,unordered_set} 29 */ 30 31 #ifndef _HASHTABLE_POLICY_H 32 #define _HASHTABLE_POLICY_H 1 33 34 #include
// for std::tuple, std::forward_as_tuple 35 #include
// for std::min, std::is_permutation. 36 #include
// for __gnu_cxx::__int_traits 37 38 namespace std _GLIBCXX_VISIBILITY(default) 39 { 40 _GLIBCXX_BEGIN_NAMESPACE_VERSION 41 /// @cond undocumented 42 43 template
47 class _Hashtable; 48 49 namespace __detail 50 { 51 /** 52 * @defgroup hashtable-detail Base and Implementation Classes 53 * @ingroup unordered_associative_containers 54 * @{ 55 */ 56 template
59 struct _Hashtable_base; 60 61 // Helper function: return distance(first, last) for forward 62 // iterators, or 0/1 for input iterators. 63 template
64 inline typename std::iterator_traits<_Iterator>::difference_type 65 __distance_fw(_Iterator __first, _Iterator __last, 66 std::input_iterator_tag) 67 { return __first != __last ? 1 : 0; } 68 69 template
70 inline typename std::iterator_traits<_Iterator>::difference_type 71 __distance_fw(_Iterator __first, _Iterator __last, 72 std::forward_iterator_tag) 73 { return std::distance(__first, __last); } 74 75 template
76 inline typename std::iterator_traits<_Iterator>::difference_type 77 __distance_fw(_Iterator __first, _Iterator __last) 78 { return __distance_fw(__first, __last, 79 std::__iterator_category(__first)); } 80 81 struct _Identity 82 { 83 template
84 _Tp&& 85 operator()(_Tp&& __x) const noexcept 86 { return std::forward<_Tp>(__x); } 87 }; 88 89 struct _Select1st 90 { 91 template
92 auto 93 operator()(_Tp&& __x) const noexcept 94 -> decltype(std::get<0>(std::forward<_Tp>(__x))) 95 { return std::get<0>(std::forward<_Tp>(__x)); } 96 }; 97 98 template
99 struct _Hashtable_alloc; 100 101 // Functor recycling a pool of nodes and using allocation once the pool is 102 // empty. 103 template
104 struct _ReuseOrAllocNode 105 { 106 private: 107 using __node_alloc_type = _NodeAlloc; 108 using __hashtable_alloc = _Hashtable_alloc<__node_alloc_type>; 109 using __node_alloc_traits = 110 typename __hashtable_alloc::__node_alloc_traits; 111 using __node_type = typename __hashtable_alloc::__node_type; 112 113 public: 114 _ReuseOrAllocNode(__node_type* __nodes, __hashtable_alloc& __h) 115 : _M_nodes(__nodes), _M_h(__h) { } 116 _ReuseOrAllocNode(const _ReuseOrAllocNode&) = delete; 117 118 ~_ReuseOrAllocNode() 119 { _M_h._M_deallocate_nodes(_M_nodes); } 120 121 template
122 __node_type* 123 operator()(_Arg&& __arg) const 124 { 125 if (_M_nodes) 126 { 127 __node_type* __node = _M_nodes; 128 _M_nodes = _M_nodes->_M_next(); 129 __node->_M_nxt = nullptr; 130 auto& __a = _M_h._M_node_allocator(); 131 __node_alloc_traits::destroy(__a, __node->_M_valptr()); 132 __try 133 { 134 __node_alloc_traits::construct(__a, __node->_M_valptr(), 135 std::forward<_Arg>(__arg)); 136 } 137 __catch(...) 138 { 139 _M_h._M_deallocate_node_ptr(__node); 140 __throw_exception_again; 141 } 142 return __node; 143 } 144 return _M_h._M_allocate_node(std::forward<_Arg>(__arg)); 145 } 146 147 private: 148 mutable __node_type* _M_nodes; 149 __hashtable_alloc& _M_h; 150 }; 151 152 // Functor similar to the previous one but without any pool of nodes to 153 // recycle. 154 template
155 struct _AllocNode 156 { 157 private: 158 using __hashtable_alloc = _Hashtable_alloc<_NodeAlloc>; 159 using __node_type = typename __hashtable_alloc::__node_type; 160 161 public: 162 _AllocNode(__hashtable_alloc& __h) 163 : _M_h(__h) { } 164 165 template
166 __node_type* 167 operator()(_Arg&& __arg) const 168 { return _M_h._M_allocate_node(std::forward<_Arg>(__arg)); } 169 170 private: 171 __hashtable_alloc& _M_h; 172 }; 173 174 // Auxiliary types used for all instantiations of _Hashtable nodes 175 // and iterators. 176 177 /** 178 * struct _Hashtable_traits 179 * 180 * Important traits for hash tables. 181 * 182 * @tparam _Cache_hash_code Boolean value. True if the value of 183 * the hash function is stored along with the value. This is a 184 * time-space tradeoff. Storing it may improve lookup speed by 185 * reducing the number of times we need to call the _Hash or _Equal 186 * functors. 187 * 188 * @tparam _Constant_iterators Boolean value. True if iterator and 189 * const_iterator are both constant iterator types. This is true 190 * for unordered_set and unordered_multiset, false for 191 * unordered_map and unordered_multimap. 192 * 193 * @tparam _Unique_keys Boolean value. True if the return value 194 * of _Hashtable::count(k) is always at most one, false if it may 195 * be an arbitrary number. This is true for unordered_set and 196 * unordered_map, false for unordered_multiset and 197 * unordered_multimap. 198 */ 199 template
200 struct _Hashtable_traits 201 { 202 using __hash_cached = __bool_constant<_Cache_hash_code>; 203 using __constant_iterators = __bool_constant<_Constant_iterators>; 204 using __unique_keys = __bool_constant<_Unique_keys>; 205 }; 206 207 /** 208 * struct _Hash_node_base 209 * 210 * Nodes, used to wrap elements stored in the hash table. A policy 211 * template parameter of class template _Hashtable controls whether 212 * nodes also store a hash code. In some cases (e.g. strings) this 213 * may be a performance win. 214 */ 215 struct _Hash_node_base 216 { 217 _Hash_node_base* _M_nxt; 218 219 _Hash_node_base() noexcept : _M_nxt() { } 220 221 _Hash_node_base(_Hash_node_base* __next) noexcept : _M_nxt(__next) { } 222 }; 223 224 /** 225 * struct _Hash_node_value_base 226 * 227 * Node type with the value to store. 228 */ 229 template
230 struct _Hash_node_value_base 231 { 232 typedef _Value value_type; 233 234 __gnu_cxx::__aligned_buffer<_Value> _M_storage; 235 236 [[__gnu__::__always_inline__]] 237 _Value* 238 _M_valptr() noexcept 239 { return _M_storage._M_ptr(); } 240 241 [[__gnu__::__always_inline__]] 242 const _Value* 243 _M_valptr() const noexcept 244 { return _M_storage._M_ptr(); } 245 246 [[__gnu__::__always_inline__]] 247 _Value& 248 _M_v() noexcept 249 { return *_M_valptr(); } 250 251 [[__gnu__::__always_inline__]] 252 const _Value& 253 _M_v() const noexcept 254 { return *_M_valptr(); } 255 }; 256 257 /** 258 * Primary template struct _Hash_node_code_cache. 259 */ 260 template
261 struct _Hash_node_code_cache 262 { }; 263 264 /** 265 * Specialization for node with cache, struct _Hash_node_code_cache. 266 */ 267 template<> 268 struct _Hash_node_code_cache
269 { std::size_t _M_hash_code; }; 270 271 template
272 struct _Hash_node_value 273 : _Hash_node_value_base<_Value> 274 , _Hash_node_code_cache<_Cache_hash_code> 275 { }; 276 277 /** 278 * Primary template struct _Hash_node. 279 */ 280 template
281 struct _Hash_node 282 : _Hash_node_base 283 , _Hash_node_value<_Value, _Cache_hash_code> 284 { 285 _Hash_node* 286 _M_next() const noexcept 287 { return static_cast<_Hash_node*>(this->_M_nxt); } 288 }; 289 290 /// Base class for node iterators. 291 template
292 struct _Node_iterator_base 293 { 294 using __node_type = _Hash_node<_Value, _Cache_hash_code>; 295 296 __node_type* _M_cur; 297 298 _Node_iterator_base() : _M_cur(nullptr) { } 299 _Node_iterator_base(__node_type* __p) noexcept 300 : _M_cur(__p) { } 301 302 void 303 _M_incr() noexcept 304 { _M_cur = _M_cur->_M_next(); } 305 306 friend bool 307 operator==(const _Node_iterator_base& __x, const _Node_iterator_base& __y) 308 noexcept 309 { return __x._M_cur == __y._M_cur; } 310 311 #if __cpp_impl_three_way_comparison < 201907L 312 friend bool 313 operator!=(const _Node_iterator_base& __x, const _Node_iterator_base& __y) 314 noexcept 315 { return __x._M_cur != __y._M_cur; } 316 #endif 317 }; 318 319 /// Node iterators, used to iterate through all the hashtable. 320 template
321 struct _Node_iterator 322 : public _Node_iterator_base<_Value, __cache> 323 { 324 private: 325 using __base_type = _Node_iterator_base<_Value, __cache>; 326 using __node_type = typename __base_type::__node_type; 327 328 public: 329 typedef _Value value_type; 330 typedef std::ptrdiff_t difference_type; 331 typedef std::forward_iterator_tag iterator_category; 332 333 using pointer = typename std::conditional<__constant_iterators, 334 const value_type*, value_type*>::type; 335 336 using reference = typename std::conditional<__constant_iterators, 337 const value_type&, value_type&>::type; 338 339 _Node_iterator() = default; 340 341 explicit 342 _Node_iterator(__node_type* __p) noexcept 343 : __base_type(__p) { } 344 345 reference 346 operator*() const noexcept 347 { return this->_M_cur->_M_v(); } 348 349 pointer 350 operator->() const noexcept 351 { return this->_M_cur->_M_valptr(); } 352 353 _Node_iterator& 354 operator++() noexcept 355 { 356 this->_M_incr(); 357 return *this; 358 } 359 360 _Node_iterator 361 operator++(int) noexcept 362 { 363 _Node_iterator __tmp(*this); 364 this->_M_incr(); 365 return __tmp; 366 } 367 }; 368 369 /// Node const_iterators, used to iterate through all the hashtable. 370 template
371 struct _Node_const_iterator 372 : public _Node_iterator_base<_Value, __cache> 373 { 374 private: 375 using __base_type = _Node_iterator_base<_Value, __cache>; 376 using __node_type = typename __base_type::__node_type; 377 378 public: 379 typedef _Value value_type; 380 typedef std::ptrdiff_t difference_type; 381 typedef std::forward_iterator_tag iterator_category; 382 383 typedef const value_type* pointer; 384 typedef const value_type& reference; 385 386 _Node_const_iterator() = default; 387 388 explicit 389 _Node_const_iterator(__node_type* __p) noexcept 390 : __base_type(__p) { } 391 392 _Node_const_iterator(const _Node_iterator<_Value, __constant_iterators, 393 __cache>& __x) noexcept 394 : __base_type(__x._M_cur) { } 395 396 reference 397 operator*() const noexcept 398 { return this->_M_cur->_M_v(); } 399 400 pointer 401 operator->() const noexcept 402 { return this->_M_cur->_M_valptr(); } 403 404 _Node_const_iterator& 405 operator++() noexcept 406 { 407 this->_M_incr(); 408 return *this; 409 } 410 411 _Node_const_iterator 412 operator++(int) noexcept 413 { 414 _Node_const_iterator __tmp(*this); 415 this->_M_incr(); 416 return __tmp; 417 } 418 }; 419 420 // Many of class template _Hashtable's template parameters are policy 421 // classes. These are defaults for the policies. 422 423 /// Default range hashing function: use division to fold a large number 424 /// into the range [0, N). 425 struct _Mod_range_hashing 426 { 427 typedef std::size_t first_argument_type; 428 typedef std::size_t second_argument_type; 429 typedef std::size_t result_type; 430 431 result_type 432 operator()(first_argument_type __num, 433 second_argument_type __den) const noexcept 434 { return __num % __den; } 435 }; 436 437 /// Default ranged hash function H. In principle it should be a 438 /// function object composed from objects of type H1 and H2 such that 439 /// h(k, N) = h2(h1(k), N), but that would mean making extra copies of 440 /// h1 and h2. So instead we'll just use a tag to tell class template 441 /// hashtable to do that composition. 442 struct _Default_ranged_hash { }; 443 444 /// Default value for rehash policy. Bucket size is (usually) the 445 /// smallest prime that keeps the load factor small enough. 446 struct _Prime_rehash_policy 447 { 448 using __has_load_factor = true_type; 449 450 _Prime_rehash_policy(float __z = 1.0) noexcept 451 : _M_max_load_factor(__z), _M_next_resize(0) { } 452 453 float 454 max_load_factor() const noexcept 455 { return _M_max_load_factor; } 456 457 // Return a bucket size no smaller than n. 458 std::size_t 459 _M_next_bkt(std::size_t __n) const; 460 461 // Return a bucket count appropriate for n elements 462 std::size_t 463 _M_bkt_for_elements(std::size_t __n) const 464 { return __builtin_ceil(__n / (double)_M_max_load_factor); } 465 466 // __n_bkt is current bucket count, __n_elt is current element count, 467 // and __n_ins is number of elements to be inserted. Do we need to 468 // increase bucket count? If so, return make_pair(true, n), where n 469 // is the new bucket count. If not, return make_pair(false, 0). 470 std::pair
471 _M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt, 472 std::size_t __n_ins) const; 473 474 typedef std::size_t _State; 475 476 _State 477 _M_state() const 478 { return _M_next_resize; } 479 480 void 481 _M_reset() noexcept 482 { _M_next_resize = 0; } 483 484 void 485 _M_reset(_State __state) 486 { _M_next_resize = __state; } 487 488 static const std::size_t _S_growth_factor = 2; 489 490 float _M_max_load_factor; 491 mutable std::size_t _M_next_resize; 492 }; 493 494 /// Range hashing function assuming that second arg is a power of 2. 495 struct _Mask_range_hashing 496 { 497 typedef std::size_t first_argument_type; 498 typedef std::size_t second_argument_type; 499 typedef std::size_t result_type; 500 501 result_type 502 operator()(first_argument_type __num, 503 second_argument_type __den) const noexcept 504 { return __num & (__den - 1); } 505 }; 506 507 /// Compute closest power of 2 not less than __n 508 inline std::size_t 509 __clp2(std::size_t __n) noexcept 510 { 511 using __gnu_cxx::__int_traits; 512 // Equivalent to return __n ? std::bit_ceil(__n) : 0; 513 if (__n < 2) 514 return __n; 515 const unsigned __lz = sizeof(size_t) > sizeof(long) 516 ? __builtin_clzll(__n - 1ull) 517 : __builtin_clzl(__n - 1ul); 518 // Doing two shifts avoids undefined behaviour when __lz == 0. 519 return (size_t(1) << (__int_traits
::__digits - __lz - 1)) << 1; 520 } 521 522 /// Rehash policy providing power of 2 bucket numbers. Avoids modulo 523 /// operations. 524 struct _Power2_rehash_policy 525 { 526 using __has_load_factor = true_type; 527 528 _Power2_rehash_policy(float __z = 1.0) noexcept 529 : _M_max_load_factor(__z), _M_next_resize(0) { } 530 531 float 532 max_load_factor() const noexcept 533 { return _M_max_load_factor; } 534 535 // Return a bucket size no smaller than n (as long as n is not above the 536 // highest power of 2). 537 std::size_t 538 _M_next_bkt(std::size_t __n) noexcept 539 { 540 if (__n == 0) 541 // Special case on container 1st initialization with 0 bucket count 542 // hint. We keep _M_next_resize to 0 to make sure that next time we 543 // want to add an element allocation will take place. 544 return 1; 545 546 const auto __max_width = std::min
(sizeof(size_t), 8); 547 const auto __max_bkt = size_t(1) << (__max_width * __CHAR_BIT__ - 1); 548 std::size_t __res = __clp2(__n); 549 550 if (__res == 0) 551 __res = __max_bkt; 552 else if (__res == 1) 553 // If __res is 1 we force it to 2 to make sure there will be an 554 // allocation so that nothing need to be stored in the initial 555 // single bucket 556 __res = 2; 557 558 if (__res == __max_bkt) 559 // Set next resize to the max value so that we never try to rehash again 560 // as we already reach the biggest possible bucket number. 561 // Note that it might result in max_load_factor not being respected. 562 _M_next_resize = size_t(-1); 563 else 564 _M_next_resize 565 = __builtin_floor(__res * (double)_M_max_load_factor); 566 567 return __res; 568 } 569 570 // Return a bucket count appropriate for n elements 571 std::size_t 572 _M_bkt_for_elements(std::size_t __n) const noexcept 573 { return __builtin_ceil(__n / (double)_M_max_load_factor); } 574 575 // __n_bkt is current bucket count, __n_elt is current element count, 576 // and __n_ins is number of elements to be inserted. Do we need to 577 // increase bucket count? If so, return make_pair(true, n), where n 578 // is the new bucket count. If not, return make_pair(false, 0). 579 std::pair
580 _M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt, 581 std::size_t __n_ins) noexcept 582 { 583 if (__n_elt + __n_ins > _M_next_resize) 584 { 585 // If _M_next_resize is 0 it means that we have nothing allocated so 586 // far and that we start inserting elements. In this case we start 587 // with an initial bucket size of 11. 588 double __min_bkts 589 = std::max
(__n_elt + __n_ins, _M_next_resize ? 0 : 11) 590 / (double)_M_max_load_factor; 591 if (__min_bkts >= __n_bkt) 592 return { true, 593 _M_next_bkt(std::max
(__builtin_floor(__min_bkts) + 1, 594 __n_bkt * _S_growth_factor)) }; 595 596 _M_next_resize 597 = __builtin_floor(__n_bkt * (double)_M_max_load_factor); 598 return { false, 0 }; 599 } 600 else 601 return { false, 0 }; 602 } 603 604 typedef std::size_t _State; 605 606 _State 607 _M_state() const noexcept 608 { return _M_next_resize; } 609 610 void 611 _M_reset() noexcept 612 { _M_next_resize = 0; } 613 614 void 615 _M_reset(_State __state) noexcept 616 { _M_next_resize = __state; } 617 618 static const std::size_t _S_growth_factor = 2; 619 620 float _M_max_load_factor; 621 std::size_t _M_next_resize; 622 }; 623 624 // Base classes for std::_Hashtable. We define these base classes 625 // because in some cases we want to do different things depending on 626 // the value of a policy class. In some cases the policy class 627 // affects which member functions and nested typedefs are defined; 628 // we handle that by specializing base class templates. Several of 629 // the base class templates need to access other members of class 630 // template _Hashtable, so we use a variant of the "Curiously 631 // Recurring Template Pattern" (CRTP) technique. 632 633 /** 634 * Primary class template _Map_base. 635 * 636 * If the hashtable has a value type of the form pair
and a 637 * key extraction policy (_ExtractKey) that returns the first part 638 * of the pair, the hashtable gets a mapped_type typedef. If it 639 * satisfies those criteria and also has unique keys, then it also 640 * gets an operator[]. 641 */ 642 template
647 struct _Map_base { }; 648 649 /// Partial specialization, __unique_keys set to false. 650 template
653 struct _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal, 654 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, false> 655 { 656 using mapped_type = typename std::tuple_element<1, _Pair>::type; 657 }; 658 659 /// Partial specialization, __unique_keys set to true. 660 template
663 struct _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal, 664 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true> 665 { 666 private: 667 using __hashtable_base = _Hashtable_base<_Key, _Pair, _Select1st, _Equal, 668 _Hash, _RangeHash, _Unused, 669 _Traits>; 670 671 using __hashtable = _Hashtable<_Key, _Pair, _Alloc, _Select1st, _Equal, 672 _Hash, _RangeHash, 673 _Unused, _RehashPolicy, _Traits>; 674 675 using __hash_code = typename __hashtable_base::__hash_code; 676 677 public: 678 using key_type = typename __hashtable_base::key_type; 679 using mapped_type = typename std::tuple_element<1, _Pair>::type; 680 681 mapped_type& 682 operator[](const key_type& __k); 683 684 mapped_type& 685 operator[](key_type&& __k); 686 687 // _GLIBCXX_RESOLVE_LIB_DEFECTS 688 // DR 761. unordered_map needs an at() member function. 689 mapped_type& 690 at(const key_type& __k); 691 692 const mapped_type& 693 at(const key_type& __k) const; 694 }; 695 696 template
699 auto 700 _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal, 701 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>:: 702 operator[](const key_type& __k) 703 -> mapped_type& 704 { 705 __hashtable* __h = static_cast<__hashtable*>(this); 706 __hash_code __code = __h->_M_hash_code(__k); 707 std::size_t __bkt = __h->_M_bucket_index(__code); 708 if (auto __node = __h->_M_find_node(__bkt, __k, __code)) 709 return __node->_M_v().second; 710 711 typename __hashtable::_Scoped_node __node { 712 __h, 713 std::piecewise_construct, 714 std::tuple
(__k), 715 std::tuple<>() 716 }; 717 auto __pos 718 = __h->_M_insert_unique_node(__bkt, __code, __node._M_node); 719 __node._M_node = nullptr; 720 return __pos->second; 721 } 722 723 template
726 auto 727 _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal, 728 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>:: 729 operator[](key_type&& __k) 730 -> mapped_type& 731 { 732 __hashtable* __h = static_cast<__hashtable*>(this); 733 __hash_code __code = __h->_M_hash_code(__k); 734 std::size_t __bkt = __h->_M_bucket_index(__code); 735 if (auto __node = __h->_M_find_node(__bkt, __k, __code)) 736 return __node->_M_v().second; 737 738 typename __hashtable::_Scoped_node __node { 739 __h, 740 std::piecewise_construct, 741 std::forward_as_tuple(std::move(__k)), 742 std::tuple<>() 743 }; 744 auto __pos 745 = __h->_M_insert_unique_node(__bkt, __code, __node._M_node); 746 __node._M_node = nullptr; 747 return __pos->second; 748 } 749 750 template
753 auto 754 _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal, 755 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>:: 756 at(const key_type& __k) 757 -> mapped_type& 758 { 759 __hashtable* __h = static_cast<__hashtable*>(this); 760 auto __ite = __h->find(__k); 761 762 if (!__ite._M_cur) 763 __throw_out_of_range(__N("_Map_base::at")); 764 return __ite->second; 765 } 766 767 template
770 auto 771 _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal, 772 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>:: 773 at(const key_type& __k) const 774 -> const mapped_type& 775 { 776 const __hashtable* __h = static_cast
(this); 777 auto __ite = __h->find(__k); 778 779 if (!__ite._M_cur) 780 __throw_out_of_range(__N("_Map_base::at")); 781 return __ite->second; 782 } 783 784 /** 785 * Primary class template _Insert_base. 786 * 787 * Defines @c insert member functions appropriate to all _Hashtables. 788 */ 789 template
793 struct _Insert_base 794 { 795 protected: 796 using __hashtable_base = _Hashtable_base<_Key, _Value, _ExtractKey, 797 _Equal, _Hash, _RangeHash, 798 _Unused, _Traits>; 799 800 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, 801 _Hash, _RangeHash, 802 _Unused, _RehashPolicy, _Traits>; 803 804 using __hash_cached = typename _Traits::__hash_cached; 805 using __constant_iterators = typename _Traits::__constant_iterators; 806 807 using __hashtable_alloc = _Hashtable_alloc< 808 __alloc_rebind<_Alloc, _Hash_node<_Value, 809 __hash_cached::value>>>; 810 811 using value_type = typename __hashtable_base::value_type; 812 using size_type = typename __hashtable_base::size_type; 813 814 using __unique_keys = typename _Traits::__unique_keys; 815 using __node_alloc_type = typename __hashtable_alloc::__node_alloc_type; 816 using __node_gen_type = _AllocNode<__node_alloc_type>; 817 818 __hashtable& 819 _M_conjure_hashtable() 820 { return *(static_cast<__hashtable*>(this)); } 821 822 template
823 void 824 _M_insert_range(_InputIterator __first, _InputIterator __last, 825 const _NodeGetter&, true_type __uks); 826 827 template
828 void 829 _M_insert_range(_InputIterator __first, _InputIterator __last, 830 const _NodeGetter&, false_type __uks); 831 832 public: 833 using iterator = _Node_iterator<_Value, __constant_iterators::value, 834 __hash_cached::value>; 835 836 using const_iterator = _Node_const_iterator<_Value, __constant_iterators::value, 837 __hash_cached::value>; 838 839 using __ireturn_type = typename std::conditional<__unique_keys::value, 840 std::pair
, 841 iterator>::type; 842 843 __ireturn_type 844 insert(const value_type& __v) 845 { 846 __hashtable& __h = _M_conjure_hashtable(); 847 __node_gen_type __node_gen(__h); 848 return __h._M_insert(__v, __node_gen, __unique_keys{}); 849 } 850 851 iterator 852 insert(const_iterator __hint, const value_type& __v) 853 { 854 __hashtable& __h = _M_conjure_hashtable(); 855 __node_gen_type __node_gen(__h); 856 return __h._M_insert(__hint, __v, __node_gen, __unique_keys{}); 857 } 858 859 template
860 std::pair
861 try_emplace(const_iterator, _KType&& __k, _Args&&... __args) 862 { 863 __hashtable& __h = _M_conjure_hashtable(); 864 auto __code = __h._M_hash_code(__k); 865 std::size_t __bkt = __h._M_bucket_index(__code); 866 if (auto __node = __h._M_find_node(__bkt, __k, __code)) 867 return { iterator(__node), false }; 868 869 typename __hashtable::_Scoped_node __node { 870 &__h, 871 std::piecewise_construct, 872 std::forward_as_tuple(std::forward<_KType>(__k)), 873 std::forward_as_tuple(std::forward<_Args>(__args)...) 874 }; 875 auto __it 876 = __h._M_insert_unique_node(__bkt, __code, __node._M_node); 877 __node._M_node = nullptr; 878 return { __it, true }; 879 } 880 881 void 882 insert(initializer_list
__l) 883 { this->insert(__l.begin(), __l.end()); } 884 885 template
886 void 887 insert(_InputIterator __first, _InputIterator __last) 888 { 889 __hashtable& __h = _M_conjure_hashtable(); 890 __node_gen_type __node_gen(__h); 891 return _M_insert_range(__first, __last, __node_gen, __unique_keys{}); 892 } 893 }; 894 895 template
899 template
900 void 901 _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal, 902 _Hash, _RangeHash, _Unused, 903 _RehashPolicy, _Traits>:: 904 _M_insert_range(_InputIterator __first, _InputIterator __last, 905 const _NodeGetter& __node_gen, true_type __uks) 906 { 907 __hashtable& __h = _M_conjure_hashtable(); 908 for (; __first != __last; ++__first) 909 __h._M_insert(*__first, __node_gen, __uks); 910 } 911 912 template
916 template
917 void 918 _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal, 919 _Hash, _RangeHash, _Unused, 920 _RehashPolicy, _Traits>:: 921 _M_insert_range(_InputIterator __first, _InputIterator __last, 922 const _NodeGetter& __node_gen, false_type __uks) 923 { 924 using __rehash_type = typename __hashtable::__rehash_type; 925 using __rehash_state = typename __hashtable::__rehash_state; 926 using pair_type = std::pair
; 927 928 size_type __n_elt = __detail::__distance_fw(__first, __last); 929 if (__n_elt == 0) 930 return; 931 932 __hashtable& __h = _M_conjure_hashtable(); 933 __rehash_type& __rehash = __h._M_rehash_policy; 934 const __rehash_state& __saved_state = __rehash._M_state(); 935 pair_type __do_rehash = __rehash._M_need_rehash(__h._M_bucket_count, 936 __h._M_element_count, 937 __n_elt); 938 939 if (__do_rehash.first) 940 __h._M_rehash(__do_rehash.second, __saved_state); 941 942 for (; __first != __last; ++__first) 943 __h._M_insert(*__first, __node_gen, __uks); 944 } 945 946 /** 947 * Primary class template _Insert. 948 * 949 * Defines @c insert member functions that depend on _Hashtable policies, 950 * via partial specializations. 951 */ 952 template
957 struct _Insert; 958 959 /// Specialization. 960 template
964 struct _Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal, 965 _Hash, _RangeHash, _Unused, 966 _RehashPolicy, _Traits, true> 967 : public _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal, 968 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits> 969 { 970 using __base_type = _Insert_base<_Key, _Value, _Alloc, _ExtractKey, 971 _Equal, _Hash, _RangeHash, _Unused, 972 _RehashPolicy, _Traits>; 973 974 using value_type = typename __base_type::value_type; 975 using iterator = typename __base_type::iterator; 976 using const_iterator = typename __base_type::const_iterator; 977 using __ireturn_type = typename __base_type::__ireturn_type; 978 979 using __unique_keys = typename __base_type::__unique_keys; 980 using __hashtable = typename __base_type::__hashtable; 981 using __node_gen_type = typename __base_type::__node_gen_type; 982 983 using __base_type::insert; 984 985 __ireturn_type 986 insert(value_type&& __v) 987 { 988 __hashtable& __h = this->_M_conjure_hashtable(); 989 __node_gen_type __node_gen(__h); 990 return __h._M_insert(std::move(__v), __node_gen, __unique_keys{}); 991 } 992 993 iterator 994 insert(const_iterator __hint, value_type&& __v) 995 { 996 __hashtable& __h = this->_M_conjure_hashtable(); 997 __node_gen_type __node_gen(__h); 998 return __h._M_insert(__hint, std::move(__v), __node_gen, 999 __unique_keys{}); 1000 } 1001 }; 1002 1003 /// Specialization. 1004 template
1008 struct _Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal, 1009 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, false> 1010 : public _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal, 1011 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits> 1012 { 1013 using __base_type = _Insert_base<_Key, _Value, _Alloc, _ExtractKey, 1014 _Equal, _Hash, _RangeHash, _Unused, 1015 _RehashPolicy, _Traits>; 1016 using value_type = typename __base_type::value_type; 1017 using iterator = typename __base_type::iterator; 1018 using const_iterator = typename __base_type::const_iterator; 1019 1020 using __unique_keys = typename __base_type::__unique_keys; 1021 using __hashtable = typename __base_type::__hashtable; 1022 using __ireturn_type = typename __base_type::__ireturn_type; 1023 1024 using __base_type::insert; 1025 1026 template
1027 using __is_cons = std::is_constructible
; 1028 1029 template
1030 using _IFcons = std::enable_if<__is_cons<_Pair>::value>; 1031 1032 template
1033 using _IFconsp = typename _IFcons<_Pair>::type; 1034 1035 template
> 1036 __ireturn_type 1037 insert(_Pair&& __v) 1038 { 1039 __hashtable& __h = this->_M_conjure_hashtable(); 1040 return __h._M_emplace(__unique_keys{}, std::forward<_Pair>(__v)); 1041 } 1042 1043 template
> 1044 iterator 1045 insert(const_iterator __hint, _Pair&& __v) 1046 { 1047 __hashtable& __h = this->_M_conjure_hashtable(); 1048 return __h._M_emplace(__hint, __unique_keys{}, 1049 std::forward<_Pair>(__v)); 1050 } 1051 }; 1052 1053 template
1054 using __has_load_factor = typename _Policy::__has_load_factor; 1055 1056 /** 1057 * Primary class template _Rehash_base. 1058 * 1059 * Give hashtable the max_load_factor functions and reserve iff the 1060 * rehash policy supports it. 1061 */ 1062 template
> 1068 struct _Rehash_base; 1069 1070 /// Specialization when rehash policy doesn't provide load factor management. 1071 template
1075 struct _Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal, 1076 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, 1077 false_type /* Has load factor */> 1078 { 1079 }; 1080 1081 /// Specialization when rehash policy provide load factor management. 1082 template
1086 struct _Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal, 1087 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, 1088 true_type /* Has load factor */> 1089 { 1090 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, 1091 _Equal, _Hash, _RangeHash, _Unused, 1092 _RehashPolicy, _Traits>; 1093 1094 float 1095 max_load_factor() const noexcept 1096 { 1097 const __hashtable* __this = static_cast
(this); 1098 return __this->__rehash_policy().max_load_factor(); 1099 } 1100 1101 void 1102 max_load_factor(float __z) 1103 { 1104 __hashtable* __this = static_cast<__hashtable*>(this); 1105 __this->__rehash_policy(_RehashPolicy(__z)); 1106 } 1107 1108 void 1109 reserve(std::size_t __n) 1110 { 1111 __hashtable* __this = static_cast<__hashtable*>(this); 1112 __this->rehash(__this->__rehash_policy()._M_bkt_for_elements(__n)); 1113 } 1114 }; 1115 1116 /** 1117 * Primary class template _Hashtable_ebo_helper. 1118 * 1119 * Helper class using EBO when it is not forbidden (the type is not 1120 * final) and when it is worth it (the type is empty.) 1121 */ 1122 template
1124 struct _Hashtable_ebo_helper; 1125 1126 /// Specialization using EBO. 1127 template
1128 struct _Hashtable_ebo_helper<_Nm, _Tp, true> 1129 : private _Tp 1130 { 1131 _Hashtable_ebo_helper() noexcept(noexcept(_Tp())) : _Tp() { } 1132 1133 template
1134 _Hashtable_ebo_helper(_OtherTp&& __tp) 1135 : _Tp(std::forward<_OtherTp>(__tp)) 1136 { } 1137 1138 const _Tp& _M_cget() const { return static_cast
(*this); } 1139 _Tp& _M_get() { return static_cast<_Tp&>(*this); } 1140 }; 1141 1142 /// Specialization not using EBO. 1143 template
1144 struct _Hashtable_ebo_helper<_Nm, _Tp, false> 1145 { 1146 _Hashtable_ebo_helper() = default; 1147 1148 template
1149 _Hashtable_ebo_helper(_OtherTp&& __tp) 1150 : _M_tp(std::forward<_OtherTp>(__tp)) 1151 { } 1152 1153 const _Tp& _M_cget() const { return _M_tp; } 1154 _Tp& _M_get() { return _M_tp; } 1155 1156 private: 1157 _Tp _M_tp{}; 1158 }; 1159 1160 /** 1161 * Primary class template _Local_iterator_base. 1162 * 1163 * Base class for local iterators, used to iterate within a bucket 1164 * but not between buckets. 1165 */ 1166 template
1169 struct _Local_iterator_base; 1170 1171 /** 1172 * Primary class template _Hash_code_base. 1173 * 1174 * Encapsulates two policy issues that aren't quite orthogonal. 1175 * (1) the difference between using a ranged hash function and using 1176 * the combination of a hash function and a range-hashing function. 1177 * In the former case we don't have such things as hash codes, so 1178 * we have a dummy type as placeholder. 1179 * (2) Whether or not we cache hash codes. Caching hash codes is 1180 * meaningless if we have a ranged hash function. 1181 * 1182 * We also put the key extraction objects here, for convenience. 1183 * Each specialization derives from one or more of the template 1184 * parameters to benefit from Ebo. This is important as this type 1185 * is inherited in some cases by the _Local_iterator_base type used 1186 * to implement local_iterator and const_local_iterator. As with 1187 * any iterator type we prefer to make it as small as possible. 1188 */ 1189 template
1192 struct _Hash_code_base 1193 : private _Hashtable_ebo_helper<1, _Hash> 1194 { 1195 private: 1196 using __ebo_hash = _Hashtable_ebo_helper<1, _Hash>; 1197 1198 // Gives the local iterator implementation access to _M_bucket_index(). 1199 friend struct _Local_iterator_base<_Key, _Value, _ExtractKey, 1200 _Hash, _RangeHash, _Unused, false>; 1201 1202 public: 1203 typedef _Hash hasher; 1204 1205 hasher 1206 hash_function() const 1207 { return _M_hash(); } 1208 1209 protected: 1210 typedef std::size_t __hash_code; 1211 1212 // We need the default constructor for the local iterators and _Hashtable 1213 // default constructor. 1214 _Hash_code_base() = default; 1215 1216 _Hash_code_base(const _Hash& __hash) : __ebo_hash(__hash) { } 1217 1218 __hash_code 1219 _M_hash_code(const _Key& __k) const 1220 { 1221 static_assert(__is_invocable
{}, 1222 "hash function must be invocable with an argument of key type"); 1223 return _M_hash()(__k); 1224 } 1225 1226 template
1227 __hash_code 1228 _M_hash_code_tr(const _Kt& __k) const 1229 { 1230 static_assert(__is_invocable
{}, 1231 "hash function must be invocable with an argument of key type"); 1232 return _M_hash()(__k); 1233 } 1234 1235 std::size_t 1236 _M_bucket_index(__hash_code __c, std::size_t __bkt_count) const 1237 { return _RangeHash{}(__c, __bkt_count); } 1238 1239 std::size_t 1240 _M_bucket_index(const _Hash_node_value<_Value, false>& __n, 1241 std::size_t __bkt_count) const 1242 noexcept( noexcept(declval
()(declval
())) 1243 && noexcept(declval
()((__hash_code)0, 1244 (std::size_t)0)) ) 1245 { 1246 return _RangeHash{}(_M_hash_code(_ExtractKey{}(__n._M_v())), 1247 __bkt_count); 1248 } 1249 1250 std::size_t 1251 _M_bucket_index(const _Hash_node_value<_Value, true>& __n, 1252 std::size_t __bkt_count) const 1253 noexcept( noexcept(declval
()((__hash_code)0, 1254 (std::size_t)0)) ) 1255 { return _RangeHash{}(__n._M_hash_code, __bkt_count); } 1256 1257 void 1258 _M_store_code(_Hash_node_code_cache
&, __hash_code) const 1259 { } 1260 1261 void 1262 _M_copy_code(_Hash_node_code_cache
&, 1263 const _Hash_node_code_cache
&) const 1264 { } 1265 1266 void 1267 _M_store_code(_Hash_node_code_cache
& __n, __hash_code __c) const 1268 { __n._M_hash_code = __c; } 1269 1270 void 1271 _M_copy_code(_Hash_node_code_cache
& __to, 1272 const _Hash_node_code_cache
& __from) const 1273 { __to._M_hash_code = __from._M_hash_code; } 1274 1275 void 1276 _M_swap(_Hash_code_base& __x) 1277 { std::swap(__ebo_hash::_M_get(), __x.__ebo_hash::_M_get()); } 1278 1279 const _Hash& 1280 _M_hash() const { return __ebo_hash::_M_cget(); } 1281 }; 1282 1283 /// Partial specialization used when nodes contain a cached hash code. 1284 template
1286 struct _Local_iterator_base<_Key, _Value, _ExtractKey, 1287 _Hash, _RangeHash, _Unused, true> 1288 : public _Node_iterator_base<_Value, true> 1289 { 1290 protected: 1291 using __base_node_iter = _Node_iterator_base<_Value, true>; 1292 using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey, 1293 _Hash, _RangeHash, _Unused, true>; 1294 1295 _Local_iterator_base() = default; 1296 _Local_iterator_base(const __hash_code_base&, 1297 _Hash_node<_Value, true>* __p, 1298 std::size_t __bkt, std::size_t __bkt_count) 1299 : __base_node_iter(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count) 1300 { } 1301 1302 void 1303 _M_incr() 1304 { 1305 __base_node_iter::_M_incr(); 1306 if (this->_M_cur) 1307 { 1308 std::size_t __bkt 1309 = _RangeHash{}(this->_M_cur->_M_hash_code, _M_bucket_count); 1310 if (__bkt != _M_bucket) 1311 this->_M_cur = nullptr; 1312 } 1313 } 1314 1315 std::size_t _M_bucket; 1316 std::size_t _M_bucket_count; 1317 1318 public: 1319 std::size_t 1320 _M_get_bucket() const { return _M_bucket; } // for debug mode 1321 }; 1322 1323 // Uninitialized storage for a _Hash_code_base. 1324 // This type is DefaultConstructible and Assignable even if the 1325 // _Hash_code_base type isn't, so that _Local_iterator_base<..., false> 1326 // can be DefaultConstructible and Assignable. 1327 template
::value> 1328 struct _Hash_code_storage 1329 { 1330 __gnu_cxx::__aligned_buffer<_Tp> _M_storage; 1331 1332 _Tp* 1333 _M_h() { return _M_storage._M_ptr(); } 1334 1335 const _Tp* 1336 _M_h() const { return _M_storage._M_ptr(); } 1337 }; 1338 1339 // Empty partial specialization for empty _Hash_code_base types. 1340 template
1341 struct _Hash_code_storage<_Tp, true> 1342 { 1343 static_assert( std::is_empty<_Tp>::value, "Type must be empty" ); 1344 1345 // As _Tp is an empty type there will be no bytes written/read through 1346 // the cast pointer, so no strict-aliasing violation. 1347 _Tp* 1348 _M_h() { return reinterpret_cast<_Tp*>(this); } 1349 1350 const _Tp* 1351 _M_h() const { return reinterpret_cast
(this); } 1352 }; 1353 1354 template
1356 using __hash_code_for_local_iter 1357 = _Hash_code_storage<_Hash_code_base<_Key, _Value, _ExtractKey, 1358 _Hash, _RangeHash, _Unused, false>>; 1359 1360 // Partial specialization used when hash codes are not cached 1361 template
1363 struct _Local_iterator_base<_Key, _Value, _ExtractKey, 1364 _Hash, _RangeHash, _Unused, false> 1365 : __hash_code_for_local_iter<_Key, _Value, _ExtractKey, _Hash, _RangeHash, 1366 _Unused> 1367 , _Node_iterator_base<_Value, false> 1368 { 1369 protected: 1370 using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey, 1371 _Hash, _RangeHash, _Unused, false>; 1372 using __node_iter_base = _Node_iterator_base<_Value, false>; 1373 1374 _Local_iterator_base() : _M_bucket_count(-1) { } 1375 1376 _Local_iterator_base(const __hash_code_base& __base, 1377 _Hash_node<_Value, false>* __p, 1378 std::size_t __bkt, std::size_t __bkt_count) 1379 : __node_iter_base(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count) 1380 { _M_init(__base); } 1381 1382 ~_Local_iterator_base() 1383 { 1384 if (_M_bucket_count != size_t(-1)) 1385 _M_destroy(); 1386 } 1387 1388 _Local_iterator_base(const _Local_iterator_base& __iter) 1389 : __node_iter_base(__iter._M_cur), _M_bucket(__iter._M_bucket) 1390 , _M_bucket_count(__iter._M_bucket_count) 1391 { 1392 if (_M_bucket_count != size_t(-1)) 1393 _M_init(*__iter._M_h()); 1394 } 1395 1396 _Local_iterator_base& 1397 operator=(const _Local_iterator_base& __iter) 1398 { 1399 if (_M_bucket_count != -1) 1400 _M_destroy(); 1401 this->_M_cur = __iter._M_cur; 1402 _M_bucket = __iter._M_bucket; 1403 _M_bucket_count = __iter._M_bucket_count; 1404 if (_M_bucket_count != -1) 1405 _M_init(*__iter._M_h()); 1406 return *this; 1407 } 1408 1409 void 1410 _M_incr() 1411 { 1412 __node_iter_base::_M_incr(); 1413 if (this->_M_cur) 1414 { 1415 std::size_t __bkt = this->_M_h()->_M_bucket_index(*this->_M_cur, 1416 _M_bucket_count); 1417 if (__bkt != _M_bucket) 1418 this->_M_cur = nullptr; 1419 } 1420 } 1421 1422 std::size_t _M_bucket; 1423 std::size_t _M_bucket_count; 1424 1425 void 1426 _M_init(const __hash_code_base& __base) 1427 { ::new(this->_M_h()) __hash_code_base(__base); } 1428 1429 void 1430 _M_destroy() { this->_M_h()->~__hash_code_base(); } 1431 1432 public: 1433 std::size_t 1434 _M_get_bucket() const { return _M_bucket; } // for debug mode 1435 }; 1436 1437 /// local iterators 1438 template
1441 struct _Local_iterator 1442 : public _Local_iterator_base<_Key, _Value, _ExtractKey, 1443 _Hash, _RangeHash, _Unused, __cache> 1444 { 1445 private: 1446 using __base_type = _Local_iterator_base<_Key, _Value, _ExtractKey, 1447 _Hash, _RangeHash, _Unused, __cache>; 1448 using __hash_code_base = typename __base_type::__hash_code_base; 1449 1450 public: 1451 typedef _Value value_type; 1452 typedef typename std::conditional<__constant_iterators, 1453 const value_type*, value_type*>::type 1454 pointer; 1455 typedef typename std::conditional<__constant_iterators, 1456 const value_type&, value_type&>::type 1457 reference; 1458 typedef std::ptrdiff_t difference_type; 1459 typedef std::forward_iterator_tag iterator_category; 1460 1461 _Local_iterator() = default; 1462 1463 _Local_iterator(const __hash_code_base& __base, 1464 _Hash_node<_Value, __cache>* __n, 1465 std::size_t __bkt, std::size_t __bkt_count) 1466 : __base_type(__base, __n, __bkt, __bkt_count) 1467 { } 1468 1469 reference 1470 operator*() const 1471 { return this->_M_cur->_M_v(); } 1472 1473 pointer 1474 operator->() const 1475 { return this->_M_cur->_M_valptr(); } 1476 1477 _Local_iterator& 1478 operator++() 1479 { 1480 this->_M_incr(); 1481 return *this; 1482 } 1483 1484 _Local_iterator 1485 operator++(int) 1486 { 1487 _Local_iterator __tmp(*this); 1488 this->_M_incr(); 1489 return __tmp; 1490 } 1491 }; 1492 1493 /// local const_iterators 1494 template
1497 struct _Local_const_iterator 1498 : public _Local_iterator_base<_Key, _Value, _ExtractKey, 1499 _Hash, _RangeHash, _Unused, __cache> 1500 { 1501 private: 1502 using __base_type = _Local_iterator_base<_Key, _Value, _ExtractKey, 1503 _Hash, _RangeHash, _Unused, __cache>; 1504 using __hash_code_base = typename __base_type::__hash_code_base; 1505 1506 public: 1507 typedef _Value value_type; 1508 typedef const value_type* pointer; 1509 typedef const value_type& reference; 1510 typedef std::ptrdiff_t difference_type; 1511 typedef std::forward_iterator_tag iterator_category; 1512 1513 _Local_const_iterator() = default; 1514 1515 _Local_const_iterator(const __hash_code_base& __base, 1516 _Hash_node<_Value, __cache>* __n, 1517 std::size_t __bkt, std::size_t __bkt_count) 1518 : __base_type(__base, __n, __bkt, __bkt_count) 1519 { } 1520 1521 _Local_const_iterator(const _Local_iterator<_Key, _Value, _ExtractKey, 1522 _Hash, _RangeHash, _Unused, 1523 __constant_iterators, 1524 __cache>& __x) 1525 : __base_type(__x) 1526 { } 1527 1528 reference 1529 operator*() const 1530 { return this->_M_cur->_M_v(); } 1531 1532 pointer 1533 operator->() const 1534 { return this->_M_cur->_M_valptr(); } 1535 1536 _Local_const_iterator& 1537 operator++() 1538 { 1539 this->_M_incr(); 1540 return *this; 1541 } 1542 1543 _Local_const_iterator 1544 operator++(int) 1545 { 1546 _Local_const_iterator __tmp(*this); 1547 this->_M_incr(); 1548 return __tmp; 1549 } 1550 }; 1551 1552 /** 1553 * Primary class template _Hashtable_base. 1554 * 1555 * Helper class adding management of _Equal functor to 1556 * _Hash_code_base type. 1557 * 1558 * Base class templates are: 1559 * - __detail::_Hash_code_base 1560 * - __detail::_Hashtable_ebo_helper 1561 */ 1562 template
1565 struct _Hashtable_base 1566 : public _Hash_code_base<_Key, _Value, _ExtractKey, _Hash, _RangeHash, 1567 _Unused, _Traits::__hash_cached::value>, 1568 private _Hashtable_ebo_helper<0, _Equal> 1569 { 1570 public: 1571 typedef _Key key_type; 1572 typedef _Value value_type; 1573 typedef _Equal key_equal; 1574 typedef std::size_t size_type; 1575 typedef std::ptrdiff_t difference_type; 1576 1577 using __traits_type = _Traits; 1578 using __hash_cached = typename __traits_type::__hash_cached; 1579 1580 using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey, 1581 _Hash, _RangeHash, _Unused, 1582 __hash_cached::value>; 1583 1584 using __hash_code = typename __hash_code_base::__hash_code; 1585 1586 private: 1587 using _EqualEBO = _Hashtable_ebo_helper<0, _Equal>; 1588 1589 static bool 1590 _S_equals(__hash_code, const _Hash_node_code_cache
&) 1591 { return true; } 1592 1593 static bool 1594 _S_node_equals(const _Hash_node_code_cache
&, 1595 const _Hash_node_code_cache
&) 1596 { return true; } 1597 1598 static bool 1599 _S_equals(__hash_code __c, const _Hash_node_code_cache
& __n) 1600 { return __c == __n._M_hash_code; } 1601 1602 static bool 1603 _S_node_equals(const _Hash_node_code_cache
& __lhn, 1604 const _Hash_node_code_cache
& __rhn) 1605 { return __lhn._M_hash_code == __rhn._M_hash_code; } 1606 1607 protected: 1608 _Hashtable_base() = default; 1609 1610 _Hashtable_base(const _Hash& __hash, const _Equal& __eq) 1611 : __hash_code_base(__hash), _EqualEBO(__eq) 1612 { } 1613 1614 bool 1615 _M_equals(const _Key& __k, __hash_code __c, 1616 const _Hash_node_value<_Value, __hash_cached::value>& __n) const 1617 { 1618 static_assert(__is_invocable
{}, 1619 "key equality predicate must be invocable with two arguments of " 1620 "key type"); 1621 return _S_equals(__c, __n) && _M_eq()(__k, _ExtractKey{}(__n._M_v())); 1622 } 1623 1624 template
1625 bool 1626 _M_equals_tr(const _Kt& __k, __hash_code __c, 1627 const _Hash_node_value<_Value, 1628 __hash_cached::value>& __n) const 1629 { 1630 static_assert( 1631 __is_invocable
{}, 1632 "key equality predicate must be invocable with two arguments of " 1633 "key type"); 1634 return _S_equals(__c, __n) && _M_eq()(__k, _ExtractKey{}(__n._M_v())); 1635 } 1636 1637 bool 1638 _M_node_equals( 1639 const _Hash_node_value<_Value, __hash_cached::value>& __lhn, 1640 const _Hash_node_value<_Value, __hash_cached::value>& __rhn) const 1641 { 1642 return _S_node_equals(__lhn, __rhn) 1643 && _M_eq()(_ExtractKey{}(__lhn._M_v()), _ExtractKey{}(__rhn._M_v())); 1644 } 1645 1646 void 1647 _M_swap(_Hashtable_base& __x) 1648 { 1649 __hash_code_base::_M_swap(__x); 1650 std::swap(_EqualEBO::_M_get(), __x._EqualEBO::_M_get()); 1651 } 1652 1653 const _Equal& 1654 _M_eq() const { return _EqualEBO::_M_cget(); } 1655 }; 1656 1657 /** 1658 * Primary class template _Equality. 1659 * 1660 * This is for implementing equality comparison for unordered 1661 * containers, per N3068, by John Lakos and Pablo Halpern. 1662 * Algorithmically, we follow closely the reference implementations 1663 * therein. 1664 */ 1665 template
1670 struct _Equality; 1671 1672 /// unordered_map and unordered_set specializations. 1673 template
1677 struct _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal, 1678 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true> 1679 { 1680 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, 1681 _Hash, _RangeHash, _Unused, 1682 _RehashPolicy, _Traits>; 1683 1684 bool 1685 _M_equal(const __hashtable&) const; 1686 }; 1687 1688 template
1692 bool 1693 _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal, 1694 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>:: 1695 _M_equal(const __hashtable& __other) const 1696 { 1697 using __node_type = typename __hashtable::__node_type; 1698 const __hashtable* __this = static_cast
(this); 1699 if (__this->size() != __other.size()) 1700 return false; 1701 1702 for (auto __itx = __this->begin(); __itx != __this->end(); ++__itx) 1703 { 1704 std::size_t __ybkt = __other._M_bucket_index(*__itx._M_cur); 1705 auto __prev_n = __other._M_buckets[__ybkt]; 1706 if (!__prev_n) 1707 return false; 1708 1709 for (__node_type* __n = static_cast<__node_type*>(__prev_n->_M_nxt);; 1710 __n = __n->_M_next()) 1711 { 1712 if (__n->_M_v() == *__itx) 1713 break; 1714 1715 if (!__n->_M_nxt 1716 || __other._M_bucket_index(*__n->_M_next()) != __ybkt) 1717 return false; 1718 } 1719 } 1720 1721 return true; 1722 } 1723 1724 /// unordered_multiset and unordered_multimap specializations. 1725 template
1729 struct _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal, 1730 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, false> 1731 { 1732 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, 1733 _Hash, _RangeHash, _Unused, 1734 _RehashPolicy, _Traits>; 1735 1736 bool 1737 _M_equal(const __hashtable&) const; 1738 }; 1739 1740 template
1744 bool 1745 _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal, 1746 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, false>:: 1747 _M_equal(const __hashtable& __other) const 1748 { 1749 using __node_type = typename __hashtable::__node_type; 1750 const __hashtable* __this = static_cast
(this); 1751 if (__this->size() != __other.size()) 1752 return false; 1753 1754 for (auto __itx = __this->begin(); __itx != __this->end();) 1755 { 1756 std::size_t __x_count = 1; 1757 auto __itx_end = __itx; 1758 for (++__itx_end; __itx_end != __this->end() 1759 && __this->key_eq()(_ExtractKey{}(*__itx), 1760 _ExtractKey{}(*__itx_end)); 1761 ++__itx_end) 1762 ++__x_count; 1763 1764 std::size_t __ybkt = __other._M_bucket_index(*__itx._M_cur); 1765 auto __y_prev_n = __other._M_buckets[__ybkt]; 1766 if (!__y_prev_n) 1767 return false; 1768 1769 __node_type* __y_n = static_cast<__node_type*>(__y_prev_n->_M_nxt); 1770 for (;;) 1771 { 1772 if (__this->key_eq()(_ExtractKey{}(__y_n->_M_v()), 1773 _ExtractKey{}(*__itx))) 1774 break; 1775 1776 auto __y_ref_n = __y_n; 1777 for (__y_n = __y_n->_M_next(); __y_n; __y_n = __y_n->_M_next()) 1778 if (!__other._M_node_equals(*__y_ref_n, *__y_n)) 1779 break; 1780 1781 if (!__y_n || __other._M_bucket_index(*__y_n) != __ybkt) 1782 return false; 1783 } 1784 1785 typename __hashtable::const_iterator __ity(__y_n); 1786 for (auto __ity_end = __ity; __ity_end != __other.end(); ++__ity_end) 1787 if (--__x_count == 0) 1788 break; 1789 1790 if (__x_count != 0) 1791 return false; 1792 1793 if (!std::is_permutation(__itx, __itx_end, __ity)) 1794 return false; 1795 1796 __itx = __itx_end; 1797 } 1798 return true; 1799 } 1800 1801 /** 1802 * This type deals with all allocation and keeps an allocator instance 1803 * through inheritance to benefit from EBO when possible. 1804 */ 1805 template
1806 struct _Hashtable_alloc : private _Hashtable_ebo_helper<0, _NodeAlloc> 1807 { 1808 private: 1809 using __ebo_node_alloc = _Hashtable_ebo_helper<0, _NodeAlloc>; 1810 public: 1811 using __node_type = typename _NodeAlloc::value_type; 1812 using __node_alloc_type = _NodeAlloc; 1813 // Use __gnu_cxx to benefit from _S_always_equal and al. 1814 using __node_alloc_traits = __gnu_cxx::__alloc_traits<__node_alloc_type>; 1815 1816 using __value_alloc_traits = typename __node_alloc_traits::template 1817 rebind_traits
; 1818 1819 using __node_ptr = __node_type*; 1820 using __node_base = _Hash_node_base; 1821 using __node_base_ptr = __node_base*; 1822 using __buckets_alloc_type = 1823 __alloc_rebind<__node_alloc_type, __node_base_ptr>; 1824 using __buckets_alloc_traits = std::allocator_traits<__buckets_alloc_type>; 1825 using __buckets_ptr = __node_base_ptr*; 1826 1827 _Hashtable_alloc() = default; 1828 _Hashtable_alloc(const _Hashtable_alloc&) = default; 1829 _Hashtable_alloc(_Hashtable_alloc&&) = default; 1830 1831 template
1832 _Hashtable_alloc(_Alloc&& __a) 1833 : __ebo_node_alloc(std::forward<_Alloc>(__a)) 1834 { } 1835 1836 __node_alloc_type& 1837 _M_node_allocator() 1838 { return __ebo_node_alloc::_M_get(); } 1839 1840 const __node_alloc_type& 1841 _M_node_allocator() const 1842 { return __ebo_node_alloc::_M_cget(); } 1843 1844 // Allocate a node and construct an element within it. 1845 template
1846 __node_ptr 1847 _M_allocate_node(_Args&&... __args); 1848 1849 // Destroy the element within a node and deallocate the node. 1850 void 1851 _M_deallocate_node(__node_ptr __n); 1852 1853 // Deallocate a node. 1854 void 1855 _M_deallocate_node_ptr(__node_ptr __n); 1856 1857 // Deallocate the linked list of nodes pointed to by __n. 1858 // The elements within the nodes are destroyed. 1859 void 1860 _M_deallocate_nodes(__node_ptr __n); 1861 1862 __buckets_ptr 1863 _M_allocate_buckets(std::size_t __bkt_count); 1864 1865 void 1866 _M_deallocate_buckets(__buckets_ptr, std::size_t __bkt_count); 1867 }; 1868 1869 // Definitions of class template _Hashtable_alloc's out-of-line member 1870 // functions. 1871 template
1872 template
1873 auto 1874 _Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&&... __args) 1875 -> __node_ptr 1876 { 1877 auto __nptr = __node_alloc_traits::allocate(_M_node_allocator(), 1); 1878 __node_ptr __n = std::__to_address(__nptr); 1879 __try 1880 { 1881 ::new ((void*)__n) __node_type; 1882 __node_alloc_traits::construct(_M_node_allocator(), 1883 __n->_M_valptr(), 1884 std::forward<_Args>(__args)...); 1885 return __n; 1886 } 1887 __catch(...) 1888 { 1889 __node_alloc_traits::deallocate(_M_node_allocator(), __nptr, 1); 1890 __throw_exception_again; 1891 } 1892 } 1893 1894 template
1895 void 1896 _Hashtable_alloc<_NodeAlloc>::_M_deallocate_node(__node_ptr __n) 1897 { 1898 __node_alloc_traits::destroy(_M_node_allocator(), __n->_M_valptr()); 1899 _M_deallocate_node_ptr(__n); 1900 } 1901 1902 template
1903 void 1904 _Hashtable_alloc<_NodeAlloc>::_M_deallocate_node_ptr(__node_ptr __n) 1905 { 1906 typedef typename __node_alloc_traits::pointer _Ptr; 1907 auto __ptr = std::pointer_traits<_Ptr>::pointer_to(*__n); 1908 __n->~__node_type(); 1909 __node_alloc_traits::deallocate(_M_node_allocator(), __ptr, 1); 1910 } 1911 1912 template
1913 void 1914 _Hashtable_alloc<_NodeAlloc>::_M_deallocate_nodes(__node_ptr __n) 1915 { 1916 while (__n) 1917 { 1918 __node_ptr __tmp = __n; 1919 __n = __n->_M_next(); 1920 _M_deallocate_node(__tmp); 1921 } 1922 } 1923 1924 template
1925 auto 1926 _Hashtable_alloc<_NodeAlloc>::_M_allocate_buckets(std::size_t __bkt_count) 1927 -> __buckets_ptr 1928 { 1929 __buckets_alloc_type __alloc(_M_node_allocator()); 1930 1931 auto __ptr = __buckets_alloc_traits::allocate(__alloc, __bkt_count); 1932 __buckets_ptr __p = std::__to_address(__ptr); 1933 __builtin_memset(__p, 0, __bkt_count * sizeof(__node_base_ptr)); 1934 return __p; 1935 } 1936 1937 template
1938 void 1939 _Hashtable_alloc<_NodeAlloc>:: 1940 _M_deallocate_buckets(__buckets_ptr __bkts, 1941 std::size_t __bkt_count) 1942 { 1943 typedef typename __buckets_alloc_traits::pointer _Ptr; 1944 auto __ptr = std::pointer_traits<_Ptr>::pointer_to(*__bkts); 1945 __buckets_alloc_type __alloc(_M_node_allocator()); 1946 __buckets_alloc_traits::deallocate(__alloc, __ptr, __bkt_count); 1947 } 1948 1949 ///@} hashtable-detail 1950 } // namespace __detail 1951 /// @endcond 1952 _GLIBCXX_END_NAMESPACE_VERSION 1953 } // namespace std 1954 1955 #endif // _HASHTABLE_POLICY_H
Contact us
|
About us
|
Term of use
|
Copyright © 2000-2025 MyWebUniversity.com ™