stl_tree.h 73 KB

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