__hash_table 91 KB

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