__tree 95 KB

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