1132720Skan// std::list utilities implementation -*- C++ -*-
2132720Skan
3169691Skan// Copyright (C) 2003, 2005 Free Software Foundation, Inc.
4132720Skan//
5132720Skan// This file is part of the GNU ISO C++ Library.  This library is free
6132720Skan// software; you can redistribute it and/or modify it under the
7132720Skan// terms of the GNU General Public License as published by the
8132720Skan// Free Software Foundation; either version 2, or (at your option)
9132720Skan// any later version.
10132720Skan
11132720Skan// This library is distributed in the hope that it will be useful,
12132720Skan// but WITHOUT ANY WARRANTY; without even the implied warranty of
13132720Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14132720Skan// GNU General Public License for more details.
15132720Skan
16132720Skan// You should have received a copy of the GNU General Public License along
17132720Skan// with this library; see the file COPYING.  If not, write to the Free
18169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19132720Skan// USA.
20132720Skan
21132720Skan// As a special exception, you may use this file as part of a free software
22132720Skan// library without restriction.  Specifically, if other files instantiate
23132720Skan// templates or use macros or inline functions from this file, or you compile
24132720Skan// this file and link it with other files to produce an executable, this
25132720Skan// file does not by itself cause the resulting executable to be covered by
26132720Skan// the GNU General Public License.  This exception does not however
27132720Skan// invalidate any other reasons why the executable file might be covered by
28132720Skan// the GNU General Public License.
29132720Skan
30132720Skan/*
31132720Skan *
32132720Skan * Copyright (c) 1994
33132720Skan * Hewlett-Packard Company
34132720Skan *
35132720Skan * Permission to use, copy, modify, distribute and sell this software
36132720Skan * and its documentation for any purpose is hereby granted without fee,
37132720Skan * provided that the above copyright notice appear in all copies and
38132720Skan * that both that copyright notice and this permission notice appear
39132720Skan * in supporting documentation.  Hewlett-Packard Company makes no
40132720Skan * representations about the suitability of this software for any
41132720Skan * purpose.  It is provided "as is" without express or implied warranty.
42132720Skan *
43132720Skan *
44132720Skan * Copyright (c) 1996,1997
45132720Skan * Silicon Graphics Computer Systems, Inc.
46132720Skan *
47132720Skan * Permission to use, copy, modify, distribute and sell this software
48132720Skan * and its documentation for any purpose is hereby granted without fee,
49132720Skan * provided that the above copyright notice appear in all copies and
50132720Skan * that both that copyright notice and this permission notice appear
51132720Skan * in supporting documentation.  Silicon Graphics makes no
52132720Skan * representations about the suitability of this software for any
53132720Skan * purpose.  It is provided "as is" without express or implied warranty.
54132720Skan */
55132720Skan
56132720Skan#include <list>
57132720Skan
58169691Skan_GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD)
59169691Skan
60132720Skan  void
61132720Skan  _List_node_base::swap(_List_node_base& __x, _List_node_base& __y)
62132720Skan  {
63132720Skan    if ( __x._M_next != &__x )
64132720Skan    {
65132720Skan      if ( __y._M_next != &__y )
66132720Skan      {
67132720Skan        // Both __x and __y are not empty.
68132720Skan        std::swap(__x._M_next,__y._M_next);
69132720Skan        std::swap(__x._M_prev,__y._M_prev);
70132720Skan        __x._M_next->_M_prev = __x._M_prev->_M_next = &__x;
71132720Skan        __y._M_next->_M_prev = __y._M_prev->_M_next = &__y;
72132720Skan      }
73132720Skan      else
74132720Skan      {
75132720Skan        // __x is not empty, __y is empty.
76132720Skan        __y._M_next = __x._M_next;
77132720Skan        __y._M_prev = __x._M_prev;
78132720Skan        __y._M_next->_M_prev = __y._M_prev->_M_next = &__y;
79132720Skan        __x._M_next = __x._M_prev = &__x;
80132720Skan      }
81132720Skan    }
82132720Skan    else if ( __y._M_next != &__y )
83132720Skan    {
84132720Skan      // __x is empty, __y is not empty.
85132720Skan      __x._M_next = __y._M_next;
86132720Skan      __x._M_prev = __y._M_prev;
87132720Skan      __x._M_next->_M_prev = __x._M_prev->_M_next = &__x;
88132720Skan      __y._M_next = __y._M_prev = &__y;
89132720Skan    }
90132720Skan  }
91132720Skan
92132720Skan  void
93132720Skan  _List_node_base::transfer(_List_node_base * const __first,
94132720Skan                            _List_node_base * const __last)
95132720Skan  {
96132720Skan    if (this != __last)
97132720Skan    {
98132720Skan      // Remove [first, last) from its old position.
99132720Skan      __last->_M_prev->_M_next  = this;
100132720Skan      __first->_M_prev->_M_next = __last;
101132720Skan      this->_M_prev->_M_next    = __first;
102132720Skan
103132720Skan      // Splice [first, last) into its new position.
104132720Skan      _List_node_base* const __tmp = this->_M_prev;
105132720Skan      this->_M_prev                = __last->_M_prev;
106132720Skan      __last->_M_prev              = __first->_M_prev;
107132720Skan      __first->_M_prev             = __tmp;
108132720Skan    }
109132720Skan  }
110132720Skan
111132720Skan  void
112132720Skan  _List_node_base::reverse()
113132720Skan  {
114132720Skan    _List_node_base* __tmp = this;
115132720Skan    do
116132720Skan    {
117132720Skan      std::swap(__tmp->_M_next, __tmp->_M_prev);
118132720Skan      __tmp = __tmp->_M_prev;     // Old next node is now prev.
119132720Skan    }
120132720Skan    while (__tmp != this);
121132720Skan  }
122132720Skan
123132720Skan  void
124132720Skan  _List_node_base::hook(_List_node_base* const __position)
125132720Skan  {
126132720Skan    this->_M_next = __position;
127132720Skan    this->_M_prev = __position->_M_prev;
128132720Skan    __position->_M_prev->_M_next = this;
129132720Skan    __position->_M_prev = this;
130132720Skan  }
131132720Skan
132132720Skan  void
133132720Skan  _List_node_base::unhook()
134132720Skan  {
135132720Skan    _List_node_base* const __next_node = this->_M_next;
136132720Skan    _List_node_base* const __prev_node = this->_M_prev;
137132720Skan    __prev_node->_M_next = __next_node;
138132720Skan    __next_node->_M_prev = __prev_node;
139132720Skan  }
140132720Skan
141169691Skan_GLIBCXX_END_NESTED_NAMESPACE
142