1 // <forward_list.tcc> -*- C++ -*-
3 // Copyright (C) 2008, 2009, 2010 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 forward_list.tcc
26 * This is a Standard C++ Library header.
29 #ifndef _FORWARD_LIST_TCC
30 #define _FORWARD_LIST_TCC 1
32 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
34 template<typename _Tp, typename _Alloc>
35 _Fwd_list_base<_Tp, _Alloc>::
36 _Fwd_list_base(const _Fwd_list_base& __lst, const _Alloc& __a)
39 this->_M_impl._M_head._M_next = 0;
40 _Fwd_list_node_base* __to = &this->_M_impl._M_head;
41 _Node* __curr = static_cast<_Node*>(__lst._M_impl._M_head._M_next);
45 __to->_M_next = _M_create_node(__curr->_M_value);
47 __curr = static_cast<_Node*>(__curr->_M_next);
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 _M_get_Node_allocator().destroy(__curr);
74 return __pos->_M_next;
77 template<typename _Tp, typename _Alloc>
79 _Fwd_list_base<_Tp, _Alloc>::
80 _M_erase_after(_Fwd_list_node_base* __pos,
81 _Fwd_list_node_base* __last)
83 _Node* __curr = static_cast<_Node*>(__pos->_M_next);
84 while (__curr != __last)
86 _Node* __temp = __curr;
87 __curr = static_cast<_Node*>(__curr->_M_next);
88 _M_get_Node_allocator().destroy(__temp);
91 __pos->_M_next = __last;
95 // Called by the range constructor to implement [23.1.1]/9
96 template<typename _Tp, typename _Alloc>
97 template<typename _InputIterator>
99 forward_list<_Tp, _Alloc>::
100 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
103 _Node_base* __to = &this->_M_impl._M_head;
104 for (; __first != __last; ++__first)
106 __to->_M_next = this->_M_create_node(*__first);
107 __to = __to->_M_next;
111 // Called by forward_list(n,v,a), and the range constructor
112 // when it turns out to be the same thing.
113 template<typename _Tp, typename _Alloc>
115 forward_list<_Tp, _Alloc>::
116 _M_fill_initialize(size_type __n, const value_type& __value)
118 _Node_base* __to = &this->_M_impl._M_head;
121 __to->_M_next = this->_M_create_node(__value);
122 __to = __to->_M_next;
126 template<typename _Tp, typename _Alloc>
128 forward_list<_Tp, _Alloc>::
129 _M_default_initialize(size_type __n)
131 _Node_base* __to = &this->_M_impl._M_head;
134 __to->_M_next = this->_M_create_node();
135 __to = __to->_M_next;
139 template<typename _Tp, typename _Alloc>
140 forward_list<_Tp, _Alloc>&
141 forward_list<_Tp, _Alloc>::
142 operator=(const forward_list& __list)
146 iterator __prev1 = before_begin();
147 iterator __curr1 = begin();
148 iterator __last1 = end();
149 const_iterator __first2 = __list.cbegin();
150 const_iterator __last2 = __list.cend();
151 while (__curr1 != __last1 && __first2 != __last2)
153 *__curr1 = *__first2;
158 if (__first2 == __last2)
159 erase_after(__prev1, __last1);
161 insert_after(__prev1, __first2, __last2);
166 template<typename _Tp, typename _Alloc>
168 forward_list<_Tp, _Alloc>::
169 _M_default_insert_after(const_iterator __pos, size_type __n)
171 const_iterator __saved_pos = __pos;
175 __pos = emplace_after(__pos);
179 erase_after(__saved_pos, ++__pos);
180 __throw_exception_again;
184 template<typename _Tp, typename _Alloc>
186 forward_list<_Tp, _Alloc>::
187 resize(size_type __sz)
189 iterator __k = before_begin();
192 while (__k._M_next() != end() && __len < __sz)
198 erase_after(__k, end());
200 _M_default_insert_after(__k, __sz - __len);
203 template<typename _Tp, typename _Alloc>
205 forward_list<_Tp, _Alloc>::
206 resize(size_type __sz, const value_type& __val)
208 iterator __k = before_begin();
211 while (__k._M_next() != end() && __len < __sz)
217 erase_after(__k, end());
219 insert_after(__k, __sz - __len, __val);
222 template<typename _Tp, typename _Alloc>
223 typename forward_list<_Tp, _Alloc>::iterator
224 forward_list<_Tp, _Alloc>::
225 _M_splice_after(const_iterator __pos, forward_list&& __list)
227 _Node_base* __tmp = const_cast<_Node_base*>(__pos._M_node);
228 iterator __before = __list.before_begin();
229 return iterator(__tmp->_M_transfer_after(__before._M_node));
232 template<typename _Tp, typename _Alloc>
234 forward_list<_Tp, _Alloc>::
235 splice_after(const_iterator __pos, forward_list&&,
236 const_iterator __before, const_iterator __last)
238 _Node_base* __tmp = const_cast<_Node_base*>(__pos._M_node);
239 __tmp->_M_transfer_after(const_cast<_Node_base*>(__before._M_node),
240 const_cast<_Node_base*>(__last._M_node));
243 template<typename _Tp, typename _Alloc>
244 typename forward_list<_Tp, _Alloc>::iterator
245 forward_list<_Tp, _Alloc>::
246 insert_after(const_iterator __pos, size_type __n, const _Tp& __val)
250 forward_list __tmp(__n, __val, this->_M_get_Node_allocator());
251 return _M_splice_after(__pos, std::move(__tmp));
254 return iterator(const_cast<_Node_base*>(__pos._M_node));
257 template<typename _Tp, typename _Alloc>
258 template<typename _InputIterator>
259 typename forward_list<_Tp, _Alloc>::iterator
260 forward_list<_Tp, _Alloc>::
261 insert_after(const_iterator __pos,
262 _InputIterator __first, _InputIterator __last)
264 forward_list __tmp(__first, __last, this->_M_get_Node_allocator());
266 return _M_splice_after(__pos, std::move(__tmp));
268 return iterator(const_cast<_Node_base*>(__pos._M_node));
271 template<typename _Tp, typename _Alloc>
272 typename forward_list<_Tp, _Alloc>::iterator
273 forward_list<_Tp, _Alloc>::
274 insert_after(const_iterator __pos, std::initializer_list<_Tp> __il)
278 forward_list __tmp(__il, this->_M_get_Node_allocator());
279 return _M_splice_after(__pos, std::move(__tmp));
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* __curr = static_cast<_Node*>(&this->_M_impl._M_head);
293 while (_Node* __tmp = static_cast<_Node*>(__curr->_M_next))
295 if (__tmp->_M_value == __val)
297 if (std::__addressof(__tmp->_M_value)
298 != std::__addressof(__val))
300 this->_M_erase_after(__curr);
306 __curr = static_cast<_Node*>(__curr->_M_next);
310 this->_M_erase_after(__extra);
313 template<typename _Tp, typename _Alloc>
314 template<typename _Pred>
316 forward_list<_Tp, _Alloc>::
317 remove_if(_Pred __pred)
319 _Node* __curr = static_cast<_Node*>(&this->_M_impl._M_head);
320 while (_Node* __tmp = static_cast<_Node*>(__curr->_M_next))
322 if (__pred(__tmp->_M_value))
323 this->_M_erase_after(__curr);
325 __curr = static_cast<_Node*>(__curr->_M_next);
329 template<typename _Tp, typename _Alloc>
330 template<typename _BinPred>
332 forward_list<_Tp, _Alloc>::
333 unique(_BinPred __binary_pred)
335 iterator __first = begin();
336 iterator __last = end();
337 if (__first == __last)
339 iterator __next = __first;
340 while (++__next != __last)
342 if (__binary_pred(*__first, *__next))
343 erase_after(__first);
350 template<typename _Tp, typename _Alloc>
351 template<typename _Comp>
353 forward_list<_Tp, _Alloc>::
354 merge(forward_list&& __list, _Comp __comp)
356 _Node_base* __node = &this->_M_impl._M_head;
357 while (__node->_M_next && __list._M_impl._M_head._M_next)
359 if (__comp(static_cast<_Node*>
360 (__list._M_impl._M_head._M_next)->_M_value,
362 (__node->_M_next)->_M_value))
363 __node->_M_transfer_after(&__list._M_impl._M_head,
364 __list._M_impl._M_head._M_next);
365 __node = __node->_M_next;
367 if (__list._M_impl._M_head._M_next)
369 __node->_M_next = __list._M_impl._M_head._M_next;
370 __list._M_impl._M_head._M_next = 0;
374 template<typename _Tp, typename _Alloc>
376 operator==(const forward_list<_Tp, _Alloc>& __lx,
377 const forward_list<_Tp, _Alloc>& __ly)
379 // We don't have size() so we need to walk through both lists
380 // making sure both iterators are valid.
381 auto __ix = __lx.cbegin();
382 auto __iy = __ly.cbegin();
383 while (__ix != __lx.cend() && __iy != __ly.cend())
390 if (__ix == __lx.cend() && __iy == __ly.cend())
396 template<typename _Tp, class _Alloc>
397 template<typename _Comp>
399 forward_list<_Tp, _Alloc>::
402 // If `next' is 0, return immediately.
403 _Node* __list = static_cast<_Node*>(this->_M_impl._M_head._M_next);
407 unsigned long __insize = 1;
415 // Count number of merges we do in this pass.
416 unsigned long __nmerges = 0;
421 // There exists a merge to be done.
422 // Step `insize' places along from p.
424 unsigned long __psize = 0;
425 for (unsigned long __i = 0; __i < __insize; ++__i)
428 __q = static_cast<_Node*>(__q->_M_next);
433 // If q hasn't fallen off end, we have two lists to merge.
434 unsigned long __qsize = __insize;
436 // Now we have two lists; merge them.
437 while (__psize > 0 || (__qsize > 0 && __q))
439 // Decide whether next node of merge comes from p or q.
443 // p is empty; e must come from q.
445 __q = static_cast<_Node*>(__q->_M_next);
448 else if (__qsize == 0 || !__q)
450 // q is empty; e must come from p.
452 __p = static_cast<_Node*>(__p->_M_next);
455 else if (__comp(__p->_M_value, __q->_M_value))
457 // First node of p is lower; e must come from p.
459 __p = static_cast<_Node*>(__p->_M_next);
464 // First node of q is lower; e must come from q.
466 __q = static_cast<_Node*>(__q->_M_next);
470 // Add the next node to the merged list.
472 __tail->_M_next = __e;
478 // Now p has stepped `insize' places along, and q has too.
483 // If we have done only one merge, we're finished.
484 // Allow for nmerges == 0, the empty list case.
487 this->_M_impl._M_head._M_next = __list;
491 // Otherwise repeat, merging lists twice the size.
496 _GLIBCXX_END_NESTED_NAMESPACE // namespace std
498 #endif /* _FORWARD_LIST_TCC */