# list.cc

    00001 // std::list utilities implementation -*- C++ -*-
    00002
    00003 // Copyright (C) 2003 Free Software Foundation, Inc.
    00004 //
    00005 // This file is part of the GNU ISO C++ Library.  This library is free
    00006 // software; you can redistribute it and/or modify it under the
    00007 // terms of the GNU General Public License as published by the
    00008 // Free Software Foundation; either version 2, or (at your option)
    00009 // any later version.
    00010
    00011 // This library is distributed in the hope that it will be useful,
    00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00014 // GNU General Public License for more details.
    00015
    00016 // You should have received a copy of the GNU General Public License along
    00017 // with this library; see the file COPYING.  If not, write to the Free
    00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    00019 // USA.
    00020
    00021 // As a special exception, you may use this file as part of a free software
    00022 // library without restriction.  Specifically, if other files instantiate
    00023 // templates or use macros or inline functions from this file, or you compile
    00024 // this file and link it with other files to produce an executable, this
    00025 // file does not by itself cause the resulting executable to be covered by
    00026 // the GNU General Public License.  This exception does not however
    00027 // invalidate any other reasons why the executable file might be covered by
    00028 // the GNU General Public License.
    00029
    00030 /*
    00031  *
    00032  * Copyright (c) 1994
    00033  * Hewlett-Packard Company
    00034  *
    00035  * Permission to use, copy, modify, distribute and sell this software
    00036  * and its documentation for any purpose is hereby granted without fee,
    00037  * provided that the above copyright notice appear in all copies and
    00038  * that both that copyright notice and this permission notice appear
    00039  * in supporting documentation.  Hewlett-Packard Company makes no
    00040  * representations about the suitability of this software for any
    00041  * purpose.  It is provided "as is" without express or implied warranty.
    00042  *
    00043  *
    00044  * Copyright (c) 1996,1997
    00045  * Silicon Graphics Computer Systems, Inc.
    00046  *
    00047  * Permission to use, copy, modify, distribute and sell this software
    00048  * and its documentation for any purpose is hereby granted without fee,
    00049  * provided that the above copyright notice appear in all copies and
    00050  * that both that copyright notice and this permission notice appear
    00051  * in supporting documentation.  Silicon Graphics makes no
    00052  * representations about the suitability of this software for any
    00053  * purpose.  It is provided "as is" without express or implied warranty.
    00054  */
    00055
    00056 #include <list>
    00057
    00058 namespace _GLIBCXX_STD
    00059 {
    00060   void
    00061   _List_node_base::swap(_List_node_base& __x, _List_node_base& __y)
    00062   {
    00063     if ( __x._M_next != &__x )
    00064     {
    00065       if ( __y._M_next != &__y )
    00066       {
    00067         // Both __x and __y are not empty.
    00068         std::swap(__x._M_next,__y._M_next);
    00069         std::swap(__x._M_prev,__y._M_prev);
    00070         __x._M_next->_M_prev = __x._M_prev->_M_next = &__x;
    00071         __y._M_next->_M_prev = __y._M_prev->_M_next = &__y;
    00072       }
    00073       else
    00074       {
    00075         // __x is not empty, __y is empty.
    00076         __y._M_next = __x._M_next;
    00077         __y._M_prev = __x._M_prev;
    00078         __y._M_next->_M_prev = __y._M_prev->_M_next = &__y;
    00079         __x._M_next = __x._M_prev = &__x;
    00080       }
    00081     }
    00082     else if ( __y._M_next != &__y )
    00083     {
    00084       // __x is empty, __y is not empty.
    00085       __x._M_next = __y._M_next;
    00086       __x._M_prev = __y._M_prev;
    00087       __x._M_next->_M_prev = __x._M_prev->_M_next = &__x;
    00088       __y._M_next = __y._M_prev = &__y;
    00089     }
    00090   }
    00091
    00092   void
    00093   _List_node_base::transfer(_List_node_base * const __first,
    00094                             _List_node_base * const __last)
    00095   {
    00096     if (this != __last)
    00097     {
    00098       // Remove [first, last) from its old position.
    00099       __last->_M_prev->_M_next  = this;
    00100       __first->_M_prev->_M_next = __last;
    00101       this->_M_prev->_M_next    = __first;
    00102
    00103       // Splice [first, last) into its new position.
    00104       _List_node_base* const __tmp = this->_M_prev;
    00105       this->_M_prev                = __last->_M_prev;
    00106       __last->_M_prev              = __first->_M_prev;
    00107       __first->_M_prev             = __tmp;
    00108     }
    00109   }
    00110
    00111   void
    00112   _List_node_base::reverse()
    00113   {
    00114     _List_node_base* __tmp = this;
    00115     do
    00116     {
    00117       std::swap(__tmp->_M_next, __tmp->_M_prev);
    00118       __tmp = __tmp->_M_prev;     // Old next node is now prev.
    00119     }
    00120     while (__tmp != this);
    00121   }
    00122
    00123   void
    00124   _List_node_base::hook(_List_node_base* const __position)
    00125   {
    00126     this->_M_next = __position;
    00127     this->_M_prev = __position->_M_prev;
    00128     __position->_M_prev->_M_next = this;
    00129     __position->_M_prev = this;
    00130   }
    00131
    00132   void
    00133   _List_node_base::unhook()
    00134   {
    00135     _List_node_base* const __next_node = this->_M_next;
    00136     _List_node_base* const __prev_node = this->_M_prev;
    00137     __prev_node->_M_next = __next_node;
    00138     __next_node->_M_prev = __prev_node;
    00139   }
    00140 } // namespace std
    00141