stl_tree.h 73 KB

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