stl_tree.h 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622
  1. // RB tree implementation -*- C++ -*-
  2. // Copyright (C) 2001-2022 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 201304L
  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 = __conditional_t<_MoveValue,
  536. value_type&&,
  537. const value_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. private:
  1375. #if __cplusplus >= 201103L
  1376. // An RAII _Node handle
  1377. struct _Auto_node
  1378. {
  1379. template<typename... _Args>
  1380. _Auto_node(_Rb_tree& __t, _Args&&... __args)
  1381. : _M_t(__t),
  1382. _M_node(__t._M_create_node(std::forward<_Args>(__args)...))
  1383. { }
  1384. ~_Auto_node()
  1385. {
  1386. if (_M_node)
  1387. _M_t._M_drop_node(_M_node);
  1388. }
  1389. _Auto_node(_Auto_node&& __n)
  1390. : _M_t(__n._M_t), _M_node(__n._M_node)
  1391. { __n._M_node = nullptr; }
  1392. const _Key&
  1393. _M_key() const
  1394. { return _S_key(_M_node); }
  1395. iterator
  1396. _M_insert(pair<_Base_ptr, _Base_ptr> __p)
  1397. {
  1398. auto __it = _M_t._M_insert_node(__p.first, __p.second, _M_node);
  1399. _M_node = nullptr;
  1400. return __it;
  1401. }
  1402. iterator
  1403. _M_insert_equal_lower()
  1404. {
  1405. auto __it = _M_t._M_insert_equal_lower_node(_M_node);
  1406. _M_node = nullptr;
  1407. return __it;
  1408. }
  1409. _Rb_tree& _M_t;
  1410. _Link_type _M_node;
  1411. };
  1412. #endif // C++11
  1413. };
  1414. template<typename _Key, typename _Val, typename _KeyOfValue,
  1415. typename _Compare, typename _Alloc>
  1416. inline void
  1417. swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
  1418. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
  1419. { __x.swap(__y); }
  1420. #if __cplusplus >= 201103L
  1421. template<typename _Key, typename _Val, typename _KeyOfValue,
  1422. typename _Compare, typename _Alloc>
  1423. void
  1424. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1425. _M_move_data(_Rb_tree& __x, false_type)
  1426. {
  1427. if (_M_get_Node_allocator() == __x._M_get_Node_allocator())
  1428. _M_move_data(__x, true_type());
  1429. else
  1430. {
  1431. constexpr bool __move = !__move_if_noexcept_cond<value_type>::value;
  1432. _Alloc_node __an(*this);
  1433. _M_root() = _M_copy<__move>(__x, __an);
  1434. if _GLIBCXX17_CONSTEXPR (__move)
  1435. __x.clear();
  1436. }
  1437. }
  1438. template<typename _Key, typename _Val, typename _KeyOfValue,
  1439. typename _Compare, typename _Alloc>
  1440. inline void
  1441. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1442. _M_move_assign(_Rb_tree& __x, true_type)
  1443. {
  1444. clear();
  1445. if (__x._M_root() != nullptr)
  1446. _M_move_data(__x, true_type());
  1447. std::__alloc_on_move(_M_get_Node_allocator(),
  1448. __x._M_get_Node_allocator());
  1449. }
  1450. template<typename _Key, typename _Val, typename _KeyOfValue,
  1451. typename _Compare, typename _Alloc>
  1452. void
  1453. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1454. _M_move_assign(_Rb_tree& __x, false_type)
  1455. {
  1456. if (_M_get_Node_allocator() == __x._M_get_Node_allocator())
  1457. return _M_move_assign(__x, true_type{});
  1458. // Try to move each node reusing existing nodes and copying __x nodes
  1459. // structure.
  1460. _Reuse_or_alloc_node __roan(*this);
  1461. _M_impl._M_reset();
  1462. if (__x._M_root() != nullptr)
  1463. {
  1464. _M_root() = _M_copy<__as_rvalue>(__x, __roan);
  1465. __x.clear();
  1466. }
  1467. }
  1468. template<typename _Key, typename _Val, typename _KeyOfValue,
  1469. typename _Compare, typename _Alloc>
  1470. inline _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
  1471. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1472. operator=(_Rb_tree&& __x)
  1473. noexcept(_Alloc_traits::_S_nothrow_move()
  1474. && is_nothrow_move_assignable<_Compare>::value)
  1475. {
  1476. _M_impl._M_key_compare = std::move(__x._M_impl._M_key_compare);
  1477. _M_move_assign(__x, __bool_constant<_Alloc_traits::_S_nothrow_move()>());
  1478. return *this;
  1479. }
  1480. template<typename _Key, typename _Val, typename _KeyOfValue,
  1481. typename _Compare, typename _Alloc>
  1482. template<typename _Iterator>
  1483. void
  1484. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1485. _M_assign_unique(_Iterator __first, _Iterator __last)
  1486. {
  1487. _Reuse_or_alloc_node __roan(*this);
  1488. _M_impl._M_reset();
  1489. for (; __first != __last; ++__first)
  1490. _M_insert_unique_(end(), *__first, __roan);
  1491. }
  1492. template<typename _Key, typename _Val, typename _KeyOfValue,
  1493. typename _Compare, typename _Alloc>
  1494. template<typename _Iterator>
  1495. void
  1496. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1497. _M_assign_equal(_Iterator __first, _Iterator __last)
  1498. {
  1499. _Reuse_or_alloc_node __roan(*this);
  1500. _M_impl._M_reset();
  1501. for (; __first != __last; ++__first)
  1502. _M_insert_equal_(end(), *__first, __roan);
  1503. }
  1504. #endif
  1505. template<typename _Key, typename _Val, typename _KeyOfValue,
  1506. typename _Compare, typename _Alloc>
  1507. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
  1508. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1509. operator=(const _Rb_tree& __x)
  1510. {
  1511. if (this != std::__addressof(__x))
  1512. {
  1513. // Note that _Key may be a constant type.
  1514. #if __cplusplus >= 201103L
  1515. if (_Alloc_traits::_S_propagate_on_copy_assign())
  1516. {
  1517. auto& __this_alloc = this->_M_get_Node_allocator();
  1518. auto& __that_alloc = __x._M_get_Node_allocator();
  1519. if (!_Alloc_traits::_S_always_equal()
  1520. && __this_alloc != __that_alloc)
  1521. {
  1522. // Replacement allocator cannot free existing storage, we need
  1523. // to erase nodes first.
  1524. clear();
  1525. std::__alloc_on_copy(__this_alloc, __that_alloc);
  1526. }
  1527. }
  1528. #endif
  1529. _Reuse_or_alloc_node __roan(*this);
  1530. _M_impl._M_reset();
  1531. _M_impl._M_key_compare = __x._M_impl._M_key_compare;
  1532. if (__x._M_root() != 0)
  1533. _M_root() = _M_copy<__as_lvalue>(__x, __roan);
  1534. }
  1535. return *this;
  1536. }
  1537. template<typename _Key, typename _Val, typename _KeyOfValue,
  1538. typename _Compare, typename _Alloc>
  1539. #if __cplusplus >= 201103L
  1540. template<typename _Arg, typename _NodeGen>
  1541. #else
  1542. template<typename _NodeGen>
  1543. #endif
  1544. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  1545. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1546. _M_insert_(_Base_ptr __x, _Base_ptr __p,
  1547. #if __cplusplus >= 201103L
  1548. _Arg&& __v,
  1549. #else
  1550. const _Val& __v,
  1551. #endif
  1552. _NodeGen& __node_gen)
  1553. {
  1554. bool __insert_left = (__x != 0 || __p == _M_end()
  1555. || _M_impl._M_key_compare(_KeyOfValue()(__v),
  1556. _S_key(__p)));
  1557. _Link_type __z = __node_gen(_GLIBCXX_FORWARD(_Arg, __v));
  1558. _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
  1559. this->_M_impl._M_header);
  1560. ++_M_impl._M_node_count;
  1561. return iterator(__z);
  1562. }
  1563. template<typename _Key, typename _Val, typename _KeyOfValue,
  1564. typename _Compare, typename _Alloc>
  1565. #if __cplusplus >= 201103L
  1566. template<typename _Arg>
  1567. #endif
  1568. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  1569. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1570. #if __cplusplus >= 201103L
  1571. _M_insert_lower(_Base_ptr __p, _Arg&& __v)
  1572. #else
  1573. _M_insert_lower(_Base_ptr __p, const _Val& __v)
  1574. #endif
  1575. {
  1576. bool __insert_left = (__p == _M_end()
  1577. || !_M_impl._M_key_compare(_S_key(__p),
  1578. _KeyOfValue()(__v)));
  1579. _Link_type __z = _M_create_node(_GLIBCXX_FORWARD(_Arg, __v));
  1580. _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
  1581. this->_M_impl._M_header);
  1582. ++_M_impl._M_node_count;
  1583. return iterator(__z);
  1584. }
  1585. template<typename _Key, typename _Val, typename _KeyOfValue,
  1586. typename _Compare, typename _Alloc>
  1587. #if __cplusplus >= 201103L
  1588. template<typename _Arg>
  1589. #endif
  1590. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  1591. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1592. #if __cplusplus >= 201103L
  1593. _M_insert_equal_lower(_Arg&& __v)
  1594. #else
  1595. _M_insert_equal_lower(const _Val& __v)
  1596. #endif
  1597. {
  1598. _Link_type __x = _M_begin();
  1599. _Base_ptr __y = _M_end();
  1600. while (__x != 0)
  1601. {
  1602. __y = __x;
  1603. __x = !_M_impl._M_key_compare(_S_key(__x), _KeyOfValue()(__v)) ?
  1604. _S_left(__x) : _S_right(__x);
  1605. }
  1606. return _M_insert_lower(__y, _GLIBCXX_FORWARD(_Arg, __v));
  1607. }
  1608. template<typename _Key, typename _Val, typename _KoV,
  1609. typename _Compare, typename _Alloc>
  1610. template<bool _MoveValues, typename _NodeGen>
  1611. typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Link_type
  1612. _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
  1613. _M_copy(_Link_type __x, _Base_ptr __p, _NodeGen& __node_gen)
  1614. {
  1615. // Structural copy. __x and __p must be non-null.
  1616. _Link_type __top = _M_clone_node<_MoveValues>(__x, __node_gen);
  1617. __top->_M_parent = __p;
  1618. __try
  1619. {
  1620. if (__x->_M_right)
  1621. __top->_M_right =
  1622. _M_copy<_MoveValues>(_S_right(__x), __top, __node_gen);
  1623. __p = __top;
  1624. __x = _S_left(__x);
  1625. while (__x != 0)
  1626. {
  1627. _Link_type __y = _M_clone_node<_MoveValues>(__x, __node_gen);
  1628. __p->_M_left = __y;
  1629. __y->_M_parent = __p;
  1630. if (__x->_M_right)
  1631. __y->_M_right = _M_copy<_MoveValues>(_S_right(__x),
  1632. __y, __node_gen);
  1633. __p = __y;
  1634. __x = _S_left(__x);
  1635. }
  1636. }
  1637. __catch(...)
  1638. {
  1639. _M_erase(__top);
  1640. __throw_exception_again;
  1641. }
  1642. return __top;
  1643. }
  1644. template<typename _Key, typename _Val, typename _KeyOfValue,
  1645. typename _Compare, typename _Alloc>
  1646. void
  1647. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1648. _M_erase(_Link_type __x)
  1649. {
  1650. // Erase without rebalancing.
  1651. while (__x != 0)
  1652. {
  1653. _M_erase(_S_right(__x));
  1654. _Link_type __y = _S_left(__x);
  1655. _M_drop_node(__x);
  1656. __x = __y;
  1657. }
  1658. }
  1659. template<typename _Key, typename _Val, typename _KeyOfValue,
  1660. typename _Compare, typename _Alloc>
  1661. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1662. _Compare, _Alloc>::iterator
  1663. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1664. _M_lower_bound(_Link_type __x, _Base_ptr __y,
  1665. const _Key& __k)
  1666. {
  1667. while (__x != 0)
  1668. if (!_M_impl._M_key_compare(_S_key(__x), __k))
  1669. __y = __x, __x = _S_left(__x);
  1670. else
  1671. __x = _S_right(__x);
  1672. return iterator(__y);
  1673. }
  1674. template<typename _Key, typename _Val, typename _KeyOfValue,
  1675. typename _Compare, typename _Alloc>
  1676. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1677. _Compare, _Alloc>::const_iterator
  1678. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1679. _M_lower_bound(_Const_Link_type __x, _Const_Base_ptr __y,
  1680. const _Key& __k) const
  1681. {
  1682. while (__x != 0)
  1683. if (!_M_impl._M_key_compare(_S_key(__x), __k))
  1684. __y = __x, __x = _S_left(__x);
  1685. else
  1686. __x = _S_right(__x);
  1687. return const_iterator(__y);
  1688. }
  1689. template<typename _Key, typename _Val, typename _KeyOfValue,
  1690. typename _Compare, typename _Alloc>
  1691. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1692. _Compare, _Alloc>::iterator
  1693. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1694. _M_upper_bound(_Link_type __x, _Base_ptr __y,
  1695. const _Key& __k)
  1696. {
  1697. while (__x != 0)
  1698. if (_M_impl._M_key_compare(__k, _S_key(__x)))
  1699. __y = __x, __x = _S_left(__x);
  1700. else
  1701. __x = _S_right(__x);
  1702. return iterator(__y);
  1703. }
  1704. template<typename _Key, typename _Val, typename _KeyOfValue,
  1705. typename _Compare, typename _Alloc>
  1706. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1707. _Compare, _Alloc>::const_iterator
  1708. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1709. _M_upper_bound(_Const_Link_type __x, _Const_Base_ptr __y,
  1710. const _Key& __k) const
  1711. {
  1712. while (__x != 0)
  1713. if (_M_impl._M_key_compare(__k, _S_key(__x)))
  1714. __y = __x, __x = _S_left(__x);
  1715. else
  1716. __x = _S_right(__x);
  1717. return const_iterator(__y);
  1718. }
  1719. template<typename _Key, typename _Val, typename _KeyOfValue,
  1720. typename _Compare, typename _Alloc>
  1721. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1722. _Compare, _Alloc>::iterator,
  1723. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1724. _Compare, _Alloc>::iterator>
  1725. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1726. equal_range(const _Key& __k)
  1727. {
  1728. _Link_type __x = _M_begin();
  1729. _Base_ptr __y = _M_end();
  1730. while (__x != 0)
  1731. {
  1732. if (_M_impl._M_key_compare(_S_key(__x), __k))
  1733. __x = _S_right(__x);
  1734. else if (_M_impl._M_key_compare(__k, _S_key(__x)))
  1735. __y = __x, __x = _S_left(__x);
  1736. else
  1737. {
  1738. _Link_type __xu(__x);
  1739. _Base_ptr __yu(__y);
  1740. __y = __x, __x = _S_left(__x);
  1741. __xu = _S_right(__xu);
  1742. return pair<iterator,
  1743. iterator>(_M_lower_bound(__x, __y, __k),
  1744. _M_upper_bound(__xu, __yu, __k));
  1745. }
  1746. }
  1747. return pair<iterator, iterator>(iterator(__y),
  1748. iterator(__y));
  1749. }
  1750. template<typename _Key, typename _Val, typename _KeyOfValue,
  1751. typename _Compare, typename _Alloc>
  1752. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1753. _Compare, _Alloc>::const_iterator,
  1754. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1755. _Compare, _Alloc>::const_iterator>
  1756. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1757. equal_range(const _Key& __k) const
  1758. {
  1759. _Const_Link_type __x = _M_begin();
  1760. _Const_Base_ptr __y = _M_end();
  1761. while (__x != 0)
  1762. {
  1763. if (_M_impl._M_key_compare(_S_key(__x), __k))
  1764. __x = _S_right(__x);
  1765. else if (_M_impl._M_key_compare(__k, _S_key(__x)))
  1766. __y = __x, __x = _S_left(__x);
  1767. else
  1768. {
  1769. _Const_Link_type __xu(__x);
  1770. _Const_Base_ptr __yu(__y);
  1771. __y = __x, __x = _S_left(__x);
  1772. __xu = _S_right(__xu);
  1773. return pair<const_iterator,
  1774. const_iterator>(_M_lower_bound(__x, __y, __k),
  1775. _M_upper_bound(__xu, __yu, __k));
  1776. }
  1777. }
  1778. return pair<const_iterator, const_iterator>(const_iterator(__y),
  1779. const_iterator(__y));
  1780. }
  1781. template<typename _Key, typename _Val, typename _KeyOfValue,
  1782. typename _Compare, typename _Alloc>
  1783. void
  1784. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1785. swap(_Rb_tree& __t)
  1786. _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
  1787. {
  1788. if (_M_root() == 0)
  1789. {
  1790. if (__t._M_root() != 0)
  1791. _M_impl._M_move_data(__t._M_impl);
  1792. }
  1793. else if (__t._M_root() == 0)
  1794. __t._M_impl._M_move_data(_M_impl);
  1795. else
  1796. {
  1797. std::swap(_M_root(),__t._M_root());
  1798. std::swap(_M_leftmost(),__t._M_leftmost());
  1799. std::swap(_M_rightmost(),__t._M_rightmost());
  1800. _M_root()->_M_parent = _M_end();
  1801. __t._M_root()->_M_parent = __t._M_end();
  1802. std::swap(this->_M_impl._M_node_count, __t._M_impl._M_node_count);
  1803. }
  1804. // No need to swap header's color as it does not change.
  1805. std::swap(this->_M_impl._M_key_compare, __t._M_impl._M_key_compare);
  1806. _Alloc_traits::_S_on_swap(_M_get_Node_allocator(),
  1807. __t._M_get_Node_allocator());
  1808. }
  1809. template<typename _Key, typename _Val, typename _KeyOfValue,
  1810. typename _Compare, typename _Alloc>
  1811. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1812. _Compare, _Alloc>::_Base_ptr,
  1813. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1814. _Compare, _Alloc>::_Base_ptr>
  1815. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1816. _M_get_insert_unique_pos(const key_type& __k)
  1817. {
  1818. typedef pair<_Base_ptr, _Base_ptr> _Res;
  1819. _Link_type __x = _M_begin();
  1820. _Base_ptr __y = _M_end();
  1821. bool __comp = true;
  1822. while (__x != 0)
  1823. {
  1824. __y = __x;
  1825. __comp = _M_impl._M_key_compare(__k, _S_key(__x));
  1826. __x = __comp ? _S_left(__x) : _S_right(__x);
  1827. }
  1828. iterator __j = iterator(__y);
  1829. if (__comp)
  1830. {
  1831. if (__j == begin())
  1832. return _Res(__x, __y);
  1833. else
  1834. --__j;
  1835. }
  1836. if (_M_impl._M_key_compare(_S_key(__j._M_node), __k))
  1837. return _Res(__x, __y);
  1838. return _Res(__j._M_node, 0);
  1839. }
  1840. template<typename _Key, typename _Val, typename _KeyOfValue,
  1841. typename _Compare, typename _Alloc>
  1842. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1843. _Compare, _Alloc>::_Base_ptr,
  1844. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1845. _Compare, _Alloc>::_Base_ptr>
  1846. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1847. _M_get_insert_equal_pos(const key_type& __k)
  1848. {
  1849. typedef pair<_Base_ptr, _Base_ptr> _Res;
  1850. _Link_type __x = _M_begin();
  1851. _Base_ptr __y = _M_end();
  1852. while (__x != 0)
  1853. {
  1854. __y = __x;
  1855. __x = _M_impl._M_key_compare(__k, _S_key(__x)) ?
  1856. _S_left(__x) : _S_right(__x);
  1857. }
  1858. return _Res(__x, __y);
  1859. }
  1860. template<typename _Key, typename _Val, typename _KeyOfValue,
  1861. typename _Compare, typename _Alloc>
  1862. #if __cplusplus >= 201103L
  1863. template<typename _Arg>
  1864. #endif
  1865. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1866. _Compare, _Alloc>::iterator, bool>
  1867. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1868. #if __cplusplus >= 201103L
  1869. _M_insert_unique(_Arg&& __v)
  1870. #else
  1871. _M_insert_unique(const _Val& __v)
  1872. #endif
  1873. {
  1874. typedef pair<iterator, bool> _Res;
  1875. pair<_Base_ptr, _Base_ptr> __res
  1876. = _M_get_insert_unique_pos(_KeyOfValue()(__v));
  1877. if (__res.second)
  1878. {
  1879. _Alloc_node __an(*this);
  1880. return _Res(_M_insert_(__res.first, __res.second,
  1881. _GLIBCXX_FORWARD(_Arg, __v), __an),
  1882. true);
  1883. }
  1884. return _Res(iterator(__res.first), false);
  1885. }
  1886. template<typename _Key, typename _Val, typename _KeyOfValue,
  1887. typename _Compare, typename _Alloc>
  1888. #if __cplusplus >= 201103L
  1889. template<typename _Arg>
  1890. #endif
  1891. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  1892. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1893. #if __cplusplus >= 201103L
  1894. _M_insert_equal(_Arg&& __v)
  1895. #else
  1896. _M_insert_equal(const _Val& __v)
  1897. #endif
  1898. {
  1899. pair<_Base_ptr, _Base_ptr> __res
  1900. = _M_get_insert_equal_pos(_KeyOfValue()(__v));
  1901. _Alloc_node __an(*this);
  1902. return _M_insert_(__res.first, __res.second,
  1903. _GLIBCXX_FORWARD(_Arg, __v), __an);
  1904. }
  1905. template<typename _Key, typename _Val, typename _KeyOfValue,
  1906. typename _Compare, typename _Alloc>
  1907. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1908. _Compare, _Alloc>::_Base_ptr,
  1909. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1910. _Compare, _Alloc>::_Base_ptr>
  1911. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1912. _M_get_insert_hint_unique_pos(const_iterator __position,
  1913. const key_type& __k)
  1914. {
  1915. iterator __pos = __position._M_const_cast();
  1916. typedef pair<_Base_ptr, _Base_ptr> _Res;
  1917. // end()
  1918. if (__pos._M_node == _M_end())
  1919. {
  1920. if (size() > 0
  1921. && _M_impl._M_key_compare(_S_key(_M_rightmost()), __k))
  1922. return _Res(0, _M_rightmost());
  1923. else
  1924. return _M_get_insert_unique_pos(__k);
  1925. }
  1926. else if (_M_impl._M_key_compare(__k, _S_key(__pos._M_node)))
  1927. {
  1928. // First, try before...
  1929. iterator __before = __pos;
  1930. if (__pos._M_node == _M_leftmost()) // begin()
  1931. return _Res(_M_leftmost(), _M_leftmost());
  1932. else if (_M_impl._M_key_compare(_S_key((--__before)._M_node), __k))
  1933. {
  1934. if (_S_right(__before._M_node) == 0)
  1935. return _Res(0, __before._M_node);
  1936. else
  1937. return _Res(__pos._M_node, __pos._M_node);
  1938. }
  1939. else
  1940. return _M_get_insert_unique_pos(__k);
  1941. }
  1942. else if (_M_impl._M_key_compare(_S_key(__pos._M_node), __k))
  1943. {
  1944. // ... then try after.
  1945. iterator __after = __pos;
  1946. if (__pos._M_node == _M_rightmost())
  1947. return _Res(0, _M_rightmost());
  1948. else if (_M_impl._M_key_compare(__k, _S_key((++__after)._M_node)))
  1949. {
  1950. if (_S_right(__pos._M_node) == 0)
  1951. return _Res(0, __pos._M_node);
  1952. else
  1953. return _Res(__after._M_node, __after._M_node);
  1954. }
  1955. else
  1956. return _M_get_insert_unique_pos(__k);
  1957. }
  1958. else
  1959. // Equivalent keys.
  1960. return _Res(__pos._M_node, 0);
  1961. }
  1962. template<typename _Key, typename _Val, typename _KeyOfValue,
  1963. typename _Compare, typename _Alloc>
  1964. #if __cplusplus >= 201103L
  1965. template<typename _Arg, typename _NodeGen>
  1966. #else
  1967. template<typename _NodeGen>
  1968. #endif
  1969. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  1970. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1971. _M_insert_unique_(const_iterator __position,
  1972. #if __cplusplus >= 201103L
  1973. _Arg&& __v,
  1974. #else
  1975. const _Val& __v,
  1976. #endif
  1977. _NodeGen& __node_gen)
  1978. {
  1979. pair<_Base_ptr, _Base_ptr> __res
  1980. = _M_get_insert_hint_unique_pos(__position, _KeyOfValue()(__v));
  1981. if (__res.second)
  1982. return _M_insert_(__res.first, __res.second,
  1983. _GLIBCXX_FORWARD(_Arg, __v),
  1984. __node_gen);
  1985. return iterator(__res.first);
  1986. }
  1987. template<typename _Key, typename _Val, typename _KeyOfValue,
  1988. typename _Compare, typename _Alloc>
  1989. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1990. _Compare, _Alloc>::_Base_ptr,
  1991. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1992. _Compare, _Alloc>::_Base_ptr>
  1993. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1994. _M_get_insert_hint_equal_pos(const_iterator __position, const key_type& __k)
  1995. {
  1996. iterator __pos = __position._M_const_cast();
  1997. typedef pair<_Base_ptr, _Base_ptr> _Res;
  1998. // end()
  1999. if (__pos._M_node == _M_end())
  2000. {
  2001. if (size() > 0
  2002. && !_M_impl._M_key_compare(__k, _S_key(_M_rightmost())))
  2003. return _Res(0, _M_rightmost());
  2004. else
  2005. return _M_get_insert_equal_pos(__k);
  2006. }
  2007. else if (!_M_impl._M_key_compare(_S_key(__pos._M_node), __k))
  2008. {
  2009. // First, try before...
  2010. iterator __before = __pos;
  2011. if (__pos._M_node == _M_leftmost()) // begin()
  2012. return _Res(_M_leftmost(), _M_leftmost());
  2013. else if (!_M_impl._M_key_compare(__k, _S_key((--__before)._M_node)))
  2014. {
  2015. if (_S_right(__before._M_node) == 0)
  2016. return _Res(0, __before._M_node);
  2017. else
  2018. return _Res(__pos._M_node, __pos._M_node);
  2019. }
  2020. else
  2021. return _M_get_insert_equal_pos(__k);
  2022. }
  2023. else
  2024. {
  2025. // ... then try after.
  2026. iterator __after = __pos;
  2027. if (__pos._M_node == _M_rightmost())
  2028. return _Res(0, _M_rightmost());
  2029. else if (!_M_impl._M_key_compare(_S_key((++__after)._M_node), __k))
  2030. {
  2031. if (_S_right(__pos._M_node) == 0)
  2032. return _Res(0, __pos._M_node);
  2033. else
  2034. return _Res(__after._M_node, __after._M_node);
  2035. }
  2036. else
  2037. return _Res(0, 0);
  2038. }
  2039. }
  2040. template<typename _Key, typename _Val, typename _KeyOfValue,
  2041. typename _Compare, typename _Alloc>
  2042. #if __cplusplus >= 201103L
  2043. template<typename _Arg, typename _NodeGen>
  2044. #else
  2045. template<typename _NodeGen>
  2046. #endif
  2047. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  2048. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2049. _M_insert_equal_(const_iterator __position,
  2050. #if __cplusplus >= 201103L
  2051. _Arg&& __v,
  2052. #else
  2053. const _Val& __v,
  2054. #endif
  2055. _NodeGen& __node_gen)
  2056. {
  2057. pair<_Base_ptr, _Base_ptr> __res
  2058. = _M_get_insert_hint_equal_pos(__position, _KeyOfValue()(__v));
  2059. if (__res.second)
  2060. return _M_insert_(__res.first, __res.second,
  2061. _GLIBCXX_FORWARD(_Arg, __v),
  2062. __node_gen);
  2063. return _M_insert_equal_lower(_GLIBCXX_FORWARD(_Arg, __v));
  2064. }
  2065. #if __cplusplus >= 201103L
  2066. template<typename _Key, typename _Val, typename _KeyOfValue,
  2067. typename _Compare, typename _Alloc>
  2068. auto
  2069. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2070. _M_insert_node(_Base_ptr __x, _Base_ptr __p, _Link_type __z)
  2071. -> iterator
  2072. {
  2073. bool __insert_left = (__x != 0 || __p == _M_end()
  2074. || _M_impl._M_key_compare(_S_key(__z),
  2075. _S_key(__p)));
  2076. _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
  2077. this->_M_impl._M_header);
  2078. ++_M_impl._M_node_count;
  2079. return iterator(__z);
  2080. }
  2081. template<typename _Key, typename _Val, typename _KeyOfValue,
  2082. typename _Compare, typename _Alloc>
  2083. auto
  2084. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2085. _M_insert_lower_node(_Base_ptr __p, _Link_type __z)
  2086. -> iterator
  2087. {
  2088. bool __insert_left = (__p == _M_end()
  2089. || !_M_impl._M_key_compare(_S_key(__p),
  2090. _S_key(__z)));
  2091. _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
  2092. this->_M_impl._M_header);
  2093. ++_M_impl._M_node_count;
  2094. return iterator(__z);
  2095. }
  2096. template<typename _Key, typename _Val, typename _KeyOfValue,
  2097. typename _Compare, typename _Alloc>
  2098. auto
  2099. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2100. _M_insert_equal_lower_node(_Link_type __z)
  2101. -> iterator
  2102. {
  2103. _Link_type __x = _M_begin();
  2104. _Base_ptr __y = _M_end();
  2105. while (__x != 0)
  2106. {
  2107. __y = __x;
  2108. __x = !_M_impl._M_key_compare(_S_key(__x), _S_key(__z)) ?
  2109. _S_left(__x) : _S_right(__x);
  2110. }
  2111. return _M_insert_lower_node(__y, __z);
  2112. }
  2113. template<typename _Key, typename _Val, typename _KeyOfValue,
  2114. typename _Compare, typename _Alloc>
  2115. template<typename... _Args>
  2116. auto
  2117. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2118. _M_emplace_unique(_Args&&... __args)
  2119. -> pair<iterator, bool>
  2120. {
  2121. _Auto_node __z(*this, std::forward<_Args>(__args)...);
  2122. auto __res = _M_get_insert_unique_pos(__z._M_key());
  2123. if (__res.second)
  2124. return {__z._M_insert(__res), true};
  2125. return {iterator(__res.first), false};
  2126. }
  2127. template<typename _Key, typename _Val, typename _KeyOfValue,
  2128. typename _Compare, typename _Alloc>
  2129. template<typename... _Args>
  2130. auto
  2131. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2132. _M_emplace_equal(_Args&&... __args)
  2133. -> iterator
  2134. {
  2135. _Auto_node __z(*this, std::forward<_Args>(__args)...);
  2136. auto __res = _M_get_insert_equal_pos(__z._M_key());
  2137. return __z._M_insert(__res);
  2138. }
  2139. template<typename _Key, typename _Val, typename _KeyOfValue,
  2140. typename _Compare, typename _Alloc>
  2141. template<typename... _Args>
  2142. auto
  2143. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2144. _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args)
  2145. -> iterator
  2146. {
  2147. _Auto_node __z(*this, std::forward<_Args>(__args)...);
  2148. auto __res = _M_get_insert_hint_unique_pos(__pos, __z._M_key());
  2149. if (__res.second)
  2150. return __z._M_insert(__res);
  2151. return iterator(__res.first);
  2152. }
  2153. template<typename _Key, typename _Val, typename _KeyOfValue,
  2154. typename _Compare, typename _Alloc>
  2155. template<typename... _Args>
  2156. auto
  2157. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2158. _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args)
  2159. -> iterator
  2160. {
  2161. _Auto_node __z(*this, std::forward<_Args>(__args)...);
  2162. auto __res = _M_get_insert_hint_equal_pos(__pos, __z._M_key());
  2163. if (__res.second)
  2164. return __z._M_insert(__res);
  2165. return __z._M_insert_equal_lower();
  2166. }
  2167. #endif
  2168. template<typename _Key, typename _Val, typename _KeyOfValue,
  2169. typename _Compare, typename _Alloc>
  2170. void
  2171. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2172. _M_erase_aux(const_iterator __position)
  2173. {
  2174. _Link_type __y =
  2175. static_cast<_Link_type>(_Rb_tree_rebalance_for_erase
  2176. (const_cast<_Base_ptr>(__position._M_node),
  2177. this->_M_impl._M_header));
  2178. _M_drop_node(__y);
  2179. --_M_impl._M_node_count;
  2180. }
  2181. template<typename _Key, typename _Val, typename _KeyOfValue,
  2182. typename _Compare, typename _Alloc>
  2183. void
  2184. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2185. _M_erase_aux(const_iterator __first, const_iterator __last)
  2186. {
  2187. if (__first == begin() && __last == end())
  2188. clear();
  2189. else
  2190. while (__first != __last)
  2191. _M_erase_aux(__first++);
  2192. }
  2193. template<typename _Key, typename _Val, typename _KeyOfValue,
  2194. typename _Compare, typename _Alloc>
  2195. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
  2196. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2197. erase(const _Key& __x)
  2198. {
  2199. pair<iterator, iterator> __p = equal_range(__x);
  2200. const size_type __old_size = size();
  2201. _M_erase_aux(__p.first, __p.second);
  2202. return __old_size - size();
  2203. }
  2204. template<typename _Key, typename _Val, typename _KeyOfValue,
  2205. typename _Compare, typename _Alloc>
  2206. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  2207. _Compare, _Alloc>::iterator
  2208. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2209. find(const _Key& __k)
  2210. {
  2211. 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,
  2219. _Compare, _Alloc>::const_iterator
  2220. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2221. find(const _Key& __k) const
  2222. {
  2223. const_iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
  2224. return (__j == end()
  2225. || _M_impl._M_key_compare(__k,
  2226. _S_key(__j._M_node))) ? end() : __j;
  2227. }
  2228. template<typename _Key, typename _Val, typename _KeyOfValue,
  2229. typename _Compare, typename _Alloc>
  2230. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
  2231. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2232. count(const _Key& __k) const
  2233. {
  2234. pair<const_iterator, const_iterator> __p = equal_range(__k);
  2235. const size_type __n = std::distance(__p.first, __p.second);
  2236. return __n;
  2237. }
  2238. _GLIBCXX_PURE unsigned int
  2239. _Rb_tree_black_count(const _Rb_tree_node_base* __node,
  2240. const _Rb_tree_node_base* __root) throw ();
  2241. template<typename _Key, typename _Val, typename _KeyOfValue,
  2242. typename _Compare, typename _Alloc>
  2243. bool
  2244. _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const
  2245. {
  2246. if (_M_impl._M_node_count == 0 || begin() == end())
  2247. return _M_impl._M_node_count == 0 && begin() == end()
  2248. && this->_M_impl._M_header._M_left == _M_end()
  2249. && this->_M_impl._M_header._M_right == _M_end();
  2250. unsigned int __len = _Rb_tree_black_count(_M_leftmost(), _M_root());
  2251. for (const_iterator __it = begin(); __it != end(); ++__it)
  2252. {
  2253. _Const_Link_type __x = static_cast<_Const_Link_type>(__it._M_node);
  2254. _Const_Link_type __L = _S_left(__x);
  2255. _Const_Link_type __R = _S_right(__x);
  2256. if (__x->_M_color == _S_red)
  2257. if ((__L && __L->_M_color == _S_red)
  2258. || (__R && __R->_M_color == _S_red))
  2259. return false;
  2260. if (__L && _M_impl._M_key_compare(_S_key(__x), _S_key(__L)))
  2261. return false;
  2262. if (__R && _M_impl._M_key_compare(_S_key(__R), _S_key(__x)))
  2263. return false;
  2264. if (!__L && !__R && _Rb_tree_black_count(__x, _M_root()) != __len)
  2265. return false;
  2266. }
  2267. if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
  2268. return false;
  2269. if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
  2270. return false;
  2271. return true;
  2272. }
  2273. #if __cplusplus > 201402L
  2274. // Allow access to internals of compatible _Rb_tree specializations.
  2275. template<typename _Key, typename _Val, typename _Sel, typename _Cmp1,
  2276. typename _Alloc, typename _Cmp2>
  2277. struct _Rb_tree_merge_helper<_Rb_tree<_Key, _Val, _Sel, _Cmp1, _Alloc>,
  2278. _Cmp2>
  2279. {
  2280. private:
  2281. friend class _Rb_tree<_Key, _Val, _Sel, _Cmp1, _Alloc>;
  2282. static auto&
  2283. _S_get_impl(_Rb_tree<_Key, _Val, _Sel, _Cmp2, _Alloc>& __tree)
  2284. { return __tree._M_impl; }
  2285. };
  2286. #endif // C++17
  2287. _GLIBCXX_END_NAMESPACE_VERSION
  2288. } // namespace
  2289. #endif