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