hashtable_policy.h 66 KB

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