stl_tree.h 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612
  1. // RB tree implementation -*- C++ -*-
  2. // Copyright (C) 2001-2021 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. *
  22. * Copyright (c) 1996,1997
  23. * Silicon Graphics Computer Systems, Inc.
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Silicon Graphics makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 1994
  35. * Hewlett-Packard Company
  36. *
  37. * Permission to use, copy, modify, distribute and sell this software
  38. * and its documentation for any purpose is hereby granted without fee,
  39. * provided that the above copyright notice appear in all copies and
  40. * that both that copyright notice and this permission notice appear
  41. * in supporting documentation. Hewlett-Packard Company makes no
  42. * representations about the suitability of this software for any
  43. * purpose. It is provided "as is" without express or implied warranty.
  44. *
  45. *
  46. */
  47. /** @file bits/stl_tree.h
  48. * This is an internal header file, included by other library headers.
  49. * Do not attempt to use it directly. @headername{map,set}
  50. */
  51. #ifndef _STL_TREE_H
  52. #define _STL_TREE_H 1
  53. #pragma GCC system_header
  54. #include <bits/stl_algobase.h>
  55. #include <bits/allocator.h>
  56. #include <bits/stl_function.h>
  57. #include <bits/cpp_type_traits.h>
  58. #include <ext/alloc_traits.h>
  59. #if __cplusplus >= 201103L
  60. # include <ext/aligned_buffer.h>
  61. #endif
  62. #if __cplusplus > 201402L
  63. # include <bits/node_handle.h>
  64. #endif
  65. namespace std _GLIBCXX_VISIBILITY(default)
  66. {
  67. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  68. #if __cplusplus > 201103L
  69. # define __cpp_lib_generic_associative_lookup 201304
  70. #endif
  71. // Red-black tree class, designed for use in implementing STL
  72. // associative containers (set, multiset, map, and multimap). The
  73. // insertion and deletion algorithms are based on those in Cormen,
  74. // Leiserson, and Rivest, Introduction to Algorithms (MIT Press,
  75. // 1990), except that
  76. //
  77. // (1) the header cell is maintained with links not only to the root
  78. // but also to the leftmost node of the tree, to enable constant
  79. // time begin(), and to the rightmost node of the tree, to enable
  80. // linear time performance when used with the generic set algorithms
  81. // (set_union, etc.)
  82. //
  83. // (2) when a node being deleted has two children its successor node
  84. // is relinked into its place, rather than copied, so that the only
  85. // iterators invalidated are those referring to the deleted node.
  86. enum _Rb_tree_color { _S_red = false, _S_black = true };
  87. struct _Rb_tree_node_base
  88. {
  89. typedef _Rb_tree_node_base* _Base_ptr;
  90. typedef const _Rb_tree_node_base* _Const_Base_ptr;
  91. _Rb_tree_color _M_color;
  92. _Base_ptr _M_parent;
  93. _Base_ptr _M_left;
  94. _Base_ptr _M_right;
  95. static _Base_ptr
  96. _S_minimum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
  97. {
  98. while (__x->_M_left != 0) __x = __x->_M_left;
  99. return __x;
  100. }
  101. static _Const_Base_ptr
  102. _S_minimum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
  103. {
  104. while (__x->_M_left != 0) __x = __x->_M_left;
  105. return __x;
  106. }
  107. static _Base_ptr
  108. _S_maximum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
  109. {
  110. while (__x->_M_right != 0) __x = __x->_M_right;
  111. return __x;
  112. }
  113. static _Const_Base_ptr
  114. _S_maximum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
  115. {
  116. while (__x->_M_right != 0) __x = __x->_M_right;
  117. return __x;
  118. }
  119. };
  120. // Helper type offering value initialization guarantee on the compare functor.
  121. template<typename _Key_compare>
  122. struct _Rb_tree_key_compare
  123. {
  124. _Key_compare _M_key_compare;
  125. _Rb_tree_key_compare()
  126. _GLIBCXX_NOEXCEPT_IF(
  127. is_nothrow_default_constructible<_Key_compare>::value)
  128. : _M_key_compare()
  129. { }
  130. _Rb_tree_key_compare(const _Key_compare& __comp)
  131. : _M_key_compare(__comp)
  132. { }
  133. #if __cplusplus >= 201103L
  134. // Copy constructor added for consistency with C++98 mode.
  135. _Rb_tree_key_compare(const _Rb_tree_key_compare&) = default;
  136. _Rb_tree_key_compare(_Rb_tree_key_compare&& __x)
  137. noexcept(is_nothrow_copy_constructible<_Key_compare>::value)
  138. : _M_key_compare(__x._M_key_compare)
  139. { }
  140. #endif
  141. };
  142. // Helper type to manage default initialization of node count and header.
  143. struct _Rb_tree_header
  144. {
  145. _Rb_tree_node_base _M_header;
  146. size_t _M_node_count; // Keeps track of size of tree.
  147. _Rb_tree_header() _GLIBCXX_NOEXCEPT
  148. {
  149. _M_header._M_color = _S_red;
  150. _M_reset();
  151. }
  152. #if __cplusplus >= 201103L
  153. _Rb_tree_header(_Rb_tree_header&& __x) noexcept
  154. {
  155. if (__x._M_header._M_parent != nullptr)
  156. _M_move_data(__x);
  157. else
  158. {
  159. _M_header._M_color = _S_red;
  160. _M_reset();
  161. }
  162. }
  163. #endif
  164. void
  165. _M_move_data(_Rb_tree_header& __from)
  166. {
  167. _M_header._M_color = __from._M_header._M_color;
  168. _M_header._M_parent = __from._M_header._M_parent;
  169. _M_header._M_left = __from._M_header._M_left;
  170. _M_header._M_right = __from._M_header._M_right;
  171. _M_header._M_parent->_M_parent = &_M_header;
  172. _M_node_count = __from._M_node_count;
  173. __from._M_reset();
  174. }
  175. void
  176. _M_reset()
  177. {
  178. _M_header._M_parent = 0;
  179. _M_header._M_left = &_M_header;
  180. _M_header._M_right = &_M_header;
  181. _M_node_count = 0;
  182. }
  183. };
  184. template<typename _Val>
  185. struct _Rb_tree_node : public _Rb_tree_node_base
  186. {
  187. typedef _Rb_tree_node<_Val>* _Link_type;
  188. #if __cplusplus < 201103L
  189. _Val _M_value_field;
  190. _Val*
  191. _M_valptr()
  192. { return std::__addressof(_M_value_field); }
  193. const _Val*
  194. _M_valptr() const
  195. { return std::__addressof(_M_value_field); }
  196. #else
  197. __gnu_cxx::__aligned_membuf<_Val> _M_storage;
  198. _Val*
  199. _M_valptr()
  200. { return _M_storage._M_ptr(); }
  201. const _Val*
  202. _M_valptr() const
  203. { return _M_storage._M_ptr(); }
  204. #endif
  205. };
  206. _GLIBCXX_PURE _Rb_tree_node_base*
  207. _Rb_tree_increment(_Rb_tree_node_base* __x) throw ();
  208. _GLIBCXX_PURE const _Rb_tree_node_base*
  209. _Rb_tree_increment(const _Rb_tree_node_base* __x) throw ();
  210. _GLIBCXX_PURE _Rb_tree_node_base*
  211. _Rb_tree_decrement(_Rb_tree_node_base* __x) throw ();
  212. _GLIBCXX_PURE const _Rb_tree_node_base*
  213. _Rb_tree_decrement(const _Rb_tree_node_base* __x) throw ();
  214. template<typename _Tp>
  215. struct _Rb_tree_iterator
  216. {
  217. typedef _Tp value_type;
  218. typedef _Tp& reference;
  219. typedef _Tp* pointer;
  220. typedef bidirectional_iterator_tag iterator_category;
  221. typedef ptrdiff_t difference_type;
  222. typedef _Rb_tree_iterator<_Tp> _Self;
  223. typedef _Rb_tree_node_base::_Base_ptr _Base_ptr;
  224. typedef _Rb_tree_node<_Tp>* _Link_type;
  225. _Rb_tree_iterator() _GLIBCXX_NOEXCEPT
  226. : _M_node() { }
  227. explicit
  228. _Rb_tree_iterator(_Base_ptr __x) _GLIBCXX_NOEXCEPT
  229. : _M_node(__x) { }
  230. reference
  231. operator*() const _GLIBCXX_NOEXCEPT
  232. { return *static_cast<_Link_type>(_M_node)->_M_valptr(); }
  233. pointer
  234. operator->() const _GLIBCXX_NOEXCEPT
  235. { return static_cast<_Link_type> (_M_node)->_M_valptr(); }
  236. _Self&
  237. operator++() _GLIBCXX_NOEXCEPT
  238. {
  239. _M_node = _Rb_tree_increment(_M_node);
  240. return *this;
  241. }
  242. _Self
  243. operator++(int) _GLIBCXX_NOEXCEPT
  244. {
  245. _Self __tmp = *this;
  246. _M_node = _Rb_tree_increment(_M_node);
  247. return __tmp;
  248. }
  249. _Self&
  250. operator--() _GLIBCXX_NOEXCEPT
  251. {
  252. _M_node = _Rb_tree_decrement(_M_node);
  253. return *this;
  254. }
  255. _Self
  256. operator--(int) _GLIBCXX_NOEXCEPT
  257. {
  258. _Self __tmp = *this;
  259. _M_node = _Rb_tree_decrement(_M_node);
  260. return __tmp;
  261. }
  262. friend bool
  263. operator==(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
  264. { return __x._M_node == __y._M_node; }
  265. #if ! __cpp_lib_three_way_comparison
  266. friend bool
  267. operator!=(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
  268. { return __x._M_node != __y._M_node; }
  269. #endif
  270. _Base_ptr _M_node;
  271. };
  272. template<typename _Tp>
  273. struct _Rb_tree_const_iterator
  274. {
  275. typedef _Tp value_type;
  276. typedef const _Tp& reference;
  277. typedef const _Tp* pointer;
  278. typedef _Rb_tree_iterator<_Tp> iterator;
  279. typedef bidirectional_iterator_tag iterator_category;
  280. typedef ptrdiff_t difference_type;
  281. typedef _Rb_tree_const_iterator<_Tp> _Self;
  282. typedef _Rb_tree_node_base::_Const_Base_ptr _Base_ptr;
  283. typedef const _Rb_tree_node<_Tp>* _Link_type;
  284. _Rb_tree_const_iterator() _GLIBCXX_NOEXCEPT
  285. : _M_node() { }
  286. explicit
  287. _Rb_tree_const_iterator(_Base_ptr __x) _GLIBCXX_NOEXCEPT
  288. : _M_node(__x) { }
  289. _Rb_tree_const_iterator(const iterator& __it) _GLIBCXX_NOEXCEPT
  290. : _M_node(__it._M_node) { }
  291. iterator
  292. _M_const_cast() const _GLIBCXX_NOEXCEPT
  293. { return iterator(const_cast<typename iterator::_Base_ptr>(_M_node)); }
  294. reference
  295. operator*() const _GLIBCXX_NOEXCEPT
  296. { return *static_cast<_Link_type>(_M_node)->_M_valptr(); }
  297. pointer
  298. operator->() const _GLIBCXX_NOEXCEPT
  299. { return static_cast<_Link_type>(_M_node)->_M_valptr(); }
  300. _Self&
  301. operator++() _GLIBCXX_NOEXCEPT
  302. {
  303. _M_node = _Rb_tree_increment(_M_node);
  304. return *this;
  305. }
  306. _Self
  307. operator++(int) _GLIBCXX_NOEXCEPT
  308. {
  309. _Self __tmp = *this;
  310. _M_node = _Rb_tree_increment(_M_node);
  311. return __tmp;
  312. }
  313. _Self&
  314. operator--() _GLIBCXX_NOEXCEPT
  315. {
  316. _M_node = _Rb_tree_decrement(_M_node);
  317. return *this;
  318. }
  319. _Self
  320. operator--(int) _GLIBCXX_NOEXCEPT
  321. {
  322. _Self __tmp = *this;
  323. _M_node = _Rb_tree_decrement(_M_node);
  324. return __tmp;
  325. }
  326. friend bool
  327. operator==(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
  328. { return __x._M_node == __y._M_node; }
  329. #if ! __cpp_lib_three_way_comparison
  330. friend bool
  331. operator!=(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
  332. { return __x._M_node != __y._M_node; }
  333. #endif
  334. _Base_ptr _M_node;
  335. };
  336. void
  337. _Rb_tree_insert_and_rebalance(const bool __insert_left,
  338. _Rb_tree_node_base* __x,
  339. _Rb_tree_node_base* __p,
  340. _Rb_tree_node_base& __header) throw ();
  341. _Rb_tree_node_base*
  342. _Rb_tree_rebalance_for_erase(_Rb_tree_node_base* const __z,
  343. _Rb_tree_node_base& __header) throw ();
  344. #if __cplusplus > 201402L
  345. template<typename _Tree1, typename _Cmp2>
  346. struct _Rb_tree_merge_helper { };
  347. #endif
  348. template<typename _Key, typename _Val, typename _KeyOfValue,
  349. typename _Compare, typename _Alloc = allocator<_Val> >
  350. class _Rb_tree
  351. {
  352. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  353. rebind<_Rb_tree_node<_Val> >::other _Node_allocator;
  354. typedef __gnu_cxx::__alloc_traits<_Node_allocator> _Alloc_traits;
  355. protected:
  356. typedef _Rb_tree_node_base* _Base_ptr;
  357. typedef const _Rb_tree_node_base* _Const_Base_ptr;
  358. typedef _Rb_tree_node<_Val>* _Link_type;
  359. typedef const _Rb_tree_node<_Val>* _Const_Link_type;
  360. private:
  361. // Functor recycling a pool of nodes and using allocation once the pool
  362. // is empty.
  363. struct _Reuse_or_alloc_node
  364. {
  365. _Reuse_or_alloc_node(_Rb_tree& __t)
  366. : _M_root(__t._M_root()), _M_nodes(__t._M_rightmost()), _M_t(__t)
  367. {
  368. if (_M_root)
  369. {
  370. _M_root->_M_parent = 0;
  371. if (_M_nodes->_M_left)
  372. _M_nodes = _M_nodes->_M_left;
  373. }
  374. else
  375. _M_nodes = 0;
  376. }
  377. #if __cplusplus >= 201103L
  378. _Reuse_or_alloc_node(const _Reuse_or_alloc_node&) = delete;
  379. #endif
  380. ~_Reuse_or_alloc_node()
  381. { _M_t._M_erase(static_cast<_Link_type>(_M_root)); }
  382. template<typename _Arg>
  383. _Link_type
  384. operator()(_GLIBCXX_FWDREF(_Arg) __arg)
  385. {
  386. _Link_type __node = static_cast<_Link_type>(_M_extract());
  387. if (__node)
  388. {
  389. _M_t._M_destroy_node(__node);
  390. _M_t._M_construct_node(__node, _GLIBCXX_FORWARD(_Arg, __arg));
  391. return __node;
  392. }
  393. return _M_t._M_create_node(_GLIBCXX_FORWARD(_Arg, __arg));
  394. }
  395. private:
  396. _Base_ptr
  397. _M_extract()
  398. {
  399. if (!_M_nodes)
  400. return _M_nodes;
  401. _Base_ptr __node = _M_nodes;
  402. _M_nodes = _M_nodes->_M_parent;
  403. if (_M_nodes)
  404. {
  405. if (_M_nodes->_M_right == __node)
  406. {
  407. _M_nodes->_M_right = 0;
  408. if (_M_nodes->_M_left)
  409. {
  410. _M_nodes = _M_nodes->_M_left;
  411. while (_M_nodes->_M_right)
  412. _M_nodes = _M_nodes->_M_right;
  413. if (_M_nodes->_M_left)
  414. _M_nodes = _M_nodes->_M_left;
  415. }
  416. }
  417. else // __node is on the left.
  418. _M_nodes->_M_left = 0;
  419. }
  420. else
  421. _M_root = 0;
  422. return __node;
  423. }
  424. _Base_ptr _M_root;
  425. _Base_ptr _M_nodes;
  426. _Rb_tree& _M_t;
  427. };
  428. // Functor similar to the previous one but without any pool of nodes to
  429. // recycle.
  430. struct _Alloc_node
  431. {
  432. _Alloc_node(_Rb_tree& __t)
  433. : _M_t(__t) { }
  434. template<typename _Arg>
  435. _Link_type
  436. operator()(_GLIBCXX_FWDREF(_Arg) __arg) const
  437. { return _M_t._M_create_node(_GLIBCXX_FORWARD(_Arg, __arg)); }
  438. private:
  439. _Rb_tree& _M_t;
  440. };
  441. public:
  442. typedef _Key key_type;
  443. typedef _Val value_type;
  444. typedef value_type* pointer;
  445. typedef const value_type* const_pointer;
  446. typedef value_type& reference;
  447. typedef const value_type& const_reference;
  448. typedef size_t size_type;
  449. typedef ptrdiff_t difference_type;
  450. typedef _Alloc allocator_type;
  451. _Node_allocator&
  452. _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
  453. { return this->_M_impl; }
  454. const _Node_allocator&
  455. _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
  456. { return this->_M_impl; }
  457. allocator_type
  458. get_allocator() const _GLIBCXX_NOEXCEPT
  459. { return allocator_type(_M_get_Node_allocator()); }
  460. protected:
  461. _Link_type
  462. _M_get_node()
  463. { return _Alloc_traits::allocate(_M_get_Node_allocator(), 1); }
  464. void
  465. _M_put_node(_Link_type __p) _GLIBCXX_NOEXCEPT
  466. { _Alloc_traits::deallocate(_M_get_Node_allocator(), __p, 1); }
  467. #if __cplusplus < 201103L
  468. void
  469. _M_construct_node(_Link_type __node, const value_type& __x)
  470. {
  471. __try
  472. { get_allocator().construct(__node->_M_valptr(), __x); }
  473. __catch(...)
  474. {
  475. _M_put_node(__node);
  476. __throw_exception_again;
  477. }
  478. }
  479. _Link_type
  480. _M_create_node(const value_type& __x)
  481. {
  482. _Link_type __tmp = _M_get_node();
  483. _M_construct_node(__tmp, __x);
  484. return __tmp;
  485. }
  486. #else
  487. template<typename... _Args>
  488. void
  489. _M_construct_node(_Link_type __node, _Args&&... __args)
  490. {
  491. __try
  492. {
  493. ::new(__node) _Rb_tree_node<_Val>;
  494. _Alloc_traits::construct(_M_get_Node_allocator(),
  495. __node->_M_valptr(),
  496. std::forward<_Args>(__args)...);
  497. }
  498. __catch(...)
  499. {
  500. __node->~_Rb_tree_node<_Val>();
  501. _M_put_node(__node);
  502. __throw_exception_again;
  503. }
  504. }
  505. template<typename... _Args>
  506. _Link_type
  507. _M_create_node(_Args&&... __args)
  508. {
  509. _Link_type __tmp = _M_get_node();
  510. _M_construct_node(__tmp, std::forward<_Args>(__args)...);
  511. return __tmp;
  512. }
  513. #endif
  514. void
  515. _M_destroy_node(_Link_type __p) _GLIBCXX_NOEXCEPT
  516. {
  517. #if __cplusplus < 201103L
  518. get_allocator().destroy(__p->_M_valptr());
  519. #else
  520. _Alloc_traits::destroy(_M_get_Node_allocator(), __p->_M_valptr());
  521. __p->~_Rb_tree_node<_Val>();
  522. #endif
  523. }
  524. void
  525. _M_drop_node(_Link_type __p) _GLIBCXX_NOEXCEPT
  526. {
  527. _M_destroy_node(__p);
  528. _M_put_node(__p);
  529. }
  530. template<bool _MoveValue, typename _NodeGen>
  531. _Link_type
  532. _M_clone_node(_Link_type __x, _NodeGen& __node_gen)
  533. {
  534. #if __cplusplus >= 201103L
  535. using _Vp = typename conditional<_MoveValue,
  536. value_type&&,
  537. const value_type&>::type;
  538. #endif
  539. _Link_type __tmp
  540. = __node_gen(_GLIBCXX_FORWARD(_Vp, *__x->_M_valptr()));
  541. __tmp->_M_color = __x->_M_color;
  542. __tmp->_M_left = 0;
  543. __tmp->_M_right = 0;
  544. return __tmp;
  545. }
  546. protected:
  547. #if _GLIBCXX_INLINE_VERSION
  548. template<typename _Key_compare>
  549. #else
  550. // Unused _Is_pod_comparator is kept as it is part of mangled name.
  551. template<typename _Key_compare,
  552. bool /* _Is_pod_comparator */ = __is_pod(_Key_compare)>
  553. #endif
  554. struct _Rb_tree_impl
  555. : public _Node_allocator
  556. , public _Rb_tree_key_compare<_Key_compare>
  557. , public _Rb_tree_header
  558. {
  559. typedef _Rb_tree_key_compare<_Key_compare> _Base_key_compare;
  560. _Rb_tree_impl()
  561. _GLIBCXX_NOEXCEPT_IF(
  562. is_nothrow_default_constructible<_Node_allocator>::value
  563. && is_nothrow_default_constructible<_Base_key_compare>::value )
  564. : _Node_allocator()
  565. { }
  566. _Rb_tree_impl(const _Rb_tree_impl& __x)
  567. : _Node_allocator(_Alloc_traits::_S_select_on_copy(__x))
  568. , _Base_key_compare(__x._M_key_compare)
  569. , _Rb_tree_header()
  570. { }
  571. #if __cplusplus < 201103L
  572. _Rb_tree_impl(const _Key_compare& __comp, const _Node_allocator& __a)
  573. : _Node_allocator(__a), _Base_key_compare(__comp)
  574. { }
  575. #else
  576. _Rb_tree_impl(_Rb_tree_impl&&)
  577. noexcept( is_nothrow_move_constructible<_Base_key_compare>::value )
  578. = default;
  579. explicit
  580. _Rb_tree_impl(_Node_allocator&& __a)
  581. : _Node_allocator(std::move(__a))
  582. { }
  583. _Rb_tree_impl(_Rb_tree_impl&& __x, _Node_allocator&& __a)
  584. : _Node_allocator(std::move(__a)),
  585. _Base_key_compare(std::move(__x)),
  586. _Rb_tree_header(std::move(__x))
  587. { }
  588. _Rb_tree_impl(const _Key_compare& __comp, _Node_allocator&& __a)
  589. : _Node_allocator(std::move(__a)), _Base_key_compare(__comp)
  590. { }
  591. #endif
  592. };
  593. _Rb_tree_impl<_Compare> _M_impl;
  594. protected:
  595. _Base_ptr&
  596. _M_root() _GLIBCXX_NOEXCEPT
  597. { return this->_M_impl._M_header._M_parent; }
  598. _Const_Base_ptr
  599. _M_root() const _GLIBCXX_NOEXCEPT
  600. { return this->_M_impl._M_header._M_parent; }
  601. _Base_ptr&
  602. _M_leftmost() _GLIBCXX_NOEXCEPT
  603. { return this->_M_impl._M_header._M_left; }
  604. _Const_Base_ptr
  605. _M_leftmost() const _GLIBCXX_NOEXCEPT
  606. { return this->_M_impl._M_header._M_left; }
  607. _Base_ptr&
  608. _M_rightmost() _GLIBCXX_NOEXCEPT
  609. { return this->_M_impl._M_header._M_right; }
  610. _Const_Base_ptr
  611. _M_rightmost() const _GLIBCXX_NOEXCEPT
  612. { return this->_M_impl._M_header._M_right; }
  613. _Link_type
  614. _M_mbegin() const _GLIBCXX_NOEXCEPT
  615. { return static_cast<_Link_type>(this->_M_impl._M_header._M_parent); }
  616. _Link_type
  617. _M_begin() _GLIBCXX_NOEXCEPT
  618. { return _M_mbegin(); }
  619. _Const_Link_type
  620. _M_begin() const _GLIBCXX_NOEXCEPT
  621. {
  622. return static_cast<_Const_Link_type>
  623. (this->_M_impl._M_header._M_parent);
  624. }
  625. _Base_ptr
  626. _M_end() _GLIBCXX_NOEXCEPT
  627. { return &this->_M_impl._M_header; }
  628. _Const_Base_ptr
  629. _M_end() const _GLIBCXX_NOEXCEPT
  630. { return &this->_M_impl._M_header; }
  631. static const _Key&
  632. _S_key(_Const_Link_type __x)
  633. {
  634. #if __cplusplus >= 201103L
  635. // If we're asking for the key we're presumably using the comparison
  636. // object, and so this is a good place to sanity check it.
  637. static_assert(__is_invocable<_Compare&, const _Key&, const _Key&>{},
  638. "comparison object must be invocable "
  639. "with two arguments of key type");
  640. # if __cplusplus >= 201703L
  641. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  642. // 2542. Missing const requirements for associative containers
  643. if constexpr (__is_invocable<_Compare&, const _Key&, const _Key&>{})
  644. static_assert(
  645. is_invocable_v<const _Compare&, const _Key&, const _Key&>,
  646. "comparison object must be invocable as const");
  647. # endif // C++17
  648. #endif // C++11
  649. return _KeyOfValue()(*__x->_M_valptr());
  650. }
  651. static _Link_type
  652. _S_left(_Base_ptr __x) _GLIBCXX_NOEXCEPT
  653. { return static_cast<_Link_type>(__x->_M_left); }
  654. static _Const_Link_type
  655. _S_left(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
  656. { return static_cast<_Const_Link_type>(__x->_M_left); }
  657. static _Link_type
  658. _S_right(_Base_ptr __x) _GLIBCXX_NOEXCEPT
  659. { return static_cast<_Link_type>(__x->_M_right); }
  660. static _Const_Link_type
  661. _S_right(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
  662. { return static_cast<_Const_Link_type>(__x->_M_right); }
  663. static const _Key&
  664. _S_key(_Const_Base_ptr __x)
  665. { return _S_key(static_cast<_Const_Link_type>(__x)); }
  666. static _Base_ptr
  667. _S_minimum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
  668. { return _Rb_tree_node_base::_S_minimum(__x); }
  669. static _Const_Base_ptr
  670. _S_minimum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
  671. { return _Rb_tree_node_base::_S_minimum(__x); }
  672. static _Base_ptr
  673. _S_maximum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
  674. { return _Rb_tree_node_base::_S_maximum(__x); }
  675. static _Const_Base_ptr
  676. _S_maximum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
  677. { return _Rb_tree_node_base::_S_maximum(__x); }
  678. public:
  679. typedef _Rb_tree_iterator<value_type> iterator;
  680. typedef _Rb_tree_const_iterator<value_type> const_iterator;
  681. typedef std::reverse_iterator<iterator> reverse_iterator;
  682. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  683. #if __cplusplus > 201402L
  684. using node_type = _Node_handle<_Key, _Val, _Node_allocator>;
  685. using insert_return_type = _Node_insert_return<
  686. conditional_t<is_same_v<_Key, _Val>, const_iterator, iterator>,
  687. node_type>;
  688. #endif
  689. pair<_Base_ptr, _Base_ptr>
  690. _M_get_insert_unique_pos(const key_type& __k);
  691. pair<_Base_ptr, _Base_ptr>
  692. _M_get_insert_equal_pos(const key_type& __k);
  693. pair<_Base_ptr, _Base_ptr>
  694. _M_get_insert_hint_unique_pos(const_iterator __pos,
  695. const key_type& __k);
  696. pair<_Base_ptr, _Base_ptr>
  697. _M_get_insert_hint_equal_pos(const_iterator __pos,
  698. const key_type& __k);
  699. private:
  700. #if __cplusplus >= 201103L
  701. template<typename _Arg, typename _NodeGen>
  702. iterator
  703. _M_insert_(_Base_ptr __x, _Base_ptr __y, _Arg&& __v, _NodeGen&);
  704. iterator
  705. _M_insert_node(_Base_ptr __x, _Base_ptr __y, _Link_type __z);
  706. template<typename _Arg>
  707. iterator
  708. _M_insert_lower(_Base_ptr __y, _Arg&& __v);
  709. template<typename _Arg>
  710. iterator
  711. _M_insert_equal_lower(_Arg&& __x);
  712. iterator
  713. _M_insert_lower_node(_Base_ptr __p, _Link_type __z);
  714. iterator
  715. _M_insert_equal_lower_node(_Link_type __z);
  716. #else
  717. template<typename _NodeGen>
  718. iterator
  719. _M_insert_(_Base_ptr __x, _Base_ptr __y,
  720. const value_type& __v, _NodeGen&);
  721. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  722. // 233. Insertion hints in associative containers.
  723. iterator
  724. _M_insert_lower(_Base_ptr __y, const value_type& __v);
  725. iterator
  726. _M_insert_equal_lower(const value_type& __x);
  727. #endif
  728. enum { __as_lvalue, __as_rvalue };
  729. template<bool _MoveValues, typename _NodeGen>
  730. _Link_type
  731. _M_copy(_Link_type, _Base_ptr, _NodeGen&);
  732. template<bool _MoveValues, typename _NodeGen>
  733. _Link_type
  734. _M_copy(const _Rb_tree& __x, _NodeGen& __gen)
  735. {
  736. _Link_type __root =
  737. _M_copy<_MoveValues>(__x._M_mbegin(), _M_end(), __gen);
  738. _M_leftmost() = _S_minimum(__root);
  739. _M_rightmost() = _S_maximum(__root);
  740. _M_impl._M_node_count = __x._M_impl._M_node_count;
  741. return __root;
  742. }
  743. _Link_type
  744. _M_copy(const _Rb_tree& __x)
  745. {
  746. _Alloc_node __an(*this);
  747. return _M_copy<__as_lvalue>(__x, __an);
  748. }
  749. void
  750. _M_erase(_Link_type __x);
  751. iterator
  752. _M_lower_bound(_Link_type __x, _Base_ptr __y,
  753. const _Key& __k);
  754. const_iterator
  755. _M_lower_bound(_Const_Link_type __x, _Const_Base_ptr __y,
  756. const _Key& __k) const;
  757. iterator
  758. _M_upper_bound(_Link_type __x, _Base_ptr __y,
  759. const _Key& __k);
  760. const_iterator
  761. _M_upper_bound(_Const_Link_type __x, _Const_Base_ptr __y,
  762. const _Key& __k) const;
  763. public:
  764. // allocation/deallocation
  765. #if __cplusplus < 201103L
  766. _Rb_tree() { }
  767. #else
  768. _Rb_tree() = default;
  769. #endif
  770. _Rb_tree(const _Compare& __comp,
  771. const allocator_type& __a = allocator_type())
  772. : _M_impl(__comp, _Node_allocator(__a)) { }
  773. _Rb_tree(const _Rb_tree& __x)
  774. : _M_impl(__x._M_impl)
  775. {
  776. if (__x._M_root() != 0)
  777. _M_root() = _M_copy(__x);
  778. }
  779. #if __cplusplus >= 201103L
  780. _Rb_tree(const allocator_type& __a)
  781. : _M_impl(_Node_allocator(__a))
  782. { }
  783. _Rb_tree(const _Rb_tree& __x, const allocator_type& __a)
  784. : _M_impl(__x._M_impl._M_key_compare, _Node_allocator(__a))
  785. {
  786. if (__x._M_root() != nullptr)
  787. _M_root() = _M_copy(__x);
  788. }
  789. _Rb_tree(_Rb_tree&&) = default;
  790. _Rb_tree(_Rb_tree&& __x, const allocator_type& __a)
  791. : _Rb_tree(std::move(__x), _Node_allocator(__a))
  792. { }
  793. private:
  794. _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a, true_type)
  795. noexcept(is_nothrow_default_constructible<_Compare>::value)
  796. : _M_impl(std::move(__x._M_impl), std::move(__a))
  797. { }
  798. _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a, false_type)
  799. : _M_impl(__x._M_impl._M_key_compare, std::move(__a))
  800. {
  801. if (__x._M_root() != nullptr)
  802. _M_move_data(__x, false_type{});
  803. }
  804. public:
  805. _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a)
  806. noexcept( noexcept(
  807. _Rb_tree(std::declval<_Rb_tree&&>(), std::declval<_Node_allocator&&>(),
  808. std::declval<typename _Alloc_traits::is_always_equal>())) )
  809. : _Rb_tree(std::move(__x), std::move(__a),
  810. typename _Alloc_traits::is_always_equal{})
  811. { }
  812. #endif
  813. ~_Rb_tree() _GLIBCXX_NOEXCEPT
  814. { _M_erase(_M_begin()); }
  815. _Rb_tree&
  816. operator=(const _Rb_tree& __x);
  817. // Accessors.
  818. _Compare
  819. key_comp() const
  820. { return _M_impl._M_key_compare; }
  821. iterator
  822. begin() _GLIBCXX_NOEXCEPT
  823. { return iterator(this->_M_impl._M_header._M_left); }
  824. const_iterator
  825. begin() const _GLIBCXX_NOEXCEPT
  826. { return const_iterator(this->_M_impl._M_header._M_left); }
  827. iterator
  828. end() _GLIBCXX_NOEXCEPT
  829. { return iterator(&this->_M_impl._M_header); }
  830. const_iterator
  831. end() const _GLIBCXX_NOEXCEPT
  832. { return const_iterator(&this->_M_impl._M_header); }
  833. reverse_iterator
  834. rbegin() _GLIBCXX_NOEXCEPT
  835. { return reverse_iterator(end()); }
  836. const_reverse_iterator
  837. rbegin() const _GLIBCXX_NOEXCEPT
  838. { return const_reverse_iterator(end()); }
  839. reverse_iterator
  840. rend() _GLIBCXX_NOEXCEPT
  841. { return reverse_iterator(begin()); }
  842. const_reverse_iterator
  843. rend() const _GLIBCXX_NOEXCEPT
  844. { return const_reverse_iterator(begin()); }
  845. _GLIBCXX_NODISCARD bool
  846. empty() const _GLIBCXX_NOEXCEPT
  847. { return _M_impl._M_node_count == 0; }
  848. size_type
  849. size() const _GLIBCXX_NOEXCEPT
  850. { return _M_impl._M_node_count; }
  851. size_type
  852. max_size() const _GLIBCXX_NOEXCEPT
  853. { return _Alloc_traits::max_size(_M_get_Node_allocator()); }
  854. void
  855. swap(_Rb_tree& __t)
  856. _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value);
  857. // Insert/erase.
  858. #if __cplusplus >= 201103L
  859. template<typename _Arg>
  860. pair<iterator, bool>
  861. _M_insert_unique(_Arg&& __x);
  862. template<typename _Arg>
  863. iterator
  864. _M_insert_equal(_Arg&& __x);
  865. template<typename _Arg, typename _NodeGen>
  866. iterator
  867. _M_insert_unique_(const_iterator __pos, _Arg&& __x, _NodeGen&);
  868. template<typename _Arg>
  869. iterator
  870. _M_insert_unique_(const_iterator __pos, _Arg&& __x)
  871. {
  872. _Alloc_node __an(*this);
  873. return _M_insert_unique_(__pos, std::forward<_Arg>(__x), __an);
  874. }
  875. template<typename _Arg, typename _NodeGen>
  876. iterator
  877. _M_insert_equal_(const_iterator __pos, _Arg&& __x, _NodeGen&);
  878. template<typename _Arg>
  879. iterator
  880. _M_insert_equal_(const_iterator __pos, _Arg&& __x)
  881. {
  882. _Alloc_node __an(*this);
  883. return _M_insert_equal_(__pos, std::forward<_Arg>(__x), __an);
  884. }
  885. template<typename... _Args>
  886. pair<iterator, bool>
  887. _M_emplace_unique(_Args&&... __args);
  888. template<typename... _Args>
  889. iterator
  890. _M_emplace_equal(_Args&&... __args);
  891. template<typename... _Args>
  892. iterator
  893. _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args);
  894. template<typename... _Args>
  895. iterator
  896. _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args);
  897. template<typename _Iter>
  898. using __same_value_type
  899. = is_same<value_type, typename iterator_traits<_Iter>::value_type>;
  900. template<typename _InputIterator>
  901. __enable_if_t<__same_value_type<_InputIterator>::value>
  902. _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
  903. {
  904. _Alloc_node __an(*this);
  905. for (; __first != __last; ++__first)
  906. _M_insert_unique_(end(), *__first, __an);
  907. }
  908. template<typename _InputIterator>
  909. __enable_if_t<!__same_value_type<_InputIterator>::value>
  910. _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
  911. {
  912. for (; __first != __last; ++__first)
  913. _M_emplace_unique(*__first);
  914. }
  915. template<typename _InputIterator>
  916. __enable_if_t<__same_value_type<_InputIterator>::value>
  917. _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
  918. {
  919. _Alloc_node __an(*this);
  920. for (; __first != __last; ++__first)
  921. _M_insert_equal_(end(), *__first, __an);
  922. }
  923. template<typename _InputIterator>
  924. __enable_if_t<!__same_value_type<_InputIterator>::value>
  925. _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
  926. {
  927. _Alloc_node __an(*this);
  928. for (; __first != __last; ++__first)
  929. _M_emplace_equal(*__first);
  930. }
  931. #else
  932. pair<iterator, bool>
  933. _M_insert_unique(const value_type& __x);
  934. iterator
  935. _M_insert_equal(const value_type& __x);
  936. template<typename _NodeGen>
  937. iterator
  938. _M_insert_unique_(const_iterator __pos, const value_type& __x,
  939. _NodeGen&);
  940. iterator
  941. _M_insert_unique_(const_iterator __pos, const value_type& __x)
  942. {
  943. _Alloc_node __an(*this);
  944. return _M_insert_unique_(__pos, __x, __an);
  945. }
  946. template<typename _NodeGen>
  947. iterator
  948. _M_insert_equal_(const_iterator __pos, const value_type& __x,
  949. _NodeGen&);
  950. iterator
  951. _M_insert_equal_(const_iterator __pos, const value_type& __x)
  952. {
  953. _Alloc_node __an(*this);
  954. return _M_insert_equal_(__pos, __x, __an);
  955. }
  956. template<typename _InputIterator>
  957. void
  958. _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
  959. {
  960. _Alloc_node __an(*this);
  961. for (; __first != __last; ++__first)
  962. _M_insert_unique_(end(), *__first, __an);
  963. }
  964. template<typename _InputIterator>
  965. void
  966. _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
  967. {
  968. _Alloc_node __an(*this);
  969. for (; __first != __last; ++__first)
  970. _M_insert_equal_(end(), *__first, __an);
  971. }
  972. #endif
  973. private:
  974. void
  975. _M_erase_aux(const_iterator __position);
  976. void
  977. _M_erase_aux(const_iterator __first, const_iterator __last);
  978. public:
  979. #if __cplusplus >= 201103L
  980. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  981. // DR 130. Associative erase should return an iterator.
  982. _GLIBCXX_ABI_TAG_CXX11
  983. iterator
  984. erase(const_iterator __position)
  985. {
  986. __glibcxx_assert(__position != end());
  987. const_iterator __result = __position;
  988. ++__result;
  989. _M_erase_aux(__position);
  990. return __result._M_const_cast();
  991. }
  992. // LWG 2059.
  993. _GLIBCXX_ABI_TAG_CXX11
  994. iterator
  995. erase(iterator __position)
  996. {
  997. __glibcxx_assert(__position != end());
  998. iterator __result = __position;
  999. ++__result;
  1000. _M_erase_aux(__position);
  1001. return __result;
  1002. }
  1003. #else
  1004. void
  1005. erase(iterator __position)
  1006. {
  1007. __glibcxx_assert(__position != end());
  1008. _M_erase_aux(__position);
  1009. }
  1010. void
  1011. erase(const_iterator __position)
  1012. {
  1013. __glibcxx_assert(__position != end());
  1014. _M_erase_aux(__position);
  1015. }
  1016. #endif
  1017. size_type
  1018. erase(const key_type& __x);
  1019. #if __cplusplus >= 201103L
  1020. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1021. // DR 130. Associative erase should return an iterator.
  1022. _GLIBCXX_ABI_TAG_CXX11
  1023. iterator
  1024. erase(const_iterator __first, const_iterator __last)
  1025. {
  1026. _M_erase_aux(__first, __last);
  1027. return __last._M_const_cast();
  1028. }
  1029. #else
  1030. void
  1031. erase(iterator __first, iterator __last)
  1032. { _M_erase_aux(__first, __last); }
  1033. void
  1034. erase(const_iterator __first, const_iterator __last)
  1035. { _M_erase_aux(__first, __last); }
  1036. #endif
  1037. void
  1038. clear() _GLIBCXX_NOEXCEPT
  1039. {
  1040. _M_erase(_M_begin());
  1041. _M_impl._M_reset();
  1042. }
  1043. // Set operations.
  1044. iterator
  1045. find(const key_type& __k);
  1046. const_iterator
  1047. find(const key_type& __k) const;
  1048. size_type
  1049. count(const key_type& __k) const;
  1050. iterator
  1051. lower_bound(const key_type& __k)
  1052. { return _M_lower_bound(_M_begin(), _M_end(), __k); }
  1053. const_iterator
  1054. lower_bound(const key_type& __k) const
  1055. { return _M_lower_bound(_M_begin(), _M_end(), __k); }
  1056. iterator
  1057. upper_bound(const key_type& __k)
  1058. { return _M_upper_bound(_M_begin(), _M_end(), __k); }
  1059. const_iterator
  1060. upper_bound(const key_type& __k) const
  1061. { return _M_upper_bound(_M_begin(), _M_end(), __k); }
  1062. pair<iterator, iterator>
  1063. equal_range(const key_type& __k);
  1064. pair<const_iterator, const_iterator>
  1065. equal_range(const key_type& __k) const;
  1066. #if __cplusplus >= 201402L
  1067. template<typename _Kt,
  1068. typename _Req = __has_is_transparent_t<_Compare, _Kt>>
  1069. iterator
  1070. _M_find_tr(const _Kt& __k)
  1071. {
  1072. const _Rb_tree* __const_this = this;
  1073. return __const_this->_M_find_tr(__k)._M_const_cast();
  1074. }
  1075. template<typename _Kt,
  1076. typename _Req = __has_is_transparent_t<_Compare, _Kt>>
  1077. const_iterator
  1078. _M_find_tr(const _Kt& __k) const
  1079. {
  1080. auto __j = _M_lower_bound_tr(__k);
  1081. if (__j != end() && _M_impl._M_key_compare(__k, _S_key(__j._M_node)))
  1082. __j = end();
  1083. return __j;
  1084. }
  1085. template<typename _Kt,
  1086. typename _Req = __has_is_transparent_t<_Compare, _Kt>>
  1087. size_type
  1088. _M_count_tr(const _Kt& __k) const
  1089. {
  1090. auto __p = _M_equal_range_tr(__k);
  1091. return std::distance(__p.first, __p.second);
  1092. }
  1093. template<typename _Kt,
  1094. typename _Req = __has_is_transparent_t<_Compare, _Kt>>
  1095. iterator
  1096. _M_lower_bound_tr(const _Kt& __k)
  1097. {
  1098. const _Rb_tree* __const_this = this;
  1099. return __const_this->_M_lower_bound_tr(__k)._M_const_cast();
  1100. }
  1101. template<typename _Kt,
  1102. typename _Req = __has_is_transparent_t<_Compare, _Kt>>
  1103. const_iterator
  1104. _M_lower_bound_tr(const _Kt& __k) const
  1105. {
  1106. auto __x = _M_begin();
  1107. auto __y = _M_end();
  1108. while (__x != 0)
  1109. if (!_M_impl._M_key_compare(_S_key(__x), __k))
  1110. {
  1111. __y = __x;
  1112. __x = _S_left(__x);
  1113. }
  1114. else
  1115. __x = _S_right(__x);
  1116. return const_iterator(__y);
  1117. }
  1118. template<typename _Kt,
  1119. typename _Req = __has_is_transparent_t<_Compare, _Kt>>
  1120. iterator
  1121. _M_upper_bound_tr(const _Kt& __k)
  1122. {
  1123. const _Rb_tree* __const_this = this;
  1124. return __const_this->_M_upper_bound_tr(__k)._M_const_cast();
  1125. }
  1126. template<typename _Kt,
  1127. typename _Req = __has_is_transparent_t<_Compare, _Kt>>
  1128. const_iterator
  1129. _M_upper_bound_tr(const _Kt& __k) const
  1130. {
  1131. auto __x = _M_begin();
  1132. auto __y = _M_end();
  1133. while (__x != 0)
  1134. if (_M_impl._M_key_compare(__k, _S_key(__x)))
  1135. {
  1136. __y = __x;
  1137. __x = _S_left(__x);
  1138. }
  1139. else
  1140. __x = _S_right(__x);
  1141. return const_iterator(__y);
  1142. }
  1143. template<typename _Kt,
  1144. typename _Req = __has_is_transparent_t<_Compare, _Kt>>
  1145. pair<iterator, iterator>
  1146. _M_equal_range_tr(const _Kt& __k)
  1147. {
  1148. const _Rb_tree* __const_this = this;
  1149. auto __ret = __const_this->_M_equal_range_tr(__k);
  1150. return { __ret.first._M_const_cast(), __ret.second._M_const_cast() };
  1151. }
  1152. template<typename _Kt,
  1153. typename _Req = __has_is_transparent_t<_Compare, _Kt>>
  1154. pair<const_iterator, const_iterator>
  1155. _M_equal_range_tr(const _Kt& __k) const
  1156. {
  1157. auto __low = _M_lower_bound_tr(__k);
  1158. auto __high = __low;
  1159. auto& __cmp = _M_impl._M_key_compare;
  1160. while (__high != end() && !__cmp(__k, _S_key(__high._M_node)))
  1161. ++__high;
  1162. return { __low, __high };
  1163. }
  1164. #endif
  1165. // Debugging.
  1166. bool
  1167. __rb_verify() const;
  1168. #if __cplusplus >= 201103L
  1169. _Rb_tree&
  1170. operator=(_Rb_tree&&)
  1171. noexcept(_Alloc_traits::_S_nothrow_move()
  1172. && is_nothrow_move_assignable<_Compare>::value);
  1173. template<typename _Iterator>
  1174. void
  1175. _M_assign_unique(_Iterator, _Iterator);
  1176. template<typename _Iterator>
  1177. void
  1178. _M_assign_equal(_Iterator, _Iterator);
  1179. private:
  1180. // Move elements from container with equal allocator.
  1181. void
  1182. _M_move_data(_Rb_tree& __x, true_type)
  1183. { _M_impl._M_move_data(__x._M_impl); }
  1184. // Move elements from container with possibly non-equal allocator,
  1185. // which might result in a copy not a move.
  1186. void
  1187. _M_move_data(_Rb_tree&, false_type);
  1188. // Move assignment from container with equal allocator.
  1189. void
  1190. _M_move_assign(_Rb_tree&, true_type);
  1191. // Move assignment from container with possibly non-equal allocator,
  1192. // which might result in a copy not a move.
  1193. void
  1194. _M_move_assign(_Rb_tree&, false_type);
  1195. #endif
  1196. #if __cplusplus > 201402L
  1197. public:
  1198. /// Re-insert an extracted node.
  1199. insert_return_type
  1200. _M_reinsert_node_unique(node_type&& __nh)
  1201. {
  1202. insert_return_type __ret;
  1203. if (__nh.empty())
  1204. __ret.position = end();
  1205. else
  1206. {
  1207. __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
  1208. auto __res = _M_get_insert_unique_pos(__nh._M_key());
  1209. if (__res.second)
  1210. {
  1211. __ret.position
  1212. = _M_insert_node(__res.first, __res.second, __nh._M_ptr);
  1213. __nh._M_ptr = nullptr;
  1214. __ret.inserted = true;
  1215. }
  1216. else
  1217. {
  1218. __ret.node = std::move(__nh);
  1219. __ret.position = iterator(__res.first);
  1220. __ret.inserted = false;
  1221. }
  1222. }
  1223. return __ret;
  1224. }
  1225. /// Re-insert an extracted node.
  1226. iterator
  1227. _M_reinsert_node_equal(node_type&& __nh)
  1228. {
  1229. iterator __ret;
  1230. if (__nh.empty())
  1231. __ret = end();
  1232. else
  1233. {
  1234. __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
  1235. auto __res = _M_get_insert_equal_pos(__nh._M_key());
  1236. if (__res.second)
  1237. __ret = _M_insert_node(__res.first, __res.second, __nh._M_ptr);
  1238. else
  1239. __ret = _M_insert_equal_lower_node(__nh._M_ptr);
  1240. __nh._M_ptr = nullptr;
  1241. }
  1242. return __ret;
  1243. }
  1244. /// Re-insert an extracted node.
  1245. iterator
  1246. _M_reinsert_node_hint_unique(const_iterator __hint, node_type&& __nh)
  1247. {
  1248. iterator __ret;
  1249. if (__nh.empty())
  1250. __ret = end();
  1251. else
  1252. {
  1253. __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
  1254. auto __res = _M_get_insert_hint_unique_pos(__hint, __nh._M_key());
  1255. if (__res.second)
  1256. {
  1257. __ret = _M_insert_node(__res.first, __res.second, __nh._M_ptr);
  1258. __nh._M_ptr = nullptr;
  1259. }
  1260. else
  1261. __ret = iterator(__res.first);
  1262. }
  1263. return __ret;
  1264. }
  1265. /// Re-insert an extracted node.
  1266. iterator
  1267. _M_reinsert_node_hint_equal(const_iterator __hint, node_type&& __nh)
  1268. {
  1269. iterator __ret;
  1270. if (__nh.empty())
  1271. __ret = end();
  1272. else
  1273. {
  1274. __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
  1275. auto __res = _M_get_insert_hint_equal_pos(__hint, __nh._M_key());
  1276. if (__res.second)
  1277. __ret = _M_insert_node(__res.first, __res.second, __nh._M_ptr);
  1278. else
  1279. __ret = _M_insert_equal_lower_node(__nh._M_ptr);
  1280. __nh._M_ptr = nullptr;
  1281. }
  1282. return __ret;
  1283. }
  1284. /// Extract a node.
  1285. node_type
  1286. extract(const_iterator __pos)
  1287. {
  1288. auto __ptr = _Rb_tree_rebalance_for_erase(
  1289. __pos._M_const_cast()._M_node, _M_impl._M_header);
  1290. --_M_impl._M_node_count;
  1291. return { static_cast<_Link_type>(__ptr), _M_get_Node_allocator() };
  1292. }
  1293. /// Extract a node.
  1294. node_type
  1295. extract(const key_type& __k)
  1296. {
  1297. node_type __nh;
  1298. auto __pos = find(__k);
  1299. if (__pos != end())
  1300. __nh = extract(const_iterator(__pos));
  1301. return __nh;
  1302. }
  1303. template<typename _Compare2>
  1304. using _Compatible_tree
  1305. = _Rb_tree<_Key, _Val, _KeyOfValue, _Compare2, _Alloc>;
  1306. template<typename, typename>
  1307. friend class _Rb_tree_merge_helper;
  1308. /// Merge from a compatible container into one with unique keys.
  1309. template<typename _Compare2>
  1310. void
  1311. _M_merge_unique(_Compatible_tree<_Compare2>& __src) noexcept
  1312. {
  1313. using _Merge_helper = _Rb_tree_merge_helper<_Rb_tree, _Compare2>;
  1314. for (auto __i = __src.begin(), __end = __src.end(); __i != __end;)
  1315. {
  1316. auto __pos = __i++;
  1317. auto __res = _M_get_insert_unique_pos(_KeyOfValue()(*__pos));
  1318. if (__res.second)
  1319. {
  1320. auto& __src_impl = _Merge_helper::_S_get_impl(__src);
  1321. auto __ptr = _Rb_tree_rebalance_for_erase(
  1322. __pos._M_node, __src_impl._M_header);
  1323. --__src_impl._M_node_count;
  1324. _M_insert_node(__res.first, __res.second,
  1325. static_cast<_Link_type>(__ptr));
  1326. }
  1327. }
  1328. }
  1329. /// Merge from a compatible container into one with equivalent keys.
  1330. template<typename _Compare2>
  1331. void
  1332. _M_merge_equal(_Compatible_tree<_Compare2>& __src) noexcept
  1333. {
  1334. using _Merge_helper = _Rb_tree_merge_helper<_Rb_tree, _Compare2>;
  1335. for (auto __i = __src.begin(), __end = __src.end(); __i != __end;)
  1336. {
  1337. auto __pos = __i++;
  1338. auto __res = _M_get_insert_equal_pos(_KeyOfValue()(*__pos));
  1339. if (__res.second)
  1340. {
  1341. auto& __src_impl = _Merge_helper::_S_get_impl(__src);
  1342. auto __ptr = _Rb_tree_rebalance_for_erase(
  1343. __pos._M_node, __src_impl._M_header);
  1344. --__src_impl._M_node_count;
  1345. _M_insert_node(__res.first, __res.second,
  1346. static_cast<_Link_type>(__ptr));
  1347. }
  1348. }
  1349. }
  1350. #endif // C++17
  1351. friend bool
  1352. operator==(const _Rb_tree& __x, const _Rb_tree& __y)
  1353. {
  1354. return __x.size() == __y.size()
  1355. && std::equal(__x.begin(), __x.end(), __y.begin());
  1356. }
  1357. #if __cpp_lib_three_way_comparison
  1358. friend auto
  1359. operator<=>(const _Rb_tree& __x, const _Rb_tree& __y)
  1360. {
  1361. if constexpr (requires { typename __detail::__synth3way_t<_Val>; })
  1362. return std::lexicographical_compare_three_way(__x.begin(), __x.end(),
  1363. __y.begin(), __y.end(),
  1364. __detail::__synth3way);
  1365. }
  1366. #else
  1367. friend bool
  1368. operator<(const _Rb_tree& __x, const _Rb_tree& __y)
  1369. {
  1370. return std::lexicographical_compare(__x.begin(), __x.end(),
  1371. __y.begin(), __y.end());
  1372. }
  1373. #endif
  1374. };
  1375. template<typename _Key, typename _Val, typename _KeyOfValue,
  1376. typename _Compare, typename _Alloc>
  1377. inline void
  1378. swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
  1379. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
  1380. { __x.swap(__y); }
  1381. #if __cplusplus >= 201103L
  1382. template<typename _Key, typename _Val, typename _KeyOfValue,
  1383. typename _Compare, typename _Alloc>
  1384. void
  1385. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1386. _M_move_data(_Rb_tree& __x, false_type)
  1387. {
  1388. if (_M_get_Node_allocator() == __x._M_get_Node_allocator())
  1389. _M_move_data(__x, true_type());
  1390. else
  1391. {
  1392. _Alloc_node __an(*this);
  1393. _M_root() =
  1394. _M_copy<!__move_if_noexcept_cond<value_type>::value>(__x, __an);
  1395. }
  1396. }
  1397. template<typename _Key, typename _Val, typename _KeyOfValue,
  1398. typename _Compare, typename _Alloc>
  1399. inline void
  1400. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1401. _M_move_assign(_Rb_tree& __x, true_type)
  1402. {
  1403. clear();
  1404. if (__x._M_root() != nullptr)
  1405. _M_move_data(__x, true_type());
  1406. std::__alloc_on_move(_M_get_Node_allocator(),
  1407. __x._M_get_Node_allocator());
  1408. }
  1409. template<typename _Key, typename _Val, typename _KeyOfValue,
  1410. typename _Compare, typename _Alloc>
  1411. void
  1412. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1413. _M_move_assign(_Rb_tree& __x, false_type)
  1414. {
  1415. if (_M_get_Node_allocator() == __x._M_get_Node_allocator())
  1416. return _M_move_assign(__x, true_type{});
  1417. // Try to move each node reusing existing nodes and copying __x nodes
  1418. // structure.
  1419. _Reuse_or_alloc_node __roan(*this);
  1420. _M_impl._M_reset();
  1421. if (__x._M_root() != nullptr)
  1422. {
  1423. _M_root() = _M_copy<__as_rvalue>(__x, __roan);
  1424. __x.clear();
  1425. }
  1426. }
  1427. template<typename _Key, typename _Val, typename _KeyOfValue,
  1428. typename _Compare, typename _Alloc>
  1429. inline _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
  1430. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1431. operator=(_Rb_tree&& __x)
  1432. noexcept(_Alloc_traits::_S_nothrow_move()
  1433. && is_nothrow_move_assignable<_Compare>::value)
  1434. {
  1435. _M_impl._M_key_compare = std::move(__x._M_impl._M_key_compare);
  1436. _M_move_assign(__x, __bool_constant<_Alloc_traits::_S_nothrow_move()>());
  1437. return *this;
  1438. }
  1439. template<typename _Key, typename _Val, typename _KeyOfValue,
  1440. typename _Compare, typename _Alloc>
  1441. template<typename _Iterator>
  1442. void
  1443. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1444. _M_assign_unique(_Iterator __first, _Iterator __last)
  1445. {
  1446. _Reuse_or_alloc_node __roan(*this);
  1447. _M_impl._M_reset();
  1448. for (; __first != __last; ++__first)
  1449. _M_insert_unique_(end(), *__first, __roan);
  1450. }
  1451. template<typename _Key, typename _Val, typename _KeyOfValue,
  1452. typename _Compare, typename _Alloc>
  1453. template<typename _Iterator>
  1454. void
  1455. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1456. _M_assign_equal(_Iterator __first, _Iterator __last)
  1457. {
  1458. _Reuse_or_alloc_node __roan(*this);
  1459. _M_impl._M_reset();
  1460. for (; __first != __last; ++__first)
  1461. _M_insert_equal_(end(), *__first, __roan);
  1462. }
  1463. #endif
  1464. template<typename _Key, typename _Val, typename _KeyOfValue,
  1465. typename _Compare, typename _Alloc>
  1466. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
  1467. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1468. operator=(const _Rb_tree& __x)
  1469. {
  1470. if (this != &__x)
  1471. {
  1472. // Note that _Key may be a constant type.
  1473. #if __cplusplus >= 201103L
  1474. if (_Alloc_traits::_S_propagate_on_copy_assign())
  1475. {
  1476. auto& __this_alloc = this->_M_get_Node_allocator();
  1477. auto& __that_alloc = __x._M_get_Node_allocator();
  1478. if (!_Alloc_traits::_S_always_equal()
  1479. && __this_alloc != __that_alloc)
  1480. {
  1481. // Replacement allocator cannot free existing storage, we need
  1482. // to erase nodes first.
  1483. clear();
  1484. std::__alloc_on_copy(__this_alloc, __that_alloc);
  1485. }
  1486. }
  1487. #endif
  1488. _Reuse_or_alloc_node __roan(*this);
  1489. _M_impl._M_reset();
  1490. _M_impl._M_key_compare = __x._M_impl._M_key_compare;
  1491. if (__x._M_root() != 0)
  1492. _M_root() = _M_copy<__as_lvalue>(__x, __roan);
  1493. }
  1494. return *this;
  1495. }
  1496. template<typename _Key, typename _Val, typename _KeyOfValue,
  1497. typename _Compare, typename _Alloc>
  1498. #if __cplusplus >= 201103L
  1499. template<typename _Arg, typename _NodeGen>
  1500. #else
  1501. template<typename _NodeGen>
  1502. #endif
  1503. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  1504. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1505. _M_insert_(_Base_ptr __x, _Base_ptr __p,
  1506. #if __cplusplus >= 201103L
  1507. _Arg&& __v,
  1508. #else
  1509. const _Val& __v,
  1510. #endif
  1511. _NodeGen& __node_gen)
  1512. {
  1513. bool __insert_left = (__x != 0 || __p == _M_end()
  1514. || _M_impl._M_key_compare(_KeyOfValue()(__v),
  1515. _S_key(__p)));
  1516. _Link_type __z = __node_gen(_GLIBCXX_FORWARD(_Arg, __v));
  1517. _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
  1518. this->_M_impl._M_header);
  1519. ++_M_impl._M_node_count;
  1520. return iterator(__z);
  1521. }
  1522. template<typename _Key, typename _Val, typename _KeyOfValue,
  1523. typename _Compare, typename _Alloc>
  1524. #if __cplusplus >= 201103L
  1525. template<typename _Arg>
  1526. #endif
  1527. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  1528. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1529. #if __cplusplus >= 201103L
  1530. _M_insert_lower(_Base_ptr __p, _Arg&& __v)
  1531. #else
  1532. _M_insert_lower(_Base_ptr __p, const _Val& __v)
  1533. #endif
  1534. {
  1535. bool __insert_left = (__p == _M_end()
  1536. || !_M_impl._M_key_compare(_S_key(__p),
  1537. _KeyOfValue()(__v)));
  1538. _Link_type __z = _M_create_node(_GLIBCXX_FORWARD(_Arg, __v));
  1539. _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
  1540. this->_M_impl._M_header);
  1541. ++_M_impl._M_node_count;
  1542. return iterator(__z);
  1543. }
  1544. template<typename _Key, typename _Val, typename _KeyOfValue,
  1545. typename _Compare, typename _Alloc>
  1546. #if __cplusplus >= 201103L
  1547. template<typename _Arg>
  1548. #endif
  1549. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  1550. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1551. #if __cplusplus >= 201103L
  1552. _M_insert_equal_lower(_Arg&& __v)
  1553. #else
  1554. _M_insert_equal_lower(const _Val& __v)
  1555. #endif
  1556. {
  1557. _Link_type __x = _M_begin();
  1558. _Base_ptr __y = _M_end();
  1559. while (__x != 0)
  1560. {
  1561. __y = __x;
  1562. __x = !_M_impl._M_key_compare(_S_key(__x), _KeyOfValue()(__v)) ?
  1563. _S_left(__x) : _S_right(__x);
  1564. }
  1565. return _M_insert_lower(__y, _GLIBCXX_FORWARD(_Arg, __v));
  1566. }
  1567. template<typename _Key, typename _Val, typename _KoV,
  1568. typename _Compare, typename _Alloc>
  1569. template<bool _MoveValues, typename _NodeGen>
  1570. typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Link_type
  1571. _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
  1572. _M_copy(_Link_type __x, _Base_ptr __p, _NodeGen& __node_gen)
  1573. {
  1574. // Structural copy. __x and __p must be non-null.
  1575. _Link_type __top = _M_clone_node<_MoveValues>(__x, __node_gen);
  1576. __top->_M_parent = __p;
  1577. __try
  1578. {
  1579. if (__x->_M_right)
  1580. __top->_M_right =
  1581. _M_copy<_MoveValues>(_S_right(__x), __top, __node_gen);
  1582. __p = __top;
  1583. __x = _S_left(__x);
  1584. while (__x != 0)
  1585. {
  1586. _Link_type __y = _M_clone_node<_MoveValues>(__x, __node_gen);
  1587. __p->_M_left = __y;
  1588. __y->_M_parent = __p;
  1589. if (__x->_M_right)
  1590. __y->_M_right = _M_copy<_MoveValues>(_S_right(__x),
  1591. __y, __node_gen);
  1592. __p = __y;
  1593. __x = _S_left(__x);
  1594. }
  1595. }
  1596. __catch(...)
  1597. {
  1598. _M_erase(__top);
  1599. __throw_exception_again;
  1600. }
  1601. return __top;
  1602. }
  1603. template<typename _Key, typename _Val, typename _KeyOfValue,
  1604. typename _Compare, typename _Alloc>
  1605. void
  1606. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1607. _M_erase(_Link_type __x)
  1608. {
  1609. // Erase without rebalancing.
  1610. while (__x != 0)
  1611. {
  1612. _M_erase(_S_right(__x));
  1613. _Link_type __y = _S_left(__x);
  1614. _M_drop_node(__x);
  1615. __x = __y;
  1616. }
  1617. }
  1618. template<typename _Key, typename _Val, typename _KeyOfValue,
  1619. typename _Compare, typename _Alloc>
  1620. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1621. _Compare, _Alloc>::iterator
  1622. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1623. _M_lower_bound(_Link_type __x, _Base_ptr __y,
  1624. const _Key& __k)
  1625. {
  1626. while (__x != 0)
  1627. if (!_M_impl._M_key_compare(_S_key(__x), __k))
  1628. __y = __x, __x = _S_left(__x);
  1629. else
  1630. __x = _S_right(__x);
  1631. return iterator(__y);
  1632. }
  1633. template<typename _Key, typename _Val, typename _KeyOfValue,
  1634. typename _Compare, typename _Alloc>
  1635. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1636. _Compare, _Alloc>::const_iterator
  1637. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1638. _M_lower_bound(_Const_Link_type __x, _Const_Base_ptr __y,
  1639. const _Key& __k) const
  1640. {
  1641. while (__x != 0)
  1642. if (!_M_impl._M_key_compare(_S_key(__x), __k))
  1643. __y = __x, __x = _S_left(__x);
  1644. else
  1645. __x = _S_right(__x);
  1646. return const_iterator(__y);
  1647. }
  1648. template<typename _Key, typename _Val, typename _KeyOfValue,
  1649. typename _Compare, typename _Alloc>
  1650. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1651. _Compare, _Alloc>::iterator
  1652. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1653. _M_upper_bound(_Link_type __x, _Base_ptr __y,
  1654. const _Key& __k)
  1655. {
  1656. while (__x != 0)
  1657. if (_M_impl._M_key_compare(__k, _S_key(__x)))
  1658. __y = __x, __x = _S_left(__x);
  1659. else
  1660. __x = _S_right(__x);
  1661. return iterator(__y);
  1662. }
  1663. template<typename _Key, typename _Val, typename _KeyOfValue,
  1664. typename _Compare, typename _Alloc>
  1665. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1666. _Compare, _Alloc>::const_iterator
  1667. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1668. _M_upper_bound(_Const_Link_type __x, _Const_Base_ptr __y,
  1669. const _Key& __k) const
  1670. {
  1671. while (__x != 0)
  1672. if (_M_impl._M_key_compare(__k, _S_key(__x)))
  1673. __y = __x, __x = _S_left(__x);
  1674. else
  1675. __x = _S_right(__x);
  1676. return const_iterator(__y);
  1677. }
  1678. template<typename _Key, typename _Val, typename _KeyOfValue,
  1679. typename _Compare, typename _Alloc>
  1680. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1681. _Compare, _Alloc>::iterator,
  1682. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1683. _Compare, _Alloc>::iterator>
  1684. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1685. equal_range(const _Key& __k)
  1686. {
  1687. _Link_type __x = _M_begin();
  1688. _Base_ptr __y = _M_end();
  1689. while (__x != 0)
  1690. {
  1691. if (_M_impl._M_key_compare(_S_key(__x), __k))
  1692. __x = _S_right(__x);
  1693. else if (_M_impl._M_key_compare(__k, _S_key(__x)))
  1694. __y = __x, __x = _S_left(__x);
  1695. else
  1696. {
  1697. _Link_type __xu(__x);
  1698. _Base_ptr __yu(__y);
  1699. __y = __x, __x = _S_left(__x);
  1700. __xu = _S_right(__xu);
  1701. return pair<iterator,
  1702. iterator>(_M_lower_bound(__x, __y, __k),
  1703. _M_upper_bound(__xu, __yu, __k));
  1704. }
  1705. }
  1706. return pair<iterator, iterator>(iterator(__y),
  1707. iterator(__y));
  1708. }
  1709. template<typename _Key, typename _Val, typename _KeyOfValue,
  1710. typename _Compare, typename _Alloc>
  1711. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1712. _Compare, _Alloc>::const_iterator,
  1713. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1714. _Compare, _Alloc>::const_iterator>
  1715. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1716. equal_range(const _Key& __k) const
  1717. {
  1718. _Const_Link_type __x = _M_begin();
  1719. _Const_Base_ptr __y = _M_end();
  1720. while (__x != 0)
  1721. {
  1722. if (_M_impl._M_key_compare(_S_key(__x), __k))
  1723. __x = _S_right(__x);
  1724. else if (_M_impl._M_key_compare(__k, _S_key(__x)))
  1725. __y = __x, __x = _S_left(__x);
  1726. else
  1727. {
  1728. _Const_Link_type __xu(__x);
  1729. _Const_Base_ptr __yu(__y);
  1730. __y = __x, __x = _S_left(__x);
  1731. __xu = _S_right(__xu);
  1732. return pair<const_iterator,
  1733. const_iterator>(_M_lower_bound(__x, __y, __k),
  1734. _M_upper_bound(__xu, __yu, __k));
  1735. }
  1736. }
  1737. return pair<const_iterator, const_iterator>(const_iterator(__y),
  1738. const_iterator(__y));
  1739. }
  1740. template<typename _Key, typename _Val, typename _KeyOfValue,
  1741. typename _Compare, typename _Alloc>
  1742. void
  1743. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1744. swap(_Rb_tree& __t)
  1745. _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
  1746. {
  1747. if (_M_root() == 0)
  1748. {
  1749. if (__t._M_root() != 0)
  1750. _M_impl._M_move_data(__t._M_impl);
  1751. }
  1752. else if (__t._M_root() == 0)
  1753. __t._M_impl._M_move_data(_M_impl);
  1754. else
  1755. {
  1756. std::swap(_M_root(),__t._M_root());
  1757. std::swap(_M_leftmost(),__t._M_leftmost());
  1758. std::swap(_M_rightmost(),__t._M_rightmost());
  1759. _M_root()->_M_parent = _M_end();
  1760. __t._M_root()->_M_parent = __t._M_end();
  1761. std::swap(this->_M_impl._M_node_count, __t._M_impl._M_node_count);
  1762. }
  1763. // No need to swap header's color as it does not change.
  1764. std::swap(this->_M_impl._M_key_compare, __t._M_impl._M_key_compare);
  1765. _Alloc_traits::_S_on_swap(_M_get_Node_allocator(),
  1766. __t._M_get_Node_allocator());
  1767. }
  1768. template<typename _Key, typename _Val, typename _KeyOfValue,
  1769. typename _Compare, typename _Alloc>
  1770. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1771. _Compare, _Alloc>::_Base_ptr,
  1772. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1773. _Compare, _Alloc>::_Base_ptr>
  1774. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1775. _M_get_insert_unique_pos(const key_type& __k)
  1776. {
  1777. typedef pair<_Base_ptr, _Base_ptr> _Res;
  1778. _Link_type __x = _M_begin();
  1779. _Base_ptr __y = _M_end();
  1780. bool __comp = true;
  1781. while (__x != 0)
  1782. {
  1783. __y = __x;
  1784. __comp = _M_impl._M_key_compare(__k, _S_key(__x));
  1785. __x = __comp ? _S_left(__x) : _S_right(__x);
  1786. }
  1787. iterator __j = iterator(__y);
  1788. if (__comp)
  1789. {
  1790. if (__j == begin())
  1791. return _Res(__x, __y);
  1792. else
  1793. --__j;
  1794. }
  1795. if (_M_impl._M_key_compare(_S_key(__j._M_node), __k))
  1796. return _Res(__x, __y);
  1797. return _Res(__j._M_node, 0);
  1798. }
  1799. template<typename _Key, typename _Val, typename _KeyOfValue,
  1800. typename _Compare, typename _Alloc>
  1801. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1802. _Compare, _Alloc>::_Base_ptr,
  1803. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1804. _Compare, _Alloc>::_Base_ptr>
  1805. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1806. _M_get_insert_equal_pos(const key_type& __k)
  1807. {
  1808. typedef pair<_Base_ptr, _Base_ptr> _Res;
  1809. _Link_type __x = _M_begin();
  1810. _Base_ptr __y = _M_end();
  1811. while (__x != 0)
  1812. {
  1813. __y = __x;
  1814. __x = _M_impl._M_key_compare(__k, _S_key(__x)) ?
  1815. _S_left(__x) : _S_right(__x);
  1816. }
  1817. return _Res(__x, __y);
  1818. }
  1819. template<typename _Key, typename _Val, typename _KeyOfValue,
  1820. typename _Compare, typename _Alloc>
  1821. #if __cplusplus >= 201103L
  1822. template<typename _Arg>
  1823. #endif
  1824. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1825. _Compare, _Alloc>::iterator, bool>
  1826. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1827. #if __cplusplus >= 201103L
  1828. _M_insert_unique(_Arg&& __v)
  1829. #else
  1830. _M_insert_unique(const _Val& __v)
  1831. #endif
  1832. {
  1833. typedef pair<iterator, bool> _Res;
  1834. pair<_Base_ptr, _Base_ptr> __res
  1835. = _M_get_insert_unique_pos(_KeyOfValue()(__v));
  1836. if (__res.second)
  1837. {
  1838. _Alloc_node __an(*this);
  1839. return _Res(_M_insert_(__res.first, __res.second,
  1840. _GLIBCXX_FORWARD(_Arg, __v), __an),
  1841. true);
  1842. }
  1843. return _Res(iterator(__res.first), false);
  1844. }
  1845. template<typename _Key, typename _Val, typename _KeyOfValue,
  1846. typename _Compare, typename _Alloc>
  1847. #if __cplusplus >= 201103L
  1848. template<typename _Arg>
  1849. #endif
  1850. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  1851. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1852. #if __cplusplus >= 201103L
  1853. _M_insert_equal(_Arg&& __v)
  1854. #else
  1855. _M_insert_equal(const _Val& __v)
  1856. #endif
  1857. {
  1858. pair<_Base_ptr, _Base_ptr> __res
  1859. = _M_get_insert_equal_pos(_KeyOfValue()(__v));
  1860. _Alloc_node __an(*this);
  1861. return _M_insert_(__res.first, __res.second,
  1862. _GLIBCXX_FORWARD(_Arg, __v), __an);
  1863. }
  1864. template<typename _Key, typename _Val, typename _KeyOfValue,
  1865. typename _Compare, typename _Alloc>
  1866. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1867. _Compare, _Alloc>::_Base_ptr,
  1868. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1869. _Compare, _Alloc>::_Base_ptr>
  1870. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1871. _M_get_insert_hint_unique_pos(const_iterator __position,
  1872. const key_type& __k)
  1873. {
  1874. iterator __pos = __position._M_const_cast();
  1875. typedef pair<_Base_ptr, _Base_ptr> _Res;
  1876. // end()
  1877. if (__pos._M_node == _M_end())
  1878. {
  1879. if (size() > 0
  1880. && _M_impl._M_key_compare(_S_key(_M_rightmost()), __k))
  1881. return _Res(0, _M_rightmost());
  1882. else
  1883. return _M_get_insert_unique_pos(__k);
  1884. }
  1885. else if (_M_impl._M_key_compare(__k, _S_key(__pos._M_node)))
  1886. {
  1887. // First, try before...
  1888. iterator __before = __pos;
  1889. if (__pos._M_node == _M_leftmost()) // begin()
  1890. return _Res(_M_leftmost(), _M_leftmost());
  1891. else if (_M_impl._M_key_compare(_S_key((--__before)._M_node), __k))
  1892. {
  1893. if (_S_right(__before._M_node) == 0)
  1894. return _Res(0, __before._M_node);
  1895. else
  1896. return _Res(__pos._M_node, __pos._M_node);
  1897. }
  1898. else
  1899. return _M_get_insert_unique_pos(__k);
  1900. }
  1901. else if (_M_impl._M_key_compare(_S_key(__pos._M_node), __k))
  1902. {
  1903. // ... then try after.
  1904. iterator __after = __pos;
  1905. if (__pos._M_node == _M_rightmost())
  1906. return _Res(0, _M_rightmost());
  1907. else if (_M_impl._M_key_compare(__k, _S_key((++__after)._M_node)))
  1908. {
  1909. if (_S_right(__pos._M_node) == 0)
  1910. return _Res(0, __pos._M_node);
  1911. else
  1912. return _Res(__after._M_node, __after._M_node);
  1913. }
  1914. else
  1915. return _M_get_insert_unique_pos(__k);
  1916. }
  1917. else
  1918. // Equivalent keys.
  1919. return _Res(__pos._M_node, 0);
  1920. }
  1921. template<typename _Key, typename _Val, typename _KeyOfValue,
  1922. typename _Compare, typename _Alloc>
  1923. #if __cplusplus >= 201103L
  1924. template<typename _Arg, typename _NodeGen>
  1925. #else
  1926. template<typename _NodeGen>
  1927. #endif
  1928. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  1929. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1930. _M_insert_unique_(const_iterator __position,
  1931. #if __cplusplus >= 201103L
  1932. _Arg&& __v,
  1933. #else
  1934. const _Val& __v,
  1935. #endif
  1936. _NodeGen& __node_gen)
  1937. {
  1938. pair<_Base_ptr, _Base_ptr> __res
  1939. = _M_get_insert_hint_unique_pos(__position, _KeyOfValue()(__v));
  1940. if (__res.second)
  1941. return _M_insert_(__res.first, __res.second,
  1942. _GLIBCXX_FORWARD(_Arg, __v),
  1943. __node_gen);
  1944. return iterator(__res.first);
  1945. }
  1946. template<typename _Key, typename _Val, typename _KeyOfValue,
  1947. typename _Compare, typename _Alloc>
  1948. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1949. _Compare, _Alloc>::_Base_ptr,
  1950. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1951. _Compare, _Alloc>::_Base_ptr>
  1952. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1953. _M_get_insert_hint_equal_pos(const_iterator __position, const key_type& __k)
  1954. {
  1955. iterator __pos = __position._M_const_cast();
  1956. typedef pair<_Base_ptr, _Base_ptr> _Res;
  1957. // end()
  1958. if (__pos._M_node == _M_end())
  1959. {
  1960. if (size() > 0
  1961. && !_M_impl._M_key_compare(__k, _S_key(_M_rightmost())))
  1962. return _Res(0, _M_rightmost());
  1963. else
  1964. return _M_get_insert_equal_pos(__k);
  1965. }
  1966. else if (!_M_impl._M_key_compare(_S_key(__pos._M_node), __k))
  1967. {
  1968. // First, try before...
  1969. iterator __before = __pos;
  1970. if (__pos._M_node == _M_leftmost()) // begin()
  1971. return _Res(_M_leftmost(), _M_leftmost());
  1972. else if (!_M_impl._M_key_compare(__k, _S_key((--__before)._M_node)))
  1973. {
  1974. if (_S_right(__before._M_node) == 0)
  1975. return _Res(0, __before._M_node);
  1976. else
  1977. return _Res(__pos._M_node, __pos._M_node);
  1978. }
  1979. else
  1980. return _M_get_insert_equal_pos(__k);
  1981. }
  1982. else
  1983. {
  1984. // ... then try after.
  1985. iterator __after = __pos;
  1986. if (__pos._M_node == _M_rightmost())
  1987. return _Res(0, _M_rightmost());
  1988. else if (!_M_impl._M_key_compare(_S_key((++__after)._M_node), __k))
  1989. {
  1990. if (_S_right(__pos._M_node) == 0)
  1991. return _Res(0, __pos._M_node);
  1992. else
  1993. return _Res(__after._M_node, __after._M_node);
  1994. }
  1995. else
  1996. return _Res(0, 0);
  1997. }
  1998. }
  1999. template<typename _Key, typename _Val, typename _KeyOfValue,
  2000. typename _Compare, typename _Alloc>
  2001. #if __cplusplus >= 201103L
  2002. template<typename _Arg, typename _NodeGen>
  2003. #else
  2004. template<typename _NodeGen>
  2005. #endif
  2006. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  2007. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2008. _M_insert_equal_(const_iterator __position,
  2009. #if __cplusplus >= 201103L
  2010. _Arg&& __v,
  2011. #else
  2012. const _Val& __v,
  2013. #endif
  2014. _NodeGen& __node_gen)
  2015. {
  2016. pair<_Base_ptr, _Base_ptr> __res
  2017. = _M_get_insert_hint_equal_pos(__position, _KeyOfValue()(__v));
  2018. if (__res.second)
  2019. return _M_insert_(__res.first, __res.second,
  2020. _GLIBCXX_FORWARD(_Arg, __v),
  2021. __node_gen);
  2022. return _M_insert_equal_lower(_GLIBCXX_FORWARD(_Arg, __v));
  2023. }
  2024. #if __cplusplus >= 201103L
  2025. template<typename _Key, typename _Val, typename _KeyOfValue,
  2026. typename _Compare, typename _Alloc>
  2027. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  2028. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2029. _M_insert_node(_Base_ptr __x, _Base_ptr __p, _Link_type __z)
  2030. {
  2031. bool __insert_left = (__x != 0 || __p == _M_end()
  2032. || _M_impl._M_key_compare(_S_key(__z),
  2033. _S_key(__p)));
  2034. _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
  2035. this->_M_impl._M_header);
  2036. ++_M_impl._M_node_count;
  2037. return iterator(__z);
  2038. }
  2039. template<typename _Key, typename _Val, typename _KeyOfValue,
  2040. typename _Compare, typename _Alloc>
  2041. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  2042. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2043. _M_insert_lower_node(_Base_ptr __p, _Link_type __z)
  2044. {
  2045. bool __insert_left = (__p == _M_end()
  2046. || !_M_impl._M_key_compare(_S_key(__p),
  2047. _S_key(__z)));
  2048. _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
  2049. this->_M_impl._M_header);
  2050. ++_M_impl._M_node_count;
  2051. return iterator(__z);
  2052. }
  2053. template<typename _Key, typename _Val, typename _KeyOfValue,
  2054. typename _Compare, typename _Alloc>
  2055. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  2056. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2057. _M_insert_equal_lower_node(_Link_type __z)
  2058. {
  2059. _Link_type __x = _M_begin();
  2060. _Base_ptr __y = _M_end();
  2061. while (__x != 0)
  2062. {
  2063. __y = __x;
  2064. __x = !_M_impl._M_key_compare(_S_key(__x), _S_key(__z)) ?
  2065. _S_left(__x) : _S_right(__x);
  2066. }
  2067. return _M_insert_lower_node(__y, __z);
  2068. }
  2069. template<typename _Key, typename _Val, typename _KeyOfValue,
  2070. typename _Compare, typename _Alloc>
  2071. template<typename... _Args>
  2072. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  2073. _Compare, _Alloc>::iterator, bool>
  2074. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2075. _M_emplace_unique(_Args&&... __args)
  2076. {
  2077. _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
  2078. __try
  2079. {
  2080. typedef pair<iterator, bool> _Res;
  2081. auto __res = _M_get_insert_unique_pos(_S_key(__z));
  2082. if (__res.second)
  2083. return _Res(_M_insert_node(__res.first, __res.second, __z), true);
  2084. _M_drop_node(__z);
  2085. return _Res(iterator(__res.first), false);
  2086. }
  2087. __catch(...)
  2088. {
  2089. _M_drop_node(__z);
  2090. __throw_exception_again;
  2091. }
  2092. }
  2093. template<typename _Key, typename _Val, typename _KeyOfValue,
  2094. typename _Compare, typename _Alloc>
  2095. template<typename... _Args>
  2096. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  2097. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2098. _M_emplace_equal(_Args&&... __args)
  2099. {
  2100. _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
  2101. __try
  2102. {
  2103. auto __res = _M_get_insert_equal_pos(_S_key(__z));
  2104. return _M_insert_node(__res.first, __res.second, __z);
  2105. }
  2106. __catch(...)
  2107. {
  2108. _M_drop_node(__z);
  2109. __throw_exception_again;
  2110. }
  2111. }
  2112. template<typename _Key, typename _Val, typename _KeyOfValue,
  2113. typename _Compare, typename _Alloc>
  2114. template<typename... _Args>
  2115. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  2116. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2117. _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args)
  2118. {
  2119. _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
  2120. __try
  2121. {
  2122. auto __res = _M_get_insert_hint_unique_pos(__pos, _S_key(__z));
  2123. if (__res.second)
  2124. return _M_insert_node(__res.first, __res.second, __z);
  2125. _M_drop_node(__z);
  2126. return iterator(__res.first);
  2127. }
  2128. __catch(...)
  2129. {
  2130. _M_drop_node(__z);
  2131. __throw_exception_again;
  2132. }
  2133. }
  2134. template<typename _Key, typename _Val, typename _KeyOfValue,
  2135. typename _Compare, typename _Alloc>
  2136. template<typename... _Args>
  2137. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  2138. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2139. _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args)
  2140. {
  2141. _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
  2142. __try
  2143. {
  2144. auto __res = _M_get_insert_hint_equal_pos(__pos, _S_key(__z));
  2145. if (__res.second)
  2146. return _M_insert_node(__res.first, __res.second, __z);
  2147. return _M_insert_equal_lower_node(__z);
  2148. }
  2149. __catch(...)
  2150. {
  2151. _M_drop_node(__z);
  2152. __throw_exception_again;
  2153. }
  2154. }
  2155. #endif
  2156. template<typename _Key, typename _Val, typename _KeyOfValue,
  2157. typename _Compare, typename _Alloc>
  2158. void
  2159. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2160. _M_erase_aux(const_iterator __position)
  2161. {
  2162. _Link_type __y =
  2163. static_cast<_Link_type>(_Rb_tree_rebalance_for_erase
  2164. (const_cast<_Base_ptr>(__position._M_node),
  2165. this->_M_impl._M_header));
  2166. _M_drop_node(__y);
  2167. --_M_impl._M_node_count;
  2168. }
  2169. template<typename _Key, typename _Val, typename _KeyOfValue,
  2170. typename _Compare, typename _Alloc>
  2171. void
  2172. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2173. _M_erase_aux(const_iterator __first, const_iterator __last)
  2174. {
  2175. if (__first == begin() && __last == end())
  2176. clear();
  2177. else
  2178. while (__first != __last)
  2179. _M_erase_aux(__first++);
  2180. }
  2181. template<typename _Key, typename _Val, typename _KeyOfValue,
  2182. typename _Compare, typename _Alloc>
  2183. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
  2184. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2185. erase(const _Key& __x)
  2186. {
  2187. pair<iterator, iterator> __p = equal_range(__x);
  2188. const size_type __old_size = size();
  2189. _M_erase_aux(__p.first, __p.second);
  2190. return __old_size - size();
  2191. }
  2192. template<typename _Key, typename _Val, typename _KeyOfValue,
  2193. typename _Compare, typename _Alloc>
  2194. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  2195. _Compare, _Alloc>::iterator
  2196. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2197. find(const _Key& __k)
  2198. {
  2199. iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
  2200. return (__j == end()
  2201. || _M_impl._M_key_compare(__k,
  2202. _S_key(__j._M_node))) ? end() : __j;
  2203. }
  2204. template<typename _Key, typename _Val, typename _KeyOfValue,
  2205. typename _Compare, typename _Alloc>
  2206. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  2207. _Compare, _Alloc>::const_iterator
  2208. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2209. find(const _Key& __k) const
  2210. {
  2211. const_iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
  2212. return (__j == end()
  2213. || _M_impl._M_key_compare(__k,
  2214. _S_key(__j._M_node))) ? end() : __j;
  2215. }
  2216. template<typename _Key, typename _Val, typename _KeyOfValue,
  2217. typename _Compare, typename _Alloc>
  2218. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
  2219. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2220. count(const _Key& __k) const
  2221. {
  2222. pair<const_iterator, const_iterator> __p = equal_range(__k);
  2223. const size_type __n = std::distance(__p.first, __p.second);
  2224. return __n;
  2225. }
  2226. _GLIBCXX_PURE unsigned int
  2227. _Rb_tree_black_count(const _Rb_tree_node_base* __node,
  2228. const _Rb_tree_node_base* __root) throw ();
  2229. template<typename _Key, typename _Val, typename _KeyOfValue,
  2230. typename _Compare, typename _Alloc>
  2231. bool
  2232. _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const
  2233. {
  2234. if (_M_impl._M_node_count == 0 || begin() == end())
  2235. return _M_impl._M_node_count == 0 && begin() == end()
  2236. && this->_M_impl._M_header._M_left == _M_end()
  2237. && this->_M_impl._M_header._M_right == _M_end();
  2238. unsigned int __len = _Rb_tree_black_count(_M_leftmost(), _M_root());
  2239. for (const_iterator __it = begin(); __it != end(); ++__it)
  2240. {
  2241. _Const_Link_type __x = static_cast<_Const_Link_type>(__it._M_node);
  2242. _Const_Link_type __L = _S_left(__x);
  2243. _Const_Link_type __R = _S_right(__x);
  2244. if (__x->_M_color == _S_red)
  2245. if ((__L && __L->_M_color == _S_red)
  2246. || (__R && __R->_M_color == _S_red))
  2247. return false;
  2248. if (__L && _M_impl._M_key_compare(_S_key(__x), _S_key(__L)))
  2249. return false;
  2250. if (__R && _M_impl._M_key_compare(_S_key(__R), _S_key(__x)))
  2251. return false;
  2252. if (!__L && !__R && _Rb_tree_black_count(__x, _M_root()) != __len)
  2253. return false;
  2254. }
  2255. if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
  2256. return false;
  2257. if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
  2258. return false;
  2259. return true;
  2260. }
  2261. #if __cplusplus > 201402L
  2262. // Allow access to internals of compatible _Rb_tree specializations.
  2263. template<typename _Key, typename _Val, typename _Sel, typename _Cmp1,
  2264. typename _Alloc, typename _Cmp2>
  2265. struct _Rb_tree_merge_helper<_Rb_tree<_Key, _Val, _Sel, _Cmp1, _Alloc>,
  2266. _Cmp2>
  2267. {
  2268. private:
  2269. friend class _Rb_tree<_Key, _Val, _Sel, _Cmp1, _Alloc>;
  2270. static auto&
  2271. _S_get_impl(_Rb_tree<_Key, _Val, _Sel, _Cmp2, _Alloc>& __tree)
  2272. { return __tree._M_impl; }
  2273. };
  2274. #endif // C++17
  2275. _GLIBCXX_END_NAMESPACE_VERSION
  2276. } // namespace
  2277. #endif