Import gcc-4.4.1
[dragonfly.git] / contrib / gcc-4.4 / libstdc++-v3 / src / list.cc
1 // std::list utilities implementation -*- C++ -*-
2
3 // Copyright (C) 2003, 2005, 2009 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 3, or (at your option)
9 // any later version.
10
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.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /*
26  *
27  * Copyright (c) 1994
28  * Hewlett-Packard Company
29  *
30  * Permission to use, copy, modify, distribute and sell this software
31  * and its documentation for any purpose is hereby granted without fee,
32  * provided that the above copyright notice appear in all copies and
33  * that both that copyright notice and this permission notice appear
34  * in supporting documentation.  Hewlett-Packard Company makes no
35  * representations about the suitability of this software for any
36  * purpose.  It is provided "as is" without express or implied warranty.
37  *
38  *
39  * Copyright (c) 1996,1997
40  * Silicon Graphics Computer Systems, Inc.
41  *
42  * Permission to use, copy, modify, distribute and sell this software
43  * and its documentation for any purpose is hereby granted without fee,
44  * provided that the above copyright notice appear in all copies and
45  * that both that copyright notice and this permission notice appear
46  * in supporting documentation.  Silicon Graphics makes no
47  * representations about the suitability of this software for any
48  * purpose.  It is provided "as is" without express or implied warranty.
49  */
50
51 #include <list>
52
53 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
54
55   void
56   _List_node_base::swap(_List_node_base& __x, _List_node_base& __y)
57   {
58     if ( __x._M_next != &__x )
59     {
60       if ( __y._M_next != &__y )
61       {
62         // Both __x and __y are not empty.
63         std::swap(__x._M_next,__y._M_next);
64         std::swap(__x._M_prev,__y._M_prev);
65         __x._M_next->_M_prev = __x._M_prev->_M_next = &__x;
66         __y._M_next->_M_prev = __y._M_prev->_M_next = &__y;
67       }
68       else
69       {
70         // __x is not empty, __y is empty.
71         __y._M_next = __x._M_next;
72         __y._M_prev = __x._M_prev;
73         __y._M_next->_M_prev = __y._M_prev->_M_next = &__y;        
74         __x._M_next = __x._M_prev = &__x;
75       }
76     }
77     else if ( __y._M_next != &__y )
78     {
79       // __x is empty, __y is not empty.
80       __x._M_next = __y._M_next;
81       __x._M_prev = __y._M_prev;
82       __x._M_next->_M_prev = __x._M_prev->_M_next = &__x;      
83       __y._M_next = __y._M_prev = &__y;
84     }
85   }
86
87   void
88   _List_node_base::transfer(_List_node_base * const __first,
89                             _List_node_base * const __last)
90   {
91     if (this != __last)
92     {
93       // Remove [first, last) from its old position.
94       __last->_M_prev->_M_next  = this;
95       __first->_M_prev->_M_next = __last;
96       this->_M_prev->_M_next    = __first;
97   
98       // Splice [first, last) into its new position.
99       _List_node_base* const __tmp = this->_M_prev;
100       this->_M_prev                = __last->_M_prev;
101       __last->_M_prev              = __first->_M_prev;
102       __first->_M_prev             = __tmp;
103     }
104   }
105
106   void
107   _List_node_base::reverse()
108   {
109     _List_node_base* __tmp = this;
110     do
111     {
112       std::swap(__tmp->_M_next, __tmp->_M_prev);
113
114       // Old next node is now prev.
115       __tmp = __tmp->_M_prev;     
116     } 
117     while (__tmp != this);
118   }
119
120   void
121   _List_node_base::hook(_List_node_base* const __position)
122   {
123     this->_M_next = __position;
124     this->_M_prev = __position->_M_prev;
125     __position->_M_prev->_M_next = this;
126     __position->_M_prev = this;
127   }
128
129   void
130   _List_node_base::unhook()
131   {
132     _List_node_base* const __next_node = this->_M_next;
133     _List_node_base* const __prev_node = this->_M_prev;
134     __prev_node->_M_next = __next_node;
135     __next_node->_M_prev = __prev_node;
136   }
137
138 _GLIBCXX_END_NESTED_NAMESPACE