]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/stl_tree.h
re PR libstdc++/23781 (Implicit conversion from NULL to list<T>::iterator)
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_tree.h
1 // RB tree implementation -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 : _M_node() { }
166
167 explicit
168 _Rb_tree_iterator(_Link_type __x)
169 : _M_node(__x) { }
170
171 reference
172 operator*() const
173 { return static_cast<_Link_type>(_M_node)->_M_value_field; }
174
175 pointer
176 operator->() const
177 { return &static_cast<_Link_type>(_M_node)->_M_value_field; }
178
179 _Self&
180 operator++()
181 {
182 _M_node = _Rb_tree_increment(_M_node);
183 return *this;
184 }
185
186 _Self
187 operator++(int)
188 {
189 _Self __tmp = *this;
190 _M_node = _Rb_tree_increment(_M_node);
191 return __tmp;
192 }
193
194 _Self&
195 operator--()
196 {
197 _M_node = _Rb_tree_decrement(_M_node);
198 return *this;
199 }
200
201 _Self
202 operator--(int)
203 {
204 _Self __tmp = *this;
205 _M_node = _Rb_tree_decrement(_M_node);
206 return __tmp;
207 }
208
209 bool
210 operator==(const _Self& __x) const
211 { return _M_node == __x._M_node; }
212
213 bool
214 operator!=(const _Self& __x) const
215 { return _M_node != __x._M_node; }
216
217 _Base_ptr _M_node;
218 };
219
220 template<typename _Tp>
221 struct _Rb_tree_const_iterator
222 {
223 typedef _Tp value_type;
224 typedef const _Tp& reference;
225 typedef const _Tp* pointer;
226
227 typedef _Rb_tree_iterator<_Tp> iterator;
228
229 typedef bidirectional_iterator_tag iterator_category;
230 typedef ptrdiff_t difference_type;
231
232 typedef _Rb_tree_const_iterator<_Tp> _Self;
233 typedef _Rb_tree_node_base::_Const_Base_ptr _Base_ptr;
234 typedef const _Rb_tree_node<_Tp>* _Link_type;
235
236 _Rb_tree_const_iterator()
237 : _M_node() { }
238
239 explicit
240 _Rb_tree_const_iterator(_Link_type __x)
241 : _M_node(__x) { }
242
243 _Rb_tree_const_iterator(const iterator& __it)
244 : _M_node(__it._M_node) { }
245
246 reference
247 operator*() const
248 { return static_cast<_Link_type>(_M_node)->_M_value_field; }
249
250 pointer
251 operator->() const
252 { return &static_cast<_Link_type>(_M_node)->_M_value_field; }
253
254 _Self&
255 operator++()
256 {
257 _M_node = _Rb_tree_increment(_M_node);
258 return *this;
259 }
260
261 _Self
262 operator++(int)
263 {
264 _Self __tmp = *this;
265 _M_node = _Rb_tree_increment(_M_node);
266 return __tmp;
267 }
268
269 _Self&
270 operator--()
271 {
272 _M_node = _Rb_tree_decrement(_M_node);
273 return *this;
274 }
275
276 _Self
277 operator--(int)
278 {
279 _Self __tmp = *this;
280 _M_node = _Rb_tree_decrement(_M_node);
281 return __tmp;
282 }
283
284 bool
285 operator==(const _Self& __x) const
286 { return _M_node == __x._M_node; }
287
288 bool
289 operator!=(const _Self& __x) const
290 { return _M_node != __x._M_node; }
291
292 _Base_ptr _M_node;
293 };
294
295 template<typename _Val>
296 inline bool
297 operator==(const _Rb_tree_iterator<_Val>& __x,
298 const _Rb_tree_const_iterator<_Val>& __y)
299 { return __x._M_node == __y._M_node; }
300
301 template<typename _Val>
302 inline bool
303 operator!=(const _Rb_tree_iterator<_Val>& __x,
304 const _Rb_tree_const_iterator<_Val>& __y)
305 { return __x._M_node != __y._M_node; }
306
307 void
308 _Rb_tree_rotate_left(_Rb_tree_node_base* const __x,
309 _Rb_tree_node_base*& __root);
310
311 void
312 _Rb_tree_rotate_right(_Rb_tree_node_base* const __x,
313 _Rb_tree_node_base*& __root);
314
315 void
316 _Rb_tree_insert_and_rebalance(const bool __insert_left,
317 _Rb_tree_node_base* __x,
318 _Rb_tree_node_base* __p,
319 _Rb_tree_node_base& __header);
320
321 _Rb_tree_node_base*
322 _Rb_tree_rebalance_for_erase(_Rb_tree_node_base* const __z,
323 _Rb_tree_node_base& __header);
324
325
326 template<typename _Key, typename _Val, typename _KeyOfValue,
327 typename _Compare, typename _Alloc = allocator<_Val> >
328 class _Rb_tree
329 {
330 typedef typename _Alloc::template rebind<_Rb_tree_node<_Val> >::other
331 _Node_allocator;
332
333 protected:
334 typedef _Rb_tree_node_base* _Base_ptr;
335 typedef const _Rb_tree_node_base* _Const_Base_ptr;
336 typedef _Rb_tree_node<_Val> _Rb_tree_node;
337
338 public:
339 typedef _Key key_type;
340 typedef _Val value_type;
341 typedef value_type* pointer;
342 typedef const value_type* const_pointer;
343 typedef value_type& reference;
344 typedef const value_type& const_reference;
345 typedef _Rb_tree_node* _Link_type;
346 typedef const _Rb_tree_node* _Const_Link_type;
347 typedef size_t size_type;
348 typedef ptrdiff_t difference_type;
349 typedef _Alloc allocator_type;
350
351 allocator_type
352 get_allocator() const
353 { return *static_cast<const _Node_allocator*>(&this->_M_impl); }
354
355 protected:
356 _Rb_tree_node*
357 _M_get_node()
358 { return _M_impl._Node_allocator::allocate(1); }
359
360 void
361 _M_put_node(_Rb_tree_node* __p)
362 { _M_impl._Node_allocator::deallocate(__p, 1); }
363
364 _Link_type
365 _M_create_node(const value_type& __x)
366 {
367 _Link_type __tmp = _M_get_node();
368 try
369 { get_allocator().construct(&__tmp->_M_value_field, __x); }
370 catch(...)
371 {
372 _M_put_node(__tmp);
373 __throw_exception_again;
374 }
375 return __tmp;
376 }
377
378 _Link_type
379 _M_clone_node(_Const_Link_type __x)
380 {
381 _Link_type __tmp = _M_create_node(__x->_M_value_field);
382 __tmp->_M_color = __x->_M_color;
383 __tmp->_M_left = 0;
384 __tmp->_M_right = 0;
385 return __tmp;
386 }
387
388 void
389 destroy_node(_Link_type __p)
390 {
391 get_allocator().destroy(&__p->_M_value_field);
392 _M_put_node(__p);
393 }
394
395 protected:
396 template<typename _Key_compare,
397 bool _Is_pod_comparator = std::__is_pod<_Key_compare>::__value>
398 struct _Rb_tree_impl : public _Node_allocator
399 {
400 _Key_compare _M_key_compare;
401 _Rb_tree_node_base _M_header;
402 size_type _M_node_count; // Keeps track of size of tree.
403
404 _Rb_tree_impl(const _Node_allocator& __a = _Node_allocator(),
405 const _Key_compare& __comp = _Key_compare())
406 : _Node_allocator(__a), _M_key_compare(__comp), _M_node_count(0)
407 {
408 this->_M_header._M_color = _S_red;
409 this->_M_header._M_parent = 0;
410 this->_M_header._M_left = &this->_M_header;
411 this->_M_header._M_right = &this->_M_header;
412 }
413 };
414
415 // Specialization for _Comparison types that are not capable of
416 // being base classes / super classes.
417 template<typename _Key_compare>
418 struct _Rb_tree_impl<_Key_compare, true> : public _Node_allocator
419 {
420 _Key_compare _M_key_compare;
421 _Rb_tree_node_base _M_header;
422 size_type _M_node_count; // Keeps track of size of tree.
423
424 _Rb_tree_impl(const _Node_allocator& __a = _Node_allocator(),
425 const _Key_compare& __comp = _Key_compare())
426 : _Node_allocator(__a), _M_key_compare(__comp), _M_node_count(0)
427 {
428 this->_M_header._M_color = _S_red;
429 this->_M_header._M_parent = 0;
430 this->_M_header._M_left = &this->_M_header;
431 this->_M_header._M_right = &this->_M_header;
432 }
433 };
434
435 _Rb_tree_impl<_Compare> _M_impl;
436
437 protected:
438 _Base_ptr&
439 _M_root()
440 { return this->_M_impl._M_header._M_parent; }
441
442 _Const_Base_ptr
443 _M_root() const
444 { return this->_M_impl._M_header._M_parent; }
445
446 _Base_ptr&
447 _M_leftmost()
448 { return this->_M_impl._M_header._M_left; }
449
450 _Const_Base_ptr
451 _M_leftmost() const
452 { return this->_M_impl._M_header._M_left; }
453
454 _Base_ptr&
455 _M_rightmost()
456 { return this->_M_impl._M_header._M_right; }
457
458 _Const_Base_ptr
459 _M_rightmost() const
460 { return this->_M_impl._M_header._M_right; }
461
462 _Link_type
463 _M_begin()
464 { return static_cast<_Link_type>(this->_M_impl._M_header._M_parent); }
465
466 _Const_Link_type
467 _M_begin() const
468 {
469 return static_cast<_Const_Link_type>
470 (this->_M_impl._M_header._M_parent);
471 }
472
473 _Link_type
474 _M_end()
475 { return static_cast<_Link_type>(&this->_M_impl._M_header); }
476
477 _Const_Link_type
478 _M_end() const
479 { return static_cast<_Const_Link_type>(&this->_M_impl._M_header); }
480
481 static const_reference
482 _S_value(_Const_Link_type __x)
483 { return __x->_M_value_field; }
484
485 static const _Key&
486 _S_key(_Const_Link_type __x)
487 { return _KeyOfValue()(_S_value(__x)); }
488
489 static _Link_type
490 _S_left(_Base_ptr __x)
491 { return static_cast<_Link_type>(__x->_M_left); }
492
493 static _Const_Link_type
494 _S_left(_Const_Base_ptr __x)
495 { return static_cast<_Const_Link_type>(__x->_M_left); }
496
497 static _Link_type
498 _S_right(_Base_ptr __x)
499 { return static_cast<_Link_type>(__x->_M_right); }
500
501 static _Const_Link_type
502 _S_right(_Const_Base_ptr __x)
503 { return static_cast<_Const_Link_type>(__x->_M_right); }
504
505 static const_reference
506 _S_value(_Const_Base_ptr __x)
507 { return static_cast<_Const_Link_type>(__x)->_M_value_field; }
508
509 static const _Key&
510 _S_key(_Const_Base_ptr __x)
511 { return _KeyOfValue()(_S_value(__x)); }
512
513 static _Base_ptr
514 _S_minimum(_Base_ptr __x)
515 { return _Rb_tree_node_base::_S_minimum(__x); }
516
517 static _Const_Base_ptr
518 _S_minimum(_Const_Base_ptr __x)
519 { return _Rb_tree_node_base::_S_minimum(__x); }
520
521 static _Base_ptr
522 _S_maximum(_Base_ptr __x)
523 { return _Rb_tree_node_base::_S_maximum(__x); }
524
525 static _Const_Base_ptr
526 _S_maximum(_Const_Base_ptr __x)
527 { return _Rb_tree_node_base::_S_maximum(__x); }
528
529 public:
530 typedef _Rb_tree_iterator<value_type> iterator;
531 typedef _Rb_tree_const_iterator<value_type> const_iterator;
532
533 typedef std::reverse_iterator<iterator> reverse_iterator;
534 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
535
536 private:
537 iterator
538 _M_insert(_Base_ptr __x, _Base_ptr __y, const value_type& __v);
539
540 _Link_type
541 _M_copy(_Const_Link_type __x, _Link_type __p);
542
543 void
544 _M_erase(_Link_type __x);
545
546 public:
547 // allocation/deallocation
548 _Rb_tree()
549 { }
550
551 _Rb_tree(const _Compare& __comp)
552 : _M_impl(allocator_type(), __comp)
553 { }
554
555 _Rb_tree(const _Compare& __comp, const allocator_type& __a)
556 : _M_impl(__a, __comp)
557 { }
558
559 _Rb_tree(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x)
560 : _M_impl(__x.get_allocator(), __x._M_impl._M_key_compare)
561 {
562 if (__x._M_root() != 0)
563 {
564 _M_root() = _M_copy(__x._M_begin(), _M_end());
565 _M_leftmost() = _S_minimum(_M_root());
566 _M_rightmost() = _S_maximum(_M_root());
567 _M_impl._M_node_count = __x._M_impl._M_node_count;
568 }
569 }
570
571 ~_Rb_tree()
572 { _M_erase(_M_begin()); }
573
574 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
575 operator=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x);
576
577 // Accessors.
578 _Compare
579 key_comp() const
580 { return _M_impl._M_key_compare; }
581
582 iterator
583 begin()
584 {
585 return iterator(static_cast<_Link_type>
586 (this->_M_impl._M_header._M_left));
587 }
588
589 const_iterator
590 begin() const
591 {
592 return const_iterator(static_cast<_Const_Link_type>
593 (this->_M_impl._M_header._M_left));
594 }
595
596 iterator
597 end()
598 { return iterator(static_cast<_Link_type>(&this->_M_impl._M_header)); }
599
600 const_iterator
601 end() const
602 {
603 return const_iterator(static_cast<_Const_Link_type>
604 (&this->_M_impl._M_header));
605 }
606
607 reverse_iterator
608 rbegin()
609 { return reverse_iterator(end()); }
610
611 const_reverse_iterator
612 rbegin() const
613 { return const_reverse_iterator(end()); }
614
615 reverse_iterator
616 rend()
617 { return reverse_iterator(begin()); }
618
619 const_reverse_iterator
620 rend() const
621 { return const_reverse_iterator(begin()); }
622
623 bool
624 empty() const
625 { return _M_impl._M_node_count == 0; }
626
627 size_type
628 size() const
629 { return _M_impl._M_node_count; }
630
631 size_type
632 max_size() const
633 { return size_type(-1); }
634
635 void
636 swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __t);
637
638 // Insert/erase.
639 pair<iterator,bool>
640 insert_unique(const value_type& __x);
641
642 iterator
643 insert_equal(const value_type& __x);
644
645 iterator
646 insert_unique(iterator __position, const value_type& __x);
647
648 iterator
649 insert_equal(iterator __position, const value_type& __x);
650
651 template<typename _InputIterator>
652 void
653 insert_unique(_InputIterator __first, _InputIterator __last);
654
655 template<typename _InputIterator>
656 void
657 insert_equal(_InputIterator __first, _InputIterator __last);
658
659 void
660 erase(iterator __position);
661
662 size_type
663 erase(const key_type& __x);
664
665 void
666 erase(iterator __first, iterator __last);
667
668 void
669 erase(const key_type* __first, const key_type* __last);
670
671 void
672 clear()
673 {
674 _M_erase(_M_begin());
675 _M_leftmost() = _M_end();
676 _M_root() = 0;
677 _M_rightmost() = _M_end();
678 _M_impl._M_node_count = 0;
679 }
680
681 // Set operations.
682 iterator
683 find(const key_type& __x);
684
685 const_iterator
686 find(const key_type& __x) const;
687
688 size_type
689 count(const key_type& __x) const;
690
691 iterator
692 lower_bound(const key_type& __x);
693
694 const_iterator
695 lower_bound(const key_type& __x) const;
696
697 iterator
698 upper_bound(const key_type& __x);
699
700 const_iterator
701 upper_bound(const key_type& __x) const;
702
703 pair<iterator,iterator>
704 equal_range(const key_type& __x);
705
706 pair<const_iterator, const_iterator>
707 equal_range(const key_type& __x) const;
708
709 // Debugging.
710 bool
711 __rb_verify() const;
712 };
713
714 template<typename _Key, typename _Val, typename _KeyOfValue,
715 typename _Compare, typename _Alloc>
716 inline bool
717 operator==(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
718 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
719 {
720 return __x.size() == __y.size()
721 && std::equal(__x.begin(), __x.end(), __y.begin());
722 }
723
724 template<typename _Key, typename _Val, typename _KeyOfValue,
725 typename _Compare, typename _Alloc>
726 inline bool
727 operator<(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
728 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
729 {
730 return std::lexicographical_compare(__x.begin(), __x.end(),
731 __y.begin(), __y.end());
732 }
733
734 template<typename _Key, typename _Val, typename _KeyOfValue,
735 typename _Compare, typename _Alloc>
736 inline bool
737 operator!=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
738 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
739 { return !(__x == __y); }
740
741 template<typename _Key, typename _Val, typename _KeyOfValue,
742 typename _Compare, typename _Alloc>
743 inline bool
744 operator>(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
745 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
746 { return __y < __x; }
747
748 template<typename _Key, typename _Val, typename _KeyOfValue,
749 typename _Compare, typename _Alloc>
750 inline bool
751 operator<=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
752 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
753 { return !(__y < __x); }
754
755 template<typename _Key, typename _Val, typename _KeyOfValue,
756 typename _Compare, typename _Alloc>
757 inline bool
758 operator>=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
759 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
760 { return !(__x < __y); }
761
762 template<typename _Key, typename _Val, typename _KeyOfValue,
763 typename _Compare, typename _Alloc>
764 inline void
765 swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
766 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
767 { __x.swap(__y); }
768
769 template<typename _Key, typename _Val, typename _KeyOfValue,
770 typename _Compare, typename _Alloc>
771 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
772 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
773 operator=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x)
774 {
775 if (this != &__x)
776 {
777 // Note that _Key may be a constant type.
778 clear();
779 _M_impl._M_key_compare = __x._M_impl._M_key_compare;
780 if (__x._M_root() != 0)
781 {
782 _M_root() = _M_copy(__x._M_begin(), _M_end());
783 _M_leftmost() = _S_minimum(_M_root());
784 _M_rightmost() = _S_maximum(_M_root());
785 _M_impl._M_node_count = __x._M_impl._M_node_count;
786 }
787 }
788 return *this;
789 }
790
791 template<typename _Key, typename _Val, typename _KeyOfValue,
792 typename _Compare, typename _Alloc>
793 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
794 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
795 _M_insert(_Base_ptr __x, _Base_ptr __p, const _Val& __v)
796 {
797 bool __insert_left = (__x != 0 || __p == _M_end()
798 || _M_impl._M_key_compare(_KeyOfValue()(__v),
799 _S_key(__p)));
800
801 _Link_type __z = _M_create_node(__v);
802
803 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
804 this->_M_impl._M_header);
805 ++_M_impl._M_node_count;
806 return iterator(__z);
807 }
808
809 template<typename _Key, typename _Val, typename _KeyOfValue,
810 typename _Compare, typename _Alloc>
811 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
812 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
813 insert_equal(const _Val& __v)
814 {
815 _Link_type __x = _M_begin();
816 _Link_type __y = _M_end();
817 while (__x != 0)
818 {
819 __y = __x;
820 __x = _M_impl._M_key_compare(_KeyOfValue()(__v), _S_key(__x)) ?
821 _S_left(__x) : _S_right(__x);
822 }
823 return _M_insert(__x, __y, __v);
824 }
825
826 template<typename _Key, typename _Val, typename _KeyOfValue,
827 typename _Compare, typename _Alloc>
828 void
829 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
830 swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __t)
831 {
832 if (_M_root() == 0)
833 {
834 if (__t._M_root() != 0)
835 {
836 _M_root() = __t._M_root();
837 _M_leftmost() = __t._M_leftmost();
838 _M_rightmost() = __t._M_rightmost();
839 _M_root()->_M_parent = _M_end();
840
841 __t._M_root() = 0;
842 __t._M_leftmost() = __t._M_end();
843 __t._M_rightmost() = __t._M_end();
844 }
845 }
846 else if (__t._M_root() == 0)
847 {
848 __t._M_root() = _M_root();
849 __t._M_leftmost() = _M_leftmost();
850 __t._M_rightmost() = _M_rightmost();
851 __t._M_root()->_M_parent = __t._M_end();
852
853 _M_root() = 0;
854 _M_leftmost() = _M_end();
855 _M_rightmost() = _M_end();
856 }
857 else
858 {
859 std::swap(_M_root(),__t._M_root());
860 std::swap(_M_leftmost(),__t._M_leftmost());
861 std::swap(_M_rightmost(),__t._M_rightmost());
862
863 _M_root()->_M_parent = _M_end();
864 __t._M_root()->_M_parent = __t._M_end();
865 }
866 // No need to swap header's color as it does not change.
867 std::swap(this->_M_impl._M_node_count, __t._M_impl._M_node_count);
868 std::swap(this->_M_impl._M_key_compare, __t._M_impl._M_key_compare);
869 }
870
871 template<typename _Key, typename _Val, typename _KeyOfValue,
872 typename _Compare, typename _Alloc>
873 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
874 _Compare, _Alloc>::iterator, bool>
875 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
876 insert_unique(const _Val& __v)
877 {
878 _Link_type __x = _M_begin();
879 _Link_type __y = _M_end();
880 bool __comp = true;
881 while (__x != 0)
882 {
883 __y = __x;
884 __comp = _M_impl._M_key_compare(_KeyOfValue()(__v), _S_key(__x));
885 __x = __comp ? _S_left(__x) : _S_right(__x);
886 }
887 iterator __j = iterator(__y);
888 if (__comp)
889 if (__j == begin())
890 return pair<iterator,bool>(_M_insert(__x, __y, __v), true);
891 else
892 --__j;
893 if (_M_impl._M_key_compare(_S_key(__j._M_node), _KeyOfValue()(__v)))
894 return pair<iterator, bool>(_M_insert(__x, __y, __v), true);
895 return pair<iterator, bool>(__j, false);
896 }
897
898 template<typename _Key, typename _Val, typename _KeyOfValue,
899 typename _Compare, typename _Alloc>
900 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
901 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
902 insert_unique(iterator __position, const _Val& __v)
903 {
904 // end()
905 if (__position._M_node == _M_end())
906 {
907 if (size() > 0
908 && _M_impl._M_key_compare(_S_key(_M_rightmost()),
909 _KeyOfValue()(__v)))
910 return _M_insert(0, _M_rightmost(), __v);
911 else
912 return insert_unique(__v).first;
913 }
914 else if (_M_impl._M_key_compare(_KeyOfValue()(__v),
915 _S_key(__position._M_node)))
916 {
917 // First, try before...
918 iterator __before = __position;
919 if (__position._M_node == _M_leftmost()) // begin()
920 return _M_insert(_M_leftmost(), _M_leftmost(), __v);
921 else if (_M_impl._M_key_compare(_S_key((--__before)._M_node),
922 _KeyOfValue()(__v)))
923 {
924 if (_S_right(__before._M_node) == 0)
925 return _M_insert(0, __before._M_node, __v);
926 else
927 return _M_insert(__position._M_node,
928 __position._M_node, __v);
929 }
930 else
931 return insert_unique(__v).first;
932 }
933 else if (_M_impl._M_key_compare(_S_key(__position._M_node),
934 _KeyOfValue()(__v)))
935 {
936 // ... then try after.
937 iterator __after = __position;
938 if (__position._M_node == _M_rightmost())
939 return _M_insert(0, _M_rightmost(), __v);
940 else if (_M_impl._M_key_compare(_KeyOfValue()(__v),
941 _S_key((++__after)._M_node)))
942 {
943 if (_S_right(__position._M_node) == 0)
944 return _M_insert(0, __position._M_node, __v);
945 else
946 return _M_insert(__after._M_node, __after._M_node, __v);
947 }
948 else
949 return insert_unique(__v).first;
950 }
951 else
952 return __position; // Equivalent keys.
953 }
954
955 template<typename _Key, typename _Val, typename _KeyOfValue,
956 typename _Compare, typename _Alloc>
957 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
958 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
959 insert_equal(iterator __position, const _Val& __v)
960 {
961 // end()
962 if (__position._M_node == _M_end())
963 {
964 if (size() > 0
965 && !_M_impl._M_key_compare(_KeyOfValue()(__v),
966 _S_key(_M_rightmost())))
967 return _M_insert(0, _M_rightmost(), __v);
968 else
969 return insert_equal(__v);
970 }
971 else if (!_M_impl._M_key_compare(_S_key(__position._M_node),
972 _KeyOfValue()(__v)))
973 {
974 // First, try before...
975 iterator __before = __position;
976 if (__position._M_node == _M_leftmost()) // begin()
977 return _M_insert(_M_leftmost(), _M_leftmost(), __v);
978 else if (!_M_impl._M_key_compare(_KeyOfValue()(__v),
979 _S_key((--__before)._M_node)))
980 {
981 if (_S_right(__before._M_node) == 0)
982 return _M_insert(0, __before._M_node, __v);
983 else
984 return _M_insert(__position._M_node,
985 __position._M_node, __v);
986 }
987 else
988 return insert_equal(__v);
989 }
990 else
991 {
992 // ... then try after.
993 iterator __after = __position;
994 if (__position._M_node == _M_rightmost())
995 return _M_insert(0, _M_rightmost(), __v);
996 else if (!_M_impl._M_key_compare(_S_key((++__after)._M_node),
997 _KeyOfValue()(__v)))
998 {
999 if (_S_right(__position._M_node) == 0)
1000 return _M_insert(0, __position._M_node, __v);
1001 else
1002 return _M_insert(__after._M_node, __after._M_node, __v);
1003 }
1004 else
1005 return insert_equal(__v);
1006 }
1007 }
1008
1009 template<typename _Key, typename _Val, typename _KoV,
1010 typename _Cmp, typename _Alloc>
1011 template<class _II>
1012 void
1013 _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::
1014 insert_equal(_II __first, _II __last)
1015 {
1016 for (; __first != __last; ++__first)
1017 insert_equal(end(), *__first);
1018 }
1019
1020 template<typename _Key, typename _Val, typename _KoV,
1021 typename _Cmp, typename _Alloc>
1022 template<class _II>
1023 void
1024 _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::
1025 insert_unique(_II __first, _II __last)
1026 {
1027 for (; __first != __last; ++__first)
1028 insert_unique(end(), *__first);
1029 }
1030
1031 template<typename _Key, typename _Val, typename _KeyOfValue,
1032 typename _Compare, typename _Alloc>
1033 inline void
1034 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1035 erase(iterator __position)
1036 {
1037 _Link_type __y =
1038 static_cast<_Link_type>(_Rb_tree_rebalance_for_erase
1039 (__position._M_node,
1040 this->_M_impl._M_header));
1041 destroy_node(__y);
1042 --_M_impl._M_node_count;
1043 }
1044
1045 template<typename _Key, typename _Val, typename _KeyOfValue,
1046 typename _Compare, typename _Alloc>
1047 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
1048 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1049 erase(const _Key& __x)
1050 {
1051 pair<iterator,iterator> __p = equal_range(__x);
1052 size_type __n = std::distance(__p.first, __p.second);
1053 erase(__p.first, __p.second);
1054 return __n;
1055 }
1056
1057 template<typename _Key, typename _Val, typename _KoV,
1058 typename _Compare, typename _Alloc>
1059 typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Link_type
1060 _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
1061 _M_copy(_Const_Link_type __x, _Link_type __p)
1062 {
1063 // Structural copy. __x and __p must be non-null.
1064 _Link_type __top = _M_clone_node(__x);
1065 __top->_M_parent = __p;
1066
1067 try
1068 {
1069 if (__x->_M_right)
1070 __top->_M_right = _M_copy(_S_right(__x), __top);
1071 __p = __top;
1072 __x = _S_left(__x);
1073
1074 while (__x != 0)
1075 {
1076 _Link_type __y = _M_clone_node(__x);
1077 __p->_M_left = __y;
1078 __y->_M_parent = __p;
1079 if (__x->_M_right)
1080 __y->_M_right = _M_copy(_S_right(__x), __y);
1081 __p = __y;
1082 __x = _S_left(__x);
1083 }
1084 }
1085 catch(...)
1086 {
1087 _M_erase(__top);
1088 __throw_exception_again;
1089 }
1090 return __top;
1091 }
1092
1093 template<typename _Key, typename _Val, typename _KeyOfValue,
1094 typename _Compare, typename _Alloc>
1095 void
1096 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1097 _M_erase(_Link_type __x)
1098 {
1099 // Erase without rebalancing.
1100 while (__x != 0)
1101 {
1102 _M_erase(_S_right(__x));
1103 _Link_type __y = _S_left(__x);
1104 destroy_node(__x);
1105 __x = __y;
1106 }
1107 }
1108
1109 template<typename _Key, typename _Val, typename _KeyOfValue,
1110 typename _Compare, typename _Alloc>
1111 void
1112 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1113 erase(iterator __first, iterator __last)
1114 {
1115 if (__first == begin() && __last == end())
1116 clear();
1117 else
1118 while (__first != __last)
1119 erase(__first++);
1120 }
1121
1122 template<typename _Key, typename _Val, typename _KeyOfValue,
1123 typename _Compare, typename _Alloc>
1124 void
1125 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1126 erase(const _Key* __first, const _Key* __last)
1127 {
1128 while (__first != __last)
1129 erase(*__first++);
1130 }
1131
1132 template<typename _Key, typename _Val, typename _KeyOfValue,
1133 typename _Compare, typename _Alloc>
1134 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1135 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1136 find(const _Key& __k)
1137 {
1138 _Link_type __x = _M_begin(); // Current node.
1139 _Link_type __y = _M_end(); // Last node which is not less than __k.
1140
1141 while (__x != 0)
1142 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1143 __y = __x, __x = _S_left(__x);
1144 else
1145 __x = _S_right(__x);
1146
1147 iterator __j = iterator(__y);
1148 return (__j == end()
1149 || _M_impl._M_key_compare(__k,
1150 _S_key(__j._M_node))) ? end() : __j;
1151 }
1152
1153 template<typename _Key, typename _Val, typename _KeyOfValue,
1154 typename _Compare, typename _Alloc>
1155 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator
1156 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1157 find(const _Key& __k) const
1158 {
1159 _Const_Link_type __x = _M_begin(); // Current node.
1160 _Const_Link_type __y = _M_end(); // Last node which is not less than __k.
1161
1162 while (__x != 0)
1163 {
1164 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1165 __y = __x, __x = _S_left(__x);
1166 else
1167 __x = _S_right(__x);
1168 }
1169 const_iterator __j = const_iterator(__y);
1170 return (__j == end()
1171 || _M_impl._M_key_compare(__k,
1172 _S_key(__j._M_node))) ? end() : __j;
1173 }
1174
1175 template<typename _Key, typename _Val, typename _KeyOfValue,
1176 typename _Compare, typename _Alloc>
1177 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
1178 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1179 count(const _Key& __k) const
1180 {
1181 pair<const_iterator, const_iterator> __p = equal_range(__k);
1182 const size_type __n = std::distance(__p.first, __p.second);
1183 return __n;
1184 }
1185
1186 template<typename _Key, typename _Val, typename _KeyOfValue,
1187 typename _Compare, typename _Alloc>
1188 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1189 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1190 lower_bound(const _Key& __k)
1191 {
1192 _Link_type __x = _M_begin(); // Current node.
1193 _Link_type __y = _M_end(); // Last node which is not less than __k.
1194
1195 while (__x != 0)
1196 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1197 __y = __x, __x = _S_left(__x);
1198 else
1199 __x = _S_right(__x);
1200
1201 return iterator(__y);
1202 }
1203
1204 template<typename _Key, typename _Val, typename _KeyOfValue,
1205 typename _Compare, typename _Alloc>
1206 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator
1207 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1208 lower_bound(const _Key& __k) const
1209 {
1210 _Const_Link_type __x = _M_begin(); // Current node.
1211 _Const_Link_type __y = _M_end(); // Last node which is not less than __k.
1212
1213 while (__x != 0)
1214 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1215 __y = __x, __x = _S_left(__x);
1216 else
1217 __x = _S_right(__x);
1218
1219 return const_iterator(__y);
1220 }
1221
1222 template<typename _Key, typename _Val, typename _KeyOfValue,
1223 typename _Compare, typename _Alloc>
1224 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1225 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1226 upper_bound(const _Key& __k)
1227 {
1228 _Link_type __x = _M_begin(); // Current node.
1229 _Link_type __y = _M_end(); // Last node which is greater than __k.
1230
1231 while (__x != 0)
1232 if (_M_impl._M_key_compare(__k, _S_key(__x)))
1233 __y = __x, __x = _S_left(__x);
1234 else
1235 __x = _S_right(__x);
1236
1237 return iterator(__y);
1238 }
1239
1240 template<typename _Key, typename _Val, typename _KeyOfValue,
1241 typename _Compare, typename _Alloc>
1242 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator
1243 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1244 upper_bound(const _Key& __k) const
1245 {
1246 _Const_Link_type __x = _M_begin(); // Current node.
1247 _Const_Link_type __y = _M_end(); // Last node which is greater than __k.
1248
1249 while (__x != 0)
1250 if (_M_impl._M_key_compare(__k, _S_key(__x)))
1251 __y = __x, __x = _S_left(__x);
1252 else
1253 __x = _S_right(__x);
1254
1255 return const_iterator(__y);
1256 }
1257
1258 template<typename _Key, typename _Val, typename _KeyOfValue,
1259 typename _Compare, typename _Alloc>
1260 inline
1261 pair<typename _Rb_tree<_Key, _Val, _KeyOfValue,
1262 _Compare, _Alloc>::iterator,
1263 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator>
1264 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1265 equal_range(const _Key& __k)
1266 { return pair<iterator, iterator>(lower_bound(__k), upper_bound(__k)); }
1267
1268 template<typename _Key, typename _Val, typename _KoV,
1269 typename _Compare, typename _Alloc>
1270 inline
1271 pair<typename _Rb_tree<_Key, _Val, _KoV,
1272 _Compare, _Alloc>::const_iterator,
1273 typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::const_iterator>
1274 _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
1275 equal_range(const _Key& __k) const
1276 { return pair<const_iterator, const_iterator>(lower_bound(__k),
1277 upper_bound(__k)); }
1278
1279 unsigned int
1280 _Rb_tree_black_count(const _Rb_tree_node_base* __node,
1281 const _Rb_tree_node_base* __root);
1282
1283 template<typename _Key, typename _Val, typename _KeyOfValue,
1284 typename _Compare, typename _Alloc>
1285 bool
1286 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const
1287 {
1288 if (_M_impl._M_node_count == 0 || begin() == end())
1289 return _M_impl._M_node_count == 0 && begin() == end()
1290 && this->_M_impl._M_header._M_left == _M_end()
1291 && this->_M_impl._M_header._M_right == _M_end();
1292
1293 unsigned int __len = _Rb_tree_black_count(_M_leftmost(), _M_root());
1294 for (const_iterator __it = begin(); __it != end(); ++__it)
1295 {
1296 _Const_Link_type __x = static_cast<_Const_Link_type>(__it._M_node);
1297 _Const_Link_type __L = _S_left(__x);
1298 _Const_Link_type __R = _S_right(__x);
1299
1300 if (__x->_M_color == _S_red)
1301 if ((__L && __L->_M_color == _S_red)
1302 || (__R && __R->_M_color == _S_red))
1303 return false;
1304
1305 if (__L && _M_impl._M_key_compare(_S_key(__x), _S_key(__L)))
1306 return false;
1307 if (__R && _M_impl._M_key_compare(_S_key(__R), _S_key(__x)))
1308 return false;
1309
1310 if (!__L && !__R && _Rb_tree_black_count(__x, _M_root()) != __len)
1311 return false;
1312 }
1313
1314 if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
1315 return false;
1316 if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
1317 return false;
1318 return true;
1319 }
1320 } // namespace std
1321
1322 #endif