1 // <forward_list.h> -*- 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 2, 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 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file forward_list.h
31 * This is a Standard C++ Library header.
34 #ifndef _FORWARD_LIST_H
35 #define _FORWARD_LIST_H 1
37 #pragma GCC system_header
39 #ifndef __GXX_EXPERIMENTAL_CXX0X__
40 # include <c++0x_warning.h>
44 #include <initializer_list>
47 _GLIBCXX_BEGIN_NAMESPACE(std)
49 using __gnu_cxx::__static_pointer_cast;
50 using __gnu_cxx::__const_pointer_cast;
53 * @brief A helper basic node class for @forward_list.
54 * This is just a linked list with nothing inside it.
55 * There are purely list shuffling utility methods here.
57 template<typename _Alloc>
58 struct _Fwd_list_node_base
60 // The type allocated by _Alloc cannot be this type, so we rebind
61 typedef typename _Alloc::template rebind<_Fwd_list_node_base<_Alloc> >
62 ::other::pointer _Pointer;
63 typedef typename _Alloc::template rebind<_Fwd_list_node_base<_Alloc> >
64 ::other::const_pointer _Const_pointer;
68 _Fwd_list_node_base() : _M_next(0) { }
71 swap(_Fwd_list_node_base& __x, _Fwd_list_node_base& __y)
72 { std::swap(__x._M_next, __y._M_next); }
75 _M_transfer_after(_Pointer __bbegin, _Pointer __bend)
77 _Pointer __keep = __bbegin->_M_next;
80 __bbegin->_M_next = __bend->_M_next;
81 __bend->_M_next = this->_M_next;
84 __bbegin->_M_next = 0;
85 this->_M_next = __keep;
89 _M_transfer_after(_Pointer __bbegin)
91 _Pointer __bend = __bbegin;
92 while (__bend && __bend->_M_next)
93 __bend = __bend->_M_next;
94 _M_transfer_after(__bbegin, __bend);
100 _Pointer __tail = this->_M_next;
103 while (_Pointer __temp = __tail->_M_next)
105 _Pointer __keep = this->_M_next;
106 this->_M_next = __temp;
107 __tail->_M_next = __temp->_M_next;
108 this->_M_next->_M_next = __keep;
114 * @brief A helper node class for @forward_list.
115 * This is just a linked list with a data value in each node.
116 * There is a sorting utility method.
118 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
119 struct _Fwd_list_node : public _Fwd_list_node_base<_Alloc>
121 typedef typename _Alloc::template rebind<_Fwd_list_node<_Tp, _Alloc> >
122 ::other::pointer _Pointer;
124 template<typename... _Args>
125 _Fwd_list_node(_Args&&... __args)
126 : _Fwd_list_node_base<_Alloc>(),
127 _M_value(std::forward<_Args>(__args)...) { }
129 template<typename _Comp>
131 _M_sort_after(_Comp __comp);
137 * @brief A forward_list::iterator.
139 * All the functions are op overloads.
141 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
142 struct _Fwd_list_iterator
144 typedef _Fwd_list_iterator<_Tp, _Alloc> _Self;
145 typedef _Fwd_list_node<_Tp, _Alloc> _Node;
146 typedef _Fwd_list_node_base<_Alloc> _Node_base;
148 typedef _Tp value_type;
149 typedef typename _Alloc::pointer pointer;
150 typedef typename _Alloc::reference reference;
151 typedef typename _Alloc::difference_type difference_type;
152 typedef std::forward_iterator_tag iterator_category;
154 _Fwd_list_iterator() : _M_node() { }
157 _Fwd_list_iterator(typename _Node_base::_Pointer __n)
162 { return __static_pointer_cast<_Node*>(this->_M_node)->_M_value; }
166 { return &__static_pointer_cast<_Node*>(this->_M_node)->_M_value; }
171 this->_M_node = this->_M_node->_M_next;
179 this->_M_node = this->_M_node->_M_next;
184 operator==(const _Self& __x) const
185 { return this->_M_node == __x._M_node; }
188 operator!=(const _Self& __x) const
189 { return this->_M_node != __x._M_node; }
195 return _Fwd_list_iterator(this->_M_node->_M_next);
197 return _Fwd_list_iterator(0);
200 typename _Node_base::_Pointer _M_node;
204 * @brief A forward_list::const_iterator.
206 * All the functions are op overloads.
208 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
209 struct _Fwd_list_const_iterator
211 typedef _Fwd_list_const_iterator<_Tp, _Alloc> _Self;
212 typedef const _Fwd_list_node<_Tp, _Alloc> _Node;
213 typedef const _Fwd_list_node_base<_Alloc> _Node_base;
214 typedef _Fwd_list_iterator<_Tp, _Alloc> iterator;
216 typedef _Tp value_type;
217 typedef typename _Alloc::const_pointer pointer;
218 typedef typename _Alloc::const_reference reference;
219 typedef typename _Alloc::difference_type difference_type;
220 typedef std::forward_iterator_tag iterator_category;
222 _Fwd_list_const_iterator() : _M_node() { }
225 _Fwd_list_const_iterator(typename _Node_base::_Const_pointer __n)
228 _Fwd_list_const_iterator(const iterator& __iter)
229 : _M_node(__iter._M_node) { }
233 { return __static_pointer_cast<_Node*>(this->_M_node)->_M_value; }
237 { return &__static_pointer_cast<_Node*>(this->_M_node)->_M_value; }
242 this->_M_node = this->_M_node->_M_next;
250 this->_M_node = this->_M_node->_M_next;
255 operator==(const _Self& __x) const
256 { return this->_M_node == __x._M_node; }
259 operator!=(const _Self& __x) const
260 { return this->_M_node != __x._M_node; }
266 return _Fwd_list_const_iterator(this->_M_node->_M_next);
268 return _Fwd_list_const_iterator(0);
271 typename _Node_base::_Const_pointer _M_node;
275 * @brief Forward list iterator equality comparison.
277 template<typename _Tp,class _Alloc>
279 operator==(const _Fwd_list_iterator<_Tp, _Alloc>& __x,
280 const _Fwd_list_const_iterator<_Tp, _Alloc>& __y)
281 { return __x._M_node == __y._M_node; }
284 * @brief Forward list iterator inequality comparison.
286 template<typename _Tp,class _Alloc>
288 operator!=(const _Fwd_list_iterator<_Tp, _Alloc>& __x,
289 const _Fwd_list_const_iterator<_Tp, _Alloc>& __y)
290 { return __x._M_node != __y._M_node; }
293 * @brief Base class for @forward_list.
295 template<typename _Tp, typename _Alloc = allocator<_Tp> >
296 struct _Fwd_list_base
299 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
301 typedef typename _Alloc::template
302 rebind<_Fwd_list_node<_Tp, _Tp_alloc_type>>::other _Node_alloc_type;
304 struct _Fwd_list_impl
305 : public _Node_alloc_type
307 _Fwd_list_node_base<_Tp_alloc_type> _M_head;
310 : _Node_alloc_type(), _M_head()
313 _Fwd_list_impl(const _Node_alloc_type& __a)
314 : _Node_alloc_type(__a), _M_head()
318 _Fwd_list_impl _M_impl;
321 typedef _Fwd_list_iterator<_Tp, _Tp_alloc_type> iterator;
322 typedef _Fwd_list_const_iterator<_Tp, _Tp_alloc_type> const_iterator;
324 typedef _Fwd_list_node<_Tp, _Tp_alloc_type> _Node;
325 typedef _Fwd_list_node_base<_Tp_alloc_type> _Node_base;
328 _M_get_Node_allocator()
329 { return *static_cast<_Node_alloc_type*>(&this->_M_impl); }
331 const _Node_alloc_type&
332 _M_get_Node_allocator() const
333 { return *static_cast<const _Node_alloc_type*>(&this->_M_impl); }
337 { this->_M_impl._M_head._M_next = 0; }
339 _Fwd_list_base(const _Alloc& __a)
341 { this->_M_impl._M_head._M_next = 0; }
343 _Fwd_list_base(const _Fwd_list_base& __lst, const _Alloc& __a);
345 _Fwd_list_base(_Fwd_list_base&& __lst, const _Alloc& __a)
347 { _Node_base::swap(this->_M_impl._M_head,
348 __lst._M_impl._M_head); }
350 _Fwd_list_base(_Fwd_list_base&& __lst)
351 : _M_impl(__lst._M_get_Node_allocator())
352 { _Node_base::swap(this->_M_impl._M_head,
353 __lst._M_impl._M_head); }
356 { _M_erase_after(&_M_impl._M_head, 0); }
360 typename _Node::_Pointer
362 { return _M_get_Node_allocator().allocate(1); }
364 template<typename... _Args>
365 typename _Node::_Pointer
366 _M_create_node(_Args&&... __args)
368 typename _Node::_Pointer __node = this->_M_get_node();
371 _M_get_Node_allocator().construct(__node,
372 std::forward<_Args>(__args)...);
377 this->_M_put_node(__node);
378 __throw_exception_again;
383 template<typename... _Args>
384 typename _Node_base::_Pointer
385 _M_insert_after(const_iterator __pos, _Args&&... __args);
388 _M_put_node(typename _Node::_Pointer __p)
389 { _M_get_Node_allocator().deallocate(__p, 1); }
391 typename _Node_base::_Pointer
392 _M_erase_after(typename _Node_base::_Pointer __pos);
394 typename _Node_base::_Pointer
395 _M_erase_after(typename _Node_base::_Pointer __pos,
396 typename _Node_base::_Pointer __last);
400 * @brief A standard container with linear time access to elements,
401 * and fixed time insertion/deletion at any point in the sequence.
405 * Meets the requirements of a <a href="tables.html#65">container</a>, a
406 * <a href="tables.html#67">sequence</a>, including the
407 * <a href="tables.html#68">optional sequence requirements</a> with the
408 * %exception of @c at and @c operator[].
410 * This is a @e singly @e linked %list. Traversal up the
411 * %list requires linear time, but adding and removing elements (or
412 * @e nodes) is done in constant time, regardless of where the
413 * change takes place. Unlike std::vector and std::deque,
414 * random-access iterators are not provided, so subscripting ( @c
415 * [] ) access is not allowed. For algorithms which only need
416 * sequential access, this lack makes no difference.
418 * Also unlike the other standard containers, std::forward_list provides
419 * specialized algorithms %unique to linked lists, such as
420 * splicing, sorting, and in-place reversal.
422 * A couple points on memory allocation for forward_list<Tp>:
424 * First, we never actually allocate a Tp, we allocate
425 * Fwd_list_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
426 * that after elements from %forward_list<X,Alloc1> are spliced into
427 * %forward_list<X,Alloc2>, destroying the memory of the second %list is a
428 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
430 template<typename _Tp, typename _Alloc = allocator<_Tp> >
431 class forward_list : private _Fwd_list_base<_Tp, _Alloc>
434 typedef _Fwd_list_base<_Tp, _Alloc> _Base;
435 typedef typename _Base::_Node _Node;
436 typedef typename _Base::_Node_base _Node_base;
437 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
441 typedef _Tp value_type;
442 typedef typename _Tp_alloc_type::pointer pointer;
443 typedef typename _Tp_alloc_type::const_pointer const_pointer;
444 typedef typename _Tp_alloc_type::reference reference;
445 typedef typename _Tp_alloc_type::const_reference const_reference;
447 typedef typename _Base::iterator iterator;
448 typedef typename _Base::const_iterator const_iterator;
449 typedef std::size_t size_type;
450 typedef std::ptrdiff_t difference_type;
451 typedef _Alloc allocator_type;
453 // 23.2.3.1 construct/copy/destroy:
456 * @brief Creates a %forward_list with no elements.
457 * @param al An allocator object.
460 forward_list(const _Alloc& __al = _Alloc())
465 * @brief Copy constructor with allocator argument.
466 * @param list Input list to copy.
467 * @param al An allocator object.
469 forward_list(const forward_list& __list, const _Alloc& __al)
470 : _Base(__list, __al)
474 * @brief Move constructor with allocator argument.
475 * @param list Input list to move.
476 * @param al An allocator object.
478 forward_list(forward_list&& __list, const _Alloc& __al)
479 : _Base(std::forward<_Base>(__list), __al)
483 * @brief Creates a %forward_list with copies of the default element
485 * @param n The number of elements to initially create.
487 * This constructor fills the %forward_list with @a n copies of
491 forward_list(size_type __n)
493 { _M_fill_initialize(__n, value_type()); }
496 * @brief Creates a %forward_list with copies of an exemplar element.
497 * @param n The number of elements to initially create.
498 * @param value An element to copy.
499 * @param al An allocator object.
501 * This constructor fills the %forward_list with @a n copies of @a
504 forward_list(size_type __n, const _Tp& __value,
505 const _Alloc& __al = _Alloc())
507 { _M_fill_initialize(__n, __value); }
510 * @brief Builds a %forward_list from a range.
511 * @param first An input iterator.
512 * @param last An input iterator.
513 * @param al An allocator object.
515 * Create a %forward_list consisting of copies of the elements from
516 * [@a first,@a last). This is linear in N (where N is
517 * distance(@a first,@a last)).
519 template<typename _InputIterator>
520 forward_list(_InputIterator __first, _InputIterator __last,
521 const _Alloc& __al = _Alloc())
524 // Check whether it's an integral type. If so, it's not an iterator.
525 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
526 _M_initialize_dispatch(__first, __last, _Integral());
530 * @brief The %forward_list copy constructor.
531 * @param list A %forward_list of identical element and allocator
534 * The newly-created %forward_list uses a copy of the allocation
535 * object used by @a list.
537 forward_list(const forward_list& __list)
538 : _Base(__list.get_allocator())
539 { _M_initialize_dispatch(__list.begin(), __list.end(), __false_type()); }
542 * @brief The %forward_list move constructor.
543 * @param list A %forward_list of identical element and allocator
546 * The newly-created %forward_list contains the exact contents of @a
547 * forward_list. The contents of @a list are a valid, but unspecified
550 forward_list(forward_list&& __list)
551 : _Base(std::forward<_Base>(__list)) { }
554 * @brief Builds a %forward_list from an initializer_list
555 * @param il An initializer_list of value_type.
556 * @param al An allocator object.
558 * Create a %forward_list consisting of copies of the elements
559 * in the initializer_list @a il. This is linear in il.size().
561 forward_list(std::initializer_list<_Tp> __il,
562 const _Alloc& __al = _Alloc())
564 { _M_initialize_dispatch(__il.begin(), __il.end(), __false_type()); }
567 * @brief The forward_list dtor.
570 { _M_erase_after(&this->_M_impl._M_head, 0); }
573 * @brief The %forward_list assignment operator.
574 * @param list A %forward_list of identical element and allocator
577 * All the elements of @a list are copied, but unlike the copy
578 * constructor, the allocator object is not copied.
581 operator=(const forward_list& __list);
584 * @brief The %forward_list move assignment operator.
585 * @param list A %forward_list of identical element and allocator
588 * The contents of @a list are moved into this %forward_list
589 * (without copying). @a list is a valid, but unspecified
593 operator=(forward_list&& __list)
604 * @brief The %forward_list initializer list assignment operator.
605 * @param il An initializer_list of value_type.
607 * Replace the contents of the %forward_list with copies of the
608 * elements in the initializer_list @a il. This is linear in
612 operator=(std::initializer_list<_Tp> __il)
619 * @brief Assigns a range to a %forward_list.
620 * @param first An input iterator.
621 * @param last An input iterator.
623 * This function fills a %forward_list with copies of the elements
624 * in the range [@a first,@a last).
626 * Note that the assignment completely changes the %forward_list and
627 * that the resulting %forward_list's size is the same as the number
628 * of elements assigned. Old data may be lost.
630 template<typename _InputIterator>
632 assign(_InputIterator __first, _InputIterator __last)
635 insert_after(cbefore_begin(), __first, __last);
639 * @brief Assigns a given value to a %forward_list.
640 * @param n Number of elements to be assigned.
641 * @param val Value to be assigned.
643 * This function fills a %forward_list with @a n copies of the given
644 * value. Note that the assignment completely changes the
645 * %forward_list and that the resulting %forward_list's size is the
646 * same as the number of elements assigned. Old data may be lost.
649 assign(size_type __n, const _Tp& __val)
652 insert_after(cbefore_begin(), __n, __val);
656 * @brief Assigns an initializer_list to a %forward_list.
657 * @param il An initializer_list of value_type.
659 * Replace the contents of the %forward_list with copies of the
660 * elements in the initializer_list @a il. This is linear in
664 assign(std::initializer_list<_Tp> __il)
667 insert_after(cbefore_begin(), __il);
670 /// Get a copy of the memory allocation object.
672 get_allocator() const
673 { return this->_M_get_Node_allocator(); }
675 // 23.2.3.2 iterators:
678 * Returns a read/write iterator that points before the first element
679 * in the %forward_list. Iteration is done in ordinary element order.
683 { return iterator(&this->_M_impl._M_head); }
686 * Returns a read-only (constant) iterator that points before the
687 * first element in the %forward_list. Iteration is done in ordinary
692 { return const_iterator(&this->_M_impl._M_head); }
695 * Returns a read/write iterator that points to the first element
696 * in the %forward_list. Iteration is done in ordinary element order.
700 { return iterator(this->_M_impl._M_head._M_next); }
703 * Returns a read-only (constant) iterator that points to the first
704 * element in the %forward_list. Iteration is done in ordinary
709 { return const_iterator(this->_M_impl._M_head._M_next); }
712 * Returns a read/write iterator that points one past the last
713 * element in the %forward_list. Iteration is done in ordinary
718 { return iterator(0); }
721 * Returns a read-only iterator that points one past the last
722 * element in the %forward_list. Iteration is done in ordinary
727 { return const_iterator(0); }
730 * Returns a read-only (constant) iterator that points to the
731 * first element in the %forward_list. Iteration is done in ordinary
736 { return const_iterator(this->_M_impl._M_head._M_next); }
739 * Returns a read-only (constant) iterator that points before the
740 * first element in the %forward_list. Iteration is done in ordinary
744 cbefore_begin() const
745 { return const_iterator(&this->_M_impl._M_head); }
748 * Returns a read-only (constant) iterator that points one past
749 * the last element in the %forward_list. Iteration is done in
750 * ordinary element order.
754 { return const_iterator(0); }
757 * Returns true if the %forward_list is empty. (Thus begin() would
762 { return this->_M_impl._M_head._M_next == 0; }
765 * Returns the largest possible size of %forward_list.
769 { return this->_M_get_Node_allocator().max_size(); }
771 // 23.2.3.3 element access:
774 * Returns a read/write reference to the data at the first
775 * element of the %forward_list.
780 _Node* __front = __static_pointer_cast<_Node*>(this->_M_impl._M_head._M_next);
781 return __front->_M_value;
785 * Returns a read-only (constant) reference to the data at the first
786 * element of the %forward_list.
792 __static_pointer_cast<_Node*>(this->_M_impl._M_head._M_next);
793 return __front->_M_value;
796 // 23.2.3.4 modifiers:
799 * @brief Constructs object in %forward_list at the front of the
801 * @param args Arguments.
803 * This function will insert an object of type Tp constructed
804 * with Tp(std::forward<Args>(args)...) at the front of the list
805 * Due to the nature of a %forward_list this operation can
806 * be done in constant time, and does not invalidate iterators
809 template<typename... _Args>
811 emplace_front(_Args&&... __args)
812 { this->_M_insert_after(cbefore_begin(),
813 std::forward<_Args>(__args)...); }
816 * @brief Add data to the front of the %forward_list.
817 * @param val Data to be added.
819 * This is a typical stack operation. The function creates an
820 * element at the front of the %forward_list and assigns the given
821 * data to it. Due to the nature of a %forward_list this operation
822 * can be done in constant time, and does not invalidate iterators
826 push_front(const _Tp& __val)
827 { this->_M_insert_after(cbefore_begin(), __val); }
833 push_front(_Tp&& __val)
834 { this->_M_insert_after(cbefore_begin(), std::move(__val)); }
837 * @brief Removes first element.
839 * This is a typical stack operation. It shrinks the %forward_list
840 * by one. Due to the nature of a %forward_list this operation can
841 * be done in constant time, and only invalidates iterators/references
842 * to the element being removed.
844 * Note that no data is returned, and if the first element's data
845 * is needed, it should be retrieved before pop_front() is
850 { this->_M_erase_after(&this->_M_impl._M_head); }
853 * @brief Constructs object in %forward_list after the specified
855 * @param pos A const_iterator into the %forward_list.
856 * @param args Arguments.
857 * @return An iterator that points to the inserted data.
859 * This function will insert an object of type T constructed
860 * with T(std::forward<Args>(args)...) after the specified
861 * location. Due to the nature of a %forward_list this operation can
862 * be done in constant time, and does not invalidate iterators
865 template<typename... _Args>
867 emplace_after(const_iterator __pos, _Args&&... __args)
868 { return iterator(this->_M_insert_after(__pos,
869 std::forward<_Args>(__args)...)); }
872 * @brief Inserts given value into %forward_list after specified
874 * @param pos An iterator into the %forward_list.
875 * @param val Data to be inserted.
876 * @return An iterator that points to the inserted data.
878 * This function will insert a copy of the given value after
879 * the specified location. Due to the nature of a %forward_list this
880 * operation can be done in constant time, and does not
881 * invalidate iterators and references.
884 insert_after(const_iterator __pos, const _Tp& __val)
885 { return iterator(this->_M_insert_after(__pos, __val)); }
891 insert_after(const_iterator __pos, _Tp&& __val)
892 { return iterator(this->_M_insert_after(__pos, std::move(__val))); }
895 * @brief Inserts a number of copies of given data into the
897 * @param pos An iterator into the %forward_list.
898 * @param n Number of elements to be inserted.
899 * @param val Data to be inserted.
901 * This function will insert a specified number of copies of the
902 * given data after the location specified by @a pos.
904 * This operation is linear in the number of elements inserted and
905 * does not invalidate iterators and references.
908 insert_after(const_iterator __pos, size_type __n, const _Tp& __val)
910 forward_list __tmp(__n, __val, this->get_allocator());
911 this->splice_after(__pos, std::move(__tmp));
915 * @brief Inserts a range into the %forward_list.
916 * @param position An iterator into the %forward_list.
917 * @param first An input iterator.
918 * @param last An input iterator.
920 * This function will insert copies of the data in the range [@a
921 * first,@a last) into the %forward_list after the location specified
924 * This operation is linear in the number of elements inserted and
925 * does not invalidate iterators and references.
927 template<typename _InputIterator>
929 insert_after(const_iterator __pos,
930 _InputIterator __first, _InputIterator __last)
932 forward_list __tmp(__first, __last, this->get_allocator());
933 this->splice_after(__pos, std::move(__tmp));
937 * @brief Inserts the contents of an initializer_list into
938 * %forward_list after the specified iterator.
939 * @param pos An iterator into the %forward_list.
940 * @param il An initializer_list of value_type.
942 * This function will insert copies of the data in the
943 * initializer_list @a il into the %forward_list before the location
944 * specified by @a pos.
946 * This operation is linear in the number of elements inserted and
947 * does not invalidate iterators and references.
950 insert_after(const_iterator __pos, std::initializer_list<_Tp> __il)
952 forward_list __tmp(__il, this->get_allocator());
953 this->splice_after(__pos, std::move(__tmp));
957 * @brief Removes the element pointed to by the iterator following
959 * @param pos Iterator pointing to element to be erased.
960 * @return An iterator pointing to the next element (or end()).
962 * This function will erase the element at the given position and
963 * thus shorten the %forward_list by one.
965 * Due to the nature of a %forward_list this operation can be done
966 * in constant time, and only invalidates iterators/references to
967 * the element being removed. The user is also cautioned that
968 * this function only erases the element, and that if the element
969 * is itself a pointer, the pointed-to memory is not touched in
970 * any way. Managing the pointer is the user's responsibility.
973 erase_after(const_iterator __pos)
975 _Node_base* __tmp = __const_pointer_cast<_Node_base*>(__pos._M_node);
977 return iterator(this->_M_erase_after(__tmp));
983 * @brief Remove a range of elements.
984 * @param pos Iterator pointing before the first element to be
986 * @param last Iterator pointing to one past the last element to be
988 * @return An iterator pointing to the element pointed to by @a last
989 * prior to erasing (or end()).
991 * This function will erase the elements in the range @a
992 * (pos,last) and shorten the %forward_list accordingly.
994 * This operation is linear time in the size of the range and only
995 * invalidates iterators/references to the element being removed.
996 * The user is also cautioned that this function only erases the
997 * elements, and that if the elements themselves are pointers, the
998 * pointed-to memory is not touched in any way. Managing the pointer
999 * is the user's responsibility.
1002 erase_after(const_iterator __pos, iterator __last)
1004 _Node_base* __tmp = __const_pointer_cast<_Node_base*>(__pos._M_node);
1005 return iterator(this->_M_erase_after(__tmp, &*__last._M_node));
1009 * @brief Swaps data with another %forward_list.
1010 * @param list A %forward_list of the same element and allocator
1013 * This exchanges the elements between two lists in constant
1014 * time. Note that the global std::swap() function is
1015 * specialized such that std::swap(l1,l2) will feed to this
1019 swap(forward_list&& __list)
1020 { _Node_base::swap(this->_M_impl._M_head, __list._M_impl._M_head); }
1023 * @brief Resizes the %forward_list to the specified number of
1025 * @param sz Number of elements the %forward_list should contain.
1027 * This function will %resize the %forward_list to the specified
1028 * number of elements. If the number is smaller than the
1029 * %forward_list's current size the %forward_list is truncated,
1030 * otherwise the %forward_list is extended and new elements are
1031 * populated with given data.
1034 resize(size_type __sz)
1035 { resize(__sz, _Tp()); }
1038 * @brief Resizes the %forward_list to the specified number of
1040 * @param sz Number of elements the %forward_list should contain.
1041 * @param val Data with which new elements should be populated.
1043 * This function will %resize the %forward_list to the specified
1044 * number of elements. If the number is smaller than the
1045 * %forward_list's current size the %forward_list is truncated,
1046 * otherwise the %forward_list is extended and new elements are
1047 * populated with given data.
1050 resize(size_type __sz, value_type __val);
1053 * @brief Erases all the elements.
1055 * Note that this function only erases
1056 * the elements, and that if the elements themselves are
1057 * pointers, the pointed-to memory is not touched in any way.
1058 * Managing the pointer is the user's responsibility.
1062 { this->_M_erase_after(&this->_M_impl._M_head, 0); }
1064 // 23.2.3.5 forward_list operations:
1067 * @brief Insert contents of another %forward_list.
1068 * @param pos Iterator referencing the element to insert after.
1069 * @param list Source list.
1071 * The elements of @a list are inserted in constant time after
1072 * the element referenced by @a pos. @a list becomes an empty
1075 * Requires this != @a x.
1078 splice_after(const_iterator __pos, forward_list&& __list);
1081 * @brief Insert element from another %forward_list.
1082 * @param pos Iterator referencing the element to insert after.
1083 * @param list Source list.
1084 * @param it Iterator referencing the element before the element
1087 * Removes the element in list @a list referenced by @a i and
1088 * inserts it into the current list after @a pos.
1091 splice_after(const_iterator __pos, forward_list&& __list,
1092 const_iterator __it)
1093 { this->splice_after(__pos, __list, __it, __it._M_next()); }
1096 * @brief Insert range from another %forward_list.
1097 * @param pos Iterator referencing the element to insert after.
1098 * @param list Source list.
1099 * @param before Iterator referencing before the start of range
1101 * @param last Iterator referencing the end of range in list.
1103 * Removes elements in the range (before,last) and inserts them
1104 * after @a pos in constant time.
1106 * Undefined if @a pos is in (before,last).
1109 splice_after(const_iterator __pos, forward_list&& __list,
1110 const_iterator __before, const_iterator __last);
1113 * @brief Remove all elements equal to value.
1114 * @param val The value to remove.
1116 * Removes every element in the list equal to @a value.
1117 * Remaining elements stay in list order. Note that this
1118 * function only erases the elements, and that if the elements
1119 * themselves are pointers, the pointed-to memory is not
1120 * touched in any way. Managing the pointer is the user's
1124 remove(const _Tp& __val);
1127 * @brief Remove all elements satisfying a predicate.
1128 * @param pred Unary predicate function or object.
1130 * Removes every element in the list for which the predicate
1131 * returns true. Remaining elements stay in list order. Note
1132 * that this function only erases the elements, and that if the
1133 * elements themselves are pointers, the pointed-to memory is
1134 * not touched in any way. Managing the pointer is the user's
1137 template<typename _Pred>
1139 remove_if(_Pred __pred);
1142 * @brief Remove consecutive duplicate elements.
1144 * For each consecutive set of elements with the same value,
1145 * remove all but the first one. Remaining elements stay in
1146 * list order. Note that this function only erases the
1147 * elements, and that if the elements themselves are pointers,
1148 * the pointed-to memory is not touched in any way. Managing
1149 * the pointer is the user's responsibility.
1153 { this->unique(std::equal_to<_Tp>()); }
1156 * @brief Remove consecutive elements satisfying a predicate.
1157 * @param binary_pred Binary predicate function or object.
1159 * For each consecutive set of elements [first,last) that
1160 * satisfy predicate(first,i) where i is an iterator in
1161 * [first,last), remove all but the first one. Remaining
1162 * elements stay in list order. Note that this function only
1163 * erases the elements, and that if the elements themselves are
1164 * pointers, the pointed-to memory is not touched in any way.
1165 * Managing the pointer is the user's responsibility.
1167 template<typename _BinPred>
1169 unique(_BinPred __binary_pred);
1172 * @brief Merge sorted lists.
1173 * @param list Sorted list to merge.
1175 * Assumes that both @a list and this list are sorted according to
1176 * operator<(). Merges elements of @a list into this list in
1177 * sorted order, leaving @a list empty when complete. Elements in
1178 * this list precede elements in @a list that are equal.
1181 merge(forward_list&& __list)
1182 { this->merge(__list, std::less<_Tp>()); }
1185 * @brief Merge sorted lists according to comparison function.
1186 * @param list Sorted list to merge.
1187 * @param comp Comparison function defining sort order.
1189 * Assumes that both @a list and this list are sorted according to
1190 * comp. Merges elements of @a list into this list
1191 * in sorted order, leaving @a list empty when complete. Elements
1192 * in this list precede elements in @a list that are equivalent
1193 * according to comp().
1195 template<typename _Comp>
1197 merge(forward_list&& __list, _Comp __comp);
1200 * @brief Sort the elements of the list.
1202 * Sorts the elements of this list in NlogN time. Equivalent
1203 * elements remain in list order.
1208 _Node* __tmp = __static_pointer_cast<_Node*>(&this->_M_impl._M_head);
1209 __tmp->_M_sort_after(std::less<_Tp>());
1213 * @brief Sort the forward_list using a comparison function.
1215 * Sorts the elements of this list in NlogN time. Equivalent
1216 * elements remain in list order.
1218 template<typename _Comp>
1222 _Node* __tmp = __static_pointer_cast<_Node*>(&this->_M_impl._M_head);
1223 __tmp->_M_sort_after(__comp);
1227 * @brief Reverse the elements in list.
1229 * Reverse the order of elements in the list in linear time.
1235 template<typename _Integer>
1237 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1238 { _M_fill_initialize(static_cast<size_type>(__n), __x); }
1240 // Called by the range constructor to implement [23.1.1]/9
1241 template<typename _InputIterator>
1243 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1246 // Called by forward_list(n,v,a), and the range constructor when it
1247 // turns out to be the same thing.
1249 _M_fill_initialize(size_type __n, const value_type& __value);
1253 * @brief Forward list equality comparison.
1254 * @param lx A %forward_list
1255 * @param ly A %forward_list of the same type as @a lx.
1256 * @return True iff the size and elements of the forward lists are equal.
1258 * This is an equivalence relation. It is linear in the size of the
1259 * forward lists. Deques are considered equivalent if corresponding
1260 * elements compare equal.
1262 template<typename _Tp, typename _Alloc>
1264 operator==(const forward_list<_Tp, _Alloc>& __lx,
1265 const forward_list<_Tp, _Alloc>& __ly);
1268 * @brief Forward list ordering relation.
1269 * @param lx A %forward_list.
1270 * @param ly A %forward_list of the same type as @a lx.
1271 * @return True iff @a lx is lexicographically less than @a ly.
1273 * This is a total ordering relation. It is linear in the size of the
1274 * forward lists. The elements must be comparable with @c <.
1276 * See std::lexicographical_compare() for how the determination is made.
1278 template<typename _Tp, typename _Alloc>
1280 operator<(const forward_list<_Tp, _Alloc>& __lx,
1281 const forward_list<_Tp, _Alloc>& __ly)
1282 { return std::lexicographical_compare(__lx.cbegin(), __lx.cend(),
1283 __ly.cbegin(), __ly.cend()); }
1285 /// Based on operator==
1286 template<typename _Tp, typename _Alloc>
1288 operator!=(const forward_list<_Tp, _Alloc>& __lx,
1289 const forward_list<_Tp, _Alloc>& __ly)
1290 { return !(__lx == __ly); }
1292 /// Based on operator<
1293 template<typename _Tp, typename _Alloc>
1295 operator>(const forward_list<_Tp, _Alloc>& __lx,
1296 const forward_list<_Tp, _Alloc>& __ly)
1297 { return (__ly < __lx); }
1299 /// Based on operator<
1300 template<typename _Tp, typename _Alloc>
1302 operator>=(const forward_list<_Tp, _Alloc>& __lx,
1303 const forward_list<_Tp, _Alloc>& __ly)
1304 { return !(__lx < __ly); }
1306 /// Based on operator<
1307 template<typename _Tp, typename _Alloc>
1309 operator<=(const forward_list<_Tp, _Alloc>& __lx,
1310 const forward_list<_Tp, _Alloc>& __ly)
1311 { return !(__ly < __lx); }
1313 /// See std::forward_list::swap().
1314 template<typename _Tp, typename _Alloc>
1316 swap(forward_list<_Tp, _Alloc>& __lx,
1317 forward_list<_Tp, _Alloc>& __ly)
1318 { __lx.swap(__ly); }
1320 /// See std::forward_list::swap().
1321 template<typename _Tp, typename _Alloc>
1323 swap(forward_list<_Tp, _Alloc>&& __lx,
1324 forward_list<_Tp, _Alloc>& __ly)
1325 { __lx.swap(__ly); }
1327 /// See std::forward_list::swap().
1328 template<typename _Tp, typename _Alloc>
1330 swap(forward_list<_Tp, _Alloc>& __lx,
1331 forward_list<_Tp, _Alloc>&& __ly)
1332 { __lx.swap(__ly); }
1334 _GLIBCXX_END_NAMESPACE // namespace std
1336 #endif // __GXX_EXPERIMENTAL_CXX0X__
1338 #endif // _FORWARD_LIST_H