1 // <forward_list.tcc> -*- C++ -*-
3 // Copyright (C) 2008-2016 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/>.
25 /** @file bits/forward_list.tcc
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{forward_list}
30 #ifndef _FORWARD_LIST_TCC
31 #define _FORWARD_LIST_TCC 1
33 namespace std _GLIBCXX_VISIBILITY(default)
35 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
37 template<typename _Tp, typename _Alloc>
38 _Fwd_list_base<_Tp, _Alloc>::
39 _Fwd_list_base(_Fwd_list_base&& __lst, _Node_alloc_type&& __a)
40 : _M_impl(std::move(__a))
42 if (__lst._M_get_Node_allocator() == _M_get_Node_allocator())
44 this->_M_impl._M_head._M_next = __lst._M_impl._M_head._M_next;
45 __lst._M_impl._M_head._M_next = 0;
48 this->_M_impl._M_head._M_next = 0;
51 template<typename _Tp, typename _Alloc>
52 template<typename... _Args>
54 _Fwd_list_base<_Tp, _Alloc>::
55 _M_insert_after(const_iterator __pos, _Args&&... __args)
57 _Fwd_list_node_base* __to
58 = const_cast<_Fwd_list_node_base*>(__pos._M_node);
59 _Node* __thing = _M_create_node(std::forward<_Args>(__args)...);
60 __thing->_M_next = __to->_M_next;
61 __to->_M_next = __thing;
65 template<typename _Tp, typename _Alloc>
67 _Fwd_list_base<_Tp, _Alloc>::
68 _M_erase_after(_Fwd_list_node_base* __pos)
70 _Node* __curr = static_cast<_Node*>(__pos->_M_next);
71 __pos->_M_next = __curr->_M_next;
72 _Tp_alloc_type __a(_M_get_Node_allocator());
73 allocator_traits<_Tp_alloc_type>::destroy(__a, __curr->_M_valptr());
76 return __pos->_M_next;
79 template<typename _Tp, typename _Alloc>
81 _Fwd_list_base<_Tp, _Alloc>::
82 _M_erase_after(_Fwd_list_node_base* __pos,
83 _Fwd_list_node_base* __last)
85 _Node* __curr = static_cast<_Node*>(__pos->_M_next);
86 while (__curr != __last)
88 _Node* __temp = __curr;
89 __curr = static_cast<_Node*>(__curr->_M_next);
90 _Tp_alloc_type __a(_M_get_Node_allocator());
91 allocator_traits<_Tp_alloc_type>::destroy(__a, __temp->_M_valptr());
95 __pos->_M_next = __last;
99 // Called by the range constructor to implement [23.3.4.2]/9
100 template<typename _Tp, typename _Alloc>
101 template<typename _InputIterator>
103 forward_list<_Tp, _Alloc>::
104 _M_range_initialize(_InputIterator __first, _InputIterator __last)
106 _Node_base* __to = &this->_M_impl._M_head;
107 for (; __first != __last; ++__first)
109 __to->_M_next = this->_M_create_node(*__first);
110 __to = __to->_M_next;
114 // Called by forward_list(n,v,a).
115 template<typename _Tp, typename _Alloc>
117 forward_list<_Tp, _Alloc>::
118 _M_fill_initialize(size_type __n, const value_type& __value)
120 _Node_base* __to = &this->_M_impl._M_head;
123 __to->_M_next = this->_M_create_node(__value);
124 __to = __to->_M_next;
128 template<typename _Tp, typename _Alloc>
130 forward_list<_Tp, _Alloc>::
131 _M_default_initialize(size_type __n)
133 _Node_base* __to = &this->_M_impl._M_head;
136 __to->_M_next = this->_M_create_node();
137 __to = __to->_M_next;
141 template<typename _Tp, typename _Alloc>
142 forward_list<_Tp, _Alloc>&
143 forward_list<_Tp, _Alloc>::
144 operator=(const forward_list& __list)
146 if (std::__addressof(__list) != this)
148 if (_Node_alloc_traits::_S_propagate_on_copy_assign())
150 auto& __this_alloc = this->_M_get_Node_allocator();
151 auto& __that_alloc = __list._M_get_Node_allocator();
152 if (!_Node_alloc_traits::_S_always_equal()
153 && __this_alloc != __that_alloc)
155 // replacement allocator cannot free existing storage
158 std::__alloc_on_copy(__this_alloc, __that_alloc);
160 assign(__list.cbegin(), __list.cend());
165 template<typename _Tp, typename _Alloc>
167 forward_list<_Tp, _Alloc>::
168 _M_default_insert_after(const_iterator __pos, size_type __n)
170 const_iterator __saved_pos = __pos;
174 __pos = emplace_after(__pos);
178 erase_after(__saved_pos, ++__pos);
179 __throw_exception_again;
183 template<typename _Tp, typename _Alloc>
185 forward_list<_Tp, _Alloc>::
186 resize(size_type __sz)
188 iterator __k = before_begin();
191 while (__k._M_next() != end() && __len < __sz)
197 erase_after(__k, end());
199 _M_default_insert_after(__k, __sz - __len);
202 template<typename _Tp, typename _Alloc>
204 forward_list<_Tp, _Alloc>::
205 resize(size_type __sz, const value_type& __val)
207 iterator __k = before_begin();
210 while (__k._M_next() != end() && __len < __sz)
216 erase_after(__k, end());
218 insert_after(__k, __sz - __len, __val);
221 template<typename _Tp, typename _Alloc>
222 typename forward_list<_Tp, _Alloc>::iterator
223 forward_list<_Tp, _Alloc>::
224 _M_splice_after(const_iterator __pos,
225 const_iterator __before, const_iterator __last)
227 _Node_base* __tmp = const_cast<_Node_base*>(__pos._M_node);
228 _Node_base* __b = const_cast<_Node_base*>(__before._M_node);
229 _Node_base* __end = __b;
231 while (__end && __end->_M_next != __last._M_node)
232 __end = __end->_M_next;
235 return iterator(__tmp->_M_transfer_after(__b, __end));
237 return iterator(__tmp);
240 template<typename _Tp, typename _Alloc>
242 forward_list<_Tp, _Alloc>::
243 splice_after(const_iterator __pos, forward_list&&,
244 const_iterator __i) noexcept
246 const_iterator __j = __i;
249 if (__pos == __i || __pos == __j)
252 _Node_base* __tmp = const_cast<_Node_base*>(__pos._M_node);
253 __tmp->_M_transfer_after(const_cast<_Node_base*>(__i._M_node),
254 const_cast<_Node_base*>(__j._M_node));
257 template<typename _Tp, typename _Alloc>
258 typename forward_list<_Tp, _Alloc>::iterator
259 forward_list<_Tp, _Alloc>::
260 insert_after(const_iterator __pos, size_type __n, const _Tp& __val)
264 forward_list __tmp(__n, __val, get_allocator());
265 return _M_splice_after(__pos, __tmp.before_begin(), __tmp.end());
268 return iterator(const_cast<_Node_base*>(__pos._M_node));
271 template<typename _Tp, typename _Alloc>
272 template<typename _InputIterator, typename>
273 typename forward_list<_Tp, _Alloc>::iterator
274 forward_list<_Tp, _Alloc>::
275 insert_after(const_iterator __pos,
276 _InputIterator __first, _InputIterator __last)
278 forward_list __tmp(__first, __last, get_allocator());
280 return _M_splice_after(__pos, __tmp.before_begin(), __tmp.end());
282 return iterator(const_cast<_Node_base*>(__pos._M_node));
285 template<typename _Tp, typename _Alloc>
287 forward_list<_Tp, _Alloc>::
288 remove(const _Tp& __val)
290 _Node_base* __curr = &this->_M_impl._M_head;
291 _Node_base* __extra = nullptr;
293 while (_Node* __tmp = static_cast<_Node*>(__curr->_M_next))
295 if (*__tmp->_M_valptr() == __val)
297 if (__tmp->_M_valptr() != std::__addressof(__val))
299 this->_M_erase_after(__curr);
305 __curr = __curr->_M_next;
309 this->_M_erase_after(__extra);
312 template<typename _Tp, typename _Alloc>
313 template<typename _Pred>
315 forward_list<_Tp, _Alloc>::
316 remove_if(_Pred __pred)
318 _Node_base* __curr = &this->_M_impl._M_head;
319 while (_Node* __tmp = static_cast<_Node*>(__curr->_M_next))
321 if (__pred(*__tmp->_M_valptr()))
322 this->_M_erase_after(__curr);
324 __curr = __curr->_M_next;
328 template<typename _Tp, typename _Alloc>
329 template<typename _BinPred>
331 forward_list<_Tp, _Alloc>::
332 unique(_BinPred __binary_pred)
334 iterator __first = begin();
335 iterator __last = end();
336 if (__first == __last)
338 iterator __next = __first;
339 while (++__next != __last)
341 if (__binary_pred(*__first, *__next))
342 erase_after(__first);
349 template<typename _Tp, typename _Alloc>
350 template<typename _Comp>
352 forward_list<_Tp, _Alloc>::
353 merge(forward_list&& __list, _Comp __comp)
355 _Node_base* __node = &this->_M_impl._M_head;
356 while (__node->_M_next && __list._M_impl._M_head._M_next)
358 if (__comp(*static_cast<_Node*>
359 (__list._M_impl._M_head._M_next)->_M_valptr(),
361 (__node->_M_next)->_M_valptr()))
362 __node->_M_transfer_after(&__list._M_impl._M_head,
363 __list._M_impl._M_head._M_next);
364 __node = __node->_M_next;
366 if (__list._M_impl._M_head._M_next)
368 __node->_M_next = __list._M_impl._M_head._M_next;
369 __list._M_impl._M_head._M_next = 0;
373 template<typename _Tp, typename _Alloc>
375 operator==(const forward_list<_Tp, _Alloc>& __lx,
376 const forward_list<_Tp, _Alloc>& __ly)
378 // We don't have size() so we need to walk through both lists
379 // making sure both iterators are valid.
380 auto __ix = __lx.cbegin();
381 auto __iy = __ly.cbegin();
382 while (__ix != __lx.cend() && __iy != __ly.cend())
389 if (__ix == __lx.cend() && __iy == __ly.cend())
395 template<typename _Tp, class _Alloc>
396 template<typename _Comp>
398 forward_list<_Tp, _Alloc>::
401 // If `next' is 0, return immediately.
402 _Node* __list = static_cast<_Node*>(this->_M_impl._M_head._M_next);
406 unsigned long __insize = 1;
414 // Count number of merges we do in this pass.
415 unsigned long __nmerges = 0;
420 // There exists a merge to be done.
421 // Step `insize' places along from p.
423 unsigned long __psize = 0;
424 for (unsigned long __i = 0; __i < __insize; ++__i)
427 __q = static_cast<_Node*>(__q->_M_next);
432 // If q hasn't fallen off end, we have two lists to merge.
433 unsigned long __qsize = __insize;
435 // Now we have two lists; merge them.
436 while (__psize > 0 || (__qsize > 0 && __q))
438 // Decide whether next node of merge comes from p or q.
442 // p is empty; e must come from q.
444 __q = static_cast<_Node*>(__q->_M_next);
447 else if (__qsize == 0 || !__q)
449 // q is empty; e must come from p.
451 __p = static_cast<_Node*>(__p->_M_next);
454 else if (__comp(*__p->_M_valptr(), *__q->_M_valptr()))
456 // First node of p is lower; e must come from p.
458 __p = static_cast<_Node*>(__p->_M_next);
463 // First node of q is lower; e must come from q.
465 __q = static_cast<_Node*>(__q->_M_next);
469 // Add the next node to the merged list.
471 __tail->_M_next = __e;
477 // Now p has stepped `insize' places along, and q has too.
482 // If we have done only one merge, we're finished.
483 // Allow for nmerges == 0, the empty list case.
486 this->_M_impl._M_head._M_next = __list;
490 // Otherwise repeat, merging lists twice the size.
495 _GLIBCXX_END_NAMESPACE_CONTAINER
498 #endif /* _FORWARD_LIST_TCC */