__tree 91 KB

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