46template <
class _Key,
class _Val,
class _KeyOf,
class _Comp,
class _Alloc>
47__rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::
48__rb_tree (
const key_compare &__cmp ,
49 const allocator_type &__alloc )
50 : allocator_type (__alloc),
60template <
class _Key,
class _Val,
class _KeyOf,
class _Comp,
class _Alloc>
61__rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>&
62__rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::
63operator= (
const __rb_tree &__x)
66 erase (begin (),
end ());
69 _C_end->_C_parent = _C_copy (__x._C_end->_C_parent, _C_end);
71 if (_C_link_t () == _C_end->_C_parent) {
73 _C_end->_C_child [0] =
74 _C_end->_C_child [1] = _C_end;
78 _C_end->_C_child [0] = _C_node_t::_C_min (_C_end->_C_parent);
79 _C_end->_C_child [1] = _C_node_t::_C_max (_C_end->_C_parent);
81 _C_size = __x._C_size;
88template <
class _Key,
class _Val,
class _KeyOf,
class _Comp,
class _Alloc>
89__rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::
90__rb_tree (
const __rb_tree &__lnk)
91 : allocator_type (__lnk.get_allocator ()), _C_buf_list (0), _C_end (0),
92 _C_size (__lnk._C_size),
95 _C_free_list = _C_next_avail = _C_last = 0;
96 _C_end = _C_get_node ();
97 _C_end->_C_color = _C_node_t::_C_red;
99 _C_end->_C_parent = _C_copy (__lnk._C_end->_C_parent, _C_end);
102 _C_deallocate_buffers ();
105 if (_C_link_t () == _C_end->_C_parent) {
106 _C_end->_C_child [0] =
107 _C_end->_C_child [1] = _C_end;
110 _C_end->_C_child [0] = _C_node_t::_C_min (_C_end->_C_parent);
111 _C_end->_C_child [1] = _C_node_t::_C_max (_C_end->_C_parent);
116template <
class _Key,
class _Val,
class _KeyOf,
class _Comp,
class _Alloc>
117__rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::
120 if (_C_link_t () != _C_end) {
121 erase (begin (),
end ());
122 _C_put_node (_C_end,
false);
123 _C_deallocate_buffers ();
128template <
class _Key,
class _Val,
class _KeyOf,
class _Comp,
class _Alloc>
129void __rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::
132 const size_type __bufsize =
133 _C_buf_ptr_t () == _C_buf_list ? 0 : _C_buf_list->size;
136 if (__newsize <= __bufsize)
137 __newsize = __bufsize + 1;
140 _C_buf_alloc_t (*this).allocate (size_type (1), (
void*)_C_buf_list);
144 _C_node_alloc_t (*this).allocate (__newsize, (
void*)_C_last);
147 _C_buf_alloc_t (*this).deallocate (__buf, 1);
150 __buf->_C_next_buffer = _C_buf_list;
151 __buf->size = __newsize;
153 _C_next_avail = _C_buf_list->_C_buffer;
154 _C_last = _C_next_avail + __newsize;
158template <
class _Key,
class _Val,
class _KeyOf,
class _Comp,
class _Alloc>
159void __rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::
160_C_deallocate_buffers ()
162 while ((
void*)_C_buf_list) {
163 _C_buf_ptr_t __tmp = _C_buf_list;
164 _C_buf_list = (_C_buf_ptr_t)(_C_buf_list->_C_next_buffer);
165 _C_node_alloc_t(*this).deallocate(__tmp->_C_buffer,__tmp->size);
166 _C_buf_alloc_t(*this).deallocate(__tmp,1);
171template <
class _Key,
class _Val,
class _KeyOf,
class _Comp,
class _Alloc>
172typename __rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::iterator
173__rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::
174_C_insert (_C_link_t __x, _C_link_t __y,
const value_type &__v)
176 _C_link_t __z = _C_get_node (__v);
180 const typename _C_node_t::_C_color_t _Red = _C_node_t::_C_red;
181 const typename _C_node_t::_C_color_t _Black = _C_node_t::_C_black;
182 const _C_link_t _Null = _C_link_t ();
184 if ( __y == _C_end || _Null != __x
185 || _C_cmp (_KeyOf()(__v), __y->_C_key ())) {
187 __y->_C_child [0] = __z;
191 _C_end->_C_parent = __z;
192 _C_end->_C_child [1] = __z;
194 else if (__y == _C_end->_C_child [0]) {
196 _C_end->_C_child [0] = __z;
200 __y->_C_child [1] = __z;
201 if (__y == _C_end->_C_child [1]) {
203 _C_end->_C_child [1] = __z;
207 __z->_C_parent = __y;
210 while ( __x != _C_end->_C_parent
211 && __x->_C_parent->_C_color == _Red) {
213#ifndef _RWSTD_NO_OPTIMIZE_SPEED
215 if (__x->_C_parent == __x->_C_parent->_C_parent->_C_child [0]) {
217 __y = __x->_C_parent->_C_parent->_C_child [1];
219 if (_Null != __y && __y->_C_color == _Red) {
220 __x->_C_parent->_C_color = _Black;
221 __y->_C_color = _Black;
222 __x->_C_parent->_C_parent->_C_color = _Red;
223 __x = __x->_C_parent->_C_parent;
226 if (__x == __x->_C_parent->_C_child [1]) {
227 __x = __x->_C_parent;
230 __x->_C_parent->_C_color = _Black;
231 __x->_C_parent->_C_parent->_C_color = _Red;
232 _C_ror (__x->_C_parent->_C_parent);
236 __y = __x->_C_parent->_C_parent->_C_child [0];
238 if (_Null != __y && __y->_C_color == _Red) {
239 __x->_C_parent->_C_color = _Black;
240 __y->_C_color = _Black;
241 __x->_C_parent->_C_parent->_C_color = _Red;
242 __x = __x->_C_parent->_C_parent;
245 if (__x == __x->_C_parent->_C_child [0]) {
246 __x = __x->_C_parent;
249 __x->_C_parent->_C_color = _Black;
250 __x->_C_parent->_C_parent->_C_color = _Red;
251 _C_rol (__x->_C_parent->_C_parent);
258 __x->_C_parent == __x->_C_parent->_C_parent->_C_child [0];
259 const bool __right = !__left;
261 __y = __x->_C_parent->_C_parent->_C_child [__left];
263 if (_Null != __y && _Red == __y->_C_color) {
265 __x->_C_parent->_C_color = _Black;
266 __x->_C_parent->_C_parent->_C_color = _Red;
267 __x = __x->_C_parent->_C_parent;
270 if (__x == __x->_C_parent->_C_child [__left]) {
271 __x = __x->_C_parent;
272 _C_rotate (__x, __right);
274 __x->_C_parent->_C_color = _Black;
275 __x->_C_parent->_C_parent->_C_color = _Red;
276 _C_rotate (__x->_C_parent->_C_parent, __left);
284 _C_end->_C_parent->_C_color = _Black;
286 return _C_make_iter (__z);
290template <
class _Key,
class _Val,
class _KeyOf,
class _Comp,
class _Alloc>
291void __rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::
292_C_insert (
const value_type &__v, _STD::pair<iterator, bool> &__ret,
295 _C_link_t __y = _C_end;
296 _C_link_t __x = _C_end->_C_parent;
300 while (_C_link_t () != __x) {
302 __right = _C_cmp (_KeyOf ()(__v), __x->_C_key ());
303 __x = __x->_C_child [!__right];
306 typedef _STD::pair<iterator, bool> _IterPair;
310 __ret = _IterPair (_C_insert (__x, __y, __v),
true);
314 iterator __j = _C_make_iter (__y);
316 if (__j == begin ()) {
317 __ret = _IterPair (_C_insert (__x, __y, __v),
true);
323 if (_C_cmp (
_ITER_NODE (__j)->_C_key (), _KeyOf ()(__v)))
324 __ret = _IterPair (_C_insert (__x, __y, __v),
true);
326 __ret = _IterPair (__j,
false);
330template <
class _Key,
class _Val,
class _KeyOf,
class _Comp,
class _Alloc>
331typename __rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::iterator
332__rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::
333insert (iterator __it,
const value_type &__v,
bool __dup)
340 size_type __two_logN = 0;
341 for (size_type __i =
size () + 1; __i >>= 1; ++__two_logN);
345 const size_type __depth = _C_depth ();
354 if (__hint == _C_end->_C_child [1]) {
358 if (_C_size && _C_cmp (__hint->_C_key (), _KeyOf ()(__v)))
359 return _C_insert (0, __hint, __v);
362 return insert (__v, __dup).first;
367 if (__hint == _C_end) {
368 if (_C_cmp (__hint->_C_child [1]->_C_key(), _KeyOf ()(__v)))
369 return _C_insert (0, __hint->_C_child [1], __v);
370 return insert (__v, __dup).first;
375 if (__hint == _C_end->_C_child [0]) {
376 if (_C_cmp (_KeyOf ()(__v), __hint->_C_key ()))
377 return _C_insert (__hint, __hint, __v);
378 return insert (__v, __dup).first;
381 const iterator __prev = __it++;
384 if ( _C_cmp (
_ITER_NODE (__prev)->_C_key (), _KeyOf ()(__v))
385 && _C_cmp (_KeyOf ()(__v),
_ITER_NODE (__it)->_C_key ())) {
389 if (_C_link_t () ==
_ITER_NODE (__prev)->_C_child [1])
390 return _C_insert (0,
_ITER_NODE (__prev), __v);
397 return insert (__v, __dup).first;
401template <
class _Key,
class _Val,
class _KeyOf,
class _Comp,
class _Alloc>
402typename __rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::iterator
403__rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::erase (iterator __it)
410 size_type __two_logN = 0;
411 for (size_type __i =
size () + 1; __i >>= 1; ++__two_logN);
415 const size_type __depth = _C_depth ();
425 const typename _C_node_t::_C_color_t _Red = _C_node_t::_C_red;
426 const typename _C_node_t::_C_color_t _Black = _C_node_t::_C_black;
427 const _C_link_t _Null = _C_link_t ();
430 const iterator __next = ++iterator (__it);
436 _C_link_t __x_parent = 0;
441 if (_Null == __y->_C_child [0]) {
444 __x = __y->_C_child [1];
446 else if (_Null == __y->_C_child [1]) {
448 __x = __y->_C_child [0];
454 __y = _C_node_t::_C_min (__y->_C_child [1]);
455 __x = __y->_C_child [1];
466 __z->_C_child [0]->_C_parent = __y;
467 __y->_C_child [0] = __z->_C_child [0];
469 if (__y != __z->_C_child [1]) {
473 __x_parent = __y->_C_parent;
477 __x->_C_parent = __y->_C_parent;
480 __y->_C_parent->_C_child [0] = __x;
483 __y->_C_child [1] = __z->_C_child [1];
486 __z->_C_child [1]->_C_parent = __y;
496 if (_C_end->_C_parent == __z) {
498 _C_end->_C_parent = __y;
504 __z->_C_parent->_C_child [__z->_C_parent->_C_child [0] != __z]
510 __y->_C_parent = __z->_C_parent;
513 _STD::swap (__y->_C_color, __z->_C_color);
524 __x_parent = __y->_C_parent;
528 __x->_C_parent = __y->_C_parent;
531 if (_C_end->_C_parent == __z) {
533 _C_end->_C_parent = __x;
538 __z->_C_parent->_C_child [__z->_C_parent->_C_child [0] != __z]
542 if (_C_end->_C_child [0] == __z) {
545 if (_Null == __z->_C_child [1])
548 _C_end->_C_child [0] = __z->_C_parent;
552 _C_end->_C_child [0] = _C_node_t::_C_min (__x);
556 if (_C_end->_C_child [1] == __z) {
559 if (_Null == __z->_C_child [0])
562 _C_end->_C_child [1] = __z->_C_parent;
566 _C_end->_C_child [1] = _C_node_t::_C_max (__x);
575 if (__y->_C_color != _Red) {
589 while ( __x != _C_end->_C_parent
590 && (_Null == __x || __x->_C_color == _Black)) {
592#ifndef _RWSTD_NO_OPTIMIZE_SPEED
594 if (__x == __x_parent->_C_child [0]) {
599 _C_link_t __w = __x_parent->_C_child [1];
601 if (_Red == __w->_C_color) {
602 __w->_C_color = _Black;
603 __x_parent->_C_color = _Red;
607 __w = __x_parent->_C_child [1];
610 if ((_Null == __w->_C_child [0] ||
611 _Black == __w->_C_child [0]->_C_color) &&
612 (_Null == __w->_C_child [1] ||
613 _Black == __w->_C_child [1]->_C_color)) {
616 __w->_C_color = _Red;
618 __x_parent = __x_parent->_C_parent;
623 if (_Null == __w->_C_child [1] ||
624 _Black == __w->_C_child [1]->_C_color) {
628 if (_Null != __w->_C_child [0])
630 __w->_C_child [0]->_C_color = _Black;
633 __w->_C_color = _Red;
637 __w = __x_parent->_C_child [1];
640 __w->_C_color = __x_parent->_C_color;
641 __x_parent->_C_color = _Black;
643 if (_Null != __w->_C_child [1])
644 __w->_C_child [1]->_C_color = _Black;
655 _C_link_t __w = __x_parent->_C_child [0];
657 if (_Red == __w->_C_color) {
658 __w->_C_color = _Black;
659 __x_parent->_C_color = _Red;
663 __w = __x_parent->_C_child [0];
666 if ((_Null == __w->_C_child [1] ||
667 _Black == __w->_C_child [1]->_C_color) &&
668 (_Null == __w->_C_child [0] ||
669 _Black == __w->_C_child [0]->_C_color)) {
670 __w->_C_color = _Red;
672 __x_parent = __x_parent->_C_parent;
675 if (_Null == __w->_C_child [0] ||
676 _Black == __w->_C_child [0]->_C_color) {
677 if (_Null != __w->_C_child [1])
678 __w->_C_child [1]->_C_color = _Black;
680 __w->_C_color = _Red;
684 __w = __x_parent->_C_child [0];
687 __w->_C_color = __x_parent->_C_color;
688 __x_parent->_C_color = _Black;
690 if (_Null != __w->_C_child [0])
691 __w->_C_child [0]->_C_color = _Black;
701 const bool __left = (__x == __x_parent->_C_child [0]);
702 const bool __right = !__left;
704 _C_link_t __w = __x_parent->_C_child [__left];
706 if (_Red == __w->_C_color) {
707 __w->_C_color = _Black;
708 __x_parent->_C_color = _Red;
711 _C_rotate (__x_parent, __right);
712 __w = __x_parent->_C_child [__left];
715 if ((_Null == __w->_C_child [__right] ||
716 _Black == __w->_C_child [__right]->_C_color) &&
717 (_Null == __w->_C_child [__left ] ||
718 _Black == __w->_C_child [__left ]->_C_color)) {
719 __w->_C_color = _Red;
721 __x_parent = __x->_C_parent;
724 if (_Null == __w->_C_child [__left] ||
725 _Black == __w->_C_child [__left]->_C_color) {
727 if (_Null != __w->_C_child [__right])
728 __w->_C_child [__right]->_C_color = _Black;
730 __w->_C_color = _Red;
731 _C_rotate (__w, __left);
732 __w = __x_parent->_C_child [__left];
736 __w->_C_color = __x_parent->_C_color;
737 __x_parent->_C_color = _Black;
739 if (_Null != __w->_C_child [__left])
740 __w->_C_child [__left]->_C_color = _Black;
742 _C_rotate (__x_parent, __right);
752 __x->_C_color = _Black;
764template <
class _Key,
class _Val,
class _KeyOf,
class _Comp,
class _Alloc>
765typename __rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::size_type
766__rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::
767erase (
const key_type &__x)
769 const _STD::pair<iterator, iterator> __p = equal_range(__x);
771 const size_type __n =
_DISTANCE (__p.first, __p.second, size_type);
773 erase (__p.first, __p.second);
779template <
class _Key,
class _Val,
class _KeyOf,
class _Comp,
class _Alloc>
780typename __rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::_C_link_t
781__rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::
782_C_copy (_C_link_t __x, _C_link_t __p)
785 _C_link_t __res = __x;
787 while (_C_link_t () != __x) {
788 const _C_link_t __y = _C_get_node (__x->_C_value);
792 __y->_C_color = __x->_C_color;
793 __y->_C_parent = __p;
794 __p->_C_child [0] = __y;
795 __y->_C_child [1] = _C_copy (__x->_C_child [1], __y);
797 __x = __x->_C_child [0];
799 __p->_C_child [0] = 0;
805template <
class _Key,
class _Val,
class _KeyOf,
class _Comp,
class _Alloc>
806void __rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::
807_C_erase (_C_link_t __x)
811 while (_C_link_t () != __x) {
812 _C_erase (__x->_C_child [1]);
813 _C_link_t __y = __x->_C_child [0];
820template <
class _Key,
class _Val,
class _KeyOf,
class _Comp,
class _Alloc>
821typename __rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::iterator
822__rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::
823erase (iterator __first, iterator __last)
830 if (__first == begin () && __last ==
end () && _C_size) {
832 _C_erase (_C_end->_C_parent);
835 _C_end->_C_parent = 0;
838 _C_end->_C_child [0] =
839 _C_end->_C_child [1] = _C_end;
847 for (__tmp =
end (); !(__first == __last); __tmp = erase (__first++));
853template <
class _Key,
class _Val,
class _KeyOf,
class _Comp,
class _Alloc>
854typename __rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::iterator
855__rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::
856find (
const key_type &__k)
858 const iterator __j = lower_bound (__k);
860 return __j ==
end () || _C_cmp (__k,
_ITER_NODE (__j)->_C_key ()) ?
865template <
class _Key,
class _Val,
class _KeyOf,
class _Comp,
class _Alloc>
866typename __rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::iterator
867__rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::
868lower_bound (
const key_type &__k)
870 _C_link_t __y = _C_end;
872 for (_C_link_t __x = __y->_C_parent; _C_link_t () != __x; ) {
873 if (_C_cmp (__x->_C_key (), __k)) {
874 __x = __x->_C_child [1];
878 __x = __x->_C_child [0];
882 return _C_make_iter (__y);
886template <
class _Key,
class _Val,
class _KeyOf,
class _Comp,
class _Alloc>
887typename __rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::iterator
888__rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::
889upper_bound (
const key_type &__k)
891 _C_link_t __y = _C_end;
893 for (_C_link_t __x = __y->_C_parent; _C_link_t () != __x; ) {
894 if (_C_cmp (__k, __x->_C_key ())) {
896 __x = __x->_C_child [0];
899 __x = __x->_C_child [1];
902 return _C_make_iter (__y);
906template <
class _Key,
class _Val,
class _KeyOf,
class _Comp,
class _Alloc>
907typename __rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::size_type
908__rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::
909_C_depth (const_iterator __it, size_type *__rank )
const
912 const typename _C_node_t::_C_color_t _Black = _C_node_t::_C_black;
913 const _C_link_t _Null = _C_link_t ();
922 const typename _C_node_t::_C_color_t _Red = _C_node_t::_C_red;
924 if (_Red == __node->_C_color) {
928 const bool __left_child_black =
929 _Null == __node->_C_child [0]
930 || _Black == __node->_C_child [0]->_C_color;
932 const bool __right_child_black =
933 _Null == __node->_C_child [1]
934 || _Black == __node->_C_child [1]->_C_color;
947 size_type __ranks [2];
949 if ( _Null == __node->_C_child [0]
950 || _Black == __node->_C_child [0]->_C_color)
955 if ( _Null == __node->_C_child [1]
956 || _Black == __node->_C_child [1]->_C_color)
961 const size_type __depth [] = {
962 _Null == __node->_C_child [0] ?
963 0 : 1 + _C_depth (_C_make_iter (__node->_C_child [0]), __ranks + 0),
964 _Null == __node->_C_child [1] ?
965 0 : 1 + _C_depth (_C_make_iter (__node->_C_child [1]), __ranks + 1)
971 *__rank += __ranks [0];
973 return __depth [__depth [0] < __depth [1]];
977template <
class _Key,
class _Val,
class _KeyOf,
class _Comp,
class _Alloc>
978typename __rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::size_type
979__rb_tree<_Key, _Val, _KeyOf, _Comp, _Alloc>::
980_C_level (const_iterator __it)
const
985 return 1 + _C_level (_C_make_iter (
_ITER_NODE (__it)->_C_parent));
#define _RWSTD_NAMESPACE(name)
#define _RWSTD_NEW_CAPACITY(ignore, what, from)
#define _RWSTD_ASSERT(ignore)
#define _DISTANCE(first, last, ignore)
#define _RWSTD_ASSERT_RANGE(ign1, ign2)