1 // <forward_list.tcc> -*- C++ -*-
3 // Copyright (C) 2008, 2009 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_NAMESPACE(std)
34 template<typename _Alloc>
36 _Fwd_list_node_base<_Alloc>::
37 _M_transfer_after(_Pointer __bbegin)
39 _Pointer __bend = __bbegin;
40 while (__bend && __bend->_M_next)
41 __bend = __bend->_M_next;
42 _M_transfer_after(__bbegin, __bend);
45 template<typename _Alloc>
47 _Fwd_list_node_base<_Alloc>::
48 _M_transfer_after(_Pointer __bbegin, _Pointer __bend)
50 _Pointer __keep = __bbegin->_M_next;
53 __bbegin->_M_next = __bend->_M_next;
54 __bend->_M_next = _M_next;
57 __bbegin->_M_next = 0;
61 template<typename _Alloc>
63 _Fwd_list_node_base<_Alloc>::
66 _Pointer __tail = _M_next;
69 while (_Pointer __temp = __tail->_M_next)
71 _Pointer __keep = _M_next;
73 __tail->_M_next = __temp->_M_next;
74 _M_next->_M_next = __keep;
79 * @brief Sort the singly linked list starting after this node.
80 * This node is assumed to be an empty head node (of type
81 * _Fwd_list_node_base).
83 template<typename _Tp, class _Alloc>
84 template<typename _Comp>
86 _Fwd_list_node<_Tp, _Alloc>::
87 _M_sort_after(_Comp __comp)
89 // If `next' is 0, return immediately.
90 _Pointer __list = __static_pointer_cast<_Pointer>(this->_M_next);
94 unsigned long __insize = 1;
98 _Pointer __p = __list;
102 // Count number of merges we do in this pass.
103 unsigned long __nmerges = 0;
108 // There exists a merge to be done.
109 // Step `insize' places along from p.
111 unsigned long __psize = 0;
112 for (unsigned long __i = 0; __i < __insize; ++__i)
115 __q = __static_pointer_cast<_Pointer>(__q->_M_next);
120 // If q hasn't fallen off end, we have two lists to merge.
121 unsigned long __qsize = __insize;
123 // Now we have two lists; merge them.
124 while (__psize > 0 || (__qsize > 0 && __q))
126 // Decide whether next node of merge comes from p or q.
130 // p is empty; e must come from q.
132 __q = __static_pointer_cast<_Pointer>(__q->_M_next);
135 else if (__qsize == 0 || !__q)
137 // q is empty; e must come from p.
139 __p = __static_pointer_cast<_Pointer>(__p->_M_next);
142 else if (__comp(__p->_M_value, __q->_M_value))
144 // First node of p is lower; e must come from p.
146 __p = __static_pointer_cast<_Pointer>(__p->_M_next);
151 // First node of q is lower; e must come from q.
153 __q = __static_pointer_cast<_Pointer>(__q->_M_next);
157 // Add the next node to the merged list.
159 __tail->_M_next = __e;
165 // Now p has stepped `insize' places along, and q has too.
170 // If we have done only one merge, we're finished.
171 // Allow for nmerges == 0, the empty list case.
174 this->_M_next = __list;
178 // Otherwise repeat, merging lists twice the size.
183 template<typename _Tp, typename _Alloc>
184 _Fwd_list_base<_Tp, _Alloc>::
185 _Fwd_list_base(const _Fwd_list_base& __lst, const _Alloc& __a)
188 this->_M_impl._M_head._M_next = 0;
189 typename _Node_base::_Pointer __to = &this->_M_impl._M_head;
190 typename _Node::_Pointer __curr
191 = __static_pointer_cast<typename _Node::_Pointer>
192 (__lst._M_impl._M_head._M_next);
195 __to->_M_next = _M_create_node(__curr->_M_value);
196 __to = __to->_M_next;
197 __curr = __static_pointer_cast<typename _Node::_Pointer>
202 template<typename _Tp, typename _Alloc>
203 template<typename... _Args>
204 typename _Fwd_list_base<_Tp, _Alloc>::_Node_base::_Pointer
205 _Fwd_list_base<_Tp, _Alloc>::
206 _M_insert_after(const_iterator __pos, _Args&&... __args)
208 typename _Node_base::_Pointer __to
209 = __const_pointer_cast<typename _Node_base::_Pointer>
211 typename _Node::_Pointer __thing
212 = __static_pointer_cast<typename _Node::_Pointer>(
213 _M_create_node(std::forward<_Args>(__args)...) );
214 __thing->_M_next = __to->_M_next;
215 __to->_M_next = __thing;
216 return __static_pointer_cast<typename _Node_base::_Pointer>
220 template<typename _Tp, typename _Alloc>
221 typename _Fwd_list_base<_Tp, _Alloc>::_Node_base::_Pointer
222 _Fwd_list_base<_Tp, _Alloc>::
223 _M_erase_after(typename _Node_base::_Pointer __pos)
225 typename _Node::_Pointer __curr
226 = __static_pointer_cast<typename _Node::_Pointer>(__pos->_M_next);
229 typename _Node_base::_Pointer __next = __curr->_M_next;
230 __pos->_M_next = __next;
231 _M_get_Node_allocator().destroy(__curr);
237 template<typename _Tp, typename _Alloc>
238 typename _Fwd_list_base<_Tp, _Alloc>::_Node_base::_Pointer
239 _Fwd_list_base<_Tp, _Alloc>::
240 _M_erase_after(typename _Node_base::_Pointer __pos,
241 typename _Node_base::_Pointer __last)
243 typename _Node::_Pointer __curr
244 = __static_pointer_cast<typename _Node::_Pointer>(__pos->_M_next);
247 typename _Node::_Pointer __temp = __curr;
248 __curr = __static_pointer_cast<typename _Node::_Pointer>
250 _M_get_Node_allocator().destroy(__temp);
252 __pos->_M_next = __curr;
253 if (__temp == __last)
259 // Called by the range constructor to implement [23.1.1]/9
260 template<typename _Tp, typename _Alloc>
261 template<typename _InputIterator>
263 forward_list<_Tp, _Alloc>::
264 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
267 typename _Node_base::_Pointer __to = &this->_M_impl._M_head;
268 for (; __first != __last; ++__first)
270 __to->_M_next = this->_M_create_node(*__first);
271 __to = __to->_M_next;
275 // Called by forward_list(n,v,a), and the range constructor
276 // when it turns out to be the same thing.
277 template<typename _Tp, typename _Alloc>
279 forward_list<_Tp, _Alloc>::
280 _M_fill_initialize(size_type __n, const value_type& __value)
282 typename _Node_base::_Pointer __to = &this->_M_impl._M_head;
283 for (; __n > 0; --__n)
285 __to->_M_next = this->_M_create_node(__value);
286 __to = __to->_M_next;
290 template<typename _Tp, typename _Alloc>
291 forward_list<_Tp, _Alloc>&
292 forward_list<_Tp, _Alloc>::
293 operator=(const forward_list& __list)
297 iterator __prev1 = before_begin();
298 iterator __curr1 = begin();
299 iterator __last1 = end();
300 const_iterator __first2 = __list.cbegin();
301 const_iterator __last2 = __list.cend();
302 while (__curr1 != __last1 && __first2 != __last2)
304 *__curr1 = *__first2;
309 if (__first2 == __last2)
310 erase_after(__prev1, __last1);
312 insert_after(__prev1, __first2, __last2);
317 template<typename _Tp, typename _Alloc>
319 forward_list<_Tp, _Alloc>::
320 resize(size_type __sz, value_type __val)
322 iterator __k = before_begin();
325 while (__k._M_next() != end() && __len < __sz)
331 erase_after(__k, end());
333 insert_after(__k, __sz - __len, __val);
336 template<typename _Tp, typename _Alloc>
338 forward_list<_Tp, _Alloc>::
339 splice_after(const_iterator __pos, forward_list&& __list)
341 if (!__list.empty() && &__list != this)
343 typename _Node_base::_Pointer __tmp
344 = __const_pointer_cast<typename _Node_base::_Pointer>
346 const_iterator __before = __list.cbefore_begin();
347 __tmp->_M_transfer_after(__const_pointer_cast
348 <typename _Node_base::_Pointer>
353 template<typename _Tp, typename _Alloc>
355 forward_list<_Tp, _Alloc>::
356 splice_after(const_iterator __pos, forward_list&& __list,
357 const_iterator __before, const_iterator __last)
359 typename _Node_base::_Pointer __tmp
360 = __const_pointer_cast<typename _Node_base::_Pointer>(__pos._M_node);
361 __tmp->_M_transfer_after(__const_pointer_cast
362 <typename _Node_base::_Pointer>
365 <typename _Node_base::_Pointer>
369 template<typename _Tp, typename _Alloc>
371 forward_list<_Tp, _Alloc>::
372 remove(const _Tp& __val)
374 typename _Node::_Pointer __curr
375 = __static_pointer_cast<typename _Node::_Pointer>
376 (&this->_M_impl._M_head);
377 while (typename _Node::_Pointer __temp =
378 __static_pointer_cast<typename _Node::_Pointer>(__curr->_M_next))
380 if (__temp->_M_value == __val)
381 this->_M_erase_after(__curr);
383 __curr = __static_pointer_cast<typename _Node::_Pointer>
388 template<typename _Tp, typename _Alloc>
389 template<typename _Pred>
391 forward_list<_Tp, _Alloc>::
392 remove_if(_Pred __pred)
394 typename _Node::_Pointer __curr
395 = __static_pointer_cast<typename _Node::_Pointer>
396 (&this->_M_impl._M_head);
397 while (typename _Node::_Pointer __temp =
398 __static_pointer_cast<typename _Node::_Pointer>(__curr->_M_next))
400 if (__pred(__temp->_M_value))
401 this->_M_erase_after(__curr);
403 __curr = __static_pointer_cast<typename _Node::_Pointer>
408 template<typename _Tp, typename _Alloc>
409 template<typename _BinPred>
411 forward_list<_Tp, _Alloc>::
412 unique(_BinPred __binary_pred)
414 iterator __first = begin();
415 iterator __last = end();
416 if (__first == __last)
418 iterator __next = __first;
419 while (++__next != __last)
421 if (__binary_pred(*__first, *__next))
422 erase_after(__first);
429 template<typename _Tp, typename _Alloc>
430 template<typename _Comp>
432 forward_list<_Tp, _Alloc>::
433 merge(forward_list&& __list, _Comp __comp)
435 typename _Node_base::_Pointer __node = &this->_M_impl._M_head;
436 while (__node->_M_next && __list._M_impl._M_head._M_next)
438 if (__comp(__static_pointer_cast<typename _Node::_Pointer>
439 (__list._M_impl._M_head._M_next)->_M_value,
440 __static_pointer_cast<typename _Node::_Pointer>
441 (__node->_M_next)->_M_value))
442 __node->_M_transfer_after(&__list._M_impl._M_head,
443 __list._M_impl._M_head._M_next);
444 __node = __node->_M_next;
446 if (__list._M_impl._M_head._M_next)
448 __node->_M_next = __list._M_impl._M_head._M_next;
449 __list._M_impl._M_head._M_next = 0;
453 template<typename _Tp, typename _Alloc>
455 operator==(const forward_list<_Tp, _Alloc>& __lx,
456 const forward_list<_Tp, _Alloc>& __ly)
458 // We don't have size() so we need to walk through both lists
459 // making sure both iterators are valid.
460 auto __ix = __lx.cbegin();
461 auto __iy = __ly.cbegin();
462 while (__ix != __lx.cend() && __iy != __ly.cend())
469 if (__ix == __lx.cend() && __iy == __ly.cend())
475 _GLIBCXX_END_NAMESPACE // namespace std
477 #endif /* _FORWARD_LIST_TCC */