?? stl_tree.c
字號(hào):
_Rb_global_inst::_Rebalance(__z, _M_header._M_data->_M_parent);
++_M_node_count;
return _Make_iterator(__z);
}
template <class _Key, class _Value, class _KeyOfValue,
class _Compare, class _Alloc>
__iterator__
_Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
::insert_equal(const __Value__& __v)
{
_Link_type __y = _M_header._M_data;
_Link_type __x = _M_root();
while (__x != 0) {
__y = __x;
__x = _M_key_compare(_KeyOfValue()(__v), _S_key(__x)) ?
_S_left(__x) : _S_right(__x);
}
return _M_insert(__x, __y, __v);
}
template <class _Key, class _Value, class _KeyOfValue,
class _Compare, class _Alloc>
pair<__iterator__, bool>
_Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
::insert_unique(const __Value__& __v)
{
_Link_type __y = _M_header._M_data;
_Link_type __x = _M_root();
bool __comp = true;
while (__x != 0) {
__y = __x;
__comp = _M_key_compare(_KeyOfValue()(__v), _S_key(__x));
__x = __comp ? _S_left(__x) : _S_right(__x);
}
iterator __j = _Make_iterator(__y);
if (__comp)
if (__j == begin())
return pair<iterator,bool>(_M_insert(__x, __y, __v), true);
else
--__j;
if (_M_key_compare(_S_key(__j._M_node), _KeyOfValue()(__v)))
return pair<iterator,bool>(_M_insert(__x, __y, __v), true);
return pair<iterator,bool>(__j, false);
}
template <class _Key, class _Value, class _KeyOfValue,
class _Compare, class _Alloc>
__iterator__
_Rb_tree<_Key, _Value, _KeyOfValue, _Compare, _Alloc>
::insert_unique(__iterator__ __position, const __Value__& __v)
{
__stl_debug_check(__check_if_owner(&_M_iter_list,__position));
if (__position._M_node == _M_header._M_data->_M_left) { // begin()
if (size() > 0 &&
_M_key_compare(_KeyOfValue()(__v), _S_key(__position._M_node)))
return _M_insert(__position._M_node, __position._M_node, __v);
// first argument just needs to be non-null
else
return insert_unique(__v).first;
} else if (__position._M_node == _M_header._M_data) { // end()
if (_M_key_compare(_S_key(_M_rightmost()), _KeyOfValue()(__v)))
return _M_insert(0, _M_rightmost(), __v);
else
return insert_unique(__v).first;
} else {
iterator __before = __position;
--__before;
if (_M_key_compare(_S_key(__before._M_node), _KeyOfValue()(__v))
&& _M_key_compare(_KeyOfValue()(__v), _S_key(__position._M_node))) {
if (_S_right(__before._M_node) == 0)
return _M_insert(0, __before._M_node, __v);
else
return _M_insert(__position._M_node, __position._M_node, __v);
// first argument just needs to be non-null
} else
return insert_unique(__v).first;
}
}
template <class _Key, class _Value, class _KeyOfValue,
class _Compare, class _Alloc>
__iterator__
_Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
::insert_equal(__iterator__ __position, const __Value__& __v)
{
__stl_debug_check(__check_if_owner(&_M_iter_list,__position));
if (__position._M_node == _M_header._M_data->_M_left) { // begin()
if (size() > 0 &&
_M_key_compare(_KeyOfValue()(__v), _S_key(__position._M_node)))
return _M_insert(__position._M_node, __position._M_node, __v);
// first argument just needs to be non-null
else
return insert_equal(__v);
} else if (__position._M_node == _M_header._M_data) {// end()
if (!_M_key_compare(_KeyOfValue()(__v), _S_key(_M_rightmost())))
return _M_insert(0, _M_rightmost(), __v);
else
return insert_equal(__v);
} else {
iterator __before = __position;
--__before;
if (!_M_key_compare(_KeyOfValue()(__v), _S_key(__before._M_node))
&& !_M_key_compare(_S_key(__position._M_node), _KeyOfValue()(__v))) {
if (_S_right(__before._M_node) == 0)
return _M_insert(0, __before._M_node, __v);
else
return _M_insert(__position._M_node, __position._M_node, __v);
// first argument just needs to be non-null
} else
return insert_equal(__v);
}
}
template <class _Key, class _Value, class _KeyOfValue,
class _Compare, class _Alloc>
__size_type__
_Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::erase(const __Key__& __x)
{
pair<iterator,iterator> __p = equal_range(__x);
size_type __n = 0;
distance(__p.first, __p.second, __n);
erase(__p.first, __p.second);
return __n;
}
template <class _Key, class _Value, class _KeyOfValue, class _Compare, class _Alloc>
__Link_type__
_Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
::_M_copy(__Link_type__ __x, __Link_type__ __p)
{
// structural copy. __x and __p must be non-null.
_Link_type __top = _M_clone_node(__x);
__top->_M_parent = __p;
__STL_TRY {
if (__x->_M_right)
__top->_M_right = _M_copy(_S_right(__x), __top);
__p = __top;
__x = _S_left(__x);
while (__x != 0) {
_Link_type __y = _M_clone_node(__x);
__p->_M_left = __y;
__y->_M_parent = __p;
if (__x->_M_right)
__y->_M_right = _M_copy(_S_right(__x), __y);
__p = __y;
__x = _S_left(__x);
}
}
__STL_UNWIND(_M_erase(__top));
return __top;
}
template <class _Key, class _Value, class _KeyOfValue,
class _Compare, class _Alloc>
void
_Rb_tree<_Key,_Value,_KeyOfValue,
_Compare,_Alloc>::_M_erase(__Link_type__ __x)
{
// erase without rebalancing
while (__x != 0) {
_M_erase(_S_right(__x));
_Link_type __y = _S_left(__x);
destroy_node(__x);
__x = __y;
}
}
template <class _Key, class _Value, class _KeyOfValue,
class _Compare, class _Alloc>
void _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
::erase(__iterator__ __first, __iterator__ __last)
{
if (__first == begin() && __last == end())
clear();
else
while (__first != __last) erase(__first++);
}
template <class _Key, class _Value, class _KeyOfValue,
class _Compare, class _Alloc>
void _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
::erase(const __Key__* __first, const __Key__* __last)
{
while (__first != __last) erase(*__first++);
}
template <class _Key, class _Value, class _KeyOfValue,
class _Compare, class _Alloc>
__iterator__
_Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::find(const __Key__& __k)
{
_Link_type __y = _M_header._M_data; // Last node which is not less than __k.
_Link_type __x = _M_root(); // Current node.
while (__x != 0)
if (!_M_key_compare(_S_key(__x), __k))
__y = __x, __x = _S_left(__x);
else
__x = _S_right(__x);
iterator __j = _Make_iterator(__y);
return (__j == end() || _M_key_compare(__k, _S_key(__j._M_node))) ?
end() : __j;
// FBP ; may need this return make_iterator((y == header || key_compare(k, key(y))) ? header : y);
}
template <class _Key, class _Value, class _KeyOfValue,
class _Compare, class _Alloc>
__const_iterator__
_Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::find(const __Key__& __k) const
{
_Link_type __y = _M_header._M_data; /* Last node which is not less than __k. */
_Link_type __x = _M_root(); /* Current node. */
while (__x != 0) {
if (!_M_key_compare(_S_key(__x), __k))
__y = __x, __x = _S_left(__x);
else
__x = _S_right(__x);
}
const_iterator __j = _Make_const_iterator(__y);
return (__j == end() || _M_key_compare(__k, _S_key(__j._M_node))) ?
end() : __j;
// FBP : may need this return make_const_iterator((y == header || key_compare(k, key(y))) ? header : y);
}
template <class _Key, class _Value, class _KeyOfValue,
class _Compare, class _Alloc>
__size_type__
_Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
::count(const __Key__& __k) const
{
pair<const_iterator, const_iterator> __p = equal_range(__k);
size_type __n = 0;
distance(__p.first, __p.second, __n);
return __n;
}
template <class _Key, class _Value, class _KeyOfValue,
class _Compare, class _Alloc>
__iterator__
_Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
::lower_bound(const __Key__& __k)
{
_Link_type __y = _M_header._M_data; /* Last node which is not less than __k. */
_Link_type __x = _M_root(); /* Current node. */
while (__x != 0)
if (!_M_key_compare(_S_key(__x), __k))
__y = __x, __x = _S_left(__x);
else
__x = _S_right(__x);
return _Make_iterator(__y);
}
template <class _Key, class _Value, class _KeyOfValue,
class _Compare, class _Alloc>
__const_iterator__
_Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
::lower_bound(const __Key__& __k) const
{
_Link_type __y = _M_header._M_data; /* Last node which is not less than __k. */
_Link_type __x = _M_root(); /* Current node. */
while (__x != 0)
if (!_M_key_compare(_S_key(__x), __k))
__y = __x, __x = _S_left(__x);
else
__x = _S_right(__x);
return _Make_const_iterator(__y);
}
template <class _Key, class _Value, class _KeyOfValue,
class _Compare, class _Alloc>
__iterator__
_Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
::upper_bound(const __Key__& __k)
{
_Link_type __y = _M_header._M_data; /* Last node which is greater than __k. */
_Link_type __x = _M_root(); /* Current node. */
while (__x != 0)
if (_M_key_compare(__k, _S_key(__x)))
__y = __x, __x = _S_left(__x);
else
__x = _S_right(__x);
return _Make_iterator(__y);
}
template <class _Key, class _Value, class _KeyOfValue,
class _Compare, class _Alloc>
__const_iterator__
_Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>
::upper_bound(const __Key__& __k) const
{
_Link_type __y = _M_header._M_data; /* Last node which is greater than __k. */
_Link_type __x = _M_root(); /* Current node. */
while (__x != 0)
if (_M_key_compare(__k, _S_key(__x)))
__y = __x, __x = _S_left(__x);
else
__x = _S_right(__x);
return _Make_const_iterator(__y);
}
inline int
__black_count(_Rb_tree_node_base* __node, _Rb_tree_node_base* __root)
{
if (__node == 0)
return 0;
else {
int __bc = __node->_M_color == _S_rb_tree_black ? 1 : 0;
if (__node == __root)
return __bc;
else
return __bc + __black_count(__node->_M_parent, __root);
}
}
template <class _Key, class _Value, class _KeyOfValue,
class _Compare, class _Alloc>
bool _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const
{
if (_M_node_count == 0 || begin() == end())
return _M_node_count == 0 && begin() == end() &&
_M_header._M_data->_M_left == _M_header._M_data && _M_header._M_data->_M_right == _M_header._M_data;
int __len = __black_count(_M_leftmost(), _M_root());
for (const_iterator __it = begin(); __it != end(); ++__it) {
_Link_type __x = (_Link_type) __it._M_node;
_Link_type __L = _S_left(__x);
_Link_type __R = _S_right(__x);
if (__x->_M_color == _S_rb_tree_red)
if ((__L && __L->_M_color == _S_rb_tree_red) ||
(__R && __R->_M_color == _S_rb_tree_red))
return false;
if (__L && _M_key_compare(_S_key(__x), _S_key(__L)))
return false;
if (__R && _M_key_compare(_S_key(__R), _S_key(__x)))
return false;
if (!__L && !__R && __black_count(__x, _M_root()) != __len)
return false;
}
if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
return false;
if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
return false;
return true;
}
__STL_END_NAMESPACE
# undef _Make_iterator
# undef _Make_const_iterator
# undef __iterator__
# undef __const_iterator__
# undef __size_type__
# undef __Link_type__
# undef __Base_ptr__
# undef __Value__
# undef __Key__
#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
#pragma reset woff 1375
#endif
#endif /* __STL_TREE_C */
// Local Variables:
// mode:C++
// End:
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -