__tree 102 KB

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