__hash_table 97 KB

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