1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/epoc32/include/tools/stlport/stl/_tree.c Wed Mar 31 12:33:34 2010 +0100
1.3 @@ -0,0 +1,730 @@
1.4 +/*
1.5 + *
1.6 + *
1.7 + * Copyright (c) 1994
1.8 + * Hewlett-Packard Company
1.9 + *
1.10 + * Copyright (c) 1996,1997
1.11 + * Silicon Graphics Computer Systems, Inc.
1.12 + *
1.13 + * Copyright (c) 1997
1.14 + * Moscow Center for SPARC Technology
1.15 + *
1.16 + * Copyright (c) 1999
1.17 + * Boris Fomitchev
1.18 + *
1.19 + * This material is provided "as is", with absolutely no warranty expressed
1.20 + * or implied. Any use is at your own risk.
1.21 + *
1.22 + * Permission to use or copy this software for any purpose is hereby granted
1.23 + * without fee, provided the above notices are retained on all copies.
1.24 + * Permission to modify the code and to distribute modified code is granted,
1.25 + * provided the above notices are retained, and a notice that the code was
1.26 + * modified is included with the above copyright notice.
1.27 + *
1.28 + * Modified CRP 7/10/00 for improved conformance / efficiency on insert_unique /
1.29 + * insert_equal with valid hint -- efficiency is improved all around, and it is
1.30 + * should now be standard conforming for complexity on insert point immediately
1.31 + * after hint (amortized constant time).
1.32 + *
1.33 + */
1.34 +#ifndef _STLP_TREE_C
1.35 +#define _STLP_TREE_C
1.36 +
1.37 +#ifndef _STLP_INTERNAL_TREE_H
1.38 +# include <stl/_tree.h>
1.39 +#endif
1.40 +
1.41 +#if defined (_STLP_DEBUG)
1.42 +# define _Rb_tree _STLP_NON_DBG_NAME(Rb_tree)
1.43 +#endif
1.44 +
1.45 +// fbp: these defines are for outline methods definitions.
1.46 +// needed for definitions to be portable. Should not be used in method bodies.
1.47 +#if defined (_STLP_NESTED_TYPE_PARAM_BUG)
1.48 +# define __iterator__ _Rb_tree_iterator<_Value, _STLP_HEADER_TYPENAME _Traits::_NonConstTraits>
1.49 +# define __size_type__ size_t
1.50 +# define iterator __iterator__
1.51 +#else
1.52 +# define __iterator__ _STLP_TYPENAME_ON_RETURN_TYPE _Rb_tree<_Key, _Compare, _Value, _KeyOfValue, _Traits, _Alloc>::iterator
1.53 +# define __size_type__ _STLP_TYPENAME_ON_RETURN_TYPE _Rb_tree<_Key, _Compare, _Value, _KeyOfValue, _Traits, _Alloc>::size_type
1.54 +#endif
1.55 +
1.56 +_STLP_BEGIN_NAMESPACE
1.57 +
1.58 +_STLP_MOVE_TO_PRIV_NAMESPACE
1.59 +
1.60 +#if defined (_STLP_EXPOSE_GLOBALS_IMPLEMENTATION)
1.61 +
1.62 +template <class _Dummy> void _STLP_CALL
1.63 +_Rb_global<_Dummy>::_Rotate_left(_Rb_tree_node_base* __x,
1.64 + _Rb_tree_node_base*& __root) {
1.65 + _Rb_tree_node_base* __y = __x->_M_right;
1.66 + __x->_M_right = __y->_M_left;
1.67 + if (__y->_M_left != 0)
1.68 + __y->_M_left->_M_parent = __x;
1.69 + __y->_M_parent = __x->_M_parent;
1.70 +
1.71 + if (__x == __root)
1.72 + __root = __y;
1.73 + else if (__x == __x->_M_parent->_M_left)
1.74 + __x->_M_parent->_M_left = __y;
1.75 + else
1.76 + __x->_M_parent->_M_right = __y;
1.77 + __y->_M_left = __x;
1.78 + __x->_M_parent = __y;
1.79 +}
1.80 +
1.81 +template <class _Dummy> void _STLP_CALL
1.82 +_Rb_global<_Dummy>::_Rotate_right(_Rb_tree_node_base* __x,
1.83 + _Rb_tree_node_base*& __root) {
1.84 + _Rb_tree_node_base* __y = __x->_M_left;
1.85 + __x->_M_left = __y->_M_right;
1.86 + if (__y->_M_right != 0)
1.87 + __y->_M_right->_M_parent = __x;
1.88 + __y->_M_parent = __x->_M_parent;
1.89 +
1.90 + if (__x == __root)
1.91 + __root = __y;
1.92 + else if (__x == __x->_M_parent->_M_right)
1.93 + __x->_M_parent->_M_right = __y;
1.94 + else
1.95 + __x->_M_parent->_M_left = __y;
1.96 + __y->_M_right = __x;
1.97 + __x->_M_parent = __y;
1.98 +}
1.99 +
1.100 +template <class _Dummy> void _STLP_CALL
1.101 +_Rb_global<_Dummy>::_Rebalance(_Rb_tree_node_base* __x,
1.102 + _Rb_tree_node_base*& __root) {
1.103 + __x->_M_color = _S_rb_tree_red;
1.104 + while (__x != __root && __x->_M_parent->_M_color == _S_rb_tree_red) {
1.105 + if (__x->_M_parent == __x->_M_parent->_M_parent->_M_left) {
1.106 + _Rb_tree_node_base* __y = __x->_M_parent->_M_parent->_M_right;
1.107 + if (__y && __y->_M_color == _S_rb_tree_red) {
1.108 + __x->_M_parent->_M_color = _S_rb_tree_black;
1.109 + __y->_M_color = _S_rb_tree_black;
1.110 + __x->_M_parent->_M_parent->_M_color = _S_rb_tree_red;
1.111 + __x = __x->_M_parent->_M_parent;
1.112 + }
1.113 + else {
1.114 + if (__x == __x->_M_parent->_M_right) {
1.115 + __x = __x->_M_parent;
1.116 + _Rotate_left(__x, __root);
1.117 + }
1.118 + __x->_M_parent->_M_color = _S_rb_tree_black;
1.119 + __x->_M_parent->_M_parent->_M_color = _S_rb_tree_red;
1.120 + _Rotate_right(__x->_M_parent->_M_parent, __root);
1.121 + }
1.122 + }
1.123 + else {
1.124 + _Rb_tree_node_base* __y = __x->_M_parent->_M_parent->_M_left;
1.125 + if (__y && __y->_M_color == _S_rb_tree_red) {
1.126 + __x->_M_parent->_M_color = _S_rb_tree_black;
1.127 + __y->_M_color = _S_rb_tree_black;
1.128 + __x->_M_parent->_M_parent->_M_color = _S_rb_tree_red;
1.129 + __x = __x->_M_parent->_M_parent;
1.130 + }
1.131 + else {
1.132 + if (__x == __x->_M_parent->_M_left) {
1.133 + __x = __x->_M_parent;
1.134 + _Rotate_right(__x, __root);
1.135 + }
1.136 + __x->_M_parent->_M_color = _S_rb_tree_black;
1.137 + __x->_M_parent->_M_parent->_M_color = _S_rb_tree_red;
1.138 + _Rotate_left(__x->_M_parent->_M_parent, __root);
1.139 + }
1.140 + }
1.141 + }
1.142 + __root->_M_color = _S_rb_tree_black;
1.143 +}
1.144 +
1.145 +template <class _Dummy> _Rb_tree_node_base* _STLP_CALL
1.146 +_Rb_global<_Dummy>::_Rebalance_for_erase(_Rb_tree_node_base* __z,
1.147 + _Rb_tree_node_base*& __root,
1.148 + _Rb_tree_node_base*& __leftmost,
1.149 + _Rb_tree_node_base*& __rightmost) {
1.150 + _Rb_tree_node_base* __y = __z;
1.151 + _Rb_tree_node_base* __x;
1.152 + _Rb_tree_node_base* __x_parent;
1.153 +
1.154 + if (__y->_M_left == 0) // __z has at most one non-null child. y == z.
1.155 + __x = __y->_M_right; // __x might be null.
1.156 + else {
1.157 + if (__y->_M_right == 0) // __z has exactly one non-null child. y == z.
1.158 + __x = __y->_M_left; // __x is not null.
1.159 + else { // __z has two non-null children. Set __y to
1.160 + __y = _Rb_tree_node_base::_S_minimum(__y->_M_right); // __z's successor. __x might be null.
1.161 + __x = __y->_M_right;
1.162 + }
1.163 + }
1.164 +
1.165 + if (__y != __z) { // relink y in place of z. y is z's successor
1.166 + __z->_M_left->_M_parent = __y;
1.167 + __y->_M_left = __z->_M_left;
1.168 + if (__y != __z->_M_right) {
1.169 + __x_parent = __y->_M_parent;
1.170 + if (__x) __x->_M_parent = __y->_M_parent;
1.171 + __y->_M_parent->_M_left = __x; // __y must be a child of _M_left
1.172 + __y->_M_right = __z->_M_right;
1.173 + __z->_M_right->_M_parent = __y;
1.174 + }
1.175 + else
1.176 + __x_parent = __y;
1.177 + if (__root == __z)
1.178 + __root = __y;
1.179 + else if (__z->_M_parent->_M_left == __z)
1.180 + __z->_M_parent->_M_left = __y;
1.181 + else
1.182 + __z->_M_parent->_M_right = __y;
1.183 + __y->_M_parent = __z->_M_parent;
1.184 + _STLP_STD::swap(__y->_M_color, __z->_M_color);
1.185 + __y = __z;
1.186 + // __y now points to node to be actually deleted
1.187 + }
1.188 + else { // __y == __z
1.189 + __x_parent = __y->_M_parent;
1.190 + if (__x) __x->_M_parent = __y->_M_parent;
1.191 + if (__root == __z)
1.192 + __root = __x;
1.193 + else {
1.194 + if (__z->_M_parent->_M_left == __z)
1.195 + __z->_M_parent->_M_left = __x;
1.196 + else
1.197 + __z->_M_parent->_M_right = __x;
1.198 + }
1.199 +
1.200 + if (__leftmost == __z) {
1.201 + if (__z->_M_right == 0) // __z->_M_left must be null also
1.202 + __leftmost = __z->_M_parent;
1.203 + // makes __leftmost == _M_header if __z == __root
1.204 + else
1.205 + __leftmost = _Rb_tree_node_base::_S_minimum(__x);
1.206 + }
1.207 + if (__rightmost == __z) {
1.208 + if (__z->_M_left == 0) // __z->_M_right must be null also
1.209 + __rightmost = __z->_M_parent;
1.210 + // makes __rightmost == _M_header if __z == __root
1.211 + else // __x == __z->_M_left
1.212 + __rightmost = _Rb_tree_node_base::_S_maximum(__x);
1.213 + }
1.214 + }
1.215 +
1.216 + if (__y->_M_color != _S_rb_tree_red) {
1.217 + while (__x != __root && (__x == 0 || __x->_M_color == _S_rb_tree_black))
1.218 + if (__x == __x_parent->_M_left) {
1.219 + _Rb_tree_node_base* __w = __x_parent->_M_right;
1.220 + if (__w->_M_color == _S_rb_tree_red) {
1.221 + __w->_M_color = _S_rb_tree_black;
1.222 + __x_parent->_M_color = _S_rb_tree_red;
1.223 + _Rotate_left(__x_parent, __root);
1.224 + __w = __x_parent->_M_right;
1.225 + }
1.226 + if ((__w->_M_left == 0 ||
1.227 + __w->_M_left->_M_color == _S_rb_tree_black) && (__w->_M_right == 0 ||
1.228 + __w->_M_right->_M_color == _S_rb_tree_black)) {
1.229 + __w->_M_color = _S_rb_tree_red;
1.230 + __x = __x_parent;
1.231 + __x_parent = __x_parent->_M_parent;
1.232 + } else {
1.233 + if (__w->_M_right == 0 ||
1.234 + __w->_M_right->_M_color == _S_rb_tree_black) {
1.235 + if (__w->_M_left) __w->_M_left->_M_color = _S_rb_tree_black;
1.236 + __w->_M_color = _S_rb_tree_red;
1.237 + _Rotate_right(__w, __root);
1.238 + __w = __x_parent->_M_right;
1.239 + }
1.240 + __w->_M_color = __x_parent->_M_color;
1.241 + __x_parent->_M_color = _S_rb_tree_black;
1.242 + if (__w->_M_right) __w->_M_right->_M_color = _S_rb_tree_black;
1.243 + _Rotate_left(__x_parent, __root);
1.244 + break;
1.245 + }
1.246 + } else { // same as above, with _M_right <-> _M_left.
1.247 + _Rb_tree_node_base* __w = __x_parent->_M_left;
1.248 + if (__w->_M_color == _S_rb_tree_red) {
1.249 + __w->_M_color = _S_rb_tree_black;
1.250 + __x_parent->_M_color = _S_rb_tree_red;
1.251 + _Rotate_right(__x_parent, __root);
1.252 + __w = __x_parent->_M_left;
1.253 + }
1.254 + if ((__w->_M_right == 0 ||
1.255 + __w->_M_right->_M_color == _S_rb_tree_black) && (__w->_M_left == 0 ||
1.256 + __w->_M_left->_M_color == _S_rb_tree_black)) {
1.257 + __w->_M_color = _S_rb_tree_red;
1.258 + __x = __x_parent;
1.259 + __x_parent = __x_parent->_M_parent;
1.260 + } else {
1.261 + if (__w->_M_left == 0 ||
1.262 + __w->_M_left->_M_color == _S_rb_tree_black) {
1.263 + if (__w->_M_right) __w->_M_right->_M_color = _S_rb_tree_black;
1.264 + __w->_M_color = _S_rb_tree_red;
1.265 + _Rotate_left(__w, __root);
1.266 + __w = __x_parent->_M_left;
1.267 + }
1.268 + __w->_M_color = __x_parent->_M_color;
1.269 + __x_parent->_M_color = _S_rb_tree_black;
1.270 + if (__w->_M_left) __w->_M_left->_M_color = _S_rb_tree_black;
1.271 + _Rotate_right(__x_parent, __root);
1.272 + break;
1.273 + }
1.274 + }
1.275 + if (__x) __x->_M_color = _S_rb_tree_black;
1.276 + }
1.277 + return __y;
1.278 +}
1.279 +
1.280 +template <class _Dummy> _Rb_tree_node_base* _STLP_CALL
1.281 +_Rb_global<_Dummy>::_M_decrement(_Rb_tree_node_base* _M_node) {
1.282 + if (_M_node->_M_color == _S_rb_tree_red && _M_node->_M_parent->_M_parent == _M_node)
1.283 + _M_node = _M_node->_M_right;
1.284 + else if (_M_node->_M_left != 0) {
1.285 + _M_node = _Rb_tree_node_base::_S_maximum(_M_node->_M_left);
1.286 + }
1.287 + else {
1.288 + _Base_ptr __y = _M_node->_M_parent;
1.289 + while (_M_node == __y->_M_left) {
1.290 + _M_node = __y;
1.291 + __y = __y->_M_parent;
1.292 + }
1.293 + _M_node = __y;
1.294 + }
1.295 + return _M_node;
1.296 +}
1.297 +
1.298 +template <class _Dummy> _Rb_tree_node_base* _STLP_CALL
1.299 +_Rb_global<_Dummy>::_M_increment(_Rb_tree_node_base* _M_node) {
1.300 + if (_M_node->_M_right != 0) {
1.301 + _M_node = _Rb_tree_node_base::_S_minimum(_M_node->_M_right);
1.302 + }
1.303 + else {
1.304 + _Base_ptr __y = _M_node->_M_parent;
1.305 + while (_M_node == __y->_M_right) {
1.306 + _M_node = __y;
1.307 + __y = __y->_M_parent;
1.308 + }
1.309 + // check special case: This is necessary if _M_node is the
1.310 + // _M_head and the tree contains only a single node __y. In
1.311 + // that case parent, left and right all point to __y!
1.312 + if (_M_node->_M_right != __y)
1.313 + _M_node = __y;
1.314 + }
1.315 + return _M_node;
1.316 +}
1.317 +
1.318 +#endif /* _STLP_EXPOSE_GLOBALS_IMPLEMENTATION */
1.319 +
1.320 +
1.321 +template <class _Key, class _Compare,
1.322 + class _Value, class _KeyOfValue, class _Traits, class _Alloc>
1.323 +_Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc>&
1.324 +_Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc> ::operator=(
1.325 + const _Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc>& __x) {
1.326 + if (this != &__x) {
1.327 + // Note that _Key may be a constant type.
1.328 + clear();
1.329 + _M_node_count = 0;
1.330 + _M_key_compare = __x._M_key_compare;
1.331 + if (__x._M_root() == 0) {
1.332 + _M_root() = 0;
1.333 + _M_leftmost() = &this->_M_header._M_data;
1.334 + _M_rightmost() = &this->_M_header._M_data;
1.335 + }
1.336 + else {
1.337 + _M_root() = _M_copy(__x._M_root(), &this->_M_header._M_data);
1.338 + _M_leftmost() = _S_minimum(_M_root());
1.339 + _M_rightmost() = _S_maximum(_M_root());
1.340 + _M_node_count = __x._M_node_count;
1.341 + }
1.342 + }
1.343 + return *this;
1.344 +}
1.345 +
1.346 +// CRP 7/10/00 inserted argument __on_right, which is another hint (meant to
1.347 +// act like __on_left and ignore a portion of the if conditions -- specify
1.348 +// __on_right != 0 to bypass comparison as false or __on_left != 0 to bypass
1.349 +// comparison as true)
1.350 +template <class _Key, class _Compare,
1.351 + class _Value, class _KeyOfValue, class _Traits, class _Alloc>
1.352 +__iterator__
1.353 +_Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc> ::_M_insert(_Rb_tree_node_base * __parent,
1.354 + const _Value& __val,
1.355 + _Rb_tree_node_base * __on_left,
1.356 + _Rb_tree_node_base * __on_right) {
1.357 + // We do not create the node here as, depending on tests, we might call
1.358 + // _M_key_compare that can throw an exception.
1.359 + _Base_ptr __new_node;
1.360 +
1.361 + if ( __parent == &this->_M_header._M_data ) {
1.362 + __new_node = _M_create_node(__val);
1.363 + _S_left(__parent) = __new_node; // also makes _M_leftmost() = __new_node
1.364 + _M_root() = __new_node;
1.365 + _M_rightmost() = __new_node;
1.366 + }
1.367 + else if ( __on_right == 0 && // If __on_right != 0, the remainder fails to false
1.368 + ( __on_left != 0 || // If __on_left != 0, the remainder succeeds to true
1.369 + _M_key_compare( _KeyOfValue()(__val), _S_key(__parent) ) ) ) {
1.370 + __new_node = _M_create_node(__val);
1.371 + _S_left(__parent) = __new_node;
1.372 + if (__parent == _M_leftmost())
1.373 + _M_leftmost() = __new_node; // maintain _M_leftmost() pointing to min node
1.374 + }
1.375 + else {
1.376 + __new_node = _M_create_node(__val);
1.377 + _S_right(__parent) = __new_node;
1.378 + if (__parent == _M_rightmost())
1.379 + _M_rightmost() = __new_node; // maintain _M_rightmost() pointing to max node
1.380 + }
1.381 + _S_parent(__new_node) = __parent;
1.382 + _Rb_global_inst::_Rebalance(__new_node, this->_M_header._M_data._M_parent);
1.383 + ++_M_node_count;
1.384 + return iterator(__new_node);
1.385 +}
1.386 +
1.387 +template <class _Key, class _Compare,
1.388 + class _Value, class _KeyOfValue, class _Traits, class _Alloc>
1.389 +__iterator__
1.390 +_Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc> ::insert_equal(const _Value& __val) {
1.391 + _Base_ptr __y = &this->_M_header._M_data;
1.392 + _Base_ptr __x = _M_root();
1.393 + while (__x != 0) {
1.394 + __y = __x;
1.395 + if (_M_key_compare(_KeyOfValue()(__val), _S_key(__x))) {
1.396 + __x = _S_left(__x);
1.397 + }
1.398 + else
1.399 + __x = _S_right(__x);
1.400 + }
1.401 + return _M_insert(__y, __val, __x);
1.402 +}
1.403 +
1.404 +
1.405 +template <class _Key, class _Compare,
1.406 + class _Value, class _KeyOfValue, class _Traits, class _Alloc>
1.407 +pair<__iterator__, bool>
1.408 +_Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc> ::insert_unique(const _Value& __val) {
1.409 + _Base_ptr __y = &this->_M_header._M_data;
1.410 + _Base_ptr __x = _M_root();
1.411 + bool __comp = true;
1.412 + while (__x != 0) {
1.413 + __y = __x;
1.414 + __comp = _M_key_compare(_KeyOfValue()(__val), _S_key(__x));
1.415 + __x = __comp ? _S_left(__x) : _S_right(__x);
1.416 + }
1.417 + iterator __j = iterator(__y);
1.418 + if (__comp) {
1.419 + if (__j == begin())
1.420 + return pair<iterator,bool>(_M_insert(__y, __val, /* __x*/ __y), true);
1.421 + else
1.422 + --__j;
1.423 + }
1.424 + if (_M_key_compare(_S_key(__j._M_node), _KeyOfValue()(__val))) {
1.425 + return pair<iterator,bool>(_M_insert(__y, __val, __x), true);
1.426 + }
1.427 + return pair<iterator,bool>(__j, false);
1.428 +}
1.429 +
1.430 +// Modifications CRP 7/10/00 as noted to improve conformance and
1.431 +// efficiency.
1.432 +template <class _Key, class _Compare,
1.433 + class _Value, class _KeyOfValue, class _Traits, class _Alloc>
1.434 +__iterator__
1.435 +_Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc> ::insert_unique(iterator __position,
1.436 + const _Value& __val) {
1.437 + if (__position._M_node == this->_M_header._M_data._M_left) { // begin()
1.438 +
1.439 + // if the container is empty, fall back on insert_unique.
1.440 + if (empty())
1.441 + return insert_unique(__val).first;
1.442 +
1.443 + if (_M_key_compare(_KeyOfValue()(__val), _S_key(__position._M_node))) {
1.444 + return _M_insert(__position._M_node, __val, __position._M_node);
1.445 + }
1.446 + // first argument just needs to be non-null
1.447 + else {
1.448 + bool __comp_pos_v = _M_key_compare( _S_key(__position._M_node), _KeyOfValue()(__val) );
1.449 +
1.450 + if (__comp_pos_v == false) // compare > and compare < both false so compare equal
1.451 + return __position;
1.452 + //Below __comp_pos_v == true
1.453 +
1.454 + // Standard-conformance - does the insertion point fall immediately AFTER
1.455 + // the hint?
1.456 + iterator __after = __position;
1.457 + ++__after;
1.458 +
1.459 + // Check for only one member -- in that case, __position points to itself,
1.460 + // and attempting to increment will cause an infinite loop.
1.461 + if (__after._M_node == &this->_M_header._M_data)
1.462 + // Check guarantees exactly one member, so comparison was already
1.463 + // performed and we know the result; skip repeating it in _M_insert
1.464 + // by specifying a non-zero fourth argument.
1.465 + return _M_insert(__position._M_node, __val, 0, __position._M_node);
1.466 +
1.467 + // All other cases:
1.468 +
1.469 + // Optimization to catch insert-equivalent -- save comparison results,
1.470 + // and we get this for free.
1.471 + if (_M_key_compare( _KeyOfValue()(__val), _S_key(__after._M_node) )) {
1.472 + if (_S_right(__position._M_node) == 0)
1.473 + return _M_insert(__position._M_node, __val, 0, __position._M_node);
1.474 + else
1.475 + return _M_insert(__after._M_node, __val, __after._M_node);
1.476 + }
1.477 + else {
1.478 + return insert_unique(__val).first;
1.479 + }
1.480 + }
1.481 + }
1.482 + else if (__position._M_node == &this->_M_header._M_data) { // end()
1.483 + if (_M_key_compare(_S_key(_M_rightmost()), _KeyOfValue()(__val))) {
1.484 + // pass along to _M_insert that it can skip comparing
1.485 + // v, Key ; since compare Key, v was true, compare v, Key must be false.
1.486 + return _M_insert(_M_rightmost(), __val, 0, __position._M_node); // Last argument only needs to be non-null
1.487 + }
1.488 + else
1.489 + return insert_unique(__val).first;
1.490 + }
1.491 + else {
1.492 + iterator __before = __position;
1.493 + --__before;
1.494 +
1.495 + bool __comp_v_pos = _M_key_compare(_KeyOfValue()(__val), _S_key(__position._M_node));
1.496 +
1.497 + if (__comp_v_pos
1.498 + && _M_key_compare( _S_key(__before._M_node), _KeyOfValue()(__val) )) {
1.499 +
1.500 + if (_S_right(__before._M_node) == 0)
1.501 + return _M_insert(__before._M_node, __val, 0, __before._M_node); // Last argument only needs to be non-null
1.502 + else
1.503 + return _M_insert(__position._M_node, __val, __position._M_node);
1.504 + // first argument just needs to be non-null
1.505 + }
1.506 + else {
1.507 + // Does the insertion point fall immediately AFTER the hint?
1.508 + iterator __after = __position;
1.509 + ++__after;
1.510 + // Optimization to catch equivalent cases and avoid unnecessary comparisons
1.511 + bool __comp_pos_v = !__comp_v_pos; // Stored this result earlier
1.512 + // If the earlier comparison was true, this comparison doesn't need to be
1.513 + // performed because it must be false. However, if the earlier comparison
1.514 + // was false, we need to perform this one because in the equal case, both will
1.515 + // be false.
1.516 + if (!__comp_v_pos) {
1.517 + __comp_pos_v = _M_key_compare(_S_key(__position._M_node), _KeyOfValue()(__val));
1.518 + }
1.519 +
1.520 + if ( (!__comp_v_pos) // comp_v_pos true implies comp_v_pos false
1.521 + && __comp_pos_v
1.522 + && (__after._M_node == &this->_M_header._M_data ||
1.523 + _M_key_compare( _KeyOfValue()(__val), _S_key(__after._M_node) ))) {
1.524 + if (_S_right(__position._M_node) == 0)
1.525 + return _M_insert(__position._M_node, __val, 0, __position._M_node);
1.526 + else
1.527 + return _M_insert(__after._M_node, __val, __after._M_node);
1.528 + } else {
1.529 + // Test for equivalent case
1.530 + if (__comp_v_pos == __comp_pos_v)
1.531 + return __position;
1.532 + else
1.533 + return insert_unique(__val).first;
1.534 + }
1.535 + }
1.536 + }
1.537 +}
1.538 +
1.539 +template <class _Key, class _Compare,
1.540 + class _Value, class _KeyOfValue, class _Traits, class _Alloc>
1.541 +__iterator__
1.542 +_Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc> ::insert_equal(iterator __position,
1.543 + const _Value& __val) {
1.544 + if (__position._M_node == this->_M_header._M_data._M_left) { // begin()
1.545 +
1.546 + // Check for zero members
1.547 + if (size() <= 0)
1.548 + return insert_equal(__val);
1.549 +
1.550 + if (!_M_key_compare(_S_key(__position._M_node), _KeyOfValue()(__val)))
1.551 + return _M_insert(__position._M_node, __val, __position._M_node);
1.552 + else {
1.553 + // Check for only one member
1.554 + if (__position._M_node->_M_left == __position._M_node)
1.555 + // Unlike insert_unique, can't avoid doing a comparison here.
1.556 + return _M_insert(__position._M_node, __val);
1.557 +
1.558 + // All other cases:
1.559 + // Standard-conformance - does the insertion point fall immediately AFTER
1.560 + // the hint?
1.561 + iterator __after = __position;
1.562 + ++__after;
1.563 +
1.564 + // Already know that compare(pos, v) must be true!
1.565 + // Therefore, we want to know if compare(after, v) is false.
1.566 + // (i.e., we now pos < v, now we want to know if v <= after)
1.567 + // If not, invalid hint.
1.568 + if ( __after._M_node == &this->_M_header._M_data ||
1.569 + !_M_key_compare( _S_key(__after._M_node), _KeyOfValue()(__val) ) ) {
1.570 + if (_S_right(__position._M_node) == 0)
1.571 + return _M_insert(__position._M_node, __val, 0, __position._M_node);
1.572 + else
1.573 + return _M_insert(__after._M_node, __val, __after._M_node);
1.574 + }
1.575 + else { // Invalid hint
1.576 + return insert_equal(__val);
1.577 + }
1.578 + }
1.579 + }
1.580 + else if (__position._M_node == &this->_M_header._M_data) { // end()
1.581 + if (!_M_key_compare(_KeyOfValue()(__val), _S_key(_M_rightmost())))
1.582 + return _M_insert(_M_rightmost(), __val, 0, __position._M_node); // Last argument only needs to be non-null
1.583 + else {
1.584 + return insert_equal(__val);
1.585 + }
1.586 + }
1.587 + else {
1.588 + iterator __before = __position;
1.589 + --__before;
1.590 + // store the result of the comparison between pos and v so
1.591 + // that we don't have to do it again later. Note that this reverses the shortcut
1.592 + // on the if, possibly harming efficiency in comparisons; I think the harm will
1.593 + // be negligible, and to do what I want to do (save the result of a comparison so
1.594 + // that it can be re-used) there is no alternative. Test here is for before <= v <= pos.
1.595 + bool __comp_pos_v = _M_key_compare(_S_key(__position._M_node), _KeyOfValue()(__val));
1.596 + if (!__comp_pos_v &&
1.597 + !_M_key_compare(_KeyOfValue()(__val), _S_key(__before._M_node))) {
1.598 + if (_S_right(__before._M_node) == 0)
1.599 + return _M_insert(__before._M_node, __val, 0, __before._M_node); // Last argument only needs to be non-null
1.600 + else
1.601 + return _M_insert(__position._M_node, __val, __position._M_node);
1.602 + }
1.603 + else {
1.604 + // Does the insertion point fall immediately AFTER the hint?
1.605 + // Test for pos < v <= after
1.606 + iterator __after = __position;
1.607 + ++__after;
1.608 +
1.609 + if (__comp_pos_v &&
1.610 + ( __after._M_node == &this->_M_header._M_data ||
1.611 + !_M_key_compare( _S_key(__after._M_node), _KeyOfValue()(__val) ) ) ) {
1.612 + if (_S_right(__position._M_node) == 0)
1.613 + return _M_insert(__position._M_node, __val, 0, __position._M_node);
1.614 + else
1.615 + return _M_insert(__after._M_node, __val, __after._M_node);
1.616 + }
1.617 + else { // Invalid hint
1.618 + return insert_equal(__val);
1.619 + }
1.620 + }
1.621 + }
1.622 +}
1.623 +
1.624 +template <class _Key, class _Compare,
1.625 + class _Value, class _KeyOfValue, class _Traits, class _Alloc>
1.626 +_Rb_tree_node_base*
1.627 +_Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc> ::_M_copy(_Rb_tree_node_base* __x,
1.628 + _Rb_tree_node_base* __p) {
1.629 + // structural copy. __x and __p must be non-null.
1.630 + _Base_ptr __top = _M_clone_node(__x);
1.631 + _S_parent(__top) = __p;
1.632 +
1.633 + _STLP_TRY {
1.634 + if (_S_right(__x))
1.635 + _S_right(__top) = _M_copy(_S_right(__x), __top);
1.636 + __p = __top;
1.637 + __x = _S_left(__x);
1.638 +
1.639 + while (__x != 0) {
1.640 + _Base_ptr __y = _M_clone_node(__x);
1.641 + _S_left(__p) = __y;
1.642 + _S_parent(__y) = __p;
1.643 + if (_S_right(__x))
1.644 + _S_right(__y) = _M_copy(_S_right(__x), __y);
1.645 + __p = __y;
1.646 + __x = _S_left(__x);
1.647 + }
1.648 + }
1.649 + _STLP_UNWIND(_M_erase(__top))
1.650 +
1.651 + return __top;
1.652 +}
1.653 +
1.654 +// this has to stay out-of-line : it's recursive
1.655 +template <class _Key, class _Compare,
1.656 + class _Value, class _KeyOfValue, class _Traits, class _Alloc>
1.657 +void
1.658 +_Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc>::_M_erase(_Rb_tree_node_base *__x) {
1.659 + // erase without rebalancing
1.660 + while (__x != 0) {
1.661 + _M_erase(_S_right(__x));
1.662 + _Base_ptr __y = _S_left(__x);
1.663 + _STLP_STD::_Destroy(&_S_value(__x));
1.664 + this->_M_header.deallocate(__STATIC_CAST(_Link_type, __x),1);
1.665 + __x = __y;
1.666 + }
1.667 +}
1.668 +
1.669 +#if defined (_STLP_DEBUG)
1.670 +inline int
1.671 +__black_count(_Rb_tree_node_base* __node, _Rb_tree_node_base* __root) {
1.672 + if (__node == 0)
1.673 + return 0;
1.674 + else {
1.675 + int __bc = __node->_M_color == _S_rb_tree_black ? 1 : 0;
1.676 + if (__node == __root)
1.677 + return __bc;
1.678 + else
1.679 + return __bc + __black_count(__node->_M_parent, __root);
1.680 + }
1.681 +}
1.682 +
1.683 +template <class _Key, class _Compare,
1.684 + class _Value, class _KeyOfValue, class _Traits, class _Alloc>
1.685 +bool _Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc>::__rb_verify() const {
1.686 + if (_M_node_count == 0 || begin() == end())
1.687 + return ((_M_node_count == 0) &&
1.688 + (begin() == end()) &&
1.689 + (this->_M_header._M_data._M_left == &this->_M_header._M_data) &&
1.690 + (this->_M_header._M_data._M_right == &this->_M_header._M_data));
1.691 +
1.692 + int __len = __black_count(_M_leftmost(), _M_root());
1.693 + for (const_iterator __it = begin(); __it != end(); ++__it) {
1.694 + _Base_ptr __x = __it._M_node;
1.695 + _Base_ptr __L = _S_left(__x);
1.696 + _Base_ptr __R = _S_right(__x);
1.697 +
1.698 + if (__x->_M_color == _S_rb_tree_red)
1.699 + if ((__L && __L->_M_color == _S_rb_tree_red) ||
1.700 + (__R && __R->_M_color == _S_rb_tree_red))
1.701 + return false;
1.702 +
1.703 + if (__L && _M_key_compare(_S_key(__x), _S_key(__L)))
1.704 + return false;
1.705 + if (__R && _M_key_compare(_S_key(__R), _S_key(__x)))
1.706 + return false;
1.707 +
1.708 + if (!__L && !__R && __black_count(__x, _M_root()) != __len)
1.709 + return false;
1.710 + }
1.711 +
1.712 + if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
1.713 + return false;
1.714 + if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
1.715 + return false;
1.716 +
1.717 + return true;
1.718 +}
1.719 +#endif /* _STLP_DEBUG */
1.720 +
1.721 +_STLP_MOVE_TO_STD_NAMESPACE
1.722 +_STLP_END_NAMESPACE
1.723 +
1.724 +#undef _Rb_tree
1.725 +#undef __iterator__
1.726 +#undef iterator
1.727 +#undef __size_type__
1.728 +
1.729 +#endif /* _STLP_TREE_C */
1.730 +
1.731 +// Local Variables:
1.732 +// mode:C++
1.733 +// End: