hashtable.h 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097
  1. // hashtable.h header -*- C++ -*-
  2. // Copyright (C) 2007-2015 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.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{unordered_map, unordered_set}
  23. */
  24. #ifndef _HASHTABLE_H
  25. #define _HASHTABLE_H 1
  26. #pragma GCC system_header
  27. #include <bits/hashtable_policy.h>
  28. namespace std _GLIBCXX_VISIBILITY(default)
  29. {
  30. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  31. template<typename _Tp, typename _Hash>
  32. using __cache_default
  33. = __not_<__and_<// Do not cache for fast hasher.
  34. __is_fast_hash<_Hash>,
  35. // Mandatory to have erase not throwing.
  36. __detail::__is_noexcept_hash<_Tp, _Hash>>>;
  37. /**
  38. * Primary class template _Hashtable.
  39. *
  40. * @ingroup hashtable-detail
  41. *
  42. * @tparam _Value CopyConstructible type.
  43. *
  44. * @tparam _Key CopyConstructible type.
  45. *
  46. * @tparam _Alloc An allocator type
  47. * ([lib.allocator.requirements]) whose _Alloc::value_type is
  48. * _Value. As a conforming extension, we allow for
  49. * _Alloc::value_type != _Value.
  50. *
  51. * @tparam _ExtractKey Function object that takes an object of type
  52. * _Value and returns a value of type _Key.
  53. *
  54. * @tparam _Equal Function object that takes two objects of type k
  55. * and returns a bool-like value that is true if the two objects
  56. * are considered equal.
  57. *
  58. * @tparam _H1 The hash function. A unary function object with
  59. * argument type _Key and result type size_t. Return values should
  60. * be distributed over the entire range [0, numeric_limits<size_t>:::max()].
  61. *
  62. * @tparam _H2 The range-hashing function (in the terminology of
  63. * Tavori and Dreizin). A binary function object whose argument
  64. * types and result type are all size_t. Given arguments r and N,
  65. * the return value is in the range [0, N).
  66. *
  67. * @tparam _Hash The ranged hash function (Tavori and Dreizin). A
  68. * binary function whose argument types are _Key and size_t and
  69. * whose result type is size_t. Given arguments k and N, the
  70. * return value is in the range [0, N). Default: hash(k, N) =
  71. * h2(h1(k), N). If _Hash is anything other than the default, _H1
  72. * and _H2 are ignored.
  73. *
  74. * @tparam _RehashPolicy Policy class with three members, all of
  75. * which govern the bucket count. _M_next_bkt(n) returns a bucket
  76. * count no smaller than n. _M_bkt_for_elements(n) returns a
  77. * bucket count appropriate for an element count of n.
  78. * _M_need_rehash(n_bkt, n_elt, n_ins) determines whether, if the
  79. * current bucket count is n_bkt and the current element count is
  80. * n_elt, we need to increase the bucket count. If so, returns
  81. * make_pair(true, n), where n is the new bucket count. If not,
  82. * returns make_pair(false, <anything>)
  83. *
  84. * @tparam _Traits Compile-time class with three boolean
  85. * std::integral_constant members: __cache_hash_code, __constant_iterators,
  86. * __unique_keys.
  87. *
  88. * Each _Hashtable data structure has:
  89. *
  90. * - _Bucket[] _M_buckets
  91. * - _Hash_node_base _M_before_begin
  92. * - size_type _M_bucket_count
  93. * - size_type _M_element_count
  94. *
  95. * with _Bucket being _Hash_node* and _Hash_node containing:
  96. *
  97. * - _Hash_node* _M_next
  98. * - Tp _M_value
  99. * - size_t _M_hash_code if cache_hash_code is true
  100. *
  101. * In terms of Standard containers the hashtable is like the aggregation of:
  102. *
  103. * - std::forward_list<_Node> containing the elements
  104. * - std::vector<std::forward_list<_Node>::iterator> representing the buckets
  105. *
  106. * The non-empty buckets contain the node before the first node in the
  107. * bucket. This design makes it possible to implement something like a
  108. * std::forward_list::insert_after on container insertion and
  109. * std::forward_list::erase_after on container erase
  110. * calls. _M_before_begin is equivalent to
  111. * std::forward_list::before_begin. Empty buckets contain
  112. * nullptr. Note that one of the non-empty buckets contains
  113. * &_M_before_begin which is not a dereferenceable node so the
  114. * node pointer in a bucket shall never be dereferenced, only its
  115. * next node can be.
  116. *
  117. * Walking through a bucket's nodes requires a check on the hash code to
  118. * see if each node is still in the bucket. Such a design assumes a
  119. * quite efficient hash functor and is one of the reasons it is
  120. * highly advisable to set __cache_hash_code to true.
  121. *
  122. * The container iterators are simply built from nodes. This way
  123. * incrementing the iterator is perfectly efficient independent of
  124. * how many empty buckets there are in the container.
  125. *
  126. * On insert we compute the element's hash code and use it to find the
  127. * bucket index. If the element must be inserted in an empty bucket
  128. * we add it at the beginning of the singly linked list and make the
  129. * bucket point to _M_before_begin. The bucket that used to point to
  130. * _M_before_begin, if any, is updated to point to its new before
  131. * begin node.
  132. *
  133. * On erase, the simple iterator design requires using the hash
  134. * functor to get the index of the bucket to update. For this
  135. * reason, when __cache_hash_code is set to false the hash functor must
  136. * not throw and this is enforced by a static assertion.
  137. *
  138. * Functionality is implemented by decomposition into base classes,
  139. * where the derived _Hashtable class is used in _Map_base,
  140. * _Insert, _Rehash_base, and _Equality base classes to access the
  141. * "this" pointer. _Hashtable_base is used in the base classes as a
  142. * non-recursive, fully-completed-type so that detailed nested type
  143. * information, such as iterator type and node type, can be
  144. * used. This is similar to the "Curiously Recurring Template
  145. * Pattern" (CRTP) technique, but uses a reconstructed, not
  146. * explicitly passed, template pattern.
  147. *
  148. * Base class templates are:
  149. * - __detail::_Hashtable_base
  150. * - __detail::_Map_base
  151. * - __detail::_Insert
  152. * - __detail::_Rehash_base
  153. * - __detail::_Equality
  154. */
  155. template<typename _Key, typename _Value, typename _Alloc,
  156. typename _ExtractKey, typename _Equal,
  157. typename _H1, typename _H2, typename _Hash,
  158. typename _RehashPolicy, typename _Traits>
  159. class _Hashtable
  160. : public __detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal,
  161. _H1, _H2, _Hash, _Traits>,
  162. public __detail::_Map_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  163. _H1, _H2, _Hash, _RehashPolicy, _Traits>,
  164. public __detail::_Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  165. _H1, _H2, _Hash, _RehashPolicy, _Traits>,
  166. public __detail::_Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  167. _H1, _H2, _Hash, _RehashPolicy, _Traits>,
  168. public __detail::_Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  169. _H1, _H2, _Hash, _RehashPolicy, _Traits>,
  170. private __detail::_Hashtable_alloc<
  171. typename __alloctr_rebind<_Alloc,
  172. __detail::_Hash_node<_Value,
  173. _Traits::__hash_cached::value> >::__type>
  174. {
  175. using __traits_type = _Traits;
  176. using __hash_cached = typename __traits_type::__hash_cached;
  177. using __node_type = __detail::_Hash_node<_Value, __hash_cached::value>;
  178. using __node_alloc_type =
  179. typename __alloctr_rebind<_Alloc, __node_type>::__type;
  180. using __hashtable_alloc = __detail::_Hashtable_alloc<__node_alloc_type>;
  181. using __value_alloc_traits =
  182. typename __hashtable_alloc::__value_alloc_traits;
  183. using __node_alloc_traits =
  184. typename __hashtable_alloc::__node_alloc_traits;
  185. using __node_base = typename __hashtable_alloc::__node_base;
  186. using __bucket_type = typename __hashtable_alloc::__bucket_type;
  187. public:
  188. typedef _Key key_type;
  189. typedef _Value value_type;
  190. typedef _Alloc allocator_type;
  191. typedef _Equal key_equal;
  192. // mapped_type, if present, comes from _Map_base.
  193. // hasher, if present, comes from _Hash_code_base/_Hashtable_base.
  194. typedef typename __value_alloc_traits::pointer pointer;
  195. typedef typename __value_alloc_traits::const_pointer const_pointer;
  196. typedef value_type& reference;
  197. typedef const value_type& const_reference;
  198. private:
  199. using __rehash_type = _RehashPolicy;
  200. using __rehash_state = typename __rehash_type::_State;
  201. using __constant_iterators = typename __traits_type::__constant_iterators;
  202. using __unique_keys = typename __traits_type::__unique_keys;
  203. using __key_extract = typename std::conditional<
  204. __constant_iterators::value,
  205. __detail::_Identity,
  206. __detail::_Select1st>::type;
  207. using __hashtable_base = __detail::
  208. _Hashtable_base<_Key, _Value, _ExtractKey,
  209. _Equal, _H1, _H2, _Hash, _Traits>;
  210. using __hash_code_base = typename __hashtable_base::__hash_code_base;
  211. using __hash_code = typename __hashtable_base::__hash_code;
  212. using __ireturn_type = typename __hashtable_base::__ireturn_type;
  213. using __map_base = __detail::_Map_base<_Key, _Value, _Alloc, _ExtractKey,
  214. _Equal, _H1, _H2, _Hash,
  215. _RehashPolicy, _Traits>;
  216. using __rehash_base = __detail::_Rehash_base<_Key, _Value, _Alloc,
  217. _ExtractKey, _Equal,
  218. _H1, _H2, _Hash,
  219. _RehashPolicy, _Traits>;
  220. using __eq_base = __detail::_Equality<_Key, _Value, _Alloc, _ExtractKey,
  221. _Equal, _H1, _H2, _Hash,
  222. _RehashPolicy, _Traits>;
  223. using __reuse_or_alloc_node_type =
  224. __detail::_ReuseOrAllocNode<__node_alloc_type>;
  225. // Metaprogramming for picking apart hash caching.
  226. template<typename _Cond>
  227. using __if_hash_cached = __or_<__not_<__hash_cached>, _Cond>;
  228. template<typename _Cond>
  229. using __if_hash_not_cached = __or_<__hash_cached, _Cond>;
  230. // Compile-time diagnostics.
  231. // _Hash_code_base has everything protected, so use this derived type to
  232. // access it.
  233. struct __hash_code_base_access : __hash_code_base
  234. { using __hash_code_base::_M_bucket_index; };
  235. // Getting a bucket index from a node shall not throw because it is used
  236. // in methods (erase, swap...) that shall not throw.
  237. static_assert(noexcept(declval<const __hash_code_base_access&>()
  238. ._M_bucket_index((const __node_type*)nullptr,
  239. (std::size_t)0)),
  240. "Cache the hash code or qualify your functors involved"
  241. " in hash code and bucket index computation with noexcept");
  242. // Following two static assertions are necessary to guarantee
  243. // that local_iterator will be default constructible.
  244. // When hash codes are cached local iterator inherits from H2 functor
  245. // which must then be default constructible.
  246. static_assert(__if_hash_cached<is_default_constructible<_H2>>::value,
  247. "Functor used to map hash code to bucket index"
  248. " must be default constructible");
  249. template<typename _Keya, typename _Valuea, typename _Alloca,
  250. typename _ExtractKeya, typename _Equala,
  251. typename _H1a, typename _H2a, typename _Hasha,
  252. typename _RehashPolicya, typename _Traitsa,
  253. bool _Unique_keysa>
  254. friend struct __detail::_Map_base;
  255. template<typename _Keya, typename _Valuea, typename _Alloca,
  256. typename _ExtractKeya, typename _Equala,
  257. typename _H1a, typename _H2a, typename _Hasha,
  258. typename _RehashPolicya, typename _Traitsa>
  259. friend struct __detail::_Insert_base;
  260. template<typename _Keya, typename _Valuea, typename _Alloca,
  261. typename _ExtractKeya, typename _Equala,
  262. typename _H1a, typename _H2a, typename _Hasha,
  263. typename _RehashPolicya, typename _Traitsa,
  264. bool _Constant_iteratorsa, bool _Unique_keysa>
  265. friend struct __detail::_Insert;
  266. public:
  267. using size_type = typename __hashtable_base::size_type;
  268. using difference_type = typename __hashtable_base::difference_type;
  269. using iterator = typename __hashtable_base::iterator;
  270. using const_iterator = typename __hashtable_base::const_iterator;
  271. using local_iterator = typename __hashtable_base::local_iterator;
  272. using const_local_iterator = typename __hashtable_base::
  273. const_local_iterator;
  274. private:
  275. __bucket_type* _M_buckets = &_M_single_bucket;
  276. size_type _M_bucket_count = 1;
  277. __node_base _M_before_begin;
  278. size_type _M_element_count = 0;
  279. _RehashPolicy _M_rehash_policy;
  280. // A single bucket used when only need for 1 bucket. Especially
  281. // interesting in move semantic to leave hashtable with only 1 buckets
  282. // which is not allocated so that we can have those operations noexcept
  283. // qualified.
  284. // Note that we can't leave hashtable with 0 bucket without adding
  285. // numerous checks in the code to avoid 0 modulus.
  286. __bucket_type _M_single_bucket = nullptr;
  287. bool
  288. _M_uses_single_bucket(__bucket_type* __bkts) const
  289. { return __builtin_expect(__bkts == &_M_single_bucket, false); }
  290. bool
  291. _M_uses_single_bucket() const
  292. { return _M_uses_single_bucket(_M_buckets); }
  293. __hashtable_alloc&
  294. _M_base_alloc() { return *this; }
  295. __bucket_type*
  296. _M_allocate_buckets(size_type __n)
  297. {
  298. if (__builtin_expect(__n == 1, false))
  299. {
  300. _M_single_bucket = nullptr;
  301. return &_M_single_bucket;
  302. }
  303. return __hashtable_alloc::_M_allocate_buckets(__n);
  304. }
  305. void
  306. _M_deallocate_buckets(__bucket_type* __bkts, size_type __n)
  307. {
  308. if (_M_uses_single_bucket(__bkts))
  309. return;
  310. __hashtable_alloc::_M_deallocate_buckets(__bkts, __n);
  311. }
  312. void
  313. _M_deallocate_buckets()
  314. { _M_deallocate_buckets(_M_buckets, _M_bucket_count); }
  315. // Gets bucket begin, deals with the fact that non-empty buckets contain
  316. // their before begin node.
  317. __node_type*
  318. _M_bucket_begin(size_type __bkt) const;
  319. __node_type*
  320. _M_begin() const
  321. { return static_cast<__node_type*>(_M_before_begin._M_nxt); }
  322. template<typename _NodeGenerator>
  323. void
  324. _M_assign(const _Hashtable&, const _NodeGenerator&);
  325. void
  326. _M_move_assign(_Hashtable&&, std::true_type);
  327. void
  328. _M_move_assign(_Hashtable&&, std::false_type);
  329. void
  330. _M_reset() noexcept;
  331. _Hashtable(const _H1& __h1, const _H2& __h2, const _Hash& __h,
  332. const _Equal& __eq, const _ExtractKey& __exk,
  333. const allocator_type& __a)
  334. : __hashtable_base(__exk, __h1, __h2, __h, __eq),
  335. __hashtable_alloc(__node_alloc_type(__a))
  336. { }
  337. public:
  338. // Constructor, destructor, assignment, swap
  339. _Hashtable() = default;
  340. _Hashtable(size_type __bucket_hint,
  341. const _H1&, const _H2&, const _Hash&,
  342. const _Equal&, const _ExtractKey&,
  343. const allocator_type&);
  344. template<typename _InputIterator>
  345. _Hashtable(_InputIterator __first, _InputIterator __last,
  346. size_type __bucket_hint,
  347. const _H1&, const _H2&, const _Hash&,
  348. const _Equal&, const _ExtractKey&,
  349. const allocator_type&);
  350. _Hashtable(const _Hashtable&);
  351. _Hashtable(_Hashtable&&) noexcept;
  352. _Hashtable(const _Hashtable&, const allocator_type&);
  353. _Hashtable(_Hashtable&&, const allocator_type&);
  354. // Use delegating constructors.
  355. explicit
  356. _Hashtable(const allocator_type& __a)
  357. : __hashtable_alloc(__node_alloc_type(__a))
  358. { }
  359. explicit
  360. _Hashtable(size_type __n,
  361. const _H1& __hf = _H1(),
  362. const key_equal& __eql = key_equal(),
  363. const allocator_type& __a = allocator_type())
  364. : _Hashtable(__n, __hf, _H2(), _Hash(), __eql,
  365. __key_extract(), __a)
  366. { }
  367. template<typename _InputIterator>
  368. _Hashtable(_InputIterator __f, _InputIterator __l,
  369. size_type __n = 0,
  370. const _H1& __hf = _H1(),
  371. const key_equal& __eql = key_equal(),
  372. const allocator_type& __a = allocator_type())
  373. : _Hashtable(__f, __l, __n, __hf, _H2(), _Hash(), __eql,
  374. __key_extract(), __a)
  375. { }
  376. _Hashtable(initializer_list<value_type> __l,
  377. size_type __n = 0,
  378. const _H1& __hf = _H1(),
  379. const key_equal& __eql = key_equal(),
  380. const allocator_type& __a = allocator_type())
  381. : _Hashtable(__l.begin(), __l.end(), __n, __hf, _H2(), _Hash(), __eql,
  382. __key_extract(), __a)
  383. { }
  384. _Hashtable&
  385. operator=(const _Hashtable& __ht);
  386. _Hashtable&
  387. operator=(_Hashtable&& __ht)
  388. noexcept(__node_alloc_traits::_S_nothrow_move())
  389. {
  390. constexpr bool __move_storage =
  391. __node_alloc_traits::_S_propagate_on_move_assign()
  392. || __node_alloc_traits::_S_always_equal();
  393. _M_move_assign(std::move(__ht),
  394. integral_constant<bool, __move_storage>());
  395. return *this;
  396. }
  397. _Hashtable&
  398. operator=(initializer_list<value_type> __l)
  399. {
  400. __reuse_or_alloc_node_type __roan(_M_begin(), *this);
  401. _M_before_begin._M_nxt = nullptr;
  402. clear();
  403. this->_M_insert_range(__l.begin(), __l.end(), __roan);
  404. return *this;
  405. }
  406. ~_Hashtable() noexcept;
  407. void
  408. swap(_Hashtable&)
  409. noexcept(__node_alloc_traits::_S_nothrow_swap());
  410. // Basic container operations
  411. iterator
  412. begin() noexcept
  413. { return iterator(_M_begin()); }
  414. const_iterator
  415. begin() const noexcept
  416. { return const_iterator(_M_begin()); }
  417. iterator
  418. end() noexcept
  419. { return iterator(nullptr); }
  420. const_iterator
  421. end() const noexcept
  422. { return const_iterator(nullptr); }
  423. const_iterator
  424. cbegin() const noexcept
  425. { return const_iterator(_M_begin()); }
  426. const_iterator
  427. cend() const noexcept
  428. { return const_iterator(nullptr); }
  429. size_type
  430. size() const noexcept
  431. { return _M_element_count; }
  432. bool
  433. empty() const noexcept
  434. { return size() == 0; }
  435. allocator_type
  436. get_allocator() const noexcept
  437. { return allocator_type(this->_M_node_allocator()); }
  438. size_type
  439. max_size() const noexcept
  440. { return __node_alloc_traits::max_size(this->_M_node_allocator()); }
  441. // Observers
  442. key_equal
  443. key_eq() const
  444. { return this->_M_eq(); }
  445. // hash_function, if present, comes from _Hash_code_base.
  446. // Bucket operations
  447. size_type
  448. bucket_count() const noexcept
  449. { return _M_bucket_count; }
  450. size_type
  451. max_bucket_count() const noexcept
  452. { return max_size(); }
  453. size_type
  454. bucket_size(size_type __n) const
  455. { return std::distance(begin(__n), end(__n)); }
  456. size_type
  457. bucket(const key_type& __k) const
  458. { return _M_bucket_index(__k, this->_M_hash_code(__k)); }
  459. local_iterator
  460. begin(size_type __n)
  461. {
  462. return local_iterator(*this, _M_bucket_begin(__n),
  463. __n, _M_bucket_count);
  464. }
  465. local_iterator
  466. end(size_type __n)
  467. { return local_iterator(*this, nullptr, __n, _M_bucket_count); }
  468. const_local_iterator
  469. begin(size_type __n) const
  470. {
  471. return const_local_iterator(*this, _M_bucket_begin(__n),
  472. __n, _M_bucket_count);
  473. }
  474. const_local_iterator
  475. end(size_type __n) const
  476. { return const_local_iterator(*this, nullptr, __n, _M_bucket_count); }
  477. // DR 691.
  478. const_local_iterator
  479. cbegin(size_type __n) const
  480. {
  481. return const_local_iterator(*this, _M_bucket_begin(__n),
  482. __n, _M_bucket_count);
  483. }
  484. const_local_iterator
  485. cend(size_type __n) const
  486. { return const_local_iterator(*this, nullptr, __n, _M_bucket_count); }
  487. float
  488. load_factor() const noexcept
  489. {
  490. return static_cast<float>(size()) / static_cast<float>(bucket_count());
  491. }
  492. // max_load_factor, if present, comes from _Rehash_base.
  493. // Generalization of max_load_factor. Extension, not found in
  494. // TR1. Only useful if _RehashPolicy is something other than
  495. // the default.
  496. const _RehashPolicy&
  497. __rehash_policy() const
  498. { return _M_rehash_policy; }
  499. void
  500. __rehash_policy(const _RehashPolicy&);
  501. // Lookup.
  502. iterator
  503. find(const key_type& __k);
  504. const_iterator
  505. find(const key_type& __k) const;
  506. size_type
  507. count(const key_type& __k) const;
  508. std::pair<iterator, iterator>
  509. equal_range(const key_type& __k);
  510. std::pair<const_iterator, const_iterator>
  511. equal_range(const key_type& __k) const;
  512. protected:
  513. // Bucket index computation helpers.
  514. size_type
  515. _M_bucket_index(__node_type* __n) const noexcept
  516. { return __hash_code_base::_M_bucket_index(__n, _M_bucket_count); }
  517. size_type
  518. _M_bucket_index(const key_type& __k, __hash_code __c) const
  519. { return __hash_code_base::_M_bucket_index(__k, __c, _M_bucket_count); }
  520. // Find and insert helper functions and types
  521. // Find the node before the one matching the criteria.
  522. __node_base*
  523. _M_find_before_node(size_type, const key_type&, __hash_code) const;
  524. __node_type*
  525. _M_find_node(size_type __bkt, const key_type& __key,
  526. __hash_code __c) const
  527. {
  528. __node_base* __before_n = _M_find_before_node(__bkt, __key, __c);
  529. if (__before_n)
  530. return static_cast<__node_type*>(__before_n->_M_nxt);
  531. return nullptr;
  532. }
  533. // Insert a node at the beginning of a bucket.
  534. void
  535. _M_insert_bucket_begin(size_type, __node_type*);
  536. // Remove the bucket first node
  537. void
  538. _M_remove_bucket_begin(size_type __bkt, __node_type* __next_n,
  539. size_type __next_bkt);
  540. // Get the node before __n in the bucket __bkt
  541. __node_base*
  542. _M_get_previous_node(size_type __bkt, __node_base* __n);
  543. // Insert node with hash code __code, in bucket bkt if no rehash (assumes
  544. // no element with its key already present). Take ownership of the node,
  545. // deallocate it on exception.
  546. iterator
  547. _M_insert_unique_node(size_type __bkt, __hash_code __code,
  548. __node_type* __n);
  549. // Insert node with hash code __code. Take ownership of the node,
  550. // deallocate it on exception.
  551. iterator
  552. _M_insert_multi_node(__node_type* __hint,
  553. __hash_code __code, __node_type* __n);
  554. template<typename... _Args>
  555. std::pair<iterator, bool>
  556. _M_emplace(std::true_type, _Args&&... __args);
  557. template<typename... _Args>
  558. iterator
  559. _M_emplace(std::false_type __uk, _Args&&... __args)
  560. { return _M_emplace(cend(), __uk, std::forward<_Args>(__args)...); }
  561. // Emplace with hint, useless when keys are unique.
  562. template<typename... _Args>
  563. iterator
  564. _M_emplace(const_iterator, std::true_type __uk, _Args&&... __args)
  565. { return _M_emplace(__uk, std::forward<_Args>(__args)...).first; }
  566. template<typename... _Args>
  567. iterator
  568. _M_emplace(const_iterator, std::false_type, _Args&&... __args);
  569. template<typename _Arg, typename _NodeGenerator>
  570. std::pair<iterator, bool>
  571. _M_insert(_Arg&&, const _NodeGenerator&, std::true_type);
  572. template<typename _Arg, typename _NodeGenerator>
  573. iterator
  574. _M_insert(_Arg&& __arg, const _NodeGenerator& __node_gen,
  575. std::false_type __uk)
  576. {
  577. return _M_insert(cend(), std::forward<_Arg>(__arg), __node_gen,
  578. __uk);
  579. }
  580. // Insert with hint, not used when keys are unique.
  581. template<typename _Arg, typename _NodeGenerator>
  582. iterator
  583. _M_insert(const_iterator, _Arg&& __arg,
  584. const _NodeGenerator& __node_gen, std::true_type __uk)
  585. {
  586. return
  587. _M_insert(std::forward<_Arg>(__arg), __node_gen, __uk).first;
  588. }
  589. // Insert with hint when keys are not unique.
  590. template<typename _Arg, typename _NodeGenerator>
  591. iterator
  592. _M_insert(const_iterator, _Arg&&,
  593. const _NodeGenerator&, std::false_type);
  594. size_type
  595. _M_erase(std::true_type, const key_type&);
  596. size_type
  597. _M_erase(std::false_type, const key_type&);
  598. iterator
  599. _M_erase(size_type __bkt, __node_base* __prev_n, __node_type* __n);
  600. public:
  601. // Emplace
  602. template<typename... _Args>
  603. __ireturn_type
  604. emplace(_Args&&... __args)
  605. { return _M_emplace(__unique_keys(), std::forward<_Args>(__args)...); }
  606. template<typename... _Args>
  607. iterator
  608. emplace_hint(const_iterator __hint, _Args&&... __args)
  609. {
  610. return _M_emplace(__hint, __unique_keys(),
  611. std::forward<_Args>(__args)...);
  612. }
  613. // Insert member functions via inheritance.
  614. // Erase
  615. iterator
  616. erase(const_iterator);
  617. // LWG 2059.
  618. iterator
  619. erase(iterator __it)
  620. { return erase(const_iterator(__it)); }
  621. size_type
  622. erase(const key_type& __k)
  623. { return _M_erase(__unique_keys(), __k); }
  624. iterator
  625. erase(const_iterator, const_iterator);
  626. void
  627. clear() noexcept;
  628. // Set number of buckets to be appropriate for container of n element.
  629. void rehash(size_type __n);
  630. // DR 1189.
  631. // reserve, if present, comes from _Rehash_base.
  632. private:
  633. // Helper rehash method used when keys are unique.
  634. void _M_rehash_aux(size_type __n, std::true_type);
  635. // Helper rehash method used when keys can be non-unique.
  636. void _M_rehash_aux(size_type __n, std::false_type);
  637. // Unconditionally change size of bucket array to n, restore
  638. // hash policy state to __state on exception.
  639. void _M_rehash(size_type __n, const __rehash_state& __state);
  640. };
  641. // Definitions of class template _Hashtable's out-of-line member functions.
  642. template<typename _Key, typename _Value,
  643. typename _Alloc, typename _ExtractKey, typename _Equal,
  644. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  645. typename _Traits>
  646. auto
  647. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  648. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  649. _M_bucket_begin(size_type __bkt) const
  650. -> __node_type*
  651. {
  652. __node_base* __n = _M_buckets[__bkt];
  653. return __n ? static_cast<__node_type*>(__n->_M_nxt) : nullptr;
  654. }
  655. template<typename _Key, typename _Value,
  656. typename _Alloc, typename _ExtractKey, typename _Equal,
  657. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  658. typename _Traits>
  659. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  660. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  661. _Hashtable(size_type __bucket_hint,
  662. const _H1& __h1, const _H2& __h2, const _Hash& __h,
  663. const _Equal& __eq, const _ExtractKey& __exk,
  664. const allocator_type& __a)
  665. : _Hashtable(__h1, __h2, __h, __eq, __exk, __a)
  666. {
  667. auto __bkt = _M_rehash_policy._M_next_bkt(__bucket_hint);
  668. if (__bkt > _M_bucket_count)
  669. {
  670. _M_buckets = _M_allocate_buckets(__bkt);
  671. _M_bucket_count = __bkt;
  672. }
  673. }
  674. template<typename _Key, typename _Value,
  675. typename _Alloc, typename _ExtractKey, typename _Equal,
  676. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  677. typename _Traits>
  678. template<typename _InputIterator>
  679. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  680. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  681. _Hashtable(_InputIterator __f, _InputIterator __l,
  682. size_type __bucket_hint,
  683. const _H1& __h1, const _H2& __h2, const _Hash& __h,
  684. const _Equal& __eq, const _ExtractKey& __exk,
  685. const allocator_type& __a)
  686. : _Hashtable(__h1, __h2, __h, __eq, __exk, __a)
  687. {
  688. auto __nb_elems = __detail::__distance_fw(__f, __l);
  689. auto __bkt_count =
  690. _M_rehash_policy._M_next_bkt(
  691. std::max(_M_rehash_policy._M_bkt_for_elements(__nb_elems),
  692. __bucket_hint));
  693. if (__bkt_count > _M_bucket_count)
  694. {
  695. _M_buckets = _M_allocate_buckets(__bkt_count);
  696. _M_bucket_count = __bkt_count;
  697. }
  698. __try
  699. {
  700. for (; __f != __l; ++__f)
  701. this->insert(*__f);
  702. }
  703. __catch(...)
  704. {
  705. clear();
  706. _M_deallocate_buckets();
  707. __throw_exception_again;
  708. }
  709. }
  710. template<typename _Key, typename _Value,
  711. typename _Alloc, typename _ExtractKey, typename _Equal,
  712. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  713. typename _Traits>
  714. auto
  715. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  716. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  717. operator=(const _Hashtable& __ht)
  718. -> _Hashtable&
  719. {
  720. if (&__ht == this)
  721. return *this;
  722. if (__node_alloc_traits::_S_propagate_on_copy_assign())
  723. {
  724. auto& __this_alloc = this->_M_node_allocator();
  725. auto& __that_alloc = __ht._M_node_allocator();
  726. if (!__node_alloc_traits::_S_always_equal()
  727. && __this_alloc != __that_alloc)
  728. {
  729. // Replacement allocator cannot free existing storage.
  730. this->_M_deallocate_nodes(_M_begin());
  731. _M_before_begin._M_nxt = nullptr;
  732. _M_deallocate_buckets();
  733. _M_buckets = nullptr;
  734. std::__alloc_on_copy(__this_alloc, __that_alloc);
  735. __hashtable_base::operator=(__ht);
  736. _M_bucket_count = __ht._M_bucket_count;
  737. _M_element_count = __ht._M_element_count;
  738. _M_rehash_policy = __ht._M_rehash_policy;
  739. __try
  740. {
  741. _M_assign(__ht,
  742. [this](const __node_type* __n)
  743. { return this->_M_allocate_node(__n->_M_v()); });
  744. }
  745. __catch(...)
  746. {
  747. // _M_assign took care of deallocating all memory. Now we
  748. // must make sure this instance remains in a usable state.
  749. _M_reset();
  750. __throw_exception_again;
  751. }
  752. return *this;
  753. }
  754. std::__alloc_on_copy(__this_alloc, __that_alloc);
  755. }
  756. // Reuse allocated buckets and nodes.
  757. __bucket_type* __former_buckets = nullptr;
  758. std::size_t __former_bucket_count = _M_bucket_count;
  759. const __rehash_state& __former_state = _M_rehash_policy._M_state();
  760. if (_M_bucket_count != __ht._M_bucket_count)
  761. {
  762. __former_buckets = _M_buckets;
  763. _M_buckets = _M_allocate_buckets(__ht._M_bucket_count);
  764. _M_bucket_count = __ht._M_bucket_count;
  765. }
  766. else
  767. __builtin_memset(_M_buckets, 0,
  768. _M_bucket_count * sizeof(__bucket_type));
  769. __try
  770. {
  771. __hashtable_base::operator=(__ht);
  772. _M_element_count = __ht._M_element_count;
  773. _M_rehash_policy = __ht._M_rehash_policy;
  774. __reuse_or_alloc_node_type __roan(_M_begin(), *this);
  775. _M_before_begin._M_nxt = nullptr;
  776. _M_assign(__ht,
  777. [&__roan](const __node_type* __n)
  778. { return __roan(__n->_M_v()); });
  779. if (__former_buckets)
  780. _M_deallocate_buckets(__former_buckets, __former_bucket_count);
  781. }
  782. __catch(...)
  783. {
  784. if (__former_buckets)
  785. {
  786. // Restore previous buckets.
  787. _M_deallocate_buckets();
  788. _M_rehash_policy._M_reset(__former_state);
  789. _M_buckets = __former_buckets;
  790. _M_bucket_count = __former_bucket_count;
  791. }
  792. __builtin_memset(_M_buckets, 0,
  793. _M_bucket_count * sizeof(__bucket_type));
  794. __throw_exception_again;
  795. }
  796. return *this;
  797. }
  798. template<typename _Key, typename _Value,
  799. typename _Alloc, typename _ExtractKey, typename _Equal,
  800. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  801. typename _Traits>
  802. template<typename _NodeGenerator>
  803. void
  804. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  805. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  806. _M_assign(const _Hashtable& __ht, const _NodeGenerator& __node_gen)
  807. {
  808. __bucket_type* __buckets = nullptr;
  809. if (!_M_buckets)
  810. _M_buckets = __buckets = _M_allocate_buckets(_M_bucket_count);
  811. __try
  812. {
  813. if (!__ht._M_before_begin._M_nxt)
  814. return;
  815. // First deal with the special first node pointed to by
  816. // _M_before_begin.
  817. __node_type* __ht_n = __ht._M_begin();
  818. __node_type* __this_n = __node_gen(__ht_n);
  819. this->_M_copy_code(__this_n, __ht_n);
  820. _M_before_begin._M_nxt = __this_n;
  821. _M_buckets[_M_bucket_index(__this_n)] = &_M_before_begin;
  822. // Then deal with other nodes.
  823. __node_base* __prev_n = __this_n;
  824. for (__ht_n = __ht_n->_M_next(); __ht_n; __ht_n = __ht_n->_M_next())
  825. {
  826. __this_n = __node_gen(__ht_n);
  827. __prev_n->_M_nxt = __this_n;
  828. this->_M_copy_code(__this_n, __ht_n);
  829. size_type __bkt = _M_bucket_index(__this_n);
  830. if (!_M_buckets[__bkt])
  831. _M_buckets[__bkt] = __prev_n;
  832. __prev_n = __this_n;
  833. }
  834. }
  835. __catch(...)
  836. {
  837. clear();
  838. if (__buckets)
  839. _M_deallocate_buckets();
  840. __throw_exception_again;
  841. }
  842. }
  843. template<typename _Key, typename _Value,
  844. typename _Alloc, typename _ExtractKey, typename _Equal,
  845. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  846. typename _Traits>
  847. void
  848. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  849. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  850. _M_reset() noexcept
  851. {
  852. _M_rehash_policy._M_reset();
  853. _M_bucket_count = 1;
  854. _M_single_bucket = nullptr;
  855. _M_buckets = &_M_single_bucket;
  856. _M_before_begin._M_nxt = nullptr;
  857. _M_element_count = 0;
  858. }
  859. template<typename _Key, typename _Value,
  860. typename _Alloc, typename _ExtractKey, typename _Equal,
  861. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  862. typename _Traits>
  863. void
  864. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  865. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  866. _M_move_assign(_Hashtable&& __ht, std::true_type)
  867. {
  868. this->_M_deallocate_nodes(_M_begin());
  869. _M_deallocate_buckets();
  870. __hashtable_base::operator=(std::move(__ht));
  871. _M_rehash_policy = __ht._M_rehash_policy;
  872. if (!__ht._M_uses_single_bucket())
  873. _M_buckets = __ht._M_buckets;
  874. else
  875. {
  876. _M_buckets = &_M_single_bucket;
  877. _M_single_bucket = __ht._M_single_bucket;
  878. }
  879. _M_bucket_count = __ht._M_bucket_count;
  880. _M_before_begin._M_nxt = __ht._M_before_begin._M_nxt;
  881. _M_element_count = __ht._M_element_count;
  882. std::__alloc_on_move(this->_M_node_allocator(), __ht._M_node_allocator());
  883. // Fix buckets containing the _M_before_begin pointers that can't be
  884. // moved.
  885. if (_M_begin())
  886. _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
  887. __ht._M_reset();
  888. }
  889. template<typename _Key, typename _Value,
  890. typename _Alloc, typename _ExtractKey, typename _Equal,
  891. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  892. typename _Traits>
  893. void
  894. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  895. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  896. _M_move_assign(_Hashtable&& __ht, std::false_type)
  897. {
  898. if (__ht._M_node_allocator() == this->_M_node_allocator())
  899. _M_move_assign(std::move(__ht), std::true_type());
  900. else
  901. {
  902. // Can't move memory, move elements then.
  903. __bucket_type* __former_buckets = nullptr;
  904. size_type __former_bucket_count = _M_bucket_count;
  905. const __rehash_state& __former_state = _M_rehash_policy._M_state();
  906. if (_M_bucket_count != __ht._M_bucket_count)
  907. {
  908. __former_buckets = _M_buckets;
  909. _M_buckets = _M_allocate_buckets(__ht._M_bucket_count);
  910. _M_bucket_count = __ht._M_bucket_count;
  911. }
  912. else
  913. __builtin_memset(_M_buckets, 0,
  914. _M_bucket_count * sizeof(__bucket_type));
  915. __try
  916. {
  917. __hashtable_base::operator=(std::move(__ht));
  918. _M_element_count = __ht._M_element_count;
  919. _M_rehash_policy = __ht._M_rehash_policy;
  920. __reuse_or_alloc_node_type __roan(_M_begin(), *this);
  921. _M_before_begin._M_nxt = nullptr;
  922. _M_assign(__ht,
  923. [&__roan](__node_type* __n)
  924. { return __roan(std::move_if_noexcept(__n->_M_v())); });
  925. __ht.clear();
  926. }
  927. __catch(...)
  928. {
  929. if (__former_buckets)
  930. {
  931. _M_deallocate_buckets();
  932. _M_rehash_policy._M_reset(__former_state);
  933. _M_buckets = __former_buckets;
  934. _M_bucket_count = __former_bucket_count;
  935. }
  936. __builtin_memset(_M_buckets, 0,
  937. _M_bucket_count * sizeof(__bucket_type));
  938. __throw_exception_again;
  939. }
  940. }
  941. }
  942. template<typename _Key, typename _Value,
  943. typename _Alloc, typename _ExtractKey, typename _Equal,
  944. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  945. typename _Traits>
  946. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  947. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  948. _Hashtable(const _Hashtable& __ht)
  949. : __hashtable_base(__ht),
  950. __map_base(__ht),
  951. __rehash_base(__ht),
  952. __hashtable_alloc(
  953. __node_alloc_traits::_S_select_on_copy(__ht._M_node_allocator())),
  954. _M_buckets(nullptr),
  955. _M_bucket_count(__ht._M_bucket_count),
  956. _M_element_count(__ht._M_element_count),
  957. _M_rehash_policy(__ht._M_rehash_policy)
  958. {
  959. _M_assign(__ht,
  960. [this](const __node_type* __n)
  961. { return this->_M_allocate_node(__n->_M_v()); });
  962. }
  963. template<typename _Key, typename _Value,
  964. typename _Alloc, typename _ExtractKey, typename _Equal,
  965. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  966. typename _Traits>
  967. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  968. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  969. _Hashtable(_Hashtable&& __ht) noexcept
  970. : __hashtable_base(__ht),
  971. __map_base(__ht),
  972. __rehash_base(__ht),
  973. __hashtable_alloc(std::move(__ht._M_base_alloc())),
  974. _M_buckets(__ht._M_buckets),
  975. _M_bucket_count(__ht._M_bucket_count),
  976. _M_before_begin(__ht._M_before_begin._M_nxt),
  977. _M_element_count(__ht._M_element_count),
  978. _M_rehash_policy(__ht._M_rehash_policy)
  979. {
  980. // Update, if necessary, buckets if __ht is using its single bucket.
  981. if (__ht._M_uses_single_bucket())
  982. {
  983. _M_buckets = &_M_single_bucket;
  984. _M_single_bucket = __ht._M_single_bucket;
  985. }
  986. // Update, if necessary, bucket pointing to before begin that hasn't
  987. // moved.
  988. if (_M_begin())
  989. _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
  990. __ht._M_reset();
  991. }
  992. template<typename _Key, typename _Value,
  993. typename _Alloc, typename _ExtractKey, typename _Equal,
  994. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  995. typename _Traits>
  996. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  997. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  998. _Hashtable(const _Hashtable& __ht, const allocator_type& __a)
  999. : __hashtable_base(__ht),
  1000. __map_base(__ht),
  1001. __rehash_base(__ht),
  1002. __hashtable_alloc(__node_alloc_type(__a)),
  1003. _M_buckets(),
  1004. _M_bucket_count(__ht._M_bucket_count),
  1005. _M_element_count(__ht._M_element_count),
  1006. _M_rehash_policy(__ht._M_rehash_policy)
  1007. {
  1008. _M_assign(__ht,
  1009. [this](const __node_type* __n)
  1010. { return this->_M_allocate_node(__n->_M_v()); });
  1011. }
  1012. template<typename _Key, typename _Value,
  1013. typename _Alloc, typename _ExtractKey, typename _Equal,
  1014. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1015. typename _Traits>
  1016. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1017. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1018. _Hashtable(_Hashtable&& __ht, const allocator_type& __a)
  1019. : __hashtable_base(__ht),
  1020. __map_base(__ht),
  1021. __rehash_base(__ht),
  1022. __hashtable_alloc(__node_alloc_type(__a)),
  1023. _M_buckets(nullptr),
  1024. _M_bucket_count(__ht._M_bucket_count),
  1025. _M_element_count(__ht._M_element_count),
  1026. _M_rehash_policy(__ht._M_rehash_policy)
  1027. {
  1028. if (__ht._M_node_allocator() == this->_M_node_allocator())
  1029. {
  1030. if (__ht._M_uses_single_bucket())
  1031. {
  1032. _M_buckets = &_M_single_bucket;
  1033. _M_single_bucket = __ht._M_single_bucket;
  1034. }
  1035. else
  1036. _M_buckets = __ht._M_buckets;
  1037. _M_before_begin._M_nxt = __ht._M_before_begin._M_nxt;
  1038. // Update, if necessary, bucket pointing to before begin that hasn't
  1039. // moved.
  1040. if (_M_begin())
  1041. _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
  1042. __ht._M_reset();
  1043. }
  1044. else
  1045. {
  1046. _M_assign(__ht,
  1047. [this](__node_type* __n)
  1048. {
  1049. return this->_M_allocate_node(
  1050. std::move_if_noexcept(__n->_M_v()));
  1051. });
  1052. __ht.clear();
  1053. }
  1054. }
  1055. template<typename _Key, typename _Value,
  1056. typename _Alloc, typename _ExtractKey, typename _Equal,
  1057. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1058. typename _Traits>
  1059. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1060. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1061. ~_Hashtable() noexcept
  1062. {
  1063. clear();
  1064. _M_deallocate_buckets();
  1065. }
  1066. template<typename _Key, typename _Value,
  1067. typename _Alloc, typename _ExtractKey, typename _Equal,
  1068. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1069. typename _Traits>
  1070. void
  1071. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1072. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1073. swap(_Hashtable& __x)
  1074. noexcept(__node_alloc_traits::_S_nothrow_swap())
  1075. {
  1076. // The only base class with member variables is hash_code_base.
  1077. // We define _Hash_code_base::_M_swap because different
  1078. // specializations have different members.
  1079. this->_M_swap(__x);
  1080. std::__alloc_on_swap(this->_M_node_allocator(), __x._M_node_allocator());
  1081. std::swap(_M_rehash_policy, __x._M_rehash_policy);
  1082. // Deal properly with potentially moved instances.
  1083. if (this->_M_uses_single_bucket())
  1084. {
  1085. if (!__x._M_uses_single_bucket())
  1086. {
  1087. _M_buckets = __x._M_buckets;
  1088. __x._M_buckets = &__x._M_single_bucket;
  1089. }
  1090. }
  1091. else if (__x._M_uses_single_bucket())
  1092. {
  1093. __x._M_buckets = _M_buckets;
  1094. _M_buckets = &_M_single_bucket;
  1095. }
  1096. else
  1097. std::swap(_M_buckets, __x._M_buckets);
  1098. std::swap(_M_bucket_count, __x._M_bucket_count);
  1099. std::swap(_M_before_begin._M_nxt, __x._M_before_begin._M_nxt);
  1100. std::swap(_M_element_count, __x._M_element_count);
  1101. std::swap(_M_single_bucket, __x._M_single_bucket);
  1102. // Fix buckets containing the _M_before_begin pointers that can't be
  1103. // swapped.
  1104. if (_M_begin())
  1105. _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
  1106. if (__x._M_begin())
  1107. __x._M_buckets[__x._M_bucket_index(__x._M_begin())]
  1108. = &__x._M_before_begin;
  1109. }
  1110. template<typename _Key, typename _Value,
  1111. typename _Alloc, typename _ExtractKey, typename _Equal,
  1112. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1113. typename _Traits>
  1114. void
  1115. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1116. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1117. __rehash_policy(const _RehashPolicy& __pol)
  1118. {
  1119. auto __do_rehash =
  1120. __pol._M_need_rehash(_M_bucket_count, _M_element_count, 0);
  1121. if (__do_rehash.first)
  1122. _M_rehash(__do_rehash.second, _M_rehash_policy._M_state());
  1123. _M_rehash_policy = __pol;
  1124. }
  1125. template<typename _Key, typename _Value,
  1126. typename _Alloc, typename _ExtractKey, typename _Equal,
  1127. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1128. typename _Traits>
  1129. auto
  1130. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1131. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1132. find(const key_type& __k)
  1133. -> iterator
  1134. {
  1135. __hash_code __code = this->_M_hash_code(__k);
  1136. std::size_t __n = _M_bucket_index(__k, __code);
  1137. __node_type* __p = _M_find_node(__n, __k, __code);
  1138. return __p ? iterator(__p) : end();
  1139. }
  1140. template<typename _Key, typename _Value,
  1141. typename _Alloc, typename _ExtractKey, typename _Equal,
  1142. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1143. typename _Traits>
  1144. auto
  1145. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1146. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1147. find(const key_type& __k) const
  1148. -> const_iterator
  1149. {
  1150. __hash_code __code = this->_M_hash_code(__k);
  1151. std::size_t __n = _M_bucket_index(__k, __code);
  1152. __node_type* __p = _M_find_node(__n, __k, __code);
  1153. return __p ? const_iterator(__p) : end();
  1154. }
  1155. template<typename _Key, typename _Value,
  1156. typename _Alloc, typename _ExtractKey, typename _Equal,
  1157. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1158. typename _Traits>
  1159. auto
  1160. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1161. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1162. count(const key_type& __k) const
  1163. -> size_type
  1164. {
  1165. __hash_code __code = this->_M_hash_code(__k);
  1166. std::size_t __n = _M_bucket_index(__k, __code);
  1167. __node_type* __p = _M_bucket_begin(__n);
  1168. if (!__p)
  1169. return 0;
  1170. std::size_t __result = 0;
  1171. for (;; __p = __p->_M_next())
  1172. {
  1173. if (this->_M_equals(__k, __code, __p))
  1174. ++__result;
  1175. else if (__result)
  1176. // All equivalent values are next to each other, if we
  1177. // found a non-equivalent value after an equivalent one it
  1178. // means that we won't find any new equivalent value.
  1179. break;
  1180. if (!__p->_M_nxt || _M_bucket_index(__p->_M_next()) != __n)
  1181. break;
  1182. }
  1183. return __result;
  1184. }
  1185. template<typename _Key, typename _Value,
  1186. typename _Alloc, typename _ExtractKey, typename _Equal,
  1187. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1188. typename _Traits>
  1189. auto
  1190. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1191. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1192. equal_range(const key_type& __k)
  1193. -> pair<iterator, iterator>
  1194. {
  1195. __hash_code __code = this->_M_hash_code(__k);
  1196. std::size_t __n = _M_bucket_index(__k, __code);
  1197. __node_type* __p = _M_find_node(__n, __k, __code);
  1198. if (__p)
  1199. {
  1200. __node_type* __p1 = __p->_M_next();
  1201. while (__p1 && _M_bucket_index(__p1) == __n
  1202. && this->_M_equals(__k, __code, __p1))
  1203. __p1 = __p1->_M_next();
  1204. return std::make_pair(iterator(__p), iterator(__p1));
  1205. }
  1206. else
  1207. return std::make_pair(end(), end());
  1208. }
  1209. template<typename _Key, typename _Value,
  1210. typename _Alloc, typename _ExtractKey, typename _Equal,
  1211. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1212. typename _Traits>
  1213. auto
  1214. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1215. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1216. equal_range(const key_type& __k) const
  1217. -> pair<const_iterator, const_iterator>
  1218. {
  1219. __hash_code __code = this->_M_hash_code(__k);
  1220. std::size_t __n = _M_bucket_index(__k, __code);
  1221. __node_type* __p = _M_find_node(__n, __k, __code);
  1222. if (__p)
  1223. {
  1224. __node_type* __p1 = __p->_M_next();
  1225. while (__p1 && _M_bucket_index(__p1) == __n
  1226. && this->_M_equals(__k, __code, __p1))
  1227. __p1 = __p1->_M_next();
  1228. return std::make_pair(const_iterator(__p), const_iterator(__p1));
  1229. }
  1230. else
  1231. return std::make_pair(end(), end());
  1232. }
  1233. // Find the node whose key compares equal to k in the bucket n.
  1234. // Return nullptr if no node is found.
  1235. template<typename _Key, typename _Value,
  1236. typename _Alloc, typename _ExtractKey, typename _Equal,
  1237. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1238. typename _Traits>
  1239. auto
  1240. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1241. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1242. _M_find_before_node(size_type __n, const key_type& __k,
  1243. __hash_code __code) const
  1244. -> __node_base*
  1245. {
  1246. __node_base* __prev_p = _M_buckets[__n];
  1247. if (!__prev_p)
  1248. return nullptr;
  1249. for (__node_type* __p = static_cast<__node_type*>(__prev_p->_M_nxt);;
  1250. __p = __p->_M_next())
  1251. {
  1252. if (this->_M_equals(__k, __code, __p))
  1253. return __prev_p;
  1254. if (!__p->_M_nxt || _M_bucket_index(__p->_M_next()) != __n)
  1255. break;
  1256. __prev_p = __p;
  1257. }
  1258. return nullptr;
  1259. }
  1260. template<typename _Key, typename _Value,
  1261. typename _Alloc, typename _ExtractKey, typename _Equal,
  1262. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1263. typename _Traits>
  1264. void
  1265. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1266. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1267. _M_insert_bucket_begin(size_type __bkt, __node_type* __node)
  1268. {
  1269. if (_M_buckets[__bkt])
  1270. {
  1271. // Bucket is not empty, we just need to insert the new node
  1272. // after the bucket before begin.
  1273. __node->_M_nxt = _M_buckets[__bkt]->_M_nxt;
  1274. _M_buckets[__bkt]->_M_nxt = __node;
  1275. }
  1276. else
  1277. {
  1278. // The bucket is empty, the new node is inserted at the
  1279. // beginning of the singly-linked list and the bucket will
  1280. // contain _M_before_begin pointer.
  1281. __node->_M_nxt = _M_before_begin._M_nxt;
  1282. _M_before_begin._M_nxt = __node;
  1283. if (__node->_M_nxt)
  1284. // We must update former begin bucket that is pointing to
  1285. // _M_before_begin.
  1286. _M_buckets[_M_bucket_index(__node->_M_next())] = __node;
  1287. _M_buckets[__bkt] = &_M_before_begin;
  1288. }
  1289. }
  1290. template<typename _Key, typename _Value,
  1291. typename _Alloc, typename _ExtractKey, typename _Equal,
  1292. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1293. typename _Traits>
  1294. void
  1295. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1296. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1297. _M_remove_bucket_begin(size_type __bkt, __node_type* __next,
  1298. size_type __next_bkt)
  1299. {
  1300. if (!__next || __next_bkt != __bkt)
  1301. {
  1302. // Bucket is now empty
  1303. // First update next bucket if any
  1304. if (__next)
  1305. _M_buckets[__next_bkt] = _M_buckets[__bkt];
  1306. // Second update before begin node if necessary
  1307. if (&_M_before_begin == _M_buckets[__bkt])
  1308. _M_before_begin._M_nxt = __next;
  1309. _M_buckets[__bkt] = nullptr;
  1310. }
  1311. }
  1312. template<typename _Key, typename _Value,
  1313. typename _Alloc, typename _ExtractKey, typename _Equal,
  1314. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1315. typename _Traits>
  1316. auto
  1317. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1318. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1319. _M_get_previous_node(size_type __bkt, __node_base* __n)
  1320. -> __node_base*
  1321. {
  1322. __node_base* __prev_n = _M_buckets[__bkt];
  1323. while (__prev_n->_M_nxt != __n)
  1324. __prev_n = __prev_n->_M_nxt;
  1325. return __prev_n;
  1326. }
  1327. template<typename _Key, typename _Value,
  1328. typename _Alloc, typename _ExtractKey, typename _Equal,
  1329. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1330. typename _Traits>
  1331. template<typename... _Args>
  1332. auto
  1333. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1334. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1335. _M_emplace(std::true_type, _Args&&... __args)
  1336. -> pair<iterator, bool>
  1337. {
  1338. // First build the node to get access to the hash code
  1339. __node_type* __node = this->_M_allocate_node(std::forward<_Args>(__args)...);
  1340. const key_type& __k = this->_M_extract()(__node->_M_v());
  1341. __hash_code __code;
  1342. __try
  1343. {
  1344. __code = this->_M_hash_code(__k);
  1345. }
  1346. __catch(...)
  1347. {
  1348. this->_M_deallocate_node(__node);
  1349. __throw_exception_again;
  1350. }
  1351. size_type __bkt = _M_bucket_index(__k, __code);
  1352. if (__node_type* __p = _M_find_node(__bkt, __k, __code))
  1353. {
  1354. // There is already an equivalent node, no insertion
  1355. this->_M_deallocate_node(__node);
  1356. return std::make_pair(iterator(__p), false);
  1357. }
  1358. // Insert the node
  1359. return std::make_pair(_M_insert_unique_node(__bkt, __code, __node),
  1360. true);
  1361. }
  1362. template<typename _Key, typename _Value,
  1363. typename _Alloc, typename _ExtractKey, typename _Equal,
  1364. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1365. typename _Traits>
  1366. template<typename... _Args>
  1367. auto
  1368. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1369. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1370. _M_emplace(const_iterator __hint, std::false_type, _Args&&... __args)
  1371. -> iterator
  1372. {
  1373. // First build the node to get its hash code.
  1374. __node_type* __node =
  1375. this->_M_allocate_node(std::forward<_Args>(__args)...);
  1376. __hash_code __code;
  1377. __try
  1378. {
  1379. __code = this->_M_hash_code(this->_M_extract()(__node->_M_v()));
  1380. }
  1381. __catch(...)
  1382. {
  1383. this->_M_deallocate_node(__node);
  1384. __throw_exception_again;
  1385. }
  1386. return _M_insert_multi_node(__hint._M_cur, __code, __node);
  1387. }
  1388. template<typename _Key, typename _Value,
  1389. typename _Alloc, typename _ExtractKey, typename _Equal,
  1390. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1391. typename _Traits>
  1392. auto
  1393. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1394. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1395. _M_insert_unique_node(size_type __bkt, __hash_code __code,
  1396. __node_type* __node)
  1397. -> iterator
  1398. {
  1399. const __rehash_state& __saved_state = _M_rehash_policy._M_state();
  1400. std::pair<bool, std::size_t> __do_rehash
  1401. = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count, 1);
  1402. __try
  1403. {
  1404. if (__do_rehash.first)
  1405. {
  1406. _M_rehash(__do_rehash.second, __saved_state);
  1407. __bkt = _M_bucket_index(this->_M_extract()(__node->_M_v()), __code);
  1408. }
  1409. this->_M_store_code(__node, __code);
  1410. // Always insert at the beginning of the bucket.
  1411. _M_insert_bucket_begin(__bkt, __node);
  1412. ++_M_element_count;
  1413. return iterator(__node);
  1414. }
  1415. __catch(...)
  1416. {
  1417. this->_M_deallocate_node(__node);
  1418. __throw_exception_again;
  1419. }
  1420. }
  1421. // Insert node, in bucket bkt if no rehash (assumes no element with its key
  1422. // already present). Take ownership of the node, deallocate it on exception.
  1423. template<typename _Key, typename _Value,
  1424. typename _Alloc, typename _ExtractKey, typename _Equal,
  1425. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1426. typename _Traits>
  1427. auto
  1428. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1429. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1430. _M_insert_multi_node(__node_type* __hint, __hash_code __code,
  1431. __node_type* __node)
  1432. -> iterator
  1433. {
  1434. const __rehash_state& __saved_state = _M_rehash_policy._M_state();
  1435. std::pair<bool, std::size_t> __do_rehash
  1436. = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count, 1);
  1437. __try
  1438. {
  1439. if (__do_rehash.first)
  1440. _M_rehash(__do_rehash.second, __saved_state);
  1441. this->_M_store_code(__node, __code);
  1442. const key_type& __k = this->_M_extract()(__node->_M_v());
  1443. size_type __bkt = _M_bucket_index(__k, __code);
  1444. // Find the node before an equivalent one or use hint if it exists and
  1445. // if it is equivalent.
  1446. __node_base* __prev
  1447. = __builtin_expect(__hint != nullptr, false)
  1448. && this->_M_equals(__k, __code, __hint)
  1449. ? __hint
  1450. : _M_find_before_node(__bkt, __k, __code);
  1451. if (__prev)
  1452. {
  1453. // Insert after the node before the equivalent one.
  1454. __node->_M_nxt = __prev->_M_nxt;
  1455. __prev->_M_nxt = __node;
  1456. if (__builtin_expect(__prev == __hint, false))
  1457. // hint might be the last bucket node, in this case we need to
  1458. // update next bucket.
  1459. if (__node->_M_nxt
  1460. && !this->_M_equals(__k, __code, __node->_M_next()))
  1461. {
  1462. size_type __next_bkt = _M_bucket_index(__node->_M_next());
  1463. if (__next_bkt != __bkt)
  1464. _M_buckets[__next_bkt] = __node;
  1465. }
  1466. }
  1467. else
  1468. // The inserted node has no equivalent in the
  1469. // hashtable. We must insert the new node at the
  1470. // beginning of the bucket to preserve equivalent
  1471. // elements' relative positions.
  1472. _M_insert_bucket_begin(__bkt, __node);
  1473. ++_M_element_count;
  1474. return iterator(__node);
  1475. }
  1476. __catch(...)
  1477. {
  1478. this->_M_deallocate_node(__node);
  1479. __throw_exception_again;
  1480. }
  1481. }
  1482. // Insert v if no element with its key is already present.
  1483. template<typename _Key, typename _Value,
  1484. typename _Alloc, typename _ExtractKey, typename _Equal,
  1485. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1486. typename _Traits>
  1487. template<typename _Arg, typename _NodeGenerator>
  1488. auto
  1489. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1490. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1491. _M_insert(_Arg&& __v, const _NodeGenerator& __node_gen, std::true_type)
  1492. -> pair<iterator, bool>
  1493. {
  1494. const key_type& __k = this->_M_extract()(__v);
  1495. __hash_code __code = this->_M_hash_code(__k);
  1496. size_type __bkt = _M_bucket_index(__k, __code);
  1497. __node_type* __n = _M_find_node(__bkt, __k, __code);
  1498. if (__n)
  1499. return std::make_pair(iterator(__n), false);
  1500. __n = __node_gen(std::forward<_Arg>(__v));
  1501. return std::make_pair(_M_insert_unique_node(__bkt, __code, __n), true);
  1502. }
  1503. // Insert v unconditionally.
  1504. template<typename _Key, typename _Value,
  1505. typename _Alloc, typename _ExtractKey, typename _Equal,
  1506. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1507. typename _Traits>
  1508. template<typename _Arg, typename _NodeGenerator>
  1509. auto
  1510. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1511. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1512. _M_insert(const_iterator __hint, _Arg&& __v,
  1513. const _NodeGenerator& __node_gen, std::false_type)
  1514. -> iterator
  1515. {
  1516. // First compute the hash code so that we don't do anything if it
  1517. // throws.
  1518. __hash_code __code = this->_M_hash_code(this->_M_extract()(__v));
  1519. // Second allocate new node so that we don't rehash if it throws.
  1520. __node_type* __node = __node_gen(std::forward<_Arg>(__v));
  1521. return _M_insert_multi_node(__hint._M_cur, __code, __node);
  1522. }
  1523. template<typename _Key, typename _Value,
  1524. typename _Alloc, typename _ExtractKey, typename _Equal,
  1525. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1526. typename _Traits>
  1527. auto
  1528. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1529. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1530. erase(const_iterator __it)
  1531. -> iterator
  1532. {
  1533. __node_type* __n = __it._M_cur;
  1534. std::size_t __bkt = _M_bucket_index(__n);
  1535. // Look for previous node to unlink it from the erased one, this
  1536. // is why we need buckets to contain the before begin to make
  1537. // this search fast.
  1538. __node_base* __prev_n = _M_get_previous_node(__bkt, __n);
  1539. return _M_erase(__bkt, __prev_n, __n);
  1540. }
  1541. template<typename _Key, typename _Value,
  1542. typename _Alloc, typename _ExtractKey, typename _Equal,
  1543. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1544. typename _Traits>
  1545. auto
  1546. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1547. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1548. _M_erase(size_type __bkt, __node_base* __prev_n, __node_type* __n)
  1549. -> iterator
  1550. {
  1551. if (__prev_n == _M_buckets[__bkt])
  1552. _M_remove_bucket_begin(__bkt, __n->_M_next(),
  1553. __n->_M_nxt ? _M_bucket_index(__n->_M_next()) : 0);
  1554. else if (__n->_M_nxt)
  1555. {
  1556. size_type __next_bkt = _M_bucket_index(__n->_M_next());
  1557. if (__next_bkt != __bkt)
  1558. _M_buckets[__next_bkt] = __prev_n;
  1559. }
  1560. __prev_n->_M_nxt = __n->_M_nxt;
  1561. iterator __result(__n->_M_next());
  1562. this->_M_deallocate_node(__n);
  1563. --_M_element_count;
  1564. return __result;
  1565. }
  1566. template<typename _Key, typename _Value,
  1567. typename _Alloc, typename _ExtractKey, typename _Equal,
  1568. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1569. typename _Traits>
  1570. auto
  1571. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1572. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1573. _M_erase(std::true_type, const key_type& __k)
  1574. -> size_type
  1575. {
  1576. __hash_code __code = this->_M_hash_code(__k);
  1577. std::size_t __bkt = _M_bucket_index(__k, __code);
  1578. // Look for the node before the first matching node.
  1579. __node_base* __prev_n = _M_find_before_node(__bkt, __k, __code);
  1580. if (!__prev_n)
  1581. return 0;
  1582. // We found a matching node, erase it.
  1583. __node_type* __n = static_cast<__node_type*>(__prev_n->_M_nxt);
  1584. _M_erase(__bkt, __prev_n, __n);
  1585. return 1;
  1586. }
  1587. template<typename _Key, typename _Value,
  1588. typename _Alloc, typename _ExtractKey, typename _Equal,
  1589. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1590. typename _Traits>
  1591. auto
  1592. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1593. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1594. _M_erase(std::false_type, const key_type& __k)
  1595. -> size_type
  1596. {
  1597. __hash_code __code = this->_M_hash_code(__k);
  1598. std::size_t __bkt = _M_bucket_index(__k, __code);
  1599. // Look for the node before the first matching node.
  1600. __node_base* __prev_n = _M_find_before_node(__bkt, __k, __code);
  1601. if (!__prev_n)
  1602. return 0;
  1603. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1604. // 526. Is it undefined if a function in the standard changes
  1605. // in parameters?
  1606. // We use one loop to find all matching nodes and another to deallocate
  1607. // them so that the key stays valid during the first loop. It might be
  1608. // invalidated indirectly when destroying nodes.
  1609. __node_type* __n = static_cast<__node_type*>(__prev_n->_M_nxt);
  1610. __node_type* __n_last = __n;
  1611. std::size_t __n_last_bkt = __bkt;
  1612. do
  1613. {
  1614. __n_last = __n_last->_M_next();
  1615. if (!__n_last)
  1616. break;
  1617. __n_last_bkt = _M_bucket_index(__n_last);
  1618. }
  1619. while (__n_last_bkt == __bkt && this->_M_equals(__k, __code, __n_last));
  1620. // Deallocate nodes.
  1621. size_type __result = 0;
  1622. do
  1623. {
  1624. __node_type* __p = __n->_M_next();
  1625. this->_M_deallocate_node(__n);
  1626. __n = __p;
  1627. ++__result;
  1628. --_M_element_count;
  1629. }
  1630. while (__n != __n_last);
  1631. if (__prev_n == _M_buckets[__bkt])
  1632. _M_remove_bucket_begin(__bkt, __n_last, __n_last_bkt);
  1633. else if (__n_last && __n_last_bkt != __bkt)
  1634. _M_buckets[__n_last_bkt] = __prev_n;
  1635. __prev_n->_M_nxt = __n_last;
  1636. return __result;
  1637. }
  1638. template<typename _Key, typename _Value,
  1639. typename _Alloc, typename _ExtractKey, typename _Equal,
  1640. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1641. typename _Traits>
  1642. auto
  1643. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1644. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1645. erase(const_iterator __first, const_iterator __last)
  1646. -> iterator
  1647. {
  1648. __node_type* __n = __first._M_cur;
  1649. __node_type* __last_n = __last._M_cur;
  1650. if (__n == __last_n)
  1651. return iterator(__n);
  1652. std::size_t __bkt = _M_bucket_index(__n);
  1653. __node_base* __prev_n = _M_get_previous_node(__bkt, __n);
  1654. bool __is_bucket_begin = __n == _M_bucket_begin(__bkt);
  1655. std::size_t __n_bkt = __bkt;
  1656. for (;;)
  1657. {
  1658. do
  1659. {
  1660. __node_type* __tmp = __n;
  1661. __n = __n->_M_next();
  1662. this->_M_deallocate_node(__tmp);
  1663. --_M_element_count;
  1664. if (!__n)
  1665. break;
  1666. __n_bkt = _M_bucket_index(__n);
  1667. }
  1668. while (__n != __last_n && __n_bkt == __bkt);
  1669. if (__is_bucket_begin)
  1670. _M_remove_bucket_begin(__bkt, __n, __n_bkt);
  1671. if (__n == __last_n)
  1672. break;
  1673. __is_bucket_begin = true;
  1674. __bkt = __n_bkt;
  1675. }
  1676. if (__n && (__n_bkt != __bkt || __is_bucket_begin))
  1677. _M_buckets[__n_bkt] = __prev_n;
  1678. __prev_n->_M_nxt = __n;
  1679. return iterator(__n);
  1680. }
  1681. template<typename _Key, typename _Value,
  1682. typename _Alloc, typename _ExtractKey, typename _Equal,
  1683. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1684. typename _Traits>
  1685. void
  1686. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1687. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1688. clear() noexcept
  1689. {
  1690. this->_M_deallocate_nodes(_M_begin());
  1691. __builtin_memset(_M_buckets, 0, _M_bucket_count * sizeof(__bucket_type));
  1692. _M_element_count = 0;
  1693. _M_before_begin._M_nxt = nullptr;
  1694. }
  1695. template<typename _Key, typename _Value,
  1696. typename _Alloc, typename _ExtractKey, typename _Equal,
  1697. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1698. typename _Traits>
  1699. void
  1700. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1701. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1702. rehash(size_type __n)
  1703. {
  1704. const __rehash_state& __saved_state = _M_rehash_policy._M_state();
  1705. std::size_t __buckets
  1706. = std::max(_M_rehash_policy._M_bkt_for_elements(_M_element_count + 1),
  1707. __n);
  1708. __buckets = _M_rehash_policy._M_next_bkt(__buckets);
  1709. if (__buckets != _M_bucket_count)
  1710. _M_rehash(__buckets, __saved_state);
  1711. else
  1712. // No rehash, restore previous state to keep a consistent state.
  1713. _M_rehash_policy._M_reset(__saved_state);
  1714. }
  1715. template<typename _Key, typename _Value,
  1716. typename _Alloc, typename _ExtractKey, typename _Equal,
  1717. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1718. typename _Traits>
  1719. void
  1720. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1721. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1722. _M_rehash(size_type __n, const __rehash_state& __state)
  1723. {
  1724. __try
  1725. {
  1726. _M_rehash_aux(__n, __unique_keys());
  1727. }
  1728. __catch(...)
  1729. {
  1730. // A failure here means that buckets allocation failed. We only
  1731. // have to restore hash policy previous state.
  1732. _M_rehash_policy._M_reset(__state);
  1733. __throw_exception_again;
  1734. }
  1735. }
  1736. // Rehash when there is no equivalent elements.
  1737. template<typename _Key, typename _Value,
  1738. typename _Alloc, typename _ExtractKey, typename _Equal,
  1739. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1740. typename _Traits>
  1741. void
  1742. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1743. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1744. _M_rehash_aux(size_type __n, std::true_type)
  1745. {
  1746. __bucket_type* __new_buckets = _M_allocate_buckets(__n);
  1747. __node_type* __p = _M_begin();
  1748. _M_before_begin._M_nxt = nullptr;
  1749. std::size_t __bbegin_bkt = 0;
  1750. while (__p)
  1751. {
  1752. __node_type* __next = __p->_M_next();
  1753. std::size_t __bkt = __hash_code_base::_M_bucket_index(__p, __n);
  1754. if (!__new_buckets[__bkt])
  1755. {
  1756. __p->_M_nxt = _M_before_begin._M_nxt;
  1757. _M_before_begin._M_nxt = __p;
  1758. __new_buckets[__bkt] = &_M_before_begin;
  1759. if (__p->_M_nxt)
  1760. __new_buckets[__bbegin_bkt] = __p;
  1761. __bbegin_bkt = __bkt;
  1762. }
  1763. else
  1764. {
  1765. __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
  1766. __new_buckets[__bkt]->_M_nxt = __p;
  1767. }
  1768. __p = __next;
  1769. }
  1770. _M_deallocate_buckets();
  1771. _M_bucket_count = __n;
  1772. _M_buckets = __new_buckets;
  1773. }
  1774. // Rehash when there can be equivalent elements, preserve their relative
  1775. // order.
  1776. template<typename _Key, typename _Value,
  1777. typename _Alloc, typename _ExtractKey, typename _Equal,
  1778. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1779. typename _Traits>
  1780. void
  1781. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1782. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1783. _M_rehash_aux(size_type __n, std::false_type)
  1784. {
  1785. __bucket_type* __new_buckets = _M_allocate_buckets(__n);
  1786. __node_type* __p = _M_begin();
  1787. _M_before_begin._M_nxt = nullptr;
  1788. std::size_t __bbegin_bkt = 0;
  1789. std::size_t __prev_bkt = 0;
  1790. __node_type* __prev_p = nullptr;
  1791. bool __check_bucket = false;
  1792. while (__p)
  1793. {
  1794. __node_type* __next = __p->_M_next();
  1795. std::size_t __bkt = __hash_code_base::_M_bucket_index(__p, __n);
  1796. if (__prev_p && __prev_bkt == __bkt)
  1797. {
  1798. // Previous insert was already in this bucket, we insert after
  1799. // the previously inserted one to preserve equivalent elements
  1800. // relative order.
  1801. __p->_M_nxt = __prev_p->_M_nxt;
  1802. __prev_p->_M_nxt = __p;
  1803. // Inserting after a node in a bucket require to check that we
  1804. // haven't change the bucket last node, in this case next
  1805. // bucket containing its before begin node must be updated. We
  1806. // schedule a check as soon as we move out of the sequence of
  1807. // equivalent nodes to limit the number of checks.
  1808. __check_bucket = true;
  1809. }
  1810. else
  1811. {
  1812. if (__check_bucket)
  1813. {
  1814. // Check if we shall update the next bucket because of
  1815. // insertions into __prev_bkt bucket.
  1816. if (__prev_p->_M_nxt)
  1817. {
  1818. std::size_t __next_bkt
  1819. = __hash_code_base::_M_bucket_index(__prev_p->_M_next(),
  1820. __n);
  1821. if (__next_bkt != __prev_bkt)
  1822. __new_buckets[__next_bkt] = __prev_p;
  1823. }
  1824. __check_bucket = false;
  1825. }
  1826. if (!__new_buckets[__bkt])
  1827. {
  1828. __p->_M_nxt = _M_before_begin._M_nxt;
  1829. _M_before_begin._M_nxt = __p;
  1830. __new_buckets[__bkt] = &_M_before_begin;
  1831. if (__p->_M_nxt)
  1832. __new_buckets[__bbegin_bkt] = __p;
  1833. __bbegin_bkt = __bkt;
  1834. }
  1835. else
  1836. {
  1837. __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
  1838. __new_buckets[__bkt]->_M_nxt = __p;
  1839. }
  1840. }
  1841. __prev_p = __p;
  1842. __prev_bkt = __bkt;
  1843. __p = __next;
  1844. }
  1845. if (__check_bucket && __prev_p->_M_nxt)
  1846. {
  1847. std::size_t __next_bkt
  1848. = __hash_code_base::_M_bucket_index(__prev_p->_M_next(), __n);
  1849. if (__next_bkt != __prev_bkt)
  1850. __new_buckets[__next_bkt] = __prev_p;
  1851. }
  1852. _M_deallocate_buckets();
  1853. _M_bucket_count = __n;
  1854. _M_buckets = __new_buckets;
  1855. }
  1856. _GLIBCXX_END_NAMESPACE_VERSION
  1857. } // namespace std
  1858. #endif // _HASHTABLE_H