1 // RB tree implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
28 * Copyright (c) 1996,1997
29 * Silicon Graphics Computer Systems, Inc.
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation. Silicon Graphics makes no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
41 * Hewlett-Packard Company
43 * Permission to use, copy, modify, distribute and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appear in all copies and
46 * that both that copyright notice and this permission notice appear
47 * in supporting documentation. Hewlett-Packard Company makes no
48 * representations about the suitability of this software for any
49 * purpose. It is provided "as is" without express or implied warranty.
55 * This is an internal header file, included by other library headers.
56 * You should not attempt to use it directly.
62 #include <bits/stl_algobase.h>
63 #include <bits/allocator.h>
64 #include <bits/stl_function.h>
65 #include <bits/cpp_type_traits.h>
67 _GLIBCXX_BEGIN_NAMESPACE(std)
69 // Red-black tree class, designed for use in implementing STL
70 // associative containers (set, multiset, map, and multimap). The
71 // insertion and deletion algorithms are based on those in Cormen,
72 // Leiserson, and Rivest, Introduction to Algorithms (MIT Press,
75 // (1) the header cell is maintained with links not only to the root
76 // but also to the leftmost node of the tree, to enable constant
77 // time begin(), and to the rightmost node of the tree, to enable
78 // linear time performance when used with the generic set algorithms
81 // (2) when a node being deleted has two children its successor node
82 // is relinked into its place, rather than copied, so that the only
83 // iterators invalidated are those referring to the deleted node.
85 enum _Rb_tree_color { _S_red = false, _S_black = true };
87 struct _Rb_tree_node_base
89 typedef _Rb_tree_node_base* _Base_ptr;
90 typedef const _Rb_tree_node_base* _Const_Base_ptr;
92 _Rb_tree_color _M_color;
98 _S_minimum(_Base_ptr __x)
100 while (__x->_M_left != 0) __x = __x->_M_left;
104 static _Const_Base_ptr
105 _S_minimum(_Const_Base_ptr __x)
107 while (__x->_M_left != 0) __x = __x->_M_left;
112 _S_maximum(_Base_ptr __x)
114 while (__x->_M_right != 0) __x = __x->_M_right;
118 static _Const_Base_ptr
119 _S_maximum(_Const_Base_ptr __x)
121 while (__x->_M_right != 0) __x = __x->_M_right;
126 template<typename _Val>
127 struct _Rb_tree_node : public _Rb_tree_node_base
129 typedef _Rb_tree_node<_Val>* _Link_type;
132 #ifdef __GXX_EXPERIMENTAL_CXX0X__
133 template<typename... _Args>
134 _Rb_tree_node(_Args&&... __args)
135 : _Rb_tree_node_base(),
136 _M_value_field(std::forward<_Args>(__args)...) { }
140 _GLIBCXX_PURE _Rb_tree_node_base*
141 _Rb_tree_increment(_Rb_tree_node_base* __x) throw ();
143 _GLIBCXX_PURE const _Rb_tree_node_base*
144 _Rb_tree_increment(const _Rb_tree_node_base* __x) throw ();
146 _GLIBCXX_PURE _Rb_tree_node_base*
147 _Rb_tree_decrement(_Rb_tree_node_base* __x) throw ();
149 _GLIBCXX_PURE const _Rb_tree_node_base*
150 _Rb_tree_decrement(const _Rb_tree_node_base* __x) throw ();
152 template<typename _Tp>
153 struct _Rb_tree_iterator
155 typedef _Tp value_type;
156 typedef _Tp& reference;
157 typedef _Tp* pointer;
159 typedef bidirectional_iterator_tag iterator_category;
160 typedef ptrdiff_t difference_type;
162 typedef _Rb_tree_iterator<_Tp> _Self;
163 typedef _Rb_tree_node_base::_Base_ptr _Base_ptr;
164 typedef _Rb_tree_node<_Tp>* _Link_type;
170 _Rb_tree_iterator(_Link_type __x)
175 { return static_cast<_Link_type>(_M_node)->_M_value_field; }
179 { return &static_cast<_Link_type>(_M_node)->_M_value_field; }
184 _M_node = _Rb_tree_increment(_M_node);
192 _M_node = _Rb_tree_increment(_M_node);
199 _M_node = _Rb_tree_decrement(_M_node);
207 _M_node = _Rb_tree_decrement(_M_node);
212 operator==(const _Self& __x) const
213 { return _M_node == __x._M_node; }
216 operator!=(const _Self& __x) const
217 { return _M_node != __x._M_node; }
222 template<typename _Tp>
223 struct _Rb_tree_const_iterator
225 typedef _Tp value_type;
226 typedef const _Tp& reference;
227 typedef const _Tp* pointer;
229 typedef _Rb_tree_iterator<_Tp> iterator;
231 typedef bidirectional_iterator_tag iterator_category;
232 typedef ptrdiff_t difference_type;
234 typedef _Rb_tree_const_iterator<_Tp> _Self;
235 typedef _Rb_tree_node_base::_Const_Base_ptr _Base_ptr;
236 typedef const _Rb_tree_node<_Tp>* _Link_type;
238 _Rb_tree_const_iterator()
242 _Rb_tree_const_iterator(_Link_type __x)
245 _Rb_tree_const_iterator(const iterator& __it)
246 : _M_node(__it._M_node) { }
250 { return static_cast<_Link_type>(_M_node)->_M_value_field; }
254 { return &static_cast<_Link_type>(_M_node)->_M_value_field; }
259 _M_node = _Rb_tree_increment(_M_node);
267 _M_node = _Rb_tree_increment(_M_node);
274 _M_node = _Rb_tree_decrement(_M_node);
282 _M_node = _Rb_tree_decrement(_M_node);
287 operator==(const _Self& __x) const
288 { return _M_node == __x._M_node; }
291 operator!=(const _Self& __x) const
292 { return _M_node != __x._M_node; }
297 template<typename _Val>
299 operator==(const _Rb_tree_iterator<_Val>& __x,
300 const _Rb_tree_const_iterator<_Val>& __y)
301 { return __x._M_node == __y._M_node; }
303 template<typename _Val>
305 operator!=(const _Rb_tree_iterator<_Val>& __x,
306 const _Rb_tree_const_iterator<_Val>& __y)
307 { return __x._M_node != __y._M_node; }
310 _Rb_tree_insert_and_rebalance(const bool __insert_left,
311 _Rb_tree_node_base* __x,
312 _Rb_tree_node_base* __p,
313 _Rb_tree_node_base& __header) throw ();
316 _Rb_tree_rebalance_for_erase(_Rb_tree_node_base* const __z,
317 _Rb_tree_node_base& __header) throw ();
320 template<typename _Key, typename _Val, typename _KeyOfValue,
321 typename _Compare, typename _Alloc = allocator<_Val> >
324 typedef typename _Alloc::template rebind<_Rb_tree_node<_Val> >::other
328 typedef _Rb_tree_node_base* _Base_ptr;
329 typedef const _Rb_tree_node_base* _Const_Base_ptr;
332 typedef _Key key_type;
333 typedef _Val value_type;
334 typedef value_type* pointer;
335 typedef const value_type* const_pointer;
336 typedef value_type& reference;
337 typedef const value_type& const_reference;
338 typedef _Rb_tree_node<_Val>* _Link_type;
339 typedef const _Rb_tree_node<_Val>* _Const_Link_type;
340 typedef size_t size_type;
341 typedef ptrdiff_t difference_type;
342 typedef _Alloc allocator_type;
345 _M_get_Node_allocator()
346 { return *static_cast<_Node_allocator*>(&this->_M_impl); }
348 const _Node_allocator&
349 _M_get_Node_allocator() const
350 { return *static_cast<const _Node_allocator*>(&this->_M_impl); }
353 get_allocator() const
354 { return allocator_type(_M_get_Node_allocator()); }
359 { return _M_impl._Node_allocator::allocate(1); }
362 _M_put_node(_Link_type __p)
363 { _M_impl._Node_allocator::deallocate(__p, 1); }
365 #ifndef __GXX_EXPERIMENTAL_CXX0X__
367 _M_create_node(const value_type& __x)
369 _Link_type __tmp = _M_get_node();
371 { get_allocator().construct(&__tmp->_M_value_field, __x); }
375 __throw_exception_again;
381 _M_destroy_node(_Link_type __p)
383 get_allocator().destroy(&__p->_M_value_field);
387 template<typename... _Args>
389 _M_create_node(_Args&&... __args)
391 _Link_type __tmp = _M_get_node();
394 _M_get_Node_allocator().construct(__tmp,
395 std::forward<_Args>(__args)...);
400 __throw_exception_again;
406 _M_destroy_node(_Link_type __p)
408 _M_get_Node_allocator().destroy(__p);
414 _M_clone_node(_Const_Link_type __x)
416 _Link_type __tmp = _M_create_node(__x->_M_value_field);
417 __tmp->_M_color = __x->_M_color;
424 template<typename _Key_compare,
425 bool _Is_pod_comparator = __is_pod(_Key_compare)>
426 struct _Rb_tree_impl : public _Node_allocator
428 _Key_compare _M_key_compare;
429 _Rb_tree_node_base _M_header;
430 size_type _M_node_count; // Keeps track of size of tree.
433 : _Node_allocator(), _M_key_compare(), _M_header(),
437 _Rb_tree_impl(const _Key_compare& __comp, const _Node_allocator& __a)
438 : _Node_allocator(__a), _M_key_compare(__comp), _M_header(),
446 this->_M_header._M_color = _S_red;
447 this->_M_header._M_parent = 0;
448 this->_M_header._M_left = &this->_M_header;
449 this->_M_header._M_right = &this->_M_header;
453 _Rb_tree_impl<_Compare> _M_impl;
458 { return this->_M_impl._M_header._M_parent; }
462 { return this->_M_impl._M_header._M_parent; }
466 { return this->_M_impl._M_header._M_left; }
470 { return this->_M_impl._M_header._M_left; }
474 { return this->_M_impl._M_header._M_right; }
478 { return this->_M_impl._M_header._M_right; }
482 { return static_cast<_Link_type>(this->_M_impl._M_header._M_parent); }
487 return static_cast<_Const_Link_type>
488 (this->_M_impl._M_header._M_parent);
493 { return static_cast<_Link_type>(&this->_M_impl._M_header); }
497 { return static_cast<_Const_Link_type>(&this->_M_impl._M_header); }
499 static const_reference
500 _S_value(_Const_Link_type __x)
501 { return __x->_M_value_field; }
504 _S_key(_Const_Link_type __x)
505 { return _KeyOfValue()(_S_value(__x)); }
508 _S_left(_Base_ptr __x)
509 { return static_cast<_Link_type>(__x->_M_left); }
511 static _Const_Link_type
512 _S_left(_Const_Base_ptr __x)
513 { return static_cast<_Const_Link_type>(__x->_M_left); }
516 _S_right(_Base_ptr __x)
517 { return static_cast<_Link_type>(__x->_M_right); }
519 static _Const_Link_type
520 _S_right(_Const_Base_ptr __x)
521 { return static_cast<_Const_Link_type>(__x->_M_right); }
523 static const_reference
524 _S_value(_Const_Base_ptr __x)
525 { return static_cast<_Const_Link_type>(__x)->_M_value_field; }
528 _S_key(_Const_Base_ptr __x)
529 { return _KeyOfValue()(_S_value(__x)); }
532 _S_minimum(_Base_ptr __x)
533 { return _Rb_tree_node_base::_S_minimum(__x); }
535 static _Const_Base_ptr
536 _S_minimum(_Const_Base_ptr __x)
537 { return _Rb_tree_node_base::_S_minimum(__x); }
540 _S_maximum(_Base_ptr __x)
541 { return _Rb_tree_node_base::_S_maximum(__x); }
543 static _Const_Base_ptr
544 _S_maximum(_Const_Base_ptr __x)
545 { return _Rb_tree_node_base::_S_maximum(__x); }
548 typedef _Rb_tree_iterator<value_type> iterator;
549 typedef _Rb_tree_const_iterator<value_type> const_iterator;
551 typedef std::reverse_iterator<iterator> reverse_iterator;
552 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
556 _M_insert_(_Const_Base_ptr __x, _Const_Base_ptr __y,
557 const value_type& __v);
559 // _GLIBCXX_RESOLVE_LIB_DEFECTS
560 // 233. Insertion hints in associative containers.
562 _M_insert_lower(_Base_ptr __x, _Base_ptr __y, const value_type& __v);
565 _M_insert_equal_lower(const value_type& __x);
568 _M_copy(_Const_Link_type __x, _Link_type __p);
571 _M_erase(_Link_type __x);
574 _M_lower_bound(_Link_type __x, _Link_type __y,
578 _M_lower_bound(_Const_Link_type __x, _Const_Link_type __y,
579 const _Key& __k) const;
582 _M_upper_bound(_Link_type __x, _Link_type __y,
586 _M_upper_bound(_Const_Link_type __x, _Const_Link_type __y,
587 const _Key& __k) const;
590 // allocation/deallocation
593 _Rb_tree(const _Compare& __comp,
594 const allocator_type& __a = allocator_type())
595 : _M_impl(__comp, __a) { }
597 _Rb_tree(const _Rb_tree& __x)
598 : _M_impl(__x._M_impl._M_key_compare, __x._M_get_Node_allocator())
600 if (__x._M_root() != 0)
602 _M_root() = _M_copy(__x._M_begin(), _M_end());
603 _M_leftmost() = _S_minimum(_M_root());
604 _M_rightmost() = _S_maximum(_M_root());
605 _M_impl._M_node_count = __x._M_impl._M_node_count;
609 #ifdef __GXX_EXPERIMENTAL_CXX0X__
610 _Rb_tree(_Rb_tree&& __x);
614 { _M_erase(_M_begin()); }
617 operator=(const _Rb_tree& __x);
622 { return _M_impl._M_key_compare; }
627 return iterator(static_cast<_Link_type>
628 (this->_M_impl._M_header._M_left));
634 return const_iterator(static_cast<_Const_Link_type>
635 (this->_M_impl._M_header._M_left));
640 { return iterator(static_cast<_Link_type>(&this->_M_impl._M_header)); }
645 return const_iterator(static_cast<_Const_Link_type>
646 (&this->_M_impl._M_header));
651 { return reverse_iterator(end()); }
653 const_reverse_iterator
655 { return const_reverse_iterator(end()); }
659 { return reverse_iterator(begin()); }
661 const_reverse_iterator
663 { return const_reverse_iterator(begin()); }
667 { return _M_impl._M_node_count == 0; }
671 { return _M_impl._M_node_count; }
675 { return _M_get_Node_allocator().max_size(); }
682 _M_insert_unique(const value_type& __x);
685 _M_insert_equal(const value_type& __x);
688 _M_insert_unique_(const_iterator __position, const value_type& __x);
691 _M_insert_equal_(const_iterator __position, const value_type& __x);
693 template<typename _InputIterator>
695 _M_insert_unique(_InputIterator __first, _InputIterator __last);
697 template<typename _InputIterator>
699 _M_insert_equal(_InputIterator __first, _InputIterator __last);
701 #ifdef __GXX_EXPERIMENTAL_CXX0X__
702 // _GLIBCXX_RESOLVE_LIB_DEFECTS
703 // DR 130. Associative erase should return an iterator.
705 erase(iterator __position);
707 // _GLIBCXX_RESOLVE_LIB_DEFECTS
708 // DR 130. Associative erase should return an iterator.
710 erase(const_iterator __position);
713 erase(iterator __position);
716 erase(const_iterator __position);
719 erase(const key_type& __x);
721 #ifdef __GXX_EXPERIMENTAL_CXX0X__
722 // _GLIBCXX_RESOLVE_LIB_DEFECTS
723 // DR 130. Associative erase should return an iterator.
725 erase(iterator __first, iterator __last);
727 // _GLIBCXX_RESOLVE_LIB_DEFECTS
728 // DR 130. Associative erase should return an iterator.
730 erase(const_iterator __first, const_iterator __last);
733 erase(iterator __first, iterator __last);
736 erase(const_iterator __first, const_iterator __last);
739 erase(const key_type* __first, const key_type* __last);
744 _M_erase(_M_begin());
745 _M_leftmost() = _M_end();
747 _M_rightmost() = _M_end();
748 _M_impl._M_node_count = 0;
753 find(const key_type& __k);
756 find(const key_type& __k) const;
759 count(const key_type& __k) const;
762 lower_bound(const key_type& __k)
763 { return _M_lower_bound(_M_begin(), _M_end(), __k); }
766 lower_bound(const key_type& __k) const
767 { return _M_lower_bound(_M_begin(), _M_end(), __k); }
770 upper_bound(const key_type& __k)
771 { return _M_upper_bound(_M_begin(), _M_end(), __k); }
774 upper_bound(const key_type& __k) const
775 { return _M_upper_bound(_M_begin(), _M_end(), __k); }
777 pair<iterator, iterator>
778 equal_range(const key_type& __k);
780 pair<const_iterator, const_iterator>
781 equal_range(const key_type& __k) const;
788 template<typename _Key, typename _Val, typename _KeyOfValue,
789 typename _Compare, typename _Alloc>
791 operator==(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
792 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
794 return __x.size() == __y.size()
795 && std::equal(__x.begin(), __x.end(), __y.begin());
798 template<typename _Key, typename _Val, typename _KeyOfValue,
799 typename _Compare, typename _Alloc>
801 operator<(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
802 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
804 return std::lexicographical_compare(__x.begin(), __x.end(),
805 __y.begin(), __y.end());
808 template<typename _Key, typename _Val, typename _KeyOfValue,
809 typename _Compare, typename _Alloc>
811 operator!=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
812 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
813 { return !(__x == __y); }
815 template<typename _Key, typename _Val, typename _KeyOfValue,
816 typename _Compare, typename _Alloc>
818 operator>(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
819 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
820 { return __y < __x; }
822 template<typename _Key, typename _Val, typename _KeyOfValue,
823 typename _Compare, typename _Alloc>
825 operator<=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
826 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
827 { return !(__y < __x); }
829 template<typename _Key, typename _Val, typename _KeyOfValue,
830 typename _Compare, typename _Alloc>
832 operator>=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
833 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
834 { return !(__x < __y); }
836 template<typename _Key, typename _Val, typename _KeyOfValue,
837 typename _Compare, typename _Alloc>
839 swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
840 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
843 #ifdef __GXX_EXPERIMENTAL_CXX0X__
844 template<typename _Key, typename _Val, typename _KeyOfValue,
845 typename _Compare, typename _Alloc>
846 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
847 _Rb_tree(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&& __x)
848 : _M_impl(__x._M_impl._M_key_compare, __x._M_get_Node_allocator())
850 if (__x._M_root() != 0)
852 _M_root() = __x._M_root();
853 _M_leftmost() = __x._M_leftmost();
854 _M_rightmost() = __x._M_rightmost();
855 _M_root()->_M_parent = _M_end();
858 __x._M_leftmost() = __x._M_end();
859 __x._M_rightmost() = __x._M_end();
861 this->_M_impl._M_node_count = __x._M_impl._M_node_count;
862 __x._M_impl._M_node_count = 0;
867 template<typename _Key, typename _Val, typename _KeyOfValue,
868 typename _Compare, typename _Alloc>
869 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
870 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
871 operator=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x)
875 // Note that _Key may be a constant type.
877 _M_impl._M_key_compare = __x._M_impl._M_key_compare;
878 if (__x._M_root() != 0)
880 _M_root() = _M_copy(__x._M_begin(), _M_end());
881 _M_leftmost() = _S_minimum(_M_root());
882 _M_rightmost() = _S_maximum(_M_root());
883 _M_impl._M_node_count = __x._M_impl._M_node_count;
889 template<typename _Key, typename _Val, typename _KeyOfValue,
890 typename _Compare, typename _Alloc>
891 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
892 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
893 _M_insert_(_Const_Base_ptr __x, _Const_Base_ptr __p, const _Val& __v)
895 bool __insert_left = (__x != 0 || __p == _M_end()
896 || _M_impl._M_key_compare(_KeyOfValue()(__v),
899 _Link_type __z = _M_create_node(__v);
901 _Rb_tree_insert_and_rebalance(__insert_left, __z,
902 const_cast<_Base_ptr>(__p),
903 this->_M_impl._M_header);
904 ++_M_impl._M_node_count;
905 return iterator(__z);
908 template<typename _Key, typename _Val, typename _KeyOfValue,
909 typename _Compare, typename _Alloc>
910 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
911 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
912 _M_insert_lower(_Base_ptr __x, _Base_ptr __p, const _Val& __v)
914 bool __insert_left = (__x != 0 || __p == _M_end()
915 || !_M_impl._M_key_compare(_S_key(__p),
916 _KeyOfValue()(__v)));
918 _Link_type __z = _M_create_node(__v);
920 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
921 this->_M_impl._M_header);
922 ++_M_impl._M_node_count;
923 return iterator(__z);
926 template<typename _Key, typename _Val, typename _KeyOfValue,
927 typename _Compare, typename _Alloc>
928 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
929 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
930 _M_insert_equal_lower(const _Val& __v)
932 _Link_type __x = _M_begin();
933 _Link_type __y = _M_end();
937 __x = !_M_impl._M_key_compare(_S_key(__x), _KeyOfValue()(__v)) ?
938 _S_left(__x) : _S_right(__x);
940 return _M_insert_lower(__x, __y, __v);
943 template<typename _Key, typename _Val, typename _KoV,
944 typename _Compare, typename _Alloc>
945 typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Link_type
946 _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
947 _M_copy(_Const_Link_type __x, _Link_type __p)
949 // Structural copy. __x and __p must be non-null.
950 _Link_type __top = _M_clone_node(__x);
951 __top->_M_parent = __p;
956 __top->_M_right = _M_copy(_S_right(__x), __top);
962 _Link_type __y = _M_clone_node(__x);
964 __y->_M_parent = __p;
966 __y->_M_right = _M_copy(_S_right(__x), __y);
974 __throw_exception_again;
979 template<typename _Key, typename _Val, typename _KeyOfValue,
980 typename _Compare, typename _Alloc>
982 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
983 _M_erase(_Link_type __x)
985 // Erase without rebalancing.
988 _M_erase(_S_right(__x));
989 _Link_type __y = _S_left(__x);
990 _M_destroy_node(__x);
995 template<typename _Key, typename _Val, typename _KeyOfValue,
996 typename _Compare, typename _Alloc>
997 typename _Rb_tree<_Key, _Val, _KeyOfValue,
998 _Compare, _Alloc>::iterator
999 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1000 _M_lower_bound(_Link_type __x, _Link_type __y,
1004 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1005 __y = __x, __x = _S_left(__x);
1007 __x = _S_right(__x);
1008 return iterator(__y);
1011 template<typename _Key, typename _Val, typename _KeyOfValue,
1012 typename _Compare, typename _Alloc>
1013 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1014 _Compare, _Alloc>::const_iterator
1015 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1016 _M_lower_bound(_Const_Link_type __x, _Const_Link_type __y,
1017 const _Key& __k) const
1020 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1021 __y = __x, __x = _S_left(__x);
1023 __x = _S_right(__x);
1024 return const_iterator(__y);
1027 template<typename _Key, typename _Val, typename _KeyOfValue,
1028 typename _Compare, typename _Alloc>
1029 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1030 _Compare, _Alloc>::iterator
1031 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1032 _M_upper_bound(_Link_type __x, _Link_type __y,
1036 if (_M_impl._M_key_compare(__k, _S_key(__x)))
1037 __y = __x, __x = _S_left(__x);
1039 __x = _S_right(__x);
1040 return iterator(__y);
1043 template<typename _Key, typename _Val, typename _KeyOfValue,
1044 typename _Compare, typename _Alloc>
1045 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1046 _Compare, _Alloc>::const_iterator
1047 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1048 _M_upper_bound(_Const_Link_type __x, _Const_Link_type __y,
1049 const _Key& __k) const
1052 if (_M_impl._M_key_compare(__k, _S_key(__x)))
1053 __y = __x, __x = _S_left(__x);
1055 __x = _S_right(__x);
1056 return const_iterator(__y);
1059 template<typename _Key, typename _Val, typename _KeyOfValue,
1060 typename _Compare, typename _Alloc>
1061 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1062 _Compare, _Alloc>::iterator,
1063 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1064 _Compare, _Alloc>::iterator>
1065 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1066 equal_range(const _Key& __k)
1068 _Link_type __x = _M_begin();
1069 _Link_type __y = _M_end();
1072 if (_M_impl._M_key_compare(_S_key(__x), __k))
1073 __x = _S_right(__x);
1074 else if (_M_impl._M_key_compare(__k, _S_key(__x)))
1075 __y = __x, __x = _S_left(__x);
1078 _Link_type __xu(__x), __yu(__y);
1079 __y = __x, __x = _S_left(__x);
1080 __xu = _S_right(__xu);
1081 return pair<iterator,
1082 iterator>(_M_lower_bound(__x, __y, __k),
1083 _M_upper_bound(__xu, __yu, __k));
1086 return pair<iterator, iterator>(iterator(__y),
1090 template<typename _Key, typename _Val, typename _KeyOfValue,
1091 typename _Compare, typename _Alloc>
1092 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1093 _Compare, _Alloc>::const_iterator,
1094 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1095 _Compare, _Alloc>::const_iterator>
1096 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1097 equal_range(const _Key& __k) const
1099 _Const_Link_type __x = _M_begin();
1100 _Const_Link_type __y = _M_end();
1103 if (_M_impl._M_key_compare(_S_key(__x), __k))
1104 __x = _S_right(__x);
1105 else if (_M_impl._M_key_compare(__k, _S_key(__x)))
1106 __y = __x, __x = _S_left(__x);
1109 _Const_Link_type __xu(__x), __yu(__y);
1110 __y = __x, __x = _S_left(__x);
1111 __xu = _S_right(__xu);
1112 return pair<const_iterator,
1113 const_iterator>(_M_lower_bound(__x, __y, __k),
1114 _M_upper_bound(__xu, __yu, __k));
1117 return pair<const_iterator, const_iterator>(const_iterator(__y),
1118 const_iterator(__y));
1121 template<typename _Key, typename _Val, typename _KeyOfValue,
1122 typename _Compare, typename _Alloc>
1124 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1125 swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __t)
1129 if (__t._M_root() != 0)
1131 _M_root() = __t._M_root();
1132 _M_leftmost() = __t._M_leftmost();
1133 _M_rightmost() = __t._M_rightmost();
1134 _M_root()->_M_parent = _M_end();
1137 __t._M_leftmost() = __t._M_end();
1138 __t._M_rightmost() = __t._M_end();
1141 else if (__t._M_root() == 0)
1143 __t._M_root() = _M_root();
1144 __t._M_leftmost() = _M_leftmost();
1145 __t._M_rightmost() = _M_rightmost();
1146 __t._M_root()->_M_parent = __t._M_end();
1149 _M_leftmost() = _M_end();
1150 _M_rightmost() = _M_end();
1154 std::swap(_M_root(),__t._M_root());
1155 std::swap(_M_leftmost(),__t._M_leftmost());
1156 std::swap(_M_rightmost(),__t._M_rightmost());
1158 _M_root()->_M_parent = _M_end();
1159 __t._M_root()->_M_parent = __t._M_end();
1161 // No need to swap header's color as it does not change.
1162 std::swap(this->_M_impl._M_node_count, __t._M_impl._M_node_count);
1163 std::swap(this->_M_impl._M_key_compare, __t._M_impl._M_key_compare);
1165 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1166 // 431. Swapping containers with unequal allocators.
1167 std::__alloc_swap<_Node_allocator>::
1168 _S_do_it(_M_get_Node_allocator(), __t._M_get_Node_allocator());
1171 template<typename _Key, typename _Val, typename _KeyOfValue,
1172 typename _Compare, typename _Alloc>
1173 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1174 _Compare, _Alloc>::iterator, bool>
1175 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1176 _M_insert_unique(const _Val& __v)
1178 _Link_type __x = _M_begin();
1179 _Link_type __y = _M_end();
1184 __comp = _M_impl._M_key_compare(_KeyOfValue()(__v), _S_key(__x));
1185 __x = __comp ? _S_left(__x) : _S_right(__x);
1187 iterator __j = iterator(__y);
1191 return pair<iterator, bool>(_M_insert_(__x, __y, __v), true);
1195 if (_M_impl._M_key_compare(_S_key(__j._M_node), _KeyOfValue()(__v)))
1196 return pair<iterator, bool>(_M_insert_(__x, __y, __v), true);
1197 return pair<iterator, bool>(__j, false);
1200 template<typename _Key, typename _Val, typename _KeyOfValue,
1201 typename _Compare, typename _Alloc>
1202 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1203 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1204 _M_insert_equal(const _Val& __v)
1206 _Link_type __x = _M_begin();
1207 _Link_type __y = _M_end();
1211 __x = _M_impl._M_key_compare(_KeyOfValue()(__v), _S_key(__x)) ?
1212 _S_left(__x) : _S_right(__x);
1214 return _M_insert_(__x, __y, __v);
1217 template<typename _Key, typename _Val, typename _KeyOfValue,
1218 typename _Compare, typename _Alloc>
1219 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1220 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1221 _M_insert_unique_(const_iterator __position, const _Val& __v)
1224 if (__position._M_node == _M_end())
1227 && _M_impl._M_key_compare(_S_key(_M_rightmost()),
1228 _KeyOfValue()(__v)))
1229 return _M_insert_(0, _M_rightmost(), __v);
1231 return _M_insert_unique(__v).first;
1233 else if (_M_impl._M_key_compare(_KeyOfValue()(__v),
1234 _S_key(__position._M_node)))
1236 // First, try before...
1237 const_iterator __before = __position;
1238 if (__position._M_node == _M_leftmost()) // begin()
1239 return _M_insert_(_M_leftmost(), _M_leftmost(), __v);
1240 else if (_M_impl._M_key_compare(_S_key((--__before)._M_node),
1241 _KeyOfValue()(__v)))
1243 if (_S_right(__before._M_node) == 0)
1244 return _M_insert_(0, __before._M_node, __v);
1246 return _M_insert_(__position._M_node,
1247 __position._M_node, __v);
1250 return _M_insert_unique(__v).first;
1252 else if (_M_impl._M_key_compare(_S_key(__position._M_node),
1253 _KeyOfValue()(__v)))
1255 // ... then try after.
1256 const_iterator __after = __position;
1257 if (__position._M_node == _M_rightmost())
1258 return _M_insert_(0, _M_rightmost(), __v);
1259 else if (_M_impl._M_key_compare(_KeyOfValue()(__v),
1260 _S_key((++__after)._M_node)))
1262 if (_S_right(__position._M_node) == 0)
1263 return _M_insert_(0, __position._M_node, __v);
1265 return _M_insert_(__after._M_node, __after._M_node, __v);
1268 return _M_insert_unique(__v).first;
1272 return iterator(static_cast<_Link_type>
1273 (const_cast<_Base_ptr>(__position._M_node)));
1276 template<typename _Key, typename _Val, typename _KeyOfValue,
1277 typename _Compare, typename _Alloc>
1278 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1279 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1280 _M_insert_equal_(const_iterator __position, const _Val& __v)
1283 if (__position._M_node == _M_end())
1286 && !_M_impl._M_key_compare(_KeyOfValue()(__v),
1287 _S_key(_M_rightmost())))
1288 return _M_insert_(0, _M_rightmost(), __v);
1290 return _M_insert_equal(__v);
1292 else if (!_M_impl._M_key_compare(_S_key(__position._M_node),
1293 _KeyOfValue()(__v)))
1295 // First, try before...
1296 const_iterator __before = __position;
1297 if (__position._M_node == _M_leftmost()) // begin()
1298 return _M_insert_(_M_leftmost(), _M_leftmost(), __v);
1299 else if (!_M_impl._M_key_compare(_KeyOfValue()(__v),
1300 _S_key((--__before)._M_node)))
1302 if (_S_right(__before._M_node) == 0)
1303 return _M_insert_(0, __before._M_node, __v);
1305 return _M_insert_(__position._M_node,
1306 __position._M_node, __v);
1309 return _M_insert_equal(__v);
1313 // ... then try after.
1314 const_iterator __after = __position;
1315 if (__position._M_node == _M_rightmost())
1316 return _M_insert_(0, _M_rightmost(), __v);
1317 else if (!_M_impl._M_key_compare(_S_key((++__after)._M_node),
1318 _KeyOfValue()(__v)))
1320 if (_S_right(__position._M_node) == 0)
1321 return _M_insert_(0, __position._M_node, __v);
1323 return _M_insert_(__after._M_node, __after._M_node, __v);
1326 return _M_insert_equal_lower(__v);
1330 template<typename _Key, typename _Val, typename _KoV,
1331 typename _Cmp, typename _Alloc>
1334 _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::
1335 _M_insert_unique(_II __first, _II __last)
1337 for (; __first != __last; ++__first)
1338 _M_insert_unique_(end(), *__first);
1341 template<typename _Key, typename _Val, typename _KoV,
1342 typename _Cmp, typename _Alloc>
1345 _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::
1346 _M_insert_equal(_II __first, _II __last)
1348 for (; __first != __last; ++__first)
1349 _M_insert_equal_(end(), *__first);
1352 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1353 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1354 // DR 130. Associative erase should return an iterator.
1355 template<typename _Key, typename _Val, typename _KeyOfValue,
1356 typename _Compare, typename _Alloc>
1357 inline typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1358 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1359 erase(iterator __position)
1361 iterator __result = __position;
1364 static_cast<_Link_type>(_Rb_tree_rebalance_for_erase
1365 (__position._M_node,
1366 this->_M_impl._M_header));
1367 _M_destroy_node(__y);
1368 --_M_impl._M_node_count;
1372 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1373 // DR 130. Associative erase should return an iterator.
1374 template<typename _Key, typename _Val, typename _KeyOfValue,
1375 typename _Compare, typename _Alloc>
1376 inline typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator
1377 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1378 erase(const_iterator __position)
1380 const_iterator __result = __position;
1383 static_cast<_Link_type>(_Rb_tree_rebalance_for_erase
1384 (const_cast<_Base_ptr>(__position._M_node),
1385 this->_M_impl._M_header));
1386 _M_destroy_node(__y);
1387 --_M_impl._M_node_count;
1391 template<typename _Key, typename _Val, typename _KeyOfValue,
1392 typename _Compare, typename _Alloc>
1394 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1395 erase(iterator __position)
1398 static_cast<_Link_type>(_Rb_tree_rebalance_for_erase
1399 (__position._M_node,
1400 this->_M_impl._M_header));
1401 _M_destroy_node(__y);
1402 --_M_impl._M_node_count;
1405 template<typename _Key, typename _Val, typename _KeyOfValue,
1406 typename _Compare, typename _Alloc>
1408 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1409 erase(const_iterator __position)
1412 static_cast<_Link_type>(_Rb_tree_rebalance_for_erase
1413 (const_cast<_Base_ptr>(__position._M_node),
1414 this->_M_impl._M_header));
1415 _M_destroy_node(__y);
1416 --_M_impl._M_node_count;
1420 template<typename _Key, typename _Val, typename _KeyOfValue,
1421 typename _Compare, typename _Alloc>
1422 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
1423 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1424 erase(const _Key& __x)
1426 pair<iterator, iterator> __p = equal_range(__x);
1427 const size_type __old_size = size();
1428 erase(__p.first, __p.second);
1429 return __old_size - size();
1432 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1433 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1434 // DR 130. Associative erase should return an iterator.
1435 template<typename _Key, typename _Val, typename _KeyOfValue,
1436 typename _Compare, typename _Alloc>
1437 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1438 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1439 erase(iterator __first, iterator __last)
1441 if (__first == begin() && __last == end())
1448 while (__first != __last)
1454 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1455 // DR 130. Associative erase should return an iterator.
1456 template<typename _Key, typename _Val, typename _KeyOfValue,
1457 typename _Compare, typename _Alloc>
1458 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator
1459 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1460 erase(const_iterator __first, const_iterator __last)
1462 if (__first == begin() && __last == end())
1469 while (__first != __last)
1475 template<typename _Key, typename _Val, typename _KeyOfValue,
1476 typename _Compare, typename _Alloc>
1478 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1479 erase(iterator __first, iterator __last)
1481 if (__first == begin() && __last == end())
1484 while (__first != __last)
1488 template<typename _Key, typename _Val, typename _KeyOfValue,
1489 typename _Compare, typename _Alloc>
1491 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1492 erase(const_iterator __first, const_iterator __last)
1494 if (__first == begin() && __last == end())
1497 while (__first != __last)
1502 template<typename _Key, typename _Val, typename _KeyOfValue,
1503 typename _Compare, typename _Alloc>
1505 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1506 erase(const _Key* __first, const _Key* __last)
1508 while (__first != __last)
1512 template<typename _Key, typename _Val, typename _KeyOfValue,
1513 typename _Compare, typename _Alloc>
1514 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1515 _Compare, _Alloc>::iterator
1516 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1517 find(const _Key& __k)
1519 iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
1520 return (__j == end()
1521 || _M_impl._M_key_compare(__k,
1522 _S_key(__j._M_node))) ? end() : __j;
1525 template<typename _Key, typename _Val, typename _KeyOfValue,
1526 typename _Compare, typename _Alloc>
1527 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1528 _Compare, _Alloc>::const_iterator
1529 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1530 find(const _Key& __k) const
1532 const_iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
1533 return (__j == end()
1534 || _M_impl._M_key_compare(__k,
1535 _S_key(__j._M_node))) ? end() : __j;
1538 template<typename _Key, typename _Val, typename _KeyOfValue,
1539 typename _Compare, typename _Alloc>
1540 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
1541 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1542 count(const _Key& __k) const
1544 pair<const_iterator, const_iterator> __p = equal_range(__k);
1545 const size_type __n = std::distance(__p.first, __p.second);
1549 _GLIBCXX_PURE unsigned int
1550 _Rb_tree_black_count(const _Rb_tree_node_base* __node,
1551 const _Rb_tree_node_base* __root) throw ();
1553 template<typename _Key, typename _Val, typename _KeyOfValue,
1554 typename _Compare, typename _Alloc>
1556 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const
1558 if (_M_impl._M_node_count == 0 || begin() == end())
1559 return _M_impl._M_node_count == 0 && begin() == end()
1560 && this->_M_impl._M_header._M_left == _M_end()
1561 && this->_M_impl._M_header._M_right == _M_end();
1563 unsigned int __len = _Rb_tree_black_count(_M_leftmost(), _M_root());
1564 for (const_iterator __it = begin(); __it != end(); ++__it)
1566 _Const_Link_type __x = static_cast<_Const_Link_type>(__it._M_node);
1567 _Const_Link_type __L = _S_left(__x);
1568 _Const_Link_type __R = _S_right(__x);
1570 if (__x->_M_color == _S_red)
1571 if ((__L && __L->_M_color == _S_red)
1572 || (__R && __R->_M_color == _S_red))
1575 if (__L && _M_impl._M_key_compare(_S_key(__x), _S_key(__L)))
1577 if (__R && _M_impl._M_key_compare(_S_key(__R), _S_key(__x)))
1580 if (!__L && !__R && _Rb_tree_black_count(__x, _M_root()) != __len)
1584 if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
1586 if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
1591 _GLIBCXX_END_NAMESPACE