1 // List implementation (out of line) -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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_D)
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 #ifdef __GXX_EXPERIMENTAL_CXX0X__
79 _M_get_Node_allocator().destroy(__tmp);
81 _M_get_Tp_allocator().destroy(&__tmp->_M_data);
87 #ifdef __GXX_EXPERIMENTAL_CXX0X__
88 template<typename _Tp, typename _Alloc>
89 template<typename... _Args>
90 typename list<_Tp, _Alloc>::iterator
92 emplace(iterator __position, _Args&&... __args)
94 _Node* __tmp = _M_create_node(std::forward<_Args>(__args)...);
95 __tmp->hook(__position._M_node);
96 return iterator(__tmp);
100 template<typename _Tp, typename _Alloc>
101 typename list<_Tp, _Alloc>::iterator
103 insert(iterator __position, const value_type& __x)
105 _Node* __tmp = _M_create_node(__x);
106 __tmp->hook(__position._M_node);
107 return iterator(__tmp);
110 template<typename _Tp, typename _Alloc>
111 typename list<_Tp, _Alloc>::iterator
113 erase(iterator __position)
115 iterator __ret = iterator(__position._M_node->_M_next);
116 _M_erase(__position);
120 template<typename _Tp, typename _Alloc>
123 resize(size_type __new_size, value_type __x)
125 iterator __i = begin();
127 for (; __i != end() && __len < __new_size; ++__i, ++__len)
129 if (__len == __new_size)
132 insert(end(), __new_size - __len, __x);
135 template<typename _Tp, typename _Alloc>
138 operator=(const list& __x)
142 iterator __first1 = begin();
143 iterator __last1 = end();
144 const_iterator __first2 = __x.begin();
145 const_iterator __last2 = __x.end();
146 for (; __first1 != __last1 && __first2 != __last2;
147 ++__first1, ++__first2)
148 *__first1 = *__first2;
149 if (__first2 == __last2)
150 erase(__first1, __last1);
152 insert(__last1, __first2, __last2);
157 template<typename _Tp, typename _Alloc>
160 _M_fill_assign(size_type __n, const value_type& __val)
162 iterator __i = begin();
163 for (; __i != end() && __n > 0; ++__i, --__n)
166 insert(end(), __n, __val);
171 template<typename _Tp, typename _Alloc>
172 template <typename _InputIterator>
175 _M_assign_dispatch(_InputIterator __first2, _InputIterator __last2,
178 iterator __first1 = begin();
179 iterator __last1 = end();
180 for (; __first1 != __last1 && __first2 != __last2;
181 ++__first1, ++__first2)
182 *__first1 = *__first2;
183 if (__first2 == __last2)
184 erase(__first1, __last1);
186 insert(__last1, __first2, __last2);
189 template<typename _Tp, typename _Alloc>
192 remove(const value_type& __value)
194 iterator __first = begin();
195 iterator __last = end();
196 iterator __extra = __last;
197 while (__first != __last)
199 iterator __next = __first;
201 if (*__first == __value)
203 // _GLIBCXX_RESOLVE_LIB_DEFECTS
204 // 526. Is it undefined if a function in the standard changes
206 if (&*__first != &__value)
213 if (__extra != __last)
217 template<typename _Tp, typename _Alloc>
222 iterator __first = begin();
223 iterator __last = end();
224 if (__first == __last)
226 iterator __next = __first;
227 while (++__next != __last)
229 if (*__first == *__next)
237 template<typename _Tp, typename _Alloc>
240 #ifdef __GXX_EXPERIMENTAL_CXX0X__
246 // _GLIBCXX_RESOLVE_LIB_DEFECTS
247 // 300. list::merge() specification incomplete
250 _M_check_equal_allocators(__x);
252 iterator __first1 = begin();
253 iterator __last1 = end();
254 iterator __first2 = __x.begin();
255 iterator __last2 = __x.end();
256 while (__first1 != __last1 && __first2 != __last2)
257 if (*__first2 < *__first1)
259 iterator __next = __first2;
260 _M_transfer(__first1, __first2, ++__next);
265 if (__first2 != __last2)
266 _M_transfer(__last1, __first2, __last2);
270 template<typename _Tp, typename _Alloc>
271 template <typename _StrictWeakOrdering>
274 #ifdef __GXX_EXPERIMENTAL_CXX0X__
275 merge(list&& __x, _StrictWeakOrdering __comp)
277 merge(list& __x, _StrictWeakOrdering __comp)
280 // _GLIBCXX_RESOLVE_LIB_DEFECTS
281 // 300. list::merge() specification incomplete
284 _M_check_equal_allocators(__x);
286 iterator __first1 = begin();
287 iterator __last1 = end();
288 iterator __first2 = __x.begin();
289 iterator __last2 = __x.end();
290 while (__first1 != __last1 && __first2 != __last2)
291 if (__comp(*__first2, *__first1))
293 iterator __next = __first2;
294 _M_transfer(__first1, __first2, ++__next);
299 if (__first2 != __last2)
300 _M_transfer(__last1, __first2, __last2);
304 template<typename _Tp, typename _Alloc>
309 // Do nothing if the list has length 0 or 1.
310 if (this->_M_impl._M_node._M_next != &this->_M_impl._M_node
311 && this->_M_impl._M_node._M_next->_M_next != &this->_M_impl._M_node)
315 list * __fill = &__tmp[0];
320 __carry.splice(__carry.begin(), *this, begin());
322 for(__counter = &__tmp[0];
323 __counter != __fill && !__counter->empty();
326 __counter->merge(__carry);
327 __carry.swap(*__counter);
329 __carry.swap(*__counter);
330 if (__counter == __fill)
335 for (__counter = &__tmp[1]; __counter != __fill; ++__counter)
336 __counter->merge(*(__counter - 1));
337 swap( *(__fill - 1) );
341 template<typename _Tp, typename _Alloc>
342 template <typename _Predicate>
345 remove_if(_Predicate __pred)
347 iterator __first = begin();
348 iterator __last = end();
349 while (__first != __last)
351 iterator __next = __first;
353 if (__pred(*__first))
359 template<typename _Tp, typename _Alloc>
360 template <typename _BinaryPredicate>
363 unique(_BinaryPredicate __binary_pred)
365 iterator __first = begin();
366 iterator __last = end();
367 if (__first == __last)
369 iterator __next = __first;
370 while (++__next != __last)
372 if (__binary_pred(*__first, *__next))
380 template<typename _Tp, typename _Alloc>
381 template <typename _StrictWeakOrdering>
384 sort(_StrictWeakOrdering __comp)
386 // Do nothing if the list has length 0 or 1.
387 if (this->_M_impl._M_node._M_next != &this->_M_impl._M_node
388 && this->_M_impl._M_node._M_next->_M_next != &this->_M_impl._M_node)
392 list * __fill = &__tmp[0];
397 __carry.splice(__carry.begin(), *this, begin());
399 for(__counter = &__tmp[0];
400 __counter != __fill && !__counter->empty();
403 __counter->merge(__carry, __comp);
404 __carry.swap(*__counter);
406 __carry.swap(*__counter);
407 if (__counter == __fill)
412 for (__counter = &__tmp[1]; __counter != __fill; ++__counter)
413 __counter->merge(*(__counter - 1), __comp);
418 _GLIBCXX_END_NESTED_NAMESPACE
420 #endif /* _LIST_TCC */