__hash_table 83 KB

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