williamr@4
|
1 |
/*
|
williamr@4
|
2 |
*
|
williamr@4
|
3 |
* Copyright (c) 1994
|
williamr@4
|
4 |
* Hewlett-Packard Company
|
williamr@4
|
5 |
*
|
williamr@4
|
6 |
* Copyright (c) 1996,1997
|
williamr@4
|
7 |
* Silicon Graphics Computer Systems, Inc.
|
williamr@4
|
8 |
*
|
williamr@4
|
9 |
* Copyright (c) 1997
|
williamr@4
|
10 |
* Moscow Center for SPARC Technology
|
williamr@4
|
11 |
*
|
williamr@4
|
12 |
* Copyright (c) 1999
|
williamr@4
|
13 |
* Boris Fomitchev
|
williamr@4
|
14 |
*
|
williamr@4
|
15 |
* This material is provided "as is", with absolutely no warranty expressed
|
williamr@4
|
16 |
* or implied. Any use is at your own risk.
|
williamr@4
|
17 |
*
|
williamr@4
|
18 |
* Permission to use or copy this software for any purpose is hereby granted
|
williamr@4
|
19 |
* without fee, provided the above notices are retained on all copies.
|
williamr@4
|
20 |
* Permission to modify the code and to distribute modified code is granted,
|
williamr@4
|
21 |
* provided the above notices are retained, and a notice that the code was
|
williamr@4
|
22 |
* modified is included with the above copyright notice.
|
williamr@4
|
23 |
*
|
williamr@4
|
24 |
*/
|
williamr@4
|
25 |
|
williamr@4
|
26 |
/* NOTE: This is an internal header file, included by other STL headers.
|
williamr@4
|
27 |
* You should not attempt to use it directly.
|
williamr@4
|
28 |
*/
|
williamr@4
|
29 |
|
williamr@4
|
30 |
#ifndef _STLP_INTERNAL_TREE_H
|
williamr@4
|
31 |
#define _STLP_INTERNAL_TREE_H
|
williamr@4
|
32 |
|
williamr@4
|
33 |
/*
|
williamr@4
|
34 |
|
williamr@4
|
35 |
Red-black tree class, designed for use in implementing STL
|
williamr@4
|
36 |
associative containers (set, multiset, map, and multimap). The
|
williamr@4
|
37 |
insertion and deletion algorithms are based on those in Cormen,
|
williamr@4
|
38 |
Leiserson, and Rivest, Introduction to Algorithms (MIT Press, 1990),
|
williamr@4
|
39 |
except that
|
williamr@4
|
40 |
|
williamr@4
|
41 |
(1) the header cell is maintained with links not only to the root
|
williamr@4
|
42 |
but also to the leftmost node of the tree, to enable constant time
|
williamr@4
|
43 |
begin(), and to the rightmost node of the tree, to enable linear time
|
williamr@4
|
44 |
performance when used with the generic set algorithms (set_union,
|
williamr@4
|
45 |
etc.);
|
williamr@4
|
46 |
|
williamr@4
|
47 |
(2) when a node being deleted has two children its successor node is
|
williamr@4
|
48 |
relinked into its place, rather than copied, so that the only
|
williamr@4
|
49 |
iterators invalidated are those referring to the deleted node.
|
williamr@4
|
50 |
|
williamr@4
|
51 |
*/
|
williamr@4
|
52 |
|
williamr@4
|
53 |
#ifndef _STLP_INTERNAL_ALGOBASE_H
|
williamr@4
|
54 |
# include <stl/_algobase.h>
|
williamr@4
|
55 |
#endif
|
williamr@4
|
56 |
|
williamr@4
|
57 |
#ifndef _STLP_INTERNAL_ALLOC_H
|
williamr@4
|
58 |
# include <stl/_alloc.h>
|
williamr@4
|
59 |
#endif
|
williamr@4
|
60 |
|
williamr@4
|
61 |
#ifndef _STLP_INTERNAL_ITERATOR_H
|
williamr@4
|
62 |
# include <stl/_iterator.h>
|
williamr@4
|
63 |
#endif
|
williamr@4
|
64 |
|
williamr@4
|
65 |
#ifndef _STLP_INTERNAL_CONSTRUCT_H
|
williamr@4
|
66 |
# include <stl/_construct.h>
|
williamr@4
|
67 |
#endif
|
williamr@4
|
68 |
|
williamr@4
|
69 |
#ifndef _STLP_INTERNAL_FUNCTION_BASE_H
|
williamr@4
|
70 |
# include <stl/_function_base.h>
|
williamr@4
|
71 |
#endif
|
williamr@4
|
72 |
|
williamr@4
|
73 |
_STLP_BEGIN_NAMESPACE
|
williamr@4
|
74 |
|
williamr@4
|
75 |
_STLP_MOVE_TO_PRIV_NAMESPACE
|
williamr@4
|
76 |
|
williamr@4
|
77 |
typedef bool _Rb_tree_Color_type;
|
williamr@4
|
78 |
//const _Rb_tree_Color_type _S_rb_tree_red = false;
|
williamr@4
|
79 |
//const _Rb_tree_Color_type _S_rb_tree_black = true;
|
williamr@4
|
80 |
|
williamr@4
|
81 |
#define _S_rb_tree_red false
|
williamr@4
|
82 |
#define _S_rb_tree_black true
|
williamr@4
|
83 |
|
williamr@4
|
84 |
struct _Rb_tree_node_base {
|
williamr@4
|
85 |
typedef _Rb_tree_Color_type _Color_type;
|
williamr@4
|
86 |
typedef _Rb_tree_node_base* _Base_ptr;
|
williamr@4
|
87 |
|
williamr@4
|
88 |
_Color_type _M_color;
|
williamr@4
|
89 |
_Base_ptr _M_parent;
|
williamr@4
|
90 |
_Base_ptr _M_left;
|
williamr@4
|
91 |
_Base_ptr _M_right;
|
williamr@4
|
92 |
|
williamr@4
|
93 |
static _Base_ptr _STLP_CALL _S_minimum(_Base_ptr __x) {
|
williamr@4
|
94 |
while (__x->_M_left != 0) __x = __x->_M_left;
|
williamr@4
|
95 |
return __x;
|
williamr@4
|
96 |
}
|
williamr@4
|
97 |
|
williamr@4
|
98 |
static _Base_ptr _STLP_CALL _S_maximum(_Base_ptr __x) {
|
williamr@4
|
99 |
while (__x->_M_right != 0) __x = __x->_M_right;
|
williamr@4
|
100 |
return __x;
|
williamr@4
|
101 |
}
|
williamr@4
|
102 |
};
|
williamr@4
|
103 |
|
williamr@4
|
104 |
template <class _Value>
|
williamr@4
|
105 |
struct _Rb_tree_node : public _Rb_tree_node_base {
|
williamr@4
|
106 |
_Value _M_value_field;
|
williamr@4
|
107 |
__TRIVIAL_STUFF(_Rb_tree_node)
|
williamr@4
|
108 |
};
|
williamr@4
|
109 |
|
williamr@4
|
110 |
struct _Rb_tree_base_iterator;
|
williamr@4
|
111 |
|
williamr@4
|
112 |
template <class _Dummy>
|
williamr@4
|
113 |
class _Rb_global {
|
williamr@4
|
114 |
public:
|
williamr@4
|
115 |
typedef _Rb_tree_node_base* _Base_ptr;
|
williamr@4
|
116 |
// those used to be global functions
|
williamr@4
|
117 |
static void _STLP_CALL _Rebalance(_Base_ptr __x, _Base_ptr& __root);
|
williamr@4
|
118 |
static _Base_ptr _STLP_CALL _Rebalance_for_erase(_Base_ptr __z,
|
williamr@4
|
119 |
_Base_ptr& __root,
|
williamr@4
|
120 |
_Base_ptr& __leftmost,
|
williamr@4
|
121 |
_Base_ptr& __rightmost);
|
williamr@4
|
122 |
// those are from _Rb_tree_base_iterator - moved here to reduce code bloat
|
williamr@4
|
123 |
// moved here to reduce code bloat without templatizing _Rb_tree_base_iterator
|
williamr@4
|
124 |
static _Base_ptr _STLP_CALL _M_increment (_Base_ptr);
|
williamr@4
|
125 |
static _Base_ptr _STLP_CALL _M_decrement (_Base_ptr);
|
williamr@4
|
126 |
static void _STLP_CALL _Rotate_left (_Base_ptr __x, _Base_ptr& __root);
|
williamr@4
|
127 |
static void _STLP_CALL _Rotate_right(_Base_ptr __x, _Base_ptr& __root);
|
williamr@4
|
128 |
};
|
williamr@4
|
129 |
|
williamr@4
|
130 |
# if defined (_STLP_USE_TEMPLATE_EXPORT)
|
williamr@4
|
131 |
_STLP_EXPORT_TEMPLATE_CLASS _Rb_global<bool>;
|
williamr@4
|
132 |
# endif
|
williamr@4
|
133 |
|
williamr@4
|
134 |
typedef _Rb_global<bool> _Rb_global_inst;
|
williamr@4
|
135 |
|
williamr@4
|
136 |
struct _Rb_tree_base_iterator {
|
williamr@4
|
137 |
typedef _Rb_tree_node_base* _Base_ptr;
|
williamr@4
|
138 |
typedef bidirectional_iterator_tag iterator_category;
|
williamr@4
|
139 |
typedef ptrdiff_t difference_type;
|
williamr@4
|
140 |
_Base_ptr _M_node;
|
williamr@4
|
141 |
_Rb_tree_base_iterator() : _M_node(0) {}
|
williamr@4
|
142 |
_Rb_tree_base_iterator(_Base_ptr __x) : _M_node(__x) {}
|
williamr@4
|
143 |
};
|
williamr@4
|
144 |
|
williamr@4
|
145 |
template <class _Value, class _Traits>
|
williamr@4
|
146 |
struct _Rb_tree_iterator : public _Rb_tree_base_iterator {
|
williamr@4
|
147 |
typedef _Value value_type;
|
williamr@4
|
148 |
typedef typename _Traits::reference reference;
|
williamr@4
|
149 |
typedef typename _Traits::pointer pointer;
|
williamr@4
|
150 |
typedef _Rb_tree_iterator<_Value, _Traits> _Self;
|
williamr@4
|
151 |
typedef _Rb_tree_node_base* _Base_ptr;
|
williamr@4
|
152 |
typedef _Rb_tree_node<_Value>* _Link_type;
|
williamr@4
|
153 |
|
williamr@4
|
154 |
typedef typename _Traits::_NonConstTraits _NonConstTraits;
|
williamr@4
|
155 |
typedef _Rb_tree_iterator<_Value, _NonConstTraits> iterator;
|
williamr@4
|
156 |
typedef typename _Traits::_ConstTraits _ConstTraits;
|
williamr@4
|
157 |
typedef _Rb_tree_iterator<_Value, _ConstTraits> const_iterator;
|
williamr@4
|
158 |
|
williamr@4
|
159 |
_Rb_tree_iterator() {}
|
williamr@4
|
160 |
#if !defined (_STLP_DEBUG)
|
williamr@4
|
161 |
/* In STL debug mode we need this constructor implicit for the pointer
|
williamr@4
|
162 |
* specialization implementation.
|
williamr@4
|
163 |
*/
|
williamr@4
|
164 |
explicit
|
williamr@4
|
165 |
#endif
|
williamr@4
|
166 |
_Rb_tree_iterator(_Base_ptr __x) : _Rb_tree_base_iterator(__x) {}
|
williamr@4
|
167 |
//copy constructor for iterator and constructor from iterator for const_iterator
|
williamr@4
|
168 |
_Rb_tree_iterator(const iterator& __it) : _Rb_tree_base_iterator(__it._M_node) {}
|
williamr@4
|
169 |
|
williamr@4
|
170 |
reference operator*() const {
|
williamr@4
|
171 |
return __STATIC_CAST(_Link_type, _M_node)->_M_value_field;
|
williamr@4
|
172 |
}
|
williamr@4
|
173 |
|
williamr@4
|
174 |
_STLP_DEFINE_ARROW_OPERATOR
|
williamr@4
|
175 |
|
williamr@4
|
176 |
_Self& operator++() {
|
williamr@4
|
177 |
_M_node = _Rb_global_inst::_M_increment(_M_node);
|
williamr@4
|
178 |
return *this;
|
williamr@4
|
179 |
}
|
williamr@4
|
180 |
_Self operator++(int) {
|
williamr@4
|
181 |
_Self __tmp = *this;
|
williamr@4
|
182 |
++(*this);
|
williamr@4
|
183 |
return __tmp;
|
williamr@4
|
184 |
}
|
williamr@4
|
185 |
|
williamr@4
|
186 |
_Self& operator--() {
|
williamr@4
|
187 |
_M_node = _Rb_global_inst::_M_decrement(_M_node);
|
williamr@4
|
188 |
return *this;
|
williamr@4
|
189 |
}
|
williamr@4
|
190 |
_Self operator--(int) {
|
williamr@4
|
191 |
_Self __tmp = *this;
|
williamr@4
|
192 |
--(*this);
|
williamr@4
|
193 |
return __tmp;
|
williamr@4
|
194 |
}
|
williamr@4
|
195 |
|
williamr@4
|
196 |
bool operator == (const_iterator __rhs) const {
|
williamr@4
|
197 |
return _M_node == __rhs._M_node;
|
williamr@4
|
198 |
}
|
williamr@4
|
199 |
bool operator != (const_iterator __rhs) const {
|
williamr@4
|
200 |
return _M_node != __rhs._M_node;
|
williamr@4
|
201 |
}
|
williamr@4
|
202 |
};
|
williamr@4
|
203 |
|
williamr@4
|
204 |
#if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
|
williamr@4
|
205 |
_STLP_MOVE_TO_STD_NAMESPACE
|
williamr@4
|
206 |
template <class _Value, class _Traits>
|
williamr@4
|
207 |
struct __type_traits<_STLP_PRIV _Rb_tree_iterator<_Value, _Traits> > {
|
williamr@4
|
208 |
typedef __false_type has_trivial_default_constructor;
|
williamr@4
|
209 |
typedef __true_type has_trivial_copy_constructor;
|
williamr@4
|
210 |
typedef __true_type has_trivial_assignment_operator;
|
williamr@4
|
211 |
typedef __true_type has_trivial_destructor;
|
williamr@4
|
212 |
typedef __false_type is_POD_type;
|
williamr@4
|
213 |
};
|
williamr@4
|
214 |
_STLP_MOVE_TO_PRIV_NAMESPACE
|
williamr@4
|
215 |
#endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
|
williamr@4
|
216 |
|
williamr@4
|
217 |
#if defined (_STLP_USE_OLD_HP_ITERATOR_QUERIES)
|
williamr@4
|
218 |
_STLP_MOVE_TO_STD_NAMESPACE
|
williamr@4
|
219 |
template <class _Value, class _Traits>
|
williamr@4
|
220 |
inline _Value* value_type(const _STLP_PRIV _Rb_tree_iterator<_Value, _Traits>&)
|
williamr@4
|
221 |
{ return (_Value*)0; }
|
williamr@4
|
222 |
inline bidirectional_iterator_tag iterator_category(const _STLP_PRIV _Rb_tree_base_iterator&)
|
williamr@4
|
223 |
{ return bidirectional_iterator_tag(); }
|
williamr@4
|
224 |
inline ptrdiff_t* distance_type(const _STLP_PRIV _Rb_tree_base_iterator&)
|
williamr@4
|
225 |
{ return (ptrdiff_t*) 0; }
|
williamr@4
|
226 |
_STLP_MOVE_TO_PRIV_NAMESPACE
|
williamr@4
|
227 |
#endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
|
williamr@4
|
228 |
|
williamr@4
|
229 |
// Base class to help EH
|
williamr@4
|
230 |
|
williamr@4
|
231 |
template <class _Tp, class _Alloc>
|
williamr@4
|
232 |
class _Rb_tree_base {
|
williamr@4
|
233 |
public:
|
williamr@4
|
234 |
typedef _Rb_tree_node_base _Node_base;
|
williamr@4
|
235 |
typedef _Rb_tree_node<_Tp> _Node;
|
williamr@4
|
236 |
_STLP_FORCE_ALLOCATORS(_Tp, _Alloc)
|
williamr@4
|
237 |
typedef typename _Alloc_traits<_Tp, _Alloc>::allocator_type allocator_type;
|
williamr@4
|
238 |
private:
|
williamr@4
|
239 |
typedef _Rb_tree_base<_Tp, _Alloc> _Self;
|
williamr@4
|
240 |
typedef typename _Alloc_traits<_Node, _Alloc>::allocator_type _M_node_allocator_type;
|
williamr@4
|
241 |
typedef _STLP_alloc_proxy<_Node_base, _Node, _M_node_allocator_type> _AllocProxy;
|
williamr@4
|
242 |
|
williamr@4
|
243 |
public:
|
williamr@4
|
244 |
allocator_type get_allocator() const {
|
williamr@4
|
245 |
return _STLP_CONVERT_ALLOCATOR(_M_header, _Tp);
|
williamr@4
|
246 |
}
|
williamr@4
|
247 |
|
williamr@4
|
248 |
protected:
|
williamr@4
|
249 |
_Rb_tree_base(const allocator_type& __a) :
|
williamr@4
|
250 |
_M_header(_STLP_CONVERT_ALLOCATOR(__a, _Node), _Node_base() ) {
|
williamr@4
|
251 |
_M_empty_initialize();
|
williamr@4
|
252 |
}
|
williamr@4
|
253 |
_Rb_tree_base(__move_source<_Self> src) :
|
williamr@4
|
254 |
_M_header(__move_source<_AllocProxy>(src.get()._M_header)) {
|
williamr@4
|
255 |
_M_rebind(&src.get()._M_header._M_data);
|
williamr@4
|
256 |
src.get()._M_empty_initialize();
|
williamr@4
|
257 |
}
|
williamr@4
|
258 |
void _M_empty_initialize() {
|
williamr@4
|
259 |
_M_header._M_data._M_color = _S_rb_tree_red; // used to distinguish header from
|
williamr@4
|
260 |
// __root, in iterator.operator++
|
williamr@4
|
261 |
_M_header._M_data._M_parent = 0;
|
williamr@4
|
262 |
_M_header._M_data._M_left = &_M_header._M_data;
|
williamr@4
|
263 |
_M_header._M_data._M_right = &_M_header._M_data;
|
williamr@4
|
264 |
}
|
williamr@4
|
265 |
|
williamr@4
|
266 |
void _M_rebind(_Node_base *__static_node) {
|
williamr@4
|
267 |
if (_M_header._M_data._M_parent != 0) {
|
williamr@4
|
268 |
_M_header._M_data._M_parent->_M_parent = &_M_header._M_data;
|
williamr@4
|
269 |
}
|
williamr@4
|
270 |
if (_M_header._M_data._M_right == __static_node) {
|
williamr@4
|
271 |
_M_header._M_data._M_right = &_M_header._M_data;
|
williamr@4
|
272 |
}
|
williamr@4
|
273 |
if (_M_header._M_data._M_left == __static_node) {
|
williamr@4
|
274 |
_M_header._M_data._M_left = &_M_header._M_data;
|
williamr@4
|
275 |
}
|
williamr@4
|
276 |
}
|
williamr@4
|
277 |
|
williamr@4
|
278 |
_AllocProxy _M_header;
|
williamr@4
|
279 |
};
|
williamr@4
|
280 |
|
williamr@4
|
281 |
#if defined (_STLP_DEBUG)
|
williamr@4
|
282 |
# define _Rb_tree _STLP_NON_DBG_NAME(Rb_tree)
|
williamr@4
|
283 |
#endif
|
williamr@4
|
284 |
|
williamr@4
|
285 |
template <class _Key, class _Compare,
|
williamr@4
|
286 |
class _Value, class _KeyOfValue, class _Traits,
|
williamr@4
|
287 |
_STLP_DEFAULT_ALLOCATOR_SELECT(_Value) >
|
williamr@4
|
288 |
class _Rb_tree : public _Rb_tree_base<_Value, _Alloc> {
|
williamr@4
|
289 |
typedef _Rb_tree_base<_Value, _Alloc> _Base;
|
williamr@4
|
290 |
typedef _Rb_tree<_Key, _Compare, _Value, _KeyOfValue, _Traits, _Alloc> _Self;
|
williamr@4
|
291 |
protected:
|
williamr@4
|
292 |
typedef _Rb_tree_node_base * _Base_ptr;
|
williamr@4
|
293 |
typedef _Rb_tree_node<_Value> _Node;
|
williamr@4
|
294 |
typedef _Node* _Link_type;
|
williamr@4
|
295 |
typedef _Rb_tree_Color_type _Color_type;
|
williamr@4
|
296 |
public:
|
williamr@4
|
297 |
typedef _Key key_type;
|
williamr@4
|
298 |
typedef _Value value_type;
|
williamr@4
|
299 |
typedef typename _Traits::pointer pointer;
|
williamr@4
|
300 |
typedef const value_type* const_pointer;
|
williamr@4
|
301 |
typedef typename _Traits::reference reference;
|
williamr@4
|
302 |
typedef const value_type& const_reference;
|
williamr@4
|
303 |
typedef size_t size_type;
|
williamr@4
|
304 |
typedef ptrdiff_t difference_type;
|
williamr@4
|
305 |
typedef bidirectional_iterator_tag _Iterator_category;
|
williamr@4
|
306 |
typedef typename _Base::allocator_type allocator_type;
|
williamr@4
|
307 |
|
williamr@4
|
308 |
protected:
|
williamr@4
|
309 |
|
williamr@4
|
310 |
_STLP_KEY_TYPE_FOR_CONT_EXT(key_type)
|
williamr@4
|
311 |
_Base_ptr _M_create_node(const value_type& __x) {
|
williamr@4
|
312 |
_Link_type __tmp = this->_M_header.allocate(1);
|
williamr@4
|
313 |
_STLP_TRY {
|
williamr@4
|
314 |
_Copy_Construct(&__tmp->_M_value_field, __x);
|
williamr@4
|
315 |
}
|
williamr@4
|
316 |
_STLP_UNWIND(this->_M_header.deallocate(__tmp,1))
|
williamr@4
|
317 |
_S_left(__tmp) = 0;
|
williamr@4
|
318 |
_S_right(__tmp) = 0;
|
williamr@4
|
319 |
return __tmp;
|
williamr@4
|
320 |
}
|
williamr@4
|
321 |
|
williamr@4
|
322 |
_Base_ptr _M_clone_node(_Base_ptr __x) {
|
williamr@4
|
323 |
_Base_ptr __tmp = _M_create_node(_S_value(__x));
|
williamr@4
|
324 |
_S_color(__tmp) = _S_color(__x);
|
williamr@4
|
325 |
return __tmp;
|
williamr@4
|
326 |
}
|
williamr@4
|
327 |
|
williamr@4
|
328 |
size_type _M_node_count; // keeps track of size of tree
|
williamr@4
|
329 |
_Compare _M_key_compare;
|
williamr@4
|
330 |
|
williamr@4
|
331 |
_Base_ptr _M_root() const
|
williamr@4
|
332 |
{ return this->_M_header._M_data._M_parent; }
|
williamr@4
|
333 |
_Base_ptr _M_leftmost() const
|
williamr@4
|
334 |
{ return this->_M_header._M_data._M_left; }
|
williamr@4
|
335 |
_Base_ptr _M_rightmost() const
|
williamr@4
|
336 |
{ return this->_M_header._M_data._M_right; }
|
williamr@4
|
337 |
|
williamr@4
|
338 |
_Base_ptr& _M_root()
|
williamr@4
|
339 |
{ return this->_M_header._M_data._M_parent; }
|
williamr@4
|
340 |
_Base_ptr& _M_leftmost()
|
williamr@4
|
341 |
{ return this->_M_header._M_data._M_left; }
|
williamr@4
|
342 |
_Base_ptr& _M_rightmost()
|
williamr@4
|
343 |
{ return this->_M_header._M_data._M_right; }
|
williamr@4
|
344 |
|
williamr@4
|
345 |
static _Base_ptr& _STLP_CALL _S_left(_Base_ptr __x)
|
williamr@4
|
346 |
{ return __x->_M_left; }
|
williamr@4
|
347 |
static _Base_ptr& _STLP_CALL _S_right(_Base_ptr __x)
|
williamr@4
|
348 |
{ return __x->_M_right; }
|
williamr@4
|
349 |
static _Base_ptr& _STLP_CALL _S_parent(_Base_ptr __x)
|
williamr@4
|
350 |
{ return __x->_M_parent; }
|
williamr@4
|
351 |
static value_type& _STLP_CALL _S_value(_Base_ptr __x)
|
williamr@4
|
352 |
{ return __STATIC_CAST(_Link_type, __x)->_M_value_field; }
|
williamr@4
|
353 |
static const _Key& _STLP_CALL _S_key(_Base_ptr __x)
|
williamr@4
|
354 |
{ return _KeyOfValue()(_S_value(__x));}
|
williamr@4
|
355 |
static _Color_type& _STLP_CALL _S_color(_Base_ptr __x)
|
williamr@4
|
356 |
{ return (_Color_type&)(__x->_M_color); }
|
williamr@4
|
357 |
|
williamr@4
|
358 |
static _Base_ptr _STLP_CALL _S_minimum(_Base_ptr __x)
|
williamr@4
|
359 |
{ return _Rb_tree_node_base::_S_minimum(__x); }
|
williamr@4
|
360 |
|
williamr@4
|
361 |
static _Base_ptr _STLP_CALL _S_maximum(_Base_ptr __x)
|
williamr@4
|
362 |
{ return _Rb_tree_node_base::_S_maximum(__x); }
|
williamr@4
|
363 |
|
williamr@4
|
364 |
public:
|
williamr@4
|
365 |
typedef typename _Traits::_NonConstTraits _NonConstTraits;
|
williamr@4
|
366 |
typedef typename _Traits::_ConstTraits _ConstTraits;
|
williamr@4
|
367 |
typedef _Rb_tree_iterator<value_type, _NonConstTraits> iterator;
|
williamr@4
|
368 |
typedef _Rb_tree_iterator<value_type, _ConstTraits> const_iterator;
|
williamr@4
|
369 |
_STLP_DECLARE_BIDIRECTIONAL_REVERSE_ITERATORS;
|
williamr@4
|
370 |
|
williamr@4
|
371 |
private:
|
williamr@4
|
372 |
iterator _M_insert(_Base_ptr __parent, const value_type& __val, _Base_ptr __on_left = 0, _Base_ptr __on_right = 0);
|
williamr@4
|
373 |
_Base_ptr _M_copy(_Base_ptr __x, _Base_ptr __p);
|
williamr@4
|
374 |
void _M_erase(_Base_ptr __x);
|
williamr@4
|
375 |
|
williamr@4
|
376 |
public:
|
williamr@4
|
377 |
// allocation/deallocation
|
williamr@4
|
378 |
_Rb_tree()
|
williamr@4
|
379 |
: _Rb_tree_base<_Value, _Alloc>(allocator_type()), _M_node_count(0), _M_key_compare(_Compare())
|
williamr@4
|
380 |
{}
|
williamr@4
|
381 |
|
williamr@4
|
382 |
_Rb_tree(const _Compare& __comp)
|
williamr@4
|
383 |
: _Rb_tree_base<_Value, _Alloc>(allocator_type()), _M_node_count(0), _M_key_compare(__comp)
|
williamr@4
|
384 |
{}
|
williamr@4
|
385 |
|
williamr@4
|
386 |
_Rb_tree(const _Compare& __comp, const allocator_type& __a)
|
williamr@4
|
387 |
: _Rb_tree_base<_Value, _Alloc>(__a), _M_node_count(0), _M_key_compare(__comp)
|
williamr@4
|
388 |
{}
|
williamr@4
|
389 |
|
williamr@4
|
390 |
_Rb_tree(const _Self& __x)
|
williamr@4
|
391 |
: _Rb_tree_base<_Value, _Alloc>(__x.get_allocator()),
|
williamr@4
|
392 |
_M_node_count(0), _M_key_compare(__x._M_key_compare) {
|
williamr@4
|
393 |
if (__x._M_root() != 0) {
|
williamr@4
|
394 |
_S_color(&this->_M_header._M_data) = _S_rb_tree_red;
|
williamr@4
|
395 |
_M_root() = _M_copy(__x._M_root(), &this->_M_header._M_data);
|
williamr@4
|
396 |
_M_leftmost() = _S_minimum(_M_root());
|
williamr@4
|
397 |
_M_rightmost() = _S_maximum(_M_root());
|
williamr@4
|
398 |
}
|
williamr@4
|
399 |
_M_node_count = __x._M_node_count;
|
williamr@4
|
400 |
}
|
williamr@4
|
401 |
|
williamr@4
|
402 |
_Rb_tree(__move_source<_Self> src)
|
williamr@4
|
403 |
: _Rb_tree_base<_Value, _Alloc>(__move_source<_Base>(src.get())),
|
williamr@4
|
404 |
_M_node_count(src.get()._M_node_count),
|
williamr@4
|
405 |
_M_key_compare(_AsMoveSource(src.get()._M_key_compare)) {
|
williamr@4
|
406 |
src.get()._M_node_count = 0;
|
williamr@4
|
407 |
}
|
williamr@4
|
408 |
|
williamr@4
|
409 |
~_Rb_tree() { clear(); }
|
williamr@4
|
410 |
_Self& operator=(const _Self& __x);
|
williamr@4
|
411 |
|
williamr@4
|
412 |
public:
|
williamr@4
|
413 |
// accessors:
|
williamr@4
|
414 |
_Compare key_comp() const { return _M_key_compare; }
|
williamr@4
|
415 |
|
williamr@4
|
416 |
iterator begin() { return iterator(_M_leftmost()); }
|
williamr@4
|
417 |
const_iterator begin() const { return const_iterator(_M_leftmost()); }
|
williamr@4
|
418 |
iterator end() { return iterator(&this->_M_header._M_data); }
|
williamr@4
|
419 |
const_iterator end() const { return const_iterator(__CONST_CAST(_Base_ptr, &this->_M_header._M_data)); }
|
williamr@4
|
420 |
|
williamr@4
|
421 |
reverse_iterator rbegin() { return reverse_iterator(end()); }
|
williamr@4
|
422 |
const_reverse_iterator rbegin() const
|
williamr@4
|
423 |
{ return const_reverse_iterator(end()); }
|
williamr@4
|
424 |
reverse_iterator rend() { return reverse_iterator(begin()); }
|
williamr@4
|
425 |
const_reverse_iterator rend() const
|
williamr@4
|
426 |
{ return const_reverse_iterator(begin()); }
|
williamr@4
|
427 |
bool empty() const { return _M_node_count == 0; }
|
williamr@4
|
428 |
size_type size() const { return _M_node_count; }
|
williamr@4
|
429 |
size_type max_size() const { return size_type(-1); }
|
williamr@4
|
430 |
|
williamr@4
|
431 |
void swap(_Self& __t) {
|
williamr@4
|
432 |
if (__t.empty()) {
|
williamr@4
|
433 |
if (this->empty()) return;
|
williamr@4
|
434 |
__t._M_header.swap(this->_M_header);
|
williamr@4
|
435 |
__t._M_rebind(&this->_M_header._M_data);
|
williamr@4
|
436 |
this->_M_empty_initialize();
|
williamr@4
|
437 |
}
|
williamr@4
|
438 |
else if (this->empty()) {
|
williamr@4
|
439 |
__t.swap(*this);
|
williamr@4
|
440 |
return;
|
williamr@4
|
441 |
}
|
williamr@4
|
442 |
else {
|
williamr@4
|
443 |
this->_M_header.swap(__t._M_header);
|
williamr@4
|
444 |
this->_M_rebind(&__t._M_header._M_data);
|
williamr@4
|
445 |
__t._M_rebind(&this->_M_header._M_data);
|
williamr@4
|
446 |
}
|
williamr@4
|
447 |
_STLP_STD::swap(_M_node_count, __t._M_node_count);
|
williamr@4
|
448 |
_STLP_STD::swap(_M_key_compare, __t._M_key_compare);
|
williamr@4
|
449 |
}
|
williamr@4
|
450 |
|
williamr@4
|
451 |
public:
|
williamr@4
|
452 |
// insert/erase
|
williamr@4
|
453 |
pair<iterator,bool> insert_unique(const value_type& __x);
|
williamr@4
|
454 |
iterator insert_equal(const value_type& __x);
|
williamr@4
|
455 |
|
williamr@4
|
456 |
iterator insert_unique(iterator __pos, const value_type& __x);
|
williamr@4
|
457 |
iterator insert_equal(iterator __pos, const value_type& __x);
|
williamr@4
|
458 |
|
williamr@4
|
459 |
#if defined (_STLP_MEMBER_TEMPLATES)
|
williamr@4
|
460 |
template<class _II> void insert_equal(_II __first, _II __last) {
|
williamr@4
|
461 |
for ( ; __first != __last; ++__first)
|
williamr@4
|
462 |
insert_equal(*__first);
|
williamr@4
|
463 |
}
|
williamr@4
|
464 |
template<class _II> void insert_unique(_II __first, _II __last) {
|
williamr@4
|
465 |
for ( ; __first != __last; ++__first)
|
williamr@4
|
466 |
insert_unique(*__first);
|
williamr@4
|
467 |
}
|
williamr@4
|
468 |
#else
|
williamr@4
|
469 |
void insert_unique(const_iterator __first, const_iterator __last) {
|
williamr@4
|
470 |
for ( ; __first != __last; ++__first)
|
williamr@4
|
471 |
insert_unique(*__first);
|
williamr@4
|
472 |
}
|
williamr@4
|
473 |
void insert_unique(const value_type* __first, const value_type* __last) {
|
williamr@4
|
474 |
for ( ; __first != __last; ++__first)
|
williamr@4
|
475 |
insert_unique(*__first);
|
williamr@4
|
476 |
}
|
williamr@4
|
477 |
void insert_equal(const_iterator __first, const_iterator __last) {
|
williamr@4
|
478 |
for ( ; __first != __last; ++__first)
|
williamr@4
|
479 |
insert_equal(*__first);
|
williamr@4
|
480 |
}
|
williamr@4
|
481 |
void insert_equal(const value_type* __first, const value_type* __last) {
|
williamr@4
|
482 |
for ( ; __first != __last; ++__first)
|
williamr@4
|
483 |
insert_equal(*__first);
|
williamr@4
|
484 |
}
|
williamr@4
|
485 |
#endif
|
williamr@4
|
486 |
|
williamr@4
|
487 |
void erase(iterator __pos) {
|
williamr@4
|
488 |
_Base_ptr __x = _Rb_global_inst::_Rebalance_for_erase(__pos._M_node,
|
williamr@4
|
489 |
this->_M_header._M_data._M_parent,
|
williamr@4
|
490 |
this->_M_header._M_data._M_left,
|
williamr@4
|
491 |
this->_M_header._M_data._M_right);
|
williamr@4
|
492 |
_STLP_STD::_Destroy(&_S_value(__x));
|
williamr@4
|
493 |
this->_M_header.deallocate(__STATIC_CAST(_Link_type, __x), 1);
|
williamr@4
|
494 |
--_M_node_count;
|
williamr@4
|
495 |
}
|
williamr@4
|
496 |
|
williamr@4
|
497 |
size_type erase(const key_type& __x) {
|
williamr@4
|
498 |
pair<iterator,iterator> __p = equal_range(__x);
|
williamr@4
|
499 |
size_type __n = distance(__p.first, __p.second);
|
williamr@4
|
500 |
erase(__p.first, __p.second);
|
williamr@4
|
501 |
return __n;
|
williamr@4
|
502 |
}
|
williamr@4
|
503 |
|
williamr@4
|
504 |
size_type erase_unique(const key_type& __x) {
|
williamr@4
|
505 |
iterator __i = find(__x);
|
williamr@4
|
506 |
if (__i._M_node != &this->_M_header._M_data) {
|
williamr@4
|
507 |
erase(__i);
|
williamr@4
|
508 |
return 1;
|
williamr@4
|
509 |
}
|
williamr@4
|
510 |
return 0;
|
williamr@4
|
511 |
}
|
williamr@4
|
512 |
|
williamr@4
|
513 |
void erase(iterator __first, iterator __last) {
|
williamr@4
|
514 |
if (__first._M_node == this->_M_header._M_data._M_left && // begin()
|
williamr@4
|
515 |
__last._M_node == &this->_M_header._M_data) // end()
|
williamr@4
|
516 |
clear();
|
williamr@4
|
517 |
else
|
williamr@4
|
518 |
while (__first != __last) erase(__first++);
|
williamr@4
|
519 |
}
|
williamr@4
|
520 |
|
williamr@4
|
521 |
void erase(const key_type* __first, const key_type* __last) {
|
williamr@4
|
522 |
while (__first != __last) erase(*__first++);
|
williamr@4
|
523 |
}
|
williamr@4
|
524 |
|
williamr@4
|
525 |
void clear() {
|
williamr@4
|
526 |
if (_M_node_count != 0) {
|
williamr@4
|
527 |
_M_erase(_M_root());
|
williamr@4
|
528 |
_M_leftmost() = &this->_M_header._M_data;
|
williamr@4
|
529 |
_M_root() = 0;
|
williamr@4
|
530 |
_M_rightmost() = &this->_M_header._M_data;
|
williamr@4
|
531 |
_M_node_count = 0;
|
williamr@4
|
532 |
}
|
williamr@4
|
533 |
}
|
williamr@4
|
534 |
|
williamr@4
|
535 |
public:
|
williamr@4
|
536 |
// set operations:
|
williamr@4
|
537 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
538 |
iterator find(const _KT& __k) { return iterator(_M_find(__k)); }
|
williamr@4
|
539 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
540 |
const_iterator find(const _KT& __k) const { return const_iterator(_M_find(__k)); }
|
williamr@4
|
541 |
private:
|
williamr@4
|
542 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
543 |
_Base_ptr _M_find(const _KT& __k) const {
|
williamr@4
|
544 |
_Base_ptr __y = __CONST_CAST(_Base_ptr, &this->_M_header._M_data); // Last node which is not less than __k.
|
williamr@4
|
545 |
_Base_ptr __x = _M_root(); // Current node.
|
williamr@4
|
546 |
|
williamr@4
|
547 |
while (__x != 0)
|
williamr@4
|
548 |
if (!_M_key_compare(_S_key(__x), __k))
|
williamr@4
|
549 |
__y = __x, __x = _S_left(__x);
|
williamr@4
|
550 |
else
|
williamr@4
|
551 |
__x = _S_right(__x);
|
williamr@4
|
552 |
|
williamr@4
|
553 |
if (__y != &this->_M_header._M_data) {
|
williamr@4
|
554 |
if (_M_key_compare(__k, _S_key(__y))) {
|
williamr@4
|
555 |
__y = __CONST_CAST(_Base_ptr, &this->_M_header._M_data);
|
williamr@4
|
556 |
}
|
williamr@4
|
557 |
}
|
williamr@4
|
558 |
return __y;
|
williamr@4
|
559 |
}
|
williamr@4
|
560 |
|
williamr@4
|
561 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
562 |
_Base_ptr _M_lower_bound(const _KT& __k) const {
|
williamr@4
|
563 |
_Base_ptr __y = __CONST_CAST(_Base_ptr, &this->_M_header._M_data); /* Last node which is not less than __k. */
|
williamr@4
|
564 |
_Base_ptr __x = _M_root(); /* Current node. */
|
williamr@4
|
565 |
|
williamr@4
|
566 |
while (__x != 0)
|
williamr@4
|
567 |
if (!_M_key_compare(_S_key(__x), __k))
|
williamr@4
|
568 |
__y = __x, __x = _S_left(__x);
|
williamr@4
|
569 |
else
|
williamr@4
|
570 |
__x = _S_right(__x);
|
williamr@4
|
571 |
|
williamr@4
|
572 |
return __y;
|
williamr@4
|
573 |
}
|
williamr@4
|
574 |
|
williamr@4
|
575 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
576 |
_Base_ptr _M_upper_bound(const _KT& __k) const {
|
williamr@4
|
577 |
_Base_ptr __y = __CONST_CAST(_Base_ptr, &this->_M_header._M_data); /* Last node which is greater than __k. */
|
williamr@4
|
578 |
_Base_ptr __x = _M_root(); /* Current node. */
|
williamr@4
|
579 |
|
williamr@4
|
580 |
while (__x != 0)
|
williamr@4
|
581 |
if (_M_key_compare(__k, _S_key(__x)))
|
williamr@4
|
582 |
__y = __x, __x = _S_left(__x);
|
williamr@4
|
583 |
else
|
williamr@4
|
584 |
__x = _S_right(__x);
|
williamr@4
|
585 |
|
williamr@4
|
586 |
return __y;
|
williamr@4
|
587 |
}
|
williamr@4
|
588 |
|
williamr@4
|
589 |
public:
|
williamr@4
|
590 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
591 |
size_type count(const _KT& __x) const {
|
williamr@4
|
592 |
pair<const_iterator, const_iterator> __p = equal_range(__x);
|
williamr@4
|
593 |
return distance(__p.first, __p.second);
|
williamr@4
|
594 |
}
|
williamr@4
|
595 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
596 |
iterator lower_bound(const _KT& __x) { return iterator(_M_lower_bound(__x)); }
|
williamr@4
|
597 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
598 |
const_iterator lower_bound(const _KT& __x) const { return const_iterator(_M_lower_bound(__x)); }
|
williamr@4
|
599 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
600 |
iterator upper_bound(const _KT& __x) { return iterator(_M_upper_bound(__x)); }
|
williamr@4
|
601 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
602 |
const_iterator upper_bound(const _KT& __x) const { return const_iterator(_M_upper_bound(__x)); }
|
williamr@4
|
603 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
604 |
pair<iterator,iterator> equal_range(const _KT& __x)
|
williamr@4
|
605 |
{ return pair<iterator, iterator>(lower_bound(__x), upper_bound(__x)); }
|
williamr@4
|
606 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
607 |
pair<const_iterator, const_iterator> equal_range(const _KT& __x) const
|
williamr@4
|
608 |
{ return pair<const_iterator, const_iterator>(lower_bound(__x), upper_bound(__x)); }
|
williamr@4
|
609 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
610 |
pair<iterator,iterator> equal_range_unique(const _KT& __x) {
|
williamr@4
|
611 |
pair<iterator, iterator> __p;
|
williamr@4
|
612 |
__p.second = lower_bound(__x);
|
williamr@4
|
613 |
if (__p.second._M_node != &this->_M_header._M_data &&
|
williamr@4
|
614 |
!_M_key_compare(__x, _S_key(__p.second._M_node))) {
|
williamr@4
|
615 |
__p.first = __p.second++;
|
williamr@4
|
616 |
}
|
williamr@4
|
617 |
else {
|
williamr@4
|
618 |
__p.first = __p.second;
|
williamr@4
|
619 |
}
|
williamr@4
|
620 |
return __p;
|
williamr@4
|
621 |
}
|
williamr@4
|
622 |
_STLP_TEMPLATE_FOR_CONT_EXT
|
williamr@4
|
623 |
pair<const_iterator, const_iterator> equal_range_unique(const _KT& __x) const {
|
williamr@4
|
624 |
pair<const_iterator, const_iterator> __p;
|
williamr@4
|
625 |
__p.second = lower_bound(__x);
|
williamr@4
|
626 |
if (__p.second._M_node != &this->_M_header._M_data &&
|
williamr@4
|
627 |
!_M_key_compare(__x, _S_key(__p.second._M_node))) {
|
williamr@4
|
628 |
__p.first = __p.second++;
|
williamr@4
|
629 |
}
|
williamr@4
|
630 |
else {
|
williamr@4
|
631 |
__p.first = __p.second;
|
williamr@4
|
632 |
}
|
williamr@4
|
633 |
return __p;
|
williamr@4
|
634 |
}
|
williamr@4
|
635 |
|
williamr@4
|
636 |
#if defined (_STLP_DEBUG)
|
williamr@4
|
637 |
public:
|
williamr@4
|
638 |
// Debugging.
|
williamr@4
|
639 |
bool __rb_verify() const;
|
williamr@4
|
640 |
#endif //_STLP_DEBUG
|
williamr@4
|
641 |
};
|
williamr@4
|
642 |
|
williamr@4
|
643 |
#if defined (_STLP_DEBUG)
|
williamr@4
|
644 |
# undef _Rb_tree
|
williamr@4
|
645 |
#endif
|
williamr@4
|
646 |
|
williamr@4
|
647 |
_STLP_MOVE_TO_STD_NAMESPACE
|
williamr@4
|
648 |
|
williamr@4
|
649 |
_STLP_END_NAMESPACE
|
williamr@4
|
650 |
|
williamr@4
|
651 |
#if !defined (_STLP_LINK_TIME_INSTANTIATION)
|
williamr@4
|
652 |
# include <stl/_tree.c>
|
williamr@4
|
653 |
#endif
|
williamr@4
|
654 |
|
williamr@4
|
655 |
#if defined (_STLP_DEBUG)
|
williamr@4
|
656 |
# include <stl/debug/_tree.h>
|
williamr@4
|
657 |
#endif
|
williamr@4
|
658 |
|
williamr@4
|
659 |
_STLP_BEGIN_NAMESPACE
|
williamr@4
|
660 |
|
williamr@4
|
661 |
#define _STLP_TEMPLATE_HEADER template <class _Key, class _Compare, class _Value, class _KeyOfValue, class _Traits, class _Alloc>
|
williamr@4
|
662 |
#define _STLP_TEMPLATE_CONTAINER _STLP_PRIV _Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc>
|
williamr@4
|
663 |
#include <stl/_relops_cont.h>
|
williamr@4
|
664 |
#undef _STLP_TEMPLATE_CONTAINER
|
williamr@4
|
665 |
#undef _STLP_TEMPLATE_HEADER
|
williamr@4
|
666 |
|
williamr@4
|
667 |
#if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
|
williamr@4
|
668 |
template <class _Key, class _Compare, class _Value, class _KeyOfValue, class _Traits, class _Alloc>
|
williamr@4
|
669 |
struct __move_traits<_STLP_PRIV _Rb_tree<_Key, _Compare, _Value, _KeyOfValue, _Traits, _Alloc> >
|
williamr@4
|
670 |
: _STLP_PRIV __move_traits_help2<_Compare, _Alloc> {};
|
williamr@4
|
671 |
#endif
|
williamr@4
|
672 |
|
williamr@4
|
673 |
_STLP_END_NAMESPACE
|
williamr@4
|
674 |
|
williamr@4
|
675 |
#endif /* _STLP_INTERNAL_TREE_H */
|
williamr@4
|
676 |
|
williamr@4
|
677 |
// Local Variables:
|
williamr@4
|
678 |
// mode:C++
|
williamr@4
|
679 |
// End:
|