__hash_table 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___HASH_TABLE
  10. #define _LIBCPP___HASH_TABLE
  11. #include <__algorithm/max.h>
  12. #include <__algorithm/min.h>
  13. #include <__assert>
  14. #include <__bit/countl.h>
  15. #include <__config>
  16. #include <__functional/hash.h>
  17. #include <__functional/invoke.h>
  18. #include <__iterator/iterator_traits.h>
  19. #include <__memory/addressof.h>
  20. #include <__memory/allocator_traits.h>
  21. #include <__memory/compressed_pair.h>
  22. #include <__memory/construct_at.h>
  23. #include <__memory/pointer_traits.h>
  24. #include <__memory/swap_allocator.h>
  25. #include <__memory/unique_ptr.h>
  26. #include <__type_traits/can_extract_key.h>
  27. #include <__type_traits/conditional.h>
  28. #include <__type_traits/is_const.h>
  29. #include <__type_traits/is_constructible.h>
  30. #include <__type_traits/is_nothrow_assignable.h>
  31. #include <__type_traits/is_nothrow_constructible.h>
  32. #include <__type_traits/is_pointer.h>
  33. #include <__type_traits/is_reference.h>
  34. #include <__type_traits/is_swappable.h>
  35. #include <__type_traits/remove_const.h>
  36. #include <__type_traits/remove_cvref.h>
  37. #include <__utility/forward.h>
  38. #include <__utility/move.h>
  39. #include <__utility/pair.h>
  40. #include <__utility/swap.h>
  41. #include <cmath>
  42. #include <cstring>
  43. #include <initializer_list>
  44. #include <new> // __launder
  45. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  46. # pragma GCC system_header
  47. #endif
  48. _LIBCPP_PUSH_MACROS
  49. #include <__undef_macros>
  50. _LIBCPP_BEGIN_NAMESPACE_STD
  51. template <class _Key, class _Tp>
  52. struct __hash_value_type;
  53. template <class _Tp>
  54. struct __is_hash_value_type_imp : false_type {};
  55. template <class _Key, class _Value>
  56. struct __is_hash_value_type_imp<__hash_value_type<_Key, _Value> > : true_type {};
  57. template <class... _Args>
  58. struct __is_hash_value_type : false_type {};
  59. template <class _One>
  60. struct __is_hash_value_type<_One> : __is_hash_value_type_imp<__remove_cvref_t<_One> > {};
  61. _LIBCPP_EXPORTED_FROM_ABI size_t __next_prime(size_t __n);
  62. template <class _NodePtr>
  63. struct __hash_node_base {
  64. typedef typename pointer_traits<_NodePtr>::element_type __node_type;
  65. typedef __hash_node_base __first_node;
  66. typedef __rebind_pointer_t<_NodePtr, __first_node> __node_base_pointer;
  67. typedef _NodePtr __node_pointer;
  68. #if defined(_LIBCPP_ABI_FIX_UNORDERED_NODE_POINTER_UB)
  69. typedef __node_base_pointer __next_pointer;
  70. #else
  71. typedef __conditional_t<is_pointer<__node_pointer>::value, __node_base_pointer, __node_pointer> __next_pointer;
  72. #endif
  73. __next_pointer __next_;
  74. _LIBCPP_HIDE_FROM_ABI __next_pointer __ptr() _NOEXCEPT {
  75. return static_cast<__next_pointer>(pointer_traits<__node_base_pointer>::pointer_to(*this));
  76. }
  77. _LIBCPP_HIDE_FROM_ABI __node_pointer __upcast() _NOEXCEPT {
  78. return static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(*this));
  79. }
  80. _LIBCPP_HIDE_FROM_ABI size_t __hash() const _NOEXCEPT { return static_cast<__node_type const&>(*this).__hash_; }
  81. _LIBCPP_HIDE_FROM_ABI __hash_node_base() _NOEXCEPT : __next_(nullptr) {}
  82. _LIBCPP_HIDE_FROM_ABI explicit __hash_node_base(__next_pointer __next) _NOEXCEPT : __next_(__next) {}
  83. };
  84. template <class _Tp, class _VoidPtr>
  85. struct __hash_node : public __hash_node_base< __rebind_pointer_t<_VoidPtr, __hash_node<_Tp, _VoidPtr> > > {
  86. typedef _Tp __node_value_type;
  87. using _Base = __hash_node_base<__rebind_pointer_t<_VoidPtr, __hash_node<_Tp, _VoidPtr> > >;
  88. using __next_pointer = typename _Base::__next_pointer;
  89. size_t __hash_;
  90. // We allow starting the lifetime of nodes without initializing the value held by the node,
  91. // since that is handled by the hash table itself in order to be allocator-aware.
  92. #ifndef _LIBCPP_CXX03_LANG
  93. private:
  94. union {
  95. _Tp __value_;
  96. };
  97. public:
  98. _LIBCPP_HIDE_FROM_ABI _Tp& __get_value() { return __value_; }
  99. #else
  100. private:
  101. _ALIGNAS_TYPE(_Tp) char __buffer_[sizeof(_Tp)];
  102. public:
  103. _LIBCPP_HIDE_FROM_ABI _Tp& __get_value() { return *std::__launder(reinterpret_cast<_Tp*>(&__buffer_)); }
  104. #endif
  105. _LIBCPP_HIDE_FROM_ABI explicit __hash_node(__next_pointer __next, size_t __hash) : _Base(__next), __hash_(__hash) {}
  106. _LIBCPP_HIDE_FROM_ABI ~__hash_node() {}
  107. };
  108. inline _LIBCPP_HIDE_FROM_ABI bool __is_hash_power2(size_t __bc) { return __bc > 2 && !(__bc & (__bc - 1)); }
  109. inline _LIBCPP_HIDE_FROM_ABI size_t __constrain_hash(size_t __h, size_t __bc) {
  110. return !(__bc & (__bc - 1)) ? __h & (__bc - 1) : (__h < __bc ? __h : __h % __bc);
  111. }
  112. inline _LIBCPP_HIDE_FROM_ABI size_t __next_hash_pow2(size_t __n) {
  113. return __n < 2 ? __n : (size_t(1) << (numeric_limits<size_t>::digits - __libcpp_clz(__n - 1)));
  114. }
  115. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  116. class __hash_table;
  117. template <class _NodePtr>
  118. class _LIBCPP_TEMPLATE_VIS __hash_iterator;
  119. template <class _ConstNodePtr>
  120. class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
  121. template <class _NodePtr>
  122. class _LIBCPP_TEMPLATE_VIS __hash_local_iterator;
  123. template <class _ConstNodePtr>
  124. class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
  125. template <class _HashIterator>
  126. class _LIBCPP_TEMPLATE_VIS __hash_map_iterator;
  127. template <class _HashIterator>
  128. class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
  129. template <class _Tp>
  130. struct __hash_key_value_types {
  131. static_assert(!is_reference<_Tp>::value && !is_const<_Tp>::value, "");
  132. typedef _Tp key_type;
  133. typedef _Tp __node_value_type;
  134. typedef _Tp __container_value_type;
  135. static const bool __is_map = false;
  136. _LIBCPP_HIDE_FROM_ABI static key_type const& __get_key(_Tp const& __v) { return __v; }
  137. _LIBCPP_HIDE_FROM_ABI static __container_value_type const& __get_value(__node_value_type const& __v) { return __v; }
  138. _LIBCPP_HIDE_FROM_ABI static __container_value_type* __get_ptr(__node_value_type& __n) { return std::addressof(__n); }
  139. _LIBCPP_HIDE_FROM_ABI static __container_value_type&& __move(__node_value_type& __v) { return std::move(__v); }
  140. };
  141. template <class _Key, class _Tp>
  142. struct __hash_key_value_types<__hash_value_type<_Key, _Tp> > {
  143. typedef _Key key_type;
  144. typedef _Tp mapped_type;
  145. typedef __hash_value_type<_Key, _Tp> __node_value_type;
  146. typedef pair<const _Key, _Tp> __container_value_type;
  147. typedef __container_value_type __map_value_type;
  148. static const bool __is_map = true;
  149. _LIBCPP_HIDE_FROM_ABI static key_type const& __get_key(__container_value_type const& __v) { return __v.first; }
  150. template <class _Up, __enable_if_t<__is_same_uncvref<_Up, __node_value_type>::value, int> = 0>
  151. _LIBCPP_HIDE_FROM_ABI static __container_value_type const& __get_value(_Up& __t) {
  152. return __t.__get_value();
  153. }
  154. template <class _Up, __enable_if_t<__is_same_uncvref<_Up, __container_value_type>::value, int> = 0>
  155. _LIBCPP_HIDE_FROM_ABI static __container_value_type const& __get_value(_Up& __t) {
  156. return __t;
  157. }
  158. _LIBCPP_HIDE_FROM_ABI static __container_value_type* __get_ptr(__node_value_type& __n) {
  159. return std::addressof(__n.__get_value());
  160. }
  161. _LIBCPP_HIDE_FROM_ABI static pair<key_type&&, mapped_type&&> __move(__node_value_type& __v) { return __v.__move(); }
  162. };
  163. template <class _Tp, class _AllocPtr, class _KVTypes = __hash_key_value_types<_Tp>, bool = _KVTypes::__is_map>
  164. struct __hash_map_pointer_types {};
  165. template <class _Tp, class _AllocPtr, class _KVTypes>
  166. struct __hash_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
  167. typedef typename _KVTypes::__map_value_type _Mv;
  168. typedef __rebind_pointer_t<_AllocPtr, _Mv> __map_value_type_pointer;
  169. typedef __rebind_pointer_t<_AllocPtr, const _Mv> __const_map_value_type_pointer;
  170. };
  171. template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
  172. struct __hash_node_types;
  173. template <class _NodePtr, class _Tp, class _VoidPtr>
  174. struct __hash_node_types<_NodePtr, __hash_node<_Tp, _VoidPtr> >
  175. : public __hash_key_value_types<_Tp>,
  176. __hash_map_pointer_types<_Tp, _VoidPtr>
  177. {
  178. typedef __hash_key_value_types<_Tp> __base;
  179. public:
  180. typedef ptrdiff_t difference_type;
  181. typedef size_t size_type;
  182. typedef __rebind_pointer_t<_NodePtr, void> __void_pointer;
  183. typedef typename pointer_traits<_NodePtr>::element_type __node_type;
  184. typedef _NodePtr __node_pointer;
  185. typedef __hash_node_base<__node_pointer> __node_base_type;
  186. typedef __rebind_pointer_t<_NodePtr, __node_base_type> __node_base_pointer;
  187. typedef typename __node_base_type::__next_pointer __next_pointer;
  188. typedef _Tp __node_value_type;
  189. typedef __rebind_pointer_t<_VoidPtr, __node_value_type> __node_value_type_pointer;
  190. typedef __rebind_pointer_t<_VoidPtr, const __node_value_type> __const_node_value_type_pointer;
  191. private:
  192. static_assert(!is_const<__node_type>::value, "_NodePtr should never be a pointer to const");
  193. static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
  194. "_VoidPtr does not point to unqualified void type");
  195. static_assert((is_same<__rebind_pointer_t<_VoidPtr, __node_type>, _NodePtr>::value),
  196. "_VoidPtr does not rebind to _NodePtr.");
  197. };
  198. template <class _HashIterator>
  199. struct __hash_node_types_from_iterator;
  200. template <class _NodePtr>
  201. struct __hash_node_types_from_iterator<__hash_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
  202. template <class _NodePtr>
  203. struct __hash_node_types_from_iterator<__hash_const_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
  204. template <class _NodePtr>
  205. struct __hash_node_types_from_iterator<__hash_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
  206. template <class _NodePtr>
  207. struct __hash_node_types_from_iterator<__hash_const_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
  208. template <class _NodeValueTp, class _VoidPtr>
  209. struct __make_hash_node_types {
  210. typedef __hash_node<_NodeValueTp, _VoidPtr> _NodeTp;
  211. typedef __rebind_pointer_t<_VoidPtr, _NodeTp> _NodePtr;
  212. typedef __hash_node_types<_NodePtr> type;
  213. };
  214. template <class _NodePtr>
  215. class _LIBCPP_TEMPLATE_VIS __hash_iterator {
  216. typedef __hash_node_types<_NodePtr> _NodeTypes;
  217. typedef _NodePtr __node_pointer;
  218. typedef typename _NodeTypes::__next_pointer __next_pointer;
  219. __next_pointer __node_;
  220. public:
  221. typedef forward_iterator_tag iterator_category;
  222. typedef typename _NodeTypes::__node_value_type value_type;
  223. typedef typename _NodeTypes::difference_type difference_type;
  224. typedef value_type& reference;
  225. typedef typename _NodeTypes::__node_value_type_pointer pointer;
  226. _LIBCPP_HIDE_FROM_ABI __hash_iterator() _NOEXCEPT : __node_(nullptr) {}
  227. _LIBCPP_HIDE_FROM_ABI reference operator*() const {
  228. _LIBCPP_ASSERT_NON_NULL(
  229. __node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container iterator");
  230. return __node_->__upcast()->__get_value();
  231. }
  232. _LIBCPP_HIDE_FROM_ABI pointer operator->() const {
  233. _LIBCPP_ASSERT_NON_NULL(
  234. __node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container iterator");
  235. return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__get_value());
  236. }
  237. _LIBCPP_HIDE_FROM_ABI __hash_iterator& operator++() {
  238. _LIBCPP_ASSERT_NON_NULL(
  239. __node_ != nullptr, "Attempted to increment a non-incrementable unordered container iterator");
  240. __node_ = __node_->__next_;
  241. return *this;
  242. }
  243. _LIBCPP_HIDE_FROM_ABI __hash_iterator operator++(int) {
  244. __hash_iterator __t(*this);
  245. ++(*this);
  246. return __t;
  247. }
  248. friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __hash_iterator& __x, const __hash_iterator& __y) {
  249. return __x.__node_ == __y.__node_;
  250. }
  251. friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y) {
  252. return !(__x == __y);
  253. }
  254. private:
  255. _LIBCPP_HIDE_FROM_ABI explicit __hash_iterator(__next_pointer __node) _NOEXCEPT : __node_(__node) {}
  256. template <class, class, class, class>
  257. friend class __hash_table;
  258. template <class>
  259. friend class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
  260. template <class>
  261. friend class _LIBCPP_TEMPLATE_VIS __hash_map_iterator;
  262. template <class, class, class, class, class>
  263. friend class _LIBCPP_TEMPLATE_VIS unordered_map;
  264. template <class, class, class, class, class>
  265. friend class _LIBCPP_TEMPLATE_VIS unordered_multimap;
  266. };
  267. template <class _NodePtr>
  268. class _LIBCPP_TEMPLATE_VIS __hash_const_iterator {
  269. static_assert(!is_const<typename pointer_traits<_NodePtr>::element_type>::value, "");
  270. typedef __hash_node_types<_NodePtr> _NodeTypes;
  271. typedef _NodePtr __node_pointer;
  272. typedef typename _NodeTypes::__next_pointer __next_pointer;
  273. __next_pointer __node_;
  274. public:
  275. typedef __hash_iterator<_NodePtr> __non_const_iterator;
  276. typedef forward_iterator_tag iterator_category;
  277. typedef typename _NodeTypes::__node_value_type value_type;
  278. typedef typename _NodeTypes::difference_type difference_type;
  279. typedef const value_type& reference;
  280. typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
  281. _LIBCPP_HIDE_FROM_ABI __hash_const_iterator() _NOEXCEPT : __node_(nullptr) {}
  282. _LIBCPP_HIDE_FROM_ABI __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT : __node_(__x.__node_) {}
  283. _LIBCPP_HIDE_FROM_ABI reference operator*() const {
  284. _LIBCPP_ASSERT_NON_NULL(
  285. __node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container const_iterator");
  286. return __node_->__upcast()->__get_value();
  287. }
  288. _LIBCPP_HIDE_FROM_ABI pointer operator->() const {
  289. _LIBCPP_ASSERT_NON_NULL(
  290. __node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container const_iterator");
  291. return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__get_value());
  292. }
  293. _LIBCPP_HIDE_FROM_ABI __hash_const_iterator& operator++() {
  294. _LIBCPP_ASSERT_NON_NULL(
  295. __node_ != nullptr, "Attempted to increment a non-incrementable unordered container const_iterator");
  296. __node_ = __node_->__next_;
  297. return *this;
  298. }
  299. _LIBCPP_HIDE_FROM_ABI __hash_const_iterator operator++(int) {
  300. __hash_const_iterator __t(*this);
  301. ++(*this);
  302. return __t;
  303. }
  304. friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y) {
  305. return __x.__node_ == __y.__node_;
  306. }
  307. friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y) {
  308. return !(__x == __y);
  309. }
  310. private:
  311. _LIBCPP_HIDE_FROM_ABI explicit __hash_const_iterator(__next_pointer __node) _NOEXCEPT : __node_(__node) {}
  312. template <class, class, class, class>
  313. friend class __hash_table;
  314. template <class>
  315. friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
  316. template <class, class, class, class, class>
  317. friend class _LIBCPP_TEMPLATE_VIS unordered_map;
  318. template <class, class, class, class, class>
  319. friend class _LIBCPP_TEMPLATE_VIS unordered_multimap;
  320. };
  321. template <class _NodePtr>
  322. class _LIBCPP_TEMPLATE_VIS __hash_local_iterator {
  323. typedef __hash_node_types<_NodePtr> _NodeTypes;
  324. typedef _NodePtr __node_pointer;
  325. typedef typename _NodeTypes::__next_pointer __next_pointer;
  326. __next_pointer __node_;
  327. size_t __bucket_;
  328. size_t __bucket_count_;
  329. public:
  330. typedef forward_iterator_tag iterator_category;
  331. typedef typename _NodeTypes::__node_value_type value_type;
  332. typedef typename _NodeTypes::difference_type difference_type;
  333. typedef value_type& reference;
  334. typedef typename _NodeTypes::__node_value_type_pointer pointer;
  335. _LIBCPP_HIDE_FROM_ABI __hash_local_iterator() _NOEXCEPT : __node_(nullptr) {}
  336. _LIBCPP_HIDE_FROM_ABI reference operator*() const {
  337. _LIBCPP_ASSERT_NON_NULL(
  338. __node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container local_iterator");
  339. return __node_->__upcast()->__get_value();
  340. }
  341. _LIBCPP_HIDE_FROM_ABI pointer operator->() const {
  342. _LIBCPP_ASSERT_NON_NULL(
  343. __node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container local_iterator");
  344. return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__get_value());
  345. }
  346. _LIBCPP_HIDE_FROM_ABI __hash_local_iterator& operator++() {
  347. _LIBCPP_ASSERT_NON_NULL(
  348. __node_ != nullptr, "Attempted to increment a non-incrementable unordered container local_iterator");
  349. __node_ = __node_->__next_;
  350. if (__node_ != nullptr && std::__constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
  351. __node_ = nullptr;
  352. return *this;
  353. }
  354. _LIBCPP_HIDE_FROM_ABI __hash_local_iterator operator++(int) {
  355. __hash_local_iterator __t(*this);
  356. ++(*this);
  357. return __t;
  358. }
  359. friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y) {
  360. return __x.__node_ == __y.__node_;
  361. }
  362. friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y) {
  363. return !(__x == __y);
  364. }
  365. private:
  366. _LIBCPP_HIDE_FROM_ABI explicit __hash_local_iterator(
  367. __next_pointer __node, size_t __bucket, size_t __bucket_count) _NOEXCEPT
  368. : __node_(__node),
  369. __bucket_(__bucket),
  370. __bucket_count_(__bucket_count) {
  371. if (__node_ != nullptr)
  372. __node_ = __node_->__next_;
  373. }
  374. template <class, class, class, class>
  375. friend class __hash_table;
  376. template <class>
  377. friend class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
  378. template <class>
  379. friend class _LIBCPP_TEMPLATE_VIS __hash_map_iterator;
  380. };
  381. template <class _ConstNodePtr>
  382. class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator {
  383. typedef __hash_node_types<_ConstNodePtr> _NodeTypes;
  384. typedef _ConstNodePtr __node_pointer;
  385. typedef typename _NodeTypes::__next_pointer __next_pointer;
  386. __next_pointer __node_;
  387. size_t __bucket_;
  388. size_t __bucket_count_;
  389. typedef pointer_traits<__node_pointer> __pointer_traits;
  390. typedef typename __pointer_traits::element_type __node;
  391. typedef __remove_const_t<__node> __non_const_node;
  392. typedef __rebind_pointer_t<__node_pointer, __non_const_node> __non_const_node_pointer;
  393. public:
  394. typedef __hash_local_iterator<__non_const_node_pointer> __non_const_iterator;
  395. typedef forward_iterator_tag iterator_category;
  396. typedef typename _NodeTypes::__node_value_type value_type;
  397. typedef typename _NodeTypes::difference_type difference_type;
  398. typedef const value_type& reference;
  399. typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
  400. _LIBCPP_HIDE_FROM_ABI __hash_const_local_iterator() _NOEXCEPT : __node_(nullptr) {}
  401. _LIBCPP_HIDE_FROM_ABI __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT
  402. : __node_(__x.__node_),
  403. __bucket_(__x.__bucket_),
  404. __bucket_count_(__x.__bucket_count_) {}
  405. _LIBCPP_HIDE_FROM_ABI reference operator*() const {
  406. _LIBCPP_ASSERT_NON_NULL(
  407. __node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
  408. return __node_->__upcast()->__get_value();
  409. }
  410. _LIBCPP_HIDE_FROM_ABI pointer operator->() const {
  411. _LIBCPP_ASSERT_NON_NULL(
  412. __node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
  413. return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__get_value());
  414. }
  415. _LIBCPP_HIDE_FROM_ABI __hash_const_local_iterator& operator++() {
  416. _LIBCPP_ASSERT_NON_NULL(
  417. __node_ != nullptr, "Attempted to increment a non-incrementable unordered container const_local_iterator");
  418. __node_ = __node_->__next_;
  419. if (__node_ != nullptr && std::__constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
  420. __node_ = nullptr;
  421. return *this;
  422. }
  423. _LIBCPP_HIDE_FROM_ABI __hash_const_local_iterator operator++(int) {
  424. __hash_const_local_iterator __t(*this);
  425. ++(*this);
  426. return __t;
  427. }
  428. friend _LIBCPP_HIDE_FROM_ABI bool
  429. operator==(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y) {
  430. return __x.__node_ == __y.__node_;
  431. }
  432. friend _LIBCPP_HIDE_FROM_ABI bool
  433. operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y) {
  434. return !(__x == __y);
  435. }
  436. private:
  437. _LIBCPP_HIDE_FROM_ABI explicit __hash_const_local_iterator(
  438. __next_pointer __node_ptr, size_t __bucket, size_t __bucket_count) _NOEXCEPT
  439. : __node_(__node_ptr),
  440. __bucket_(__bucket),
  441. __bucket_count_(__bucket_count) {
  442. if (__node_ != nullptr)
  443. __node_ = __node_->__next_;
  444. }
  445. template <class, class, class, class>
  446. friend class __hash_table;
  447. template <class>
  448. friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
  449. };
  450. template <class _Alloc>
  451. class __bucket_list_deallocator {
  452. typedef _Alloc allocator_type;
  453. typedef allocator_traits<allocator_type> __alloc_traits;
  454. typedef typename __alloc_traits::size_type size_type;
  455. __compressed_pair<size_type, allocator_type> __data_;
  456. public:
  457. typedef typename __alloc_traits::pointer pointer;
  458. _LIBCPP_HIDE_FROM_ABI __bucket_list_deallocator() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
  459. : __data_(0, __default_init_tag()) {}
  460. _LIBCPP_HIDE_FROM_ABI __bucket_list_deallocator(const allocator_type& __a, size_type __size)
  461. _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
  462. : __data_(__size, __a) {}
  463. _LIBCPP_HIDE_FROM_ABI __bucket_list_deallocator(__bucket_list_deallocator&& __x)
  464. _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
  465. : __data_(std::move(__x.__data_)) {
  466. __x.size() = 0;
  467. }
  468. _LIBCPP_HIDE_FROM_ABI size_type& size() _NOEXCEPT { return __data_.first(); }
  469. _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __data_.first(); }
  470. _LIBCPP_HIDE_FROM_ABI allocator_type& __alloc() _NOEXCEPT { return __data_.second(); }
  471. _LIBCPP_HIDE_FROM_ABI const allocator_type& __alloc() const _NOEXCEPT { return __data_.second(); }
  472. _LIBCPP_HIDE_FROM_ABI void operator()(pointer __p) _NOEXCEPT { __alloc_traits::deallocate(__alloc(), __p, size()); }
  473. };
  474. template <class _Alloc>
  475. class __hash_map_node_destructor;
  476. template <class _Alloc>
  477. class __hash_node_destructor {
  478. typedef _Alloc allocator_type;
  479. typedef allocator_traits<allocator_type> __alloc_traits;
  480. public:
  481. typedef typename __alloc_traits::pointer pointer;
  482. private:
  483. typedef __hash_node_types<pointer> _NodeTypes;
  484. allocator_type& __na_;
  485. public:
  486. bool __value_constructed;
  487. _LIBCPP_HIDE_FROM_ABI __hash_node_destructor(__hash_node_destructor const&) = default;
  488. _LIBCPP_HIDE_FROM_ABI __hash_node_destructor& operator=(const __hash_node_destructor&) = delete;
  489. _LIBCPP_HIDE_FROM_ABI explicit __hash_node_destructor(allocator_type& __na, bool __constructed = false) _NOEXCEPT
  490. : __na_(__na),
  491. __value_constructed(__constructed) {}
  492. _LIBCPP_HIDE_FROM_ABI void operator()(pointer __p) _NOEXCEPT {
  493. if (__value_constructed) {
  494. __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__get_value()));
  495. std::__destroy_at(std::addressof(*__p));
  496. }
  497. if (__p)
  498. __alloc_traits::deallocate(__na_, __p, 1);
  499. }
  500. template <class>
  501. friend class __hash_map_node_destructor;
  502. };
  503. #if _LIBCPP_STD_VER >= 17
  504. template <class _NodeType, class _Alloc>
  505. struct __generic_container_node_destructor;
  506. template <class _Tp, class _VoidPtr, class _Alloc>
  507. struct __generic_container_node_destructor<__hash_node<_Tp, _VoidPtr>, _Alloc> : __hash_node_destructor<_Alloc> {
  508. using __hash_node_destructor<_Alloc>::__hash_node_destructor;
  509. };
  510. #endif
  511. template <class _Key, class _Hash, class _Equal>
  512. struct __enforce_unordered_container_requirements {
  513. #ifndef _LIBCPP_CXX03_LANG
  514. static_assert(__check_hash_requirements<_Key, _Hash>::value,
  515. "the specified hash does not meet the Hash requirements");
  516. static_assert(is_copy_constructible<_Equal>::value, "the specified comparator is required to be copy constructible");
  517. #endif
  518. typedef int type;
  519. };
  520. template <class _Key, class _Hash, class _Equal>
  521. #ifndef _LIBCPP_CXX03_LANG
  522. _LIBCPP_DIAGNOSE_WARNING(!__invokable<_Equal const&, _Key const&, _Key const&>::value,
  523. "the specified comparator type does not provide a viable const call operator")
  524. _LIBCPP_DIAGNOSE_WARNING(!__invokable<_Hash const&, _Key const&>::value,
  525. "the specified hash functor does not provide a viable const call operator")
  526. #endif
  527. typename __enforce_unordered_container_requirements<_Key, _Hash, _Equal>::type
  528. __diagnose_unordered_container_requirements(int);
  529. // This dummy overload is used so that the compiler won't emit a spurious
  530. // "no matching function for call to __diagnose_unordered_xxx" diagnostic
  531. // when the overload above causes a hard error.
  532. template <class _Key, class _Hash, class _Equal>
  533. int __diagnose_unordered_container_requirements(void*);
  534. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  535. class __hash_table {
  536. public:
  537. typedef _Tp value_type;
  538. typedef _Hash hasher;
  539. typedef _Equal key_equal;
  540. typedef _Alloc allocator_type;
  541. private:
  542. typedef allocator_traits<allocator_type> __alloc_traits;
  543. typedef typename __make_hash_node_types<value_type, typename __alloc_traits::void_pointer>::type _NodeTypes;
  544. public:
  545. typedef typename _NodeTypes::__node_value_type __node_value_type;
  546. typedef typename _NodeTypes::__container_value_type __container_value_type;
  547. typedef typename _NodeTypes::key_type key_type;
  548. typedef value_type& reference;
  549. typedef const value_type& const_reference;
  550. typedef typename __alloc_traits::pointer pointer;
  551. typedef typename __alloc_traits::const_pointer const_pointer;
  552. #ifndef _LIBCPP_ABI_FIX_UNORDERED_CONTAINER_SIZE_TYPE
  553. typedef typename __alloc_traits::size_type size_type;
  554. #else
  555. typedef typename _NodeTypes::size_type size_type;
  556. #endif
  557. typedef typename _NodeTypes::difference_type difference_type;
  558. public:
  559. // Create __node
  560. typedef typename _NodeTypes::__node_type __node;
  561. typedef __rebind_alloc<__alloc_traits, __node> __node_allocator;
  562. typedef allocator_traits<__node_allocator> __node_traits;
  563. typedef typename _NodeTypes::__void_pointer __void_pointer;
  564. typedef typename _NodeTypes::__node_pointer __node_pointer;
  565. typedef typename _NodeTypes::__node_pointer __node_const_pointer;
  566. typedef typename _NodeTypes::__node_base_type __first_node;
  567. typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
  568. typedef typename _NodeTypes::__next_pointer __next_pointer;
  569. private:
  570. // check for sane allocator pointer rebinding semantics. Rebinding the
  571. // allocator for a new pointer type should be exactly the same as rebinding
  572. // the pointer using 'pointer_traits'.
  573. static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
  574. "Allocator does not rebind pointers in a sane manner.");
  575. typedef __rebind_alloc<__node_traits, __first_node> __node_base_allocator;
  576. typedef allocator_traits<__node_base_allocator> __node_base_traits;
  577. static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
  578. "Allocator does not rebind pointers in a sane manner.");
  579. private:
  580. typedef __rebind_alloc<__node_traits, __next_pointer> __pointer_allocator;
  581. typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter;
  582. typedef unique_ptr<__next_pointer[], __bucket_list_deleter> __bucket_list;
  583. typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits;
  584. typedef typename __bucket_list_deleter::pointer __node_pointer_pointer;
  585. // --- Member data begin ---
  586. __bucket_list __bucket_list_;
  587. __compressed_pair<__first_node, __node_allocator> __p1_;
  588. __compressed_pair<size_type, hasher> __p2_;
  589. __compressed_pair<float, key_equal> __p3_;
  590. // --- Member data end ---
  591. _LIBCPP_HIDE_FROM_ABI size_type& size() _NOEXCEPT { return __p2_.first(); }
  592. public:
  593. _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __p2_.first(); }
  594. _LIBCPP_HIDE_FROM_ABI hasher& hash_function() _NOEXCEPT { return __p2_.second(); }
  595. _LIBCPP_HIDE_FROM_ABI const hasher& hash_function() const _NOEXCEPT { return __p2_.second(); }
  596. _LIBCPP_HIDE_FROM_ABI float& max_load_factor() _NOEXCEPT { return __p3_.first(); }
  597. _LIBCPP_HIDE_FROM_ABI float max_load_factor() const _NOEXCEPT { return __p3_.first(); }
  598. _LIBCPP_HIDE_FROM_ABI key_equal& key_eq() _NOEXCEPT { return __p3_.second(); }
  599. _LIBCPP_HIDE_FROM_ABI const key_equal& key_eq() const _NOEXCEPT { return __p3_.second(); }
  600. _LIBCPP_HIDE_FROM_ABI __node_allocator& __node_alloc() _NOEXCEPT { return __p1_.second(); }
  601. _LIBCPP_HIDE_FROM_ABI const __node_allocator& __node_alloc() const _NOEXCEPT { return __p1_.second(); }
  602. public:
  603. typedef __hash_iterator<__node_pointer> iterator;
  604. typedef __hash_const_iterator<__node_pointer> const_iterator;
  605. typedef __hash_local_iterator<__node_pointer> local_iterator;
  606. typedef __hash_const_local_iterator<__node_pointer> const_local_iterator;
  607. _LIBCPP_HIDE_FROM_ABI __hash_table() _NOEXCEPT_(
  608. is_nothrow_default_constructible<__bucket_list>::value&& is_nothrow_default_constructible<__first_node>::value&&
  609. is_nothrow_default_constructible<__node_allocator>::value&& is_nothrow_default_constructible<hasher>::value&&
  610. is_nothrow_default_constructible<key_equal>::value);
  611. _LIBCPP_HIDE_FROM_ABI __hash_table(const hasher& __hf, const key_equal& __eql);
  612. _LIBCPP_HIDE_FROM_ABI __hash_table(const hasher& __hf, const key_equal& __eql, const allocator_type& __a);
  613. _LIBCPP_HIDE_FROM_ABI explicit __hash_table(const allocator_type& __a);
  614. _LIBCPP_HIDE_FROM_ABI __hash_table(const __hash_table& __u);
  615. _LIBCPP_HIDE_FROM_ABI __hash_table(const __hash_table& __u, const allocator_type& __a);
  616. _LIBCPP_HIDE_FROM_ABI __hash_table(__hash_table&& __u) _NOEXCEPT_(
  617. is_nothrow_move_constructible<__bucket_list>::value&& is_nothrow_move_constructible<__first_node>::value&&
  618. is_nothrow_move_constructible<__node_allocator>::value&& is_nothrow_move_constructible<hasher>::value&&
  619. is_nothrow_move_constructible<key_equal>::value);
  620. _LIBCPP_HIDE_FROM_ABI __hash_table(__hash_table&& __u, const allocator_type& __a);
  621. _LIBCPP_HIDE_FROM_ABI ~__hash_table();
  622. _LIBCPP_HIDE_FROM_ABI __hash_table& operator=(const __hash_table& __u);
  623. _LIBCPP_HIDE_FROM_ABI __hash_table& operator=(__hash_table&& __u)
  624. _NOEXCEPT_(__node_traits::propagate_on_container_move_assignment::value&&
  625. is_nothrow_move_assignable<__node_allocator>::value&& is_nothrow_move_assignable<hasher>::value&&
  626. is_nothrow_move_assignable<key_equal>::value);
  627. template <class _InputIterator>
  628. _LIBCPP_HIDE_FROM_ABI void __assign_unique(_InputIterator __first, _InputIterator __last);
  629. template <class _InputIterator>
  630. _LIBCPP_HIDE_FROM_ABI void __assign_multi(_InputIterator __first, _InputIterator __last);
  631. _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
  632. return std::min<size_type>(__node_traits::max_size(__node_alloc()), numeric_limits<difference_type >::max());
  633. }
  634. private:
  635. _LIBCPP_HIDE_FROM_ABI __next_pointer __node_insert_multi_prepare(size_t __cp_hash, value_type& __cp_val);
  636. _LIBCPP_HIDE_FROM_ABI void __node_insert_multi_perform(__node_pointer __cp, __next_pointer __pn) _NOEXCEPT;
  637. _LIBCPP_HIDE_FROM_ABI __next_pointer __node_insert_unique_prepare(size_t __nd_hash, value_type& __nd_val);
  638. _LIBCPP_HIDE_FROM_ABI void __node_insert_unique_perform(__node_pointer __ptr) _NOEXCEPT;
  639. public:
  640. _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
  641. _LIBCPP_HIDE_FROM_ABI iterator __node_insert_multi(__node_pointer __nd);
  642. _LIBCPP_HIDE_FROM_ABI iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
  643. template <class _Key, class... _Args>
  644. _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique_key_args(_Key const& __k, _Args&&... __args);
  645. template <class... _Args>
  646. _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
  647. template <class _Pp>
  648. _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique(_Pp&& __x) {
  649. return __emplace_unique_extract_key(std::forward<_Pp>(__x), __can_extract_key<_Pp, key_type>());
  650. }
  651. template <class _First,
  652. class _Second,
  653. __enable_if_t<__can_extract_map_key<_First, key_type, __container_value_type>::value, int> = 0>
  654. _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique(_First&& __f, _Second&& __s) {
  655. return __emplace_unique_key_args(__f, std::forward<_First>(__f), std::forward<_Second>(__s));
  656. }
  657. template <class... _Args>
  658. _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique(_Args&&... __args) {
  659. return __emplace_unique_impl(std::forward<_Args>(__args)...);
  660. }
  661. template <class _Pp>
  662. _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
  663. return __emplace_unique_impl(std::forward<_Pp>(__x));
  664. }
  665. template <class _Pp>
  666. _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
  667. return __emplace_unique_key_args(__x, std::forward<_Pp>(__x));
  668. }
  669. template <class _Pp>
  670. _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
  671. return __emplace_unique_key_args(__x.first, std::forward<_Pp>(__x));
  672. }
  673. template <class... _Args>
  674. _LIBCPP_HIDE_FROM_ABI iterator __emplace_multi(_Args&&... __args);
  675. template <class... _Args>
  676. _LIBCPP_HIDE_FROM_ABI iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
  677. _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __insert_unique(__container_value_type&& __x) {
  678. return __emplace_unique_key_args(_NodeTypes::__get_key(__x), std::move(__x));
  679. }
  680. template <class _Pp, __enable_if_t<!__is_same_uncvref<_Pp, __container_value_type>::value, int> = 0>
  681. _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __insert_unique(_Pp&& __x) {
  682. return __emplace_unique(std::forward<_Pp>(__x));
  683. }
  684. template <class _Pp>
  685. _LIBCPP_HIDE_FROM_ABI iterator __insert_multi(_Pp&& __x) {
  686. return __emplace_multi(std::forward<_Pp>(__x));
  687. }
  688. template <class _Pp>
  689. _LIBCPP_HIDE_FROM_ABI iterator __insert_multi(const_iterator __p, _Pp&& __x) {
  690. return __emplace_hint_multi(__p, std::forward<_Pp>(__x));
  691. }
  692. _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __insert_unique(const __container_value_type& __x) {
  693. return __emplace_unique_key_args(_NodeTypes::__get_key(__x), __x);
  694. }
  695. #if _LIBCPP_STD_VER >= 17
  696. template <class _NodeHandle, class _InsertReturnType>
  697. _LIBCPP_HIDE_FROM_ABI _InsertReturnType __node_handle_insert_unique(_NodeHandle&& __nh);
  698. template <class _NodeHandle>
  699. _LIBCPP_HIDE_FROM_ABI iterator __node_handle_insert_unique(const_iterator __hint, _NodeHandle&& __nh);
  700. template <class _Table>
  701. _LIBCPP_HIDE_FROM_ABI void __node_handle_merge_unique(_Table& __source);
  702. template <class _NodeHandle>
  703. _LIBCPP_HIDE_FROM_ABI iterator __node_handle_insert_multi(_NodeHandle&& __nh);
  704. template <class _NodeHandle>
  705. _LIBCPP_HIDE_FROM_ABI iterator __node_handle_insert_multi(const_iterator __hint, _NodeHandle&& __nh);
  706. template <class _Table>
  707. _LIBCPP_HIDE_FROM_ABI void __node_handle_merge_multi(_Table& __source);
  708. template <class _NodeHandle>
  709. _LIBCPP_HIDE_FROM_ABI _NodeHandle __node_handle_extract(key_type const& __key);
  710. template <class _NodeHandle>
  711. _LIBCPP_HIDE_FROM_ABI _NodeHandle __node_handle_extract(const_iterator __it);
  712. #endif
  713. _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT;
  714. _LIBCPP_HIDE_FROM_ABI void __rehash_unique(size_type __n) { __rehash<true>(__n); }
  715. _LIBCPP_HIDE_FROM_ABI void __rehash_multi(size_type __n) { __rehash<false>(__n); }
  716. _LIBCPP_HIDE_FROM_ABI void __reserve_unique(size_type __n) {
  717. __rehash_unique(static_cast<size_type>(std::ceil(__n / max_load_factor())));
  718. }
  719. _LIBCPP_HIDE_FROM_ABI void __reserve_multi(size_type __n) {
  720. __rehash_multi(static_cast<size_type>(std::ceil(__n / max_load_factor())));
  721. }
  722. _LIBCPP_HIDE_FROM_ABI size_type bucket_count() const _NOEXCEPT { return __bucket_list_.get_deleter().size(); }
  723. _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT;
  724. _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT;
  725. _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT;
  726. _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT;
  727. template <class _Key>
  728. _LIBCPP_HIDE_FROM_ABI size_type bucket(const _Key& __k) const {
  729. _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
  730. bucket_count() > 0, "unordered container::bucket(key) called when bucket_count() == 0");
  731. return std::__constrain_hash(hash_function()(__k), bucket_count());
  732. }
  733. template <class _Key>
  734. _LIBCPP_HIDE_FROM_ABI iterator find(const _Key& __x);
  735. template <class _Key>
  736. _LIBCPP_HIDE_FROM_ABI const_iterator find(const _Key& __x) const;
  737. typedef __hash_node_destructor<__node_allocator> _Dp;
  738. typedef unique_ptr<__node, _Dp> __node_holder;
  739. _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __p);
  740. _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __first, const_iterator __last);
  741. template <class _Key>
  742. _LIBCPP_HIDE_FROM_ABI size_type __erase_unique(const _Key& __k);
  743. template <class _Key>
  744. _LIBCPP_HIDE_FROM_ABI size_type __erase_multi(const _Key& __k);
  745. _LIBCPP_HIDE_FROM_ABI __node_holder remove(const_iterator __p) _NOEXCEPT;
  746. template <class _Key>
  747. _LIBCPP_HIDE_FROM_ABI size_type __count_unique(const _Key& __k) const;
  748. template <class _Key>
  749. _LIBCPP_HIDE_FROM_ABI size_type __count_multi(const _Key& __k) const;
  750. template <class _Key>
  751. _LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> __equal_range_unique(const _Key& __k);
  752. template <class _Key>
  753. _LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> __equal_range_unique(const _Key& __k) const;
  754. template <class _Key>
  755. _LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> __equal_range_multi(const _Key& __k);
  756. template <class _Key>
  757. _LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> __equal_range_multi(const _Key& __k) const;
  758. _LIBCPP_HIDE_FROM_ABI void swap(__hash_table& __u)
  759. #if _LIBCPP_STD_VER <= 11
  760. _NOEXCEPT_(__is_nothrow_swappable<hasher>::value&& __is_nothrow_swappable<key_equal>::value &&
  761. (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value ||
  762. __is_nothrow_swappable<__pointer_allocator>::value) &&
  763. (!__node_traits::propagate_on_container_swap::value ||
  764. __is_nothrow_swappable<__node_allocator>::value));
  765. #else
  766. _NOEXCEPT_(__is_nothrow_swappable<hasher>::value&& __is_nothrow_swappable<key_equal>::value);
  767. #endif
  768. _LIBCPP_HIDE_FROM_ABI size_type max_bucket_count() const _NOEXCEPT { return max_size(); }
  769. _LIBCPP_HIDE_FROM_ABI size_type bucket_size(size_type __n) const;
  770. _LIBCPP_HIDE_FROM_ABI float load_factor() const _NOEXCEPT {
  771. size_type __bc = bucket_count();
  772. return __bc != 0 ? (float)size() / __bc : 0.f;
  773. }
  774. _LIBCPP_HIDE_FROM_ABI void max_load_factor(float __mlf) _NOEXCEPT {
  775. // While passing a non-positive load factor is undefined behavior, in practice the result will be benign (the
  776. // call will be equivalent to `max_load_factor(load_factor())`, which is also the case for passing a valid value
  777. // less than the current `load_factor`).
  778. _LIBCPP_ASSERT_PEDANTIC(__mlf > 0, "unordered container::max_load_factor(lf) called with lf <= 0");
  779. max_load_factor() = std::max(__mlf, load_factor());
  780. }
  781. _LIBCPP_HIDE_FROM_ABI local_iterator begin(size_type __n) {
  782. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  783. __n < bucket_count(), "unordered container::begin(n) called with n >= bucket_count()");
  784. return local_iterator(__bucket_list_[__n], __n, bucket_count());
  785. }
  786. _LIBCPP_HIDE_FROM_ABI local_iterator end(size_type __n) {
  787. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  788. __n < bucket_count(), "unordered container::end(n) called with n >= bucket_count()");
  789. return local_iterator(nullptr, __n, bucket_count());
  790. }
  791. _LIBCPP_HIDE_FROM_ABI const_local_iterator cbegin(size_type __n) const {
  792. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  793. __n < bucket_count(), "unordered container::cbegin(n) called with n >= bucket_count()");
  794. return const_local_iterator(__bucket_list_[__n], __n, bucket_count());
  795. }
  796. _LIBCPP_HIDE_FROM_ABI const_local_iterator cend(size_type __n) const {
  797. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  798. __n < bucket_count(), "unordered container::cend(n) called with n >= bucket_count()");
  799. return const_local_iterator(nullptr, __n, bucket_count());
  800. }
  801. private:
  802. template <bool _UniqueKeys>
  803. _LIBCPP_HIDE_FROM_ABI void __rehash(size_type __n);
  804. template <bool _UniqueKeys>
  805. _LIBCPP_HIDE_FROM_ABI void __do_rehash(size_type __n);
  806. template <class... _Args>
  807. _LIBCPP_HIDE_FROM_ABI __node_holder __construct_node(_Args&&... __args);
  808. template <class _First, class... _Rest>
  809. _LIBCPP_HIDE_FROM_ABI __node_holder __construct_node_hash(size_t __hash, _First&& __f, _Rest&&... __rest);
  810. _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __hash_table& __u) {
  811. __copy_assign_alloc(__u, integral_constant<bool, __node_traits::propagate_on_container_copy_assignment::value>());
  812. }
  813. _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __hash_table& __u, true_type);
  814. _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __hash_table&, false_type) {}
  815. _LIBCPP_HIDE_FROM_ABI void __move_assign(__hash_table& __u, false_type);
  816. _LIBCPP_HIDE_FROM_ABI void __move_assign(__hash_table& __u, true_type)
  817. _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value&& is_nothrow_move_assignable<hasher>::value&&
  818. is_nothrow_move_assignable<key_equal>::value);
  819. _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__hash_table& __u) _NOEXCEPT_(
  820. !__node_traits::propagate_on_container_move_assignment::value ||
  821. (is_nothrow_move_assignable<__pointer_allocator>::value && is_nothrow_move_assignable<__node_allocator>::value)) {
  822. __move_assign_alloc(__u, integral_constant<bool, __node_traits::propagate_on_container_move_assignment::value>());
  823. }
  824. _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__hash_table& __u, true_type) _NOEXCEPT_(
  825. is_nothrow_move_assignable<__pointer_allocator>::value&& is_nothrow_move_assignable<__node_allocator>::value) {
  826. __bucket_list_.get_deleter().__alloc() = std::move(__u.__bucket_list_.get_deleter().__alloc());
  827. __node_alloc() = std::move(__u.__node_alloc());
  828. }
  829. _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
  830. _LIBCPP_HIDE_FROM_ABI void __deallocate_node(__next_pointer __np) _NOEXCEPT;
  831. _LIBCPP_HIDE_FROM_ABI __next_pointer __detach() _NOEXCEPT;
  832. template <class, class, class, class, class>
  833. friend class _LIBCPP_TEMPLATE_VIS unordered_map;
  834. template <class, class, class, class, class>
  835. friend class _LIBCPP_TEMPLATE_VIS unordered_multimap;
  836. };
  837. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  838. inline __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table() _NOEXCEPT_(
  839. is_nothrow_default_constructible<__bucket_list>::value&& is_nothrow_default_constructible<__first_node>::value&&
  840. is_nothrow_default_constructible<__node_allocator>::value&& is_nothrow_default_constructible<hasher>::value&&
  841. is_nothrow_default_constructible<key_equal>::value)
  842. : __p2_(0, __default_init_tag()), __p3_(1.0f, __default_init_tag()) {}
  843. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  844. inline __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf, const key_equal& __eql)
  845. : __bucket_list_(nullptr, __bucket_list_deleter()), __p1_(), __p2_(0, __hf), __p3_(1.0f, __eql) {}
  846. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  847. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(
  848. const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
  849. : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
  850. __p1_(__default_init_tag(), __node_allocator(__a)),
  851. __p2_(0, __hf),
  852. __p3_(1.0f, __eql) {}
  853. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  854. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a)
  855. : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
  856. __p1_(__default_init_tag(), __node_allocator(__a)),
  857. __p2_(0, __default_init_tag()),
  858. __p3_(1.0f, __default_init_tag()) {}
  859. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  860. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u)
  861. : __bucket_list_(nullptr,
  862. __bucket_list_deleter(allocator_traits<__pointer_allocator>::select_on_container_copy_construction(
  863. __u.__bucket_list_.get_deleter().__alloc()),
  864. 0)),
  865. __p1_(__default_init_tag(),
  866. allocator_traits<__node_allocator>::select_on_container_copy_construction(__u.__node_alloc())),
  867. __p2_(0, __u.hash_function()),
  868. __p3_(__u.__p3_) {}
  869. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  870. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u, const allocator_type& __a)
  871. : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
  872. __p1_(__default_init_tag(), __node_allocator(__a)),
  873. __p2_(0, __u.hash_function()),
  874. __p3_(__u.__p3_) {}
  875. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  876. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u) _NOEXCEPT_(
  877. is_nothrow_move_constructible<__bucket_list>::value&& is_nothrow_move_constructible<__first_node>::value&&
  878. is_nothrow_move_constructible<__node_allocator>::value&& is_nothrow_move_constructible<hasher>::value&&
  879. is_nothrow_move_constructible<key_equal>::value)
  880. : __bucket_list_(std::move(__u.__bucket_list_)),
  881. __p1_(std::move(__u.__p1_)),
  882. __p2_(std::move(__u.__p2_)),
  883. __p3_(std::move(__u.__p3_)) {
  884. if (size() > 0) {
  885. __bucket_list_[std::__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] = __p1_.first().__ptr();
  886. __u.__p1_.first().__next_ = nullptr;
  887. __u.size() = 0;
  888. }
  889. }
  890. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  891. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u, const allocator_type& __a)
  892. : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
  893. __p1_(__default_init_tag(), __node_allocator(__a)),
  894. __p2_(0, std::move(__u.hash_function())),
  895. __p3_(std::move(__u.__p3_)) {
  896. if (__a == allocator_type(__u.__node_alloc())) {
  897. __bucket_list_.reset(__u.__bucket_list_.release());
  898. __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
  899. __u.__bucket_list_.get_deleter().size() = 0;
  900. if (__u.size() > 0) {
  901. __p1_.first().__next_ = __u.__p1_.first().__next_;
  902. __u.__p1_.first().__next_ = nullptr;
  903. __bucket_list_[std::__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] = __p1_.first().__ptr();
  904. size() = __u.size();
  905. __u.size() = 0;
  906. }
  907. }
  908. }
  909. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  910. __hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table() {
  911. #if defined(_LIBCPP_CXX03_LANG)
  912. static_assert((is_copy_constructible<key_equal>::value), "Predicate must be copy-constructible.");
  913. static_assert((is_copy_constructible<hasher>::value), "Hasher must be copy-constructible.");
  914. #endif
  915. __deallocate_node(__p1_.first().__next_);
  916. }
  917. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  918. void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(const __hash_table& __u, true_type) {
  919. if (__node_alloc() != __u.__node_alloc()) {
  920. clear();
  921. __bucket_list_.reset();
  922. __bucket_list_.get_deleter().size() = 0;
  923. }
  924. __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
  925. __node_alloc() = __u.__node_alloc();
  926. }
  927. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  928. __hash_table<_Tp, _Hash, _Equal, _Alloc>& __hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u) {
  929. if (this != std::addressof(__u)) {
  930. __copy_assign_alloc(__u);
  931. hash_function() = __u.hash_function();
  932. key_eq() = __u.key_eq();
  933. max_load_factor() = __u.max_load_factor();
  934. __assign_multi(__u.begin(), __u.end());
  935. }
  936. return *this;
  937. }
  938. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  939. void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate_node(__next_pointer __np) _NOEXCEPT {
  940. __node_allocator& __na = __node_alloc();
  941. while (__np != nullptr) {
  942. __next_pointer __next = __np->__next_;
  943. __node_pointer __real_np = __np->__upcast();
  944. __node_traits::destroy(__na, _NodeTypes::__get_ptr(__real_np->__get_value()));
  945. std::__destroy_at(std::addressof(*__real_np));
  946. __node_traits::deallocate(__na, __real_np, 1);
  947. __np = __next;
  948. }
  949. }
  950. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  951. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
  952. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT {
  953. size_type __bc = bucket_count();
  954. for (size_type __i = 0; __i < __bc; ++__i)
  955. __bucket_list_[__i] = nullptr;
  956. size() = 0;
  957. __next_pointer __cache = __p1_.first().__next_;
  958. __p1_.first().__next_ = nullptr;
  959. return __cache;
  960. }
  961. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  962. void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(__hash_table& __u, true_type)
  963. _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value&& is_nothrow_move_assignable<hasher>::value&&
  964. is_nothrow_move_assignable<key_equal>::value) {
  965. clear();
  966. __bucket_list_.reset(__u.__bucket_list_.release());
  967. __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
  968. __u.__bucket_list_.get_deleter().size() = 0;
  969. __move_assign_alloc(__u);
  970. size() = __u.size();
  971. hash_function() = std::move(__u.hash_function());
  972. max_load_factor() = __u.max_load_factor();
  973. key_eq() = std::move(__u.key_eq());
  974. __p1_.first().__next_ = __u.__p1_.first().__next_;
  975. if (size() > 0) {
  976. __bucket_list_[std::__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] = __p1_.first().__ptr();
  977. __u.__p1_.first().__next_ = nullptr;
  978. __u.size() = 0;
  979. }
  980. }
  981. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  982. void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(__hash_table& __u, false_type) {
  983. if (__node_alloc() == __u.__node_alloc())
  984. __move_assign(__u, true_type());
  985. else {
  986. hash_function() = std::move(__u.hash_function());
  987. key_eq() = std::move(__u.key_eq());
  988. max_load_factor() = __u.max_load_factor();
  989. if (bucket_count() != 0) {
  990. __next_pointer __cache = __detach();
  991. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  992. try {
  993. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  994. const_iterator __i = __u.begin();
  995. while (__cache != nullptr && __u.size() != 0) {
  996. __cache->__upcast()->__get_value() = std::move(__u.remove(__i++)->__get_value());
  997. __next_pointer __next = __cache->__next_;
  998. __node_insert_multi(__cache->__upcast());
  999. __cache = __next;
  1000. }
  1001. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1002. } catch (...) {
  1003. __deallocate_node(__cache);
  1004. throw;
  1005. }
  1006. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1007. __deallocate_node(__cache);
  1008. }
  1009. const_iterator __i = __u.begin();
  1010. while (__u.size() != 0) {
  1011. __node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__get_value()));
  1012. __node_insert_multi(__h.get());
  1013. __h.release();
  1014. }
  1015. }
  1016. }
  1017. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1018. inline __hash_table<_Tp, _Hash, _Equal, _Alloc>&
  1019. __hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u) _NOEXCEPT_(
  1020. __node_traits::propagate_on_container_move_assignment::value&& is_nothrow_move_assignable<__node_allocator>::value&&
  1021. is_nothrow_move_assignable<hasher>::value&& is_nothrow_move_assignable<key_equal>::value) {
  1022. __move_assign(__u, integral_constant<bool, __node_traits::propagate_on_container_move_assignment::value>());
  1023. return *this;
  1024. }
  1025. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1026. template <class _InputIterator>
  1027. void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first, _InputIterator __last) {
  1028. typedef iterator_traits<_InputIterator> _ITraits;
  1029. typedef typename _ITraits::value_type _ItValueType;
  1030. static_assert((is_same<_ItValueType, __container_value_type>::value),
  1031. "__assign_unique may only be called with the containers value type");
  1032. if (bucket_count() != 0) {
  1033. __next_pointer __cache = __detach();
  1034. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1035. try {
  1036. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1037. for (; __cache != nullptr && __first != __last; ++__first) {
  1038. __cache->__upcast()->__get_value() = *__first;
  1039. __next_pointer __next = __cache->__next_;
  1040. __node_insert_unique(__cache->__upcast());
  1041. __cache = __next;
  1042. }
  1043. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1044. } catch (...) {
  1045. __deallocate_node(__cache);
  1046. throw;
  1047. }
  1048. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1049. __deallocate_node(__cache);
  1050. }
  1051. for (; __first != __last; ++__first)
  1052. __insert_unique(*__first);
  1053. }
  1054. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1055. template <class _InputIterator>
  1056. void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first, _InputIterator __last) {
  1057. typedef iterator_traits<_InputIterator> _ITraits;
  1058. typedef typename _ITraits::value_type _ItValueType;
  1059. static_assert(
  1060. (is_same<_ItValueType, __container_value_type>::value || is_same<_ItValueType, __node_value_type>::value),
  1061. "__assign_multi may only be called with the containers value type"
  1062. " or the nodes value type");
  1063. if (bucket_count() != 0) {
  1064. __next_pointer __cache = __detach();
  1065. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1066. try {
  1067. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1068. for (; __cache != nullptr && __first != __last; ++__first) {
  1069. __cache->__upcast()->__get_value() = *__first;
  1070. __next_pointer __next = __cache->__next_;
  1071. __node_insert_multi(__cache->__upcast());
  1072. __cache = __next;
  1073. }
  1074. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  1075. } catch (...) {
  1076. __deallocate_node(__cache);
  1077. throw;
  1078. }
  1079. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  1080. __deallocate_node(__cache);
  1081. }
  1082. for (; __first != __last; ++__first)
  1083. __insert_multi(_NodeTypes::__get_value(*__first));
  1084. }
  1085. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1086. inline typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  1087. __hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT {
  1088. return iterator(__p1_.first().__next_);
  1089. }
  1090. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1091. inline typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  1092. __hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT {
  1093. return iterator(nullptr);
  1094. }
  1095. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1096. inline typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
  1097. __hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT {
  1098. return const_iterator(__p1_.first().__next_);
  1099. }
  1100. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1101. inline typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
  1102. __hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT {
  1103. return const_iterator(nullptr);
  1104. }
  1105. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1106. void __hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT {
  1107. if (size() > 0) {
  1108. __deallocate_node(__p1_.first().__next_);
  1109. __p1_.first().__next_ = nullptr;
  1110. size_type __bc = bucket_count();
  1111. for (size_type __i = 0; __i < __bc; ++__i)
  1112. __bucket_list_[__i] = nullptr;
  1113. size() = 0;
  1114. }
  1115. }
  1116. // Prepare the container for an insertion of the value __value with the hash
  1117. // __hash. This does a lookup into the container to see if __value is already
  1118. // present, and performs a rehash if necessary. Returns a pointer to the
  1119. // existing element if it exists, otherwise nullptr.
  1120. //
  1121. // Note that this function does forward exceptions if key_eq() throws, and never
  1122. // mutates __value or actually inserts into the map.
  1123. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1124. _LIBCPP_HIDE_FROM_ABI typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
  1125. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique_prepare(size_t __hash, value_type& __value) {
  1126. size_type __bc = bucket_count();
  1127. if (__bc != 0) {
  1128. size_t __chash = std::__constrain_hash(__hash, __bc);
  1129. __next_pointer __ndptr = __bucket_list_[__chash];
  1130. if (__ndptr != nullptr) {
  1131. for (__ndptr = __ndptr->__next_;
  1132. __ndptr != nullptr &&
  1133. (__ndptr->__hash() == __hash || std::__constrain_hash(__ndptr->__hash(), __bc) == __chash);
  1134. __ndptr = __ndptr->__next_) {
  1135. if ((__ndptr->__hash() == __hash) && key_eq()(__ndptr->__upcast()->__get_value(), __value))
  1136. return __ndptr;
  1137. }
  1138. }
  1139. }
  1140. if (size() + 1 > __bc * max_load_factor() || __bc == 0) {
  1141. __rehash_unique(std::max<size_type>(
  1142. 2 * __bc + !std::__is_hash_power2(__bc), size_type(std::ceil(float(size() + 1) / max_load_factor()))));
  1143. }
  1144. return nullptr;
  1145. }
  1146. // Insert the node __nd into the container by pushing it into the right bucket,
  1147. // and updating size(). Assumes that __nd->__hash is up-to-date, and that
  1148. // rehashing has already occurred and that no element with the same key exists
  1149. // in the map.
  1150. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1151. _LIBCPP_HIDE_FROM_ABI void
  1152. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique_perform(__node_pointer __nd) _NOEXCEPT {
  1153. size_type __bc = bucket_count();
  1154. size_t __chash = std::__constrain_hash(__nd->__hash(), __bc);
  1155. // insert_after __bucket_list_[__chash], or __first_node if bucket is null
  1156. __next_pointer __pn = __bucket_list_[__chash];
  1157. if (__pn == nullptr) {
  1158. __pn = __p1_.first().__ptr();
  1159. __nd->__next_ = __pn->__next_;
  1160. __pn->__next_ = __nd->__ptr();
  1161. // fix up __bucket_list_
  1162. __bucket_list_[__chash] = __pn;
  1163. if (__nd->__next_ != nullptr)
  1164. __bucket_list_[std::__constrain_hash(__nd->__next_->__hash(), __bc)] = __nd->__ptr();
  1165. } else {
  1166. __nd->__next_ = __pn->__next_;
  1167. __pn->__next_ = __nd->__ptr();
  1168. }
  1169. ++size();
  1170. }
  1171. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1172. pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
  1173. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd) {
  1174. __nd->__hash_ = hash_function()(__nd->__get_value());
  1175. __next_pointer __existing_node = __node_insert_unique_prepare(__nd->__hash(), __nd->__get_value());
  1176. // Insert the node, unless it already exists in the container.
  1177. bool __inserted = false;
  1178. if (__existing_node == nullptr) {
  1179. __node_insert_unique_perform(__nd);
  1180. __existing_node = __nd->__ptr();
  1181. __inserted = true;
  1182. }
  1183. return pair<iterator, bool>(iterator(__existing_node), __inserted);
  1184. }
  1185. // Prepare the container for an insertion of the value __cp_val with the hash
  1186. // __cp_hash. This does a lookup into the container to see if __cp_value is
  1187. // already present, and performs a rehash if necessary. Returns a pointer to the
  1188. // last occurrence of __cp_val in the map.
  1189. //
  1190. // Note that this function does forward exceptions if key_eq() throws, and never
  1191. // mutates __value or actually inserts into the map.
  1192. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1193. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
  1194. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi_prepare(size_t __cp_hash, value_type& __cp_val) {
  1195. size_type __bc = bucket_count();
  1196. if (size() + 1 > __bc * max_load_factor() || __bc == 0) {
  1197. __rehash_multi(std::max<size_type>(
  1198. 2 * __bc + !std::__is_hash_power2(__bc), size_type(std::ceil(float(size() + 1) / max_load_factor()))));
  1199. __bc = bucket_count();
  1200. }
  1201. size_t __chash = std::__constrain_hash(__cp_hash, __bc);
  1202. __next_pointer __pn = __bucket_list_[__chash];
  1203. if (__pn != nullptr) {
  1204. for (bool __found = false;
  1205. __pn->__next_ != nullptr && std::__constrain_hash(__pn->__next_->__hash(), __bc) == __chash;
  1206. __pn = __pn->__next_) {
  1207. // __found key_eq() action
  1208. // false false loop
  1209. // true true loop
  1210. // false true set __found to true
  1211. // true false break
  1212. if (__found !=
  1213. (__pn->__next_->__hash() == __cp_hash && key_eq()(__pn->__next_->__upcast()->__get_value(), __cp_val))) {
  1214. if (!__found)
  1215. __found = true;
  1216. else
  1217. break;
  1218. }
  1219. }
  1220. }
  1221. return __pn;
  1222. }
  1223. // Insert the node __cp into the container after __pn (which is the last node in
  1224. // the bucket that compares equal to __cp). Rehashing, and checking for
  1225. // uniqueness has already been performed (in __node_insert_multi_prepare), so
  1226. // all we need to do is update the bucket and size(). Assumes that __cp->__hash
  1227. // is up-to-date.
  1228. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1229. void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi_perform(
  1230. __node_pointer __cp, __next_pointer __pn) _NOEXCEPT {
  1231. size_type __bc = bucket_count();
  1232. size_t __chash = std::__constrain_hash(__cp->__hash_, __bc);
  1233. if (__pn == nullptr) {
  1234. __pn = __p1_.first().__ptr();
  1235. __cp->__next_ = __pn->__next_;
  1236. __pn->__next_ = __cp->__ptr();
  1237. // fix up __bucket_list_
  1238. __bucket_list_[__chash] = __pn;
  1239. if (__cp->__next_ != nullptr)
  1240. __bucket_list_[std::__constrain_hash(__cp->__next_->__hash(), __bc)] = __cp->__ptr();
  1241. } else {
  1242. __cp->__next_ = __pn->__next_;
  1243. __pn->__next_ = __cp->__ptr();
  1244. if (__cp->__next_ != nullptr) {
  1245. size_t __nhash = std::__constrain_hash(__cp->__next_->__hash(), __bc);
  1246. if (__nhash != __chash)
  1247. __bucket_list_[__nhash] = __cp->__ptr();
  1248. }
  1249. }
  1250. ++size();
  1251. }
  1252. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1253. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  1254. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp) {
  1255. __cp->__hash_ = hash_function()(__cp->__get_value());
  1256. __next_pointer __pn = __node_insert_multi_prepare(__cp->__hash(), __cp->__get_value());
  1257. __node_insert_multi_perform(__cp, __pn);
  1258. return iterator(__cp->__ptr());
  1259. }
  1260. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1261. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  1262. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(const_iterator __p, __node_pointer __cp) {
  1263. if (__p != end() && key_eq()(*__p, __cp->__get_value())) {
  1264. __next_pointer __np = __p.__node_;
  1265. __cp->__hash_ = __np->__hash();
  1266. size_type __bc = bucket_count();
  1267. if (size() + 1 > __bc * max_load_factor() || __bc == 0) {
  1268. __rehash_multi(std::max<size_type>(
  1269. 2 * __bc + !std::__is_hash_power2(__bc), size_type(std::ceil(float(size() + 1) / max_load_factor()))));
  1270. __bc = bucket_count();
  1271. }
  1272. size_t __chash = std::__constrain_hash(__cp->__hash_, __bc);
  1273. __next_pointer __pp = __bucket_list_[__chash];
  1274. while (__pp->__next_ != __np)
  1275. __pp = __pp->__next_;
  1276. __cp->__next_ = __np;
  1277. __pp->__next_ = static_cast<__next_pointer>(__cp);
  1278. ++size();
  1279. return iterator(static_cast<__next_pointer>(__cp));
  1280. }
  1281. return __node_insert_multi(__cp);
  1282. }
  1283. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1284. template <class _Key, class... _Args>
  1285. pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
  1286. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args) {
  1287. size_t __hash = hash_function()(__k);
  1288. size_type __bc = bucket_count();
  1289. bool __inserted = false;
  1290. __next_pointer __nd;
  1291. size_t __chash;
  1292. if (__bc != 0) {
  1293. __chash = std::__constrain_hash(__hash, __bc);
  1294. __nd = __bucket_list_[__chash];
  1295. if (__nd != nullptr) {
  1296. for (__nd = __nd->__next_;
  1297. __nd != nullptr && (__nd->__hash() == __hash || std::__constrain_hash(__nd->__hash(), __bc) == __chash);
  1298. __nd = __nd->__next_) {
  1299. if ((__nd->__hash() == __hash) && key_eq()(__nd->__upcast()->__get_value(), __k))
  1300. goto __done;
  1301. }
  1302. }
  1303. }
  1304. {
  1305. __node_holder __h = __construct_node_hash(__hash, std::forward<_Args>(__args)...);
  1306. if (size() + 1 > __bc * max_load_factor() || __bc == 0) {
  1307. __rehash_unique(std::max<size_type>(
  1308. 2 * __bc + !std::__is_hash_power2(__bc), size_type(std::ceil(float(size() + 1) / max_load_factor()))));
  1309. __bc = bucket_count();
  1310. __chash = std::__constrain_hash(__hash, __bc);
  1311. }
  1312. // insert_after __bucket_list_[__chash], or __first_node if bucket is null
  1313. __next_pointer __pn = __bucket_list_[__chash];
  1314. if (__pn == nullptr) {
  1315. __pn = __p1_.first().__ptr();
  1316. __h->__next_ = __pn->__next_;
  1317. __pn->__next_ = __h.get()->__ptr();
  1318. // fix up __bucket_list_
  1319. __bucket_list_[__chash] = __pn;
  1320. if (__h->__next_ != nullptr)
  1321. __bucket_list_[std::__constrain_hash(__h->__next_->__hash(), __bc)] = __h.get()->__ptr();
  1322. } else {
  1323. __h->__next_ = __pn->__next_;
  1324. __pn->__next_ = static_cast<__next_pointer>(__h.get());
  1325. }
  1326. __nd = static_cast<__next_pointer>(__h.release());
  1327. // increment size
  1328. ++size();
  1329. __inserted = true;
  1330. }
  1331. __done:
  1332. return pair<iterator, bool>(iterator(__nd), __inserted);
  1333. }
  1334. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1335. template <class... _Args>
  1336. pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
  1337. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_impl(_Args&&... __args) {
  1338. __node_holder __h = __construct_node(std::forward<_Args>(__args)...);
  1339. pair<iterator, bool> __r = __node_insert_unique(__h.get());
  1340. if (__r.second)
  1341. __h.release();
  1342. return __r;
  1343. }
  1344. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1345. template <class... _Args>
  1346. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  1347. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args) {
  1348. __node_holder __h = __construct_node(std::forward<_Args>(__args)...);
  1349. iterator __r = __node_insert_multi(__h.get());
  1350. __h.release();
  1351. return __r;
  1352. }
  1353. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1354. template <class... _Args>
  1355. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  1356. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(const_iterator __p, _Args&&... __args) {
  1357. __node_holder __h = __construct_node(std::forward<_Args>(__args)...);
  1358. iterator __r = __node_insert_multi(__p, __h.get());
  1359. __h.release();
  1360. return __r;
  1361. }
  1362. #if _LIBCPP_STD_VER >= 17
  1363. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1364. template <class _NodeHandle, class _InsertReturnType>
  1365. _LIBCPP_HIDE_FROM_ABI _InsertReturnType
  1366. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_unique(_NodeHandle&& __nh) {
  1367. if (__nh.empty())
  1368. return _InsertReturnType{end(), false, _NodeHandle()};
  1369. pair<iterator, bool> __result = __node_insert_unique(__nh.__ptr_);
  1370. if (__result.second)
  1371. __nh.__release_ptr();
  1372. return _InsertReturnType{__result.first, __result.second, std::move(__nh)};
  1373. }
  1374. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1375. template <class _NodeHandle>
  1376. _LIBCPP_HIDE_FROM_ABI typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  1377. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_unique(const_iterator, _NodeHandle&& __nh) {
  1378. if (__nh.empty())
  1379. return end();
  1380. pair<iterator, bool> __result = __node_insert_unique(__nh.__ptr_);
  1381. if (__result.second)
  1382. __nh.__release_ptr();
  1383. return __result.first;
  1384. }
  1385. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1386. template <class _NodeHandle>
  1387. _LIBCPP_HIDE_FROM_ABI _NodeHandle
  1388. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_extract(key_type const& __key) {
  1389. iterator __i = find(__key);
  1390. if (__i == end())
  1391. return _NodeHandle();
  1392. return __node_handle_extract<_NodeHandle>(__i);
  1393. }
  1394. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1395. template <class _NodeHandle>
  1396. _LIBCPP_HIDE_FROM_ABI _NodeHandle __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_extract(const_iterator __p) {
  1397. allocator_type __alloc(__node_alloc());
  1398. return _NodeHandle(remove(__p).release(), __alloc);
  1399. }
  1400. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1401. template <class _Table>
  1402. _LIBCPP_HIDE_FROM_ABI void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_merge_unique(_Table& __source) {
  1403. static_assert(is_same<__node, typename _Table::__node>::value, "");
  1404. for (typename _Table::iterator __it = __source.begin(); __it != __source.end();) {
  1405. __node_pointer __src_ptr = __it.__node_->__upcast();
  1406. size_t __hash = hash_function()(__src_ptr->__get_value());
  1407. __next_pointer __existing_node = __node_insert_unique_prepare(__hash, __src_ptr->__get_value());
  1408. auto __prev_iter = __it++;
  1409. if (__existing_node == nullptr) {
  1410. (void)__source.remove(__prev_iter).release();
  1411. __src_ptr->__hash_ = __hash;
  1412. __node_insert_unique_perform(__src_ptr);
  1413. }
  1414. }
  1415. }
  1416. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1417. template <class _NodeHandle>
  1418. _LIBCPP_HIDE_FROM_ABI typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  1419. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_multi(_NodeHandle&& __nh) {
  1420. if (__nh.empty())
  1421. return end();
  1422. iterator __result = __node_insert_multi(__nh.__ptr_);
  1423. __nh.__release_ptr();
  1424. return __result;
  1425. }
  1426. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1427. template <class _NodeHandle>
  1428. _LIBCPP_HIDE_FROM_ABI typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  1429. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_multi(const_iterator __hint, _NodeHandle&& __nh) {
  1430. if (__nh.empty())
  1431. return end();
  1432. iterator __result = __node_insert_multi(__hint, __nh.__ptr_);
  1433. __nh.__release_ptr();
  1434. return __result;
  1435. }
  1436. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1437. template <class _Table>
  1438. _LIBCPP_HIDE_FROM_ABI void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_merge_multi(_Table& __source) {
  1439. static_assert(is_same<typename _Table::__node, __node>::value, "");
  1440. for (typename _Table::iterator __it = __source.begin(); __it != __source.end();) {
  1441. __node_pointer __src_ptr = __it.__node_->__upcast();
  1442. size_t __src_hash = hash_function()(__src_ptr->__get_value());
  1443. __next_pointer __pn = __node_insert_multi_prepare(__src_hash, __src_ptr->__get_value());
  1444. (void)__source.remove(__it++).release();
  1445. __src_ptr->__hash_ = __src_hash;
  1446. __node_insert_multi_perform(__src_ptr, __pn);
  1447. }
  1448. }
  1449. #endif // _LIBCPP_STD_VER >= 17
  1450. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1451. template <bool _UniqueKeys>
  1452. void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __n) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK {
  1453. if (__n == 1)
  1454. __n = 2;
  1455. else if (__n & (__n - 1))
  1456. __n = std::__next_prime(__n);
  1457. size_type __bc = bucket_count();
  1458. if (__n > __bc)
  1459. __do_rehash<_UniqueKeys>(__n);
  1460. else if (__n < __bc) {
  1461. __n = std::max<size_type>(
  1462. __n,
  1463. std::__is_hash_power2(__bc) ? std::__next_hash_pow2(size_t(std::ceil(float(size()) / max_load_factor())))
  1464. : std::__next_prime(size_t(std::ceil(float(size()) / max_load_factor()))));
  1465. if (__n < __bc)
  1466. __do_rehash<_UniqueKeys>(__n);
  1467. }
  1468. }
  1469. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1470. template <bool _UniqueKeys>
  1471. void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__do_rehash(size_type __nbc) {
  1472. __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
  1473. __bucket_list_.reset(__nbc > 0 ? __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
  1474. __bucket_list_.get_deleter().size() = __nbc;
  1475. if (__nbc > 0) {
  1476. for (size_type __i = 0; __i < __nbc; ++__i)
  1477. __bucket_list_[__i] = nullptr;
  1478. __next_pointer __pp = __p1_.first().__ptr();
  1479. __next_pointer __cp = __pp->__next_;
  1480. if (__cp != nullptr) {
  1481. size_type __chash = std::__constrain_hash(__cp->__hash(), __nbc);
  1482. __bucket_list_[__chash] = __pp;
  1483. size_type __phash = __chash;
  1484. for (__pp = __cp, void(), __cp = __cp->__next_; __cp != nullptr; __cp = __pp->__next_) {
  1485. __chash = std::__constrain_hash(__cp->__hash(), __nbc);
  1486. if (__chash == __phash)
  1487. __pp = __cp;
  1488. else {
  1489. if (__bucket_list_[__chash] == nullptr) {
  1490. __bucket_list_[__chash] = __pp;
  1491. __pp = __cp;
  1492. __phash = __chash;
  1493. } else {
  1494. __next_pointer __np = __cp;
  1495. if _LIBCPP_CONSTEXPR_SINCE_CXX17 (!_UniqueKeys) {
  1496. for (; __np->__next_ != nullptr &&
  1497. key_eq()(__cp->__upcast()->__get_value(), __np->__next_->__upcast()->__get_value());
  1498. __np = __np->__next_)
  1499. ;
  1500. }
  1501. __pp->__next_ = __np->__next_;
  1502. __np->__next_ = __bucket_list_[__chash]->__next_;
  1503. __bucket_list_[__chash]->__next_ = __cp;
  1504. }
  1505. }
  1506. }
  1507. }
  1508. }
  1509. }
  1510. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1511. template <class _Key>
  1512. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  1513. __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) {
  1514. size_t __hash = hash_function()(__k);
  1515. size_type __bc = bucket_count();
  1516. if (__bc != 0) {
  1517. size_t __chash = std::__constrain_hash(__hash, __bc);
  1518. __next_pointer __nd = __bucket_list_[__chash];
  1519. if (__nd != nullptr) {
  1520. for (__nd = __nd->__next_;
  1521. __nd != nullptr && (__nd->__hash() == __hash || std::__constrain_hash(__nd->__hash(), __bc) == __chash);
  1522. __nd = __nd->__next_) {
  1523. if ((__nd->__hash() == __hash) && key_eq()(__nd->__upcast()->__get_value(), __k))
  1524. return iterator(__nd);
  1525. }
  1526. }
  1527. }
  1528. return end();
  1529. }
  1530. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1531. template <class _Key>
  1532. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
  1533. __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const {
  1534. size_t __hash = hash_function()(__k);
  1535. size_type __bc = bucket_count();
  1536. if (__bc != 0) {
  1537. size_t __chash = std::__constrain_hash(__hash, __bc);
  1538. __next_pointer __nd = __bucket_list_[__chash];
  1539. if (__nd != nullptr) {
  1540. for (__nd = __nd->__next_;
  1541. __nd != nullptr && (__hash == __nd->__hash() || std::__constrain_hash(__nd->__hash(), __bc) == __chash);
  1542. __nd = __nd->__next_) {
  1543. if ((__nd->__hash() == __hash) && key_eq()(__nd->__upcast()->__get_value(), __k))
  1544. return const_iterator(__nd);
  1545. }
  1546. }
  1547. }
  1548. return end();
  1549. }
  1550. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1551. template <class... _Args>
  1552. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
  1553. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&&... __args) {
  1554. static_assert(!__is_hash_value_type<_Args...>::value, "Construct cannot be called with a hash value type");
  1555. __node_allocator& __na = __node_alloc();
  1556. __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
  1557. // Begin the lifetime of the node itself. Note that this doesn't begin the lifetime of the value
  1558. // held inside the node, since we need to use the allocator's construct() method for that.
  1559. //
  1560. // We don't use the allocator's construct() method to construct the node itself since the
  1561. // Cpp17FooInsertable named requirements don't require the allocator's construct() method
  1562. // to work on anything other than the value_type.
  1563. std::__construct_at(std::addressof(*__h), /* next = */ nullptr, /* hash = */ 0);
  1564. // Now construct the value_type using the allocator's construct() method.
  1565. __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__get_value()), std::forward<_Args>(__args)...);
  1566. __h.get_deleter().__value_constructed = true;
  1567. __h->__hash_ = hash_function()(__h->__get_value());
  1568. return __h;
  1569. }
  1570. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1571. template <class _First, class... _Rest>
  1572. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
  1573. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(size_t __hash, _First&& __f, _Rest&&... __rest) {
  1574. static_assert(!__is_hash_value_type<_First, _Rest...>::value, "Construct cannot be called with a hash value type");
  1575. __node_allocator& __na = __node_alloc();
  1576. __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
  1577. std::__construct_at(std::addressof(*__h), /* next = */ nullptr, /* hash = */ __hash);
  1578. __node_traits::construct(
  1579. __na, _NodeTypes::__get_ptr(__h->__get_value()), std::forward<_First>(__f), std::forward<_Rest>(__rest)...);
  1580. __h.get_deleter().__value_constructed = true;
  1581. return __h;
  1582. }
  1583. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1584. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  1585. __hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p) {
  1586. __next_pointer __np = __p.__node_;
  1587. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  1588. __p != end(), "unordered container::erase(iterator) called with a non-dereferenceable iterator");
  1589. iterator __r(__np);
  1590. ++__r;
  1591. remove(__p);
  1592. return __r;
  1593. }
  1594. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1595. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  1596. __hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first, const_iterator __last) {
  1597. for (const_iterator __p = __first; __first != __last; __p = __first) {
  1598. ++__first;
  1599. erase(__p);
  1600. }
  1601. __next_pointer __np = __last.__node_;
  1602. return iterator(__np);
  1603. }
  1604. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1605. template <class _Key>
  1606. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
  1607. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k) {
  1608. iterator __i = find(__k);
  1609. if (__i == end())
  1610. return 0;
  1611. erase(__i);
  1612. return 1;
  1613. }
  1614. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1615. template <class _Key>
  1616. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
  1617. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k) {
  1618. size_type __r = 0;
  1619. iterator __i = find(__k);
  1620. if (__i != end()) {
  1621. iterator __e = end();
  1622. do {
  1623. erase(__i++);
  1624. ++__r;
  1625. } while (__i != __e && key_eq()(*__i, __k));
  1626. }
  1627. return __r;
  1628. }
  1629. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1630. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
  1631. __hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT {
  1632. // current node
  1633. __next_pointer __cn = __p.__node_;
  1634. size_type __bc = bucket_count();
  1635. size_t __chash = std::__constrain_hash(__cn->__hash(), __bc);
  1636. // find previous node
  1637. __next_pointer __pn = __bucket_list_[__chash];
  1638. for (; __pn->__next_ != __cn; __pn = __pn->__next_)
  1639. ;
  1640. // Fix up __bucket_list_
  1641. // if __pn is not in same bucket (before begin is not in same bucket) &&
  1642. // if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
  1643. if (__pn == __p1_.first().__ptr() || std::__constrain_hash(__pn->__hash(), __bc) != __chash) {
  1644. if (__cn->__next_ == nullptr || std::__constrain_hash(__cn->__next_->__hash(), __bc) != __chash)
  1645. __bucket_list_[__chash] = nullptr;
  1646. }
  1647. // if __cn->__next_ is not in same bucket (nullptr is in same bucket)
  1648. if (__cn->__next_ != nullptr) {
  1649. size_t __nhash = std::__constrain_hash(__cn->__next_->__hash(), __bc);
  1650. if (__nhash != __chash)
  1651. __bucket_list_[__nhash] = __pn;
  1652. }
  1653. // remove __cn
  1654. __pn->__next_ = __cn->__next_;
  1655. __cn->__next_ = nullptr;
  1656. --size();
  1657. return __node_holder(__cn->__upcast(), _Dp(__node_alloc(), true));
  1658. }
  1659. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1660. template <class _Key>
  1661. inline typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
  1662. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const {
  1663. return static_cast<size_type>(find(__k) != end());
  1664. }
  1665. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1666. template <class _Key>
  1667. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
  1668. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const {
  1669. size_type __r = 0;
  1670. const_iterator __i = find(__k);
  1671. if (__i != end()) {
  1672. const_iterator __e = end();
  1673. do {
  1674. ++__i;
  1675. ++__r;
  1676. } while (__i != __e && key_eq()(*__i, __k));
  1677. }
  1678. return __r;
  1679. }
  1680. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1681. template <class _Key>
  1682. pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
  1683. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
  1684. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(const _Key& __k) {
  1685. iterator __i = find(__k);
  1686. iterator __j = __i;
  1687. if (__i != end())
  1688. ++__j;
  1689. return pair<iterator, iterator>(__i, __j);
  1690. }
  1691. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1692. template <class _Key>
  1693. pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
  1694. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
  1695. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(const _Key& __k) const {
  1696. const_iterator __i = find(__k);
  1697. const_iterator __j = __i;
  1698. if (__i != end())
  1699. ++__j;
  1700. return pair<const_iterator, const_iterator>(__i, __j);
  1701. }
  1702. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1703. template <class _Key>
  1704. pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
  1705. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
  1706. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(const _Key& __k) {
  1707. iterator __i = find(__k);
  1708. iterator __j = __i;
  1709. if (__i != end()) {
  1710. iterator __e = end();
  1711. do {
  1712. ++__j;
  1713. } while (__j != __e && key_eq()(*__j, __k));
  1714. }
  1715. return pair<iterator, iterator>(__i, __j);
  1716. }
  1717. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1718. template <class _Key>
  1719. pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
  1720. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
  1721. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(const _Key& __k) const {
  1722. const_iterator __i = find(__k);
  1723. const_iterator __j = __i;
  1724. if (__i != end()) {
  1725. const_iterator __e = end();
  1726. do {
  1727. ++__j;
  1728. } while (__j != __e && key_eq()(*__j, __k));
  1729. }
  1730. return pair<const_iterator, const_iterator>(__i, __j);
  1731. }
  1732. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1733. void __hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
  1734. #if _LIBCPP_STD_VER <= 11
  1735. _NOEXCEPT_(__is_nothrow_swappable<hasher>::value&& __is_nothrow_swappable<key_equal>::value &&
  1736. (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value ||
  1737. __is_nothrow_swappable<__pointer_allocator>::value) &&
  1738. (!__node_traits::propagate_on_container_swap::value || __is_nothrow_swappable<__node_allocator>::value))
  1739. #else
  1740. _NOEXCEPT_(__is_nothrow_swappable<hasher>::value&& __is_nothrow_swappable<key_equal>::value)
  1741. #endif
  1742. {
  1743. _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
  1744. __node_traits::propagate_on_container_swap::value || this->__node_alloc() == __u.__node_alloc(),
  1745. "unordered container::swap: Either propagate_on_container_swap "
  1746. "must be true or the allocators must compare equal");
  1747. {
  1748. __node_pointer_pointer __npp = __bucket_list_.release();
  1749. __bucket_list_.reset(__u.__bucket_list_.release());
  1750. __u.__bucket_list_.reset(__npp);
  1751. }
  1752. std::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
  1753. std::__swap_allocator(__bucket_list_.get_deleter().__alloc(), __u.__bucket_list_.get_deleter().__alloc());
  1754. std::__swap_allocator(__node_alloc(), __u.__node_alloc());
  1755. std::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
  1756. __p2_.swap(__u.__p2_);
  1757. __p3_.swap(__u.__p3_);
  1758. if (size() > 0)
  1759. __bucket_list_[std::__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] = __p1_.first().__ptr();
  1760. if (__u.size() > 0)
  1761. __u.__bucket_list_[std::__constrain_hash(__u.__p1_.first().__next_->__hash(), __u.bucket_count())] =
  1762. __u.__p1_.first().__ptr();
  1763. }
  1764. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1765. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
  1766. __hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const {
  1767. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  1768. __n < bucket_count(), "unordered container::bucket_size(n) called with n >= bucket_count()");
  1769. __next_pointer __np = __bucket_list_[__n];
  1770. size_type __bc = bucket_count();
  1771. size_type __r = 0;
  1772. if (__np != nullptr) {
  1773. for (__np = __np->__next_; __np != nullptr && std::__constrain_hash(__np->__hash(), __bc) == __n;
  1774. __np = __np->__next_, (void)++__r)
  1775. ;
  1776. }
  1777. return __r;
  1778. }
  1779. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1780. inline _LIBCPP_HIDE_FROM_ABI void
  1781. swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x, __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
  1782. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
  1783. __x.swap(__y);
  1784. }
  1785. _LIBCPP_END_NAMESPACE_STD
  1786. _LIBCPP_POP_MACROS
  1787. #endif // _LIBCPP___HASH_TABLE