⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _tree.c

📁 MONA是为数不多的C++语言编写的一个很小的操作系统
💻 C
📖 第 1 页 / 共 2 页
字号:
        __z = _M_create_node(__v);    _S_left(__y) = __z;               // also makes _M_leftmost() = __z                                       //    when __y == _M_header    if (__y == this->_M_header._M_data) {      _M_root() = __z;      _M_rightmost() = __z;    }    else if (__y == _M_leftmost())      _M_leftmost() = __z;   // maintain _M_leftmost() pointing to min node  }  else {    __z = _M_create_node(__v);    _S_right(__y) = __z;    if (__y == _M_rightmost())      _M_rightmost() = __z;  // maintain _M_rightmost() pointing to max node  }  _S_parent(__z) = __y;  _S_left(__z) = 0;  _S_right(__z) = 0;  _Rb_global_inst::_Rebalance(__z, this->_M_header._M_data->_M_parent);  ++_M_node_count;  return 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 = this->_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< _Rb_tree_iterator<_Value, _Nonconst_traits<_Value> >, bool> _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc> ::insert_unique(const _Value& __v){  _Link_type __y = this->_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 = iterator(__y);     if (__comp)    if (__j == begin())           return pair<iterator,bool>(_M_insert(/* __x*/ __y, __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);}// Modifications CRP 7/10/00 as noted to improve conformance and// efficiency.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){  if (__position._M_node == this->_M_header._M_data->_M_left) { // begin()    // if the container is empty, fall back on insert_unique.    if (size() <= 0)      return insert_unique(__v).first;    if ( _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      {	bool __comp_pos_v = _M_key_compare( _S_key(__position._M_node), _KeyOfValue()(__v) );		if (__comp_pos_v == false)  // compare > and compare < both false so compare equal	  return __position;	//Below __comp_pos_v == true	// Standard-conformance - does the insertion point fall immediately AFTER	// the hint?	iterator __after = __position;	++__after;	// Check for only one member -- in that case, __position points to itself,	// and attempting to increment will cause an infinite loop.	if (__after._M_node == this->_M_header._M_data)	  // Check guarantees exactly one member, so comparison was already	  // performed and we know the result; skip repeating it in _M_insert	  // by specifying a non-zero fourth argument.	  return _M_insert(0, __position._M_node, __v, __position._M_node);				// All other cases:		// Optimization to catch insert-equivalent -- save comparison results,	// and we get this for free.	if(_M_key_compare( _KeyOfValue()(__v), _S_key(__after._M_node) )) {	  if (_S_right(__position._M_node) == 0)	    return _M_insert(0, __position._M_node, __v, __position._M_node);	  else	    return _M_insert(__after._M_node, __after._M_node, __v);	} else {	    return insert_unique(__v).first;	}      }  } else if (__position._M_node == this->_M_header._M_data) { // end()    if (_M_key_compare(_S_key(_M_rightmost()), _KeyOfValue()(__v)))      // pass along to _M_insert that it can skip comparing      // v, Key ; since compare Key, v was true, compare v, Key must be false.      return _M_insert(0, _M_rightmost(), __v, __position._M_node); // Last argument only needs to be non-null    else      return insert_unique(__v).first;  } else {    iterator __before = __position;    --__before;        bool __comp_v_pos = _M_key_compare(_KeyOfValue()(__v), _S_key(__position._M_node));    if (__comp_v_pos      && _M_key_compare( _S_key(__before._M_node), _KeyOfValue()(__v) )) {      if (_S_right(__before._M_node) == 0)        return _M_insert(0, __before._M_node, __v, __before._M_node); // Last argument only needs to be non-null      else        return _M_insert(__position._M_node, __position._M_node, __v);    // first argument just needs to be non-null     } else      {	// Does the insertion point fall immediately AFTER the hint?	iterator __after = __position;	++__after;		// Optimization to catch equivalent cases and avoid unnecessary comparisons	bool __comp_pos_v = !__comp_v_pos;  // Stored this result earlier	// If the earlier comparison was true, this comparison doesn't need to be	// performed because it must be false.  However, if the earlier comparison	// was false, we need to perform this one because in the equal case, both will	// be false.	if (!__comp_v_pos) __comp_pos_v = _M_key_compare(_S_key(__position._M_node), _KeyOfValue()(__v));		if ( (!__comp_v_pos) // comp_v_pos true implies comp_v_pos false	     && __comp_pos_v	     && (__after._M_node == this->_M_header._M_data ||	        _M_key_compare( _KeyOfValue()(__v), _S_key(__after._M_node) ))) {	  	  if (_S_right(__position._M_node) == 0)	    return _M_insert(0, __position._M_node, __v, __position._M_node);	  else	    return _M_insert(__after._M_node, __after._M_node, __v);	} else {	  // Test for equivalent case	  if (__comp_v_pos == __comp_pos_v)	    return __position;	  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){  if (__position._M_node == this->_M_header._M_data->_M_left) { // begin()    // Check for zero members    if (size() <= 0)        return insert_equal(__v);    if (!_M_key_compare(_S_key(__position._M_node), _KeyOfValue()(__v)))      return _M_insert(__position._M_node, __position._M_node, __v);    else    {      // Check for only one member      if (__position._M_node->_M_left == __position._M_node)        // Unlike insert_unique, can't avoid doing a comparison here.        return _M_insert(0, __position._M_node, __v);                      // All other cases:      // Standard-conformance - does the insertion point fall immediately AFTER      // the hint?      iterator __after = __position;      ++__after;            // Already know that compare(pos, v) must be true!      // Therefore, we want to know if compare(after, v) is false.      // (i.e., we now pos < v, now we want to know if v <= after)      // If not, invalid hint.      if ( __after._M_node==this->_M_header._M_data ||	   !_M_key_compare( _S_key(__after._M_node), _KeyOfValue()(__v) ) ) {        if (_S_right(__position._M_node) == 0)          return _M_insert(0, __position._M_node, __v, __position._M_node);        else          return _M_insert(__after._M_node, __after._M_node, __v);      } else // Invalid hint        return insert_equal(__v);    }  } else if (__position._M_node == this->_M_header._M_data) {// end()    if (!_M_key_compare(_KeyOfValue()(__v), _S_key(_M_rightmost())))      return _M_insert(0, _M_rightmost(), __v, __position._M_node); // Last argument only needs to be non-null    else      return insert_equal(__v);  } else {    iterator __before = __position;    --__before;    // store the result of the comparison between pos and v so    // that we don't have to do it again later.  Note that this reverses the shortcut    // on the if, possibly harming efficiency in comparisons; I think the harm will    // be negligible, and to do what I want to do (save the result of a comparison so    // that it can be re-used) there is no alternative.  Test here is for before <= v <= pos.    bool __comp_pos_v = _M_key_compare(_S_key(__position._M_node), _KeyOfValue()(__v));    if (!__comp_pos_v        && !_M_key_compare(_KeyOfValue()(__v), _S_key(__before._M_node))) {      if (_S_right(__before._M_node) == 0)        return _M_insert(0, __before._M_node, __v, __before._M_node); // Last argument only needs to be non-null      else        return _M_insert(__position._M_node, __position._M_node, __v);    } else  {      // Does the insertion point fall immediately AFTER the hint?      // Test for pos < v <= after      iterator __after = __position;      ++__after;            if (__comp_pos_v	  && ( __after._M_node==this->_M_header._M_data 	       || !_M_key_compare( _S_key(__after._M_node), _KeyOfValue()(__v) ) ) ) {        if (_S_right(__position._M_node) == 0)          return _M_insert(0, __position._M_node, __v, __position._M_node);        else          return _M_insert(__after._M_node, __after._M_node, __v);      } else // Invalid hint        return insert_equal(__v);    }  }}template <class _Key, class _Value, class _KeyOfValue, class _Compare, class _Alloc> _Rb_tree_node<_Value>* _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc> ::_M_copy(_Rb_tree_node<_Value>* __x, _Rb_tree_node<_Value>* __p){                        // structural copy.  __x and __p must be non-null.  _Link_type __top = _M_clone_node(__x);  __top->_M_parent = __p;    _STLP_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);    }  }  _STLP_UNWIND(_M_erase(__top));  return __top;}// this has to stay out-of-line : it's recursivetemplate <class _Key, class _Value, class _KeyOfValue,           class _Compare, class _Alloc> void _Rb_tree<_Key,_Value,_KeyOfValue,  _Compare,_Alloc>::_M_erase(_Rb_tree_node<_Value>* __x){                                // erase without rebalancing  while (__x != 0) {    _M_erase(_S_right(__x));    _Link_type __y = _S_left(__x);    _STLP_STD::_Destroy(&__x->_M_value_field);    this->_M_header.deallocate(__x,1);    __x = __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 = distance(__p.first, __p.second);  return __n;}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() && this->_M_header._M_data->_M_left == this->_M_header._M_data      && this->_M_header._M_data->_M_right == this->_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;}_STLP_END_NAMESPACE# undef __iterator__        # undef iterator# undef __size_type__  #endif /*  _STLP_TREE_C */// Local Variables:// mode:C++// End:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -