__hash_table 99 KB

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