__tree 100 KB

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