hashtable.h 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222
  1. // hashtable.h header -*- C++ -*-
  2. // Copyright (C) 2007-2019 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/hashtable.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. using __traits_type = _Traits;
  185. using __hash_cached = typename __traits_type::__hash_cached;
  186. using __node_type = __detail::_Hash_node<_Value, __hash_cached::value>;
  187. using __node_alloc_type = __alloc_rebind<_Alloc, __node_type>;
  188. using __hashtable_alloc = __detail::_Hashtable_alloc<__node_alloc_type>;
  189. using __value_alloc_traits =
  190. typename __hashtable_alloc::__value_alloc_traits;
  191. using __node_alloc_traits =
  192. typename __hashtable_alloc::__node_alloc_traits;
  193. using __node_base = typename __hashtable_alloc::__node_base;
  194. using __bucket_type = typename __hashtable_alloc::__bucket_type;
  195. public:
  196. typedef _Key key_type;
  197. typedef _Value value_type;
  198. typedef _Alloc allocator_type;
  199. typedef _Equal key_equal;
  200. // mapped_type, if present, comes from _Map_base.
  201. // hasher, if present, comes from _Hash_code_base/_Hashtable_base.
  202. typedef typename __value_alloc_traits::pointer pointer;
  203. typedef typename __value_alloc_traits::const_pointer const_pointer;
  204. typedef value_type& reference;
  205. typedef const value_type& const_reference;
  206. private:
  207. using __rehash_type = _RehashPolicy;
  208. using __rehash_state = typename __rehash_type::_State;
  209. using __constant_iterators = typename __traits_type::__constant_iterators;
  210. using __unique_keys = typename __traits_type::__unique_keys;
  211. using __key_extract = typename std::conditional<
  212. __constant_iterators::value,
  213. __detail::_Identity,
  214. __detail::_Select1st>::type;
  215. using __hashtable_base = __detail::
  216. _Hashtable_base<_Key, _Value, _ExtractKey,
  217. _Equal, _H1, _H2, _Hash, _Traits>;
  218. using __hash_code_base = typename __hashtable_base::__hash_code_base;
  219. using __hash_code = typename __hashtable_base::__hash_code;
  220. using __ireturn_type = typename __hashtable_base::__ireturn_type;
  221. using __map_base = __detail::_Map_base<_Key, _Value, _Alloc, _ExtractKey,
  222. _Equal, _H1, _H2, _Hash,
  223. _RehashPolicy, _Traits>;
  224. using __rehash_base = __detail::_Rehash_base<_Key, _Value, _Alloc,
  225. _ExtractKey, _Equal,
  226. _H1, _H2, _Hash,
  227. _RehashPolicy, _Traits>;
  228. using __eq_base = __detail::_Equality<_Key, _Value, _Alloc, _ExtractKey,
  229. _Equal, _H1, _H2, _Hash,
  230. _RehashPolicy, _Traits>;
  231. using __reuse_or_alloc_node_type =
  232. __detail::_ReuseOrAllocNode<__node_alloc_type>;
  233. // Metaprogramming for picking apart hash caching.
  234. template<typename _Cond>
  235. using __if_hash_cached = __or_<__not_<__hash_cached>, _Cond>;
  236. template<typename _Cond>
  237. using __if_hash_not_cached = __or_<__hash_cached, _Cond>;
  238. // Compile-time diagnostics.
  239. // _Hash_code_base has everything protected, so use this derived type to
  240. // access it.
  241. struct __hash_code_base_access : __hash_code_base
  242. { using __hash_code_base::_M_bucket_index; };
  243. // Getting a bucket index from a node shall not throw because it is used
  244. // in methods (erase, swap...) that shall not throw.
  245. static_assert(noexcept(declval<const __hash_code_base_access&>()
  246. ._M_bucket_index((const __node_type*)nullptr,
  247. (std::size_t)0)),
  248. "Cache the hash code or qualify your functors involved"
  249. " in hash code and bucket index computation with noexcept");
  250. // Following two static assertions are necessary to guarantee
  251. // that local_iterator will be default constructible.
  252. // When hash codes are cached local iterator inherits from H2 functor
  253. // which must then be default constructible.
  254. static_assert(__if_hash_cached<is_default_constructible<_H2>>::value,
  255. "Functor used to map hash code to bucket index"
  256. " must be default constructible");
  257. template<typename _Keya, typename _Valuea, typename _Alloca,
  258. typename _ExtractKeya, typename _Equala,
  259. typename _H1a, typename _H2a, typename _Hasha,
  260. typename _RehashPolicya, typename _Traitsa,
  261. bool _Unique_keysa>
  262. friend struct __detail::_Map_base;
  263. template<typename _Keya, typename _Valuea, typename _Alloca,
  264. typename _ExtractKeya, typename _Equala,
  265. typename _H1a, typename _H2a, typename _Hasha,
  266. typename _RehashPolicya, typename _Traitsa>
  267. friend struct __detail::_Insert_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. bool _Constant_iteratorsa>
  273. friend struct __detail::_Insert;
  274. public:
  275. using size_type = typename __hashtable_base::size_type;
  276. using difference_type = typename __hashtable_base::difference_type;
  277. using iterator = typename __hashtable_base::iterator;
  278. using const_iterator = typename __hashtable_base::const_iterator;
  279. using local_iterator = typename __hashtable_base::local_iterator;
  280. using const_local_iterator = typename __hashtable_base::
  281. const_local_iterator;
  282. #if __cplusplus > 201402L
  283. using node_type = _Node_handle<_Key, _Value, __node_alloc_type>;
  284. using insert_return_type = _Node_insert_return<iterator, node_type>;
  285. #endif
  286. private:
  287. __bucket_type* _M_buckets = &_M_single_bucket;
  288. size_type _M_bucket_count = 1;
  289. __node_base _M_before_begin;
  290. size_type _M_element_count = 0;
  291. _RehashPolicy _M_rehash_policy;
  292. // A single bucket used when only need for 1 bucket. Especially
  293. // interesting in move semantic to leave hashtable with only 1 buckets
  294. // which is not allocated so that we can have those operations noexcept
  295. // qualified.
  296. // Note that we can't leave hashtable with 0 bucket without adding
  297. // numerous checks in the code to avoid 0 modulus.
  298. __bucket_type _M_single_bucket = nullptr;
  299. bool
  300. _M_uses_single_bucket(__bucket_type* __bkts) const
  301. { return __builtin_expect(__bkts == &_M_single_bucket, false); }
  302. bool
  303. _M_uses_single_bucket() const
  304. { return _M_uses_single_bucket(_M_buckets); }
  305. __hashtable_alloc&
  306. _M_base_alloc() { return *this; }
  307. __bucket_type*
  308. _M_allocate_buckets(size_type __n)
  309. {
  310. if (__builtin_expect(__n == 1, false))
  311. {
  312. _M_single_bucket = nullptr;
  313. return &_M_single_bucket;
  314. }
  315. return __hashtable_alloc::_M_allocate_buckets(__n);
  316. }
  317. void
  318. _M_deallocate_buckets(__bucket_type* __bkts, size_type __n)
  319. {
  320. if (_M_uses_single_bucket(__bkts))
  321. return;
  322. __hashtable_alloc::_M_deallocate_buckets(__bkts, __n);
  323. }
  324. void
  325. _M_deallocate_buckets()
  326. { _M_deallocate_buckets(_M_buckets, _M_bucket_count); }
  327. // Gets bucket begin, deals with the fact that non-empty buckets contain
  328. // their before begin node.
  329. __node_type*
  330. _M_bucket_begin(size_type __bkt) const;
  331. __node_type*
  332. _M_begin() const
  333. { return static_cast<__node_type*>(_M_before_begin._M_nxt); }
  334. // Assign *this using another _Hashtable instance. Either elements
  335. // are copy or move depends on the _NodeGenerator.
  336. template<typename _Ht, typename _NodeGenerator>
  337. void
  338. _M_assign_elements(_Ht&&, const _NodeGenerator&);
  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. _GLIBCXX_NODISCARD 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. _M_assign_elements(__ht,
  889. [](const __reuse_or_alloc_node_type& __roan, const __node_type* __n)
  890. { return __roan(__n->_M_v()); });
  891. return *this;
  892. }
  893. template<typename _Key, typename _Value,
  894. typename _Alloc, typename _ExtractKey, typename _Equal,
  895. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  896. typename _Traits>
  897. template<typename _Ht, typename _NodeGenerator>
  898. void
  899. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  900. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  901. _M_assign_elements(_Ht&& __ht, const _NodeGenerator& __node_gen)
  902. {
  903. __bucket_type* __former_buckets = nullptr;
  904. std::size_t __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::forward<_Ht>(__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. [&__node_gen, &__roan](__node_type* __n)
  924. { return __node_gen(__roan, __n); });
  925. if (__former_buckets)
  926. _M_deallocate_buckets(__former_buckets, __former_bucket_count);
  927. }
  928. __catch(...)
  929. {
  930. if (__former_buckets)
  931. {
  932. // Restore previous buckets.
  933. _M_deallocate_buckets();
  934. _M_rehash_policy._M_reset(__former_state);
  935. _M_buckets = __former_buckets;
  936. _M_bucket_count = __former_bucket_count;
  937. }
  938. __builtin_memset(_M_buckets, 0,
  939. _M_bucket_count * sizeof(__bucket_type));
  940. __throw_exception_again;
  941. }
  942. }
  943. template<typename _Key, typename _Value,
  944. typename _Alloc, typename _ExtractKey, typename _Equal,
  945. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  946. typename _Traits>
  947. template<typename _NodeGenerator>
  948. void
  949. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  950. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  951. _M_assign(const _Hashtable& __ht, const _NodeGenerator& __node_gen)
  952. {
  953. __bucket_type* __buckets = nullptr;
  954. if (!_M_buckets)
  955. _M_buckets = __buckets = _M_allocate_buckets(_M_bucket_count);
  956. __try
  957. {
  958. if (!__ht._M_before_begin._M_nxt)
  959. return;
  960. // First deal with the special first node pointed to by
  961. // _M_before_begin.
  962. __node_type* __ht_n = __ht._M_begin();
  963. __node_type* __this_n = __node_gen(__ht_n);
  964. this->_M_copy_code(__this_n, __ht_n);
  965. _M_before_begin._M_nxt = __this_n;
  966. _M_buckets[_M_bucket_index(__this_n)] = &_M_before_begin;
  967. // Then deal with other nodes.
  968. __node_base* __prev_n = __this_n;
  969. for (__ht_n = __ht_n->_M_next(); __ht_n; __ht_n = __ht_n->_M_next())
  970. {
  971. __this_n = __node_gen(__ht_n);
  972. __prev_n->_M_nxt = __this_n;
  973. this->_M_copy_code(__this_n, __ht_n);
  974. size_type __bkt = _M_bucket_index(__this_n);
  975. if (!_M_buckets[__bkt])
  976. _M_buckets[__bkt] = __prev_n;
  977. __prev_n = __this_n;
  978. }
  979. }
  980. __catch(...)
  981. {
  982. clear();
  983. if (__buckets)
  984. _M_deallocate_buckets();
  985. __throw_exception_again;
  986. }
  987. }
  988. template<typename _Key, typename _Value,
  989. typename _Alloc, typename _ExtractKey, typename _Equal,
  990. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  991. typename _Traits>
  992. void
  993. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  994. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  995. _M_reset() noexcept
  996. {
  997. _M_rehash_policy._M_reset();
  998. _M_bucket_count = 1;
  999. _M_single_bucket = nullptr;
  1000. _M_buckets = &_M_single_bucket;
  1001. _M_before_begin._M_nxt = nullptr;
  1002. _M_element_count = 0;
  1003. }
  1004. template<typename _Key, typename _Value,
  1005. typename _Alloc, typename _ExtractKey, typename _Equal,
  1006. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1007. typename _Traits>
  1008. void
  1009. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1010. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1011. _M_move_assign(_Hashtable&& __ht, std::true_type)
  1012. {
  1013. this->_M_deallocate_nodes(_M_begin());
  1014. _M_deallocate_buckets();
  1015. __hashtable_base::operator=(std::move(__ht));
  1016. _M_rehash_policy = __ht._M_rehash_policy;
  1017. if (!__ht._M_uses_single_bucket())
  1018. _M_buckets = __ht._M_buckets;
  1019. else
  1020. {
  1021. _M_buckets = &_M_single_bucket;
  1022. _M_single_bucket = __ht._M_single_bucket;
  1023. }
  1024. _M_bucket_count = __ht._M_bucket_count;
  1025. _M_before_begin._M_nxt = __ht._M_before_begin._M_nxt;
  1026. _M_element_count = __ht._M_element_count;
  1027. std::__alloc_on_move(this->_M_node_allocator(), __ht._M_node_allocator());
  1028. // Fix buckets containing the _M_before_begin pointers that can't be
  1029. // moved.
  1030. if (_M_begin())
  1031. _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
  1032. __ht._M_reset();
  1033. }
  1034. template<typename _Key, typename _Value,
  1035. typename _Alloc, typename _ExtractKey, typename _Equal,
  1036. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1037. typename _Traits>
  1038. void
  1039. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1040. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1041. _M_move_assign(_Hashtable&& __ht, std::false_type)
  1042. {
  1043. if (__ht._M_node_allocator() == this->_M_node_allocator())
  1044. _M_move_assign(std::move(__ht), std::true_type());
  1045. else
  1046. {
  1047. // Can't move memory, move elements then.
  1048. _M_assign_elements(std::move(__ht),
  1049. [](const __reuse_or_alloc_node_type& __roan, __node_type* __n)
  1050. { return __roan(std::move_if_noexcept(__n->_M_v())); });
  1051. __ht.clear();
  1052. }
  1053. }
  1054. template<typename _Key, typename _Value,
  1055. typename _Alloc, typename _ExtractKey, typename _Equal,
  1056. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1057. typename _Traits>
  1058. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1059. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1060. _Hashtable(const _Hashtable& __ht)
  1061. : __hashtable_base(__ht),
  1062. __map_base(__ht),
  1063. __rehash_base(__ht),
  1064. __hashtable_alloc(
  1065. __node_alloc_traits::_S_select_on_copy(__ht._M_node_allocator())),
  1066. _M_buckets(nullptr),
  1067. _M_bucket_count(__ht._M_bucket_count),
  1068. _M_element_count(__ht._M_element_count),
  1069. _M_rehash_policy(__ht._M_rehash_policy)
  1070. {
  1071. _M_assign(__ht,
  1072. [this](const __node_type* __n)
  1073. { return this->_M_allocate_node(__n->_M_v()); });
  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(_Hashtable&& __ht) noexcept
  1082. : __hashtable_base(__ht),
  1083. __map_base(__ht),
  1084. __rehash_base(__ht),
  1085. __hashtable_alloc(std::move(__ht._M_base_alloc())),
  1086. _M_buckets(__ht._M_buckets),
  1087. _M_bucket_count(__ht._M_bucket_count),
  1088. _M_before_begin(__ht._M_before_begin._M_nxt),
  1089. _M_element_count(__ht._M_element_count),
  1090. _M_rehash_policy(__ht._M_rehash_policy)
  1091. {
  1092. // Update, if necessary, buckets if __ht is using its single bucket.
  1093. if (__ht._M_uses_single_bucket())
  1094. {
  1095. _M_buckets = &_M_single_bucket;
  1096. _M_single_bucket = __ht._M_single_bucket;
  1097. }
  1098. // Update, if necessary, bucket pointing to before begin that hasn't
  1099. // moved.
  1100. if (_M_begin())
  1101. _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
  1102. __ht._M_reset();
  1103. }
  1104. template<typename _Key, typename _Value,
  1105. typename _Alloc, typename _ExtractKey, typename _Equal,
  1106. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1107. typename _Traits>
  1108. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1109. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1110. _Hashtable(const _Hashtable& __ht, const allocator_type& __a)
  1111. : __hashtable_base(__ht),
  1112. __map_base(__ht),
  1113. __rehash_base(__ht),
  1114. __hashtable_alloc(__node_alloc_type(__a)),
  1115. _M_buckets(),
  1116. _M_bucket_count(__ht._M_bucket_count),
  1117. _M_element_count(__ht._M_element_count),
  1118. _M_rehash_policy(__ht._M_rehash_policy)
  1119. {
  1120. _M_assign(__ht,
  1121. [this](const __node_type* __n)
  1122. { return this->_M_allocate_node(__n->_M_v()); });
  1123. }
  1124. template<typename _Key, typename _Value,
  1125. typename _Alloc, typename _ExtractKey, typename _Equal,
  1126. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1127. typename _Traits>
  1128. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1129. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1130. _Hashtable(_Hashtable&& __ht, const allocator_type& __a)
  1131. : __hashtable_base(__ht),
  1132. __map_base(__ht),
  1133. __rehash_base(__ht),
  1134. __hashtable_alloc(__node_alloc_type(__a)),
  1135. _M_buckets(nullptr),
  1136. _M_bucket_count(__ht._M_bucket_count),
  1137. _M_element_count(__ht._M_element_count),
  1138. _M_rehash_policy(__ht._M_rehash_policy)
  1139. {
  1140. if (__ht._M_node_allocator() == this->_M_node_allocator())
  1141. {
  1142. if (__ht._M_uses_single_bucket())
  1143. {
  1144. _M_buckets = &_M_single_bucket;
  1145. _M_single_bucket = __ht._M_single_bucket;
  1146. }
  1147. else
  1148. _M_buckets = __ht._M_buckets;
  1149. _M_before_begin._M_nxt = __ht._M_before_begin._M_nxt;
  1150. // Update, if necessary, bucket pointing to before begin that hasn't
  1151. // moved.
  1152. if (_M_begin())
  1153. _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
  1154. __ht._M_reset();
  1155. }
  1156. else
  1157. {
  1158. _M_assign(__ht,
  1159. [this](__node_type* __n)
  1160. {
  1161. return this->_M_allocate_node(
  1162. std::move_if_noexcept(__n->_M_v()));
  1163. });
  1164. __ht.clear();
  1165. }
  1166. }
  1167. template<typename _Key, typename _Value,
  1168. typename _Alloc, typename _ExtractKey, typename _Equal,
  1169. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1170. typename _Traits>
  1171. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1172. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1173. ~_Hashtable() noexcept
  1174. {
  1175. clear();
  1176. _M_deallocate_buckets();
  1177. }
  1178. template<typename _Key, typename _Value,
  1179. typename _Alloc, typename _ExtractKey, typename _Equal,
  1180. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1181. typename _Traits>
  1182. void
  1183. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1184. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1185. swap(_Hashtable& __x)
  1186. noexcept(__and_<__is_nothrow_swappable<_H1>,
  1187. __is_nothrow_swappable<_Equal>>::value)
  1188. {
  1189. // The only base class with member variables is hash_code_base.
  1190. // We define _Hash_code_base::_M_swap because different
  1191. // specializations have different members.
  1192. this->_M_swap(__x);
  1193. std::__alloc_on_swap(this->_M_node_allocator(), __x._M_node_allocator());
  1194. std::swap(_M_rehash_policy, __x._M_rehash_policy);
  1195. // Deal properly with potentially moved instances.
  1196. if (this->_M_uses_single_bucket())
  1197. {
  1198. if (!__x._M_uses_single_bucket())
  1199. {
  1200. _M_buckets = __x._M_buckets;
  1201. __x._M_buckets = &__x._M_single_bucket;
  1202. }
  1203. }
  1204. else if (__x._M_uses_single_bucket())
  1205. {
  1206. __x._M_buckets = _M_buckets;
  1207. _M_buckets = &_M_single_bucket;
  1208. }
  1209. else
  1210. std::swap(_M_buckets, __x._M_buckets);
  1211. std::swap(_M_bucket_count, __x._M_bucket_count);
  1212. std::swap(_M_before_begin._M_nxt, __x._M_before_begin._M_nxt);
  1213. std::swap(_M_element_count, __x._M_element_count);
  1214. std::swap(_M_single_bucket, __x._M_single_bucket);
  1215. // Fix buckets containing the _M_before_begin pointers that can't be
  1216. // swapped.
  1217. if (_M_begin())
  1218. _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
  1219. if (__x._M_begin())
  1220. __x._M_buckets[__x._M_bucket_index(__x._M_begin())]
  1221. = &__x._M_before_begin;
  1222. }
  1223. template<typename _Key, typename _Value,
  1224. typename _Alloc, typename _ExtractKey, typename _Equal,
  1225. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1226. typename _Traits>
  1227. auto
  1228. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1229. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1230. find(const key_type& __k)
  1231. -> iterator
  1232. {
  1233. __hash_code __code = this->_M_hash_code(__k);
  1234. std::size_t __n = _M_bucket_index(__k, __code);
  1235. __node_type* __p = _M_find_node(__n, __k, __code);
  1236. return __p ? iterator(__p) : end();
  1237. }
  1238. template<typename _Key, typename _Value,
  1239. typename _Alloc, typename _ExtractKey, typename _Equal,
  1240. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1241. typename _Traits>
  1242. auto
  1243. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1244. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1245. find(const key_type& __k) const
  1246. -> const_iterator
  1247. {
  1248. __hash_code __code = this->_M_hash_code(__k);
  1249. std::size_t __n = _M_bucket_index(__k, __code);
  1250. __node_type* __p = _M_find_node(__n, __k, __code);
  1251. return __p ? const_iterator(__p) : end();
  1252. }
  1253. template<typename _Key, typename _Value,
  1254. typename _Alloc, typename _ExtractKey, typename _Equal,
  1255. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1256. typename _Traits>
  1257. auto
  1258. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1259. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1260. count(const key_type& __k) const
  1261. -> size_type
  1262. {
  1263. __hash_code __code = this->_M_hash_code(__k);
  1264. std::size_t __n = _M_bucket_index(__k, __code);
  1265. __node_type* __p = _M_bucket_begin(__n);
  1266. if (!__p)
  1267. return 0;
  1268. std::size_t __result = 0;
  1269. for (;; __p = __p->_M_next())
  1270. {
  1271. if (this->_M_equals(__k, __code, __p))
  1272. ++__result;
  1273. else if (__result)
  1274. // All equivalent values are next to each other, if we
  1275. // found a non-equivalent value after an equivalent one it
  1276. // means that we won't find any new equivalent value.
  1277. break;
  1278. if (!__p->_M_nxt || _M_bucket_index(__p->_M_next()) != __n)
  1279. break;
  1280. }
  1281. return __result;
  1282. }
  1283. template<typename _Key, typename _Value,
  1284. typename _Alloc, typename _ExtractKey, typename _Equal,
  1285. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1286. typename _Traits>
  1287. auto
  1288. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1289. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1290. equal_range(const key_type& __k)
  1291. -> pair<iterator, iterator>
  1292. {
  1293. __hash_code __code = this->_M_hash_code(__k);
  1294. std::size_t __n = _M_bucket_index(__k, __code);
  1295. __node_type* __p = _M_find_node(__n, __k, __code);
  1296. if (__p)
  1297. {
  1298. __node_type* __p1 = __p->_M_next();
  1299. while (__p1 && _M_bucket_index(__p1) == __n
  1300. && this->_M_equals(__k, __code, __p1))
  1301. __p1 = __p1->_M_next();
  1302. return std::make_pair(iterator(__p), iterator(__p1));
  1303. }
  1304. else
  1305. return std::make_pair(end(), end());
  1306. }
  1307. template<typename _Key, typename _Value,
  1308. typename _Alloc, typename _ExtractKey, typename _Equal,
  1309. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1310. typename _Traits>
  1311. auto
  1312. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1313. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1314. equal_range(const key_type& __k) const
  1315. -> pair<const_iterator, const_iterator>
  1316. {
  1317. __hash_code __code = this->_M_hash_code(__k);
  1318. std::size_t __n = _M_bucket_index(__k, __code);
  1319. __node_type* __p = _M_find_node(__n, __k, __code);
  1320. if (__p)
  1321. {
  1322. __node_type* __p1 = __p->_M_next();
  1323. while (__p1 && _M_bucket_index(__p1) == __n
  1324. && this->_M_equals(__k, __code, __p1))
  1325. __p1 = __p1->_M_next();
  1326. return std::make_pair(const_iterator(__p), const_iterator(__p1));
  1327. }
  1328. else
  1329. return std::make_pair(end(), end());
  1330. }
  1331. // Find the node whose key compares equal to k in the bucket n.
  1332. // Return nullptr if no node is found.
  1333. template<typename _Key, typename _Value,
  1334. typename _Alloc, typename _ExtractKey, typename _Equal,
  1335. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1336. typename _Traits>
  1337. auto
  1338. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1339. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1340. _M_find_before_node(size_type __n, const key_type& __k,
  1341. __hash_code __code) const
  1342. -> __node_base*
  1343. {
  1344. __node_base* __prev_p = _M_buckets[__n];
  1345. if (!__prev_p)
  1346. return nullptr;
  1347. for (__node_type* __p = static_cast<__node_type*>(__prev_p->_M_nxt);;
  1348. __p = __p->_M_next())
  1349. {
  1350. if (this->_M_equals(__k, __code, __p))
  1351. return __prev_p;
  1352. if (!__p->_M_nxt || _M_bucket_index(__p->_M_next()) != __n)
  1353. break;
  1354. __prev_p = __p;
  1355. }
  1356. return nullptr;
  1357. }
  1358. template<typename _Key, typename _Value,
  1359. typename _Alloc, typename _ExtractKey, typename _Equal,
  1360. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1361. typename _Traits>
  1362. void
  1363. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1364. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1365. _M_insert_bucket_begin(size_type __bkt, __node_type* __node)
  1366. {
  1367. if (_M_buckets[__bkt])
  1368. {
  1369. // Bucket is not empty, we just need to insert the new node
  1370. // after the bucket before begin.
  1371. __node->_M_nxt = _M_buckets[__bkt]->_M_nxt;
  1372. _M_buckets[__bkt]->_M_nxt = __node;
  1373. }
  1374. else
  1375. {
  1376. // The bucket is empty, the new node is inserted at the
  1377. // beginning of the singly-linked list and the bucket will
  1378. // contain _M_before_begin pointer.
  1379. __node->_M_nxt = _M_before_begin._M_nxt;
  1380. _M_before_begin._M_nxt = __node;
  1381. if (__node->_M_nxt)
  1382. // We must update former begin bucket that is pointing to
  1383. // _M_before_begin.
  1384. _M_buckets[_M_bucket_index(__node->_M_next())] = __node;
  1385. _M_buckets[__bkt] = &_M_before_begin;
  1386. }
  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. void
  1393. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1394. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1395. _M_remove_bucket_begin(size_type __bkt, __node_type* __next,
  1396. size_type __next_bkt)
  1397. {
  1398. if (!__next || __next_bkt != __bkt)
  1399. {
  1400. // Bucket is now empty
  1401. // First update next bucket if any
  1402. if (__next)
  1403. _M_buckets[__next_bkt] = _M_buckets[__bkt];
  1404. // Second update before begin node if necessary
  1405. if (&_M_before_begin == _M_buckets[__bkt])
  1406. _M_before_begin._M_nxt = __next;
  1407. _M_buckets[__bkt] = nullptr;
  1408. }
  1409. }
  1410. template<typename _Key, typename _Value,
  1411. typename _Alloc, typename _ExtractKey, typename _Equal,
  1412. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1413. typename _Traits>
  1414. auto
  1415. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1416. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1417. _M_get_previous_node(size_type __bkt, __node_base* __n)
  1418. -> __node_base*
  1419. {
  1420. __node_base* __prev_n = _M_buckets[__bkt];
  1421. while (__prev_n->_M_nxt != __n)
  1422. __prev_n = __prev_n->_M_nxt;
  1423. return __prev_n;
  1424. }
  1425. template<typename _Key, typename _Value,
  1426. typename _Alloc, typename _ExtractKey, typename _Equal,
  1427. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1428. typename _Traits>
  1429. template<typename... _Args>
  1430. auto
  1431. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1432. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1433. _M_emplace(std::true_type, _Args&&... __args)
  1434. -> pair<iterator, bool>
  1435. {
  1436. // First build the node to get access to the hash code
  1437. __node_type* __node = this->_M_allocate_node(std::forward<_Args>(__args)...);
  1438. const key_type& __k = this->_M_extract()(__node->_M_v());
  1439. __hash_code __code;
  1440. __try
  1441. {
  1442. __code = this->_M_hash_code(__k);
  1443. }
  1444. __catch(...)
  1445. {
  1446. this->_M_deallocate_node(__node);
  1447. __throw_exception_again;
  1448. }
  1449. size_type __bkt = _M_bucket_index(__k, __code);
  1450. if (__node_type* __p = _M_find_node(__bkt, __k, __code))
  1451. {
  1452. // There is already an equivalent node, no insertion
  1453. this->_M_deallocate_node(__node);
  1454. return std::make_pair(iterator(__p), false);
  1455. }
  1456. // Insert the node
  1457. return std::make_pair(_M_insert_unique_node(__bkt, __code, __node),
  1458. true);
  1459. }
  1460. template<typename _Key, typename _Value,
  1461. typename _Alloc, typename _ExtractKey, typename _Equal,
  1462. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1463. typename _Traits>
  1464. template<typename... _Args>
  1465. auto
  1466. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1467. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1468. _M_emplace(const_iterator __hint, std::false_type, _Args&&... __args)
  1469. -> iterator
  1470. {
  1471. // First build the node to get its hash code.
  1472. __node_type* __node =
  1473. this->_M_allocate_node(std::forward<_Args>(__args)...);
  1474. __hash_code __code;
  1475. __try
  1476. {
  1477. __code = this->_M_hash_code(this->_M_extract()(__node->_M_v()));
  1478. }
  1479. __catch(...)
  1480. {
  1481. this->_M_deallocate_node(__node);
  1482. __throw_exception_again;
  1483. }
  1484. return _M_insert_multi_node(__hint._M_cur, __code, __node);
  1485. }
  1486. template<typename _Key, typename _Value,
  1487. typename _Alloc, typename _ExtractKey, typename _Equal,
  1488. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1489. typename _Traits>
  1490. auto
  1491. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1492. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1493. _M_insert_unique_node(size_type __bkt, __hash_code __code,
  1494. __node_type* __node, size_type __n_elt)
  1495. -> iterator
  1496. {
  1497. const __rehash_state& __saved_state = _M_rehash_policy._M_state();
  1498. std::pair<bool, std::size_t> __do_rehash
  1499. = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count,
  1500. __n_elt);
  1501. __try
  1502. {
  1503. if (__do_rehash.first)
  1504. {
  1505. _M_rehash(__do_rehash.second, __saved_state);
  1506. __bkt = _M_bucket_index(this->_M_extract()(__node->_M_v()), __code);
  1507. }
  1508. this->_M_store_code(__node, __code);
  1509. // Always insert at the beginning of the bucket.
  1510. _M_insert_bucket_begin(__bkt, __node);
  1511. ++_M_element_count;
  1512. return iterator(__node);
  1513. }
  1514. __catch(...)
  1515. {
  1516. this->_M_deallocate_node(__node);
  1517. __throw_exception_again;
  1518. }
  1519. }
  1520. // Insert node, in bucket bkt if no rehash (assumes no element with its key
  1521. // already present). Take ownership of the node, deallocate it on exception.
  1522. template<typename _Key, typename _Value,
  1523. typename _Alloc, typename _ExtractKey, typename _Equal,
  1524. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1525. typename _Traits>
  1526. auto
  1527. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1528. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1529. _M_insert_multi_node(__node_type* __hint, __hash_code __code,
  1530. __node_type* __node)
  1531. -> iterator
  1532. {
  1533. const __rehash_state& __saved_state = _M_rehash_policy._M_state();
  1534. std::pair<bool, std::size_t> __do_rehash
  1535. = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count, 1);
  1536. __try
  1537. {
  1538. if (__do_rehash.first)
  1539. _M_rehash(__do_rehash.second, __saved_state);
  1540. this->_M_store_code(__node, __code);
  1541. const key_type& __k = this->_M_extract()(__node->_M_v());
  1542. size_type __bkt = _M_bucket_index(__k, __code);
  1543. // Find the node before an equivalent one or use hint if it exists and
  1544. // if it is equivalent.
  1545. __node_base* __prev
  1546. = __builtin_expect(__hint != nullptr, false)
  1547. && this->_M_equals(__k, __code, __hint)
  1548. ? __hint
  1549. : _M_find_before_node(__bkt, __k, __code);
  1550. if (__prev)
  1551. {
  1552. // Insert after the node before the equivalent one.
  1553. __node->_M_nxt = __prev->_M_nxt;
  1554. __prev->_M_nxt = __node;
  1555. if (__builtin_expect(__prev == __hint, false))
  1556. // hint might be the last bucket node, in this case we need to
  1557. // update next bucket.
  1558. if (__node->_M_nxt
  1559. && !this->_M_equals(__k, __code, __node->_M_next()))
  1560. {
  1561. size_type __next_bkt = _M_bucket_index(__node->_M_next());
  1562. if (__next_bkt != __bkt)
  1563. _M_buckets[__next_bkt] = __node;
  1564. }
  1565. }
  1566. else
  1567. // The inserted node has no equivalent in the
  1568. // hashtable. We must insert the new node at the
  1569. // beginning of the bucket to preserve equivalent
  1570. // elements' relative positions.
  1571. _M_insert_bucket_begin(__bkt, __node);
  1572. ++_M_element_count;
  1573. return iterator(__node);
  1574. }
  1575. __catch(...)
  1576. {
  1577. this->_M_deallocate_node(__node);
  1578. __throw_exception_again;
  1579. }
  1580. }
  1581. // Insert v if no element with its key is already present.
  1582. template<typename _Key, typename _Value,
  1583. typename _Alloc, typename _ExtractKey, typename _Equal,
  1584. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1585. typename _Traits>
  1586. template<typename _Arg, typename _NodeGenerator>
  1587. auto
  1588. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1589. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1590. _M_insert(_Arg&& __v, const _NodeGenerator& __node_gen, true_type,
  1591. size_type __n_elt)
  1592. -> pair<iterator, bool>
  1593. {
  1594. const key_type& __k = this->_M_extract()(__v);
  1595. __hash_code __code = this->_M_hash_code(__k);
  1596. size_type __bkt = _M_bucket_index(__k, __code);
  1597. __node_type* __n = _M_find_node(__bkt, __k, __code);
  1598. if (__n)
  1599. return std::make_pair(iterator(__n), false);
  1600. __n = __node_gen(std::forward<_Arg>(__v));
  1601. return { _M_insert_unique_node(__bkt, __code, __n, __n_elt), true };
  1602. }
  1603. // Insert v unconditionally.
  1604. template<typename _Key, typename _Value,
  1605. typename _Alloc, typename _ExtractKey, typename _Equal,
  1606. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1607. typename _Traits>
  1608. template<typename _Arg, typename _NodeGenerator>
  1609. auto
  1610. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1611. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1612. _M_insert(const_iterator __hint, _Arg&& __v,
  1613. const _NodeGenerator& __node_gen, false_type)
  1614. -> iterator
  1615. {
  1616. // First compute the hash code so that we don't do anything if it
  1617. // throws.
  1618. __hash_code __code = this->_M_hash_code(this->_M_extract()(__v));
  1619. // Second allocate new node so that we don't rehash if it throws.
  1620. __node_type* __node = __node_gen(std::forward<_Arg>(__v));
  1621. return _M_insert_multi_node(__hint._M_cur, __code, __node);
  1622. }
  1623. template<typename _Key, typename _Value,
  1624. typename _Alloc, typename _ExtractKey, typename _Equal,
  1625. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1626. typename _Traits>
  1627. auto
  1628. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1629. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1630. erase(const_iterator __it)
  1631. -> iterator
  1632. {
  1633. __node_type* __n = __it._M_cur;
  1634. std::size_t __bkt = _M_bucket_index(__n);
  1635. // Look for previous node to unlink it from the erased one, this
  1636. // is why we need buckets to contain the before begin to make
  1637. // this search fast.
  1638. __node_base* __prev_n = _M_get_previous_node(__bkt, __n);
  1639. return _M_erase(__bkt, __prev_n, __n);
  1640. }
  1641. template<typename _Key, typename _Value,
  1642. typename _Alloc, typename _ExtractKey, typename _Equal,
  1643. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1644. typename _Traits>
  1645. auto
  1646. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1647. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1648. _M_erase(size_type __bkt, __node_base* __prev_n, __node_type* __n)
  1649. -> iterator
  1650. {
  1651. if (__prev_n == _M_buckets[__bkt])
  1652. _M_remove_bucket_begin(__bkt, __n->_M_next(),
  1653. __n->_M_nxt ? _M_bucket_index(__n->_M_next()) : 0);
  1654. else if (__n->_M_nxt)
  1655. {
  1656. size_type __next_bkt = _M_bucket_index(__n->_M_next());
  1657. if (__next_bkt != __bkt)
  1658. _M_buckets[__next_bkt] = __prev_n;
  1659. }
  1660. __prev_n->_M_nxt = __n->_M_nxt;
  1661. iterator __result(__n->_M_next());
  1662. this->_M_deallocate_node(__n);
  1663. --_M_element_count;
  1664. return __result;
  1665. }
  1666. template<typename _Key, typename _Value,
  1667. typename _Alloc, typename _ExtractKey, typename _Equal,
  1668. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1669. typename _Traits>
  1670. auto
  1671. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1672. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1673. _M_erase(std::true_type, const key_type& __k)
  1674. -> size_type
  1675. {
  1676. __hash_code __code = this->_M_hash_code(__k);
  1677. std::size_t __bkt = _M_bucket_index(__k, __code);
  1678. // Look for the node before the first matching node.
  1679. __node_base* __prev_n = _M_find_before_node(__bkt, __k, __code);
  1680. if (!__prev_n)
  1681. return 0;
  1682. // We found a matching node, erase it.
  1683. __node_type* __n = static_cast<__node_type*>(__prev_n->_M_nxt);
  1684. _M_erase(__bkt, __prev_n, __n);
  1685. return 1;
  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::false_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. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1704. // 526. Is it undefined if a function in the standard changes
  1705. // in parameters?
  1706. // We use one loop to find all matching nodes and another to deallocate
  1707. // them so that the key stays valid during the first loop. It might be
  1708. // invalidated indirectly when destroying nodes.
  1709. __node_type* __n = static_cast<__node_type*>(__prev_n->_M_nxt);
  1710. __node_type* __n_last = __n;
  1711. std::size_t __n_last_bkt = __bkt;
  1712. do
  1713. {
  1714. __n_last = __n_last->_M_next();
  1715. if (!__n_last)
  1716. break;
  1717. __n_last_bkt = _M_bucket_index(__n_last);
  1718. }
  1719. while (__n_last_bkt == __bkt && this->_M_equals(__k, __code, __n_last));
  1720. // Deallocate nodes.
  1721. size_type __result = 0;
  1722. do
  1723. {
  1724. __node_type* __p = __n->_M_next();
  1725. this->_M_deallocate_node(__n);
  1726. __n = __p;
  1727. ++__result;
  1728. --_M_element_count;
  1729. }
  1730. while (__n != __n_last);
  1731. if (__prev_n == _M_buckets[__bkt])
  1732. _M_remove_bucket_begin(__bkt, __n_last, __n_last_bkt);
  1733. else if (__n_last && __n_last_bkt != __bkt)
  1734. _M_buckets[__n_last_bkt] = __prev_n;
  1735. __prev_n->_M_nxt = __n_last;
  1736. return __result;
  1737. }
  1738. template<typename _Key, typename _Value,
  1739. typename _Alloc, typename _ExtractKey, typename _Equal,
  1740. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1741. typename _Traits>
  1742. auto
  1743. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1744. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1745. erase(const_iterator __first, const_iterator __last)
  1746. -> iterator
  1747. {
  1748. __node_type* __n = __first._M_cur;
  1749. __node_type* __last_n = __last._M_cur;
  1750. if (__n == __last_n)
  1751. return iterator(__n);
  1752. std::size_t __bkt = _M_bucket_index(__n);
  1753. __node_base* __prev_n = _M_get_previous_node(__bkt, __n);
  1754. bool __is_bucket_begin = __n == _M_bucket_begin(__bkt);
  1755. std::size_t __n_bkt = __bkt;
  1756. for (;;)
  1757. {
  1758. do
  1759. {
  1760. __node_type* __tmp = __n;
  1761. __n = __n->_M_next();
  1762. this->_M_deallocate_node(__tmp);
  1763. --_M_element_count;
  1764. if (!__n)
  1765. break;
  1766. __n_bkt = _M_bucket_index(__n);
  1767. }
  1768. while (__n != __last_n && __n_bkt == __bkt);
  1769. if (__is_bucket_begin)
  1770. _M_remove_bucket_begin(__bkt, __n, __n_bkt);
  1771. if (__n == __last_n)
  1772. break;
  1773. __is_bucket_begin = true;
  1774. __bkt = __n_bkt;
  1775. }
  1776. if (__n && (__n_bkt != __bkt || __is_bucket_begin))
  1777. _M_buckets[__n_bkt] = __prev_n;
  1778. __prev_n->_M_nxt = __n;
  1779. return iterator(__n);
  1780. }
  1781. template<typename _Key, typename _Value,
  1782. typename _Alloc, typename _ExtractKey, typename _Equal,
  1783. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1784. typename _Traits>
  1785. void
  1786. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1787. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1788. clear() noexcept
  1789. {
  1790. this->_M_deallocate_nodes(_M_begin());
  1791. __builtin_memset(_M_buckets, 0, _M_bucket_count * sizeof(__bucket_type));
  1792. _M_element_count = 0;
  1793. _M_before_begin._M_nxt = nullptr;
  1794. }
  1795. template<typename _Key, typename _Value,
  1796. typename _Alloc, typename _ExtractKey, typename _Equal,
  1797. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1798. typename _Traits>
  1799. void
  1800. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1801. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1802. rehash(size_type __n)
  1803. {
  1804. const __rehash_state& __saved_state = _M_rehash_policy._M_state();
  1805. std::size_t __buckets
  1806. = std::max(_M_rehash_policy._M_bkt_for_elements(_M_element_count + 1),
  1807. __n);
  1808. __buckets = _M_rehash_policy._M_next_bkt(__buckets);
  1809. if (__buckets != _M_bucket_count)
  1810. _M_rehash(__buckets, __saved_state);
  1811. else
  1812. // No rehash, restore previous state to keep a consistent state.
  1813. _M_rehash_policy._M_reset(__saved_state);
  1814. }
  1815. template<typename _Key, typename _Value,
  1816. typename _Alloc, typename _ExtractKey, typename _Equal,
  1817. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1818. typename _Traits>
  1819. void
  1820. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1821. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1822. _M_rehash(size_type __n, const __rehash_state& __state)
  1823. {
  1824. __try
  1825. {
  1826. _M_rehash_aux(__n, __unique_keys());
  1827. }
  1828. __catch(...)
  1829. {
  1830. // A failure here means that buckets allocation failed. We only
  1831. // have to restore hash policy previous state.
  1832. _M_rehash_policy._M_reset(__state);
  1833. __throw_exception_again;
  1834. }
  1835. }
  1836. // Rehash when there is no equivalent elements.
  1837. template<typename _Key, typename _Value,
  1838. typename _Alloc, typename _ExtractKey, typename _Equal,
  1839. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1840. typename _Traits>
  1841. void
  1842. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1843. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1844. _M_rehash_aux(size_type __n, std::true_type)
  1845. {
  1846. __bucket_type* __new_buckets = _M_allocate_buckets(__n);
  1847. __node_type* __p = _M_begin();
  1848. _M_before_begin._M_nxt = nullptr;
  1849. std::size_t __bbegin_bkt = 0;
  1850. while (__p)
  1851. {
  1852. __node_type* __next = __p->_M_next();
  1853. std::size_t __bkt = __hash_code_base::_M_bucket_index(__p, __n);
  1854. if (!__new_buckets[__bkt])
  1855. {
  1856. __p->_M_nxt = _M_before_begin._M_nxt;
  1857. _M_before_begin._M_nxt = __p;
  1858. __new_buckets[__bkt] = &_M_before_begin;
  1859. if (__p->_M_nxt)
  1860. __new_buckets[__bbegin_bkt] = __p;
  1861. __bbegin_bkt = __bkt;
  1862. }
  1863. else
  1864. {
  1865. __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
  1866. __new_buckets[__bkt]->_M_nxt = __p;
  1867. }
  1868. __p = __next;
  1869. }
  1870. _M_deallocate_buckets();
  1871. _M_bucket_count = __n;
  1872. _M_buckets = __new_buckets;
  1873. }
  1874. // Rehash when there can be equivalent elements, preserve their relative
  1875. // order.
  1876. template<typename _Key, typename _Value,
  1877. typename _Alloc, typename _ExtractKey, typename _Equal,
  1878. typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
  1879. typename _Traits>
  1880. void
  1881. _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
  1882. _H1, _H2, _Hash, _RehashPolicy, _Traits>::
  1883. _M_rehash_aux(size_type __n, std::false_type)
  1884. {
  1885. __bucket_type* __new_buckets = _M_allocate_buckets(__n);
  1886. __node_type* __p = _M_begin();
  1887. _M_before_begin._M_nxt = nullptr;
  1888. std::size_t __bbegin_bkt = 0;
  1889. std::size_t __prev_bkt = 0;
  1890. __node_type* __prev_p = nullptr;
  1891. bool __check_bucket = false;
  1892. while (__p)
  1893. {
  1894. __node_type* __next = __p->_M_next();
  1895. std::size_t __bkt = __hash_code_base::_M_bucket_index(__p, __n);
  1896. if (__prev_p && __prev_bkt == __bkt)
  1897. {
  1898. // Previous insert was already in this bucket, we insert after
  1899. // the previously inserted one to preserve equivalent elements
  1900. // relative order.
  1901. __p->_M_nxt = __prev_p->_M_nxt;
  1902. __prev_p->_M_nxt = __p;
  1903. // Inserting after a node in a bucket require to check that we
  1904. // haven't change the bucket last node, in this case next
  1905. // bucket containing its before begin node must be updated. We
  1906. // schedule a check as soon as we move out of the sequence of
  1907. // equivalent nodes to limit the number of checks.
  1908. __check_bucket = true;
  1909. }
  1910. else
  1911. {
  1912. if (__check_bucket)
  1913. {
  1914. // Check if we shall update the next bucket because of
  1915. // insertions into __prev_bkt bucket.
  1916. if (__prev_p->_M_nxt)
  1917. {
  1918. std::size_t __next_bkt
  1919. = __hash_code_base::_M_bucket_index(__prev_p->_M_next(),
  1920. __n);
  1921. if (__next_bkt != __prev_bkt)
  1922. __new_buckets[__next_bkt] = __prev_p;
  1923. }
  1924. __check_bucket = false;
  1925. }
  1926. if (!__new_buckets[__bkt])
  1927. {
  1928. __p->_M_nxt = _M_before_begin._M_nxt;
  1929. _M_before_begin._M_nxt = __p;
  1930. __new_buckets[__bkt] = &_M_before_begin;
  1931. if (__p->_M_nxt)
  1932. __new_buckets[__bbegin_bkt] = __p;
  1933. __bbegin_bkt = __bkt;
  1934. }
  1935. else
  1936. {
  1937. __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
  1938. __new_buckets[__bkt]->_M_nxt = __p;
  1939. }
  1940. }
  1941. __prev_p = __p;
  1942. __prev_bkt = __bkt;
  1943. __p = __next;
  1944. }
  1945. if (__check_bucket && __prev_p->_M_nxt)
  1946. {
  1947. std::size_t __next_bkt
  1948. = __hash_code_base::_M_bucket_index(__prev_p->_M_next(), __n);
  1949. if (__next_bkt != __prev_bkt)
  1950. __new_buckets[__next_bkt] = __prev_p;
  1951. }
  1952. _M_deallocate_buckets();
  1953. _M_bucket_count = __n;
  1954. _M_buckets = __new_buckets;
  1955. }
  1956. #if __cplusplus > 201402L
  1957. template<typename, typename, typename> class _Hash_merge_helper { };
  1958. #endif // C++17
  1959. #if __cpp_deduction_guides >= 201606
  1960. // Used to constrain deduction guides
  1961. template<typename _Hash>
  1962. using _RequireNotAllocatorOrIntegral
  1963. = __enable_if_t<!__or_<is_integral<_Hash>, __is_allocator<_Hash>>::value>;
  1964. #endif
  1965. _GLIBCXX_END_NAMESPACE_VERSION
  1966. } // namespace std
  1967. #endif // _HASHTABLE_H