__tree 101 KB

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