1 // List implementation (out of line) -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
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 iterator __extra = __last;
180 while (__first != __last)
182 iterator __next = __first;
184 if (*__first == __value)
186 // _GLIBCXX_RESOLVE_LIB_DEFECTS
187 // 526. Is it undefined if a function in the standard changes
189 if (&*__first != &__value)
196 if (__extra != __last)
200 template<typename _Tp, typename _Alloc>
205 iterator __first = begin();
206 iterator __last = end();
207 if (__first == __last)
209 iterator __next = __first;
210 while (++__next != __last)
212 if (*__first == *__next)
220 template<typename _Tp, typename _Alloc>
225 // _GLIBCXX_RESOLVE_LIB_DEFECTS
226 // 300. list::merge() specification incomplete
229 _M_check_equal_allocators(__x);
231 iterator __first1 = begin();
232 iterator __last1 = end();
233 iterator __first2 = __x.begin();
234 iterator __last2 = __x.end();
235 while (__first1 != __last1 && __first2 != __last2)
236 if (*__first2 < *__first1)
238 iterator __next = __first2;
239 _M_transfer(__first1, __first2, ++__next);
244 if (__first2 != __last2)
245 _M_transfer(__last1, __first2, __last2);
249 template<typename _Tp, typename _Alloc>
250 template <typename _StrictWeakOrdering>
253 merge(list& __x, _StrictWeakOrdering __comp)
255 // _GLIBCXX_RESOLVE_LIB_DEFECTS
256 // 300. list::merge() specification incomplete
259 _M_check_equal_allocators(__x);
261 iterator __first1 = begin();
262 iterator __last1 = end();
263 iterator __first2 = __x.begin();
264 iterator __last2 = __x.end();
265 while (__first1 != __last1 && __first2 != __last2)
266 if (__comp(*__first2, *__first1))
268 iterator __next = __first2;
269 _M_transfer(__first1, __first2, ++__next);
274 if (__first2 != __last2)
275 _M_transfer(__last1, __first2, __last2);
279 template<typename _Tp, typename _Alloc>
284 // Do nothing if the list has length 0 or 1.
285 if (this->_M_impl._M_node._M_next != &this->_M_impl._M_node
286 && this->_M_impl._M_node._M_next->_M_next != &this->_M_impl._M_node)
290 list * __fill = &__tmp[0];
295 __carry.splice(__carry.begin(), *this, begin());
297 for(__counter = &__tmp[0];
298 __counter != __fill && !__counter->empty();
301 __counter->merge(__carry);
302 __carry.swap(*__counter);
304 __carry.swap(*__counter);
305 if (__counter == __fill)
310 for (__counter = &__tmp[1]; __counter != __fill; ++__counter)
311 __counter->merge(*(__counter - 1));
312 swap( *(__fill - 1) );
316 template<typename _Tp, typename _Alloc>
317 template <typename _Predicate>
320 remove_if(_Predicate __pred)
322 iterator __first = begin();
323 iterator __last = end();
324 while (__first != __last)
326 iterator __next = __first;
328 if (__pred(*__first))
334 template<typename _Tp, typename _Alloc>
335 template <typename _BinaryPredicate>
338 unique(_BinaryPredicate __binary_pred)
340 iterator __first = begin();
341 iterator __last = end();
342 if (__first == __last)
344 iterator __next = __first;
345 while (++__next != __last)
347 if (__binary_pred(*__first, *__next))
355 template<typename _Tp, typename _Alloc>
356 template <typename _StrictWeakOrdering>
359 sort(_StrictWeakOrdering __comp)
361 // Do nothing if the list has length 0 or 1.
362 if (this->_M_impl._M_node._M_next != &this->_M_impl._M_node
363 && this->_M_impl._M_node._M_next->_M_next != &this->_M_impl._M_node)
367 list * __fill = &__tmp[0];
372 __carry.splice(__carry.begin(), *this, begin());
374 for(__counter = &__tmp[0];
375 __counter != __fill && !__counter->empty();
378 __counter->merge(__carry, __comp);
379 __carry.swap(*__counter);
381 __carry.swap(*__counter);
382 if (__counter == __fill)
387 for (__counter = &__tmp[1]; __counter != __fill; ++__counter)
388 __counter->merge(*(__counter - 1), __comp);
393 _GLIBCXX_END_NESTED_NAMESPACE
395 #endif /* _LIST_TCC */