1 // List implementation (out of line) -*- C++ -*-
3 // Copyright (C) 2001-2013 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
28 * Hewlett-Packard Company
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
39 * Copyright (c) 1996,1997
40 * Silicon Graphics Computer Systems, Inc.
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
51 /** @file bits/list.tcc
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{list}
59 namespace std _GLIBCXX_VISIBILITY(default)
61 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
63 template<typename _Tp, typename _Alloc>
65 _List_base<_Tp, _Alloc>::
68 typedef _List_node<_Tp> _Node;
69 _Node* __cur = static_cast<_Node*>(_M_impl._M_node._M_next);
70 while (__cur != &_M_impl._M_node)
73 __cur = static_cast<_Node*>(__cur->_M_next);
74 #if __cplusplus >= 201103L
75 _M_get_Node_allocator().destroy(__tmp);
77 _M_get_Tp_allocator().destroy(std::__addressof(__tmp->_M_data));
83 #if __cplusplus >= 201103L
84 template<typename _Tp, typename _Alloc>
85 template<typename... _Args>
86 typename list<_Tp, _Alloc>::iterator
88 emplace(iterator __position, _Args&&... __args)
90 _Node* __tmp = _M_create_node(std::forward<_Args>(__args)...);
91 __tmp->_M_hook(__position._M_node);
92 return iterator(__tmp);
96 template<typename _Tp, typename _Alloc>
97 typename list<_Tp, _Alloc>::iterator
99 insert(iterator __position, const value_type& __x)
101 _Node* __tmp = _M_create_node(__x);
102 __tmp->_M_hook(__position._M_node);
103 return iterator(__tmp);
106 template<typename _Tp, typename _Alloc>
107 typename list<_Tp, _Alloc>::iterator
109 erase(iterator __position)
111 iterator __ret = iterator(__position._M_node->_M_next);
112 _M_erase(__position);
116 #if __cplusplus >= 201103L
117 template<typename _Tp, typename _Alloc>
120 _M_default_append(size_type __n)
125 for (; __i < __n; ++__i)
132 __throw_exception_again;
136 template<typename _Tp, typename _Alloc>
139 resize(size_type __new_size)
141 iterator __i = begin();
143 for (; __i != end() && __len < __new_size; ++__i, ++__len)
145 if (__len == __new_size)
148 _M_default_append(__new_size - __len);
151 template<typename _Tp, typename _Alloc>
154 resize(size_type __new_size, const value_type& __x)
156 iterator __i = begin();
158 for (; __i != end() && __len < __new_size; ++__i, ++__len)
160 if (__len == __new_size)
163 insert(end(), __new_size - __len, __x);
166 template<typename _Tp, typename _Alloc>
169 resize(size_type __new_size, value_type __x)
171 iterator __i = begin();
173 for (; __i != end() && __len < __new_size; ++__i, ++__len)
175 if (__len == __new_size)
178 insert(end(), __new_size - __len, __x);
182 template<typename _Tp, typename _Alloc>
185 operator=(const list& __x)
189 iterator __first1 = begin();
190 iterator __last1 = end();
191 const_iterator __first2 = __x.begin();
192 const_iterator __last2 = __x.end();
193 for (; __first1 != __last1 && __first2 != __last2;
194 ++__first1, ++__first2)
195 *__first1 = *__first2;
196 if (__first2 == __last2)
197 erase(__first1, __last1);
199 insert(__last1, __first2, __last2);
204 template<typename _Tp, typename _Alloc>
207 _M_fill_assign(size_type __n, const value_type& __val)
209 iterator __i = begin();
210 for (; __i != end() && __n > 0; ++__i, --__n)
213 insert(end(), __n, __val);
218 template<typename _Tp, typename _Alloc>
219 template <typename _InputIterator>
222 _M_assign_dispatch(_InputIterator __first2, _InputIterator __last2,
225 iterator __first1 = begin();
226 iterator __last1 = end();
227 for (; __first1 != __last1 && __first2 != __last2;
228 ++__first1, ++__first2)
229 *__first1 = *__first2;
230 if (__first2 == __last2)
231 erase(__first1, __last1);
233 insert(__last1, __first2, __last2);
236 template<typename _Tp, typename _Alloc>
239 remove(const value_type& __value)
241 iterator __first = begin();
242 iterator __last = end();
243 iterator __extra = __last;
244 while (__first != __last)
246 iterator __next = __first;
248 if (*__first == __value)
250 // _GLIBCXX_RESOLVE_LIB_DEFECTS
251 // 526. Is it undefined if a function in the standard changes
253 if (std::__addressof(*__first) != std::__addressof(__value))
260 if (__extra != __last)
264 template<typename _Tp, typename _Alloc>
269 iterator __first = begin();
270 iterator __last = end();
271 if (__first == __last)
273 iterator __next = __first;
274 while (++__next != __last)
276 if (*__first == *__next)
284 template<typename _Tp, typename _Alloc>
287 #if __cplusplus >= 201103L
293 // _GLIBCXX_RESOLVE_LIB_DEFECTS
294 // 300. list::merge() specification incomplete
297 _M_check_equal_allocators(__x);
299 iterator __first1 = begin();
300 iterator __last1 = end();
301 iterator __first2 = __x.begin();
302 iterator __last2 = __x.end();
303 while (__first1 != __last1 && __first2 != __last2)
304 if (*__first2 < *__first1)
306 iterator __next = __first2;
307 _M_transfer(__first1, __first2, ++__next);
312 if (__first2 != __last2)
313 _M_transfer(__last1, __first2, __last2);
317 template<typename _Tp, typename _Alloc>
318 template <typename _StrictWeakOrdering>
321 #if __cplusplus >= 201103L
322 merge(list&& __x, _StrictWeakOrdering __comp)
324 merge(list& __x, _StrictWeakOrdering __comp)
327 // _GLIBCXX_RESOLVE_LIB_DEFECTS
328 // 300. list::merge() specification incomplete
331 _M_check_equal_allocators(__x);
333 iterator __first1 = begin();
334 iterator __last1 = end();
335 iterator __first2 = __x.begin();
336 iterator __last2 = __x.end();
337 while (__first1 != __last1 && __first2 != __last2)
338 if (__comp(*__first2, *__first1))
340 iterator __next = __first2;
341 _M_transfer(__first1, __first2, ++__next);
346 if (__first2 != __last2)
347 _M_transfer(__last1, __first2, __last2);
351 template<typename _Tp, typename _Alloc>
356 // Do nothing if the list has length 0 or 1.
357 if (this->_M_impl._M_node._M_next != &this->_M_impl._M_node
358 && this->_M_impl._M_node._M_next->_M_next != &this->_M_impl._M_node)
362 list * __fill = &__tmp[0];
367 __carry.splice(__carry.begin(), *this, begin());
369 for(__counter = &__tmp[0];
370 __counter != __fill && !__counter->empty();
373 __counter->merge(__carry);
374 __carry.swap(*__counter);
376 __carry.swap(*__counter);
377 if (__counter == __fill)
382 for (__counter = &__tmp[1]; __counter != __fill; ++__counter)
383 __counter->merge(*(__counter - 1));
384 swap( *(__fill - 1) );
388 template<typename _Tp, typename _Alloc>
389 template <typename _Predicate>
392 remove_if(_Predicate __pred)
394 iterator __first = begin();
395 iterator __last = end();
396 while (__first != __last)
398 iterator __next = __first;
400 if (__pred(*__first))
406 template<typename _Tp, typename _Alloc>
407 template <typename _BinaryPredicate>
410 unique(_BinaryPredicate __binary_pred)
412 iterator __first = begin();
413 iterator __last = end();
414 if (__first == __last)
416 iterator __next = __first;
417 while (++__next != __last)
419 if (__binary_pred(*__first, *__next))
427 template<typename _Tp, typename _Alloc>
428 template <typename _StrictWeakOrdering>
431 sort(_StrictWeakOrdering __comp)
433 // Do nothing if the list has length 0 or 1.
434 if (this->_M_impl._M_node._M_next != &this->_M_impl._M_node
435 && this->_M_impl._M_node._M_next->_M_next != &this->_M_impl._M_node)
439 list * __fill = &__tmp[0];
444 __carry.splice(__carry.begin(), *this, begin());
446 for(__counter = &__tmp[0];
447 __counter != __fill && !__counter->empty();
450 __counter->merge(__carry, __comp);
451 __carry.swap(*__counter);
453 __carry.swap(*__counter);
454 if (__counter == __fill)
459 for (__counter = &__tmp[1]; __counter != __fill; ++__counter)
460 __counter->merge(*(__counter - 1), __comp);
465 _GLIBCXX_END_NAMESPACE_CONTAINER
468 #endif /* _LIST_TCC */