1 // List implementation (out of line) -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
29 * Hewlett-Packard Company
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation. Hewlett-Packard Company makes no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
40 * Copyright (c) 1996,1997
41 * Silicon Graphics Computer Systems, Inc.
43 * Permission to use, copy, modify, distribute and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appear in all copies and
46 * that both that copyright notice and this permission notice appear
47 * in supporting documentation. Silicon Graphics makes no
48 * representations about the suitability of this software for any
49 * purpose. It is provided "as is" without express or implied warranty.
53 * This is an internal header file, included by other library headers.
54 * You should not attempt to use it directly.
60 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
62 template<typename _Tp, typename _Alloc>
64 _List_base<_Tp, _Alloc>::
67 typedef _List_node<_Tp> _Node;
68 _Node* __cur = static_cast<_Node*>(this->_M_impl._M_node._M_next);
69 while (__cur != &this->_M_impl._M_node)
72 __cur = static_cast<_Node*>(__cur->_M_next);
73 #ifdef __GXX_EXPERIMENTAL_CXX0X__
74 _M_get_Node_allocator().destroy(__tmp);
76 _M_get_Tp_allocator().destroy(&__tmp->_M_data);
82 #ifdef __GXX_EXPERIMENTAL_CXX0X__
83 template<typename _Tp, typename _Alloc>
84 template<typename... _Args>
85 typename list<_Tp, _Alloc>::iterator
87 emplace(iterator __position, _Args&&... __args)
89 _Node* __tmp = _M_create_node(std::forward<_Args>(__args)...);
90 __tmp->hook(__position._M_node);
91 return iterator(__tmp);
95 template<typename _Tp, typename _Alloc>
96 typename list<_Tp, _Alloc>::iterator
98 insert(iterator __position, const value_type& __x)
100 _Node* __tmp = _M_create_node(__x);
101 __tmp->hook(__position._M_node);
102 return iterator(__tmp);
105 template<typename _Tp, typename _Alloc>
106 typename list<_Tp, _Alloc>::iterator
108 erase(iterator __position)
110 iterator __ret = iterator(__position._M_node->_M_next);
111 _M_erase(__position);
115 template<typename _Tp, typename _Alloc>
118 resize(size_type __new_size, value_type __x)
120 iterator __i = begin();
122 for (; __i != end() && __len < __new_size; ++__i, ++__len)
124 if (__len == __new_size)
127 insert(end(), __new_size - __len, __x);
130 template<typename _Tp, typename _Alloc>
133 operator=(const list& __x)
137 iterator __first1 = begin();
138 iterator __last1 = end();
139 const_iterator __first2 = __x.begin();
140 const_iterator __last2 = __x.end();
141 for (; __first1 != __last1 && __first2 != __last2;
142 ++__first1, ++__first2)
143 *__first1 = *__first2;
144 if (__first2 == __last2)
145 erase(__first1, __last1);
147 insert(__last1, __first2, __last2);
152 template<typename _Tp, typename _Alloc>
155 _M_fill_assign(size_type __n, const value_type& __val)
157 iterator __i = begin();
158 for (; __i != end() && __n > 0; ++__i, --__n)
161 insert(end(), __n, __val);
166 template<typename _Tp, typename _Alloc>
167 template <typename _InputIterator>
170 _M_assign_dispatch(_InputIterator __first2, _InputIterator __last2,
173 iterator __first1 = begin();
174 iterator __last1 = end();
175 for (; __first1 != __last1 && __first2 != __last2;
176 ++__first1, ++__first2)
177 *__first1 = *__first2;
178 if (__first2 == __last2)
179 erase(__first1, __last1);
181 insert(__last1, __first2, __last2);
184 template<typename _Tp, typename _Alloc>
187 remove(const value_type& __value)
189 iterator __first = begin();
190 iterator __last = end();
191 iterator __extra = __last;
192 while (__first != __last)
194 iterator __next = __first;
196 if (*__first == __value)
198 // _GLIBCXX_RESOLVE_LIB_DEFECTS
199 // 526. Is it undefined if a function in the standard changes
201 if (&*__first != &__value)
208 if (__extra != __last)
212 template<typename _Tp, typename _Alloc>
217 iterator __first = begin();
218 iterator __last = end();
219 if (__first == __last)
221 iterator __next = __first;
222 while (++__next != __last)
224 if (*__first == *__next)
232 template<typename _Tp, typename _Alloc>
235 #ifdef __GXX_EXPERIMENTAL_CXX0X__
241 // _GLIBCXX_RESOLVE_LIB_DEFECTS
242 // 300. list::merge() specification incomplete
245 _M_check_equal_allocators(__x);
247 iterator __first1 = begin();
248 iterator __last1 = end();
249 iterator __first2 = __x.begin();
250 iterator __last2 = __x.end();
251 while (__first1 != __last1 && __first2 != __last2)
252 if (*__first2 < *__first1)
254 iterator __next = __first2;
255 _M_transfer(__first1, __first2, ++__next);
260 if (__first2 != __last2)
261 _M_transfer(__last1, __first2, __last2);
265 template<typename _Tp, typename _Alloc>
266 template <typename _StrictWeakOrdering>
269 #ifdef __GXX_EXPERIMENTAL_CXX0X__
270 merge(list&& __x, _StrictWeakOrdering __comp)
272 merge(list& __x, _StrictWeakOrdering __comp)
275 // _GLIBCXX_RESOLVE_LIB_DEFECTS
276 // 300. list::merge() specification incomplete
279 _M_check_equal_allocators(__x);
281 iterator __first1 = begin();
282 iterator __last1 = end();
283 iterator __first2 = __x.begin();
284 iterator __last2 = __x.end();
285 while (__first1 != __last1 && __first2 != __last2)
286 if (__comp(*__first2, *__first1))
288 iterator __next = __first2;
289 _M_transfer(__first1, __first2, ++__next);
294 if (__first2 != __last2)
295 _M_transfer(__last1, __first2, __last2);
299 template<typename _Tp, typename _Alloc>
304 // Do nothing if the list has length 0 or 1.
305 if (this->_M_impl._M_node._M_next != &this->_M_impl._M_node
306 && this->_M_impl._M_node._M_next->_M_next != &this->_M_impl._M_node)
310 list * __fill = &__tmp[0];
315 __carry.splice(__carry.begin(), *this, begin());
317 for(__counter = &__tmp[0];
318 __counter != __fill && !__counter->empty();
321 __counter->merge(__carry);
322 __carry.swap(*__counter);
324 __carry.swap(*__counter);
325 if (__counter == __fill)
330 for (__counter = &__tmp[1]; __counter != __fill; ++__counter)
331 __counter->merge(*(__counter - 1));
332 swap( *(__fill - 1) );
336 template<typename _Tp, typename _Alloc>
337 template <typename _Predicate>
340 remove_if(_Predicate __pred)
342 iterator __first = begin();
343 iterator __last = end();
344 while (__first != __last)
346 iterator __next = __first;
348 if (__pred(*__first))
354 template<typename _Tp, typename _Alloc>
355 template <typename _BinaryPredicate>
358 unique(_BinaryPredicate __binary_pred)
360 iterator __first = begin();
361 iterator __last = end();
362 if (__first == __last)
364 iterator __next = __first;
365 while (++__next != __last)
367 if (__binary_pred(*__first, *__next))
375 template<typename _Tp, typename _Alloc>
376 template <typename _StrictWeakOrdering>
379 sort(_StrictWeakOrdering __comp)
381 // Do nothing if the list has length 0 or 1.
382 if (this->_M_impl._M_node._M_next != &this->_M_impl._M_node
383 && this->_M_impl._M_node._M_next->_M_next != &this->_M_impl._M_node)
387 list * __fill = &__tmp[0];
392 __carry.splice(__carry.begin(), *this, begin());
394 for(__counter = &__tmp[0];
395 __counter != __fill && !__counter->empty();
398 __counter->merge(__carry, __comp);
399 __carry.swap(*__counter);
401 __carry.swap(*__counter);
402 if (__counter == __fill)
407 for (__counter = &__tmp[1]; __counter != __fill; ++__counter)
408 __counter->merge(*(__counter - 1), __comp);
413 _GLIBCXX_END_NESTED_NAMESPACE
415 #endif /* _LIST_TCC */