__tree 91 KB

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