2005-05-13 Josh Conner <jconner@apple.com>
[official-gcc.git] / libstdc++-v3 / src / list.cc
blobec94053b41116425d5ebbad7c3275adf2f0c245a
1 // std::list utilities implementation -*- C++ -*-
3 // Copyright (C) 2003 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
32 * Copyright (c) 1994
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
44 * Copyright (c) 1996,1997
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
56 #include <list>
58 namespace _GLIBCXX_STD
60 void
61 _List_node_base::swap(_List_node_base& __x, _List_node_base& __y)
63 if ( __x._M_next != &__x )
65 if ( __y._M_next != &__y )
67 // Both __x and __y are not empty.
68 std::swap(__x._M_next,__y._M_next);
69 std::swap(__x._M_prev,__y._M_prev);
70 __x._M_next->_M_prev = __x._M_prev->_M_next = &__x;
71 __y._M_next->_M_prev = __y._M_prev->_M_next = &__y;
73 else
75 // __x is not empty, __y is empty.
76 __y._M_next = __x._M_next;
77 __y._M_prev = __x._M_prev;
78 __y._M_next->_M_prev = __y._M_prev->_M_next = &__y;
79 __x._M_next = __x._M_prev = &__x;
82 else if ( __y._M_next != &__y )
84 // __x is empty, __y is not empty.
85 __x._M_next = __y._M_next;
86 __x._M_prev = __y._M_prev;
87 __x._M_next->_M_prev = __x._M_prev->_M_next = &__x;
88 __y._M_next = __y._M_prev = &__y;
92 void
93 _List_node_base::transfer(_List_node_base * const __first,
94 _List_node_base * const __last)
96 if (this != __last)
98 // Remove [first, last) from its old position.
99 __last->_M_prev->_M_next = this;
100 __first->_M_prev->_M_next = __last;
101 this->_M_prev->_M_next = __first;
103 // Splice [first, last) into its new position.
104 _List_node_base* const __tmp = this->_M_prev;
105 this->_M_prev = __last->_M_prev;
106 __last->_M_prev = __first->_M_prev;
107 __first->_M_prev = __tmp;
111 void
112 _List_node_base::reverse()
114 _List_node_base* __tmp = this;
117 std::swap(__tmp->_M_next, __tmp->_M_prev);
118 __tmp = __tmp->_M_prev; // Old next node is now prev.
120 while (__tmp != this);
123 void
124 _List_node_base::hook(_List_node_base* const __position)
126 this->_M_next = __position;
127 this->_M_prev = __position->_M_prev;
128 __position->_M_prev->_M_next = this;
129 __position->_M_prev = this;
132 void
133 _List_node_base::unhook()
135 _List_node_base* const __next_node = this->_M_next;
136 _List_node_base* const __prev_node = this->_M_prev;
137 __prev_node->_M_next = __next_node;
138 __next_node->_M_prev = __prev_node;
140 } // namespace std