1 // RB tree implementation -*- C++ -*-
3 // Copyright (C) 2001-2015 Free Software Foundation, Inc.
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
8 // Free Software Foundation; either version 3, or (at your option)
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.
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.
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/>.
27 * Copyright (c) 1996,1997
28 * Silicon Graphics Computer Systems, Inc.
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.
40 * Hewlett-Packard Company
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.
53 /** @file bits/stl_tree.h
54 * This is an internal header file, included by other library headers.
55 * Do not attempt to use it directly. @headername{map,set}
61 #pragma GCC system_header
63 #include <bits/stl_algobase.h>
64 #include <bits/allocator.h>
65 #include <bits/stl_function.h>
66 #include <bits/cpp_type_traits.h>
67 #include <ext/alloc_traits.h>
68 #if __cplusplus >= 201103L
69 #include <ext/aligned_buffer.h>
72 namespace std _GLIBCXX_VISIBILITY(default)
74 _GLIBCXX_BEGIN_NAMESPACE_VERSION
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,
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
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.
92 enum _Rb_tree_color { _S_red = false, _S_black = true };
94 struct _Rb_tree_node_base
96 typedef _Rb_tree_node_base* _Base_ptr;
97 typedef const _Rb_tree_node_base* _Const_Base_ptr;
99 _Rb_tree_color _M_color;
105 _S_minimum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
107 while (__x->_M_left != 0) __x = __x->_M_left;
111 static _Const_Base_ptr
112 _S_minimum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
114 while (__x->_M_left != 0) __x = __x->_M_left;
119 _S_maximum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
121 while (__x->_M_right != 0) __x = __x->_M_right;
125 static _Const_Base_ptr
126 _S_maximum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
128 while (__x->_M_right != 0) __x = __x->_M_right;
133 template<typename _Val>
134 struct _Rb_tree_node : public _Rb_tree_node_base
136 typedef _Rb_tree_node<_Val>* _Link_type;
138 #if __cplusplus < 201103L
143 { return std::__addressof(_M_value_field); }
147 { return std::__addressof(_M_value_field); }
149 __gnu_cxx::__aligned_buffer<_Val> _M_storage;
153 { return _M_storage._M_ptr(); }
157 { return _M_storage._M_ptr(); }
161 _GLIBCXX_PURE _Rb_tree_node_base*
162 _Rb_tree_increment(_Rb_tree_node_base* __x) throw ();
164 _GLIBCXX_PURE const _Rb_tree_node_base*
165 _Rb_tree_increment(const _Rb_tree_node_base* __x) throw ();
167 _GLIBCXX_PURE _Rb_tree_node_base*
168 _Rb_tree_decrement(_Rb_tree_node_base* __x) throw ();
170 _GLIBCXX_PURE const _Rb_tree_node_base*
171 _Rb_tree_decrement(const _Rb_tree_node_base* __x) throw ();
173 template<typename _Tp>
174 struct _Rb_tree_iterator
176 typedef _Tp value_type;
177 typedef _Tp& reference;
178 typedef _Tp* pointer;
180 typedef bidirectional_iterator_tag iterator_category;
181 typedef ptrdiff_t difference_type;
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;
187 _Rb_tree_iterator() _GLIBCXX_NOEXCEPT
191 _Rb_tree_iterator(_Link_type __x) _GLIBCXX_NOEXCEPT
195 operator*() const _GLIBCXX_NOEXCEPT
196 { return *static_cast<_Link_type>(_M_node)->_M_valptr(); }
199 operator->() const _GLIBCXX_NOEXCEPT
200 { return static_cast<_Link_type> (_M_node)->_M_valptr(); }
203 operator++() _GLIBCXX_NOEXCEPT
205 _M_node = _Rb_tree_increment(_M_node);
210 operator++(int) _GLIBCXX_NOEXCEPT
213 _M_node = _Rb_tree_increment(_M_node);
218 operator--() _GLIBCXX_NOEXCEPT
220 _M_node = _Rb_tree_decrement(_M_node);
225 operator--(int) _GLIBCXX_NOEXCEPT
228 _M_node = _Rb_tree_decrement(_M_node);
233 operator==(const _Self& __x) const _GLIBCXX_NOEXCEPT
234 { return _M_node == __x._M_node; }
237 operator!=(const _Self& __x) const _GLIBCXX_NOEXCEPT
238 { return _M_node != __x._M_node; }
243 template<typename _Tp>
244 struct _Rb_tree_const_iterator
246 typedef _Tp value_type;
247 typedef const _Tp& reference;
248 typedef const _Tp* pointer;
250 typedef _Rb_tree_iterator<_Tp> iterator;
252 typedef bidirectional_iterator_tag iterator_category;
253 typedef ptrdiff_t difference_type;
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;
259 _Rb_tree_const_iterator() _GLIBCXX_NOEXCEPT
263 _Rb_tree_const_iterator(_Link_type __x) _GLIBCXX_NOEXCEPT
266 _Rb_tree_const_iterator(const iterator& __it) _GLIBCXX_NOEXCEPT
267 : _M_node(__it._M_node) { }
270 _M_const_cast() const _GLIBCXX_NOEXCEPT
271 { return iterator(static_cast<typename iterator::_Link_type>
272 (const_cast<typename iterator::_Base_ptr>(_M_node))); }
275 operator*() const _GLIBCXX_NOEXCEPT
276 { return *static_cast<_Link_type>(_M_node)->_M_valptr(); }
279 operator->() const _GLIBCXX_NOEXCEPT
280 { return static_cast<_Link_type>(_M_node)->_M_valptr(); }
283 operator++() _GLIBCXX_NOEXCEPT
285 _M_node = _Rb_tree_increment(_M_node);
290 operator++(int) _GLIBCXX_NOEXCEPT
293 _M_node = _Rb_tree_increment(_M_node);
298 operator--() _GLIBCXX_NOEXCEPT
300 _M_node = _Rb_tree_decrement(_M_node);
305 operator--(int) _GLIBCXX_NOEXCEPT
308 _M_node = _Rb_tree_decrement(_M_node);
313 operator==(const _Self& __x) const _GLIBCXX_NOEXCEPT
314 { return _M_node == __x._M_node; }
317 operator!=(const _Self& __x) const _GLIBCXX_NOEXCEPT
318 { return _M_node != __x._M_node; }
323 template<typename _Val>
325 operator==(const _Rb_tree_iterator<_Val>& __x,
326 const _Rb_tree_const_iterator<_Val>& __y) _GLIBCXX_NOEXCEPT
327 { return __x._M_node == __y._M_node; }
329 template<typename _Val>
331 operator!=(const _Rb_tree_iterator<_Val>& __x,
332 const _Rb_tree_const_iterator<_Val>& __y) _GLIBCXX_NOEXCEPT
333 { return __x._M_node != __y._M_node; }
336 _Rb_tree_insert_and_rebalance(const bool __insert_left,
337 _Rb_tree_node_base* __x,
338 _Rb_tree_node_base* __p,
339 _Rb_tree_node_base& __header) throw ();
342 _Rb_tree_rebalance_for_erase(_Rb_tree_node_base* const __z,
343 _Rb_tree_node_base& __header) throw ();
346 template<typename _Key, typename _Val, typename _KeyOfValue,
347 typename _Compare, typename _Alloc = allocator<_Val> >
350 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
351 rebind<_Rb_tree_node<_Val> >::other _Node_allocator;
353 typedef __gnu_cxx::__alloc_traits<_Node_allocator> _Alloc_traits;
356 typedef _Rb_tree_node_base* _Base_ptr;
357 typedef const _Rb_tree_node_base* _Const_Base_ptr;
358 typedef _Rb_tree_node<_Val>* _Link_type;
359 typedef const _Rb_tree_node<_Val>* _Const_Link_type;
362 // Functor recycling a pool of nodes and using allocation once the pool
364 struct _Reuse_or_alloc_node
366 _Reuse_or_alloc_node(_Rb_tree& __t)
367 : _M_root(__t._M_root()), _M_nodes(__t._M_rightmost()), _M_t(__t)
371 _M_root->_M_parent = 0;
373 if (_M_nodes->_M_left)
374 _M_nodes = _M_nodes->_M_left;
380 #if __cplusplus >= 201103L
381 _Reuse_or_alloc_node(const _Reuse_or_alloc_node&) = delete;
384 ~_Reuse_or_alloc_node()
385 { _M_t._M_erase(static_cast<_Link_type>(_M_root)); }
387 template<typename _Arg>
389 #if __cplusplus < 201103L
390 operator()(const _Arg& __arg)
392 operator()(_Arg&& __arg)
395 _Link_type __node = static_cast<_Link_type>(_M_extract());
398 _M_t._M_destroy_node(__node);
399 _M_t._M_construct_node(__node, _GLIBCXX_FORWARD(_Arg, __arg));
403 return _M_t._M_create_node(_GLIBCXX_FORWARD(_Arg, __arg));
413 _Base_ptr __node = _M_nodes;
414 _M_nodes = _M_nodes->_M_parent;
417 if (_M_nodes->_M_right == __node)
419 _M_nodes->_M_right = 0;
421 if (_M_nodes->_M_left)
423 _M_nodes = _M_nodes->_M_left;
425 while (_M_nodes->_M_right)
426 _M_nodes = _M_nodes->_M_right;
428 if (_M_nodes->_M_left)
429 _M_nodes = _M_nodes->_M_left;
432 else // __node is on the left.
433 _M_nodes->_M_left = 0;
446 // Functor similar to the previous one but without any pool of nodes to
450 _Alloc_node(_Rb_tree& __t)
453 template<typename _Arg>
455 #if __cplusplus < 201103L
456 operator()(const _Arg& __arg) const
458 operator()(_Arg&& __arg) const
460 { return _M_t._M_create_node(_GLIBCXX_FORWARD(_Arg, __arg)); }
467 typedef _Key key_type;
468 typedef _Val value_type;
469 typedef value_type* pointer;
470 typedef const value_type* const_pointer;
471 typedef value_type& reference;
472 typedef const value_type& const_reference;
473 typedef size_t size_type;
474 typedef ptrdiff_t difference_type;
475 typedef _Alloc allocator_type;
478 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
479 { return *static_cast<_Node_allocator*>(&this->_M_impl); }
481 const _Node_allocator&
482 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
483 { return *static_cast<const _Node_allocator*>(&this->_M_impl); }
486 get_allocator() const _GLIBCXX_NOEXCEPT
487 { return allocator_type(_M_get_Node_allocator()); }
492 { return _Alloc_traits::allocate(_M_get_Node_allocator(), 1); }
495 _M_put_node(_Link_type __p) _GLIBCXX_NOEXCEPT
496 { _Alloc_traits::deallocate(_M_get_Node_allocator(), __p, 1); }
498 #if __cplusplus < 201103L
500 _M_construct_node(_Link_type __node, const value_type& __x)
503 { get_allocator().construct(__node->_M_valptr(), __x); }
507 __throw_exception_again;
512 _M_create_node(const value_type& __x)
514 _Link_type __tmp = _M_get_node();
515 _M_construct_node(__tmp, __x);
520 _M_destroy_node(_Link_type __p)
521 { get_allocator().destroy(__p->_M_valptr()); }
523 template<typename... _Args>
525 _M_construct_node(_Link_type __node, _Args&&... __args)
529 ::new(__node) _Rb_tree_node<_Val>;
530 _Alloc_traits::construct(_M_get_Node_allocator(),
532 std::forward<_Args>(__args)...);
536 __node->~_Rb_tree_node<_Val>();
538 __throw_exception_again;
542 template<typename... _Args>
544 _M_create_node(_Args&&... __args)
546 _Link_type __tmp = _M_get_node();
547 _M_construct_node(__tmp, std::forward<_Args>(__args)...);
552 _M_destroy_node(_Link_type __p) noexcept
554 _Alloc_traits::destroy(_M_get_Node_allocator(), __p->_M_valptr());
555 __p->~_Rb_tree_node<_Val>();
560 _M_drop_node(_Link_type __p) _GLIBCXX_NOEXCEPT
562 _M_destroy_node(__p);
566 template<typename _NodeGen>
568 _M_clone_node(_Const_Link_type __x, _NodeGen& __node_gen)
570 _Link_type __tmp = __node_gen(*__x->_M_valptr());
571 __tmp->_M_color = __x->_M_color;
578 // Unused _Is_pod_comparator is kept as it is part of mangled name.
579 template<typename _Key_compare,
580 bool /* _Is_pod_comparator */ = __is_pod(_Key_compare)>
581 struct _Rb_tree_impl : public _Node_allocator
583 _Key_compare _M_key_compare;
584 _Rb_tree_node_base _M_header;
585 size_type _M_node_count; // Keeps track of size of tree.
588 : _Node_allocator(), _M_key_compare(), _M_header(),
592 _Rb_tree_impl(const _Key_compare& __comp, const _Node_allocator& __a)
593 : _Node_allocator(__a), _M_key_compare(__comp), _M_header(),
597 #if __cplusplus >= 201103L
598 _Rb_tree_impl(const _Key_compare& __comp, _Node_allocator&& __a)
599 : _Node_allocator(std::move(__a)), _M_key_compare(__comp),
600 _M_header(), _M_node_count(0)
607 this->_M_header._M_parent = 0;
608 this->_M_header._M_left = &this->_M_header;
609 this->_M_header._M_right = &this->_M_header;
610 this->_M_node_count = 0;
617 this->_M_header._M_color = _S_red;
618 this->_M_header._M_parent = 0;
619 this->_M_header._M_left = &this->_M_header;
620 this->_M_header._M_right = &this->_M_header;
624 _Rb_tree_impl<_Compare> _M_impl;
628 _M_root() _GLIBCXX_NOEXCEPT
629 { return this->_M_impl._M_header._M_parent; }
632 _M_root() const _GLIBCXX_NOEXCEPT
633 { return this->_M_impl._M_header._M_parent; }
636 _M_leftmost() _GLIBCXX_NOEXCEPT
637 { return this->_M_impl._M_header._M_left; }
640 _M_leftmost() const _GLIBCXX_NOEXCEPT
641 { return this->_M_impl._M_header._M_left; }
644 _M_rightmost() _GLIBCXX_NOEXCEPT
645 { return this->_M_impl._M_header._M_right; }
648 _M_rightmost() const _GLIBCXX_NOEXCEPT
649 { return this->_M_impl._M_header._M_right; }
652 _M_begin() _GLIBCXX_NOEXCEPT
653 { return static_cast<_Link_type>(this->_M_impl._M_header._M_parent); }
656 _M_begin() const _GLIBCXX_NOEXCEPT
658 return static_cast<_Const_Link_type>
659 (this->_M_impl._M_header._M_parent);
663 _M_end() _GLIBCXX_NOEXCEPT
664 { return reinterpret_cast<_Link_type>(&this->_M_impl._M_header); }
667 _M_end() const _GLIBCXX_NOEXCEPT
668 { return reinterpret_cast<_Const_Link_type>(&this->_M_impl._M_header); }
670 static const_reference
671 _S_value(_Const_Link_type __x)
672 { return *__x->_M_valptr(); }
675 _S_key(_Const_Link_type __x)
676 { return _KeyOfValue()(_S_value(__x)); }
679 _S_left(_Base_ptr __x) _GLIBCXX_NOEXCEPT
680 { return static_cast<_Link_type>(__x->_M_left); }
682 static _Const_Link_type
683 _S_left(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
684 { return static_cast<_Const_Link_type>(__x->_M_left); }
687 _S_right(_Base_ptr __x) _GLIBCXX_NOEXCEPT
688 { return static_cast<_Link_type>(__x->_M_right); }
690 static _Const_Link_type
691 _S_right(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
692 { return static_cast<_Const_Link_type>(__x->_M_right); }
694 static const_reference
695 _S_value(_Const_Base_ptr __x)
696 { return *static_cast<_Const_Link_type>(__x)->_M_valptr(); }
699 _S_key(_Const_Base_ptr __x)
700 { return _KeyOfValue()(_S_value(__x)); }
703 _S_minimum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
704 { return _Rb_tree_node_base::_S_minimum(__x); }
706 static _Const_Base_ptr
707 _S_minimum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
708 { return _Rb_tree_node_base::_S_minimum(__x); }
711 _S_maximum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
712 { return _Rb_tree_node_base::_S_maximum(__x); }
714 static _Const_Base_ptr
715 _S_maximum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
716 { return _Rb_tree_node_base::_S_maximum(__x); }
719 typedef _Rb_tree_iterator<value_type> iterator;
720 typedef _Rb_tree_const_iterator<value_type> const_iterator;
722 typedef std::reverse_iterator<iterator> reverse_iterator;
723 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
726 pair<_Base_ptr, _Base_ptr>
727 _M_get_insert_unique_pos(const key_type& __k);
729 pair<_Base_ptr, _Base_ptr>
730 _M_get_insert_equal_pos(const key_type& __k);
732 pair<_Base_ptr, _Base_ptr>
733 _M_get_insert_hint_unique_pos(const_iterator __pos,
734 const key_type& __k);
736 pair<_Base_ptr, _Base_ptr>
737 _M_get_insert_hint_equal_pos(const_iterator __pos,
738 const key_type& __k);
740 #if __cplusplus >= 201103L
741 template<typename _Arg, typename _NodeGen>
743 _M_insert_(_Base_ptr __x, _Base_ptr __y, _Arg&& __v, _NodeGen&);
746 _M_insert_node(_Base_ptr __x, _Base_ptr __y, _Link_type __z);
748 template<typename _Arg>
750 _M_insert_lower(_Base_ptr __y, _Arg&& __v);
752 template<typename _Arg>
754 _M_insert_equal_lower(_Arg&& __x);
757 _M_insert_lower_node(_Base_ptr __p, _Link_type __z);
760 _M_insert_equal_lower_node(_Link_type __z);
762 template<typename _NodeGen>
764 _M_insert_(_Base_ptr __x, _Base_ptr __y,
765 const value_type& __v, _NodeGen&);
767 // _GLIBCXX_RESOLVE_LIB_DEFECTS
768 // 233. Insertion hints in associative containers.
770 _M_insert_lower(_Base_ptr __y, const value_type& __v);
773 _M_insert_equal_lower(const value_type& __x);
776 template<typename _NodeGen>
778 _M_copy(_Const_Link_type __x, _Link_type __p, _NodeGen&);
781 _M_copy(_Const_Link_type __x, _Link_type __p)
783 _Alloc_node __an(*this);
784 return _M_copy(__x, __p, __an);
788 _M_erase(_Link_type __x);
791 _M_lower_bound(_Link_type __x, _Link_type __y,
795 _M_lower_bound(_Const_Link_type __x, _Const_Link_type __y,
796 const _Key& __k) const;
799 _M_upper_bound(_Link_type __x, _Link_type __y,
803 _M_upper_bound(_Const_Link_type __x, _Const_Link_type __y,
804 const _Key& __k) const;
807 // allocation/deallocation
810 _Rb_tree(const _Compare& __comp,
811 const allocator_type& __a = allocator_type())
812 : _M_impl(__comp, _Node_allocator(__a)) { }
814 _Rb_tree(const _Rb_tree& __x)
815 : _M_impl(__x._M_impl._M_key_compare,
816 _Alloc_traits::_S_select_on_copy(__x._M_get_Node_allocator()))
818 if (__x._M_root() != 0)
820 _M_root() = _M_copy(__x._M_begin(), _M_end());
821 _M_leftmost() = _S_minimum(_M_root());
822 _M_rightmost() = _S_maximum(_M_root());
823 _M_impl._M_node_count = __x._M_impl._M_node_count;
827 #if __cplusplus >= 201103L
828 _Rb_tree(const allocator_type& __a)
829 : _M_impl(_Compare(), _Node_allocator(__a))
832 _Rb_tree(const _Rb_tree& __x, const allocator_type& __a)
833 : _M_impl(__x._M_impl._M_key_compare, _Node_allocator(__a))
835 if (__x._M_root() != nullptr)
837 _M_root() = _M_copy(__x._M_begin(), _M_end());
838 _M_leftmost() = _S_minimum(_M_root());
839 _M_rightmost() = _S_maximum(_M_root());
840 _M_impl._M_node_count = __x._M_impl._M_node_count;
844 _Rb_tree(_Rb_tree&& __x)
845 : _M_impl(__x._M_impl._M_key_compare, __x._M_get_Node_allocator())
847 if (__x._M_root() != 0)
848 _M_move_data(__x, std::true_type());
851 _Rb_tree(_Rb_tree&& __x, const allocator_type& __a)
852 : _Rb_tree(std::move(__x), _Node_allocator(__a))
855 _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a);
858 ~_Rb_tree() _GLIBCXX_NOEXCEPT
859 { _M_erase(_M_begin()); }
862 operator=(const _Rb_tree& __x);
867 { return _M_impl._M_key_compare; }
870 begin() _GLIBCXX_NOEXCEPT
872 return iterator(static_cast<_Link_type>
873 (this->_M_impl._M_header._M_left));
877 begin() const _GLIBCXX_NOEXCEPT
879 return const_iterator(static_cast<_Const_Link_type>
880 (this->_M_impl._M_header._M_left));
884 end() _GLIBCXX_NOEXCEPT
885 { return iterator(static_cast<_Link_type>(&this->_M_impl._M_header)); }
888 end() const _GLIBCXX_NOEXCEPT
890 return const_iterator(static_cast<_Const_Link_type>
891 (&this->_M_impl._M_header));
895 rbegin() _GLIBCXX_NOEXCEPT
896 { return reverse_iterator(end()); }
898 const_reverse_iterator
899 rbegin() const _GLIBCXX_NOEXCEPT
900 { return const_reverse_iterator(end()); }
903 rend() _GLIBCXX_NOEXCEPT
904 { return reverse_iterator(begin()); }
906 const_reverse_iterator
907 rend() const _GLIBCXX_NOEXCEPT
908 { return const_reverse_iterator(begin()); }
911 empty() const _GLIBCXX_NOEXCEPT
912 { return _M_impl._M_node_count == 0; }
915 size() const _GLIBCXX_NOEXCEPT
916 { return _M_impl._M_node_count; }
919 max_size() const _GLIBCXX_NOEXCEPT
920 { return _Alloc_traits::max_size(_M_get_Node_allocator()); }
923 #if __cplusplus >= 201103L
924 swap(_Rb_tree& __t) noexcept(_Alloc_traits::_S_nothrow_swap());
930 #if __cplusplus >= 201103L
931 template<typename _Arg>
933 _M_insert_unique(_Arg&& __x);
935 template<typename _Arg>
937 _M_insert_equal(_Arg&& __x);
939 template<typename _Arg, typename _NodeGen>
941 _M_insert_unique_(const_iterator __pos, _Arg&& __x, _NodeGen&);
943 template<typename _Arg>
945 _M_insert_unique_(const_iterator __pos, _Arg&& __x)
947 _Alloc_node __an(*this);
948 return _M_insert_unique_(__pos, std::forward<_Arg>(__x), __an);
951 template<typename _Arg, typename _NodeGen>
953 _M_insert_equal_(const_iterator __pos, _Arg&& __x, _NodeGen&);
955 template<typename _Arg>
957 _M_insert_equal_(const_iterator __pos, _Arg&& __x)
959 _Alloc_node __an(*this);
960 return _M_insert_equal_(__pos, std::forward<_Arg>(__x), __an);
963 template<typename... _Args>
965 _M_emplace_unique(_Args&&... __args);
967 template<typename... _Args>
969 _M_emplace_equal(_Args&&... __args);
971 template<typename... _Args>
973 _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args);
975 template<typename... _Args>
977 _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args);
980 _M_insert_unique(const value_type& __x);
983 _M_insert_equal(const value_type& __x);
985 template<typename _NodeGen>
987 _M_insert_unique_(const_iterator __pos, const value_type& __x,
991 _M_insert_unique_(const_iterator __pos, const value_type& __x)
993 _Alloc_node __an(*this);
994 return _M_insert_unique_(__pos, __x, __an);
997 template<typename _NodeGen>
999 _M_insert_equal_(const_iterator __pos, const value_type& __x,
1002 _M_insert_equal_(const_iterator __pos, const value_type& __x)
1004 _Alloc_node __an(*this);
1005 return _M_insert_equal_(__pos, __x, __an);
1009 template<typename _InputIterator>
1011 _M_insert_unique(_InputIterator __first, _InputIterator __last);
1013 template<typename _InputIterator>
1015 _M_insert_equal(_InputIterator __first, _InputIterator __last);
1019 _M_erase_aux(const_iterator __position);
1022 _M_erase_aux(const_iterator __first, const_iterator __last);
1025 #if __cplusplus >= 201103L
1026 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1027 // DR 130. Associative erase should return an iterator.
1028 _GLIBCXX_ABI_TAG_CXX11
1030 erase(const_iterator __position)
1032 const_iterator __result = __position;
1034 _M_erase_aux(__position);
1035 return __result._M_const_cast();
1039 _GLIBCXX_ABI_TAG_CXX11
1041 erase(iterator __position)
1043 iterator __result = __position;
1045 _M_erase_aux(__position);
1050 erase(iterator __position)
1051 { _M_erase_aux(__position); }
1054 erase(const_iterator __position)
1055 { _M_erase_aux(__position); }
1058 erase(const key_type& __x);
1060 #if __cplusplus >= 201103L
1061 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1062 // DR 130. Associative erase should return an iterator.
1063 _GLIBCXX_ABI_TAG_CXX11
1065 erase(const_iterator __first, const_iterator __last)
1067 _M_erase_aux(__first, __last);
1068 return __last._M_const_cast();
1072 erase(iterator __first, iterator __last)
1073 { _M_erase_aux(__first, __last); }
1076 erase(const_iterator __first, const_iterator __last)
1077 { _M_erase_aux(__first, __last); }
1080 erase(const key_type* __first, const key_type* __last);
1083 clear() _GLIBCXX_NOEXCEPT
1085 _M_erase(_M_begin());
1091 find(const key_type& __k);
1094 find(const key_type& __k) const;
1097 count(const key_type& __k) const;
1100 lower_bound(const key_type& __k)
1101 { return _M_lower_bound(_M_begin(), _M_end(), __k); }
1104 lower_bound(const key_type& __k) const
1105 { return _M_lower_bound(_M_begin(), _M_end(), __k); }
1108 upper_bound(const key_type& __k)
1109 { return _M_upper_bound(_M_begin(), _M_end(), __k); }
1112 upper_bound(const key_type& __k) const
1113 { return _M_upper_bound(_M_begin(), _M_end(), __k); }
1115 pair<iterator, iterator>
1116 equal_range(const key_type& __k);
1118 pair<const_iterator, const_iterator>
1119 equal_range(const key_type& __k) const;
1121 #if __cplusplus > 201103L
1122 template<typename _Cmp, typename _Kt, typename = __void_t<>>
1123 struct __is_transparent { };
1125 template<typename _Cmp, typename _Kt>
1127 __is_transparent<_Cmp, _Kt, __void_t<typename _Cmp::is_transparent>>
1128 { typedef void type; };
1130 static auto _S_iter(_Link_type __x) { return iterator(__x); }
1132 static auto _S_iter(_Const_Link_type __x) { return const_iterator(__x); }
1134 template<typename _Cmp, typename _Link, typename _Kt>
1136 _S_lower_bound_tr(_Cmp& __cmp, _Link __x, _Link __y, const _Kt& __k)
1139 if (!__cmp(_S_key(__x), __k))
1140 __y = __x, __x = _S_left(__x);
1142 __x = _S_right(__x);
1143 return _S_iter(__y);
1146 template<typename _Cmp, typename _Link, typename _Kt>
1148 _S_upper_bound_tr(_Cmp& __cmp, _Link __x, _Link __y, const _Kt& __k)
1151 if (__cmp(__k, _S_key(__x)))
1152 __y = __x, __x = _S_left(__x);
1154 __x = _S_right(__x);
1155 return _S_iter(__y);
1158 template<typename _Kt,
1159 typename _Req = typename __is_transparent<_Compare, _Kt>::type>
1161 _M_find_tr(const _Kt& __k)
1163 auto& __cmp = _M_impl._M_key_compare;
1164 auto __j = _S_lower_bound_tr(__cmp, _M_begin(), _M_end(), __k);
1165 return (__j == end() || __cmp(__k, _S_key(__j._M_node)))
1169 template<typename _Kt,
1170 typename _Req = typename __is_transparent<_Compare, _Kt>::type>
1172 _M_find_tr(const _Kt& __k) const
1174 auto& __cmp = _M_impl._M_key_compare;
1175 auto __j = _S_lower_bound_tr(__cmp, _M_begin(), _M_end(), __k);
1176 return (__j == end() || __cmp(__k, _S_key(__j._M_node)))
1180 template<typename _Kt,
1181 typename _Req = typename __is_transparent<_Compare, _Kt>::type>
1183 _M_count_tr(const _Kt& __k) const
1185 auto __p = _M_equal_range_tr(__k);
1186 return std::distance(__p.first, __p.second);
1189 template<typename _Kt,
1190 typename _Req = typename __is_transparent<_Compare, _Kt>::type>
1192 _M_lower_bound_tr(const _Kt& __k)
1194 auto& __cmp = _M_impl._M_key_compare;
1195 return _S_lower_bound_tr(__cmp, _M_begin(), _M_end(), __k);
1198 template<typename _Kt,
1199 typename _Req = typename __is_transparent<_Compare, _Kt>::type>
1201 _M_lower_bound_tr(const _Kt& __k) const
1203 auto& __cmp = _M_impl._M_key_compare;
1204 return _S_lower_bound_tr(__cmp, _M_begin(), _M_end(), __k);
1207 template<typename _Kt,
1208 typename _Req = typename __is_transparent<_Compare, _Kt>::type>
1210 _M_upper_bound_tr(const _Kt& __k)
1212 auto& __cmp = _M_impl._M_key_compare;
1213 return _S_upper_bound_tr(__cmp, _M_begin(), _M_end(), __k);
1216 template<typename _Kt,
1217 typename _Req = typename __is_transparent<_Compare, _Kt>::type>
1219 _M_upper_bound_tr(const _Kt& __k) const
1221 auto& __cmp = _M_impl._M_key_compare;
1222 return _S_upper_bound_tr(__cmp, _M_begin(), _M_end(), __k);
1225 template<typename _Kt,
1226 typename _Req = typename __is_transparent<_Compare, _Kt>::type>
1227 pair<iterator, iterator>
1228 _M_equal_range_tr(const _Kt& __k)
1230 auto __low = _M_lower_bound_tr(__k);
1231 auto __high = __low;
1232 auto& __cmp = _M_impl._M_key_compare;
1233 while (__high != end() && !__cmp(__k, _S_key(__high._M_node)))
1235 return { __low, __high };
1238 template<typename _Kt,
1239 typename _Req = typename __is_transparent<_Compare, _Kt>::type>
1240 pair<const_iterator, const_iterator>
1241 _M_equal_range_tr(const _Kt& __k) const
1243 auto __low = _M_lower_bound_tr(__k);
1244 auto __high = __low;
1245 auto& __cmp = _M_impl._M_key_compare;
1246 while (__high != end() && !__cmp(__k, _S_key(__high._M_node)))
1248 return { __low, __high };
1254 __rb_verify() const;
1256 #if __cplusplus >= 201103L
1258 operator=(_Rb_tree&&) noexcept(_Alloc_traits::_S_nothrow_move());
1260 template<typename _Iterator>
1262 _M_assign_unique(_Iterator, _Iterator);
1264 template<typename _Iterator>
1266 _M_assign_equal(_Iterator, _Iterator);
1269 // Move elements from container with equal allocator.
1271 _M_move_data(_Rb_tree&, std::true_type);
1273 // Move elements from container with possibly non-equal allocator,
1274 // which might result in a copy not a move.
1276 _M_move_data(_Rb_tree&, std::false_type);
1280 template<typename _Key, typename _Val, typename _KeyOfValue,
1281 typename _Compare, typename _Alloc>
1283 operator==(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
1284 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
1286 return __x.size() == __y.size()
1287 && std::equal(__x.begin(), __x.end(), __y.begin());
1290 template<typename _Key, typename _Val, typename _KeyOfValue,
1291 typename _Compare, typename _Alloc>
1293 operator<(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
1294 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
1296 return std::lexicographical_compare(__x.begin(), __x.end(),
1297 __y.begin(), __y.end());
1300 template<typename _Key, typename _Val, typename _KeyOfValue,
1301 typename _Compare, typename _Alloc>
1303 operator!=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
1304 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
1305 { return !(__x == __y); }
1307 template<typename _Key, typename _Val, typename _KeyOfValue,
1308 typename _Compare, typename _Alloc>
1310 operator>(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
1311 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
1312 { return __y < __x; }
1314 template<typename _Key, typename _Val, typename _KeyOfValue,
1315 typename _Compare, typename _Alloc>
1317 operator<=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
1318 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
1319 { return !(__y < __x); }
1321 template<typename _Key, typename _Val, typename _KeyOfValue,
1322 typename _Compare, typename _Alloc>
1324 operator>=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
1325 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
1326 { return !(__x < __y); }
1328 template<typename _Key, typename _Val, typename _KeyOfValue,
1329 typename _Compare, typename _Alloc>
1331 swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
1332 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
1335 #if __cplusplus >= 201103L
1336 template<typename _Key, typename _Val, typename _KeyOfValue,
1337 typename _Compare, typename _Alloc>
1338 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1339 _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a)
1340 : _M_impl(__x._M_impl._M_key_compare, std::move(__a))
1342 using __eq = integral_constant<bool, _Alloc_traits::_S_always_equal()>;
1343 if (__x._M_root() != nullptr)
1344 _M_move_data(__x, __eq());
1347 template<typename _Key, typename _Val, typename _KeyOfValue,
1348 typename _Compare, typename _Alloc>
1350 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1351 _M_move_data(_Rb_tree& __x, std::true_type)
1353 _M_root() = __x._M_root();
1354 _M_leftmost() = __x._M_leftmost();
1355 _M_rightmost() = __x._M_rightmost();
1356 _M_root()->_M_parent = _M_end();
1359 __x._M_leftmost() = __x._M_end();
1360 __x._M_rightmost() = __x._M_end();
1362 this->_M_impl._M_node_count = __x._M_impl._M_node_count;
1363 __x._M_impl._M_node_count = 0;
1366 template<typename _Key, typename _Val, typename _KeyOfValue,
1367 typename _Compare, typename _Alloc>
1369 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1370 _M_move_data(_Rb_tree& __x, std::false_type)
1372 if (_M_get_Node_allocator() == __x._M_get_Node_allocator())
1373 _M_move_data(__x, std::true_type());
1376 _Alloc_node __an(*this);
1378 [&__an](const value_type& __cval)
1380 auto& __val = const_cast<value_type&>(__cval);
1381 return __an(std::move_if_noexcept(__val));
1383 _M_root() = _M_copy(__x._M_begin(), _M_end(), __lbd);
1384 _M_leftmost() = _S_minimum(_M_root());
1385 _M_rightmost() = _S_maximum(_M_root());
1386 _M_impl._M_node_count = __x._M_impl._M_node_count;
1390 template<typename _Key, typename _Val, typename _KeyOfValue,
1391 typename _Compare, typename _Alloc>
1392 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
1393 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1394 operator=(_Rb_tree&& __x)
1395 noexcept(_Alloc_traits::_S_nothrow_move())
1397 _M_impl._M_key_compare = __x._M_impl._M_key_compare;
1398 if (_Alloc_traits::_S_propagate_on_move_assign()
1399 || _Alloc_traits::_S_always_equal()
1400 || _M_get_Node_allocator() == __x._M_get_Node_allocator())
1403 if (__x._M_root() != nullptr)
1404 _M_move_data(__x, std::true_type());
1405 std::__alloc_on_move(_M_get_Node_allocator(),
1406 __x._M_get_Node_allocator());
1410 // Try to move each node reusing existing nodes and copying __x nodes
1412 _Reuse_or_alloc_node __roan(*this);
1414 if (__x._M_root() != nullptr)
1417 [&__roan](const value_type& __cval)
1419 auto& __val = const_cast<value_type&>(__cval);
1420 return __roan(std::move_if_noexcept(__val));
1422 _M_root() = _M_copy(__x._M_begin(), _M_end(), __lbd);
1423 _M_leftmost() = _S_minimum(_M_root());
1424 _M_rightmost() = _S_maximum(_M_root());
1425 _M_impl._M_node_count = __x._M_impl._M_node_count;
1431 template<typename _Key, typename _Val, typename _KeyOfValue,
1432 typename _Compare, typename _Alloc>
1433 template<typename _Iterator>
1435 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1436 _M_assign_unique(_Iterator __first, _Iterator __last)
1438 _Reuse_or_alloc_node __roan(*this);
1440 for (; __first != __last; ++__first)
1441 _M_insert_unique_(end(), *__first, __roan);
1444 template<typename _Key, typename _Val, typename _KeyOfValue,
1445 typename _Compare, typename _Alloc>
1446 template<typename _Iterator>
1448 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1449 _M_assign_equal(_Iterator __first, _Iterator __last)
1451 _Reuse_or_alloc_node __roan(*this);
1453 for (; __first != __last; ++__first)
1454 _M_insert_equal_(end(), *__first, __roan);
1458 template<typename _Key, typename _Val, typename _KeyOfValue,
1459 typename _Compare, typename _Alloc>
1460 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
1461 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1462 operator=(const _Rb_tree& __x)
1466 // Note that _Key may be a constant type.
1467 #if __cplusplus >= 201103L
1468 if (_Alloc_traits::_S_propagate_on_copy_assign())
1470 auto& __this_alloc = this->_M_get_Node_allocator();
1471 auto& __that_alloc = __x._M_get_Node_allocator();
1472 if (!_Alloc_traits::_S_always_equal()
1473 && __this_alloc != __that_alloc)
1475 // Replacement allocator cannot free existing storage, we need
1476 // to erase nodes first.
1478 std::__alloc_on_copy(__this_alloc, __that_alloc);
1483 _Reuse_or_alloc_node __roan(*this);
1485 _M_impl._M_key_compare = __x._M_impl._M_key_compare;
1486 if (__x._M_root() != 0)
1488 _M_root() = _M_copy(__x._M_begin(), _M_end(), __roan);
1489 _M_leftmost() = _S_minimum(_M_root());
1490 _M_rightmost() = _S_maximum(_M_root());
1491 _M_impl._M_node_count = __x._M_impl._M_node_count;
1498 template<typename _Key, typename _Val, typename _KeyOfValue,
1499 typename _Compare, typename _Alloc>
1500 #if __cplusplus >= 201103L
1501 template<typename _Arg, typename _NodeGen>
1503 template<typename _NodeGen>
1505 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1506 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1507 _M_insert_(_Base_ptr __x, _Base_ptr __p,
1508 #if __cplusplus >= 201103L
1513 _NodeGen& __node_gen)
1515 bool __insert_left = (__x != 0 || __p == _M_end()
1516 || _M_impl._M_key_compare(_KeyOfValue()(__v),
1519 _Link_type __z = __node_gen(_GLIBCXX_FORWARD(_Arg, __v));
1521 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
1522 this->_M_impl._M_header);
1523 ++_M_impl._M_node_count;
1524 return iterator(__z);
1527 template<typename _Key, typename _Val, typename _KeyOfValue,
1528 typename _Compare, typename _Alloc>
1529 #if __cplusplus >= 201103L
1530 template<typename _Arg>
1532 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1533 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1534 #if __cplusplus >= 201103L
1535 _M_insert_lower(_Base_ptr __p, _Arg&& __v)
1537 _M_insert_lower(_Base_ptr __p, const _Val& __v)
1540 bool __insert_left = (__p == _M_end()
1541 || !_M_impl._M_key_compare(_S_key(__p),
1542 _KeyOfValue()(__v)));
1544 _Link_type __z = _M_create_node(_GLIBCXX_FORWARD(_Arg, __v));
1546 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
1547 this->_M_impl._M_header);
1548 ++_M_impl._M_node_count;
1549 return iterator(__z);
1552 template<typename _Key, typename _Val, typename _KeyOfValue,
1553 typename _Compare, typename _Alloc>
1554 #if __cplusplus >= 201103L
1555 template<typename _Arg>
1557 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1558 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1559 #if __cplusplus >= 201103L
1560 _M_insert_equal_lower(_Arg&& __v)
1562 _M_insert_equal_lower(const _Val& __v)
1565 _Link_type __x = _M_begin();
1566 _Link_type __y = _M_end();
1570 __x = !_M_impl._M_key_compare(_S_key(__x), _KeyOfValue()(__v)) ?
1571 _S_left(__x) : _S_right(__x);
1573 return _M_insert_lower(__y, _GLIBCXX_FORWARD(_Arg, __v));
1576 template<typename _Key, typename _Val, typename _KoV,
1577 typename _Compare, typename _Alloc>
1578 template<typename _NodeGen>
1579 typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Link_type
1580 _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
1581 _M_copy(_Const_Link_type __x, _Link_type __p, _NodeGen& __node_gen)
1583 // Structural copy. __x and __p must be non-null.
1584 _Link_type __top = _M_clone_node(__x, __node_gen);
1585 __top->_M_parent = __p;
1590 __top->_M_right = _M_copy(_S_right(__x), __top, __node_gen);
1596 _Link_type __y = _M_clone_node(__x, __node_gen);
1598 __y->_M_parent = __p;
1600 __y->_M_right = _M_copy(_S_right(__x), __y, __node_gen);
1608 __throw_exception_again;
1613 template<typename _Key, typename _Val, typename _KeyOfValue,
1614 typename _Compare, typename _Alloc>
1616 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1617 _M_erase(_Link_type __x)
1619 // Erase without rebalancing.
1622 _M_erase(_S_right(__x));
1623 _Link_type __y = _S_left(__x);
1629 template<typename _Key, typename _Val, typename _KeyOfValue,
1630 typename _Compare, typename _Alloc>
1631 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1632 _Compare, _Alloc>::iterator
1633 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1634 _M_lower_bound(_Link_type __x, _Link_type __y,
1638 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1639 __y = __x, __x = _S_left(__x);
1641 __x = _S_right(__x);
1642 return iterator(__y);
1645 template<typename _Key, typename _Val, typename _KeyOfValue,
1646 typename _Compare, typename _Alloc>
1647 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1648 _Compare, _Alloc>::const_iterator
1649 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1650 _M_lower_bound(_Const_Link_type __x, _Const_Link_type __y,
1651 const _Key& __k) const
1654 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1655 __y = __x, __x = _S_left(__x);
1657 __x = _S_right(__x);
1658 return const_iterator(__y);
1661 template<typename _Key, typename _Val, typename _KeyOfValue,
1662 typename _Compare, typename _Alloc>
1663 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1664 _Compare, _Alloc>::iterator
1665 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1666 _M_upper_bound(_Link_type __x, _Link_type __y,
1670 if (_M_impl._M_key_compare(__k, _S_key(__x)))
1671 __y = __x, __x = _S_left(__x);
1673 __x = _S_right(__x);
1674 return iterator(__y);
1677 template<typename _Key, typename _Val, typename _KeyOfValue,
1678 typename _Compare, typename _Alloc>
1679 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1680 _Compare, _Alloc>::const_iterator
1681 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1682 _M_upper_bound(_Const_Link_type __x, _Const_Link_type __y,
1683 const _Key& __k) const
1686 if (_M_impl._M_key_compare(__k, _S_key(__x)))
1687 __y = __x, __x = _S_left(__x);
1689 __x = _S_right(__x);
1690 return const_iterator(__y);
1693 template<typename _Key, typename _Val, typename _KeyOfValue,
1694 typename _Compare, typename _Alloc>
1695 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1696 _Compare, _Alloc>::iterator,
1697 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1698 _Compare, _Alloc>::iterator>
1699 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1700 equal_range(const _Key& __k)
1702 _Link_type __x = _M_begin();
1703 _Link_type __y = _M_end();
1706 if (_M_impl._M_key_compare(_S_key(__x), __k))
1707 __x = _S_right(__x);
1708 else if (_M_impl._M_key_compare(__k, _S_key(__x)))
1709 __y = __x, __x = _S_left(__x);
1712 _Link_type __xu(__x), __yu(__y);
1713 __y = __x, __x = _S_left(__x);
1714 __xu = _S_right(__xu);
1715 return pair<iterator,
1716 iterator>(_M_lower_bound(__x, __y, __k),
1717 _M_upper_bound(__xu, __yu, __k));
1720 return pair<iterator, iterator>(iterator(__y),
1724 template<typename _Key, typename _Val, typename _KeyOfValue,
1725 typename _Compare, typename _Alloc>
1726 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1727 _Compare, _Alloc>::const_iterator,
1728 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1729 _Compare, _Alloc>::const_iterator>
1730 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1731 equal_range(const _Key& __k) const
1733 _Const_Link_type __x = _M_begin();
1734 _Const_Link_type __y = _M_end();
1737 if (_M_impl._M_key_compare(_S_key(__x), __k))
1738 __x = _S_right(__x);
1739 else if (_M_impl._M_key_compare(__k, _S_key(__x)))
1740 __y = __x, __x = _S_left(__x);
1743 _Const_Link_type __xu(__x), __yu(__y);
1744 __y = __x, __x = _S_left(__x);
1745 __xu = _S_right(__xu);
1746 return pair<const_iterator,
1747 const_iterator>(_M_lower_bound(__x, __y, __k),
1748 _M_upper_bound(__xu, __yu, __k));
1751 return pair<const_iterator, const_iterator>(const_iterator(__y),
1752 const_iterator(__y));
1755 template<typename _Key, typename _Val, typename _KeyOfValue,
1756 typename _Compare, typename _Alloc>
1758 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1759 swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __t)
1760 #if __cplusplus >= 201103L
1761 noexcept(_Alloc_traits::_S_nothrow_swap())
1766 if (__t._M_root() != 0)
1768 _M_root() = __t._M_root();
1769 _M_leftmost() = __t._M_leftmost();
1770 _M_rightmost() = __t._M_rightmost();
1771 _M_root()->_M_parent = _M_end();
1772 _M_impl._M_node_count = __t._M_impl._M_node_count;
1774 __t._M_impl._M_reset();
1777 else if (__t._M_root() == 0)
1779 __t._M_root() = _M_root();
1780 __t._M_leftmost() = _M_leftmost();
1781 __t._M_rightmost() = _M_rightmost();
1782 __t._M_root()->_M_parent = __t._M_end();
1783 __t._M_impl._M_node_count = _M_impl._M_node_count;
1789 std::swap(_M_root(),__t._M_root());
1790 std::swap(_M_leftmost(),__t._M_leftmost());
1791 std::swap(_M_rightmost(),__t._M_rightmost());
1793 _M_root()->_M_parent = _M_end();
1794 __t._M_root()->_M_parent = __t._M_end();
1795 std::swap(this->_M_impl._M_node_count, __t._M_impl._M_node_count);
1797 // No need to swap header's color as it does not change.
1798 std::swap(this->_M_impl._M_key_compare, __t._M_impl._M_key_compare);
1800 _Alloc_traits::_S_on_swap(_M_get_Node_allocator(),
1801 __t._M_get_Node_allocator());
1804 template<typename _Key, typename _Val, typename _KeyOfValue,
1805 typename _Compare, typename _Alloc>
1806 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1807 _Compare, _Alloc>::_Base_ptr,
1808 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1809 _Compare, _Alloc>::_Base_ptr>
1810 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1811 _M_get_insert_unique_pos(const key_type& __k)
1813 typedef pair<_Base_ptr, _Base_ptr> _Res;
1814 _Link_type __x = _M_begin();
1815 _Link_type __y = _M_end();
1820 __comp = _M_impl._M_key_compare(__k, _S_key(__x));
1821 __x = __comp ? _S_left(__x) : _S_right(__x);
1823 iterator __j = iterator(__y);
1827 return _Res(__x, __y);
1831 if (_M_impl._M_key_compare(_S_key(__j._M_node), __k))
1832 return _Res(__x, __y);
1833 return _Res(__j._M_node, 0);
1836 template<typename _Key, typename _Val, typename _KeyOfValue,
1837 typename _Compare, typename _Alloc>
1838 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1839 _Compare, _Alloc>::_Base_ptr,
1840 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1841 _Compare, _Alloc>::_Base_ptr>
1842 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1843 _M_get_insert_equal_pos(const key_type& __k)
1845 typedef pair<_Base_ptr, _Base_ptr> _Res;
1846 _Link_type __x = _M_begin();
1847 _Link_type __y = _M_end();
1851 __x = _M_impl._M_key_compare(__k, _S_key(__x)) ?
1852 _S_left(__x) : _S_right(__x);
1854 return _Res(__x, __y);
1857 template<typename _Key, typename _Val, typename _KeyOfValue,
1858 typename _Compare, typename _Alloc>
1859 #if __cplusplus >= 201103L
1860 template<typename _Arg>
1862 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1863 _Compare, _Alloc>::iterator, bool>
1864 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1865 #if __cplusplus >= 201103L
1866 _M_insert_unique(_Arg&& __v)
1868 _M_insert_unique(const _Val& __v)
1871 typedef pair<iterator, bool> _Res;
1872 pair<_Base_ptr, _Base_ptr> __res
1873 = _M_get_insert_unique_pos(_KeyOfValue()(__v));
1877 _Alloc_node __an(*this);
1878 return _Res(_M_insert_(__res.first, __res.second,
1879 _GLIBCXX_FORWARD(_Arg, __v), __an),
1883 return _Res(iterator(static_cast<_Link_type>(__res.first)), false);
1886 template<typename _Key, typename _Val, typename _KeyOfValue,
1887 typename _Compare, typename _Alloc>
1888 #if __cplusplus >= 201103L
1889 template<typename _Arg>
1891 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1892 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1893 #if __cplusplus >= 201103L
1894 _M_insert_equal(_Arg&& __v)
1896 _M_insert_equal(const _Val& __v)
1899 pair<_Base_ptr, _Base_ptr> __res
1900 = _M_get_insert_equal_pos(_KeyOfValue()(__v));
1901 _Alloc_node __an(*this);
1902 return _M_insert_(__res.first, __res.second,
1903 _GLIBCXX_FORWARD(_Arg, __v), __an);
1906 template<typename _Key, typename _Val, typename _KeyOfValue,
1907 typename _Compare, typename _Alloc>
1908 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1909 _Compare, _Alloc>::_Base_ptr,
1910 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1911 _Compare, _Alloc>::_Base_ptr>
1912 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1913 _M_get_insert_hint_unique_pos(const_iterator __position,
1914 const key_type& __k)
1916 iterator __pos = __position._M_const_cast();
1917 typedef pair<_Base_ptr, _Base_ptr> _Res;
1920 if (__pos._M_node == _M_end())
1923 && _M_impl._M_key_compare(_S_key(_M_rightmost()), __k))
1924 return _Res(0, _M_rightmost());
1926 return _M_get_insert_unique_pos(__k);
1928 else if (_M_impl._M_key_compare(__k, _S_key(__pos._M_node)))
1930 // First, try before...
1931 iterator __before = __pos;
1932 if (__pos._M_node == _M_leftmost()) // begin()
1933 return _Res(_M_leftmost(), _M_leftmost());
1934 else if (_M_impl._M_key_compare(_S_key((--__before)._M_node), __k))
1936 if (_S_right(__before._M_node) == 0)
1937 return _Res(0, __before._M_node);
1939 return _Res(__pos._M_node, __pos._M_node);
1942 return _M_get_insert_unique_pos(__k);
1944 else if (_M_impl._M_key_compare(_S_key(__pos._M_node), __k))
1946 // ... then try after.
1947 iterator __after = __pos;
1948 if (__pos._M_node == _M_rightmost())
1949 return _Res(0, _M_rightmost());
1950 else if (_M_impl._M_key_compare(__k, _S_key((++__after)._M_node)))
1952 if (_S_right(__pos._M_node) == 0)
1953 return _Res(0, __pos._M_node);
1955 return _Res(__after._M_node, __after._M_node);
1958 return _M_get_insert_unique_pos(__k);
1962 return _Res(__pos._M_node, 0);
1965 template<typename _Key, typename _Val, typename _KeyOfValue,
1966 typename _Compare, typename _Alloc>
1967 #if __cplusplus >= 201103L
1968 template<typename _Arg, typename _NodeGen>
1970 template<typename _NodeGen>
1972 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1973 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1974 _M_insert_unique_(const_iterator __position,
1975 #if __cplusplus >= 201103L
1980 _NodeGen& __node_gen)
1982 pair<_Base_ptr, _Base_ptr> __res
1983 = _M_get_insert_hint_unique_pos(__position, _KeyOfValue()(__v));
1986 return _M_insert_(__res.first, __res.second,
1987 _GLIBCXX_FORWARD(_Arg, __v),
1989 return iterator(static_cast<_Link_type>(__res.first));
1992 template<typename _Key, typename _Val, typename _KeyOfValue,
1993 typename _Compare, typename _Alloc>
1994 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1995 _Compare, _Alloc>::_Base_ptr,
1996 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1997 _Compare, _Alloc>::_Base_ptr>
1998 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1999 _M_get_insert_hint_equal_pos(const_iterator __position, const key_type& __k)
2001 iterator __pos = __position._M_const_cast();
2002 typedef pair<_Base_ptr, _Base_ptr> _Res;
2005 if (__pos._M_node == _M_end())
2008 && !_M_impl._M_key_compare(__k, _S_key(_M_rightmost())))
2009 return _Res(0, _M_rightmost());
2011 return _M_get_insert_equal_pos(__k);
2013 else if (!_M_impl._M_key_compare(_S_key(__pos._M_node), __k))
2015 // First, try before...
2016 iterator __before = __pos;
2017 if (__pos._M_node == _M_leftmost()) // begin()
2018 return _Res(_M_leftmost(), _M_leftmost());
2019 else if (!_M_impl._M_key_compare(__k, _S_key((--__before)._M_node)))
2021 if (_S_right(__before._M_node) == 0)
2022 return _Res(0, __before._M_node);
2024 return _Res(__pos._M_node, __pos._M_node);
2027 return _M_get_insert_equal_pos(__k);
2031 // ... then try after.
2032 iterator __after = __pos;
2033 if (__pos._M_node == _M_rightmost())
2034 return _Res(0, _M_rightmost());
2035 else if (!_M_impl._M_key_compare(_S_key((++__after)._M_node), __k))
2037 if (_S_right(__pos._M_node) == 0)
2038 return _Res(0, __pos._M_node);
2040 return _Res(__after._M_node, __after._M_node);
2047 template<typename _Key, typename _Val, typename _KeyOfValue,
2048 typename _Compare, typename _Alloc>
2049 #if __cplusplus >= 201103L
2050 template<typename _Arg, typename _NodeGen>
2052 template<typename _NodeGen>
2054 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2055 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2056 _M_insert_equal_(const_iterator __position,
2057 #if __cplusplus >= 201103L
2062 _NodeGen& __node_gen)
2064 pair<_Base_ptr, _Base_ptr> __res
2065 = _M_get_insert_hint_equal_pos(__position, _KeyOfValue()(__v));
2068 return _M_insert_(__res.first, __res.second,
2069 _GLIBCXX_FORWARD(_Arg, __v),
2072 return _M_insert_equal_lower(_GLIBCXX_FORWARD(_Arg, __v));
2075 #if __cplusplus >= 201103L
2076 template<typename _Key, typename _Val, typename _KeyOfValue,
2077 typename _Compare, typename _Alloc>
2078 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2079 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2080 _M_insert_node(_Base_ptr __x, _Base_ptr __p, _Link_type __z)
2082 bool __insert_left = (__x != 0 || __p == _M_end()
2083 || _M_impl._M_key_compare(_S_key(__z),
2086 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
2087 this->_M_impl._M_header);
2088 ++_M_impl._M_node_count;
2089 return iterator(__z);
2092 template<typename _Key, typename _Val, typename _KeyOfValue,
2093 typename _Compare, typename _Alloc>
2094 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2095 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2096 _M_insert_lower_node(_Base_ptr __p, _Link_type __z)
2098 bool __insert_left = (__p == _M_end()
2099 || !_M_impl._M_key_compare(_S_key(__p),
2102 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
2103 this->_M_impl._M_header);
2104 ++_M_impl._M_node_count;
2105 return iterator(__z);
2108 template<typename _Key, typename _Val, typename _KeyOfValue,
2109 typename _Compare, typename _Alloc>
2110 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2111 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2112 _M_insert_equal_lower_node(_Link_type __z)
2114 _Link_type __x = _M_begin();
2115 _Link_type __y = _M_end();
2119 __x = !_M_impl._M_key_compare(_S_key(__x), _S_key(__z)) ?
2120 _S_left(__x) : _S_right(__x);
2122 return _M_insert_lower_node(__y, __z);
2125 template<typename _Key, typename _Val, typename _KeyOfValue,
2126 typename _Compare, typename _Alloc>
2127 template<typename... _Args>
2128 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
2129 _Compare, _Alloc>::iterator, bool>
2130 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2131 _M_emplace_unique(_Args&&... __args)
2133 _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
2137 typedef pair<iterator, bool> _Res;
2138 auto __res = _M_get_insert_unique_pos(_S_key(__z));
2140 return _Res(_M_insert_node(__res.first, __res.second, __z), true);
2143 return _Res(iterator(static_cast<_Link_type>(__res.first)), false);
2148 __throw_exception_again;
2152 template<typename _Key, typename _Val, typename _KeyOfValue,
2153 typename _Compare, typename _Alloc>
2154 template<typename... _Args>
2155 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2156 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2157 _M_emplace_equal(_Args&&... __args)
2159 _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
2163 auto __res = _M_get_insert_equal_pos(_S_key(__z));
2164 return _M_insert_node(__res.first, __res.second, __z);
2169 __throw_exception_again;
2173 template<typename _Key, typename _Val, typename _KeyOfValue,
2174 typename _Compare, typename _Alloc>
2175 template<typename... _Args>
2176 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2177 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2178 _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args)
2180 _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
2184 auto __res = _M_get_insert_hint_unique_pos(__pos, _S_key(__z));
2187 return _M_insert_node(__res.first, __res.second, __z);
2190 return iterator(static_cast<_Link_type>(__res.first));
2195 __throw_exception_again;
2199 template<typename _Key, typename _Val, typename _KeyOfValue,
2200 typename _Compare, typename _Alloc>
2201 template<typename... _Args>
2202 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2203 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2204 _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args)
2206 _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
2210 auto __res = _M_get_insert_hint_equal_pos(__pos, _S_key(__z));
2213 return _M_insert_node(__res.first, __res.second, __z);
2215 return _M_insert_equal_lower_node(__z);
2220 __throw_exception_again;
2225 template<typename _Key, typename _Val, typename _KoV,
2226 typename _Cmp, typename _Alloc>
2229 _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::
2230 _M_insert_unique(_II __first, _II __last)
2232 _Alloc_node __an(*this);
2233 for (; __first != __last; ++__first)
2234 _M_insert_unique_(end(), *__first, __an);
2237 template<typename _Key, typename _Val, typename _KoV,
2238 typename _Cmp, typename _Alloc>
2241 _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::
2242 _M_insert_equal(_II __first, _II __last)
2244 _Alloc_node __an(*this);
2245 for (; __first != __last; ++__first)
2246 _M_insert_equal_(end(), *__first, __an);
2249 template<typename _Key, typename _Val, typename _KeyOfValue,
2250 typename _Compare, typename _Alloc>
2252 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2253 _M_erase_aux(const_iterator __position)
2256 static_cast<_Link_type>(_Rb_tree_rebalance_for_erase
2257 (const_cast<_Base_ptr>(__position._M_node),
2258 this->_M_impl._M_header));
2260 --_M_impl._M_node_count;
2263 template<typename _Key, typename _Val, typename _KeyOfValue,
2264 typename _Compare, typename _Alloc>
2266 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2267 _M_erase_aux(const_iterator __first, const_iterator __last)
2269 if (__first == begin() && __last == end())
2272 while (__first != __last)
2276 template<typename _Key, typename _Val, typename _KeyOfValue,
2277 typename _Compare, typename _Alloc>
2278 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
2279 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2280 erase(const _Key& __x)
2282 pair<iterator, iterator> __p = equal_range(__x);
2283 const size_type __old_size = size();
2284 erase(__p.first, __p.second);
2285 return __old_size - size();
2288 template<typename _Key, typename _Val, typename _KeyOfValue,
2289 typename _Compare, typename _Alloc>
2291 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2292 erase(const _Key* __first, const _Key* __last)
2294 while (__first != __last)
2298 template<typename _Key, typename _Val, typename _KeyOfValue,
2299 typename _Compare, typename _Alloc>
2300 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2301 _Compare, _Alloc>::iterator
2302 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2303 find(const _Key& __k)
2305 iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
2306 return (__j == end()
2307 || _M_impl._M_key_compare(__k,
2308 _S_key(__j._M_node))) ? end() : __j;
2311 template<typename _Key, typename _Val, typename _KeyOfValue,
2312 typename _Compare, typename _Alloc>
2313 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2314 _Compare, _Alloc>::const_iterator
2315 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2316 find(const _Key& __k) const
2318 const_iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
2319 return (__j == end()
2320 || _M_impl._M_key_compare(__k,
2321 _S_key(__j._M_node))) ? end() : __j;
2324 template<typename _Key, typename _Val, typename _KeyOfValue,
2325 typename _Compare, typename _Alloc>
2326 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
2327 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2328 count(const _Key& __k) const
2330 pair<const_iterator, const_iterator> __p = equal_range(__k);
2331 const size_type __n = std::distance(__p.first, __p.second);
2335 _GLIBCXX_PURE unsigned int
2336 _Rb_tree_black_count(const _Rb_tree_node_base* __node,
2337 const _Rb_tree_node_base* __root) throw ();
2339 template<typename _Key, typename _Val, typename _KeyOfValue,
2340 typename _Compare, typename _Alloc>
2342 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const
2344 if (_M_impl._M_node_count == 0 || begin() == end())
2345 return _M_impl._M_node_count == 0 && begin() == end()
2346 && this->_M_impl._M_header._M_left == _M_end()
2347 && this->_M_impl._M_header._M_right == _M_end();
2349 unsigned int __len = _Rb_tree_black_count(_M_leftmost(), _M_root());
2350 for (const_iterator __it = begin(); __it != end(); ++__it)
2352 _Const_Link_type __x = static_cast<_Const_Link_type>(__it._M_node);
2353 _Const_Link_type __L = _S_left(__x);
2354 _Const_Link_type __R = _S_right(__x);
2356 if (__x->_M_color == _S_red)
2357 if ((__L && __L->_M_color == _S_red)
2358 || (__R && __R->_M_color == _S_red))
2361 if (__L && _M_impl._M_key_compare(_S_key(__x), _S_key(__L)))
2363 if (__R && _M_impl._M_key_compare(_S_key(__R), _S_key(__x)))
2366 if (!__L && !__R && _Rb_tree_black_count(__x, _M_root()) != __len)
2370 if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
2372 if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
2377 _GLIBCXX_END_NAMESPACE_VERSION