__hash_table 102 KB

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