Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22004

Why does STL function use node's color to calculate std::map node predecessor

$
0
0

I was looking through libstdc++'s implementation of std::map and noticed that iterator increment and decrement functions are not entirely symmetrical. local_Rb_tree_decrement function (aka predecessor) has an additional clause checking for the color of the node:

static _Rb_tree_node_base*
local_Rb_tree_decrement(_Rb_tree_node_base* __x) throw ()
{
  if (__x->_M_color == _S_red
      && __x->_M_parent->_M_parent == __x)
    __x = __x->_M_right;
  else if (__x->_M_left != 0)
    {
      _Rb_tree_node_base* __y = __x->_M_left;
      while (__y->_M_right != 0)
        __y = __y->_M_right;
      __x = __y;
    }
  else
    {
      _Rb_tree_node_base* __y = __x->_M_parent;
      while (__x == __y->_M_left)
        {
          __x = __y;
          __y = __y->_M_parent;
        }
      __x = __y;
    }
  return __x;
}

What is the purpose of the first case and why does the node's color affect tree traversal in any way? And why is it different from local_Rb_tree_increment?

if (__x->_M_color == _S_red
    && __x->_M_parent->_M_parent == __x)
  __x = __x->_M_right;

Thanks in advance for your comments and explanation!


Viewing all articles
Browse latest Browse all 22004

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>