You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3 年之前
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644
  1. // RB tree implementation -*- C++ -*-
  2. // Copyright (C) 2001-2020 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 _Cmp, typename _SfinaeType, typename = __void_t<>>
  346. struct __has_is_transparent
  347. { };
  348. template<typename _Cmp, typename _SfinaeType>
  349. struct __has_is_transparent<_Cmp, _SfinaeType,
  350. __void_t<typename _Cmp::is_transparent>>
  351. { typedef void type; };
  352. template<typename _Cmp, typename _SfinaeType>
  353. using __has_is_transparent_t
  354. = typename __has_is_transparent<_Cmp, _SfinaeType>::type;
  355. #endif
  356. #if __cplusplus > 201402L
  357. template<typename _Tree1, typename _Cmp2>
  358. struct _Rb_tree_merge_helper { };
  359. #endif
  360. template<typename _Key, typename _Val, typename _KeyOfValue,
  361. typename _Compare, typename _Alloc = allocator<_Val> >
  362. class _Rb_tree
  363. {
  364. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  365. rebind<_Rb_tree_node<_Val> >::other _Node_allocator;
  366. typedef __gnu_cxx::__alloc_traits<_Node_allocator> _Alloc_traits;
  367. protected:
  368. typedef _Rb_tree_node_base* _Base_ptr;
  369. typedef const _Rb_tree_node_base* _Const_Base_ptr;
  370. typedef _Rb_tree_node<_Val>* _Link_type;
  371. typedef const _Rb_tree_node<_Val>* _Const_Link_type;
  372. private:
  373. // Functor recycling a pool of nodes and using allocation once the pool
  374. // is empty.
  375. struct _Reuse_or_alloc_node
  376. {
  377. _Reuse_or_alloc_node(_Rb_tree& __t)
  378. : _M_root(__t._M_root()), _M_nodes(__t._M_rightmost()), _M_t(__t)
  379. {
  380. if (_M_root)
  381. {
  382. _M_root->_M_parent = 0;
  383. if (_M_nodes->_M_left)
  384. _M_nodes = _M_nodes->_M_left;
  385. }
  386. else
  387. _M_nodes = 0;
  388. }
  389. #if __cplusplus >= 201103L
  390. _Reuse_or_alloc_node(const _Reuse_or_alloc_node&) = delete;
  391. #endif
  392. ~_Reuse_or_alloc_node()
  393. { _M_t._M_erase(static_cast<_Link_type>(_M_root)); }
  394. template<typename _Arg>
  395. _Link_type
  396. #if __cplusplus < 201103L
  397. operator()(const _Arg& __arg)
  398. #else
  399. operator()(_Arg&& __arg)
  400. #endif
  401. {
  402. _Link_type __node = static_cast<_Link_type>(_M_extract());
  403. if (__node)
  404. {
  405. _M_t._M_destroy_node(__node);
  406. _M_t._M_construct_node(__node, _GLIBCXX_FORWARD(_Arg, __arg));
  407. return __node;
  408. }
  409. return _M_t._M_create_node(_GLIBCXX_FORWARD(_Arg, __arg));
  410. }
  411. private:
  412. _Base_ptr
  413. _M_extract()
  414. {
  415. if (!_M_nodes)
  416. return _M_nodes;
  417. _Base_ptr __node = _M_nodes;
  418. _M_nodes = _M_nodes->_M_parent;
  419. if (_M_nodes)
  420. {
  421. if (_M_nodes->_M_right == __node)
  422. {
  423. _M_nodes->_M_right = 0;
  424. if (_M_nodes->_M_left)
  425. {
  426. _M_nodes = _M_nodes->_M_left;
  427. while (_M_nodes->_M_right)
  428. _M_nodes = _M_nodes->_M_right;
  429. if (_M_nodes->_M_left)
  430. _M_nodes = _M_nodes->_M_left;
  431. }
  432. }
  433. else // __node is on the left.
  434. _M_nodes->_M_left = 0;
  435. }
  436. else
  437. _M_root = 0;
  438. return __node;
  439. }
  440. _Base_ptr _M_root;
  441. _Base_ptr _M_nodes;
  442. _Rb_tree& _M_t;
  443. };
  444. // Functor similar to the previous one but without any pool of nodes to
  445. // recycle.
  446. struct _Alloc_node
  447. {
  448. _Alloc_node(_Rb_tree& __t)
  449. : _M_t(__t) { }
  450. template<typename _Arg>
  451. _Link_type
  452. #if __cplusplus < 201103L
  453. operator()(const _Arg& __arg) const
  454. #else
  455. operator()(_Arg&& __arg) const
  456. #endif
  457. { return _M_t._M_create_node(_GLIBCXX_FORWARD(_Arg, __arg)); }
  458. private:
  459. _Rb_tree& _M_t;
  460. };
  461. public:
  462. typedef _Key key_type;
  463. typedef _Val value_type;
  464. typedef value_type* pointer;
  465. typedef const value_type* const_pointer;
  466. typedef value_type& reference;
  467. typedef const value_type& const_reference;
  468. typedef size_t size_type;
  469. typedef ptrdiff_t difference_type;
  470. typedef _Alloc allocator_type;
  471. _Node_allocator&
  472. _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
  473. { return this->_M_impl; }
  474. const _Node_allocator&
  475. _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
  476. { return this->_M_impl; }
  477. allocator_type
  478. get_allocator() const _GLIBCXX_NOEXCEPT
  479. { return allocator_type(_M_get_Node_allocator()); }
  480. protected:
  481. _Link_type
  482. _M_get_node()
  483. { return _Alloc_traits::allocate(_M_get_Node_allocator(), 1); }
  484. void
  485. _M_put_node(_Link_type __p) _GLIBCXX_NOEXCEPT
  486. { _Alloc_traits::deallocate(_M_get_Node_allocator(), __p, 1); }
  487. #if __cplusplus < 201103L
  488. void
  489. _M_construct_node(_Link_type __node, const value_type& __x)
  490. {
  491. __try
  492. { get_allocator().construct(__node->_M_valptr(), __x); }
  493. __catch(...)
  494. {
  495. _M_put_node(__node);
  496. __throw_exception_again;
  497. }
  498. }
  499. _Link_type
  500. _M_create_node(const value_type& __x)
  501. {
  502. _Link_type __tmp = _M_get_node();
  503. _M_construct_node(__tmp, __x);
  504. return __tmp;
  505. }
  506. #else
  507. template<typename... _Args>
  508. void
  509. _M_construct_node(_Link_type __node, _Args&&... __args)
  510. {
  511. __try
  512. {
  513. ::new(__node) _Rb_tree_node<_Val>;
  514. _Alloc_traits::construct(_M_get_Node_allocator(),
  515. __node->_M_valptr(),
  516. std::forward<_Args>(__args)...);
  517. }
  518. __catch(...)
  519. {
  520. __node->~_Rb_tree_node<_Val>();
  521. _M_put_node(__node);
  522. __throw_exception_again;
  523. }
  524. }
  525. template<typename... _Args>
  526. _Link_type
  527. _M_create_node(_Args&&... __args)
  528. {
  529. _Link_type __tmp = _M_get_node();
  530. _M_construct_node(__tmp, std::forward<_Args>(__args)...);
  531. return __tmp;
  532. }
  533. #endif
  534. void
  535. _M_destroy_node(_Link_type __p) _GLIBCXX_NOEXCEPT
  536. {
  537. #if __cplusplus < 201103L
  538. get_allocator().destroy(__p->_M_valptr());
  539. #else
  540. _Alloc_traits::destroy(_M_get_Node_allocator(), __p->_M_valptr());
  541. __p->~_Rb_tree_node<_Val>();
  542. #endif
  543. }
  544. void
  545. _M_drop_node(_Link_type __p) _GLIBCXX_NOEXCEPT
  546. {
  547. _M_destroy_node(__p);
  548. _M_put_node(__p);
  549. }
  550. template<typename _NodeGen>
  551. _Link_type
  552. _M_clone_node(_Const_Link_type __x, _NodeGen& __node_gen)
  553. {
  554. _Link_type __tmp = __node_gen(*__x->_M_valptr());
  555. __tmp->_M_color = __x->_M_color;
  556. __tmp->_M_left = 0;
  557. __tmp->_M_right = 0;
  558. return __tmp;
  559. }
  560. protected:
  561. #if _GLIBCXX_INLINE_VERSION
  562. template<typename _Key_compare>
  563. #else
  564. // Unused _Is_pod_comparator is kept as it is part of mangled name.
  565. template<typename _Key_compare,
  566. bool /* _Is_pod_comparator */ = __is_pod(_Key_compare)>
  567. #endif
  568. struct _Rb_tree_impl
  569. : public _Node_allocator
  570. , public _Rb_tree_key_compare<_Key_compare>
  571. , public _Rb_tree_header
  572. {
  573. typedef _Rb_tree_key_compare<_Key_compare> _Base_key_compare;
  574. _Rb_tree_impl()
  575. _GLIBCXX_NOEXCEPT_IF(
  576. is_nothrow_default_constructible<_Node_allocator>::value
  577. && is_nothrow_default_constructible<_Base_key_compare>::value )
  578. : _Node_allocator()
  579. { }
  580. _Rb_tree_impl(const _Rb_tree_impl& __x)
  581. : _Node_allocator(_Alloc_traits::_S_select_on_copy(__x))
  582. , _Base_key_compare(__x._M_key_compare)
  583. { }
  584. #if __cplusplus < 201103L
  585. _Rb_tree_impl(const _Key_compare& __comp, const _Node_allocator& __a)
  586. : _Node_allocator(__a), _Base_key_compare(__comp)
  587. { }
  588. #else
  589. _Rb_tree_impl(_Rb_tree_impl&&) = default;
  590. explicit
  591. _Rb_tree_impl(_Node_allocator&& __a)
  592. : _Node_allocator(std::move(__a))
  593. { }
  594. _Rb_tree_impl(_Rb_tree_impl&& __x, _Node_allocator&& __a)
  595. : _Node_allocator(std::move(__a)),
  596. _Base_key_compare(std::move(__x)),
  597. _Rb_tree_header(std::move(__x))
  598. { }
  599. _Rb_tree_impl(const _Key_compare& __comp, _Node_allocator&& __a)
  600. : _Node_allocator(std::move(__a)), _Base_key_compare(__comp)
  601. { }
  602. #endif
  603. };
  604. _Rb_tree_impl<_Compare> _M_impl;
  605. protected:
  606. _Base_ptr&
  607. _M_root() _GLIBCXX_NOEXCEPT
  608. { return this->_M_impl._M_header._M_parent; }
  609. _Const_Base_ptr
  610. _M_root() const _GLIBCXX_NOEXCEPT
  611. { return this->_M_impl._M_header._M_parent; }
  612. _Base_ptr&
  613. _M_leftmost() _GLIBCXX_NOEXCEPT
  614. { return this->_M_impl._M_header._M_left; }
  615. _Const_Base_ptr
  616. _M_leftmost() const _GLIBCXX_NOEXCEPT
  617. { return this->_M_impl._M_header._M_left; }
  618. _Base_ptr&
  619. _M_rightmost() _GLIBCXX_NOEXCEPT
  620. { return this->_M_impl._M_header._M_right; }
  621. _Const_Base_ptr
  622. _M_rightmost() const _GLIBCXX_NOEXCEPT
  623. { return this->_M_impl._M_header._M_right; }
  624. _Link_type
  625. _M_begin() _GLIBCXX_NOEXCEPT
  626. { return static_cast<_Link_type>(this->_M_impl._M_header._M_parent); }
  627. _Const_Link_type
  628. _M_begin() const _GLIBCXX_NOEXCEPT
  629. {
  630. return static_cast<_Const_Link_type>
  631. (this->_M_impl._M_header._M_parent);
  632. }
  633. _Base_ptr
  634. _M_end() _GLIBCXX_NOEXCEPT
  635. { return &this->_M_impl._M_header; }
  636. _Const_Base_ptr
  637. _M_end() const _GLIBCXX_NOEXCEPT
  638. { return &this->_M_impl._M_header; }
  639. static const _Key&
  640. _S_key(_Const_Link_type __x)
  641. {
  642. #if __cplusplus >= 201103L
  643. // If we're asking for the key we're presumably using the comparison
  644. // object, and so this is a good place to sanity check it.
  645. static_assert(__is_invocable<_Compare&, const _Key&, const _Key&>{},
  646. "comparison object must be invocable "
  647. "with two arguments of key type");
  648. # if __cplusplus >= 201703L
  649. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  650. // 2542. Missing const requirements for associative containers
  651. if constexpr (__is_invocable<_Compare&, const _Key&, const _Key&>{})
  652. static_assert(
  653. is_invocable_v<const _Compare&, const _Key&, const _Key&>,
  654. "comparison object must be invocable as const");
  655. # endif // C++17
  656. #endif // C++11
  657. return _KeyOfValue()(*__x->_M_valptr());
  658. }
  659. static _Link_type
  660. _S_left(_Base_ptr __x) _GLIBCXX_NOEXCEPT
  661. { return static_cast<_Link_type>(__x->_M_left); }
  662. static _Const_Link_type
  663. _S_left(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
  664. { return static_cast<_Const_Link_type>(__x->_M_left); }
  665. static _Link_type
  666. _S_right(_Base_ptr __x) _GLIBCXX_NOEXCEPT
  667. { return static_cast<_Link_type>(__x->_M_right); }
  668. static _Const_Link_type
  669. _S_right(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
  670. { return static_cast<_Const_Link_type>(__x->_M_right); }
  671. static const _Key&
  672. _S_key(_Const_Base_ptr __x)
  673. { return _S_key(static_cast<_Const_Link_type>(__x)); }
  674. static _Base_ptr
  675. _S_minimum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
  676. { return _Rb_tree_node_base::_S_minimum(__x); }
  677. static _Const_Base_ptr
  678. _S_minimum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
  679. { return _Rb_tree_node_base::_S_minimum(__x); }
  680. static _Base_ptr
  681. _S_maximum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
  682. { return _Rb_tree_node_base::_S_maximum(__x); }
  683. static _Const_Base_ptr
  684. _S_maximum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
  685. { return _Rb_tree_node_base::_S_maximum(__x); }
  686. public:
  687. typedef _Rb_tree_iterator<value_type> iterator;
  688. typedef _Rb_tree_const_iterator<value_type> const_iterator;
  689. typedef std::reverse_iterator<iterator> reverse_iterator;
  690. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  691. #if __cplusplus > 201402L
  692. using node_type = _Node_handle<_Key, _Val, _Node_allocator>;
  693. using insert_return_type = _Node_insert_return<
  694. conditional_t<is_same_v<_Key, _Val>, const_iterator, iterator>,
  695. node_type>;
  696. #endif
  697. pair<_Base_ptr, _Base_ptr>
  698. _M_get_insert_unique_pos(const key_type& __k);
  699. pair<_Base_ptr, _Base_ptr>
  700. _M_get_insert_equal_pos(const key_type& __k);
  701. pair<_Base_ptr, _Base_ptr>
  702. _M_get_insert_hint_unique_pos(const_iterator __pos,
  703. const key_type& __k);
  704. pair<_Base_ptr, _Base_ptr>
  705. _M_get_insert_hint_equal_pos(const_iterator __pos,
  706. const key_type& __k);
  707. private:
  708. #if __cplusplus >= 201103L
  709. template<typename _Arg, typename _NodeGen>
  710. iterator
  711. _M_insert_(_Base_ptr __x, _Base_ptr __y, _Arg&& __v, _NodeGen&);
  712. iterator
  713. _M_insert_node(_Base_ptr __x, _Base_ptr __y, _Link_type __z);
  714. template<typename _Arg>
  715. iterator
  716. _M_insert_lower(_Base_ptr __y, _Arg&& __v);
  717. template<typename _Arg>
  718. iterator
  719. _M_insert_equal_lower(_Arg&& __x);
  720. iterator
  721. _M_insert_lower_node(_Base_ptr __p, _Link_type __z);
  722. iterator
  723. _M_insert_equal_lower_node(_Link_type __z);
  724. #else
  725. template<typename _NodeGen>
  726. iterator
  727. _M_insert_(_Base_ptr __x, _Base_ptr __y,
  728. const value_type& __v, _NodeGen&);
  729. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  730. // 233. Insertion hints in associative containers.
  731. iterator
  732. _M_insert_lower(_Base_ptr __y, const value_type& __v);
  733. iterator
  734. _M_insert_equal_lower(const value_type& __x);
  735. #endif
  736. template<typename _NodeGen>
  737. _Link_type
  738. _M_copy(_Const_Link_type __x, _Base_ptr __p, _NodeGen&);
  739. template<typename _NodeGen>
  740. _Link_type
  741. _M_copy(const _Rb_tree& __x, _NodeGen& __gen)
  742. {
  743. _Link_type __root = _M_copy(__x._M_begin(), _M_end(), __gen);
  744. _M_leftmost() = _S_minimum(__root);
  745. _M_rightmost() = _S_maximum(__root);
  746. _M_impl._M_node_count = __x._M_impl._M_node_count;
  747. return __root;
  748. }
  749. _Link_type
  750. _M_copy(const _Rb_tree& __x)
  751. {
  752. _Alloc_node __an(*this);
  753. return _M_copy(__x, __an);
  754. }
  755. void
  756. _M_erase(_Link_type __x);
  757. iterator
  758. _M_lower_bound(_Link_type __x, _Base_ptr __y,
  759. const _Key& __k);
  760. const_iterator
  761. _M_lower_bound(_Const_Link_type __x, _Const_Base_ptr __y,
  762. const _Key& __k) const;
  763. iterator
  764. _M_upper_bound(_Link_type __x, _Base_ptr __y,
  765. const _Key& __k);
  766. const_iterator
  767. _M_upper_bound(_Const_Link_type __x, _Const_Base_ptr __y,
  768. const _Key& __k) const;
  769. public:
  770. // allocation/deallocation
  771. #if __cplusplus < 201103L
  772. _Rb_tree() { }
  773. #else
  774. _Rb_tree() = default;
  775. #endif
  776. _Rb_tree(const _Compare& __comp,
  777. const allocator_type& __a = allocator_type())
  778. : _M_impl(__comp, _Node_allocator(__a)) { }
  779. _Rb_tree(const _Rb_tree& __x)
  780. : _M_impl(__x._M_impl)
  781. {
  782. if (__x._M_root() != 0)
  783. _M_root() = _M_copy(__x);
  784. }
  785. #if __cplusplus >= 201103L
  786. _Rb_tree(const allocator_type& __a)
  787. : _M_impl(_Node_allocator(__a))
  788. { }
  789. _Rb_tree(const _Rb_tree& __x, const allocator_type& __a)
  790. : _M_impl(__x._M_impl._M_key_compare, _Node_allocator(__a))
  791. {
  792. if (__x._M_root() != nullptr)
  793. _M_root() = _M_copy(__x);
  794. }
  795. _Rb_tree(_Rb_tree&&) = default;
  796. _Rb_tree(_Rb_tree&& __x, const allocator_type& __a)
  797. : _Rb_tree(std::move(__x), _Node_allocator(__a))
  798. { }
  799. private:
  800. _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a, true_type)
  801. noexcept(is_nothrow_default_constructible<_Compare>::value)
  802. : _M_impl(std::move(__x._M_impl), std::move(__a))
  803. { }
  804. _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a, false_type)
  805. : _M_impl(__x._M_impl._M_key_compare, std::move(__a))
  806. {
  807. if (__x._M_root() != nullptr)
  808. _M_move_data(__x, false_type{});
  809. }
  810. public:
  811. _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a)
  812. noexcept( noexcept(
  813. _Rb_tree(std::declval<_Rb_tree&&>(), std::declval<_Node_allocator&&>(),
  814. std::declval<typename _Alloc_traits::is_always_equal>())) )
  815. : _Rb_tree(std::move(__x), std::move(__a),
  816. typename _Alloc_traits::is_always_equal{})
  817. { }
  818. #endif
  819. ~_Rb_tree() _GLIBCXX_NOEXCEPT
  820. { _M_erase(_M_begin()); }
  821. _Rb_tree&
  822. operator=(const _Rb_tree& __x);
  823. // Accessors.
  824. _Compare
  825. key_comp() const
  826. { return _M_impl._M_key_compare; }
  827. iterator
  828. begin() _GLIBCXX_NOEXCEPT
  829. { return iterator(this->_M_impl._M_header._M_left); }
  830. const_iterator
  831. begin() const _GLIBCXX_NOEXCEPT
  832. { return const_iterator(this->_M_impl._M_header._M_left); }
  833. iterator
  834. end() _GLIBCXX_NOEXCEPT
  835. { return iterator(&this->_M_impl._M_header); }
  836. const_iterator
  837. end() const _GLIBCXX_NOEXCEPT
  838. { return const_iterator(&this->_M_impl._M_header); }
  839. reverse_iterator
  840. rbegin() _GLIBCXX_NOEXCEPT
  841. { return reverse_iterator(end()); }
  842. const_reverse_iterator
  843. rbegin() const _GLIBCXX_NOEXCEPT
  844. { return const_reverse_iterator(end()); }
  845. reverse_iterator
  846. rend() _GLIBCXX_NOEXCEPT
  847. { return reverse_iterator(begin()); }
  848. const_reverse_iterator
  849. rend() const _GLIBCXX_NOEXCEPT
  850. { return const_reverse_iterator(begin()); }
  851. _GLIBCXX_NODISCARD bool
  852. empty() const _GLIBCXX_NOEXCEPT
  853. { return _M_impl._M_node_count == 0; }
  854. size_type
  855. size() const _GLIBCXX_NOEXCEPT
  856. { return _M_impl._M_node_count; }
  857. size_type
  858. max_size() const _GLIBCXX_NOEXCEPT
  859. { return _Alloc_traits::max_size(_M_get_Node_allocator()); }
  860. void
  861. swap(_Rb_tree& __t)
  862. _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value);
  863. // Insert/erase.
  864. #if __cplusplus >= 201103L
  865. template<typename _Arg>
  866. pair<iterator, bool>
  867. _M_insert_unique(_Arg&& __x);
  868. template<typename _Arg>
  869. iterator
  870. _M_insert_equal(_Arg&& __x);
  871. template<typename _Arg, typename _NodeGen>
  872. iterator
  873. _M_insert_unique_(const_iterator __pos, _Arg&& __x, _NodeGen&);
  874. template<typename _Arg>
  875. iterator
  876. _M_insert_unique_(const_iterator __pos, _Arg&& __x)
  877. {
  878. _Alloc_node __an(*this);
  879. return _M_insert_unique_(__pos, std::forward<_Arg>(__x), __an);
  880. }
  881. template<typename _Arg, typename _NodeGen>
  882. iterator
  883. _M_insert_equal_(const_iterator __pos, _Arg&& __x, _NodeGen&);
  884. template<typename _Arg>
  885. iterator
  886. _M_insert_equal_(const_iterator __pos, _Arg&& __x)
  887. {
  888. _Alloc_node __an(*this);
  889. return _M_insert_equal_(__pos, std::forward<_Arg>(__x), __an);
  890. }
  891. template<typename... _Args>
  892. pair<iterator, bool>
  893. _M_emplace_unique(_Args&&... __args);
  894. template<typename... _Args>
  895. iterator
  896. _M_emplace_equal(_Args&&... __args);
  897. template<typename... _Args>
  898. iterator
  899. _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args);
  900. template<typename... _Args>
  901. iterator
  902. _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args);
  903. template<typename _Iter>
  904. using __same_value_type
  905. = is_same<value_type, typename iterator_traits<_Iter>::value_type>;
  906. template<typename _InputIterator>
  907. __enable_if_t<__same_value_type<_InputIterator>::value>
  908. _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
  909. {
  910. _Alloc_node __an(*this);
  911. for (; __first != __last; ++__first)
  912. _M_insert_unique_(end(), *__first, __an);
  913. }
  914. template<typename _InputIterator>
  915. __enable_if_t<!__same_value_type<_InputIterator>::value>
  916. _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
  917. {
  918. for (; __first != __last; ++__first)
  919. _M_emplace_unique(*__first);
  920. }
  921. template<typename _InputIterator>
  922. __enable_if_t<__same_value_type<_InputIterator>::value>
  923. _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
  924. {
  925. _Alloc_node __an(*this);
  926. for (; __first != __last; ++__first)
  927. _M_insert_equal_(end(), *__first, __an);
  928. }
  929. template<typename _InputIterator>
  930. __enable_if_t<!__same_value_type<_InputIterator>::value>
  931. _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
  932. {
  933. _Alloc_node __an(*this);
  934. for (; __first != __last; ++__first)
  935. _M_emplace_equal(*__first);
  936. }
  937. #else
  938. pair<iterator, bool>
  939. _M_insert_unique(const value_type& __x);
  940. iterator
  941. _M_insert_equal(const value_type& __x);
  942. template<typename _NodeGen>
  943. iterator
  944. _M_insert_unique_(const_iterator __pos, const value_type& __x,
  945. _NodeGen&);
  946. iterator
  947. _M_insert_unique_(const_iterator __pos, const value_type& __x)
  948. {
  949. _Alloc_node __an(*this);
  950. return _M_insert_unique_(__pos, __x, __an);
  951. }
  952. template<typename _NodeGen>
  953. iterator
  954. _M_insert_equal_(const_iterator __pos, const value_type& __x,
  955. _NodeGen&);
  956. iterator
  957. _M_insert_equal_(const_iterator __pos, const value_type& __x)
  958. {
  959. _Alloc_node __an(*this);
  960. return _M_insert_equal_(__pos, __x, __an);
  961. }
  962. template<typename _InputIterator>
  963. void
  964. _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
  965. {
  966. _Alloc_node __an(*this);
  967. for (; __first != __last; ++__first)
  968. _M_insert_unique_(end(), *__first, __an);
  969. }
  970. template<typename _InputIterator>
  971. void
  972. _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
  973. {
  974. _Alloc_node __an(*this);
  975. for (; __first != __last; ++__first)
  976. _M_insert_equal_(end(), *__first, __an);
  977. }
  978. #endif
  979. private:
  980. void
  981. _M_erase_aux(const_iterator __position);
  982. void
  983. _M_erase_aux(const_iterator __first, const_iterator __last);
  984. public:
  985. #if __cplusplus >= 201103L
  986. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  987. // DR 130. Associative erase should return an iterator.
  988. _GLIBCXX_ABI_TAG_CXX11
  989. iterator
  990. erase(const_iterator __position)
  991. {
  992. __glibcxx_assert(__position != end());
  993. const_iterator __result = __position;
  994. ++__result;
  995. _M_erase_aux(__position);
  996. return __result._M_const_cast();
  997. }
  998. // LWG 2059.
  999. _GLIBCXX_ABI_TAG_CXX11
  1000. iterator
  1001. erase(iterator __position)
  1002. {
  1003. __glibcxx_assert(__position != end());
  1004. iterator __result = __position;
  1005. ++__result;
  1006. _M_erase_aux(__position);
  1007. return __result;
  1008. }
  1009. #else
  1010. void
  1011. erase(iterator __position)
  1012. {
  1013. __glibcxx_assert(__position != end());
  1014. _M_erase_aux(__position);
  1015. }
  1016. void
  1017. erase(const_iterator __position)
  1018. {
  1019. __glibcxx_assert(__position != end());
  1020. _M_erase_aux(__position);
  1021. }
  1022. #endif
  1023. size_type
  1024. erase(const key_type& __x);
  1025. #if __cplusplus >= 201103L
  1026. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1027. // DR 130. Associative erase should return an iterator.
  1028. _GLIBCXX_ABI_TAG_CXX11
  1029. iterator
  1030. erase(const_iterator __first, const_iterator __last)
  1031. {
  1032. _M_erase_aux(__first, __last);
  1033. return __last._M_const_cast();
  1034. }
  1035. #else
  1036. void
  1037. erase(iterator __first, iterator __last)
  1038. { _M_erase_aux(__first, __last); }
  1039. void
  1040. erase(const_iterator __first, const_iterator __last)
  1041. { _M_erase_aux(__first, __last); }
  1042. #endif
  1043. void
  1044. clear() _GLIBCXX_NOEXCEPT
  1045. {
  1046. _M_erase(_M_begin());
  1047. _M_impl._M_reset();
  1048. }
  1049. // Set operations.
  1050. iterator
  1051. find(const key_type& __k);
  1052. const_iterator
  1053. find(const key_type& __k) const;
  1054. size_type
  1055. count(const key_type& __k) const;
  1056. iterator
  1057. lower_bound(const key_type& __k)
  1058. { return _M_lower_bound(_M_begin(), _M_end(), __k); }
  1059. const_iterator
  1060. lower_bound(const key_type& __k) const
  1061. { return _M_lower_bound(_M_begin(), _M_end(), __k); }
  1062. iterator
  1063. upper_bound(const key_type& __k)
  1064. { return _M_upper_bound(_M_begin(), _M_end(), __k); }
  1065. const_iterator
  1066. upper_bound(const key_type& __k) const
  1067. { return _M_upper_bound(_M_begin(), _M_end(), __k); }
  1068. pair<iterator, iterator>
  1069. equal_range(const key_type& __k);
  1070. pair<const_iterator, const_iterator>
  1071. equal_range(const key_type& __k) const;
  1072. #if __cplusplus >= 201402L
  1073. template<typename _Kt,
  1074. typename _Req = __has_is_transparent_t<_Compare, _Kt>>
  1075. iterator
  1076. _M_find_tr(const _Kt& __k)
  1077. {
  1078. const _Rb_tree* __const_this = this;
  1079. return __const_this->_M_find_tr(__k)._M_const_cast();
  1080. }
  1081. template<typename _Kt,
  1082. typename _Req = __has_is_transparent_t<_Compare, _Kt>>
  1083. const_iterator
  1084. _M_find_tr(const _Kt& __k) const
  1085. {
  1086. auto __j = _M_lower_bound_tr(__k);
  1087. if (__j != end() && _M_impl._M_key_compare(__k, _S_key(__j._M_node)))
  1088. __j = end();
  1089. return __j;
  1090. }
  1091. template<typename _Kt,
  1092. typename _Req = __has_is_transparent_t<_Compare, _Kt>>
  1093. size_type
  1094. _M_count_tr(const _Kt& __k) const
  1095. {
  1096. auto __p = _M_equal_range_tr(__k);
  1097. return std::distance(__p.first, __p.second);
  1098. }
  1099. template<typename _Kt,
  1100. typename _Req = __has_is_transparent_t<_Compare, _Kt>>
  1101. iterator
  1102. _M_lower_bound_tr(const _Kt& __k)
  1103. {
  1104. const _Rb_tree* __const_this = this;
  1105. return __const_this->_M_lower_bound_tr(__k)._M_const_cast();
  1106. }
  1107. template<typename _Kt,
  1108. typename _Req = __has_is_transparent_t<_Compare, _Kt>>
  1109. const_iterator
  1110. _M_lower_bound_tr(const _Kt& __k) const
  1111. {
  1112. auto __x = _M_begin();
  1113. auto __y = _M_end();
  1114. while (__x != 0)
  1115. if (!_M_impl._M_key_compare(_S_key(__x), __k))
  1116. {
  1117. __y = __x;
  1118. __x = _S_left(__x);
  1119. }
  1120. else
  1121. __x = _S_right(__x);
  1122. return const_iterator(__y);
  1123. }
  1124. template<typename _Kt,
  1125. typename _Req = __has_is_transparent_t<_Compare, _Kt>>
  1126. iterator
  1127. _M_upper_bound_tr(const _Kt& __k)
  1128. {
  1129. const _Rb_tree* __const_this = this;
  1130. return __const_this->_M_upper_bound_tr(__k)._M_const_cast();
  1131. }
  1132. template<typename _Kt,
  1133. typename _Req = __has_is_transparent_t<_Compare, _Kt>>
  1134. const_iterator
  1135. _M_upper_bound_tr(const _Kt& __k) const
  1136. {
  1137. auto __x = _M_begin();
  1138. auto __y = _M_end();
  1139. while (__x != 0)
  1140. if (_M_impl._M_key_compare(__k, _S_key(__x)))
  1141. {
  1142. __y = __x;
  1143. __x = _S_left(__x);
  1144. }
  1145. else
  1146. __x = _S_right(__x);
  1147. return const_iterator(__y);
  1148. }
  1149. template<typename _Kt,
  1150. typename _Req = __has_is_transparent_t<_Compare, _Kt>>
  1151. pair<iterator, iterator>
  1152. _M_equal_range_tr(const _Kt& __k)
  1153. {
  1154. const _Rb_tree* __const_this = this;
  1155. auto __ret = __const_this->_M_equal_range_tr(__k);
  1156. return { __ret.first._M_const_cast(), __ret.second._M_const_cast() };
  1157. }
  1158. template<typename _Kt,
  1159. typename _Req = __has_is_transparent_t<_Compare, _Kt>>
  1160. pair<const_iterator, const_iterator>
  1161. _M_equal_range_tr(const _Kt& __k) const
  1162. {
  1163. auto __low = _M_lower_bound_tr(__k);
  1164. auto __high = __low;
  1165. auto& __cmp = _M_impl._M_key_compare;
  1166. while (__high != end() && !__cmp(__k, _S_key(__high._M_node)))
  1167. ++__high;
  1168. return { __low, __high };
  1169. }
  1170. #endif
  1171. // Debugging.
  1172. bool
  1173. __rb_verify() const;
  1174. #if __cplusplus >= 201103L
  1175. _Rb_tree&
  1176. operator=(_Rb_tree&&)
  1177. noexcept(_Alloc_traits::_S_nothrow_move()
  1178. && is_nothrow_move_assignable<_Compare>::value);
  1179. template<typename _Iterator>
  1180. void
  1181. _M_assign_unique(_Iterator, _Iterator);
  1182. template<typename _Iterator>
  1183. void
  1184. _M_assign_equal(_Iterator, _Iterator);
  1185. private:
  1186. // Move elements from container with equal allocator.
  1187. void
  1188. _M_move_data(_Rb_tree& __x, true_type)
  1189. { _M_impl._M_move_data(__x._M_impl); }
  1190. // Move elements from container with possibly non-equal allocator,
  1191. // which might result in a copy not a move.
  1192. void
  1193. _M_move_data(_Rb_tree&, false_type);
  1194. // Move assignment from container with equal allocator.
  1195. void
  1196. _M_move_assign(_Rb_tree&, true_type);
  1197. // Move assignment from container with possibly non-equal allocator,
  1198. // which might result in a copy not a move.
  1199. void
  1200. _M_move_assign(_Rb_tree&, false_type);
  1201. #endif
  1202. #if __cplusplus > 201402L
  1203. public:
  1204. /// Re-insert an extracted node.
  1205. insert_return_type
  1206. _M_reinsert_node_unique(node_type&& __nh)
  1207. {
  1208. insert_return_type __ret;
  1209. if (__nh.empty())
  1210. __ret.position = end();
  1211. else
  1212. {
  1213. __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
  1214. auto __res = _M_get_insert_unique_pos(__nh._M_key());
  1215. if (__res.second)
  1216. {
  1217. __ret.position
  1218. = _M_insert_node(__res.first, __res.second, __nh._M_ptr);
  1219. __nh._M_ptr = nullptr;
  1220. __ret.inserted = true;
  1221. }
  1222. else
  1223. {
  1224. __ret.node = std::move(__nh);
  1225. __ret.position = iterator(__res.first);
  1226. __ret.inserted = false;
  1227. }
  1228. }
  1229. return __ret;
  1230. }
  1231. /// Re-insert an extracted node.
  1232. iterator
  1233. _M_reinsert_node_equal(node_type&& __nh)
  1234. {
  1235. iterator __ret;
  1236. if (__nh.empty())
  1237. __ret = end();
  1238. else
  1239. {
  1240. __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
  1241. auto __res = _M_get_insert_equal_pos(__nh._M_key());
  1242. if (__res.second)
  1243. __ret = _M_insert_node(__res.first, __res.second, __nh._M_ptr);
  1244. else
  1245. __ret = _M_insert_equal_lower_node(__nh._M_ptr);
  1246. __nh._M_ptr = nullptr;
  1247. }
  1248. return __ret;
  1249. }
  1250. /// Re-insert an extracted node.
  1251. iterator
  1252. _M_reinsert_node_hint_unique(const_iterator __hint, node_type&& __nh)
  1253. {
  1254. iterator __ret;
  1255. if (__nh.empty())
  1256. __ret = end();
  1257. else
  1258. {
  1259. __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
  1260. auto __res = _M_get_insert_hint_unique_pos(__hint, __nh._M_key());
  1261. if (__res.second)
  1262. {
  1263. __ret = _M_insert_node(__res.first, __res.second, __nh._M_ptr);
  1264. __nh._M_ptr = nullptr;
  1265. }
  1266. else
  1267. __ret = iterator(__res.first);
  1268. }
  1269. return __ret;
  1270. }
  1271. /// Re-insert an extracted node.
  1272. iterator
  1273. _M_reinsert_node_hint_equal(const_iterator __hint, node_type&& __nh)
  1274. {
  1275. iterator __ret;
  1276. if (__nh.empty())
  1277. __ret = end();
  1278. else
  1279. {
  1280. __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
  1281. auto __res = _M_get_insert_hint_equal_pos(__hint, __nh._M_key());
  1282. if (__res.second)
  1283. __ret = _M_insert_node(__res.first, __res.second, __nh._M_ptr);
  1284. else
  1285. __ret = _M_insert_equal_lower_node(__nh._M_ptr);
  1286. __nh._M_ptr = nullptr;
  1287. }
  1288. return __ret;
  1289. }
  1290. /// Extract a node.
  1291. node_type
  1292. extract(const_iterator __pos)
  1293. {
  1294. auto __ptr = _Rb_tree_rebalance_for_erase(
  1295. __pos._M_const_cast()._M_node, _M_impl._M_header);
  1296. --_M_impl._M_node_count;
  1297. return { static_cast<_Link_type>(__ptr), _M_get_Node_allocator() };
  1298. }
  1299. /// Extract a node.
  1300. node_type
  1301. extract(const key_type& __k)
  1302. {
  1303. node_type __nh;
  1304. auto __pos = find(__k);
  1305. if (__pos != end())
  1306. __nh = extract(const_iterator(__pos));
  1307. return __nh;
  1308. }
  1309. template<typename _Compare2>
  1310. using _Compatible_tree
  1311. = _Rb_tree<_Key, _Val, _KeyOfValue, _Compare2, _Alloc>;
  1312. template<typename, typename>
  1313. friend class _Rb_tree_merge_helper;
  1314. /// Merge from a compatible container into one with unique keys.
  1315. template<typename _Compare2>
  1316. void
  1317. _M_merge_unique(_Compatible_tree<_Compare2>& __src) noexcept
  1318. {
  1319. using _Merge_helper = _Rb_tree_merge_helper<_Rb_tree, _Compare2>;
  1320. for (auto __i = __src.begin(), __end = __src.end(); __i != __end;)
  1321. {
  1322. auto __pos = __i++;
  1323. auto __res = _M_get_insert_unique_pos(_KeyOfValue()(*__pos));
  1324. if (__res.second)
  1325. {
  1326. auto& __src_impl = _Merge_helper::_S_get_impl(__src);
  1327. auto __ptr = _Rb_tree_rebalance_for_erase(
  1328. __pos._M_node, __src_impl._M_header);
  1329. --__src_impl._M_node_count;
  1330. _M_insert_node(__res.first, __res.second,
  1331. static_cast<_Link_type>(__ptr));
  1332. }
  1333. }
  1334. }
  1335. /// Merge from a compatible container into one with equivalent keys.
  1336. template<typename _Compare2>
  1337. void
  1338. _M_merge_equal(_Compatible_tree<_Compare2>& __src) noexcept
  1339. {
  1340. using _Merge_helper = _Rb_tree_merge_helper<_Rb_tree, _Compare2>;
  1341. for (auto __i = __src.begin(), __end = __src.end(); __i != __end;)
  1342. {
  1343. auto __pos = __i++;
  1344. auto __res = _M_get_insert_equal_pos(_KeyOfValue()(*__pos));
  1345. if (__res.second)
  1346. {
  1347. auto& __src_impl = _Merge_helper::_S_get_impl(__src);
  1348. auto __ptr = _Rb_tree_rebalance_for_erase(
  1349. __pos._M_node, __src_impl._M_header);
  1350. --__src_impl._M_node_count;
  1351. _M_insert_node(__res.first, __res.second,
  1352. static_cast<_Link_type>(__ptr));
  1353. }
  1354. }
  1355. }
  1356. #endif // C++17
  1357. friend bool
  1358. operator==(const _Rb_tree& __x, const _Rb_tree& __y)
  1359. {
  1360. return __x.size() == __y.size()
  1361. && std::equal(__x.begin(), __x.end(), __y.begin());
  1362. }
  1363. #if __cpp_lib_three_way_comparison
  1364. friend auto
  1365. operator<=>(const _Rb_tree& __x, const _Rb_tree& __y)
  1366. {
  1367. if constexpr (requires { typename __detail::__synth3way_t<_Val>; })
  1368. return std::lexicographical_compare_three_way(__x.begin(), __x.end(),
  1369. __y.begin(), __y.end(),
  1370. __detail::__synth3way);
  1371. }
  1372. #else
  1373. friend bool
  1374. operator<(const _Rb_tree& __x, const _Rb_tree& __y)
  1375. {
  1376. return std::lexicographical_compare(__x.begin(), __x.end(),
  1377. __y.begin(), __y.end());
  1378. }
  1379. friend bool _GLIBCXX_DEPRECATED
  1380. operator!=(const _Rb_tree& __x, const _Rb_tree& __y)
  1381. { return !(__x == __y); }
  1382. friend bool _GLIBCXX_DEPRECATED
  1383. operator>(const _Rb_tree& __x, const _Rb_tree& __y)
  1384. { return __y < __x; }
  1385. friend bool _GLIBCXX_DEPRECATED
  1386. operator<=(const _Rb_tree& __x, const _Rb_tree& __y)
  1387. { return !(__y < __x); }
  1388. friend bool _GLIBCXX_DEPRECATED
  1389. operator>=(const _Rb_tree& __x, const _Rb_tree& __y)
  1390. { return !(__x < __y); }
  1391. #endif
  1392. };
  1393. template<typename _Key, typename _Val, typename _KeyOfValue,
  1394. typename _Compare, typename _Alloc>
  1395. inline void
  1396. swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
  1397. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
  1398. { __x.swap(__y); }
  1399. #if __cplusplus >= 201103L
  1400. template<typename _Key, typename _Val, typename _KeyOfValue,
  1401. typename _Compare, typename _Alloc>
  1402. void
  1403. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1404. _M_move_data(_Rb_tree& __x, false_type)
  1405. {
  1406. if (_M_get_Node_allocator() == __x._M_get_Node_allocator())
  1407. _M_move_data(__x, true_type());
  1408. else
  1409. {
  1410. _Alloc_node __an(*this);
  1411. auto __lbd =
  1412. [&__an](const value_type& __cval)
  1413. {
  1414. auto& __val = const_cast<value_type&>(__cval);
  1415. return __an(std::move_if_noexcept(__val));
  1416. };
  1417. _M_root() = _M_copy(__x, __lbd);
  1418. }
  1419. }
  1420. template<typename _Key, typename _Val, typename _KeyOfValue,
  1421. typename _Compare, typename _Alloc>
  1422. inline void
  1423. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1424. _M_move_assign(_Rb_tree& __x, true_type)
  1425. {
  1426. clear();
  1427. if (__x._M_root() != nullptr)
  1428. _M_move_data(__x, true_type());
  1429. std::__alloc_on_move(_M_get_Node_allocator(),
  1430. __x._M_get_Node_allocator());
  1431. }
  1432. template<typename _Key, typename _Val, typename _KeyOfValue,
  1433. typename _Compare, typename _Alloc>
  1434. void
  1435. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1436. _M_move_assign(_Rb_tree& __x, false_type)
  1437. {
  1438. if (_M_get_Node_allocator() == __x._M_get_Node_allocator())
  1439. return _M_move_assign(__x, true_type{});
  1440. // Try to move each node reusing existing nodes and copying __x nodes
  1441. // structure.
  1442. _Reuse_or_alloc_node __roan(*this);
  1443. _M_impl._M_reset();
  1444. if (__x._M_root() != nullptr)
  1445. {
  1446. auto __lbd =
  1447. [&__roan](const value_type& __cval)
  1448. {
  1449. auto& __val = const_cast<value_type&>(__cval);
  1450. return __roan(std::move(__val));
  1451. };
  1452. _M_root() = _M_copy(__x, __lbd);
  1453. __x.clear();
  1454. }
  1455. }
  1456. template<typename _Key, typename _Val, typename _KeyOfValue,
  1457. typename _Compare, typename _Alloc>
  1458. inline _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
  1459. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1460. operator=(_Rb_tree&& __x)
  1461. noexcept(_Alloc_traits::_S_nothrow_move()
  1462. && is_nothrow_move_assignable<_Compare>::value)
  1463. {
  1464. _M_impl._M_key_compare = std::move(__x._M_impl._M_key_compare);
  1465. _M_move_assign(__x, __bool_constant<_Alloc_traits::_S_nothrow_move()>());
  1466. return *this;
  1467. }
  1468. template<typename _Key, typename _Val, typename _KeyOfValue,
  1469. typename _Compare, typename _Alloc>
  1470. template<typename _Iterator>
  1471. void
  1472. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1473. _M_assign_unique(_Iterator __first, _Iterator __last)
  1474. {
  1475. _Reuse_or_alloc_node __roan(*this);
  1476. _M_impl._M_reset();
  1477. for (; __first != __last; ++__first)
  1478. _M_insert_unique_(end(), *__first, __roan);
  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_equal(_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_equal_(end(), *__first, __roan);
  1491. }
  1492. #endif
  1493. template<typename _Key, typename _Val, typename _KeyOfValue,
  1494. typename _Compare, typename _Alloc>
  1495. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
  1496. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1497. operator=(const _Rb_tree& __x)
  1498. {
  1499. if (this != &__x)
  1500. {
  1501. // Note that _Key may be a constant type.
  1502. #if __cplusplus >= 201103L
  1503. if (_Alloc_traits::_S_propagate_on_copy_assign())
  1504. {
  1505. auto& __this_alloc = this->_M_get_Node_allocator();
  1506. auto& __that_alloc = __x._M_get_Node_allocator();
  1507. if (!_Alloc_traits::_S_always_equal()
  1508. && __this_alloc != __that_alloc)
  1509. {
  1510. // Replacement allocator cannot free existing storage, we need
  1511. // to erase nodes first.
  1512. clear();
  1513. std::__alloc_on_copy(__this_alloc, __that_alloc);
  1514. }
  1515. }
  1516. #endif
  1517. _Reuse_or_alloc_node __roan(*this);
  1518. _M_impl._M_reset();
  1519. _M_impl._M_key_compare = __x._M_impl._M_key_compare;
  1520. if (__x._M_root() != 0)
  1521. _M_root() = _M_copy(__x, __roan);
  1522. }
  1523. return *this;
  1524. }
  1525. template<typename _Key, typename _Val, typename _KeyOfValue,
  1526. typename _Compare, typename _Alloc>
  1527. #if __cplusplus >= 201103L
  1528. template<typename _Arg, typename _NodeGen>
  1529. #else
  1530. template<typename _NodeGen>
  1531. #endif
  1532. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  1533. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1534. _M_insert_(_Base_ptr __x, _Base_ptr __p,
  1535. #if __cplusplus >= 201103L
  1536. _Arg&& __v,
  1537. #else
  1538. const _Val& __v,
  1539. #endif
  1540. _NodeGen& __node_gen)
  1541. {
  1542. bool __insert_left = (__x != 0 || __p == _M_end()
  1543. || _M_impl._M_key_compare(_KeyOfValue()(__v),
  1544. _S_key(__p)));
  1545. _Link_type __z = __node_gen(_GLIBCXX_FORWARD(_Arg, __v));
  1546. _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
  1547. this->_M_impl._M_header);
  1548. ++_M_impl._M_node_count;
  1549. return iterator(__z);
  1550. }
  1551. template<typename _Key, typename _Val, typename _KeyOfValue,
  1552. typename _Compare, typename _Alloc>
  1553. #if __cplusplus >= 201103L
  1554. template<typename _Arg>
  1555. #endif
  1556. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  1557. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1558. #if __cplusplus >= 201103L
  1559. _M_insert_lower(_Base_ptr __p, _Arg&& __v)
  1560. #else
  1561. _M_insert_lower(_Base_ptr __p, const _Val& __v)
  1562. #endif
  1563. {
  1564. bool __insert_left = (__p == _M_end()
  1565. || !_M_impl._M_key_compare(_S_key(__p),
  1566. _KeyOfValue()(__v)));
  1567. _Link_type __z = _M_create_node(_GLIBCXX_FORWARD(_Arg, __v));
  1568. _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
  1569. this->_M_impl._M_header);
  1570. ++_M_impl._M_node_count;
  1571. return iterator(__z);
  1572. }
  1573. template<typename _Key, typename _Val, typename _KeyOfValue,
  1574. typename _Compare, typename _Alloc>
  1575. #if __cplusplus >= 201103L
  1576. template<typename _Arg>
  1577. #endif
  1578. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  1579. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1580. #if __cplusplus >= 201103L
  1581. _M_insert_equal_lower(_Arg&& __v)
  1582. #else
  1583. _M_insert_equal_lower(const _Val& __v)
  1584. #endif
  1585. {
  1586. _Link_type __x = _M_begin();
  1587. _Base_ptr __y = _M_end();
  1588. while (__x != 0)
  1589. {
  1590. __y = __x;
  1591. __x = !_M_impl._M_key_compare(_S_key(__x), _KeyOfValue()(__v)) ?
  1592. _S_left(__x) : _S_right(__x);
  1593. }
  1594. return _M_insert_lower(__y, _GLIBCXX_FORWARD(_Arg, __v));
  1595. }
  1596. template<typename _Key, typename _Val, typename _KoV,
  1597. typename _Compare, typename _Alloc>
  1598. template<typename _NodeGen>
  1599. typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Link_type
  1600. _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
  1601. _M_copy(_Const_Link_type __x, _Base_ptr __p, _NodeGen& __node_gen)
  1602. {
  1603. // Structural copy. __x and __p must be non-null.
  1604. _Link_type __top = _M_clone_node(__x, __node_gen);
  1605. __top->_M_parent = __p;
  1606. __try
  1607. {
  1608. if (__x->_M_right)
  1609. __top->_M_right = _M_copy(_S_right(__x), __top, __node_gen);
  1610. __p = __top;
  1611. __x = _S_left(__x);
  1612. while (__x != 0)
  1613. {
  1614. _Link_type __y = _M_clone_node(__x, __node_gen);
  1615. __p->_M_left = __y;
  1616. __y->_M_parent = __p;
  1617. if (__x->_M_right)
  1618. __y->_M_right = _M_copy(_S_right(__x), __y, __node_gen);
  1619. __p = __y;
  1620. __x = _S_left(__x);
  1621. }
  1622. }
  1623. __catch(...)
  1624. {
  1625. _M_erase(__top);
  1626. __throw_exception_again;
  1627. }
  1628. return __top;
  1629. }
  1630. template<typename _Key, typename _Val, typename _KeyOfValue,
  1631. typename _Compare, typename _Alloc>
  1632. void
  1633. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1634. _M_erase(_Link_type __x)
  1635. {
  1636. // Erase without rebalancing.
  1637. while (__x != 0)
  1638. {
  1639. _M_erase(_S_right(__x));
  1640. _Link_type __y = _S_left(__x);
  1641. _M_drop_node(__x);
  1642. __x = __y;
  1643. }
  1644. }
  1645. template<typename _Key, typename _Val, typename _KeyOfValue,
  1646. typename _Compare, typename _Alloc>
  1647. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1648. _Compare, _Alloc>::iterator
  1649. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1650. _M_lower_bound(_Link_type __x, _Base_ptr __y,
  1651. const _Key& __k)
  1652. {
  1653. while (__x != 0)
  1654. if (!_M_impl._M_key_compare(_S_key(__x), __k))
  1655. __y = __x, __x = _S_left(__x);
  1656. else
  1657. __x = _S_right(__x);
  1658. return iterator(__y);
  1659. }
  1660. template<typename _Key, typename _Val, typename _KeyOfValue,
  1661. typename _Compare, typename _Alloc>
  1662. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1663. _Compare, _Alloc>::const_iterator
  1664. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1665. _M_lower_bound(_Const_Link_type __x, _Const_Base_ptr __y,
  1666. const _Key& __k) const
  1667. {
  1668. while (__x != 0)
  1669. if (!_M_impl._M_key_compare(_S_key(__x), __k))
  1670. __y = __x, __x = _S_left(__x);
  1671. else
  1672. __x = _S_right(__x);
  1673. return const_iterator(__y);
  1674. }
  1675. template<typename _Key, typename _Val, typename _KeyOfValue,
  1676. typename _Compare, typename _Alloc>
  1677. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1678. _Compare, _Alloc>::iterator
  1679. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1680. _M_upper_bound(_Link_type __x, _Base_ptr __y,
  1681. const _Key& __k)
  1682. {
  1683. while (__x != 0)
  1684. if (_M_impl._M_key_compare(__k, _S_key(__x)))
  1685. __y = __x, __x = _S_left(__x);
  1686. else
  1687. __x = _S_right(__x);
  1688. return iterator(__y);
  1689. }
  1690. template<typename _Key, typename _Val, typename _KeyOfValue,
  1691. typename _Compare, typename _Alloc>
  1692. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1693. _Compare, _Alloc>::const_iterator
  1694. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1695. _M_upper_bound(_Const_Link_type __x, _Const_Base_ptr __y,
  1696. const _Key& __k) const
  1697. {
  1698. while (__x != 0)
  1699. if (_M_impl._M_key_compare(__k, _S_key(__x)))
  1700. __y = __x, __x = _S_left(__x);
  1701. else
  1702. __x = _S_right(__x);
  1703. return const_iterator(__y);
  1704. }
  1705. template<typename _Key, typename _Val, typename _KeyOfValue,
  1706. typename _Compare, typename _Alloc>
  1707. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1708. _Compare, _Alloc>::iterator,
  1709. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1710. _Compare, _Alloc>::iterator>
  1711. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1712. equal_range(const _Key& __k)
  1713. {
  1714. _Link_type __x = _M_begin();
  1715. _Base_ptr __y = _M_end();
  1716. while (__x != 0)
  1717. {
  1718. if (_M_impl._M_key_compare(_S_key(__x), __k))
  1719. __x = _S_right(__x);
  1720. else if (_M_impl._M_key_compare(__k, _S_key(__x)))
  1721. __y = __x, __x = _S_left(__x);
  1722. else
  1723. {
  1724. _Link_type __xu(__x);
  1725. _Base_ptr __yu(__y);
  1726. __y = __x, __x = _S_left(__x);
  1727. __xu = _S_right(__xu);
  1728. return pair<iterator,
  1729. iterator>(_M_lower_bound(__x, __y, __k),
  1730. _M_upper_bound(__xu, __yu, __k));
  1731. }
  1732. }
  1733. return pair<iterator, iterator>(iterator(__y),
  1734. iterator(__y));
  1735. }
  1736. template<typename _Key, typename _Val, typename _KeyOfValue,
  1737. typename _Compare, typename _Alloc>
  1738. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1739. _Compare, _Alloc>::const_iterator,
  1740. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1741. _Compare, _Alloc>::const_iterator>
  1742. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1743. equal_range(const _Key& __k) const
  1744. {
  1745. _Const_Link_type __x = _M_begin();
  1746. _Const_Base_ptr __y = _M_end();
  1747. while (__x != 0)
  1748. {
  1749. if (_M_impl._M_key_compare(_S_key(__x), __k))
  1750. __x = _S_right(__x);
  1751. else if (_M_impl._M_key_compare(__k, _S_key(__x)))
  1752. __y = __x, __x = _S_left(__x);
  1753. else
  1754. {
  1755. _Const_Link_type __xu(__x);
  1756. _Const_Base_ptr __yu(__y);
  1757. __y = __x, __x = _S_left(__x);
  1758. __xu = _S_right(__xu);
  1759. return pair<const_iterator,
  1760. const_iterator>(_M_lower_bound(__x, __y, __k),
  1761. _M_upper_bound(__xu, __yu, __k));
  1762. }
  1763. }
  1764. return pair<const_iterator, const_iterator>(const_iterator(__y),
  1765. const_iterator(__y));
  1766. }
  1767. template<typename _Key, typename _Val, typename _KeyOfValue,
  1768. typename _Compare, typename _Alloc>
  1769. void
  1770. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1771. swap(_Rb_tree& __t)
  1772. _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
  1773. {
  1774. if (_M_root() == 0)
  1775. {
  1776. if (__t._M_root() != 0)
  1777. _M_impl._M_move_data(__t._M_impl);
  1778. }
  1779. else if (__t._M_root() == 0)
  1780. __t._M_impl._M_move_data(_M_impl);
  1781. else
  1782. {
  1783. std::swap(_M_root(),__t._M_root());
  1784. std::swap(_M_leftmost(),__t._M_leftmost());
  1785. std::swap(_M_rightmost(),__t._M_rightmost());
  1786. _M_root()->_M_parent = _M_end();
  1787. __t._M_root()->_M_parent = __t._M_end();
  1788. std::swap(this->_M_impl._M_node_count, __t._M_impl._M_node_count);
  1789. }
  1790. // No need to swap header's color as it does not change.
  1791. std::swap(this->_M_impl._M_key_compare, __t._M_impl._M_key_compare);
  1792. _Alloc_traits::_S_on_swap(_M_get_Node_allocator(),
  1793. __t._M_get_Node_allocator());
  1794. }
  1795. template<typename _Key, typename _Val, typename _KeyOfValue,
  1796. typename _Compare, typename _Alloc>
  1797. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1798. _Compare, _Alloc>::_Base_ptr,
  1799. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1800. _Compare, _Alloc>::_Base_ptr>
  1801. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1802. _M_get_insert_unique_pos(const key_type& __k)
  1803. {
  1804. typedef pair<_Base_ptr, _Base_ptr> _Res;
  1805. _Link_type __x = _M_begin();
  1806. _Base_ptr __y = _M_end();
  1807. bool __comp = true;
  1808. while (__x != 0)
  1809. {
  1810. __y = __x;
  1811. __comp = _M_impl._M_key_compare(__k, _S_key(__x));
  1812. __x = __comp ? _S_left(__x) : _S_right(__x);
  1813. }
  1814. iterator __j = iterator(__y);
  1815. if (__comp)
  1816. {
  1817. if (__j == begin())
  1818. return _Res(__x, __y);
  1819. else
  1820. --__j;
  1821. }
  1822. if (_M_impl._M_key_compare(_S_key(__j._M_node), __k))
  1823. return _Res(__x, __y);
  1824. return _Res(__j._M_node, 0);
  1825. }
  1826. template<typename _Key, typename _Val, typename _KeyOfValue,
  1827. typename _Compare, typename _Alloc>
  1828. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1829. _Compare, _Alloc>::_Base_ptr,
  1830. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1831. _Compare, _Alloc>::_Base_ptr>
  1832. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1833. _M_get_insert_equal_pos(const key_type& __k)
  1834. {
  1835. typedef pair<_Base_ptr, _Base_ptr> _Res;
  1836. _Link_type __x = _M_begin();
  1837. _Base_ptr __y = _M_end();
  1838. while (__x != 0)
  1839. {
  1840. __y = __x;
  1841. __x = _M_impl._M_key_compare(__k, _S_key(__x)) ?
  1842. _S_left(__x) : _S_right(__x);
  1843. }
  1844. return _Res(__x, __y);
  1845. }
  1846. template<typename _Key, typename _Val, typename _KeyOfValue,
  1847. typename _Compare, typename _Alloc>
  1848. #if __cplusplus >= 201103L
  1849. template<typename _Arg>
  1850. #endif
  1851. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1852. _Compare, _Alloc>::iterator, bool>
  1853. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1854. #if __cplusplus >= 201103L
  1855. _M_insert_unique(_Arg&& __v)
  1856. #else
  1857. _M_insert_unique(const _Val& __v)
  1858. #endif
  1859. {
  1860. typedef pair<iterator, bool> _Res;
  1861. pair<_Base_ptr, _Base_ptr> __res
  1862. = _M_get_insert_unique_pos(_KeyOfValue()(__v));
  1863. if (__res.second)
  1864. {
  1865. _Alloc_node __an(*this);
  1866. return _Res(_M_insert_(__res.first, __res.second,
  1867. _GLIBCXX_FORWARD(_Arg, __v), __an),
  1868. true);
  1869. }
  1870. return _Res(iterator(__res.first), false);
  1871. }
  1872. template<typename _Key, typename _Val, typename _KeyOfValue,
  1873. typename _Compare, typename _Alloc>
  1874. #if __cplusplus >= 201103L
  1875. template<typename _Arg>
  1876. #endif
  1877. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  1878. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1879. #if __cplusplus >= 201103L
  1880. _M_insert_equal(_Arg&& __v)
  1881. #else
  1882. _M_insert_equal(const _Val& __v)
  1883. #endif
  1884. {
  1885. pair<_Base_ptr, _Base_ptr> __res
  1886. = _M_get_insert_equal_pos(_KeyOfValue()(__v));
  1887. _Alloc_node __an(*this);
  1888. return _M_insert_(__res.first, __res.second,
  1889. _GLIBCXX_FORWARD(_Arg, __v), __an);
  1890. }
  1891. template<typename _Key, typename _Val, typename _KeyOfValue,
  1892. typename _Compare, typename _Alloc>
  1893. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1894. _Compare, _Alloc>::_Base_ptr,
  1895. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1896. _Compare, _Alloc>::_Base_ptr>
  1897. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1898. _M_get_insert_hint_unique_pos(const_iterator __position,
  1899. const key_type& __k)
  1900. {
  1901. iterator __pos = __position._M_const_cast();
  1902. typedef pair<_Base_ptr, _Base_ptr> _Res;
  1903. // end()
  1904. if (__pos._M_node == _M_end())
  1905. {
  1906. if (size() > 0
  1907. && _M_impl._M_key_compare(_S_key(_M_rightmost()), __k))
  1908. return _Res(0, _M_rightmost());
  1909. else
  1910. return _M_get_insert_unique_pos(__k);
  1911. }
  1912. else if (_M_impl._M_key_compare(__k, _S_key(__pos._M_node)))
  1913. {
  1914. // First, try before...
  1915. iterator __before = __pos;
  1916. if (__pos._M_node == _M_leftmost()) // begin()
  1917. return _Res(_M_leftmost(), _M_leftmost());
  1918. else if (_M_impl._M_key_compare(_S_key((--__before)._M_node), __k))
  1919. {
  1920. if (_S_right(__before._M_node) == 0)
  1921. return _Res(0, __before._M_node);
  1922. else
  1923. return _Res(__pos._M_node, __pos._M_node);
  1924. }
  1925. else
  1926. return _M_get_insert_unique_pos(__k);
  1927. }
  1928. else if (_M_impl._M_key_compare(_S_key(__pos._M_node), __k))
  1929. {
  1930. // ... then try after.
  1931. iterator __after = __pos;
  1932. if (__pos._M_node == _M_rightmost())
  1933. return _Res(0, _M_rightmost());
  1934. else if (_M_impl._M_key_compare(__k, _S_key((++__after)._M_node)))
  1935. {
  1936. if (_S_right(__pos._M_node) == 0)
  1937. return _Res(0, __pos._M_node);
  1938. else
  1939. return _Res(__after._M_node, __after._M_node);
  1940. }
  1941. else
  1942. return _M_get_insert_unique_pos(__k);
  1943. }
  1944. else
  1945. // Equivalent keys.
  1946. return _Res(__pos._M_node, 0);
  1947. }
  1948. template<typename _Key, typename _Val, typename _KeyOfValue,
  1949. typename _Compare, typename _Alloc>
  1950. #if __cplusplus >= 201103L
  1951. template<typename _Arg, typename _NodeGen>
  1952. #else
  1953. template<typename _NodeGen>
  1954. #endif
  1955. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  1956. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1957. _M_insert_unique_(const_iterator __position,
  1958. #if __cplusplus >= 201103L
  1959. _Arg&& __v,
  1960. #else
  1961. const _Val& __v,
  1962. #endif
  1963. _NodeGen& __node_gen)
  1964. {
  1965. pair<_Base_ptr, _Base_ptr> __res
  1966. = _M_get_insert_hint_unique_pos(__position, _KeyOfValue()(__v));
  1967. if (__res.second)
  1968. return _M_insert_(__res.first, __res.second,
  1969. _GLIBCXX_FORWARD(_Arg, __v),
  1970. __node_gen);
  1971. return iterator(__res.first);
  1972. }
  1973. template<typename _Key, typename _Val, typename _KeyOfValue,
  1974. typename _Compare, typename _Alloc>
  1975. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1976. _Compare, _Alloc>::_Base_ptr,
  1977. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  1978. _Compare, _Alloc>::_Base_ptr>
  1979. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  1980. _M_get_insert_hint_equal_pos(const_iterator __position, const key_type& __k)
  1981. {
  1982. iterator __pos = __position._M_const_cast();
  1983. typedef pair<_Base_ptr, _Base_ptr> _Res;
  1984. // end()
  1985. if (__pos._M_node == _M_end())
  1986. {
  1987. if (size() > 0
  1988. && !_M_impl._M_key_compare(__k, _S_key(_M_rightmost())))
  1989. return _Res(0, _M_rightmost());
  1990. else
  1991. return _M_get_insert_equal_pos(__k);
  1992. }
  1993. else if (!_M_impl._M_key_compare(_S_key(__pos._M_node), __k))
  1994. {
  1995. // First, try before...
  1996. iterator __before = __pos;
  1997. if (__pos._M_node == _M_leftmost()) // begin()
  1998. return _Res(_M_leftmost(), _M_leftmost());
  1999. else if (!_M_impl._M_key_compare(__k, _S_key((--__before)._M_node)))
  2000. {
  2001. if (_S_right(__before._M_node) == 0)
  2002. return _Res(0, __before._M_node);
  2003. else
  2004. return _Res(__pos._M_node, __pos._M_node);
  2005. }
  2006. else
  2007. return _M_get_insert_equal_pos(__k);
  2008. }
  2009. else
  2010. {
  2011. // ... then try after.
  2012. iterator __after = __pos;
  2013. if (__pos._M_node == _M_rightmost())
  2014. return _Res(0, _M_rightmost());
  2015. else if (!_M_impl._M_key_compare(_S_key((++__after)._M_node), __k))
  2016. {
  2017. if (_S_right(__pos._M_node) == 0)
  2018. return _Res(0, __pos._M_node);
  2019. else
  2020. return _Res(__after._M_node, __after._M_node);
  2021. }
  2022. else
  2023. return _Res(0, 0);
  2024. }
  2025. }
  2026. template<typename _Key, typename _Val, typename _KeyOfValue,
  2027. typename _Compare, typename _Alloc>
  2028. #if __cplusplus >= 201103L
  2029. template<typename _Arg, typename _NodeGen>
  2030. #else
  2031. template<typename _NodeGen>
  2032. #endif
  2033. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  2034. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2035. _M_insert_equal_(const_iterator __position,
  2036. #if __cplusplus >= 201103L
  2037. _Arg&& __v,
  2038. #else
  2039. const _Val& __v,
  2040. #endif
  2041. _NodeGen& __node_gen)
  2042. {
  2043. pair<_Base_ptr, _Base_ptr> __res
  2044. = _M_get_insert_hint_equal_pos(__position, _KeyOfValue()(__v));
  2045. if (__res.second)
  2046. return _M_insert_(__res.first, __res.second,
  2047. _GLIBCXX_FORWARD(_Arg, __v),
  2048. __node_gen);
  2049. return _M_insert_equal_lower(_GLIBCXX_FORWARD(_Arg, __v));
  2050. }
  2051. #if __cplusplus >= 201103L
  2052. template<typename _Key, typename _Val, typename _KeyOfValue,
  2053. typename _Compare, typename _Alloc>
  2054. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  2055. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2056. _M_insert_node(_Base_ptr __x, _Base_ptr __p, _Link_type __z)
  2057. {
  2058. bool __insert_left = (__x != 0 || __p == _M_end()
  2059. || _M_impl._M_key_compare(_S_key(__z),
  2060. _S_key(__p)));
  2061. _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
  2062. this->_M_impl._M_header);
  2063. ++_M_impl._M_node_count;
  2064. return iterator(__z);
  2065. }
  2066. template<typename _Key, typename _Val, typename _KeyOfValue,
  2067. typename _Compare, typename _Alloc>
  2068. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  2069. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2070. _M_insert_lower_node(_Base_ptr __p, _Link_type __z)
  2071. {
  2072. bool __insert_left = (__p == _M_end()
  2073. || !_M_impl._M_key_compare(_S_key(__p),
  2074. _S_key(__z)));
  2075. _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
  2076. this->_M_impl._M_header);
  2077. ++_M_impl._M_node_count;
  2078. return iterator(__z);
  2079. }
  2080. template<typename _Key, typename _Val, typename _KeyOfValue,
  2081. typename _Compare, typename _Alloc>
  2082. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  2083. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2084. _M_insert_equal_lower_node(_Link_type __z)
  2085. {
  2086. _Link_type __x = _M_begin();
  2087. _Base_ptr __y = _M_end();
  2088. while (__x != 0)
  2089. {
  2090. __y = __x;
  2091. __x = !_M_impl._M_key_compare(_S_key(__x), _S_key(__z)) ?
  2092. _S_left(__x) : _S_right(__x);
  2093. }
  2094. return _M_insert_lower_node(__y, __z);
  2095. }
  2096. template<typename _Key, typename _Val, typename _KeyOfValue,
  2097. typename _Compare, typename _Alloc>
  2098. template<typename... _Args>
  2099. pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
  2100. _Compare, _Alloc>::iterator, bool>
  2101. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2102. _M_emplace_unique(_Args&&... __args)
  2103. {
  2104. _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
  2105. __try
  2106. {
  2107. typedef pair<iterator, bool> _Res;
  2108. auto __res = _M_get_insert_unique_pos(_S_key(__z));
  2109. if (__res.second)
  2110. return _Res(_M_insert_node(__res.first, __res.second, __z), true);
  2111. _M_drop_node(__z);
  2112. return _Res(iterator(__res.first), false);
  2113. }
  2114. __catch(...)
  2115. {
  2116. _M_drop_node(__z);
  2117. __throw_exception_again;
  2118. }
  2119. }
  2120. template<typename _Key, typename _Val, typename _KeyOfValue,
  2121. typename _Compare, typename _Alloc>
  2122. template<typename... _Args>
  2123. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  2124. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2125. _M_emplace_equal(_Args&&... __args)
  2126. {
  2127. _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
  2128. __try
  2129. {
  2130. auto __res = _M_get_insert_equal_pos(_S_key(__z));
  2131. return _M_insert_node(__res.first, __res.second, __z);
  2132. }
  2133. __catch(...)
  2134. {
  2135. _M_drop_node(__z);
  2136. __throw_exception_again;
  2137. }
  2138. }
  2139. template<typename _Key, typename _Val, typename _KeyOfValue,
  2140. typename _Compare, typename _Alloc>
  2141. template<typename... _Args>
  2142. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  2143. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2144. _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args)
  2145. {
  2146. _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
  2147. __try
  2148. {
  2149. auto __res = _M_get_insert_hint_unique_pos(__pos, _S_key(__z));
  2150. if (__res.second)
  2151. return _M_insert_node(__res.first, __res.second, __z);
  2152. _M_drop_node(__z);
  2153. return iterator(__res.first);
  2154. }
  2155. __catch(...)
  2156. {
  2157. _M_drop_node(__z);
  2158. __throw_exception_again;
  2159. }
  2160. }
  2161. template<typename _Key, typename _Val, typename _KeyOfValue,
  2162. typename _Compare, typename _Alloc>
  2163. template<typename... _Args>
  2164. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
  2165. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2166. _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args)
  2167. {
  2168. _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
  2169. __try
  2170. {
  2171. auto __res = _M_get_insert_hint_equal_pos(__pos, _S_key(__z));
  2172. if (__res.second)
  2173. return _M_insert_node(__res.first, __res.second, __z);
  2174. return _M_insert_equal_lower_node(__z);
  2175. }
  2176. __catch(...)
  2177. {
  2178. _M_drop_node(__z);
  2179. __throw_exception_again;
  2180. }
  2181. }
  2182. #endif
  2183. template<typename _Key, typename _Val, typename _KeyOfValue,
  2184. typename _Compare, typename _Alloc>
  2185. void
  2186. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2187. _M_erase_aux(const_iterator __position)
  2188. {
  2189. _Link_type __y =
  2190. static_cast<_Link_type>(_Rb_tree_rebalance_for_erase
  2191. (const_cast<_Base_ptr>(__position._M_node),
  2192. this->_M_impl._M_header));
  2193. _M_drop_node(__y);
  2194. --_M_impl._M_node_count;
  2195. }
  2196. template<typename _Key, typename _Val, typename _KeyOfValue,
  2197. typename _Compare, typename _Alloc>
  2198. void
  2199. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2200. _M_erase_aux(const_iterator __first, const_iterator __last)
  2201. {
  2202. if (__first == begin() && __last == end())
  2203. clear();
  2204. else
  2205. while (__first != __last)
  2206. _M_erase_aux(__first++);
  2207. }
  2208. template<typename _Key, typename _Val, typename _KeyOfValue,
  2209. typename _Compare, typename _Alloc>
  2210. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
  2211. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2212. erase(const _Key& __x)
  2213. {
  2214. pair<iterator, iterator> __p = equal_range(__x);
  2215. const size_type __old_size = size();
  2216. _M_erase_aux(__p.first, __p.second);
  2217. return __old_size - size();
  2218. }
  2219. template<typename _Key, typename _Val, typename _KeyOfValue,
  2220. typename _Compare, typename _Alloc>
  2221. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  2222. _Compare, _Alloc>::iterator
  2223. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2224. find(const _Key& __k)
  2225. {
  2226. iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
  2227. return (__j == end()
  2228. || _M_impl._M_key_compare(__k,
  2229. _S_key(__j._M_node))) ? end() : __j;
  2230. }
  2231. template<typename _Key, typename _Val, typename _KeyOfValue,
  2232. typename _Compare, typename _Alloc>
  2233. typename _Rb_tree<_Key, _Val, _KeyOfValue,
  2234. _Compare, _Alloc>::const_iterator
  2235. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2236. find(const _Key& __k) const
  2237. {
  2238. const_iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
  2239. return (__j == end()
  2240. || _M_impl._M_key_compare(__k,
  2241. _S_key(__j._M_node))) ? end() : __j;
  2242. }
  2243. template<typename _Key, typename _Val, typename _KeyOfValue,
  2244. typename _Compare, typename _Alloc>
  2245. typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
  2246. _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
  2247. count(const _Key& __k) const
  2248. {
  2249. pair<const_iterator, const_iterator> __p = equal_range(__k);
  2250. const size_type __n = std::distance(__p.first, __p.second);
  2251. return __n;
  2252. }
  2253. _GLIBCXX_PURE unsigned int
  2254. _Rb_tree_black_count(const _Rb_tree_node_base* __node,
  2255. const _Rb_tree_node_base* __root) throw ();
  2256. template<typename _Key, typename _Val, typename _KeyOfValue,
  2257. typename _Compare, typename _Alloc>
  2258. bool
  2259. _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const
  2260. {
  2261. if (_M_impl._M_node_count == 0 || begin() == end())
  2262. return _M_impl._M_node_count == 0 && begin() == end()
  2263. && this->_M_impl._M_header._M_left == _M_end()
  2264. && this->_M_impl._M_header._M_right == _M_end();
  2265. unsigned int __len = _Rb_tree_black_count(_M_leftmost(), _M_root());
  2266. for (const_iterator __it = begin(); __it != end(); ++__it)
  2267. {
  2268. _Const_Link_type __x = static_cast<_Const_Link_type>(__it._M_node);
  2269. _Const_Link_type __L = _S_left(__x);
  2270. _Const_Link_type __R = _S_right(__x);
  2271. if (__x->_M_color == _S_red)
  2272. if ((__L && __L->_M_color == _S_red)
  2273. || (__R && __R->_M_color == _S_red))
  2274. return false;
  2275. if (__L && _M_impl._M_key_compare(_S_key(__x), _S_key(__L)))
  2276. return false;
  2277. if (__R && _M_impl._M_key_compare(_S_key(__R), _S_key(__x)))
  2278. return false;
  2279. if (!__L && !__R && _Rb_tree_black_count(__x, _M_root()) != __len)
  2280. return false;
  2281. }
  2282. if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
  2283. return false;
  2284. if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
  2285. return false;
  2286. return true;
  2287. }
  2288. #if __cplusplus > 201402L
  2289. // Allow access to internals of compatible _Rb_tree specializations.
  2290. template<typename _Key, typename _Val, typename _Sel, typename _Cmp1,
  2291. typename _Alloc, typename _Cmp2>
  2292. struct _Rb_tree_merge_helper<_Rb_tree<_Key, _Val, _Sel, _Cmp1, _Alloc>,
  2293. _Cmp2>
  2294. {
  2295. private:
  2296. friend class _Rb_tree<_Key, _Val, _Sel, _Cmp1, _Alloc>;
  2297. static auto&
  2298. _S_get_impl(_Rb_tree<_Key, _Val, _Sel, _Cmp2, _Alloc>& __tree)
  2299. { return __tree._M_impl; }
  2300. };
  2301. #endif // C++17
  2302. _GLIBCXX_END_NAMESPACE_VERSION
  2303. } // namespace
  2304. #endif