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