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