hashtable_policy.h 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156
  1. // Internal policy header for unordered_set and unordered_map -*- C++ -*-
  2. // Copyright (C) 2010-2019 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.
  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 _Equal
  158. * function.
  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 = std::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_ceil(__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::ceil2(__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 = std::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. const auto __max_width = std::min<size_t>(sizeof(size_t), 8);
  450. const auto __max_bkt = size_t(1) << (__max_width * __CHAR_BIT__ - 1);
  451. std::size_t __res = __clp2(__n);
  452. if (__res == __n)
  453. __res <<= 1;
  454. if (__res == 0)
  455. __res = __max_bkt;
  456. if (__res == __max_bkt)
  457. // Set next resize to the max value so that we never try to rehash again
  458. // as we already reach the biggest possible bucket number.
  459. // Note that it might result in max_load_factor not being respected.
  460. _M_next_resize = std::size_t(-1);
  461. else
  462. _M_next_resize
  463. = __builtin_ceil(__res * (long double)_M_max_load_factor);
  464. return __res;
  465. }
  466. // Return a bucket count appropriate for n elements
  467. std::size_t
  468. _M_bkt_for_elements(std::size_t __n) const noexcept
  469. { return __builtin_ceil(__n / (long double)_M_max_load_factor); }
  470. // __n_bkt is current bucket count, __n_elt is current element count,
  471. // and __n_ins is number of elements to be inserted. Do we need to
  472. // increase bucket count? If so, return make_pair(true, n), where n
  473. // is the new bucket count. If not, return make_pair(false, 0).
  474. std::pair<bool, std::size_t>
  475. _M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt,
  476. std::size_t __n_ins) noexcept
  477. {
  478. if (__n_elt + __n_ins >= _M_next_resize)
  479. {
  480. long double __min_bkts = (__n_elt + __n_ins)
  481. / (long double)_M_max_load_factor;
  482. if (__min_bkts >= __n_bkt)
  483. return std::make_pair(true,
  484. _M_next_bkt(std::max<std::size_t>(__builtin_floor(__min_bkts) + 1,
  485. __n_bkt * _S_growth_factor)));
  486. _M_next_resize
  487. = __builtin_floor(__n_bkt * (long double)_M_max_load_factor);
  488. return std::make_pair(false, 0);
  489. }
  490. else
  491. return std::make_pair(false, 0);
  492. }
  493. typedef std::size_t _State;
  494. _State
  495. _M_state() const noexcept
  496. { return _M_next_resize; }
  497. void
  498. _M_reset() noexcept
  499. { _M_next_resize = 0; }
  500. void
  501. _M_reset(_State __state) noexcept
  502. { _M_next_resize = __state; }
  503. static const std::size_t _S_growth_factor = 2;
  504. float _M_max_load_factor;
  505. std::size_t _M_next_resize;
  506. };
  507. // Base classes for std::_Hashtable. We define these base classes
  508. // because in some cases we want to do different things depending on
  509. // the value of a policy class. In some cases the policy class
  510. // affects which member functions and nested typedefs are defined;
  511. // we handle that by specializing base class templates. Several of
  512. // the base class templates need to access other members of class
  513. // template _Hashtable, so we use a variant of the "Curiously
  514. // Recurring Template Pattern" (CRTP) technique.
  515. /**
  516. * Primary class template _Map_base.
  517. *
  518. * If the hashtable has a value type of the form pair<T1, T2> and a
  519. * key extraction policy (_ExtractKey) that returns the first part
  520. * of the pair, the hashtable gets a mapped_type typedef. If it
  521. * satisfies those criteria and also has unique keys, then it also
  522. * gets an operator[].
  523. */
  524. template<typename _Key, typename _Value, typename _Alloc,
  525. typename _ExtractKey, typename _Equal,
  526. typename _H1, typename _H2, typename _Hash,
  527. typename _RehashPolicy, typename _Traits,
  528. bool _Unique_keys = _Traits::__unique_keys::value>
  529. struct _Map_base { };
  530. /// Partial specialization, __unique_keys set to false.
  531. template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
  532. typename _H1, typename _H2, typename _Hash,
  533. typename _RehashPolicy, typename _Traits>
  534. struct _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
  535. _H1, _H2, _Hash, _RehashPolicy, _Traits, false>
  536. {
  537. using mapped_type = typename std::tuple_element<1, _Pair>::type;
  538. };
  539. /// Partial specialization, __unique_keys set to true.
  540. template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
  541. typename _H1, typename _H2, typename _Hash,
  542. typename _RehashPolicy, typename _Traits>
  543. struct _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
  544. _H1, _H2, _Hash, _RehashPolicy, _Traits, true>
  545. {
  546. private:
  547. using __hashtable_base = __detail::_Hashtable_base<_Key, _Pair,
  548. _Select1st,
  549. _Equal, _H1, _H2, _Hash,
  550. _Traits>;
  551. using __hashtable = _Hashtable<_Key, _Pair, _Alloc,
  552. _Select1st, _Equal,
  553. _H1, _H2, _Hash, _RehashPolicy, _Traits>;
  554. using __hash_code = typename __hashtable_base::__hash_code;
  555. using __node_type = typename __hashtable_base::__node_type;
  556. public:
  557. using key_type = typename __hashtable_base::key_type;
  558. using iterator = typename __hashtable_base::iterator;
  559. using mapped_type = typename std::tuple_element<1, _Pair>::type;
  560. mapped_type&
  561. operator[](const key_type& __k);
  562. mapped_type&
  563. operator[](key_type&& __k);
  564. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  565. // DR 761. unordered_map needs an at() member function.
  566. mapped_type&
  567. at(const key_type& __k);
  568. const mapped_type&
  569. at(const key_type& __k) const;
  570. };
  571. template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
  572. typename _H1, typename _H2, typename _Hash,
  573. typename _RehashPolicy, typename _Traits>
  574. auto
  575. _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
  576. _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
  577. operator[](const key_type& __k)
  578. -> mapped_type&
  579. {
  580. __hashtable* __h = static_cast<__hashtable*>(this);
  581. __hash_code __code = __h->_M_hash_code(__k);
  582. std::size_t __n = __h->_M_bucket_index(__k, __code);
  583. __node_type* __p = __h->_M_find_node(__n, __k, __code);
  584. if (!__p)
  585. {
  586. __p = __h->_M_allocate_node(std::piecewise_construct,
  587. std::tuple<const key_type&>(__k),
  588. std::tuple<>());
  589. return __h->_M_insert_unique_node(__n, __code, __p)->second;
  590. }
  591. return __p->_M_v().second;
  592. }
  593. template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
  594. typename _H1, typename _H2, typename _Hash,
  595. typename _RehashPolicy, typename _Traits>
  596. auto
  597. _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
  598. _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
  599. operator[](key_type&& __k)
  600. -> mapped_type&
  601. {
  602. __hashtable* __h = static_cast<__hashtable*>(this);
  603. __hash_code __code = __h->_M_hash_code(__k);
  604. std::size_t __n = __h->_M_bucket_index(__k, __code);
  605. __node_type* __p = __h->_M_find_node(__n, __k, __code);
  606. if (!__p)
  607. {
  608. __p = __h->_M_allocate_node(std::piecewise_construct,
  609. std::forward_as_tuple(std::move(__k)),
  610. std::tuple<>());
  611. return __h->_M_insert_unique_node(__n, __code, __p)->second;
  612. }
  613. return __p->_M_v().second;
  614. }
  615. template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
  616. typename _H1, typename _H2, typename _Hash,
  617. typename _RehashPolicy, typename _Traits>
  618. auto
  619. _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
  620. _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
  621. at(const key_type& __k)
  622. -> mapped_type&
  623. {
  624. __hashtable* __h = static_cast<__hashtable*>(this);
  625. __hash_code __code = __h->_M_hash_code(__k);
  626. std::size_t __n = __h->_M_bucket_index(__k, __code);
  627. __node_type* __p = __h->_M_find_node(__n, __k, __code);
  628. if (!__p)
  629. __throw_out_of_range(__N("_Map_base::at"));
  630. return __p->_M_v().second;
  631. }
  632. template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
  633. typename _H1, typename _H2, typename _Hash,
  634. typename _RehashPolicy, typename _Traits>
  635. auto
  636. _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
  637. _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
  638. at(const key_type& __k) const
  639. -> const mapped_type&
  640. {
  641. const __hashtable* __h = static_cast<const __hashtable*>(this);
  642. __hash_code __code = __h->_M_hash_code(__k);
  643. std::size_t __n = __h->_M_bucket_index(__k, __code);
  644. __node_type* __p = __h->_M_find_node(__n, __k, __code);
  645. if (!__p)
  646. __throw_out_of_range(__N("_Map_base::at"));
  647. return __p->_M_v().second;
  648. }
  649. /**
  650. * Primary class template _Insert_base.
  651. *
  652. * Defines @c insert member functions appropriate to all _Hashtables.
  653. */
  654. template<typename _Key, typename _Value, typename _Alloc,
  655. typename _ExtractKey, typename _Equal,
  656. typename _H1, typename _H2, typename _Hash,
  657. typename _RehashPolicy, typename _Traits>
  658. struct _Insert_base
  659. {
  660. protected:
  661. using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
  662. _Equal, _H1, _H2, _Hash,
  663. _RehashPolicy, _Traits>;
  664. using __hashtable_base = _Hashtable_base<_Key, _Value, _ExtractKey,
  665. _Equal, _H1, _H2, _Hash,
  666. _Traits>;
  667. using value_type = typename __hashtable_base::value_type;
  668. using iterator = typename __hashtable_base::iterator;
  669. using const_iterator = typename __hashtable_base::const_iterator;
  670. using size_type = typename __hashtable_base::size_type;
  671. using __unique_keys = typename __hashtable_base::__unique_keys;
  672. using __ireturn_type = typename __hashtable_base::__ireturn_type;
  673. using __node_type = _Hash_node<_Value, _Traits::__hash_cached::value>;
  674. using __node_alloc_type = __alloc_rebind<_Alloc, __node_type>;
  675. using __node_gen_type = _AllocNode<__node_alloc_type>;
  676. __hashtable&
  677. _M_conjure_hashtable()
  678. { return *(static_cast<__hashtable*>(this)); }
  679. template<typename _InputIterator, typename _NodeGetter>
  680. void
  681. _M_insert_range(_InputIterator __first, _InputIterator __last,
  682. const _NodeGetter&, true_type);
  683. template<typename _InputIterator, typename _NodeGetter>
  684. void
  685. _M_insert_range(_InputIterator __first, _InputIterator __last,
  686. const _NodeGetter&, false_type);
  687. public:
  688. __ireturn_type
  689. insert(const value_type& __v)
  690. {
  691. __hashtable& __h = _M_conjure_hashtable();
  692. __node_gen_type __node_gen(__h);
  693. return __h._M_insert(__v, __node_gen, __unique_keys());
  694. }
  695. iterator
  696. insert(const_iterator __hint, const value_type& __v)
  697. {
  698. __hashtable& __h = _M_conjure_hashtable();
  699. __node_gen_type __node_gen(__h);
  700. return __h._M_insert(__hint, __v, __node_gen, __unique_keys());
  701. }
  702. void
  703. insert(initializer_list<value_type> __l)
  704. { this->insert(__l.begin(), __l.end()); }
  705. template<typename _InputIterator>
  706. void
  707. insert(_InputIterator __first, _InputIterator __last)
  708. {
  709. __hashtable& __h = _M_conjure_hashtable();
  710. __node_gen_type __node_gen(__h);
  711. return _M_insert_range(__first, __last, __node_gen, __unique_keys());
  712. }
  713. };
  714. template<typename _Key, typename _Value, typename _Alloc,
  715. typename _ExtractKey, typename _Equal,
  716. typename _H1, typename _H2, typename _Hash,
  717. typename _RehashPolicy, typename _Traits>
  718. template<typename _InputIterator, typename _NodeGetter>
  719. void
  720. _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash,
  721. _RehashPolicy, _Traits>::
  722. _M_insert_range(_InputIterator __first, _InputIterator __last,
  723. const _NodeGetter& __node_gen, true_type)
  724. {
  725. size_type __n_elt = __detail::__distance_fw(__first, __last);
  726. if (__n_elt == 0)
  727. return;
  728. __hashtable& __h = _M_conjure_hashtable();
  729. for (; __first != __last; ++__first)
  730. {
  731. if (__h._M_insert(*__first, __node_gen, __unique_keys(),
  732. __n_elt).second)
  733. __n_elt = 1;
  734. else if (__n_elt != 1)
  735. --__n_elt;
  736. }
  737. }
  738. template<typename _Key, typename _Value, typename _Alloc,
  739. typename _ExtractKey, typename _Equal,
  740. typename _H1, typename _H2, typename _Hash,
  741. typename _RehashPolicy, typename _Traits>
  742. template<typename _InputIterator, typename _NodeGetter>
  743. void
  744. _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash,
  745. _RehashPolicy, _Traits>::
  746. _M_insert_range(_InputIterator __first, _InputIterator __last,
  747. const _NodeGetter& __node_gen, false_type)
  748. {
  749. using __rehash_type = typename __hashtable::__rehash_type;
  750. using __rehash_state = typename __hashtable::__rehash_state;
  751. using pair_type = std::pair<bool, std::size_t>;
  752. size_type __n_elt = __detail::__distance_fw(__first, __last);
  753. if (__n_elt == 0)
  754. return;
  755. __hashtable& __h = _M_conjure_hashtable();
  756. __rehash_type& __rehash = __h._M_rehash_policy;
  757. const __rehash_state& __saved_state = __rehash._M_state();
  758. pair_type __do_rehash = __rehash._M_need_rehash(__h._M_bucket_count,
  759. __h._M_element_count,
  760. __n_elt);
  761. if (__do_rehash.first)
  762. __h._M_rehash(__do_rehash.second, __saved_state);
  763. for (; __first != __last; ++__first)
  764. __h._M_insert(*__first, __node_gen, __unique_keys());
  765. }
  766. /**
  767. * Primary class template _Insert.
  768. *
  769. * Defines @c insert member functions that depend on _Hashtable policies,
  770. * via partial specializations.
  771. */
  772. template<typename _Key, typename _Value, typename _Alloc,
  773. typename _ExtractKey, typename _Equal,
  774. typename _H1, typename _H2, typename _Hash,
  775. typename _RehashPolicy, typename _Traits,
  776. bool _Constant_iterators = _Traits::__constant_iterators::value>
  777. struct _Insert;
  778. /// Specialization.
  779. template<typename _Key, typename _Value, typename _Alloc,
  780. typename _ExtractKey, typename _Equal,
  781. typename _H1, typename _H2, typename _Hash,
  782. typename _RehashPolicy, typename _Traits>
  783. struct _Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash,
  784. _RehashPolicy, _Traits, true>
  785. : public _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  786. _H1, _H2, _Hash, _RehashPolicy, _Traits>
  787. {
  788. using __base_type = _Insert_base<_Key, _Value, _Alloc, _ExtractKey,
  789. _Equal, _H1, _H2, _Hash,
  790. _RehashPolicy, _Traits>;
  791. using __hashtable_base = _Hashtable_base<_Key, _Value, _ExtractKey,
  792. _Equal, _H1, _H2, _Hash,
  793. _Traits>;
  794. using value_type = typename __base_type::value_type;
  795. using iterator = typename __base_type::iterator;
  796. using const_iterator = typename __base_type::const_iterator;
  797. using __unique_keys = typename __base_type::__unique_keys;
  798. using __ireturn_type = typename __hashtable_base::__ireturn_type;
  799. using __hashtable = typename __base_type::__hashtable;
  800. using __node_gen_type = typename __base_type::__node_gen_type;
  801. using __base_type::insert;
  802. __ireturn_type
  803. insert(value_type&& __v)
  804. {
  805. __hashtable& __h = this->_M_conjure_hashtable();
  806. __node_gen_type __node_gen(__h);
  807. return __h._M_insert(std::move(__v), __node_gen, __unique_keys());
  808. }
  809. iterator
  810. insert(const_iterator __hint, value_type&& __v)
  811. {
  812. __hashtable& __h = this->_M_conjure_hashtable();
  813. __node_gen_type __node_gen(__h);
  814. return __h._M_insert(__hint, std::move(__v), __node_gen,
  815. __unique_keys());
  816. }
  817. };
  818. /// Specialization.
  819. template<typename _Key, typename _Value, typename _Alloc,
  820. typename _ExtractKey, typename _Equal,
  821. typename _H1, typename _H2, typename _Hash,
  822. typename _RehashPolicy, typename _Traits>
  823. struct _Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash,
  824. _RehashPolicy, _Traits, false>
  825. : public _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  826. _H1, _H2, _Hash, _RehashPolicy, _Traits>
  827. {
  828. using __base_type = _Insert_base<_Key, _Value, _Alloc, _ExtractKey,
  829. _Equal, _H1, _H2, _Hash,
  830. _RehashPolicy, _Traits>;
  831. using value_type = typename __base_type::value_type;
  832. using iterator = typename __base_type::iterator;
  833. using const_iterator = typename __base_type::const_iterator;
  834. using __unique_keys = typename __base_type::__unique_keys;
  835. using __hashtable = typename __base_type::__hashtable;
  836. using __ireturn_type = typename __base_type::__ireturn_type;
  837. using __base_type::insert;
  838. template<typename _Pair>
  839. using __is_cons = std::is_constructible<value_type, _Pair&&>;
  840. template<typename _Pair>
  841. using _IFcons = std::enable_if<__is_cons<_Pair>::value>;
  842. template<typename _Pair>
  843. using _IFconsp = typename _IFcons<_Pair>::type;
  844. template<typename _Pair, typename = _IFconsp<_Pair>>
  845. __ireturn_type
  846. insert(_Pair&& __v)
  847. {
  848. __hashtable& __h = this->_M_conjure_hashtable();
  849. return __h._M_emplace(__unique_keys(), std::forward<_Pair>(__v));
  850. }
  851. template<typename _Pair, typename = _IFconsp<_Pair>>
  852. iterator
  853. insert(const_iterator __hint, _Pair&& __v)
  854. {
  855. __hashtable& __h = this->_M_conjure_hashtable();
  856. return __h._M_emplace(__hint, __unique_keys(),
  857. std::forward<_Pair>(__v));
  858. }
  859. };
  860. template<typename _Policy>
  861. using __has_load_factor = typename _Policy::__has_load_factor;
  862. /**
  863. * Primary class template _Rehash_base.
  864. *
  865. * Give hashtable the max_load_factor functions and reserve iff the
  866. * rehash policy supports it.
  867. */
  868. template<typename _Key, typename _Value, typename _Alloc,
  869. typename _ExtractKey, typename _Equal,
  870. typename _H1, typename _H2, typename _Hash,
  871. typename _RehashPolicy, typename _Traits,
  872. typename =
  873. __detected_or_t<std::false_type, __has_load_factor, _RehashPolicy>>
  874. struct _Rehash_base;
  875. /// Specialization when rehash policy doesn't provide load factor management.
  876. template<typename _Key, typename _Value, typename _Alloc,
  877. typename _ExtractKey, typename _Equal,
  878. typename _H1, typename _H2, typename _Hash,
  879. typename _RehashPolicy, typename _Traits>
  880. struct _Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  881. _H1, _H2, _Hash, _RehashPolicy, _Traits,
  882. std::false_type>
  883. {
  884. };
  885. /// Specialization when rehash policy provide load factor management.
  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. struct _Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  891. _H1, _H2, _Hash, _RehashPolicy, _Traits,
  892. std::true_type>
  893. {
  894. using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
  895. _Equal, _H1, _H2, _Hash,
  896. _RehashPolicy, _Traits>;
  897. float
  898. max_load_factor() const noexcept
  899. {
  900. const __hashtable* __this = static_cast<const __hashtable*>(this);
  901. return __this->__rehash_policy().max_load_factor();
  902. }
  903. void
  904. max_load_factor(float __z)
  905. {
  906. __hashtable* __this = static_cast<__hashtable*>(this);
  907. __this->__rehash_policy(_RehashPolicy(__z));
  908. }
  909. void
  910. reserve(std::size_t __n)
  911. {
  912. __hashtable* __this = static_cast<__hashtable*>(this);
  913. __this->rehash(__builtin_ceil(__n / max_load_factor()));
  914. }
  915. };
  916. /**
  917. * Primary class template _Hashtable_ebo_helper.
  918. *
  919. * Helper class using EBO when it is not forbidden (the type is not
  920. * final) and when it is worth it (the type is empty.)
  921. */
  922. template<int _Nm, typename _Tp,
  923. bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
  924. struct _Hashtable_ebo_helper;
  925. /// Specialization using EBO.
  926. template<int _Nm, typename _Tp>
  927. struct _Hashtable_ebo_helper<_Nm, _Tp, true>
  928. : private _Tp
  929. {
  930. _Hashtable_ebo_helper() = default;
  931. template<typename _OtherTp>
  932. _Hashtable_ebo_helper(_OtherTp&& __tp)
  933. : _Tp(std::forward<_OtherTp>(__tp))
  934. { }
  935. static const _Tp&
  936. _S_cget(const _Hashtable_ebo_helper& __eboh)
  937. { return static_cast<const _Tp&>(__eboh); }
  938. static _Tp&
  939. _S_get(_Hashtable_ebo_helper& __eboh)
  940. { return static_cast<_Tp&>(__eboh); }
  941. };
  942. /// Specialization not using EBO.
  943. template<int _Nm, typename _Tp>
  944. struct _Hashtable_ebo_helper<_Nm, _Tp, false>
  945. {
  946. _Hashtable_ebo_helper() = default;
  947. template<typename _OtherTp>
  948. _Hashtable_ebo_helper(_OtherTp&& __tp)
  949. : _M_tp(std::forward<_OtherTp>(__tp))
  950. { }
  951. static const _Tp&
  952. _S_cget(const _Hashtable_ebo_helper& __eboh)
  953. { return __eboh._M_tp; }
  954. static _Tp&
  955. _S_get(_Hashtable_ebo_helper& __eboh)
  956. { return __eboh._M_tp; }
  957. private:
  958. _Tp _M_tp;
  959. };
  960. /**
  961. * Primary class template _Local_iterator_base.
  962. *
  963. * Base class for local iterators, used to iterate within a bucket
  964. * but not between buckets.
  965. */
  966. template<typename _Key, typename _Value, typename _ExtractKey,
  967. typename _H1, typename _H2, typename _Hash,
  968. bool __cache_hash_code>
  969. struct _Local_iterator_base;
  970. /**
  971. * Primary class template _Hash_code_base.
  972. *
  973. * Encapsulates two policy issues that aren't quite orthogonal.
  974. * (1) the difference between using a ranged hash function and using
  975. * the combination of a hash function and a range-hashing function.
  976. * In the former case we don't have such things as hash codes, so
  977. * we have a dummy type as placeholder.
  978. * (2) Whether or not we cache hash codes. Caching hash codes is
  979. * meaningless if we have a ranged hash function.
  980. *
  981. * We also put the key extraction objects here, for convenience.
  982. * Each specialization derives from one or more of the template
  983. * parameters to benefit from Ebo. This is important as this type
  984. * is inherited in some cases by the _Local_iterator_base type used
  985. * to implement local_iterator and const_local_iterator. As with
  986. * any iterator type we prefer to make it as small as possible.
  987. *
  988. * Primary template is unused except as a hook for specializations.
  989. */
  990. template<typename _Key, typename _Value, typename _ExtractKey,
  991. typename _H1, typename _H2, typename _Hash,
  992. bool __cache_hash_code>
  993. struct _Hash_code_base;
  994. /// Specialization: ranged hash function, no caching hash codes. H1
  995. /// and H2 are provided but ignored. We define a dummy hash code type.
  996. template<typename _Key, typename _Value, typename _ExtractKey,
  997. typename _H1, typename _H2, typename _Hash>
  998. struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash, false>
  999. : private _Hashtable_ebo_helper<0, _ExtractKey>,
  1000. private _Hashtable_ebo_helper<1, _Hash>
  1001. {
  1002. private:
  1003. using __ebo_extract_key = _Hashtable_ebo_helper<0, _ExtractKey>;
  1004. using __ebo_hash = _Hashtable_ebo_helper<1, _Hash>;
  1005. protected:
  1006. typedef void* __hash_code;
  1007. typedef _Hash_node<_Value, false> __node_type;
  1008. // We need the default constructor for the local iterators and _Hashtable
  1009. // default constructor.
  1010. _Hash_code_base() = default;
  1011. _Hash_code_base(const _ExtractKey& __ex, const _H1&, const _H2&,
  1012. const _Hash& __h)
  1013. : __ebo_extract_key(__ex), __ebo_hash(__h) { }
  1014. __hash_code
  1015. _M_hash_code(const _Key& __key) const
  1016. { return 0; }
  1017. std::size_t
  1018. _M_bucket_index(const _Key& __k, __hash_code, std::size_t __n) const
  1019. { return _M_ranged_hash()(__k, __n); }
  1020. std::size_t
  1021. _M_bucket_index(const __node_type* __p, std::size_t __n) const
  1022. noexcept( noexcept(declval<const _Hash&>()(declval<const _Key&>(),
  1023. (std::size_t)0)) )
  1024. { return _M_ranged_hash()(_M_extract()(__p->_M_v()), __n); }
  1025. void
  1026. _M_store_code(__node_type*, __hash_code) const
  1027. { }
  1028. void
  1029. _M_copy_code(__node_type*, const __node_type*) const
  1030. { }
  1031. void
  1032. _M_swap(_Hash_code_base& __x)
  1033. {
  1034. std::swap(_M_extract(), __x._M_extract());
  1035. std::swap(_M_ranged_hash(), __x._M_ranged_hash());
  1036. }
  1037. const _ExtractKey&
  1038. _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
  1039. _ExtractKey&
  1040. _M_extract() { return __ebo_extract_key::_S_get(*this); }
  1041. const _Hash&
  1042. _M_ranged_hash() const { return __ebo_hash::_S_cget(*this); }
  1043. _Hash&
  1044. _M_ranged_hash() { return __ebo_hash::_S_get(*this); }
  1045. };
  1046. // No specialization for ranged hash function while caching hash codes.
  1047. // That combination is meaningless, and trying to do it is an error.
  1048. /// Specialization: ranged hash function, cache hash codes. This
  1049. /// combination is meaningless, so we provide only a declaration
  1050. /// and no definition.
  1051. template<typename _Key, typename _Value, typename _ExtractKey,
  1052. typename _H1, typename _H2, typename _Hash>
  1053. struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash, true>;
  1054. /// Specialization: hash function and range-hashing function, no
  1055. /// caching of hash codes.
  1056. /// Provides typedef and accessor required by C++ 11.
  1057. template<typename _Key, typename _Value, typename _ExtractKey,
  1058. typename _H1, typename _H2>
  1059. struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
  1060. _Default_ranged_hash, false>
  1061. : private _Hashtable_ebo_helper<0, _ExtractKey>,
  1062. private _Hashtable_ebo_helper<1, _H1>,
  1063. private _Hashtable_ebo_helper<2, _H2>
  1064. {
  1065. private:
  1066. using __ebo_extract_key = _Hashtable_ebo_helper<0, _ExtractKey>;
  1067. using __ebo_h1 = _Hashtable_ebo_helper<1, _H1>;
  1068. using __ebo_h2 = _Hashtable_ebo_helper<2, _H2>;
  1069. // Gives the local iterator implementation access to _M_bucket_index().
  1070. friend struct _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2,
  1071. _Default_ranged_hash, false>;
  1072. public:
  1073. typedef _H1 hasher;
  1074. hasher
  1075. hash_function() const
  1076. { return _M_h1(); }
  1077. protected:
  1078. typedef std::size_t __hash_code;
  1079. typedef _Hash_node<_Value, false> __node_type;
  1080. // We need the default constructor for the local iterators and _Hashtable
  1081. // default constructor.
  1082. _Hash_code_base() = default;
  1083. _Hash_code_base(const _ExtractKey& __ex,
  1084. const _H1& __h1, const _H2& __h2,
  1085. const _Default_ranged_hash&)
  1086. : __ebo_extract_key(__ex), __ebo_h1(__h1), __ebo_h2(__h2) { }
  1087. __hash_code
  1088. _M_hash_code(const _Key& __k) const
  1089. {
  1090. static_assert(__is_invocable<const _H1&, const _Key&>{},
  1091. "hash function must be invocable with an argument of key type");
  1092. return _M_h1()(__k);
  1093. }
  1094. std::size_t
  1095. _M_bucket_index(const _Key&, __hash_code __c, std::size_t __n) const
  1096. { return _M_h2()(__c, __n); }
  1097. std::size_t
  1098. _M_bucket_index(const __node_type* __p, std::size_t __n) const
  1099. noexcept( noexcept(declval<const _H1&>()(declval<const _Key&>()))
  1100. && noexcept(declval<const _H2&>()((__hash_code)0,
  1101. (std::size_t)0)) )
  1102. { return _M_h2()(_M_h1()(_M_extract()(__p->_M_v())), __n); }
  1103. void
  1104. _M_store_code(__node_type*, __hash_code) const
  1105. { }
  1106. void
  1107. _M_copy_code(__node_type*, const __node_type*) const
  1108. { }
  1109. void
  1110. _M_swap(_Hash_code_base& __x)
  1111. {
  1112. std::swap(_M_extract(), __x._M_extract());
  1113. std::swap(_M_h1(), __x._M_h1());
  1114. std::swap(_M_h2(), __x._M_h2());
  1115. }
  1116. const _ExtractKey&
  1117. _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
  1118. _ExtractKey&
  1119. _M_extract() { return __ebo_extract_key::_S_get(*this); }
  1120. const _H1&
  1121. _M_h1() const { return __ebo_h1::_S_cget(*this); }
  1122. _H1&
  1123. _M_h1() { return __ebo_h1::_S_get(*this); }
  1124. const _H2&
  1125. _M_h2() const { return __ebo_h2::_S_cget(*this); }
  1126. _H2&
  1127. _M_h2() { return __ebo_h2::_S_get(*this); }
  1128. };
  1129. /// Specialization: hash function and range-hashing function,
  1130. /// caching hash codes. H is provided but ignored. Provides
  1131. /// typedef and accessor required by C++ 11.
  1132. template<typename _Key, typename _Value, typename _ExtractKey,
  1133. typename _H1, typename _H2>
  1134. struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
  1135. _Default_ranged_hash, true>
  1136. : private _Hashtable_ebo_helper<0, _ExtractKey>,
  1137. private _Hashtable_ebo_helper<1, _H1>,
  1138. private _Hashtable_ebo_helper<2, _H2>
  1139. {
  1140. private:
  1141. // Gives the local iterator implementation access to _M_h2().
  1142. friend struct _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2,
  1143. _Default_ranged_hash, true>;
  1144. using __ebo_extract_key = _Hashtable_ebo_helper<0, _ExtractKey>;
  1145. using __ebo_h1 = _Hashtable_ebo_helper<1, _H1>;
  1146. using __ebo_h2 = _Hashtable_ebo_helper<2, _H2>;
  1147. public:
  1148. typedef _H1 hasher;
  1149. hasher
  1150. hash_function() const
  1151. { return _M_h1(); }
  1152. protected:
  1153. typedef std::size_t __hash_code;
  1154. typedef _Hash_node<_Value, true> __node_type;
  1155. // We need the default constructor for _Hashtable default constructor.
  1156. _Hash_code_base() = default;
  1157. _Hash_code_base(const _ExtractKey& __ex,
  1158. const _H1& __h1, const _H2& __h2,
  1159. const _Default_ranged_hash&)
  1160. : __ebo_extract_key(__ex), __ebo_h1(__h1), __ebo_h2(__h2) { }
  1161. __hash_code
  1162. _M_hash_code(const _Key& __k) const
  1163. {
  1164. static_assert(__is_invocable<const _H1&, const _Key&>{},
  1165. "hash function must be invocable with an argument of key type");
  1166. return _M_h1()(__k);
  1167. }
  1168. std::size_t
  1169. _M_bucket_index(const _Key&, __hash_code __c,
  1170. std::size_t __n) const
  1171. { return _M_h2()(__c, __n); }
  1172. std::size_t
  1173. _M_bucket_index(const __node_type* __p, std::size_t __n) const
  1174. noexcept( noexcept(declval<const _H2&>()((__hash_code)0,
  1175. (std::size_t)0)) )
  1176. { return _M_h2()(__p->_M_hash_code, __n); }
  1177. void
  1178. _M_store_code(__node_type* __n, __hash_code __c) const
  1179. { __n->_M_hash_code = __c; }
  1180. void
  1181. _M_copy_code(__node_type* __to, const __node_type* __from) const
  1182. { __to->_M_hash_code = __from->_M_hash_code; }
  1183. void
  1184. _M_swap(_Hash_code_base& __x)
  1185. {
  1186. std::swap(_M_extract(), __x._M_extract());
  1187. std::swap(_M_h1(), __x._M_h1());
  1188. std::swap(_M_h2(), __x._M_h2());
  1189. }
  1190. const _ExtractKey&
  1191. _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
  1192. _ExtractKey&
  1193. _M_extract() { return __ebo_extract_key::_S_get(*this); }
  1194. const _H1&
  1195. _M_h1() const { return __ebo_h1::_S_cget(*this); }
  1196. _H1&
  1197. _M_h1() { return __ebo_h1::_S_get(*this); }
  1198. const _H2&
  1199. _M_h2() const { return __ebo_h2::_S_cget(*this); }
  1200. _H2&
  1201. _M_h2() { return __ebo_h2::_S_get(*this); }
  1202. };
  1203. /**
  1204. * Primary class template _Equal_helper.
  1205. *
  1206. */
  1207. template <typename _Key, typename _Value, typename _ExtractKey,
  1208. typename _Equal, typename _HashCodeType,
  1209. bool __cache_hash_code>
  1210. struct _Equal_helper;
  1211. /// Specialization.
  1212. template<typename _Key, typename _Value, typename _ExtractKey,
  1213. typename _Equal, typename _HashCodeType>
  1214. struct _Equal_helper<_Key, _Value, _ExtractKey, _Equal, _HashCodeType, true>
  1215. {
  1216. static bool
  1217. _S_equals(const _Equal& __eq, const _ExtractKey& __extract,
  1218. const _Key& __k, _HashCodeType __c, _Hash_node<_Value, true>* __n)
  1219. { return __c == __n->_M_hash_code && __eq(__k, __extract(__n->_M_v())); }
  1220. };
  1221. /// Specialization.
  1222. template<typename _Key, typename _Value, typename _ExtractKey,
  1223. typename _Equal, typename _HashCodeType>
  1224. struct _Equal_helper<_Key, _Value, _ExtractKey, _Equal, _HashCodeType, false>
  1225. {
  1226. static bool
  1227. _S_equals(const _Equal& __eq, const _ExtractKey& __extract,
  1228. const _Key& __k, _HashCodeType, _Hash_node<_Value, false>* __n)
  1229. { return __eq(__k, __extract(__n->_M_v())); }
  1230. };
  1231. /// Partial specialization used when nodes contain a cached hash code.
  1232. template<typename _Key, typename _Value, typename _ExtractKey,
  1233. typename _H1, typename _H2, typename _Hash>
  1234. struct _Local_iterator_base<_Key, _Value, _ExtractKey,
  1235. _H1, _H2, _Hash, true>
  1236. : private _Hashtable_ebo_helper<0, _H2>
  1237. {
  1238. protected:
  1239. using __base_type = _Hashtable_ebo_helper<0, _H2>;
  1240. using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
  1241. _H1, _H2, _Hash, true>;
  1242. _Local_iterator_base() = default;
  1243. _Local_iterator_base(const __hash_code_base& __base,
  1244. _Hash_node<_Value, true>* __p,
  1245. std::size_t __bkt, std::size_t __bkt_count)
  1246. : __base_type(__base._M_h2()),
  1247. _M_cur(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count) { }
  1248. void
  1249. _M_incr()
  1250. {
  1251. _M_cur = _M_cur->_M_next();
  1252. if (_M_cur)
  1253. {
  1254. std::size_t __bkt
  1255. = __base_type::_S_get(*this)(_M_cur->_M_hash_code,
  1256. _M_bucket_count);
  1257. if (__bkt != _M_bucket)
  1258. _M_cur = nullptr;
  1259. }
  1260. }
  1261. _Hash_node<_Value, true>* _M_cur;
  1262. std::size_t _M_bucket;
  1263. std::size_t _M_bucket_count;
  1264. public:
  1265. const void*
  1266. _M_curr() const { return _M_cur; } // for equality ops
  1267. std::size_t
  1268. _M_get_bucket() const { return _M_bucket; } // for debug mode
  1269. };
  1270. // Uninitialized storage for a _Hash_code_base.
  1271. // This type is DefaultConstructible and Assignable even if the
  1272. // _Hash_code_base type isn't, so that _Local_iterator_base<..., false>
  1273. // can be DefaultConstructible and Assignable.
  1274. template<typename _Tp, bool _IsEmpty = std::is_empty<_Tp>::value>
  1275. struct _Hash_code_storage
  1276. {
  1277. __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
  1278. _Tp*
  1279. _M_h() { return _M_storage._M_ptr(); }
  1280. const _Tp*
  1281. _M_h() const { return _M_storage._M_ptr(); }
  1282. };
  1283. // Empty partial specialization for empty _Hash_code_base types.
  1284. template<typename _Tp>
  1285. struct _Hash_code_storage<_Tp, true>
  1286. {
  1287. static_assert( std::is_empty<_Tp>::value, "Type must be empty" );
  1288. // As _Tp is an empty type there will be no bytes written/read through
  1289. // the cast pointer, so no strict-aliasing violation.
  1290. _Tp*
  1291. _M_h() { return reinterpret_cast<_Tp*>(this); }
  1292. const _Tp*
  1293. _M_h() const { return reinterpret_cast<const _Tp*>(this); }
  1294. };
  1295. template<typename _Key, typename _Value, typename _ExtractKey,
  1296. typename _H1, typename _H2, typename _Hash>
  1297. using __hash_code_for_local_iter
  1298. = _Hash_code_storage<_Hash_code_base<_Key, _Value, _ExtractKey,
  1299. _H1, _H2, _Hash, false>>;
  1300. // Partial specialization used when hash codes are not cached
  1301. template<typename _Key, typename _Value, typename _ExtractKey,
  1302. typename _H1, typename _H2, typename _Hash>
  1303. struct _Local_iterator_base<_Key, _Value, _ExtractKey,
  1304. _H1, _H2, _Hash, false>
  1305. : __hash_code_for_local_iter<_Key, _Value, _ExtractKey, _H1, _H2, _Hash>
  1306. {
  1307. protected:
  1308. using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
  1309. _H1, _H2, _Hash, false>;
  1310. _Local_iterator_base() : _M_bucket_count(-1) { }
  1311. _Local_iterator_base(const __hash_code_base& __base,
  1312. _Hash_node<_Value, false>* __p,
  1313. std::size_t __bkt, std::size_t __bkt_count)
  1314. : _M_cur(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count)
  1315. { _M_init(__base); }
  1316. ~_Local_iterator_base()
  1317. {
  1318. if (_M_bucket_count != -1)
  1319. _M_destroy();
  1320. }
  1321. _Local_iterator_base(const _Local_iterator_base& __iter)
  1322. : _M_cur(__iter._M_cur), _M_bucket(__iter._M_bucket),
  1323. _M_bucket_count(__iter._M_bucket_count)
  1324. {
  1325. if (_M_bucket_count != -1)
  1326. _M_init(*__iter._M_h());
  1327. }
  1328. _Local_iterator_base&
  1329. operator=(const _Local_iterator_base& __iter)
  1330. {
  1331. if (_M_bucket_count != -1)
  1332. _M_destroy();
  1333. _M_cur = __iter._M_cur;
  1334. _M_bucket = __iter._M_bucket;
  1335. _M_bucket_count = __iter._M_bucket_count;
  1336. if (_M_bucket_count != -1)
  1337. _M_init(*__iter._M_h());
  1338. return *this;
  1339. }
  1340. void
  1341. _M_incr()
  1342. {
  1343. _M_cur = _M_cur->_M_next();
  1344. if (_M_cur)
  1345. {
  1346. std::size_t __bkt = this->_M_h()->_M_bucket_index(_M_cur,
  1347. _M_bucket_count);
  1348. if (__bkt != _M_bucket)
  1349. _M_cur = nullptr;
  1350. }
  1351. }
  1352. _Hash_node<_Value, false>* _M_cur;
  1353. std::size_t _M_bucket;
  1354. std::size_t _M_bucket_count;
  1355. void
  1356. _M_init(const __hash_code_base& __base)
  1357. { ::new(this->_M_h()) __hash_code_base(__base); }
  1358. void
  1359. _M_destroy() { this->_M_h()->~__hash_code_base(); }
  1360. public:
  1361. const void*
  1362. _M_curr() const { return _M_cur; } // for equality ops and debug mode
  1363. std::size_t
  1364. _M_get_bucket() const { return _M_bucket; } // for debug mode
  1365. };
  1366. template<typename _Key, typename _Value, typename _ExtractKey,
  1367. typename _H1, typename _H2, typename _Hash, bool __cache>
  1368. inline bool
  1369. operator==(const _Local_iterator_base<_Key, _Value, _ExtractKey,
  1370. _H1, _H2, _Hash, __cache>& __x,
  1371. const _Local_iterator_base<_Key, _Value, _ExtractKey,
  1372. _H1, _H2, _Hash, __cache>& __y)
  1373. { return __x._M_curr() == __y._M_curr(); }
  1374. template<typename _Key, typename _Value, typename _ExtractKey,
  1375. typename _H1, typename _H2, typename _Hash, bool __cache>
  1376. inline bool
  1377. operator!=(const _Local_iterator_base<_Key, _Value, _ExtractKey,
  1378. _H1, _H2, _Hash, __cache>& __x,
  1379. const _Local_iterator_base<_Key, _Value, _ExtractKey,
  1380. _H1, _H2, _Hash, __cache>& __y)
  1381. { return __x._M_curr() != __y._M_curr(); }
  1382. /// local iterators
  1383. template<typename _Key, typename _Value, typename _ExtractKey,
  1384. typename _H1, typename _H2, typename _Hash,
  1385. bool __constant_iterators, bool __cache>
  1386. struct _Local_iterator
  1387. : public _Local_iterator_base<_Key, _Value, _ExtractKey,
  1388. _H1, _H2, _Hash, __cache>
  1389. {
  1390. private:
  1391. using __base_type = _Local_iterator_base<_Key, _Value, _ExtractKey,
  1392. _H1, _H2, _Hash, __cache>;
  1393. using __hash_code_base = typename __base_type::__hash_code_base;
  1394. public:
  1395. typedef _Value value_type;
  1396. typedef typename std::conditional<__constant_iterators,
  1397. const _Value*, _Value*>::type
  1398. pointer;
  1399. typedef typename std::conditional<__constant_iterators,
  1400. const _Value&, _Value&>::type
  1401. reference;
  1402. typedef std::ptrdiff_t difference_type;
  1403. typedef std::forward_iterator_tag iterator_category;
  1404. _Local_iterator() = default;
  1405. _Local_iterator(const __hash_code_base& __base,
  1406. _Hash_node<_Value, __cache>* __p,
  1407. std::size_t __bkt, std::size_t __bkt_count)
  1408. : __base_type(__base, __p, __bkt, __bkt_count)
  1409. { }
  1410. reference
  1411. operator*() const
  1412. { return this->_M_cur->_M_v(); }
  1413. pointer
  1414. operator->() const
  1415. { return this->_M_cur->_M_valptr(); }
  1416. _Local_iterator&
  1417. operator++()
  1418. {
  1419. this->_M_incr();
  1420. return *this;
  1421. }
  1422. _Local_iterator
  1423. operator++(int)
  1424. {
  1425. _Local_iterator __tmp(*this);
  1426. this->_M_incr();
  1427. return __tmp;
  1428. }
  1429. };
  1430. /// local const_iterators
  1431. template<typename _Key, typename _Value, typename _ExtractKey,
  1432. typename _H1, typename _H2, typename _Hash,
  1433. bool __constant_iterators, bool __cache>
  1434. struct _Local_const_iterator
  1435. : public _Local_iterator_base<_Key, _Value, _ExtractKey,
  1436. _H1, _H2, _Hash, __cache>
  1437. {
  1438. private:
  1439. using __base_type = _Local_iterator_base<_Key, _Value, _ExtractKey,
  1440. _H1, _H2, _Hash, __cache>;
  1441. using __hash_code_base = typename __base_type::__hash_code_base;
  1442. public:
  1443. typedef _Value value_type;
  1444. typedef const _Value* pointer;
  1445. typedef const _Value& reference;
  1446. typedef std::ptrdiff_t difference_type;
  1447. typedef std::forward_iterator_tag iterator_category;
  1448. _Local_const_iterator() = default;
  1449. _Local_const_iterator(const __hash_code_base& __base,
  1450. _Hash_node<_Value, __cache>* __p,
  1451. std::size_t __bkt, std::size_t __bkt_count)
  1452. : __base_type(__base, __p, __bkt, __bkt_count)
  1453. { }
  1454. _Local_const_iterator(const _Local_iterator<_Key, _Value, _ExtractKey,
  1455. _H1, _H2, _Hash,
  1456. __constant_iterators,
  1457. __cache>& __x)
  1458. : __base_type(__x)
  1459. { }
  1460. reference
  1461. operator*() const
  1462. { return this->_M_cur->_M_v(); }
  1463. pointer
  1464. operator->() const
  1465. { return this->_M_cur->_M_valptr(); }
  1466. _Local_const_iterator&
  1467. operator++()
  1468. {
  1469. this->_M_incr();
  1470. return *this;
  1471. }
  1472. _Local_const_iterator
  1473. operator++(int)
  1474. {
  1475. _Local_const_iterator __tmp(*this);
  1476. this->_M_incr();
  1477. return __tmp;
  1478. }
  1479. };
  1480. /**
  1481. * Primary class template _Hashtable_base.
  1482. *
  1483. * Helper class adding management of _Equal functor to
  1484. * _Hash_code_base type.
  1485. *
  1486. * Base class templates are:
  1487. * - __detail::_Hash_code_base
  1488. * - __detail::_Hashtable_ebo_helper
  1489. */
  1490. template<typename _Key, typename _Value,
  1491. typename _ExtractKey, typename _Equal,
  1492. typename _H1, typename _H2, typename _Hash, typename _Traits>
  1493. struct _Hashtable_base
  1494. : public _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
  1495. _Traits::__hash_cached::value>,
  1496. private _Hashtable_ebo_helper<0, _Equal>
  1497. {
  1498. public:
  1499. typedef _Key key_type;
  1500. typedef _Value value_type;
  1501. typedef _Equal key_equal;
  1502. typedef std::size_t size_type;
  1503. typedef std::ptrdiff_t difference_type;
  1504. using __traits_type = _Traits;
  1505. using __hash_cached = typename __traits_type::__hash_cached;
  1506. using __constant_iterators = typename __traits_type::__constant_iterators;
  1507. using __unique_keys = typename __traits_type::__unique_keys;
  1508. using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
  1509. _H1, _H2, _Hash,
  1510. __hash_cached::value>;
  1511. using __hash_code = typename __hash_code_base::__hash_code;
  1512. using __node_type = typename __hash_code_base::__node_type;
  1513. using iterator = __detail::_Node_iterator<value_type,
  1514. __constant_iterators::value,
  1515. __hash_cached::value>;
  1516. using const_iterator = __detail::_Node_const_iterator<value_type,
  1517. __constant_iterators::value,
  1518. __hash_cached::value>;
  1519. using local_iterator = __detail::_Local_iterator<key_type, value_type,
  1520. _ExtractKey, _H1, _H2, _Hash,
  1521. __constant_iterators::value,
  1522. __hash_cached::value>;
  1523. using const_local_iterator = __detail::_Local_const_iterator<key_type,
  1524. value_type,
  1525. _ExtractKey, _H1, _H2, _Hash,
  1526. __constant_iterators::value,
  1527. __hash_cached::value>;
  1528. using __ireturn_type = typename std::conditional<__unique_keys::value,
  1529. std::pair<iterator, bool>,
  1530. iterator>::type;
  1531. private:
  1532. using _EqualEBO = _Hashtable_ebo_helper<0, _Equal>;
  1533. using _EqualHelper = _Equal_helper<_Key, _Value, _ExtractKey, _Equal,
  1534. __hash_code, __hash_cached::value>;
  1535. protected:
  1536. _Hashtable_base() = default;
  1537. _Hashtable_base(const _ExtractKey& __ex, const _H1& __h1, const _H2& __h2,
  1538. const _Hash& __hash, const _Equal& __eq)
  1539. : __hash_code_base(__ex, __h1, __h2, __hash), _EqualEBO(__eq)
  1540. { }
  1541. bool
  1542. _M_equals(const _Key& __k, __hash_code __c, __node_type* __n) const
  1543. {
  1544. static_assert(__is_invocable<const _Equal&, const _Key&, const _Key&>{},
  1545. "key equality predicate must be invocable with two arguments of "
  1546. "key type");
  1547. return _EqualHelper::_S_equals(_M_eq(), this->_M_extract(),
  1548. __k, __c, __n);
  1549. }
  1550. void
  1551. _M_swap(_Hashtable_base& __x)
  1552. {
  1553. __hash_code_base::_M_swap(__x);
  1554. std::swap(_M_eq(), __x._M_eq());
  1555. }
  1556. const _Equal&
  1557. _M_eq() const { return _EqualEBO::_S_cget(*this); }
  1558. _Equal&
  1559. _M_eq() { return _EqualEBO::_S_get(*this); }
  1560. };
  1561. /**
  1562. * struct _Equality_base.
  1563. *
  1564. * Common types and functions for class _Equality.
  1565. */
  1566. struct _Equality_base
  1567. {
  1568. protected:
  1569. template<typename _Uiterator>
  1570. static bool
  1571. _S_is_permutation(_Uiterator, _Uiterator, _Uiterator);
  1572. };
  1573. // See std::is_permutation in N3068.
  1574. template<typename _Uiterator>
  1575. bool
  1576. _Equality_base::
  1577. _S_is_permutation(_Uiterator __first1, _Uiterator __last1,
  1578. _Uiterator __first2)
  1579. {
  1580. for (; __first1 != __last1; ++__first1, ++__first2)
  1581. if (!(*__first1 == *__first2))
  1582. break;
  1583. if (__first1 == __last1)
  1584. return true;
  1585. _Uiterator __last2 = __first2;
  1586. std::advance(__last2, std::distance(__first1, __last1));
  1587. for (_Uiterator __it1 = __first1; __it1 != __last1; ++__it1)
  1588. {
  1589. _Uiterator __tmp = __first1;
  1590. while (__tmp != __it1 && !bool(*__tmp == *__it1))
  1591. ++__tmp;
  1592. // We've seen this one before.
  1593. if (__tmp != __it1)
  1594. continue;
  1595. std::ptrdiff_t __n2 = 0;
  1596. for (__tmp = __first2; __tmp != __last2; ++__tmp)
  1597. if (*__tmp == *__it1)
  1598. ++__n2;
  1599. if (!__n2)
  1600. return false;
  1601. std::ptrdiff_t __n1 = 0;
  1602. for (__tmp = __it1; __tmp != __last1; ++__tmp)
  1603. if (*__tmp == *__it1)
  1604. ++__n1;
  1605. if (__n1 != __n2)
  1606. return false;
  1607. }
  1608. return true;
  1609. }
  1610. /**
  1611. * Primary class template _Equality.
  1612. *
  1613. * This is for implementing equality comparison for unordered
  1614. * containers, per N3068, by John Lakos and Pablo Halpern.
  1615. * Algorithmically, we follow closely the reference implementations
  1616. * therein.
  1617. */
  1618. template<typename _Key, typename _Value, typename _Alloc,
  1619. typename _ExtractKey, typename _Equal,
  1620. typename _H1, typename _H2, typename _Hash,
  1621. typename _RehashPolicy, typename _Traits,
  1622. bool _Unique_keys = _Traits::__unique_keys::value>
  1623. struct _Equality;
  1624. /// Specialization.
  1625. template<typename _Key, typename _Value, typename _Alloc,
  1626. typename _ExtractKey, typename _Equal,
  1627. typename _H1, typename _H2, typename _Hash,
  1628. typename _RehashPolicy, typename _Traits>
  1629. struct _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1630. _H1, _H2, _Hash, _RehashPolicy, _Traits, true>
  1631. {
  1632. using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1633. _H1, _H2, _Hash, _RehashPolicy, _Traits>;
  1634. bool
  1635. _M_equal(const __hashtable&) const;
  1636. };
  1637. template<typename _Key, typename _Value, typename _Alloc,
  1638. typename _ExtractKey, typename _Equal,
  1639. typename _H1, typename _H2, typename _Hash,
  1640. typename _RehashPolicy, typename _Traits>
  1641. bool
  1642. _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1643. _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
  1644. _M_equal(const __hashtable& __other) const
  1645. {
  1646. const __hashtable* __this = static_cast<const __hashtable*>(this);
  1647. if (__this->size() != __other.size())
  1648. return false;
  1649. for (auto __itx = __this->begin(); __itx != __this->end(); ++__itx)
  1650. {
  1651. const auto __ity = __other.find(_ExtractKey()(*__itx));
  1652. if (__ity == __other.end() || !bool(*__ity == *__itx))
  1653. return false;
  1654. }
  1655. return true;
  1656. }
  1657. /// Specialization.
  1658. template<typename _Key, typename _Value, typename _Alloc,
  1659. typename _ExtractKey, typename _Equal,
  1660. typename _H1, typename _H2, typename _Hash,
  1661. typename _RehashPolicy, typename _Traits>
  1662. struct _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1663. _H1, _H2, _Hash, _RehashPolicy, _Traits, false>
  1664. : public _Equality_base
  1665. {
  1666. using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1667. _H1, _H2, _Hash, _RehashPolicy, _Traits>;
  1668. bool
  1669. _M_equal(const __hashtable&) const;
  1670. };
  1671. template<typename _Key, typename _Value, typename _Alloc,
  1672. typename _ExtractKey, typename _Equal,
  1673. typename _H1, typename _H2, typename _Hash,
  1674. typename _RehashPolicy, typename _Traits>
  1675. bool
  1676. _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1677. _H1, _H2, _Hash, _RehashPolicy, _Traits, false>::
  1678. _M_equal(const __hashtable& __other) const
  1679. {
  1680. const __hashtable* __this = static_cast<const __hashtable*>(this);
  1681. if (__this->size() != __other.size())
  1682. return false;
  1683. for (auto __itx = __this->begin(); __itx != __this->end();)
  1684. {
  1685. const auto __xrange = __this->equal_range(_ExtractKey()(*__itx));
  1686. const auto __yrange = __other.equal_range(_ExtractKey()(*__itx));
  1687. if (std::distance(__xrange.first, __xrange.second)
  1688. != std::distance(__yrange.first, __yrange.second))
  1689. return false;
  1690. if (!_S_is_permutation(__xrange.first, __xrange.second,
  1691. __yrange.first))
  1692. return false;
  1693. __itx = __xrange.second;
  1694. }
  1695. return true;
  1696. }
  1697. /**
  1698. * This type deals with all allocation and keeps an allocator instance through
  1699. * inheritance to benefit from EBO when possible.
  1700. */
  1701. template<typename _NodeAlloc>
  1702. struct _Hashtable_alloc : private _Hashtable_ebo_helper<0, _NodeAlloc>
  1703. {
  1704. private:
  1705. using __ebo_node_alloc = _Hashtable_ebo_helper<0, _NodeAlloc>;
  1706. public:
  1707. using __node_type = typename _NodeAlloc::value_type;
  1708. using __node_alloc_type = _NodeAlloc;
  1709. // Use __gnu_cxx to benefit from _S_always_equal and al.
  1710. using __node_alloc_traits = __gnu_cxx::__alloc_traits<__node_alloc_type>;
  1711. using __value_alloc_traits = typename __node_alloc_traits::template
  1712. rebind_traits<typename __node_type::value_type>;
  1713. using __node_base = __detail::_Hash_node_base;
  1714. using __bucket_type = __node_base*;
  1715. using __bucket_alloc_type =
  1716. __alloc_rebind<__node_alloc_type, __bucket_type>;
  1717. using __bucket_alloc_traits = std::allocator_traits<__bucket_alloc_type>;
  1718. _Hashtable_alloc() = default;
  1719. _Hashtable_alloc(const _Hashtable_alloc&) = default;
  1720. _Hashtable_alloc(_Hashtable_alloc&&) = default;
  1721. template<typename _Alloc>
  1722. _Hashtable_alloc(_Alloc&& __a)
  1723. : __ebo_node_alloc(std::forward<_Alloc>(__a))
  1724. { }
  1725. __node_alloc_type&
  1726. _M_node_allocator()
  1727. { return __ebo_node_alloc::_S_get(*this); }
  1728. const __node_alloc_type&
  1729. _M_node_allocator() const
  1730. { return __ebo_node_alloc::_S_cget(*this); }
  1731. template<typename... _Args>
  1732. __node_type*
  1733. _M_allocate_node(_Args&&... __args);
  1734. void
  1735. _M_deallocate_node(__node_type* __n);
  1736. void
  1737. _M_deallocate_node_ptr(__node_type* __n);
  1738. // Deallocate the linked list of nodes pointed to by __n
  1739. void
  1740. _M_deallocate_nodes(__node_type* __n);
  1741. __bucket_type*
  1742. _M_allocate_buckets(std::size_t __n);
  1743. void
  1744. _M_deallocate_buckets(__bucket_type*, std::size_t __n);
  1745. };
  1746. // Definitions of class template _Hashtable_alloc's out-of-line member
  1747. // functions.
  1748. template<typename _NodeAlloc>
  1749. template<typename... _Args>
  1750. typename _Hashtable_alloc<_NodeAlloc>::__node_type*
  1751. _Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&&... __args)
  1752. {
  1753. auto __nptr = __node_alloc_traits::allocate(_M_node_allocator(), 1);
  1754. __node_type* __n = std::__to_address(__nptr);
  1755. __try
  1756. {
  1757. ::new ((void*)__n) __node_type;
  1758. __node_alloc_traits::construct(_M_node_allocator(),
  1759. __n->_M_valptr(),
  1760. std::forward<_Args>(__args)...);
  1761. return __n;
  1762. }
  1763. __catch(...)
  1764. {
  1765. __node_alloc_traits::deallocate(_M_node_allocator(), __nptr, 1);
  1766. __throw_exception_again;
  1767. }
  1768. }
  1769. template<typename _NodeAlloc>
  1770. void
  1771. _Hashtable_alloc<_NodeAlloc>::_M_deallocate_node(__node_type* __n)
  1772. {
  1773. __node_alloc_traits::destroy(_M_node_allocator(), __n->_M_valptr());
  1774. _M_deallocate_node_ptr(__n);
  1775. }
  1776. template<typename _NodeAlloc>
  1777. void
  1778. _Hashtable_alloc<_NodeAlloc>::_M_deallocate_node_ptr(__node_type* __n)
  1779. {
  1780. typedef typename __node_alloc_traits::pointer _Ptr;
  1781. auto __ptr = std::pointer_traits<_Ptr>::pointer_to(*__n);
  1782. __n->~__node_type();
  1783. __node_alloc_traits::deallocate(_M_node_allocator(), __ptr, 1);
  1784. }
  1785. template<typename _NodeAlloc>
  1786. void
  1787. _Hashtable_alloc<_NodeAlloc>::_M_deallocate_nodes(__node_type* __n)
  1788. {
  1789. while (__n)
  1790. {
  1791. __node_type* __tmp = __n;
  1792. __n = __n->_M_next();
  1793. _M_deallocate_node(__tmp);
  1794. }
  1795. }
  1796. template<typename _NodeAlloc>
  1797. typename _Hashtable_alloc<_NodeAlloc>::__bucket_type*
  1798. _Hashtable_alloc<_NodeAlloc>::_M_allocate_buckets(std::size_t __n)
  1799. {
  1800. __bucket_alloc_type __alloc(_M_node_allocator());
  1801. auto __ptr = __bucket_alloc_traits::allocate(__alloc, __n);
  1802. __bucket_type* __p = std::__to_address(__ptr);
  1803. __builtin_memset(__p, 0, __n * sizeof(__bucket_type));
  1804. return __p;
  1805. }
  1806. template<typename _NodeAlloc>
  1807. void
  1808. _Hashtable_alloc<_NodeAlloc>::_M_deallocate_buckets(__bucket_type* __bkts,
  1809. std::size_t __n)
  1810. {
  1811. typedef typename __bucket_alloc_traits::pointer _Ptr;
  1812. auto __ptr = std::pointer_traits<_Ptr>::pointer_to(*__bkts);
  1813. __bucket_alloc_type __alloc(_M_node_allocator());
  1814. __bucket_alloc_traits::deallocate(__alloc, __ptr, __n);
  1815. }
  1816. //@} hashtable-detail
  1817. } // namespace __detail
  1818. _GLIBCXX_END_NAMESPACE_VERSION
  1819. } // namespace std
  1820. #endif // _HASHTABLE_POLICY_H