1 // List implementation (out of line) -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
45 * Copyright (c) 1996,1997
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
65 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD)
67 template<typename _Tp, typename _Alloc>
69 _List_base<_Tp, _Alloc>::
72 typedef _List_node<_Tp> _Node;
73 _Node* __cur = static_cast<_Node*>(this->_M_impl._M_node._M_next);
74 while (__cur != &this->_M_impl._M_node)
77 __cur = static_cast<_Node*>(__cur->_M_next);
78 _M_get_Tp_allocator().destroy(&__tmp->_M_data);
83 template<typename _Tp, typename _Alloc>
84 typename list<_Tp, _Alloc>::iterator
86 insert(iterator __position, const value_type& __x)
88 _Node* __tmp = _M_create_node(__x);
89 __tmp->hook(__position._M_node);
90 return iterator(__tmp);
93 template<typename _Tp, typename _Alloc>
94 typename list<_Tp, _Alloc>::iterator
96 erase(iterator __position)
98 iterator __ret = iterator(__position._M_node->_M_next);
103 template<typename _Tp, typename _Alloc>
106 resize(size_type __new_size, value_type __x)
108 iterator __i = begin();
110 for (; __i != end() && __len < __new_size; ++__i, ++__len)
112 if (__len == __new_size)
115 insert(end(), __new_size - __len, __x);
118 template<typename _Tp, typename _Alloc>
121 operator=(const list& __x)
125 iterator __first1 = begin();
126 iterator __last1 = end();
127 const_iterator __first2 = __x.begin();
128 const_iterator __last2 = __x.end();
129 for (; __first1 != __last1 && __first2 != __last2;
130 ++__first1, ++__first2)
131 *__first1 = *__first2;
132 if (__first2 == __last2)
133 erase(__first1, __last1);
135 insert(__last1, __first2, __last2);
140 template<typename _Tp, typename _Alloc>
143 _M_fill_assign(size_type __n, const value_type& __val)
145 iterator __i = begin();
146 for (; __i != end() && __n > 0; ++__i, --__n)
149 insert(end(), __n, __val);
154 template<typename _Tp, typename _Alloc>
155 template <typename _InputIterator>
158 _M_assign_dispatch(_InputIterator __first2, _InputIterator __last2,
161 iterator __first1 = begin();
162 iterator __last1 = end();
163 for (; __first1 != __last1 && __first2 != __last2;
164 ++__first1, ++__first2)
165 *__first1 = *__first2;
166 if (__first2 == __last2)
167 erase(__first1, __last1);
169 insert(__last1, __first2, __last2);
172 template<typename _Tp, typename _Alloc>
175 remove(const value_type& __value)
177 iterator __first = begin();
178 iterator __last = end();
179 while (__first != __last)
181 iterator __next = __first;
183 if (*__first == __value)
189 template<typename _Tp, typename _Alloc>
194 iterator __first = begin();
195 iterator __last = end();
196 if (__first == __last)
198 iterator __next = __first;
199 while (++__next != __last)
201 if (*__first == *__next)
209 template<typename _Tp, typename _Alloc>
214 // _GLIBCXX_RESOLVE_LIB_DEFECTS
215 // 300. list::merge() specification incomplete
218 _M_check_equal_allocators(__x);
220 iterator __first1 = begin();
221 iterator __last1 = end();
222 iterator __first2 = __x.begin();
223 iterator __last2 = __x.end();
224 while (__first1 != __last1 && __first2 != __last2)
225 if (*__first2 < *__first1)
227 iterator __next = __first2;
228 _M_transfer(__first1, __first2, ++__next);
233 if (__first2 != __last2)
234 _M_transfer(__last1, __first2, __last2);
238 template<typename _Tp, typename _Alloc>
239 template <typename _StrictWeakOrdering>
242 merge(list& __x, _StrictWeakOrdering __comp)
244 // _GLIBCXX_RESOLVE_LIB_DEFECTS
245 // 300. list::merge() specification incomplete
248 _M_check_equal_allocators(__x);
250 iterator __first1 = begin();
251 iterator __last1 = end();
252 iterator __first2 = __x.begin();
253 iterator __last2 = __x.end();
254 while (__first1 != __last1 && __first2 != __last2)
255 if (__comp(*__first2, *__first1))
257 iterator __next = __first2;
258 _M_transfer(__first1, __first2, ++__next);
263 if (__first2 != __last2)
264 _M_transfer(__last1, __first2, __last2);
268 template<typename _Tp, typename _Alloc>
273 // Do nothing if the list has length 0 or 1.
274 if (this->_M_impl._M_node._M_next != &this->_M_impl._M_node
275 && this->_M_impl._M_node._M_next->_M_next != &this->_M_impl._M_node)
279 list * __fill = &__tmp[0];
284 __carry.splice(__carry.begin(), *this, begin());
286 for(__counter = &__tmp[0];
287 __counter != __fill && !__counter->empty();
290 __counter->merge(__carry);
291 __carry.swap(*__counter);
293 __carry.swap(*__counter);
294 if (__counter == __fill)
299 for (__counter = &__tmp[1]; __counter != __fill; ++__counter)
300 __counter->merge(*(__counter - 1));
301 swap( *(__fill - 1) );
305 template<typename _Tp, typename _Alloc>
306 template <typename _Predicate>
309 remove_if(_Predicate __pred)
311 iterator __first = begin();
312 iterator __last = end();
313 while (__first != __last)
315 iterator __next = __first;
317 if (__pred(*__first))
323 template<typename _Tp, typename _Alloc>
324 template <typename _BinaryPredicate>
327 unique(_BinaryPredicate __binary_pred)
329 iterator __first = begin();
330 iterator __last = end();
331 if (__first == __last)
333 iterator __next = __first;
334 while (++__next != __last)
336 if (__binary_pred(*__first, *__next))
344 template<typename _Tp, typename _Alloc>
345 template <typename _StrictWeakOrdering>
348 sort(_StrictWeakOrdering __comp)
350 // Do nothing if the list has length 0 or 1.
351 if (this->_M_impl._M_node._M_next != &this->_M_impl._M_node
352 && this->_M_impl._M_node._M_next->_M_next != &this->_M_impl._M_node)
356 list * __fill = &__tmp[0];
361 __carry.splice(__carry.begin(), *this, begin());
363 for(__counter = &__tmp[0];
364 __counter != __fill && !__counter->empty();
367 __counter->merge(__carry, __comp);
368 __carry.swap(*__counter);
370 __carry.swap(*__counter);
371 if (__counter == __fill)
376 for (__counter = &__tmp[1]; __counter != __fill; ++__counter)
377 __counter->merge(*(__counter - 1), __comp);
382 _GLIBCXX_END_NESTED_NAMESPACE
384 #endif /* _LIST_TCC */