SGI STL map traversal

I stumbled upon SGIs STL map implementation (stl_map.h), and there it has this piece of code for finding an item:

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);
      __x = _S_right(__x);

  iterator __j = iterator(__y);
  return (__j == end() || _M_key_compare(__k, _S_key(__j._M_node))) ?
     end() : __j;

I must be reading it wrong but it seems to me that it always traverses the whole tree. Why doesnt it stop as soon as it finds the wanted key? Could someone please explain to me how it works?

I would have expected that in case of red-black tree one first finds out whether an items key is the same as the searched-for key, and if not then one steps either to left or right. The above code doesnt seem to perform the first check.



Il Sole 24 Ore – 22.03.2015

Il Sole 24 Ore – 22.03.2015

Italian | PDF | 42 Pages | 51 MB

Category: Tv
Seeds: 0
Leechers: 0
Size: 196.3 MB

More info at