📄 stl_tree.h
字号:
_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 = 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 _Val, class _KeyOfValue, class _Compare, class _Alloc>typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc> ::insert_unique(iterator __position, const _Val& __v){ if (__position._M_node == _M_header->_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) { // 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 _Val, class _KeyOfValue, class _Compare, class _Alloc>typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc> ::insert_equal(iterator __position, const _Val& __v){ if (__position._M_node == _M_header->_M_left) { // begin() if (size() > 0 && !_M_key_compare(_S_key(__position._M_node), _KeyOfValue()(__v))) 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) {// 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); }}#ifdef __STL_MEMBER_TEMPLATES template <class _Key, class _Val, class _KoV, class _Cmp, class _Alloc> template<class _II>void _Rb_tree<_Key,_Val,_KoV,_Cmp,_Alloc> ::insert_equal(_II __first, _II __last){ for ( ; __first != __last; ++__first) insert_equal(*__first);}template <class _Key, class _Val, class _KoV, class _Cmp, class _Alloc> template<class _II>void _Rb_tree<_Key,_Val,_KoV,_Cmp,_Alloc> ::insert_unique(_II __first, _II __last) { for ( ; __first != __last; ++__first) insert_unique(*__first);}#else /* __STL_MEMBER_TEMPLATES */template <class _Key, class _Val, class _KoV, class _Cmp, class _Alloc>void_Rb_tree<_Key,_Val,_KoV,_Cmp,_Alloc> ::insert_equal(const _Val* __first, const _Val* __last){ for ( ; __first != __last; ++__first) insert_equal(*__first);}template <class _Key, class _Val, class _KoV, class _Cmp, class _Alloc>void_Rb_tree<_Key,_Val,_KoV,_Cmp,_Alloc> ::insert_equal(const_iterator __first, const_iterator __last){ for ( ; __first != __last; ++__first) insert_equal(*__first);}template <class _Key, class _Val, class _KoV, class _Cmp, class _Alloc>void _Rb_tree<_Key,_Val,_KoV,_Cmp,_Alloc> ::insert_unique(const _Val* __first, const _Val* __last){ for ( ; __first != __last; ++__first) insert_unique(*__first);}template <class _Key, class _Val, class _KoV, class _Cmp, class _Alloc>void _Rb_tree<_Key,_Val,_KoV,_Cmp,_Alloc> ::insert_unique(const_iterator __first, const_iterator __last){ for ( ; __first != __last; ++__first) insert_unique(*__first);}#endif /* __STL_MEMBER_TEMPLATES */ template <class _Key, class _Value, class _KeyOfValue, class _Compare, class _Alloc>inline void _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc> ::erase(iterator __position){ _Link_type __y = (_Link_type) _Rb_tree_rebalance_for_erase(__position._M_node, _M_header->_M_parent, _M_header->_M_left, _M_header->_M_right); destroy_node(__y); --_M_node_count;}template <class _Key, class _Value, class _KeyOfValue, class _Compare, class _Alloc>typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_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 _Val, class _KoV, class _Compare, class _Alloc>typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Link_type _Rb_tree<_Key,_Val,_KoV,_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>typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::iterator _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::find(const _Key& __k){ _Link_type __y = _M_header; // 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 = iterator(__y); return (__j == end() || _M_key_compare(__k, _S_key(__j._M_node))) ? end() : __j;}template <class _Key, class _Value, class _KeyOfValue, class _Compare, class _Alloc>typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::const_iterator _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::find(const _Key& __k) const{ _Link_type __y = _M_header; /* 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 = const_iterator(__y); return (__j == end() || _M_key_compare(__k, _S_key(__j._M_node))) ? end() : __j;}template <class _Key, class _Value, class _KeyOfValue, class _Compare, class _Alloc>typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_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>typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::iterator _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc> ::lower_bound(const _Key& __k){ _Link_type __y = _M_header; /* 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 iterator(__y);}template <class _Key, class _Value, class _KeyOfValue, class _Compare, class _Alloc>typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::const_iterator _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc> ::lower_bound(const _Key& __k) const{ _Link_type __y = _M_header; /* 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 const_iterator(__y);}template <class _Key, class _Value, class _KeyOfValue, class _Compare, class _Alloc>typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::iterator _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc> ::upper_bound(const _Key& __k){ _Link_type __y = _M_header; /* 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 iterator(__y);}template <class _Key, class _Value, class _KeyOfValue, class _Compare, class _Alloc>typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::const_iterator _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc> ::upper_bound(const _Key& __k) const{ _Link_type __y = _M_header; /* 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 const_iterator(__y);}template <class _Key, class _Value, class _KeyOfValue, class _Compare, class _Alloc>inline pair<typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::iterator, typename _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::iterator>_Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc> ::equal_range(const _Key& __k){ return pair<iterator, iterator>(lower_bound(__k), upper_bound(__k));}template <class _Key, class _Value, class _KoV, class _Compare, class _Alloc>inline pair<typename _Rb_tree<_Key, _Value, _KoV, _Compare, _Alloc>::const_iterator, typename _Rb_tree<_Key, _Value, _KoV, _Compare, _Alloc>::const_iterator>_Rb_tree<_Key, _Value, _KoV, _Compare, _Alloc> ::equal_range(const _Key& __k) const{ return pair<const_iterator,const_iterator>(lower_bound(__k), upper_bound(__k));}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_left == _M_header && _M_header->_M_right == _M_header; 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;}// Class rb_tree is not part of the C++ standard. It is provided for// compatibility with the HP STL.template <class _Key, class _Value, class _KeyOfValue, class _Compare, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Value) >struct rb_tree : public _Rb_tree<_Key, _Value, _KeyOfValue, _Compare, _Alloc>{ typedef _Rb_tree<_Key, _Value, _KeyOfValue, _Compare, _Alloc> _Base; typedef typename _Base::allocator_type allocator_type; rb_tree(const _Compare& __comp = _Compare(), const allocator_type& __a = allocator_type()) : _Base(__comp, __a) {} ~rb_tree() {}};#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)#pragma reset woff 1375#endif__STL_END_NAMESPACE #endif /* __SGI_STL_INTERNAL_TREE_H */// Local Variables:// mode:C++// End:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -