__tree 102 KB

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