hashtable.h 72 KB

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