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;
78 template<typename _Tp, typename _Alloc>
79 _Fwd_list_base<_Tp, _Alloc>::
80 _Fwd_list_base(const _Fwd_list_base& __lst, const _Alloc& __a)
83 this->_M_impl._M_head._M_next = 0;
84 typename _Node_base::_Pointer __to = &this->_M_impl._M_head;
85 typename _Node::_Pointer __curr
86 = __static_pointer_cast<typename _Node::_Pointer>
87 (__lst._M_impl._M_head._M_next);
90 __to->_M_next = _M_create_node(__curr->_M_value);
92 __curr = __static_pointer_cast<typename _Node::_Pointer>
97 template<typename _Tp, typename _Alloc>
98 template<typename... _Args>
99 typename _Fwd_list_base<_Tp, _Alloc>::_Node_base::_Pointer
100 _Fwd_list_base<_Tp, _Alloc>::
101 _M_insert_after(const_iterator __pos, _Args&&... __args)
103 typename _Node_base::_Pointer __to
104 = __const_pointer_cast<typename _Node_base::_Pointer>
106 typename _Node::_Pointer __thing
107 = __static_pointer_cast<typename _Node::_Pointer>(
108 _M_create_node(std::forward<_Args>(__args)...) );
109 __thing->_M_next = __to->_M_next;
110 __to->_M_next = __thing;
111 return __static_pointer_cast<typename _Node_base::_Pointer>
115 template<typename _Tp, typename _Alloc>
117 _Fwd_list_base<_Tp, _Alloc>::
118 _M_erase_after(typename _Node_base::_Pointer __pos)
120 typename _Node::_Pointer __curr
121 = __static_pointer_cast<typename _Node::_Pointer>(__pos->_M_next);
122 __pos->_M_next = __curr->_M_next;
123 _M_get_Node_allocator().destroy(__curr);
127 template<typename _Tp, typename _Alloc>
129 _Fwd_list_base<_Tp, _Alloc>::
130 _M_erase_after(typename _Node_base::_Pointer __pos,
131 typename _Node_base::_Pointer __last)
133 typename _Node::_Pointer __curr
134 = __static_pointer_cast<typename _Node::_Pointer>(__pos->_M_next);
135 while (__curr != __last)
137 typename _Node::_Pointer __temp = __curr;
138 __curr = __static_pointer_cast<typename _Node::_Pointer>
140 _M_get_Node_allocator().destroy(__temp);
143 __pos->_M_next = __last;
146 // Called by the range constructor to implement [23.1.1]/9
147 template<typename _Tp, typename _Alloc>
148 template<typename _InputIterator>
150 forward_list<_Tp, _Alloc>::
151 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
154 typename _Node_base::_Pointer __to = &this->_M_impl._M_head;
155 for (; __first != __last; ++__first)
157 __to->_M_next = this->_M_create_node(*__first);
158 __to = __to->_M_next;
162 // Called by forward_list(n,v,a), and the range constructor
163 // when it turns out to be the same thing.
164 template<typename _Tp, typename _Alloc>
166 forward_list<_Tp, _Alloc>::
167 _M_fill_initialize(size_type __n, const value_type& __value)
169 typename _Node_base::_Pointer __to = &this->_M_impl._M_head;
170 for (; __n > 0; --__n)
172 __to->_M_next = this->_M_create_node(__value);
173 __to = __to->_M_next;
177 template<typename _Tp, typename _Alloc>
178 forward_list<_Tp, _Alloc>&
179 forward_list<_Tp, _Alloc>::
180 operator=(const forward_list& __list)
184 iterator __prev1 = before_begin();
185 iterator __curr1 = begin();
186 iterator __last1 = end();
187 const_iterator __first2 = __list.cbegin();
188 const_iterator __last2 = __list.cend();
189 while (__curr1 != __last1 && __first2 != __last2)
191 *__curr1 = *__first2;
196 if (__first2 == __last2)
197 erase_after(__prev1, __last1);
199 insert_after(__prev1, __first2, __last2);
204 template<typename _Tp, typename _Alloc>
206 forward_list<_Tp, _Alloc>::
207 resize(size_type __sz, value_type __val)
209 iterator __k = before_begin();
212 while (__k._M_next() != end() && __len < __sz)
218 erase_after(__k, end());
220 insert_after(__k, __sz - __len, __val);
223 template<typename _Tp, typename _Alloc>
225 forward_list<_Tp, _Alloc>::
226 splice_after(const_iterator __pos, forward_list&& __list)
228 if (!__list.empty() && &__list != this)
230 typename _Node_base::_Pointer __tmp
231 = __const_pointer_cast<typename _Node_base::_Pointer>
233 const_iterator __before = __list.cbefore_begin();
234 __tmp->_M_transfer_after(__const_pointer_cast
235 <typename _Node_base::_Pointer>
240 template<typename _Tp, typename _Alloc>
242 forward_list<_Tp, _Alloc>::
243 splice_after(const_iterator __pos, forward_list&& __list,
244 const_iterator __before, const_iterator __last)
246 typename _Node_base::_Pointer __tmp
247 = __const_pointer_cast<typename _Node_base::_Pointer>(__pos._M_node);
248 __tmp->_M_transfer_after(__const_pointer_cast
249 <typename _Node_base::_Pointer>
252 <typename _Node_base::_Pointer>
256 template<typename _Tp, typename _Alloc>
258 forward_list<_Tp, _Alloc>::
259 remove(const _Tp& __val)
261 typename _Node::_Pointer __curr
262 = __static_pointer_cast<typename _Node::_Pointer>
263 (&this->_M_impl._M_head);
264 while (typename _Node::_Pointer __temp =
265 __static_pointer_cast<typename _Node::_Pointer>(__curr->_M_next))
267 if (__temp->_M_value == __val)
268 this->_M_erase_after(__curr);
270 __curr = __static_pointer_cast<typename _Node::_Pointer>
275 template<typename _Tp, typename _Alloc>
276 template<typename _Pred>
278 forward_list<_Tp, _Alloc>::
279 remove_if(_Pred __pred)
281 typename _Node::_Pointer __curr
282 = __static_pointer_cast<typename _Node::_Pointer>
283 (&this->_M_impl._M_head);
284 while (typename _Node::_Pointer __temp =
285 __static_pointer_cast<typename _Node::_Pointer>(__curr->_M_next))
287 if (__pred(__temp->_M_value))
288 this->_M_erase_after(__curr);
290 __curr = __static_pointer_cast<typename _Node::_Pointer>
295 template<typename _Tp, typename _Alloc>
296 template<typename _BinPred>
298 forward_list<_Tp, _Alloc>::
299 unique(_BinPred __binary_pred)
301 iterator __first = begin();
302 iterator __last = end();
303 if (__first == __last)
305 iterator __next = __first;
306 while (++__next != __last)
308 if (__binary_pred(*__first, *__next))
309 erase_after(__first);
316 template<typename _Tp, typename _Alloc>
317 template<typename _Comp>
319 forward_list<_Tp, _Alloc>::
320 merge(forward_list&& __list, _Comp __comp)
322 typename _Node_base::_Pointer __node = &this->_M_impl._M_head;
323 while (__node->_M_next && __list._M_impl._M_head._M_next)
325 if (__comp(__static_pointer_cast<typename _Node::_Pointer>
326 (__list._M_impl._M_head._M_next)->_M_value,
327 __static_pointer_cast<typename _Node::_Pointer>
328 (__node->_M_next)->_M_value))
329 __node->_M_transfer_after(&__list._M_impl._M_head,
330 __list._M_impl._M_head._M_next);
331 __node = __node->_M_next;
333 if (__list._M_impl._M_head._M_next)
335 __node->_M_next = __list._M_impl._M_head._M_next;
336 __list._M_impl._M_head._M_next = 0;
340 template<typename _Tp, typename _Alloc>
342 operator==(const forward_list<_Tp, _Alloc>& __lx,
343 const forward_list<_Tp, _Alloc>& __ly)
345 // We don't have size() so we need to walk through both lists
346 // making sure both iterators are valid.
347 auto __ix = __lx.cbegin();
348 auto __iy = __ly.cbegin();
349 while (__ix != __lx.cend() && __iy != __ly.cend())
356 if (__ix == __lx.cend() && __iy == __ly.cend())
362 template<typename _Tp, class _Alloc>
363 template<typename _Comp>
365 forward_list<_Tp, _Alloc>::
368 typedef typename _Node::_Pointer _Pointer;
370 // If `next' is 0, return immediately.
372 __static_pointer_cast<_Pointer>(this->_M_impl._M_head._M_next);
376 unsigned long __insize = 1;
380 _Pointer __p = __list;
384 // Count number of merges we do in this pass.
385 unsigned long __nmerges = 0;
390 // There exists a merge to be done.
391 // Step `insize' places along from p.
393 unsigned long __psize = 0;
394 for (unsigned long __i = 0; __i < __insize; ++__i)
397 __q = __static_pointer_cast<_Pointer>(__q->_M_next);
402 // If q hasn't fallen off end, we have two lists to merge.
403 unsigned long __qsize = __insize;
405 // Now we have two lists; merge them.
406 while (__psize > 0 || (__qsize > 0 && __q))
408 // Decide whether next node of merge comes from p or q.
412 // p is empty; e must come from q.
414 __q = __static_pointer_cast<_Pointer>(__q->_M_next);
417 else if (__qsize == 0 || !__q)
419 // q is empty; e must come from p.
421 __p = __static_pointer_cast<_Pointer>(__p->_M_next);
424 else if (__comp(__p->_M_value, __q->_M_value))
426 // First node of p is lower; e must come from p.
428 __p = __static_pointer_cast<_Pointer>(__p->_M_next);
433 // First node of q is lower; e must come from q.
435 __q = __static_pointer_cast<_Pointer>(__q->_M_next);
439 // Add the next node to the merged list.
441 __tail->_M_next = __e;
447 // Now p has stepped `insize' places along, and q has too.
452 // If we have done only one merge, we're finished.
453 // Allow for nmerges == 0, the empty list case.
456 this->_M_impl._M_head._M_next = __list;
460 // Otherwise repeat, merging lists twice the size.
465 _GLIBCXX_END_NAMESPACE // namespace std
467 #endif /* _FORWARD_LIST_TCC */