Gcc list node Is complex swap implementation complicated?
I am using SGI STL (GCC) as a reference for a custom library, and while digging into I std::list::swap()
came across the following implementation,
Note. This method does not properly handle neighbors.
// namespace std {
// namespace __detail {
void
_List_node_base::swap(_List_node_base& __x, _List_node_base& __y) throw()
{
if ( __x._M_next != &__x )
{
if ( __y._M_next != &__y )
{
// Both __x and __y are not empty.
std::swap(__x._M_next,__y._M_next);
std::swap(__x._M_prev,__y._M_prev);
__x._M_next->_M_prev = __x._M_prev->_M_next = &__x;
__y._M_next->_M_prev = __y._M_prev->_M_next = &__y;
}
else
{
// __x is not empty, __y is empty.
__y._M_next = __x._M_next;
__y._M_prev = __x._M_prev;
__y._M_next->_M_prev = __y._M_prev->_M_next = &__y;
__x._M_next = __x._M_prev = &__x;
}
}
else if ( __y._M_next != &__y )
{
// __x is empty, __y is not empty.
__x._M_next = __y._M_next;
__x._M_prev = __y._M_prev;
__x._M_next->_M_prev = __x._M_prev->_M_next = &__x;
__y._M_next = __y._M_prev = &__y;
}
}
It looks as if it were simplified,
void
_List_node_base::swap(_List_node_base& __x, _List_node_base& __y) throw()
{
_List_node_base* __xnext = __x._M_next;
_List_node_base* __xprev = __x._M_prev;
_List_node_base* __ynext = __y._M_next;
_List_node_base* __yprev = __y._M_prev;
__xnext->_M_prev = __xprev->_M_next = &__y;
__ynext->_M_prev = __yprev->_M_next = &__x;
std::swap(__x._M_next,__y._M_next);
std::swap(__x._M_prev,__y._M_prev);
}
I've tested this for all cases (empty / empty, empty / non-empty, etc.) including __x
and __y
referencing the same node and it seems to work trusting the SGI codebase makes me doubt it.
So my question is: is this correct? And if so, can the longer version be used?
Thank.
+3
source to share