You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2108 lines
66KB

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