]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/stl_tree.h
stl_construct.h (_Destroy): New three-argument overload that takes an allocator argument.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_tree.h
1 // RB tree implementation -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 /*
31 *
32 * Copyright (c) 1996,1997
33 * Silicon Graphics Computer Systems, Inc.
34 *
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Silicon Graphics makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
42 *
43 *
44 * Copyright (c) 1994
45 * Hewlett-Packard Company
46 *
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Hewlett-Packard Company makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
54 *
55 *
56 */
57
58 /** @file stl_tree.h
59 * This is an internal header file, included by other library headers.
60 * You should not attempt to use it directly.
61 */
62
63 #ifndef _TREE_H
64 #define _TREE_H 1
65
66 #include <bits/stl_algobase.h>
67 #include <bits/allocator.h>
68 #include <bits/stl_construct.h>
69 #include <bits/stl_function.h>
70 #include <bits/cpp_type_traits.h>
71
72 namespace std
73 {
74 // Red-black tree class, designed for use in implementing STL
75 // associative containers (set, multiset, map, and multimap). The
76 // insertion and deletion algorithms are based on those in Cormen,
77 // Leiserson, and Rivest, Introduction to Algorithms (MIT Press,
78 // 1990), except that
79 //
80 // (1) the header cell is maintained with links not only to the root
81 // but also to the leftmost node of the tree, to enable constant
82 // time begin(), and to the rightmost node of the tree, to enable
83 // linear time performance when used with the generic set algorithms
84 // (set_union, etc.)
85 //
86 // (2) when a node being deleted has two children its successor node
87 // is relinked into its place, rather than copied, so that the only
88 // iterators invalidated are those referring to the deleted node.
89
90 enum _Rb_tree_color { _S_red = false, _S_black = true };
91
92 struct _Rb_tree_node_base
93 {
94 typedef _Rb_tree_node_base* _Base_ptr;
95 typedef const _Rb_tree_node_base* _Const_Base_ptr;
96
97 _Rb_tree_color _M_color;
98 _Base_ptr _M_parent;
99 _Base_ptr _M_left;
100 _Base_ptr _M_right;
101
102 static _Base_ptr
103 _S_minimum(_Base_ptr __x)
104 {
105 while (__x->_M_left != 0) __x = __x->_M_left;
106 return __x;
107 }
108
109 static _Const_Base_ptr
110 _S_minimum(_Const_Base_ptr __x)
111 {
112 while (__x->_M_left != 0) __x = __x->_M_left;
113 return __x;
114 }
115
116 static _Base_ptr
117 _S_maximum(_Base_ptr __x)
118 {
119 while (__x->_M_right != 0) __x = __x->_M_right;
120 return __x;
121 }
122
123 static _Const_Base_ptr
124 _S_maximum(_Const_Base_ptr __x)
125 {
126 while (__x->_M_right != 0) __x = __x->_M_right;
127 return __x;
128 }
129 };
130
131 template<typename _Val>
132 struct _Rb_tree_node : public _Rb_tree_node_base
133 {
134 typedef _Rb_tree_node<_Val>* _Link_type;
135 _Val _M_value_field;
136 };
137
138 _Rb_tree_node_base*
139 _Rb_tree_increment(_Rb_tree_node_base* __x);
140
141 const _Rb_tree_node_base*
142 _Rb_tree_increment(const _Rb_tree_node_base* __x);
143
144 _Rb_tree_node_base*
145 _Rb_tree_decrement(_Rb_tree_node_base* __x);
146
147 const _Rb_tree_node_base*
148 _Rb_tree_decrement(const _Rb_tree_node_base* __x);
149
150 template<typename _Tp>
151 struct _Rb_tree_iterator
152 {
153 typedef _Tp value_type;
154 typedef _Tp& reference;
155 typedef _Tp* pointer;
156
157 typedef bidirectional_iterator_tag iterator_category;
158 typedef ptrdiff_t difference_type;
159
160 typedef _Rb_tree_iterator<_Tp> _Self;
161 typedef _Rb_tree_node_base::_Base_ptr _Base_ptr;
162 typedef _Rb_tree_node<_Tp>* _Link_type;
163
164 _Rb_tree_iterator() { }
165
166 _Rb_tree_iterator(_Link_type __x)
167 : _M_node(__x) { }
168
169 reference
170 operator*() const
171 { return static_cast<_Link_type>(_M_node)->_M_value_field; }
172
173 pointer
174 operator->() const
175 { return &static_cast<_Link_type>(_M_node)->_M_value_field; }
176
177 _Self&
178 operator++()
179 {
180 _M_node = _Rb_tree_increment(_M_node);
181 return *this;
182 }
183
184 _Self
185 operator++(int)
186 {
187 _Self __tmp = *this;
188 _M_node = _Rb_tree_increment(_M_node);
189 return __tmp;
190 }
191
192 _Self&
193 operator--()
194 {
195 _M_node = _Rb_tree_decrement(_M_node);
196 return *this;
197 }
198
199 _Self
200 operator--(int)
201 {
202 _Self __tmp = *this;
203 _M_node = _Rb_tree_decrement(_M_node);
204 return __tmp;
205 }
206
207 bool
208 operator==(const _Self& __x) const
209 { return _M_node == __x._M_node; }
210
211 bool
212 operator!=(const _Self& __x) const
213 { return _M_node != __x._M_node; }
214
215 _Base_ptr _M_node;
216 };
217
218 template<typename _Tp>
219 struct _Rb_tree_const_iterator
220 {
221 typedef _Tp value_type;
222 typedef const _Tp& reference;
223 typedef const _Tp* pointer;
224
225 typedef _Rb_tree_iterator<_Tp> iterator;
226
227 typedef bidirectional_iterator_tag iterator_category;
228 typedef ptrdiff_t difference_type;
229
230 typedef _Rb_tree_const_iterator<_Tp> _Self;
231 typedef _Rb_tree_node_base::_Const_Base_ptr _Base_ptr;
232 typedef const _Rb_tree_node<_Tp>* _Link_type;
233
234 _Rb_tree_const_iterator() { }
235
236 _Rb_tree_const_iterator(_Link_type __x)
237 : _M_node(__x) { }
238
239 _Rb_tree_const_iterator(const iterator& __it)
240 : _M_node(__it._M_node) { }
241
242 reference
243 operator*() const
244 { return static_cast<_Link_type>(_M_node)->_M_value_field; }
245
246 pointer
247 operator->() const
248 { return &static_cast<_Link_type>(_M_node)->_M_value_field; }
249
250 _Self&
251 operator++()
252 {
253 _M_node = _Rb_tree_increment(_M_node);
254 return *this;
255 }
256
257 _Self
258 operator++(int)
259 {
260 _Self __tmp = *this;
261 _M_node = _Rb_tree_increment(_M_node);
262 return __tmp;
263 }
264
265 _Self&
266 operator--()
267 {
268 _M_node = _Rb_tree_decrement(_M_node);
269 return *this;
270 }
271
272 _Self
273 operator--(int)
274 {
275 _Self __tmp = *this;
276 _M_node = _Rb_tree_decrement(_M_node);
277 return __tmp;
278 }
279
280 bool
281 operator==(const _Self& __x) const
282 { return _M_node == __x._M_node; }
283
284 bool
285 operator!=(const _Self& __x) const
286 { return _M_node != __x._M_node; }
287
288 _Base_ptr _M_node;
289 };
290
291 template<typename _Val>
292 inline bool
293 operator==(const _Rb_tree_iterator<_Val>& __x,
294 const _Rb_tree_const_iterator<_Val>& __y)
295 { return __x._M_node == __y._M_node; }
296
297 template<typename _Val>
298 inline bool
299 operator!=(const _Rb_tree_iterator<_Val>& __x,
300 const _Rb_tree_const_iterator<_Val>& __y)
301 { return __x._M_node != __y._M_node; }
302
303 void
304 _Rb_tree_rotate_left(_Rb_tree_node_base* const __x,
305 _Rb_tree_node_base*& __root);
306
307 void
308 _Rb_tree_rotate_right(_Rb_tree_node_base* const __x,
309 _Rb_tree_node_base*& __root);
310
311 void
312 _Rb_tree_insert_and_rebalance(const bool __insert_left,
313 _Rb_tree_node_base* __x,
314 _Rb_tree_node_base* __p,
315 _Rb_tree_node_base& __header);
316
317 _Rb_tree_node_base*
318 _Rb_tree_rebalance_for_erase(_Rb_tree_node_base* const __z,
319 _Rb_tree_node_base& __header);
320
321
322 template<typename _Key, typename _Val, typename _KeyOfValue,
323 typename _Compare, typename _Alloc = allocator<_Val> >
324 class _Rb_tree
325 {
326 typedef typename _Alloc::template rebind<_Rb_tree_node<_Val> >::other
327 _Node_allocator;
328
329 protected:
330 typedef _Rb_tree_node_base* _Base_ptr;
331 typedef const _Rb_tree_node_base* _Const_Base_ptr;
332 typedef _Rb_tree_node<_Val> _Rb_tree_node;
333
334 public:
335 typedef _Key key_type;
336 typedef _Val value_type;
337 typedef value_type* pointer;
338 typedef const value_type* const_pointer;
339 typedef value_type& reference;
340 typedef const value_type& const_reference;
341 typedef _Rb_tree_node* _Link_type;
342 typedef const _Rb_tree_node* _Const_Link_type;
343 typedef size_t size_type;
344 typedef ptrdiff_t difference_type;
345 typedef _Alloc allocator_type;
346
347 allocator_type
348 get_allocator() const
349 { return *static_cast<const _Node_allocator*>(&this->_M_impl); }
350
351 protected:
352 _Rb_tree_node*
353 _M_get_node()
354 { return _M_impl._Node_allocator::allocate(1); }
355
356 void
357 _M_put_node(_Rb_tree_node* __p)
358 { _M_impl._Node_allocator::deallocate(__p, 1); }
359
360 _Link_type
361 _M_create_node(const value_type& __x)
362 {
363 _Link_type __tmp = _M_get_node();
364 try
365 { get_allocator().construct(&__tmp->_M_value_field, __x); }
366 catch(...)
367 {
368 _M_put_node(__tmp);
369 __throw_exception_again;
370 }
371 return __tmp;
372 }
373
374 _Link_type
375 _M_clone_node(_Const_Link_type __x)
376 {
377 _Link_type __tmp = _M_create_node(__x->_M_value_field);
378 __tmp->_M_color = __x->_M_color;
379 __tmp->_M_left = 0;
380 __tmp->_M_right = 0;
381 return __tmp;
382 }
383
384 void
385 destroy_node(_Link_type __p)
386 {
387 get_allocator().destroy(&__p->_M_value_field);
388 _M_put_node(__p);
389 }
390
391 protected:
392 template<typename _Key_compare,
393 bool _Is_pod_comparator = std::__is_pod<_Key_compare>::_M_type>
394 struct _Rb_tree_impl : public _Node_allocator
395 {
396 _Key_compare _M_key_compare;
397 _Rb_tree_node_base _M_header;
398 size_type _M_node_count; // Keeps track of size of tree.
399
400 _Rb_tree_impl(const _Node_allocator& __a = _Node_allocator(),
401 const _Key_compare& __comp = _Key_compare())
402 : _Node_allocator(__a), _M_key_compare(__comp), _M_node_count(0)
403 {
404 this->_M_header._M_color = _S_red;
405 this->_M_header._M_parent = 0;
406 this->_M_header._M_left = &this->_M_header;
407 this->_M_header._M_right = &this->_M_header;
408 }
409 };
410
411 // Specialization for _Comparison types that are not capable of
412 // being base classes / super classes.
413 template<typename _Key_compare>
414 struct _Rb_tree_impl<_Key_compare, true> : public _Node_allocator
415 {
416 _Key_compare _M_key_compare;
417 _Rb_tree_node_base _M_header;
418 size_type _M_node_count; // Keeps track of size of tree.
419
420 _Rb_tree_impl(const _Node_allocator& __a = _Node_allocator(),
421 const _Key_compare& __comp = _Key_compare())
422 : _Node_allocator(__a), _M_key_compare(__comp), _M_node_count(0)
423 {
424 this->_M_header._M_color = _S_red;
425 this->_M_header._M_parent = 0;
426 this->_M_header._M_left = &this->_M_header;
427 this->_M_header._M_right = &this->_M_header;
428 }
429 };
430
431 _Rb_tree_impl<_Compare> _M_impl;
432
433 protected:
434 _Base_ptr&
435 _M_root()
436 { return this->_M_impl._M_header._M_parent; }
437
438 _Const_Base_ptr
439 _M_root() const
440 { return this->_M_impl._M_header._M_parent; }
441
442 _Base_ptr&
443 _M_leftmost()
444 { return this->_M_impl._M_header._M_left; }
445
446 _Const_Base_ptr
447 _M_leftmost() const
448 { return this->_M_impl._M_header._M_left; }
449
450 _Base_ptr&
451 _M_rightmost()
452 { return this->_M_impl._M_header._M_right; }
453
454 _Const_Base_ptr
455 _M_rightmost() const
456 { return this->_M_impl._M_header._M_right; }
457
458 _Link_type
459 _M_begin()
460 { return static_cast<_Link_type>(this->_M_impl._M_header._M_parent); }
461
462 _Const_Link_type
463 _M_begin() const
464 { return static_cast<
465 _Const_Link_type>(this->_M_impl._M_header._M_parent); }
466
467 _Link_type
468 _M_end()
469 { return static_cast<_Link_type>(&this->_M_impl._M_header); }
470
471 _Const_Link_type
472 _M_end() const
473 { return static_cast<_Const_Link_type>(&this->_M_impl._M_header); }
474
475 static const_reference
476 _S_value(_Const_Link_type __x)
477 { return __x->_M_value_field; }
478
479 static const _Key&
480 _S_key(_Const_Link_type __x)
481 { return _KeyOfValue()(_S_value(__x)); }
482
483 static _Link_type
484 _S_left(_Base_ptr __x)
485 { return static_cast<_Link_type>(__x->_M_left); }
486
487 static _Const_Link_type
488 _S_left(_Const_Base_ptr __x)
489 { return static_cast<_Const_Link_type>(__x->_M_left); }
490
491 static _Link_type
492 _S_right(_Base_ptr __x)
493 { return static_cast<_Link_type>(__x->_M_right); }
494
495 static _Const_Link_type
496 _S_right(_Const_Base_ptr __x)
497 { return static_cast<_Const_Link_type>(__x->_M_right); }
498
499 static const_reference
500 _S_value(_Const_Base_ptr __x)
501 { return static_cast<_Const_Link_type>(__x)->_M_value_field; }
502
503 static const _Key&
504 _S_key(_Const_Base_ptr __x)
505 { return _KeyOfValue()(_S_value(__x)); }
506
507 static _Base_ptr
508 _S_minimum(_Base_ptr __x)
509 { return _Rb_tree_node_base::_S_minimum(__x); }
510
511 static _Const_Base_ptr
512 _S_minimum(_Const_Base_ptr __x)
513 { return _Rb_tree_node_base::_S_minimum(__x); }
514
515 static _Base_ptr
516 _S_maximum(_Base_ptr __x)
517 { return _Rb_tree_node_base::_S_maximum(__x); }
518
519 static _Const_Base_ptr
520 _S_maximum(_Const_Base_ptr __x)
521 { return _Rb_tree_node_base::_S_maximum(__x); }
522
523 public:
524 typedef _Rb_tree_iterator<value_type> iterator;
525 typedef _Rb_tree_const_iterator<value_type> const_iterator;
526
527 typedef std::reverse_iterator<iterator> reverse_iterator;
528 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
529
530 private:
531 iterator
532 _M_insert(_Base_ptr __x, _Base_ptr __y, const value_type& __v);
533
534 _Link_type
535 _M_copy(_Const_Link_type __x, _Link_type __p);
536
537 void
538 _M_erase(_Link_type __x);
539
540 public:
541 // allocation/deallocation
542 _Rb_tree()
543 { }
544
545 _Rb_tree(const _Compare& __comp)
546 : _M_impl(allocator_type(), __comp)
547 { }
548
549 _Rb_tree(const _Compare& __comp, const allocator_type& __a)
550 : _M_impl(__a, __comp)
551 { }
552
553 _Rb_tree(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x)
554 : _M_impl(__x.get_allocator(), __x._M_impl._M_key_compare)
555 {
556 if (__x._M_root() != 0)
557 {
558 _M_root() = _M_copy(__x._M_begin(), _M_end());
559 _M_leftmost() = _S_minimum(_M_root());
560 _M_rightmost() = _S_maximum(_M_root());
561 _M_impl._M_node_count = __x._M_impl._M_node_count;
562 }
563 }
564
565 ~_Rb_tree()
566 { _M_erase(_M_begin()); }
567
568 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>&
569 operator=(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x);
570
571 // Accessors.
572 _Compare
573 key_comp() const
574 { return _M_impl._M_key_compare; }
575
576 iterator
577 begin()
578 { return static_cast<_Link_type>(this->_M_impl._M_header._M_left); }
579
580 const_iterator
581 begin() const
582 { return static_cast<_Const_Link_type>(this->_M_impl._M_header._M_left); }
583
584 iterator
585 end()
586 { return static_cast<_Link_type>(&this->_M_impl._M_header); }
587
588 const_iterator
589 end() const
590 { return static_cast<_Const_Link_type>(&this->_M_impl._M_header); }
591
592 reverse_iterator
593 rbegin()
594 { return reverse_iterator(end()); }
595
596 const_reverse_iterator
597 rbegin() const
598 { return const_reverse_iterator(end()); }
599
600 reverse_iterator
601 rend()
602 { return reverse_iterator(begin()); }
603
604 const_reverse_iterator
605 rend() const
606 { return const_reverse_iterator(begin()); }
607
608 bool
609 empty() const
610 { return _M_impl._M_node_count == 0; }
611
612 size_type
613 size() const
614 { return _M_impl._M_node_count; }
615
616 size_type
617 max_size() const
618 { return size_type(-1); }
619
620 void
621 swap(_Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __t);
622
623 // Insert/erase.
624 pair<iterator,bool>
625 insert_unique(const value_type& __x);
626
627 iterator
628 insert_equal(const value_type& __x);
629
630 iterator
631 insert_unique(iterator __position, const value_type& __x);
632
633 iterator
634 insert_equal(iterator __position, const value_type& __x);
635
636 template<typename _InputIterator>
637 void
638 insert_unique(_InputIterator __first, _InputIterator __last);
639
640 template<typename _InputIterator>
641 void
642 insert_equal(_InputIterator __first, _InputIterator __last);
643
644 void
645 erase(iterator __position);
646
647 size_type
648 erase(const key_type& __x);
649
650 void
651 erase(iterator __first, iterator __last);
652
653 void
654 erase(const key_type* __first, const key_type* __last);
655
656 void
657 clear()
658 {
659 _M_erase(_M_begin());
660 _M_leftmost() = _M_end();
661 _M_root() = 0;
662 _M_rightmost() = _M_end();
663 _M_impl._M_node_count = 0;
664 }
665
666 // Set operations.
667 iterator
668 find(const key_type& __x);
669
670 const_iterator
671 find(const key_type& __x) const;
672
673 size_type
674 count(const key_type& __x) const;
675
676 iterator
677 lower_bound(const key_type& __x);
678
679 const_iterator
680 lower_bound(const key_type& __x) const;
681
682 iterator
683 upper_bound(const key_type& __x);
684
685 const_iterator
686 upper_bound(const key_type& __x) const;
687
688 pair<iterator,iterator>
689 equal_range(const key_type& __x);
690
691 pair<const_iterator, const_iterator>
692 equal_range(const key_type& __x) const;
693
694 // Debugging.
695 bool
696 __rb_verify() const;
697 };
698
699 template<typename _Key, typename _Val, typename _KeyOfValue,
700 typename _Compare, typename _Alloc>
701 inline bool
702 operator==(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x,
703 const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y)
704 {
705 return __x.size() == __y.size()
706 && equal(__x.begin(), __x.end(), __y.begin());
707 }
708
709 template<typename _Key, typename _Val, typename _KeyOfValue,
710 typename _Compare, typename _Alloc>
711 inline bool
712 operator<(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x,
713 const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y)
714 {
715 return lexicographical_compare(__x.begin(), __x.end(),
716 __y.begin(), __y.end());
717 }
718
719 template<typename _Key, typename _Val, typename _KeyOfValue,
720 typename _Compare, typename _Alloc>
721 inline bool
722 operator!=(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x,
723 const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y)
724 { return !(__x == __y); }
725
726 template<typename _Key, typename _Val, typename _KeyOfValue,
727 typename _Compare, typename _Alloc>
728 inline bool
729 operator>(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x,
730 const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y)
731 { return __y < __x; }
732
733 template<typename _Key, typename _Val, typename _KeyOfValue,
734 typename _Compare, typename _Alloc>
735 inline bool
736 operator<=(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x,
737 const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y)
738 { return !(__y < __x); }
739
740 template<typename _Key, typename _Val, typename _KeyOfValue,
741 typename _Compare, typename _Alloc>
742 inline bool
743 operator>=(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x,
744 const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y)
745 { return !(__x < __y); }
746
747 template<typename _Key, typename _Val, typename _KeyOfValue,
748 typename _Compare, typename _Alloc>
749 inline void
750 swap(_Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x,
751 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y)
752 { __x.swap(__y); }
753
754 template<typename _Key, typename _Val, typename _KeyOfValue,
755 typename _Compare, typename _Alloc>
756 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>&
757 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
758 operator=(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x)
759 {
760 if (this != &__x)
761 {
762 // Note that _Key may be a constant type.
763 clear();
764 _M_impl._M_key_compare = __x._M_impl._M_key_compare;
765 if (__x._M_root() != 0)
766 {
767 _M_root() = _M_copy(__x._M_begin(), _M_end());
768 _M_leftmost() = _S_minimum(_M_root());
769 _M_rightmost() = _S_maximum(_M_root());
770 _M_impl._M_node_count = __x._M_impl._M_node_count;
771 }
772 }
773 return *this;
774 }
775
776 template<typename _Key, typename _Val, typename _KeyOfValue,
777 typename _Compare, typename _Alloc>
778 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator
779 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
780 _M_insert(_Base_ptr __x, _Base_ptr __p, const _Val& __v)
781 {
782 _Link_type __z = _M_create_node(__v);
783 bool __insert_left;
784
785 __insert_left = __x != 0 || __p == _M_end()
786 || _M_impl._M_key_compare(_KeyOfValue()(__v),
787 _S_key(__p));
788
789 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
790 this->_M_impl._M_header);
791 ++_M_impl._M_node_count;
792 return iterator(__z);
793 }
794
795 template<typename _Key, typename _Val, typename _KeyOfValue,
796 typename _Compare, typename _Alloc>
797 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator
798 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
799 insert_equal(const _Val& __v)
800 {
801 _Link_type __x = _M_begin();
802 _Link_type __y = _M_end();
803 while (__x != 0)
804 {
805 __y = __x;
806 __x = _M_impl._M_key_compare(_KeyOfValue()(__v), _S_key(__x)) ?
807 _S_left(__x) : _S_right(__x);
808 }
809 return _M_insert(__x, __y, __v);
810 }
811
812 template<typename _Key, typename _Val, typename _KeyOfValue,
813 typename _Compare, typename _Alloc>
814 void
815 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
816 swap(_Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __t)
817 {
818 if (_M_root() == 0)
819 {
820 if (__t._M_root() != 0)
821 {
822 _M_root() = __t._M_root();
823 _M_leftmost() = __t._M_leftmost();
824 _M_rightmost() = __t._M_rightmost();
825 _M_root()->_M_parent = _M_end();
826
827 __t._M_root() = 0;
828 __t._M_leftmost() = __t._M_end();
829 __t._M_rightmost() = __t._M_end();
830 }
831 }
832 else if (__t._M_root() == 0)
833 {
834 __t._M_root() = _M_root();
835 __t._M_leftmost() = _M_leftmost();
836 __t._M_rightmost() = _M_rightmost();
837 __t._M_root()->_M_parent = __t._M_end();
838
839 _M_root() = 0;
840 _M_leftmost() = _M_end();
841 _M_rightmost() = _M_end();
842 }
843 else
844 {
845 std::swap(_M_root(),__t._M_root());
846 std::swap(_M_leftmost(),__t._M_leftmost());
847 std::swap(_M_rightmost(),__t._M_rightmost());
848
849 _M_root()->_M_parent = _M_end();
850 __t._M_root()->_M_parent = __t._M_end();
851 }
852 // No need to swap header's color as it does not change.
853 std::swap(this->_M_impl._M_node_count, __t._M_impl._M_node_count);
854 std::swap(this->_M_impl._M_key_compare, __t._M_impl._M_key_compare);
855 }
856
857 template<typename _Key, typename _Val, typename _KeyOfValue,
858 typename _Compare, typename _Alloc>
859 pair<typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator,
860 bool>
861 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
862 insert_unique(const _Val& __v)
863 {
864 _Link_type __x = _M_begin();
865 _Link_type __y = _M_end();
866 bool __comp = true;
867 while (__x != 0)
868 {
869 __y = __x;
870 __comp = _M_impl._M_key_compare(_KeyOfValue()(__v), _S_key(__x));
871 __x = __comp ? _S_left(__x) : _S_right(__x);
872 }
873 iterator __j = iterator(__y);
874 if (__comp)
875 if (__j == begin())
876 return pair<iterator,bool>(_M_insert(__x, __y, __v), true);
877 else
878 --__j;
879 if (_M_impl._M_key_compare(_S_key(__j._M_node), _KeyOfValue()(__v)))
880 return pair<iterator,bool>(_M_insert(__x, __y, __v), true);
881 return pair<iterator,bool>(__j, false);
882 }
883
884 template<typename _Key, typename _Val, typename _KeyOfValue,
885 typename _Compare, typename _Alloc>
886 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
887 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
888 insert_unique(iterator __position, const _Val& __v)
889 {
890 if (__position._M_node == _M_leftmost())
891 {
892 // begin()
893 if (size() > 0
894 && _M_impl._M_key_compare(_KeyOfValue()(__v),
895 _S_key(__position._M_node)))
896 return _M_insert(__position._M_node, __position._M_node, __v);
897 // First argument just needs to be non-null.
898 else
899 return insert_unique(__v).first;
900 }
901 else if (__position._M_node == _M_end())
902 {
903 // end()
904 if (_M_impl._M_key_compare(_S_key(_M_rightmost()),
905 _KeyOfValue()(__v)))
906 return _M_insert(0, _M_rightmost(), __v);
907 else
908 return insert_unique(__v).first;
909 }
910 else
911 {
912 iterator __before = __position;
913 --__before;
914 if (_M_impl._M_key_compare(_S_key(__before._M_node),
915 _KeyOfValue()(__v))
916 && _M_impl._M_key_compare(_KeyOfValue()(__v),
917 _S_key(__position._M_node)))
918 {
919 if (_S_right(__before._M_node) == 0)
920 return _M_insert(0, __before._M_node, __v);
921 else
922 return _M_insert(__position._M_node, __position._M_node, __v);
923 // First argument just needs to be non-null.
924 }
925 else
926 return insert_unique(__v).first;
927 }
928 }
929
930 template<typename _Key, typename _Val, typename _KeyOfValue,
931 typename _Compare, typename _Alloc>
932 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator
933 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
934 insert_equal(iterator __position, const _Val& __v)
935 {
936 if (__position._M_node == _M_leftmost())
937 {
938 // begin()
939 if (size() > 0
940 && !_M_impl._M_key_compare(_S_key(__position._M_node),
941 _KeyOfValue()(__v)))
942 return _M_insert(__position._M_node, __position._M_node, __v);
943 // first argument just needs to be non-null
944 else
945 return insert_equal(__v);
946 }
947 else if (__position._M_node == _M_end())
948 {
949 // end()
950 if (!_M_impl._M_key_compare(_KeyOfValue()(__v),
951 _S_key(_M_rightmost())))
952 return _M_insert(0, _M_rightmost(), __v);
953 else
954 return insert_equal(__v);
955 }
956 else
957 {
958 iterator __before = __position;
959 --__before;
960 if (!_M_impl._M_key_compare(_KeyOfValue()(__v),
961 _S_key(__before._M_node))
962 && !_M_impl._M_key_compare(_S_key(__position._M_node),
963 _KeyOfValue()(__v)))
964 {
965 if (_S_right(__before._M_node) == 0)
966 return _M_insert(0, __before._M_node, __v);
967 else
968 return _M_insert(__position._M_node, __position._M_node, __v);
969 // First argument just needs to be non-null.
970 }
971 else
972 return insert_equal(__v);
973 }
974 }
975
976 template<typename _Key, typename _Val, typename _KoV,
977 typename _Cmp, typename _Alloc>
978 template<class _II>
979 void
980 _Rb_tree<_Key,_Val,_KoV,_Cmp,_Alloc>::
981 insert_equal(_II __first, _II __last)
982 {
983 for ( ; __first != __last; ++__first)
984 insert_equal(*__first);
985 }
986
987 template<typename _Key, typename _Val, typename _KoV,
988 typename _Cmp, typename _Alloc>
989 template<class _II>
990 void
991 _Rb_tree<_Key,_Val,_KoV,_Cmp,_Alloc>::
992 insert_unique(_II __first, _II __last)
993 {
994 for ( ; __first != __last; ++__first)
995 insert_unique(*__first);
996 }
997
998 template<typename _Key, typename _Val, typename _KeyOfValue,
999 typename _Compare, typename _Alloc>
1000 inline void
1001 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::erase(iterator __position)
1002 {
1003 _Link_type __y = static_cast<
1004 _Link_type>(_Rb_tree_rebalance_for_erase(__position._M_node,
1005 this->_M_impl._M_header));
1006 destroy_node(__y);
1007 --_M_impl._M_node_count;
1008 }
1009
1010 template<typename _Key, typename _Val, typename _KeyOfValue,
1011 typename _Compare, typename _Alloc>
1012 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::size_type
1013 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::erase(const _Key& __x)
1014 {
1015 pair<iterator,iterator> __p = equal_range(__x);
1016 size_type __n = std::distance(__p.first, __p.second);
1017 erase(__p.first, __p.second);
1018 return __n;
1019 }
1020
1021 template<typename _Key, typename _Val, typename _KoV,
1022 typename _Compare, typename _Alloc>
1023 typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Link_type
1024 _Rb_tree<_Key,_Val,_KoV,_Compare,_Alloc>::
1025 _M_copy(_Const_Link_type __x, _Link_type __p)
1026 {
1027 // Structural copy. __x and __p must be non-null.
1028 _Link_type __top = _M_clone_node(__x);
1029 __top->_M_parent = __p;
1030
1031 try
1032 {
1033 if (__x->_M_right)
1034 __top->_M_right = _M_copy(_S_right(__x), __top);
1035 __p = __top;
1036 __x = _S_left(__x);
1037
1038 while (__x != 0)
1039 {
1040 _Link_type __y = _M_clone_node(__x);
1041 __p->_M_left = __y;
1042 __y->_M_parent = __p;
1043 if (__x->_M_right)
1044 __y->_M_right = _M_copy(_S_right(__x), __y);
1045 __p = __y;
1046 __x = _S_left(__x);
1047 }
1048 }
1049 catch(...)
1050 {
1051 _M_erase(__top);
1052 __throw_exception_again;
1053 }
1054 return __top;
1055 }
1056
1057 template<typename _Key, typename _Val, typename _KeyOfValue,
1058 typename _Compare, typename _Alloc>
1059 void
1060 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::_M_erase(_Link_type __x)
1061 {
1062 // Erase without rebalancing.
1063 while (__x != 0)
1064 {
1065 _M_erase(_S_right(__x));
1066 _Link_type __y = _S_left(__x);
1067 destroy_node(__x);
1068 __x = __y;
1069 }
1070 }
1071
1072 template<typename _Key, typename _Val, typename _KeyOfValue,
1073 typename _Compare, typename _Alloc>
1074 void
1075 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
1076 erase(iterator __first, iterator __last)
1077 {
1078 if (__first == begin() && __last == end())
1079 clear();
1080 else
1081 while (__first != __last) erase(__first++);
1082 }
1083
1084 template<typename _Key, typename _Val, typename _KeyOfValue,
1085 typename _Compare, typename _Alloc>
1086 void
1087 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
1088 erase(const _Key* __first, const _Key* __last)
1089 {
1090 while (__first != __last)
1091 erase(*__first++);
1092 }
1093
1094 template<typename _Key, typename _Val, typename _KeyOfValue,
1095 typename _Compare, typename _Alloc>
1096 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator
1097 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::find(const _Key& __k)
1098 {
1099 _Link_type __x = _M_begin(); // Current node.
1100 _Link_type __y = _M_end(); // Last node which is not less than __k.
1101
1102 while (__x != 0)
1103 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1104 __y = __x, __x = _S_left(__x);
1105 else
1106 __x = _S_right(__x);
1107
1108 iterator __j = iterator(__y);
1109 return (__j == end()
1110 || _M_impl._M_key_compare(__k, _S_key(__j._M_node))) ? end() : __j;
1111 }
1112
1113 template<typename _Key, typename _Val, typename _KeyOfValue,
1114 typename _Compare, typename _Alloc>
1115 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::const_iterator
1116 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
1117 find(const _Key& __k) const
1118 {
1119 _Const_Link_type __x = _M_begin(); // Current node.
1120 _Const_Link_type __y = _M_end(); // Last node which is not less than __k.
1121
1122 while (__x != 0)
1123 {
1124 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1125 __y = __x, __x = _S_left(__x);
1126 else
1127 __x = _S_right(__x);
1128 }
1129 const_iterator __j = const_iterator(__y);
1130 return (__j == end()
1131 || _M_impl._M_key_compare(__k, _S_key(__j._M_node))) ? end() : __j;
1132 }
1133
1134 template<typename _Key, typename _Val, typename _KeyOfValue,
1135 typename _Compare, typename _Alloc>
1136 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::size_type
1137 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
1138 count(const _Key& __k) const
1139 {
1140 pair<const_iterator, const_iterator> __p = equal_range(__k);
1141 const size_type __n = std::distance(__p.first, __p.second);
1142 return __n;
1143 }
1144
1145 template<typename _Key, typename _Val, typename _KeyOfValue,
1146 typename _Compare, typename _Alloc>
1147 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator
1148 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
1149 lower_bound(const _Key& __k)
1150 {
1151 _Link_type __x = _M_begin(); // Current node.
1152 _Link_type __y = _M_end(); // Last node which is not less than __k.
1153
1154 while (__x != 0)
1155 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1156 __y = __x, __x = _S_left(__x);
1157 else
1158 __x = _S_right(__x);
1159
1160 return iterator(__y);
1161 }
1162
1163 template<typename _Key, typename _Val, typename _KeyOfValue,
1164 typename _Compare, typename _Alloc>
1165 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::const_iterator
1166 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
1167 lower_bound(const _Key& __k) const
1168 {
1169 _Const_Link_type __x = _M_begin(); // Current node.
1170 _Const_Link_type __y = _M_end(); // Last node which is not less than __k.
1171
1172 while (__x != 0)
1173 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1174 __y = __x, __x = _S_left(__x);
1175 else
1176 __x = _S_right(__x);
1177
1178 return const_iterator(__y);
1179 }
1180
1181 template<typename _Key, typename _Val, typename _KeyOfValue,
1182 typename _Compare, typename _Alloc>
1183 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator
1184 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
1185 upper_bound(const _Key& __k)
1186 {
1187 _Link_type __x = _M_begin(); // Current node.
1188 _Link_type __y = _M_end(); // Last node which is greater than __k.
1189
1190 while (__x != 0)
1191 if (_M_impl._M_key_compare(__k, _S_key(__x)))
1192 __y = __x, __x = _S_left(__x);
1193 else
1194 __x = _S_right(__x);
1195
1196 return iterator(__y);
1197 }
1198
1199 template<typename _Key, typename _Val, typename _KeyOfValue,
1200 typename _Compare, typename _Alloc>
1201 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::const_iterator
1202 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
1203 upper_bound(const _Key& __k) const
1204 {
1205 _Const_Link_type __x = _M_begin(); // Current node.
1206 _Const_Link_type __y = _M_end(); // Last node which is greater than __k.
1207
1208 while (__x != 0)
1209 if (_M_impl._M_key_compare(__k, _S_key(__x)))
1210 __y = __x, __x = _S_left(__x);
1211 else
1212 __x = _S_right(__x);
1213
1214 return const_iterator(__y);
1215 }
1216
1217 template<typename _Key, typename _Val, typename _KeyOfValue,
1218 typename _Compare, typename _Alloc>
1219 inline
1220 pair<typename _Rb_tree<_Key,_Val,_KeyOfValue,
1221 _Compare,_Alloc>::iterator,
1222 typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator>
1223 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
1224 equal_range(const _Key& __k)
1225 { return pair<iterator, iterator>(lower_bound(__k), upper_bound(__k)); }
1226
1227 template<typename _Key, typename _Val, typename _KoV,
1228 typename _Compare, typename _Alloc>
1229 inline
1230 pair<typename _Rb_tree<_Key, _Val, _KoV,
1231 _Compare, _Alloc>::const_iterator,
1232 typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::const_iterator>
1233 _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
1234 equal_range(const _Key& __k) const
1235 { return pair<const_iterator, const_iterator>(lower_bound(__k),
1236 upper_bound(__k)); }
1237
1238 unsigned int
1239 _Rb_tree_black_count(const _Rb_tree_node_base* __node,
1240 const _Rb_tree_node_base* __root);
1241
1242 template<typename _Key, typename _Val, typename _KeyOfValue,
1243 typename _Compare, typename _Alloc>
1244 bool
1245 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const
1246 {
1247 if (_M_impl._M_node_count == 0 || begin() == end())
1248 return _M_impl._M_node_count == 0 && begin() == end()
1249 && this->_M_impl._M_header._M_left == _M_end()
1250 && this->_M_impl._M_header._M_right == _M_end();
1251
1252 unsigned int __len = _Rb_tree_black_count(_M_leftmost(), _M_root());
1253 for (const_iterator __it = begin(); __it != end(); ++__it)
1254 {
1255 _Const_Link_type __x = static_cast<_Const_Link_type>(__it._M_node);
1256 _Const_Link_type __L = _S_left(__x);
1257 _Const_Link_type __R = _S_right(__x);
1258
1259 if (__x->_M_color == _S_red)
1260 if ((__L && __L->_M_color == _S_red)
1261 || (__R && __R->_M_color == _S_red))
1262 return false;
1263
1264 if (__L && _M_impl._M_key_compare(_S_key(__x), _S_key(__L)))
1265 return false;
1266 if (__R && _M_impl._M_key_compare(_S_key(__R), _S_key(__x)))
1267 return false;
1268
1269 if (!__L && !__R && _Rb_tree_black_count(__x, _M_root()) != __len)
1270 return false;
1271 }
1272
1273 if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
1274 return false;
1275 if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
1276 return false;
1277 return true;
1278 }
1279 } // namespace std
1280
1281 #endif