1 // List implementation (out of line) -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
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)
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,
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.
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.
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
66 template<typename _Tp, typename _Alloc>
68 _List_base<_Tp,_Alloc>::
71 typedef _List_node<_Tp> _Node;
72 _Node* __cur = static_cast<_Node*>(this->_M_node._M_next);
73 while (__cur != &this->_M_node)
76 __cur = static_cast<_Node*>(__cur->_M_next);
77 std::_Destroy(&__tmp->_M_data);
80 this->_M_node._M_next = &this->_M_node;
81 this->_M_node._M_prev = &this->_M_node;
84 template<typename _Tp, typename _Alloc>
85 void list<_Tp, _Alloc>::
86 swap(list<_Tp, _Alloc>& __x)
88 if ( this->_M_node._M_next == &this->_M_node )
90 if ( __x._M_node._M_next != &__x._M_node )
92 this->_M_node._M_next = __x._M_node._M_next;
93 this->_M_node._M_prev = __x._M_node._M_prev;
95 this->_M_node._M_prev->_M_next = &this->_M_node;
96 this->_M_node._M_next->_M_prev = this->_M_node._M_prev->_M_next;
97 __x._M_node._M_next = __x._M_node._M_prev = &__x._M_node;
100 else if ( __x._M_node._M_next == &__x._M_node )
102 __x._M_node._M_next = this->_M_node._M_next;
103 __x._M_node._M_prev = this->_M_node._M_prev;
105 __x._M_node._M_prev->_M_next = &__x._M_node;
106 __x._M_node._M_next->_M_prev = __x._M_node._M_prev->_M_next;
107 this->_M_node._M_next = this->_M_node._M_prev = &this->_M_node;
111 std::swap(this->_M_node._M_next,__x._M_node._M_next);
112 std::swap(this->_M_node._M_prev,__x._M_node._M_prev);
114 this->_M_node._M_prev->_M_next = &this->_M_node;
115 this->_M_node._M_next->_M_prev = this->_M_node._M_prev->_M_next;
116 __x._M_node._M_prev->_M_next = &__x._M_node;
117 __x._M_node._M_next->_M_prev = __x._M_node._M_prev->_M_next;
121 template<typename _Tp, typename _Alloc>
122 typename list<_Tp,_Alloc>::iterator
124 insert(iterator __position, const value_type& __x)
126 _Node* __tmp = _M_create_node(__x);
127 __tmp->_M_next = __position._M_node;
128 __tmp->_M_prev = __position._M_node->_M_prev;
129 __position._M_node->_M_prev->_M_next = __tmp;
130 __position._M_node->_M_prev = __tmp;
134 template<typename _Tp, typename _Alloc>
135 typename list<_Tp,_Alloc>::iterator
137 erase(iterator __position)
139 _List_node_base* __next_node = __position._M_node->_M_next;
140 _List_node_base* __prev_node = __position._M_node->_M_prev;
141 _Node* __n = static_cast<_Node*>(__position._M_node);
142 __prev_node->_M_next = __next_node;
143 __next_node->_M_prev = __prev_node;
144 std::_Destroy(&__n->_M_data);
146 return iterator(static_cast<_Node*>(__next_node));
149 template<typename _Tp, typename _Alloc>
152 resize(size_type __new_size, const value_type& __x)
154 iterator __i = begin();
156 for ( ; __i != end() && __len < __new_size; ++__i, ++__len)
158 if (__len == __new_size)
161 insert(end(), __new_size - __len, __x);
164 template<typename _Tp, typename _Alloc>
167 operator=(const list& __x)
171 iterator __first1 = begin();
172 iterator __last1 = end();
173 const_iterator __first2 = __x.begin();
174 const_iterator __last2 = __x.end();
175 while (__first1 != __last1 && __first2 != __last2)
176 *__first1++ = *__first2++;
177 if (__first2 == __last2)
178 erase(__first1, __last1);
180 insert(__last1, __first2, __last2);
185 template<typename _Tp, typename _Alloc>
188 _M_fill_assign(size_type __n, const value_type& __val)
190 iterator __i = begin();
191 for ( ; __i != end() && __n > 0; ++__i, --__n)
194 insert(end(), __n, __val);
199 template<typename _Tp, typename _Alloc>
200 template <typename _InputIterator>
203 _M_assign_dispatch(_InputIterator __first2, _InputIterator __last2,
206 iterator __first1 = begin();
207 iterator __last1 = end();
208 for (; __first1 != __last1 && __first2 != __last2;
209 ++__first1, ++__first2)
210 *__first1 = *__first2;
211 if (__first2 == __last2)
212 erase(__first1, __last1);
214 insert(__last1, __first2, __last2);
217 template<typename _Tp, typename _Alloc>
220 remove(const value_type& __value)
222 iterator __first = begin();
223 iterator __last = end();
224 while (__first != __last)
226 iterator __next = __first;
228 if (*__first == __value)
234 template<typename _Tp, typename _Alloc>
239 iterator __first = begin();
240 iterator __last = end();
241 if (__first == __last) return;
242 iterator __next = __first;
243 while (++__next != __last)
245 if (*__first == *__next)
253 template<typename _Tp, typename _Alloc>
258 // _GLIBCXX_RESOLVE_LIB_DEFECTS
259 // 300. list::merge() specification incomplete
262 iterator __first1 = begin();
263 iterator __last1 = end();
264 iterator __first2 = __x.begin();
265 iterator __last2 = __x.end();
266 while (__first1 != __last1 && __first2 != __last2)
267 if (*__first2 < *__first1)
269 iterator __next = __first2;
270 _M_transfer(__first1, __first2, ++__next);
275 if (__first2 != __last2)
276 _M_transfer(__last1, __first2, __last2);
280 // FIXME put this somewhere else
282 __List_base_reverse(_List_node_base* __p)
284 _List_node_base* __tmp = __p;
287 std::swap(__tmp->_M_next, __tmp->_M_prev);
288 __tmp = __tmp->_M_prev; // Old next node is now prev.
290 while (__tmp != __p);
293 template<typename _Tp, typename _Alloc>
298 // Do nothing if the list has length 0 or 1.
299 if (this->_M_node._M_next != &this->_M_node
300 && this->_M_node._M_next->_M_next != &this->_M_node)
307 __carry.splice(__carry.begin(), *this, begin());
309 while(__i < __fill && !__counter[__i].empty())
311 __counter[__i].merge(__carry);
312 __carry.swap(__counter[__i++]);
314 __carry.swap(__counter[__i]);
315 if (__i == __fill) ++__fill;
318 for (int __i = 1; __i < __fill; ++__i)
319 __counter[__i].merge(__counter[__i-1]);
320 swap(__counter[__fill-1]);
324 template<typename _Tp, typename _Alloc>
325 template <typename _Predicate>
328 remove_if(_Predicate __pred)
330 iterator __first = begin();
331 iterator __last = end();
332 while (__first != __last)
334 iterator __next = __first;
336 if (__pred(*__first)) erase(__first);
341 template<typename _Tp, typename _Alloc>
342 template <typename _BinaryPredicate>
345 unique(_BinaryPredicate __binary_pred)
347 iterator __first = begin();
348 iterator __last = end();
349 if (__first == __last) return;
350 iterator __next = __first;
351 while (++__next != __last)
353 if (__binary_pred(*__first, *__next))
361 template<typename _Tp, typename _Alloc>
362 template <typename _StrictWeakOrdering>
365 merge(list& __x, _StrictWeakOrdering __comp)
367 // _GLIBCXX_RESOLVE_LIB_DEFECTS
368 // 300. list::merge() specification incomplete
371 iterator __first1 = begin();
372 iterator __last1 = end();
373 iterator __first2 = __x.begin();
374 iterator __last2 = __x.end();
375 while (__first1 != __last1 && __first2 != __last2)
376 if (__comp(*__first2, *__first1))
378 iterator __next = __first2;
379 _M_transfer(__first1, __first2, ++__next);
384 if (__first2 != __last2)
385 _M_transfer(__last1, __first2, __last2);
389 template<typename _Tp, typename _Alloc>
390 template <typename _StrictWeakOrdering>
393 sort(_StrictWeakOrdering __comp)
395 // Do nothing if the list has length 0 or 1.
396 if (this->_M_node._M_next != &this->_M_node &&
397 this->_M_node._M_next->_M_next != &this->_M_node)
404 __carry.splice(__carry.begin(), *this, begin());
406 while(__i < __fill && !__counter[__i].empty())
408 __counter[__i].merge(__carry, __comp);
409 __carry.swap(__counter[__i++]);
411 __carry.swap(__counter[__i]);
412 if (__i == __fill) ++__fill;
415 for (int __i = 1; __i < __fill; ++__i)
416 __counter[__i].merge(__counter[__i-1], __comp);
417 swap(__counter[__fill-1]);
420 } // namespace __gnu_norm
422 #endif /* _LIST_TCC */