__hash_table 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809
  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 <cmath>
  19. #include <initializer_list>
  20. #include <iterator>
  21. #include <memory>
  22. #include <type_traits>
  23. #include <utility>
  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. __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. __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. __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. __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. __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. __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. __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. __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. #if _LIBCPP_DEBUG_LEVEL == 2
  1321. __get_db()->__erase_c(this);
  1322. #endif
  1323. }
  1324. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1325. void
  1326. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(
  1327. const __hash_table& __u, true_type)
  1328. {
  1329. if (__node_alloc() != __u.__node_alloc())
  1330. {
  1331. clear();
  1332. __bucket_list_.reset();
  1333. __bucket_list_.get_deleter().size() = 0;
  1334. }
  1335. __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
  1336. __node_alloc() = __u.__node_alloc();
  1337. }
  1338. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1339. __hash_table<_Tp, _Hash, _Equal, _Alloc>&
  1340. __hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u)
  1341. {
  1342. if (this != _VSTD::addressof(__u))
  1343. {
  1344. __copy_assign_alloc(__u);
  1345. hash_function() = __u.hash_function();
  1346. key_eq() = __u.key_eq();
  1347. max_load_factor() = __u.max_load_factor();
  1348. __assign_multi(__u.begin(), __u.end());
  1349. }
  1350. return *this;
  1351. }
  1352. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1353. void
  1354. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate_node(__next_pointer __np)
  1355. _NOEXCEPT
  1356. {
  1357. __node_allocator& __na = __node_alloc();
  1358. while (__np != nullptr)
  1359. {
  1360. __next_pointer __next = __np->__next_;
  1361. #if _LIBCPP_DEBUG_LEVEL == 2
  1362. __c_node* __c = __get_db()->__find_c_and_lock(this);
  1363. for (__i_node** __p = __c->end_; __p != __c->beg_; )
  1364. {
  1365. --__p;
  1366. iterator* __i = static_cast<iterator*>((*__p)->__i_);
  1367. if (__i->__node_ == __np)
  1368. {
  1369. (*__p)->__c_ = nullptr;
  1370. if (--__c->end_ != __p)
  1371. _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
  1372. }
  1373. }
  1374. __get_db()->unlock();
  1375. #endif
  1376. __node_pointer __real_np = __np->__upcast();
  1377. __node_traits::destroy(__na, _NodeTypes::__get_ptr(__real_np->__value_));
  1378. __node_traits::deallocate(__na, __real_np, 1);
  1379. __np = __next;
  1380. }
  1381. }
  1382. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1383. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
  1384. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
  1385. {
  1386. size_type __bc = bucket_count();
  1387. for (size_type __i = 0; __i < __bc; ++__i)
  1388. __bucket_list_[__i] = nullptr;
  1389. size() = 0;
  1390. __next_pointer __cache = __p1_.first().__next_;
  1391. __p1_.first().__next_ = nullptr;
  1392. return __cache;
  1393. }
  1394. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1395. void
  1396. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
  1397. __hash_table& __u, true_type)
  1398. _NOEXCEPT_(
  1399. is_nothrow_move_assignable<__node_allocator>::value &&
  1400. is_nothrow_move_assignable<hasher>::value &&
  1401. is_nothrow_move_assignable<key_equal>::value)
  1402. {
  1403. clear();
  1404. __bucket_list_.reset(__u.__bucket_list_.release());
  1405. __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
  1406. __u.__bucket_list_.get_deleter().size() = 0;
  1407. __move_assign_alloc(__u);
  1408. size() = __u.size();
  1409. hash_function() = _VSTD::move(__u.hash_function());
  1410. max_load_factor() = __u.max_load_factor();
  1411. key_eq() = _VSTD::move(__u.key_eq());
  1412. __p1_.first().__next_ = __u.__p1_.first().__next_;
  1413. if (size() > 0)
  1414. {
  1415. __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
  1416. __p1_.first().__ptr();
  1417. __u.__p1_.first().__next_ = nullptr;
  1418. __u.size() = 0;
  1419. }
  1420. #if _LIBCPP_DEBUG_LEVEL == 2
  1421. __get_db()->swap(this, _VSTD::addressof(__u));
  1422. #endif
  1423. }
  1424. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1425. void
  1426. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
  1427. __hash_table& __u, false_type)
  1428. {
  1429. if (__node_alloc() == __u.__node_alloc())
  1430. __move_assign(__u, true_type());
  1431. else
  1432. {
  1433. hash_function() = _VSTD::move(__u.hash_function());
  1434. key_eq() = _VSTD::move(__u.key_eq());
  1435. max_load_factor() = __u.max_load_factor();
  1436. if (bucket_count() != 0)
  1437. {
  1438. __next_pointer __cache = __detach();
  1439. #ifndef _LIBCPP_NO_EXCEPTIONS
  1440. try
  1441. {
  1442. #endif // _LIBCPP_NO_EXCEPTIONS
  1443. const_iterator __i = __u.begin();
  1444. while (__cache != nullptr && __u.size() != 0)
  1445. {
  1446. __cache->__upcast()->__value_ =
  1447. _VSTD::move(__u.remove(__i++)->__value_);
  1448. __next_pointer __next = __cache->__next_;
  1449. __node_insert_multi(__cache->__upcast());
  1450. __cache = __next;
  1451. }
  1452. #ifndef _LIBCPP_NO_EXCEPTIONS
  1453. }
  1454. catch (...)
  1455. {
  1456. __deallocate_node(__cache);
  1457. throw;
  1458. }
  1459. #endif // _LIBCPP_NO_EXCEPTIONS
  1460. __deallocate_node(__cache);
  1461. }
  1462. const_iterator __i = __u.begin();
  1463. while (__u.size() != 0)
  1464. {
  1465. __node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__value_));
  1466. __node_insert_multi(__h.get());
  1467. __h.release();
  1468. }
  1469. }
  1470. }
  1471. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1472. inline
  1473. __hash_table<_Tp, _Hash, _Equal, _Alloc>&
  1474. __hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
  1475. _NOEXCEPT_(
  1476. __node_traits::propagate_on_container_move_assignment::value &&
  1477. is_nothrow_move_assignable<__node_allocator>::value &&
  1478. is_nothrow_move_assignable<hasher>::value &&
  1479. is_nothrow_move_assignable<key_equal>::value)
  1480. {
  1481. __move_assign(__u, integral_constant<bool,
  1482. __node_traits::propagate_on_container_move_assignment::value>());
  1483. return *this;
  1484. }
  1485. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1486. template <class _InputIterator>
  1487. void
  1488. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first,
  1489. _InputIterator __last)
  1490. {
  1491. typedef iterator_traits<_InputIterator> _ITraits;
  1492. typedef typename _ITraits::value_type _ItValueType;
  1493. static_assert((is_same<_ItValueType, __container_value_type>::value),
  1494. "__assign_unique may only be called with the containers value type");
  1495. if (bucket_count() != 0)
  1496. {
  1497. __next_pointer __cache = __detach();
  1498. #ifndef _LIBCPP_NO_EXCEPTIONS
  1499. try
  1500. {
  1501. #endif // _LIBCPP_NO_EXCEPTIONS
  1502. for (; __cache != nullptr && __first != __last; ++__first)
  1503. {
  1504. __cache->__upcast()->__value_ = *__first;
  1505. __next_pointer __next = __cache->__next_;
  1506. __node_insert_unique(__cache->__upcast());
  1507. __cache = __next;
  1508. }
  1509. #ifndef _LIBCPP_NO_EXCEPTIONS
  1510. }
  1511. catch (...)
  1512. {
  1513. __deallocate_node(__cache);
  1514. throw;
  1515. }
  1516. #endif // _LIBCPP_NO_EXCEPTIONS
  1517. __deallocate_node(__cache);
  1518. }
  1519. for (; __first != __last; ++__first)
  1520. __insert_unique(*__first);
  1521. }
  1522. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1523. template <class _InputIterator>
  1524. void
  1525. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
  1526. _InputIterator __last)
  1527. {
  1528. typedef iterator_traits<_InputIterator> _ITraits;
  1529. typedef typename _ITraits::value_type _ItValueType;
  1530. static_assert((is_same<_ItValueType, __container_value_type>::value ||
  1531. is_same<_ItValueType, __node_value_type>::value),
  1532. "__assign_multi may only be called with the containers value type"
  1533. " or the nodes value type");
  1534. if (bucket_count() != 0)
  1535. {
  1536. __next_pointer __cache = __detach();
  1537. #ifndef _LIBCPP_NO_EXCEPTIONS
  1538. try
  1539. {
  1540. #endif // _LIBCPP_NO_EXCEPTIONS
  1541. for (; __cache != nullptr && __first != __last; ++__first)
  1542. {
  1543. __cache->__upcast()->__value_ = *__first;
  1544. __next_pointer __next = __cache->__next_;
  1545. __node_insert_multi(__cache->__upcast());
  1546. __cache = __next;
  1547. }
  1548. #ifndef _LIBCPP_NO_EXCEPTIONS
  1549. }
  1550. catch (...)
  1551. {
  1552. __deallocate_node(__cache);
  1553. throw;
  1554. }
  1555. #endif // _LIBCPP_NO_EXCEPTIONS
  1556. __deallocate_node(__cache);
  1557. }
  1558. for (; __first != __last; ++__first)
  1559. __insert_multi(_NodeTypes::__get_value(*__first));
  1560. }
  1561. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1562. inline
  1563. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  1564. __hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT
  1565. {
  1566. #if _LIBCPP_DEBUG_LEVEL == 2
  1567. return iterator(__p1_.first().__next_, this);
  1568. #else
  1569. return iterator(__p1_.first().__next_);
  1570. #endif
  1571. }
  1572. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1573. inline
  1574. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  1575. __hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT
  1576. {
  1577. #if _LIBCPP_DEBUG_LEVEL == 2
  1578. return iterator(nullptr, this);
  1579. #else
  1580. return iterator(nullptr);
  1581. #endif
  1582. }
  1583. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1584. inline
  1585. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
  1586. __hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT
  1587. {
  1588. #if _LIBCPP_DEBUG_LEVEL == 2
  1589. return const_iterator(__p1_.first().__next_, this);
  1590. #else
  1591. return const_iterator(__p1_.first().__next_);
  1592. #endif
  1593. }
  1594. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1595. inline
  1596. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
  1597. __hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT
  1598. {
  1599. #if _LIBCPP_DEBUG_LEVEL == 2
  1600. return const_iterator(nullptr, this);
  1601. #else
  1602. return const_iterator(nullptr);
  1603. #endif
  1604. }
  1605. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1606. void
  1607. __hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT
  1608. {
  1609. if (size() > 0)
  1610. {
  1611. __deallocate_node(__p1_.first().__next_);
  1612. __p1_.first().__next_ = nullptr;
  1613. size_type __bc = bucket_count();
  1614. for (size_type __i = 0; __i < __bc; ++__i)
  1615. __bucket_list_[__i] = nullptr;
  1616. size() = 0;
  1617. }
  1618. }
  1619. // Prepare the container for an insertion of the value __value with the hash
  1620. // __hash. This does a lookup into the container to see if __value is already
  1621. // present, and performs a rehash if necessary. Returns a pointer to the
  1622. // existing element if it exists, otherwise nullptr.
  1623. //
  1624. // Note that this function does forward exceptions if key_eq() throws, and never
  1625. // mutates __value or actually inserts into the map.
  1626. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1627. _LIBCPP_INLINE_VISIBILITY
  1628. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
  1629. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique_prepare(
  1630. size_t __hash, value_type& __value)
  1631. {
  1632. size_type __bc = bucket_count();
  1633. if (__bc != 0)
  1634. {
  1635. size_t __chash = __constrain_hash(__hash, __bc);
  1636. __next_pointer __ndptr = __bucket_list_[__chash];
  1637. if (__ndptr != nullptr)
  1638. {
  1639. for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
  1640. __constrain_hash(__ndptr->__hash(), __bc) == __chash;
  1641. __ndptr = __ndptr->__next_)
  1642. {
  1643. if (key_eq()(__ndptr->__upcast()->__value_, __value))
  1644. return __ndptr;
  1645. }
  1646. }
  1647. }
  1648. if (size()+1 > __bc * max_load_factor() || __bc == 0)
  1649. {
  1650. rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
  1651. size_type(ceil(float(size() + 1) / max_load_factor()))));
  1652. }
  1653. return nullptr;
  1654. }
  1655. // Insert the node __nd into the container by pushing it into the right bucket,
  1656. // and updating size(). Assumes that __nd->__hash is up-to-date, and that
  1657. // rehashing has already occurred and that no element with the same key exists
  1658. // in the map.
  1659. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1660. _LIBCPP_INLINE_VISIBILITY
  1661. void
  1662. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique_perform(
  1663. __node_pointer __nd) _NOEXCEPT
  1664. {
  1665. size_type __bc = bucket_count();
  1666. size_t __chash = __constrain_hash(__nd->__hash(), __bc);
  1667. // insert_after __bucket_list_[__chash], or __first_node if bucket is null
  1668. __next_pointer __pn = __bucket_list_[__chash];
  1669. if (__pn == nullptr)
  1670. {
  1671. __pn =__p1_.first().__ptr();
  1672. __nd->__next_ = __pn->__next_;
  1673. __pn->__next_ = __nd->__ptr();
  1674. // fix up __bucket_list_
  1675. __bucket_list_[__chash] = __pn;
  1676. if (__nd->__next_ != nullptr)
  1677. __bucket_list_[__constrain_hash(__nd->__next_->__hash(), __bc)] = __nd->__ptr();
  1678. }
  1679. else
  1680. {
  1681. __nd->__next_ = __pn->__next_;
  1682. __pn->__next_ = __nd->__ptr();
  1683. }
  1684. ++size();
  1685. }
  1686. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1687. pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
  1688. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd)
  1689. {
  1690. __nd->__hash_ = hash_function()(__nd->__value_);
  1691. __next_pointer __existing_node =
  1692. __node_insert_unique_prepare(__nd->__hash(), __nd->__value_);
  1693. // Insert the node, unless it already exists in the container.
  1694. bool __inserted = false;
  1695. if (__existing_node == nullptr)
  1696. {
  1697. __node_insert_unique_perform(__nd);
  1698. __existing_node = __nd->__ptr();
  1699. __inserted = true;
  1700. }
  1701. #if _LIBCPP_DEBUG_LEVEL == 2
  1702. return pair<iterator, bool>(iterator(__existing_node, this), __inserted);
  1703. #else
  1704. return pair<iterator, bool>(iterator(__existing_node), __inserted);
  1705. #endif
  1706. }
  1707. // Prepare the container for an insertion of the value __cp_val with the hash
  1708. // __cp_hash. This does a lookup into the container to see if __cp_value is
  1709. // already present, and performs a rehash if necessary. Returns a pointer to the
  1710. // last occurrence of __cp_val in the map.
  1711. //
  1712. // Note that this function does forward exceptions if key_eq() throws, and never
  1713. // mutates __value or actually inserts into the map.
  1714. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1715. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
  1716. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi_prepare(
  1717. size_t __cp_hash, value_type& __cp_val)
  1718. {
  1719. size_type __bc = bucket_count();
  1720. if (size()+1 > __bc * max_load_factor() || __bc == 0)
  1721. {
  1722. rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
  1723. size_type(ceil(float(size() + 1) / max_load_factor()))));
  1724. __bc = bucket_count();
  1725. }
  1726. size_t __chash = __constrain_hash(__cp_hash, __bc);
  1727. __next_pointer __pn = __bucket_list_[__chash];
  1728. if (__pn != nullptr)
  1729. {
  1730. for (bool __found = false; __pn->__next_ != nullptr &&
  1731. __constrain_hash(__pn->__next_->__hash(), __bc) == __chash;
  1732. __pn = __pn->__next_)
  1733. {
  1734. // __found key_eq() action
  1735. // false false loop
  1736. // true true loop
  1737. // false true set __found to true
  1738. // true false break
  1739. if (__found != (__pn->__next_->__hash() == __cp_hash &&
  1740. key_eq()(__pn->__next_->__upcast()->__value_, __cp_val)))
  1741. {
  1742. if (!__found)
  1743. __found = true;
  1744. else
  1745. break;
  1746. }
  1747. }
  1748. }
  1749. return __pn;
  1750. }
  1751. // Insert the node __cp into the container after __pn (which is the last node in
  1752. // the bucket that compares equal to __cp). Rehashing, and checking for
  1753. // uniqueness has already been performed (in __node_insert_multi_prepare), so
  1754. // all we need to do is update the bucket and size(). Assumes that __cp->__hash
  1755. // is up-to-date.
  1756. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1757. void
  1758. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi_perform(
  1759. __node_pointer __cp, __next_pointer __pn) _NOEXCEPT
  1760. {
  1761. size_type __bc = bucket_count();
  1762. size_t __chash = __constrain_hash(__cp->__hash_, __bc);
  1763. if (__pn == nullptr)
  1764. {
  1765. __pn =__p1_.first().__ptr();
  1766. __cp->__next_ = __pn->__next_;
  1767. __pn->__next_ = __cp->__ptr();
  1768. // fix up __bucket_list_
  1769. __bucket_list_[__chash] = __pn;
  1770. if (__cp->__next_ != nullptr)
  1771. __bucket_list_[__constrain_hash(__cp->__next_->__hash(), __bc)]
  1772. = __cp->__ptr();
  1773. }
  1774. else
  1775. {
  1776. __cp->__next_ = __pn->__next_;
  1777. __pn->__next_ = __cp->__ptr();
  1778. if (__cp->__next_ != nullptr)
  1779. {
  1780. size_t __nhash = __constrain_hash(__cp->__next_->__hash(), __bc);
  1781. if (__nhash != __chash)
  1782. __bucket_list_[__nhash] = __cp->__ptr();
  1783. }
  1784. }
  1785. ++size();
  1786. }
  1787. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1788. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  1789. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp)
  1790. {
  1791. __cp->__hash_ = hash_function()(__cp->__value_);
  1792. __next_pointer __pn = __node_insert_multi_prepare(__cp->__hash(), __cp->__value_);
  1793. __node_insert_multi_perform(__cp, __pn);
  1794. #if _LIBCPP_DEBUG_LEVEL == 2
  1795. return iterator(__cp->__ptr(), this);
  1796. #else
  1797. return iterator(__cp->__ptr());
  1798. #endif
  1799. }
  1800. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1801. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  1802. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(
  1803. const_iterator __p, __node_pointer __cp)
  1804. {
  1805. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
  1806. "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
  1807. " referring to this unordered container");
  1808. if (__p != end() && key_eq()(*__p, __cp->__value_))
  1809. {
  1810. __next_pointer __np = __p.__node_;
  1811. __cp->__hash_ = __np->__hash();
  1812. size_type __bc = bucket_count();
  1813. if (size()+1 > __bc * max_load_factor() || __bc == 0)
  1814. {
  1815. rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
  1816. size_type(ceil(float(size() + 1) / max_load_factor()))));
  1817. __bc = bucket_count();
  1818. }
  1819. size_t __chash = __constrain_hash(__cp->__hash_, __bc);
  1820. __next_pointer __pp = __bucket_list_[__chash];
  1821. while (__pp->__next_ != __np)
  1822. __pp = __pp->__next_;
  1823. __cp->__next_ = __np;
  1824. __pp->__next_ = static_cast<__next_pointer>(__cp);
  1825. ++size();
  1826. #if _LIBCPP_DEBUG_LEVEL == 2
  1827. return iterator(static_cast<__next_pointer>(__cp), this);
  1828. #else
  1829. return iterator(static_cast<__next_pointer>(__cp));
  1830. #endif
  1831. }
  1832. return __node_insert_multi(__cp);
  1833. }
  1834. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1835. template <class _Key, class ..._Args>
  1836. pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
  1837. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
  1838. {
  1839. size_t __hash = hash_function()(__k);
  1840. size_type __bc = bucket_count();
  1841. bool __inserted = false;
  1842. __next_pointer __nd;
  1843. size_t __chash;
  1844. if (__bc != 0)
  1845. {
  1846. __chash = __constrain_hash(__hash, __bc);
  1847. __nd = __bucket_list_[__chash];
  1848. if (__nd != nullptr)
  1849. {
  1850. for (__nd = __nd->__next_; __nd != nullptr &&
  1851. (__nd->__hash() == __hash || __constrain_hash(__nd->__hash(), __bc) == __chash);
  1852. __nd = __nd->__next_)
  1853. {
  1854. if (key_eq()(__nd->__upcast()->__value_, __k))
  1855. goto __done;
  1856. }
  1857. }
  1858. }
  1859. {
  1860. __node_holder __h = __construct_node_hash(__hash, _VSTD::forward<_Args>(__args)...);
  1861. if (size()+1 > __bc * max_load_factor() || __bc == 0)
  1862. {
  1863. rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
  1864. size_type(ceil(float(size() + 1) / max_load_factor()))));
  1865. __bc = bucket_count();
  1866. __chash = __constrain_hash(__hash, __bc);
  1867. }
  1868. // insert_after __bucket_list_[__chash], or __first_node if bucket is null
  1869. __next_pointer __pn = __bucket_list_[__chash];
  1870. if (__pn == nullptr)
  1871. {
  1872. __pn = __p1_.first().__ptr();
  1873. __h->__next_ = __pn->__next_;
  1874. __pn->__next_ = __h.get()->__ptr();
  1875. // fix up __bucket_list_
  1876. __bucket_list_[__chash] = __pn;
  1877. if (__h->__next_ != nullptr)
  1878. __bucket_list_[__constrain_hash(__h->__next_->__hash(), __bc)]
  1879. = __h.get()->__ptr();
  1880. }
  1881. else
  1882. {
  1883. __h->__next_ = __pn->__next_;
  1884. __pn->__next_ = static_cast<__next_pointer>(__h.get());
  1885. }
  1886. __nd = static_cast<__next_pointer>(__h.release());
  1887. // increment size
  1888. ++size();
  1889. __inserted = true;
  1890. }
  1891. __done:
  1892. #if _LIBCPP_DEBUG_LEVEL == 2
  1893. return pair<iterator, bool>(iterator(__nd, this), __inserted);
  1894. #else
  1895. return pair<iterator, bool>(iterator(__nd), __inserted);
  1896. #endif
  1897. }
  1898. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1899. template <class... _Args>
  1900. pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
  1901. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_impl(_Args&&... __args)
  1902. {
  1903. __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
  1904. pair<iterator, bool> __r = __node_insert_unique(__h.get());
  1905. if (__r.second)
  1906. __h.release();
  1907. return __r;
  1908. }
  1909. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1910. template <class... _Args>
  1911. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  1912. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args)
  1913. {
  1914. __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
  1915. iterator __r = __node_insert_multi(__h.get());
  1916. __h.release();
  1917. return __r;
  1918. }
  1919. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1920. template <class... _Args>
  1921. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  1922. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(
  1923. const_iterator __p, _Args&&... __args)
  1924. {
  1925. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
  1926. "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
  1927. " referring to this unordered container");
  1928. __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
  1929. iterator __r = __node_insert_multi(__p, __h.get());
  1930. __h.release();
  1931. return __r;
  1932. }
  1933. #if _LIBCPP_STD_VER > 14
  1934. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1935. template <class _NodeHandle, class _InsertReturnType>
  1936. _LIBCPP_INLINE_VISIBILITY
  1937. _InsertReturnType
  1938. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_unique(
  1939. _NodeHandle&& __nh)
  1940. {
  1941. if (__nh.empty())
  1942. return _InsertReturnType{end(), false, _NodeHandle()};
  1943. pair<iterator, bool> __result = __node_insert_unique(__nh.__ptr_);
  1944. if (__result.second)
  1945. __nh.__release_ptr();
  1946. return _InsertReturnType{__result.first, __result.second, _VSTD::move(__nh)};
  1947. }
  1948. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1949. template <class _NodeHandle>
  1950. _LIBCPP_INLINE_VISIBILITY
  1951. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  1952. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_unique(
  1953. const_iterator, _NodeHandle&& __nh)
  1954. {
  1955. if (__nh.empty())
  1956. return end();
  1957. pair<iterator, bool> __result = __node_insert_unique(__nh.__ptr_);
  1958. if (__result.second)
  1959. __nh.__release_ptr();
  1960. return __result.first;
  1961. }
  1962. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1963. template <class _NodeHandle>
  1964. _LIBCPP_INLINE_VISIBILITY
  1965. _NodeHandle
  1966. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_extract(
  1967. key_type const& __key)
  1968. {
  1969. iterator __i = find(__key);
  1970. if (__i == end())
  1971. return _NodeHandle();
  1972. return __node_handle_extract<_NodeHandle>(__i);
  1973. }
  1974. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1975. template <class _NodeHandle>
  1976. _LIBCPP_INLINE_VISIBILITY
  1977. _NodeHandle
  1978. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_extract(
  1979. const_iterator __p)
  1980. {
  1981. allocator_type __alloc(__node_alloc());
  1982. return _NodeHandle(remove(__p).release(), __alloc);
  1983. }
  1984. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  1985. template <class _Table>
  1986. _LIBCPP_INLINE_VISIBILITY
  1987. void
  1988. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_merge_unique(
  1989. _Table& __source)
  1990. {
  1991. static_assert(is_same<__node, typename _Table::__node>::value, "");
  1992. for (typename _Table::iterator __it = __source.begin();
  1993. __it != __source.end();)
  1994. {
  1995. __node_pointer __src_ptr = __it.__node_->__upcast();
  1996. size_t __hash = hash_function()(__src_ptr->__value_);
  1997. __next_pointer __existing_node =
  1998. __node_insert_unique_prepare(__hash, __src_ptr->__value_);
  1999. auto __prev_iter = __it++;
  2000. if (__existing_node == nullptr)
  2001. {
  2002. (void)__source.remove(__prev_iter).release();
  2003. __src_ptr->__hash_ = __hash;
  2004. __node_insert_unique_perform(__src_ptr);
  2005. }
  2006. }
  2007. }
  2008. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2009. template <class _NodeHandle>
  2010. _LIBCPP_INLINE_VISIBILITY
  2011. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  2012. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_multi(
  2013. _NodeHandle&& __nh)
  2014. {
  2015. if (__nh.empty())
  2016. return end();
  2017. iterator __result = __node_insert_multi(__nh.__ptr_);
  2018. __nh.__release_ptr();
  2019. return __result;
  2020. }
  2021. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2022. template <class _NodeHandle>
  2023. _LIBCPP_INLINE_VISIBILITY
  2024. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  2025. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_multi(
  2026. const_iterator __hint, _NodeHandle&& __nh)
  2027. {
  2028. if (__nh.empty())
  2029. return end();
  2030. iterator __result = __node_insert_multi(__hint, __nh.__ptr_);
  2031. __nh.__release_ptr();
  2032. return __result;
  2033. }
  2034. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2035. template <class _Table>
  2036. _LIBCPP_INLINE_VISIBILITY
  2037. void
  2038. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_merge_multi(
  2039. _Table& __source)
  2040. {
  2041. static_assert(is_same<typename _Table::__node, __node>::value, "");
  2042. for (typename _Table::iterator __it = __source.begin();
  2043. __it != __source.end();)
  2044. {
  2045. __node_pointer __src_ptr = __it.__node_->__upcast();
  2046. size_t __src_hash = hash_function()(__src_ptr->__value_);
  2047. __next_pointer __pn =
  2048. __node_insert_multi_prepare(__src_hash, __src_ptr->__value_);
  2049. (void)__source.remove(__it++).release();
  2050. __src_ptr->__hash_ = __src_hash;
  2051. __node_insert_multi_perform(__src_ptr, __pn);
  2052. }
  2053. }
  2054. #endif // _LIBCPP_STD_VER > 14
  2055. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2056. void
  2057. __hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
  2058. _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
  2059. {
  2060. if (__n == 1)
  2061. __n = 2;
  2062. else if (__n & (__n - 1))
  2063. __n = __next_prime(__n);
  2064. size_type __bc = bucket_count();
  2065. if (__n > __bc)
  2066. __rehash(__n);
  2067. else if (__n < __bc)
  2068. {
  2069. __n = _VSTD::max<size_type>
  2070. (
  2071. __n,
  2072. __is_hash_power2(__bc) ? __next_hash_pow2(size_t(ceil(float(size()) / max_load_factor()))) :
  2073. __next_prime(size_t(ceil(float(size()) / max_load_factor())))
  2074. );
  2075. if (__n < __bc)
  2076. __rehash(__n);
  2077. }
  2078. }
  2079. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2080. void
  2081. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc)
  2082. {
  2083. #if _LIBCPP_DEBUG_LEVEL == 2
  2084. __get_db()->__invalidate_all(this);
  2085. #endif
  2086. __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
  2087. __bucket_list_.reset(__nbc > 0 ?
  2088. __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
  2089. __bucket_list_.get_deleter().size() = __nbc;
  2090. if (__nbc > 0)
  2091. {
  2092. for (size_type __i = 0; __i < __nbc; ++__i)
  2093. __bucket_list_[__i] = nullptr;
  2094. __next_pointer __pp = __p1_.first().__ptr();
  2095. __next_pointer __cp = __pp->__next_;
  2096. if (__cp != nullptr)
  2097. {
  2098. size_type __chash = __constrain_hash(__cp->__hash(), __nbc);
  2099. __bucket_list_[__chash] = __pp;
  2100. size_type __phash = __chash;
  2101. for (__pp = __cp, void(), __cp = __cp->__next_; __cp != nullptr;
  2102. __cp = __pp->__next_)
  2103. {
  2104. __chash = __constrain_hash(__cp->__hash(), __nbc);
  2105. if (__chash == __phash)
  2106. __pp = __cp;
  2107. else
  2108. {
  2109. if (__bucket_list_[__chash] == nullptr)
  2110. {
  2111. __bucket_list_[__chash] = __pp;
  2112. __pp = __cp;
  2113. __phash = __chash;
  2114. }
  2115. else
  2116. {
  2117. __next_pointer __np = __cp;
  2118. for (; __np->__next_ != nullptr &&
  2119. key_eq()(__cp->__upcast()->__value_,
  2120. __np->__next_->__upcast()->__value_);
  2121. __np = __np->__next_)
  2122. ;
  2123. __pp->__next_ = __np->__next_;
  2124. __np->__next_ = __bucket_list_[__chash]->__next_;
  2125. __bucket_list_[__chash]->__next_ = __cp;
  2126. }
  2127. }
  2128. }
  2129. }
  2130. }
  2131. }
  2132. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2133. template <class _Key>
  2134. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  2135. __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
  2136. {
  2137. size_t __hash = hash_function()(__k);
  2138. size_type __bc = bucket_count();
  2139. if (__bc != 0)
  2140. {
  2141. size_t __chash = __constrain_hash(__hash, __bc);
  2142. __next_pointer __nd = __bucket_list_[__chash];
  2143. if (__nd != nullptr)
  2144. {
  2145. for (__nd = __nd->__next_; __nd != nullptr &&
  2146. (__nd->__hash() == __hash
  2147. || __constrain_hash(__nd->__hash(), __bc) == __chash);
  2148. __nd = __nd->__next_)
  2149. {
  2150. if ((__nd->__hash() == __hash)
  2151. && key_eq()(__nd->__upcast()->__value_, __k))
  2152. #if _LIBCPP_DEBUG_LEVEL == 2
  2153. return iterator(__nd, this);
  2154. #else
  2155. return iterator(__nd);
  2156. #endif
  2157. }
  2158. }
  2159. }
  2160. return end();
  2161. }
  2162. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2163. template <class _Key>
  2164. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
  2165. __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
  2166. {
  2167. size_t __hash = hash_function()(__k);
  2168. size_type __bc = bucket_count();
  2169. if (__bc != 0)
  2170. {
  2171. size_t __chash = __constrain_hash(__hash, __bc);
  2172. __next_pointer __nd = __bucket_list_[__chash];
  2173. if (__nd != nullptr)
  2174. {
  2175. for (__nd = __nd->__next_; __nd != nullptr &&
  2176. (__hash == __nd->__hash()
  2177. || __constrain_hash(__nd->__hash(), __bc) == __chash);
  2178. __nd = __nd->__next_)
  2179. {
  2180. if ((__nd->__hash() == __hash)
  2181. && key_eq()(__nd->__upcast()->__value_, __k))
  2182. #if _LIBCPP_DEBUG_LEVEL == 2
  2183. return const_iterator(__nd, this);
  2184. #else
  2185. return const_iterator(__nd);
  2186. #endif
  2187. }
  2188. }
  2189. }
  2190. return end();
  2191. }
  2192. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2193. template <class ..._Args>
  2194. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
  2195. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args)
  2196. {
  2197. static_assert(!__is_hash_value_type<_Args...>::value,
  2198. "Construct cannot be called with a hash value type");
  2199. __node_allocator& __na = __node_alloc();
  2200. __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
  2201. __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
  2202. __h.get_deleter().__value_constructed = true;
  2203. __h->__hash_ = hash_function()(__h->__value_);
  2204. __h->__next_ = nullptr;
  2205. return __h;
  2206. }
  2207. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2208. template <class _First, class ..._Rest>
  2209. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
  2210. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(
  2211. size_t __hash, _First&& __f, _Rest&& ...__rest)
  2212. {
  2213. static_assert(!__is_hash_value_type<_First, _Rest...>::value,
  2214. "Construct cannot be called with a hash value type");
  2215. __node_allocator& __na = __node_alloc();
  2216. __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
  2217. __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_),
  2218. _VSTD::forward<_First>(__f),
  2219. _VSTD::forward<_Rest>(__rest)...);
  2220. __h.get_deleter().__value_constructed = true;
  2221. __h->__hash_ = __hash;
  2222. __h->__next_ = nullptr;
  2223. return __h;
  2224. }
  2225. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2226. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  2227. __hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p)
  2228. {
  2229. __next_pointer __np = __p.__node_;
  2230. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__p)) == this,
  2231. "unordered container erase(iterator) called with an iterator not"
  2232. " referring to this container");
  2233. _LIBCPP_DEBUG_ASSERT(__p != end(),
  2234. "unordered container erase(iterator) called with a non-dereferenceable iterator");
  2235. #if _LIBCPP_DEBUG_LEVEL == 2
  2236. iterator __r(__np, this);
  2237. #else
  2238. iterator __r(__np);
  2239. #endif
  2240. ++__r;
  2241. remove(__p);
  2242. return __r;
  2243. }
  2244. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2245. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
  2246. __hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first,
  2247. const_iterator __last)
  2248. {
  2249. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__first)) == this,
  2250. "unordered container::erase(iterator, iterator) called with an iterator not"
  2251. " referring to this container");
  2252. _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(_VSTD::addressof(__last)) == this,
  2253. "unordered container::erase(iterator, iterator) called with an iterator not"
  2254. " referring to this container");
  2255. for (const_iterator __p = __first; __first != __last; __p = __first)
  2256. {
  2257. ++__first;
  2258. erase(__p);
  2259. }
  2260. __next_pointer __np = __last.__node_;
  2261. #if _LIBCPP_DEBUG_LEVEL == 2
  2262. return iterator (__np, this);
  2263. #else
  2264. return iterator (__np);
  2265. #endif
  2266. }
  2267. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2268. template <class _Key>
  2269. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
  2270. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k)
  2271. {
  2272. iterator __i = find(__k);
  2273. if (__i == end())
  2274. return 0;
  2275. erase(__i);
  2276. return 1;
  2277. }
  2278. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2279. template <class _Key>
  2280. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
  2281. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k)
  2282. {
  2283. size_type __r = 0;
  2284. iterator __i = find(__k);
  2285. if (__i != end())
  2286. {
  2287. iterator __e = end();
  2288. do
  2289. {
  2290. erase(__i++);
  2291. ++__r;
  2292. } while (__i != __e && key_eq()(*__i, __k));
  2293. }
  2294. return __r;
  2295. }
  2296. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2297. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
  2298. __hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT
  2299. {
  2300. // current node
  2301. __next_pointer __cn = __p.__node_;
  2302. size_type __bc = bucket_count();
  2303. size_t __chash = __constrain_hash(__cn->__hash(), __bc);
  2304. // find previous node
  2305. __next_pointer __pn = __bucket_list_[__chash];
  2306. for (; __pn->__next_ != __cn; __pn = __pn->__next_)
  2307. ;
  2308. // Fix up __bucket_list_
  2309. // if __pn is not in same bucket (before begin is not in same bucket) &&
  2310. // if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
  2311. if (__pn == __p1_.first().__ptr()
  2312. || __constrain_hash(__pn->__hash(), __bc) != __chash)
  2313. {
  2314. if (__cn->__next_ == nullptr
  2315. || __constrain_hash(__cn->__next_->__hash(), __bc) != __chash)
  2316. __bucket_list_[__chash] = nullptr;
  2317. }
  2318. // if __cn->__next_ is not in same bucket (nullptr is in same bucket)
  2319. if (__cn->__next_ != nullptr)
  2320. {
  2321. size_t __nhash = __constrain_hash(__cn->__next_->__hash(), __bc);
  2322. if (__nhash != __chash)
  2323. __bucket_list_[__nhash] = __pn;
  2324. }
  2325. // remove __cn
  2326. __pn->__next_ = __cn->__next_;
  2327. __cn->__next_ = nullptr;
  2328. --size();
  2329. #if _LIBCPP_DEBUG_LEVEL == 2
  2330. __c_node* __c = __get_db()->__find_c_and_lock(this);
  2331. for (__i_node** __dp = __c->end_; __dp != __c->beg_; )
  2332. {
  2333. --__dp;
  2334. iterator* __i = static_cast<iterator*>((*__dp)->__i_);
  2335. if (__i->__node_ == __cn)
  2336. {
  2337. (*__dp)->__c_ = nullptr;
  2338. if (--__c->end_ != __dp)
  2339. _VSTD::memmove(__dp, __dp+1, (__c->end_ - __dp)*sizeof(__i_node*));
  2340. }
  2341. }
  2342. __get_db()->unlock();
  2343. #endif
  2344. return __node_holder(__cn->__upcast(), _Dp(__node_alloc(), true));
  2345. }
  2346. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2347. template <class _Key>
  2348. inline
  2349. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
  2350. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const
  2351. {
  2352. return static_cast<size_type>(find(__k) != end());
  2353. }
  2354. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2355. template <class _Key>
  2356. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
  2357. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const
  2358. {
  2359. size_type __r = 0;
  2360. const_iterator __i = find(__k);
  2361. if (__i != end())
  2362. {
  2363. const_iterator __e = end();
  2364. do
  2365. {
  2366. ++__i;
  2367. ++__r;
  2368. } while (__i != __e && key_eq()(*__i, __k));
  2369. }
  2370. return __r;
  2371. }
  2372. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2373. template <class _Key>
  2374. pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
  2375. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
  2376. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
  2377. const _Key& __k)
  2378. {
  2379. iterator __i = find(__k);
  2380. iterator __j = __i;
  2381. if (__i != end())
  2382. ++__j;
  2383. return pair<iterator, iterator>(__i, __j);
  2384. }
  2385. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2386. template <class _Key>
  2387. pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
  2388. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
  2389. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
  2390. const _Key& __k) const
  2391. {
  2392. const_iterator __i = find(__k);
  2393. const_iterator __j = __i;
  2394. if (__i != end())
  2395. ++__j;
  2396. return pair<const_iterator, const_iterator>(__i, __j);
  2397. }
  2398. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2399. template <class _Key>
  2400. pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
  2401. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
  2402. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
  2403. const _Key& __k)
  2404. {
  2405. iterator __i = find(__k);
  2406. iterator __j = __i;
  2407. if (__i != end())
  2408. {
  2409. iterator __e = end();
  2410. do
  2411. {
  2412. ++__j;
  2413. } while (__j != __e && key_eq()(*__j, __k));
  2414. }
  2415. return pair<iterator, iterator>(__i, __j);
  2416. }
  2417. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2418. template <class _Key>
  2419. pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
  2420. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
  2421. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
  2422. const _Key& __k) const
  2423. {
  2424. const_iterator __i = find(__k);
  2425. const_iterator __j = __i;
  2426. if (__i != end())
  2427. {
  2428. const_iterator __e = end();
  2429. do
  2430. {
  2431. ++__j;
  2432. } while (__j != __e && key_eq()(*__j, __k));
  2433. }
  2434. return pair<const_iterator, const_iterator>(__i, __j);
  2435. }
  2436. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2437. void
  2438. __hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
  2439. #if _LIBCPP_STD_VER <= 11
  2440. _NOEXCEPT_(
  2441. __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
  2442. && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
  2443. || __is_nothrow_swappable<__pointer_allocator>::value)
  2444. && (!__node_traits::propagate_on_container_swap::value
  2445. || __is_nothrow_swappable<__node_allocator>::value)
  2446. )
  2447. #else
  2448. _NOEXCEPT_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value)
  2449. #endif
  2450. {
  2451. _LIBCPP_ASSERT(__node_traits::propagate_on_container_swap::value ||
  2452. this->__node_alloc() == __u.__node_alloc(),
  2453. "list::swap: Either propagate_on_container_swap must be true"
  2454. " or the allocators must compare equal");
  2455. {
  2456. __node_pointer_pointer __npp = __bucket_list_.release();
  2457. __bucket_list_.reset(__u.__bucket_list_.release());
  2458. __u.__bucket_list_.reset(__npp);
  2459. }
  2460. _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
  2461. _VSTD::__swap_allocator(__bucket_list_.get_deleter().__alloc(),
  2462. __u.__bucket_list_.get_deleter().__alloc());
  2463. _VSTD::__swap_allocator(__node_alloc(), __u.__node_alloc());
  2464. _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
  2465. __p2_.swap(__u.__p2_);
  2466. __p3_.swap(__u.__p3_);
  2467. if (size() > 0)
  2468. __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
  2469. __p1_.first().__ptr();
  2470. if (__u.size() > 0)
  2471. __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash(), __u.bucket_count())] =
  2472. __u.__p1_.first().__ptr();
  2473. #if _LIBCPP_DEBUG_LEVEL == 2
  2474. __get_db()->swap(this, _VSTD::addressof(__u));
  2475. #endif
  2476. }
  2477. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2478. typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
  2479. __hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const
  2480. {
  2481. _LIBCPP_ASSERT(__n < bucket_count(),
  2482. "unordered container::bucket_size(n) called with n >= bucket_count()");
  2483. __next_pointer __np = __bucket_list_[__n];
  2484. size_type __bc = bucket_count();
  2485. size_type __r = 0;
  2486. if (__np != nullptr)
  2487. {
  2488. for (__np = __np->__next_; __np != nullptr &&
  2489. __constrain_hash(__np->__hash(), __bc) == __n;
  2490. __np = __np->__next_, (void) ++__r)
  2491. ;
  2492. }
  2493. return __r;
  2494. }
  2495. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2496. inline _LIBCPP_INLINE_VISIBILITY
  2497. void
  2498. swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x,
  2499. __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
  2500. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  2501. {
  2502. __x.swap(__y);
  2503. }
  2504. #if _LIBCPP_DEBUG_LEVEL == 2
  2505. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2506. bool
  2507. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const
  2508. {
  2509. return __i->__node_ != nullptr;
  2510. }
  2511. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2512. bool
  2513. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const
  2514. {
  2515. return false;
  2516. }
  2517. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2518. bool
  2519. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
  2520. {
  2521. return false;
  2522. }
  2523. template <class _Tp, class _Hash, class _Equal, class _Alloc>
  2524. bool
  2525. __hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
  2526. {
  2527. return false;
  2528. }
  2529. #endif // _LIBCPP_DEBUG_LEVEL == 2
  2530. _LIBCPP_END_NAMESPACE_STD
  2531. _LIBCPP_POP_MACROS
  2532. #endif // _LIBCPP__HASH_TABLE