Pregunta

From what I know, a splice is supposed to cut out a piece from one list and put it into another. I don't understand what the following code is for (taken from http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01477_source.html line 112, some standard library I was reading to better understand various data structures):

inline void
__slist_splice_after(_Slist_node_base* __pos, _Slist_node_base* __head)
{
  _Slist_node_base* __before_last = __slist_previous(__head, 0);
  if (__before_last != __head)
    {
  _Slist_node_base* __after = __pos->_M_next;
  __pos->_M_next = __head->_M_next;
  __head->_M_next = 0;
  __before_last->_M_next = __after;
    }
}

It appears that __head gets cut off on both ends and doesn't end up anywhere.

¿Fue útil?

Solución

Look how it's used:

  // Removes all of the elements from the list __x to *this, inserting
  // them immediately after __pos.  __x must not be *this.  Complexity:
  // linear in __x.size().
  void
  splice_after(iterator __pos, slist& __x)
  { __slist_splice_after(__pos._M_node, &__x._M_head); }

And look how _M_head is declared:

  _Slist_node_base _M_head;

So the __head parameter is not a real node, it's a node_base, which means it doesn't contain any data, it's just a pointer to the first element, so it doesn't get "cut off" because it's still in the slist it's a member of, and it ends up exactly where it started.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top