1 // List implementation (out of line) -*- C++ -*-
3 // Copyright (C) 2001-2015 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>::
66 _M_clear() _GLIBCXX_NOEXCEPT
68 typedef _List_node<_Tp> _Node;
69 __detail::_List_node_base* __cur = _M_impl._M_node._M_next;
70 while (__cur != &_M_impl._M_node)
72 _Node* __tmp = static_cast<_Node*>(__cur);
73 __cur = __tmp->_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(const_iterator __position, _Args&&... __args)
90 _Node* __tmp = _M_create_node(std::forward<_Args>(__args)...);
91 __tmp->_M_hook(__position._M_const_cast()._M_node);
93 return iterator(__tmp);
97 template<typename _Tp, typename _Alloc>
98 typename list<_Tp, _Alloc>::iterator
100 #if __cplusplus >= 201103L
101 insert(const_iterator __position, const value_type& __x)
103 insert(iterator __position, const value_type& __x)
106 _Node* __tmp = _M_create_node(__x);
107 __tmp->_M_hook(__position._M_const_cast()._M_node);
108 this->_M_inc_size(1);
109 return iterator(__tmp);
112 #if __cplusplus >= 201103L
113 template<typename _Tp, typename _Alloc>
114 typename list<_Tp, _Alloc>::iterator
116 insert(const_iterator __position, size_type __n, const value_type& __x)
120 list __tmp(__n, __x, get_allocator());
121 iterator __it = __tmp.begin();
122 splice(__position, __tmp);
125 return __position._M_const_cast();
128 template<typename _Tp, typename _Alloc>
129 template<typename _InputIterator, typename>
130 typename list<_Tp, _Alloc>::iterator
132 insert(const_iterator __position, _InputIterator __first,
133 _InputIterator __last)
135 list __tmp(__first, __last, get_allocator());
138 iterator __it = __tmp.begin();
139 splice(__position, __tmp);
142 return __position._M_const_cast();
146 template<typename _Tp, typename _Alloc>
147 typename list<_Tp, _Alloc>::iterator
149 #if __cplusplus >= 201103L
150 erase(const_iterator __position) noexcept
152 erase(iterator __position)
155 iterator __ret = iterator(__position._M_node->_M_next);
156 _M_erase(__position._M_const_cast());
160 // Return a const_iterator indicating the position to start inserting or
161 // erasing elements (depending whether the list is growing or shrinking),
162 // and set __new_size to the number of new elements that must be appended.
163 // Equivalent to the following, but performed optimally:
164 // if (__new_size < size()) {
166 // return std::next(begin(), __new_size);
168 // __newsize -= size();
171 template<typename _Tp, typename _Alloc>
172 typename list<_Tp, _Alloc>::const_iterator
174 _M_resize_pos(size_type& __new_size) const
177 #if _GLIBCXX_USE_CXX11_ABI
178 const size_type __len = size();
179 if (__new_size < __len)
181 if (__new_size <= __len / 2)
184 std::advance(__i, __new_size);
189 ptrdiff_t __num_erase = __len - __new_size;
190 std::advance(__i, -__num_erase);
199 for (__i = begin(); __i != end() && __len < __new_size; ++__i, ++__len)
206 #if __cplusplus >= 201103L
207 template<typename _Tp, typename _Alloc>
210 _M_default_append(size_type __n)
215 for (; __i < __n; ++__i)
222 __throw_exception_again;
226 template<typename _Tp, typename _Alloc>
229 resize(size_type __new_size)
231 const_iterator __i = _M_resize_pos(__new_size);
233 _M_default_append(__new_size);
238 template<typename _Tp, typename _Alloc>
241 resize(size_type __new_size, const value_type& __x)
243 const_iterator __i = _M_resize_pos(__new_size);
245 insert(end(), __new_size, __x);
250 template<typename _Tp, typename _Alloc>
253 resize(size_type __new_size, value_type __x)
255 const_iterator __i = _M_resize_pos(__new_size);
257 insert(end(), __new_size, __x);
259 erase(__i._M_const_cast(), end());
263 template<typename _Tp, typename _Alloc>
266 operator=(const list& __x)
268 if (this != std::__addressof(__x))
270 iterator __first1 = begin();
271 iterator __last1 = end();
272 const_iterator __first2 = __x.begin();
273 const_iterator __last2 = __x.end();
274 for (; __first1 != __last1 && __first2 != __last2;
275 ++__first1, ++__first2)
276 *__first1 = *__first2;
277 if (__first2 == __last2)
278 erase(__first1, __last1);
280 insert(__last1, __first2, __last2);
285 template<typename _Tp, typename _Alloc>
288 _M_fill_assign(size_type __n, const value_type& __val)
290 iterator __i = begin();
291 for (; __i != end() && __n > 0; ++__i, --__n)
294 insert(end(), __n, __val);
299 template<typename _Tp, typename _Alloc>
300 template <typename _InputIterator>
303 _M_assign_dispatch(_InputIterator __first2, _InputIterator __last2,
306 iterator __first1 = begin();
307 iterator __last1 = end();
308 for (; __first1 != __last1 && __first2 != __last2;
309 ++__first1, ++__first2)
310 *__first1 = *__first2;
311 if (__first2 == __last2)
312 erase(__first1, __last1);
314 insert(__last1, __first2, __last2);
317 template<typename _Tp, typename _Alloc>
320 remove(const value_type& __value)
322 iterator __first = begin();
323 iterator __last = end();
324 iterator __extra = __last;
325 while (__first != __last)
327 iterator __next = __first;
329 if (*__first == __value)
331 // _GLIBCXX_RESOLVE_LIB_DEFECTS
332 // 526. Is it undefined if a function in the standard changes
334 if (std::__addressof(*__first) != std::__addressof(__value))
341 if (__extra != __last)
345 template<typename _Tp, typename _Alloc>
350 iterator __first = begin();
351 iterator __last = end();
352 if (__first == __last)
354 iterator __next = __first;
355 while (++__next != __last)
357 if (*__first == *__next)
365 template<typename _Tp, typename _Alloc>
368 #if __cplusplus >= 201103L
374 // _GLIBCXX_RESOLVE_LIB_DEFECTS
375 // 300. list::merge() specification incomplete
376 if (this != std::__addressof(__x))
378 _M_check_equal_allocators(__x);
380 iterator __first1 = begin();
381 iterator __last1 = end();
382 iterator __first2 = __x.begin();
383 iterator __last2 = __x.end();
384 while (__first1 != __last1 && __first2 != __last2)
385 if (*__first2 < *__first1)
387 iterator __next = __first2;
388 _M_transfer(__first1, __first2, ++__next);
393 if (__first2 != __last2)
394 _M_transfer(__last1, __first2, __last2);
396 this->_M_inc_size(__x._M_get_size());
401 template<typename _Tp, typename _Alloc>
402 template <typename _StrictWeakOrdering>
405 #if __cplusplus >= 201103L
406 merge(list&& __x, _StrictWeakOrdering __comp)
408 merge(list& __x, _StrictWeakOrdering __comp)
411 // _GLIBCXX_RESOLVE_LIB_DEFECTS
412 // 300. list::merge() specification incomplete
413 if (this != std::__addressof(__x))
415 _M_check_equal_allocators(__x);
417 iterator __first1 = begin();
418 iterator __last1 = end();
419 iterator __first2 = __x.begin();
420 iterator __last2 = __x.end();
421 while (__first1 != __last1 && __first2 != __last2)
422 if (__comp(*__first2, *__first1))
424 iterator __next = __first2;
425 _M_transfer(__first1, __first2, ++__next);
430 if (__first2 != __last2)
431 _M_transfer(__last1, __first2, __last2);
433 this->_M_inc_size(__x._M_get_size());
438 template<typename _Tp, typename _Alloc>
443 // Do nothing if the list has length 0 or 1.
444 if (this->_M_impl._M_node._M_next != &this->_M_impl._M_node
445 && this->_M_impl._M_node._M_next->_M_next != &this->_M_impl._M_node)
449 list * __fill = __tmp;
454 __carry.splice(__carry.begin(), *this, begin());
456 for(__counter = __tmp;
457 __counter != __fill && !__counter->empty();
460 __counter->merge(__carry);
461 __carry.swap(*__counter);
463 __carry.swap(*__counter);
464 if (__counter == __fill)
469 for (__counter = __tmp + 1; __counter != __fill; ++__counter)
470 __counter->merge(*(__counter - 1));
471 swap( *(__fill - 1) );
475 template<typename _Tp, typename _Alloc>
476 template <typename _Predicate>
479 remove_if(_Predicate __pred)
481 iterator __first = begin();
482 iterator __last = end();
483 while (__first != __last)
485 iterator __next = __first;
487 if (__pred(*__first))
493 template<typename _Tp, typename _Alloc>
494 template <typename _BinaryPredicate>
497 unique(_BinaryPredicate __binary_pred)
499 iterator __first = begin();
500 iterator __last = end();
501 if (__first == __last)
503 iterator __next = __first;
504 while (++__next != __last)
506 if (__binary_pred(*__first, *__next))
514 template<typename _Tp, typename _Alloc>
515 template <typename _StrictWeakOrdering>
518 sort(_StrictWeakOrdering __comp)
520 // Do nothing if the list has length 0 or 1.
521 if (this->_M_impl._M_node._M_next != &this->_M_impl._M_node
522 && this->_M_impl._M_node._M_next->_M_next != &this->_M_impl._M_node)
526 list * __fill = __tmp;
531 __carry.splice(__carry.begin(), *this, begin());
533 for(__counter = __tmp;
534 __counter != __fill && !__counter->empty();
537 __counter->merge(__carry, __comp);
538 __carry.swap(*__counter);
540 __carry.swap(*__counter);
541 if (__counter == __fill)
546 for (__counter = __tmp + 1; __counter != __fill; ++__counter)
547 __counter->merge(*(__counter - 1), __comp);
552 _GLIBCXX_END_NAMESPACE_CONTAINER
555 #endif /* _LIST_TCC */