1 // <forward_list.h> -*- C++ -*-
3 // Copyright (C) 2008 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>
46 _GLIBCXX_BEGIN_NAMESPACE(std)
49 * @brief A helper basic node class for @forward_list.
50 * This is just a linked list with nothing inside it.
51 * There are purely list shuffling utility methods here.
53 struct _Fwd_list_node_base
55 _Fwd_list_node_base() : _M_next(0) { }
57 _Fwd_list_node_base* _M_next;
60 swap(_Fwd_list_node_base& __x, _Fwd_list_node_base& __y)
61 { std::swap(__x._M_next, __y._M_next); }
64 _M_transfer_after(_Fwd_list_node_base* __bbegin,
65 _Fwd_list_node_base* __bend)
67 _Fwd_list_node_base* __keep = __bbegin->_M_next;
70 __bbegin->_M_next = __bend->_M_next;
71 __bend->_M_next = this->_M_next;
74 __bbegin->_M_next = 0;
75 this->_M_next = __keep;
79 _M_transfer_after(_Fwd_list_node_base* __bbegin)
81 _Fwd_list_node_base* __bend = __bbegin;
82 while (__bend && __bend->_M_next)
83 __bend = __bend->_M_next;
84 _M_transfer_after(__bbegin, __bend);
90 _Fwd_list_node_base* __tail = this->_M_next;
93 while (_Fwd_list_node_base* __temp = __tail->_M_next)
95 _Fwd_list_node_base* __keep = this->_M_next;
96 this->_M_next = __temp;
97 __tail->_M_next = __temp->_M_next;
98 this->_M_next->_M_next = __keep;
104 * @brief A helper node class for @forward_list.
105 * This is just a linked list with a data value in each node.
106 * There is a sorting utility method.
108 template<typename _Tp>
109 struct _Fwd_list_node : public _Fwd_list_node_base
111 template<typename... _Args>
112 _Fwd_list_node(_Args&&... __args)
113 : _Fwd_list_node_base(), _M_value(std::forward<_Args>(__args)...) { }
115 template<typename _Comp>
117 _M_sort_after(_Comp __comp);
123 * @brief A forward_list::iterator.
125 * All the functions are op overloads.
127 template<typename _Tp>
128 struct _Fwd_list_iterator
130 typedef _Fwd_list_iterator<_Tp> _Self;
131 typedef _Fwd_list_node<_Tp> _Node;
133 typedef _Tp value_type;
134 typedef _Tp* pointer;
135 typedef _Tp& reference;
136 typedef ptrdiff_t difference_type;
137 typedef std::forward_iterator_tag iterator_category;
139 _Fwd_list_iterator() : _M_node() { }
142 _Fwd_list_iterator(_Fwd_list_node_base* __n) : _M_node(__n) { }
146 { return static_cast<_Node*>(this->_M_node)->_M_value; }
150 { return &static_cast<_Node*>(this->_M_node)->_M_value; }
155 this->_M_node = this->_M_node->_M_next;
163 this->_M_node = this->_M_node->_M_next;
168 operator==(const _Self& __x) const
169 { return this->_M_node == __x._M_node; }
172 operator!=(const _Self& __x) const
173 { return this->_M_node != __x._M_node; }
179 return _Fwd_list_iterator(this->_M_node->_M_next);
181 return _Fwd_list_iterator(0);
184 _Fwd_list_node_base* _M_node;
188 * @brief A forward_list::const_iterator.
190 * All the functions are op overloads.
192 template<typename _Tp>
193 struct _Fwd_list_const_iterator
195 typedef _Fwd_list_const_iterator<_Tp> _Self;
196 typedef const _Fwd_list_node<_Tp> _Node;
197 typedef _Fwd_list_iterator<_Tp> iterator;
199 typedef _Tp value_type;
200 typedef const _Tp* pointer;
201 typedef const _Tp& reference;
202 typedef ptrdiff_t difference_type;
203 typedef std::forward_iterator_tag iterator_category;
205 _Fwd_list_const_iterator() : _M_node() { }
208 _Fwd_list_const_iterator(const _Fwd_list_node_base* __n)
211 _Fwd_list_const_iterator(const iterator& __iter)
212 : _M_node(__iter._M_node) { }
216 { return static_cast<_Node*>(this->_M_node)->_M_value; }
220 { return &static_cast<_Node*>(this->_M_node)->_M_value; }
225 this->_M_node = this->_M_node->_M_next;
233 this->_M_node = this->_M_node->_M_next;
238 operator==(const _Self& __x) const
239 { return this->_M_node == __x._M_node; }
242 operator!=(const _Self& __x) const
243 { return this->_M_node != __x._M_node; }
249 return _Fwd_list_const_iterator(this->_M_node->_M_next);
251 return _Fwd_list_const_iterator(0);
254 const _Fwd_list_node_base* _M_node;
258 * @brief Forward list iterator equality comparison.
260 template<typename _Tp>
262 operator==(const _Fwd_list_iterator<_Tp>& __x,
263 const _Fwd_list_const_iterator<_Tp>& __y)
264 { return __x._M_node == __y._M_node; }
267 * @brief Forward list iterator inequality comparison.
269 template<typename _Tp>
271 operator!=(const _Fwd_list_iterator<_Tp>& __x,
272 const _Fwd_list_const_iterator<_Tp>& __y)
273 { return __x._M_node != __y._M_node; }
276 * @brief Base class for @forward_list.
278 template<typename _Tp, typename _Alloc>
279 struct _Fwd_list_base
282 typedef typename _Alloc::template rebind<_Fwd_list_node<_Tp>>::other
285 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
287 struct _Fwd_list_impl
288 : public _Node_alloc_type
290 _Fwd_list_node_base _M_head;
293 : _Node_alloc_type(), _M_head()
296 _Fwd_list_impl(const _Node_alloc_type& __a)
297 : _Node_alloc_type(__a), _M_head()
301 _Fwd_list_impl _M_impl;
304 typedef _Fwd_list_iterator<_Tp> iterator;
305 typedef _Fwd_list_const_iterator<_Tp> const_iterator;
307 typedef _Fwd_list_node<_Tp> _Node;
310 _M_get_Node_allocator()
311 { return *static_cast<_Node_alloc_type*>(&this->_M_impl); }
313 const _Node_alloc_type&
314 _M_get_Node_allocator() const
315 { return *static_cast<const _Node_alloc_type*>(&this->_M_impl); }
319 { this->_M_impl._M_head._M_next = 0; }
321 _Fwd_list_base(const _Alloc& __a)
323 { this->_M_impl._M_head._M_next = 0; }
325 _Fwd_list_base(const _Fwd_list_base& __lst, const _Alloc& __a);
327 _Fwd_list_base(_Fwd_list_base&& __lst, const _Alloc& __a)
329 { _Fwd_list_node_base::swap(this->_M_impl._M_head,
330 __lst._M_impl._M_head); }
332 _Fwd_list_base(_Fwd_list_base&& __lst)
333 : _M_impl(__lst._M_get_Node_allocator())
334 { _Fwd_list_node_base::swap(this->_M_impl._M_head,
335 __lst._M_impl._M_head); }
338 { _M_erase_after(&_M_impl._M_head, 0); }
344 { return _M_get_Node_allocator().allocate(1); }
346 template<typename... _Args>
348 _M_create_node(_Args&&... __args)
350 _Node* __node = this->_M_get_node();
353 _M_get_Node_allocator().construct(__node,
354 std::forward<_Args>(__args)...);
359 this->_M_put_node(__node);
360 __throw_exception_again;
365 template<typename... _Args>
367 _M_insert_after(const_iterator __pos, _Args&&... __args);
370 _M_put_node(_Node* __p)
371 { _M_get_Node_allocator().deallocate(__p, 1); }
374 _M_erase_after(_Fwd_list_node_base* __pos);
377 _M_erase_after(_Fwd_list_node_base* __pos,
378 _Fwd_list_node_base* __last);
382 * @brief A standard container with linear time access to elements,
383 * and fixed time insertion/deletion at any point in the sequence.
385 * @ingroup Containers
388 * Meets the requirements of a <a href="tables.html#65">container</a>, a
389 * <a href="tables.html#67">sequence</a>, including the
390 * <a href="tables.html#68">optional sequence requirements</a> with the
391 * %exception of @c at and @c operator[].
393 * This is a @e singly @e linked %list. Traversal up the
394 * %list requires linear time, but adding and removing elements (or
395 * @e nodes) is done in constant time, regardless of where the
396 * change takes place. Unlike std::vector and std::deque,
397 * random-access iterators are not provided, so subscripting ( @c
398 * [] ) access is not allowed. For algorithms which only need
399 * sequential access, this lack makes no difference.
401 * Also unlike the other standard containers, std::forward_list provides
402 * specialized algorithms %unique to linked lists, such as
403 * splicing, sorting, and in-place reversal.
405 * A couple points on memory allocation for forward_list<Tp>:
407 * First, we never actually allocate a Tp, we allocate
408 * Fwd_list_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
409 * that after elements from %forward_list<X,Alloc1> are spliced into
410 * %forward_list<X,Alloc2>, destroying the memory of the second %list is a
411 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
413 template<typename _Tp, typename _Alloc = allocator<_Tp> >
414 class forward_list : private _Fwd_list_base<_Tp, _Alloc>
417 typedef _Fwd_list_base<_Tp, _Alloc> _Base;
418 typedef _Fwd_list_node<_Tp> _Node;
419 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
423 typedef _Tp value_type;
424 typedef typename _Tp_alloc_type::pointer pointer;
425 typedef typename _Tp_alloc_type::const_pointer const_pointer;
426 typedef typename _Tp_alloc_type::reference reference;
427 typedef typename _Tp_alloc_type::const_reference const_reference;
428 typedef _Fwd_list_iterator<_Tp> iterator;
429 typedef _Fwd_list_const_iterator<_Tp> const_iterator;
430 typedef std::size_t size_type;
431 typedef std::ptrdiff_t difference_type;
432 typedef _Alloc allocator_type;
434 // 23.2.3.1 construct/copy/destroy:
437 * @brief Creates a %forward_list with no elements.
438 * @param al An allocator object.
441 forward_list(const _Alloc& __al = _Alloc())
446 * @brief Copy constructor with allocator argument.
447 * @param list Input list to copy.
448 * @param al An allocator object.
450 forward_list(const forward_list& __list, const _Alloc& __al)
451 : _Base(__list, __al)
455 * @brief Move constructor with allocator argument.
456 * @param list Input list to move.
457 * @param al An allocator object.
459 forward_list(forward_list&& __list, const _Alloc& __al)
460 : _Base(std::forward<_Base>(__list), __al)
464 * @brief Creates a %forward_list with copies of the default element
466 * @param n The number of elements to initially create.
468 * This constructor fills the %forward_list with @a n copies of
472 forward_list(size_type __n)
474 { _M_fill_initialize(__n, value_type()); }
477 * @brief Creates a %forward_list with copies of an exemplar element.
478 * @param n The number of elements to initially create.
479 * @param value An element to copy.
480 * @param al An allocator object.
482 * This constructor fills the %forward_list with @a n copies of @a
485 forward_list(size_type __n, const _Tp& __value,
486 const _Alloc& __al = _Alloc())
488 { _M_fill_initialize(__n, __value); }
491 * @brief Builds a %forward_list from a range.
492 * @param first An input iterator.
493 * @param last An input iterator.
494 * @param al An allocator object.
496 * Create a %forward_list consisting of copies of the elements from
497 * [@a first,@a last). This is linear in N (where N is
498 * distance(@a first,@a last)).
500 template<typename _InputIterator>
501 forward_list(_InputIterator __first, _InputIterator __last,
502 const _Alloc& __al = _Alloc())
505 // Check whether it's an integral type. If so, it's not an iterator.
506 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
507 _M_initialize_dispatch(__first, __last, _Integral());
511 * @brief The %forward_list copy constructor.
512 * @param list A %forward_list of identical element and allocator
515 * The newly-created %forward_list uses a copy of the allocation
516 * object used by @a list.
518 forward_list(const forward_list& __list)
519 : _Base(__list.get_allocator())
520 { _M_initialize_dispatch(__list.begin(), __list.end(), __false_type()); }
523 * @brief The %forward_list move constructor.
524 * @param list A %forward_list of identical element and allocator
527 * The newly-created %forward_list contains the exact contents of @a
528 * forward_list. The contents of @a list are a valid, but unspecified
531 forward_list(forward_list&& __list)
532 : _Base(std::forward<_Base>(__list)) { }
535 * @brief Builds a %forward_list from an initializer_list
536 * @param il An initializer_list of value_type.
537 * @param al An allocator object.
539 * Create a %forward_list consisting of copies of the elements
540 * in the initializer_list @a il. This is linear in il.size().
542 forward_list(std::initializer_list<_Tp> __il,
543 const _Alloc& __al = _Alloc())
545 { _M_initialize_dispatch(__il.begin(), __il.end(), __false_type()); }
548 * @brief The forward_list dtor.
551 { _M_erase_after(&this->_M_impl._M_head, 0); }
554 * @brief The %forward_list assignment operator.
555 * @param list A %forward_list of identical element and allocator
558 * All the elements of @a list are copied, but unlike the copy
559 * constructor, the allocator object is not copied.
562 operator=(const forward_list& __list);
565 * @brief The %forward_list move assignment operator.
566 * @param list A %forward_list of identical element and allocator
569 * The contents of @a list are moved into this %forward_list
570 * (without copying). @a list is a valid, but unspecified
574 operator=(forward_list&& __list)
585 * @brief The %forward_list initializer list assignment operator.
586 * @param il An initializer_list of value_type.
588 * Replace the contents of the %forward_list with copies of the
589 * elements in the initializer_list @a il. This is linear in
593 operator=(std::initializer_list<_Tp> __il)
600 * @brief Assigns a range to a %forward_list.
601 * @param first An input iterator.
602 * @param last An input iterator.
604 * This function fills a %forward_list with copies of the elements
605 * in the range [@a first,@a last).
607 * Note that the assignment completely changes the %forward_list and
608 * that the resulting %forward_list's size is the same as the number
609 * of elements assigned. Old data may be lost.
611 template<typename _InputIterator>
613 assign(_InputIterator __first, _InputIterator __last)
616 insert_after(cbefore_begin(), __first, __last);
620 * @brief Assigns a given value to a %forward_list.
621 * @param n Number of elements to be assigned.
622 * @param val Value to be assigned.
624 * This function fills a %forward_list with @a n copies of the given
625 * value. Note that the assignment completely changes the
626 * %forward_list and that the resulting %forward_list's size is the
627 * same as the number of elements assigned. Old data may be lost.
630 assign(size_type __n, const _Tp& __val)
633 insert_after(cbefore_begin(), __n, __val);
637 * @brief Assigns an initializer_list to a %forward_list.
638 * @param il An initializer_list of value_type.
640 * Replace the contents of the %forward_list with copies of the
641 * elements in the initializer_list @a il. This is linear in
645 assign(std::initializer_list<_Tp> __il)
648 insert_after(cbefore_begin(), __il);
651 /// Get a copy of the memory allocation object.
653 get_allocator() const
654 { return this->_M_get_Node_allocator(); }
656 // 23.2.3.2 iterators:
659 * Returns a read/write iterator that points before the first element
660 * in the %forward_list. Iteration is done in ordinary element order.
664 { return iterator(&this->_M_impl._M_head); }
667 * Returns a read-only (constant) iterator that points before the
668 * first element in the %forward_list. Iteration is done in ordinary
673 { return const_iterator(&this->_M_impl._M_head); }
676 * Returns a read/write iterator that points to the first element
677 * in the %forward_list. Iteration is done in ordinary element order.
681 { return iterator(this->_M_impl._M_head._M_next); }
684 * Returns a read-only (constant) iterator that points to the first
685 * element in the %forward_list. Iteration is done in ordinary
690 { return const_iterator(this->_M_impl._M_head._M_next); }
693 * Returns a read/write iterator that points one past the last
694 * element in the %forward_list. Iteration is done in ordinary
699 { return iterator(0); }
702 * Returns a read-only iterator that points one past the last
703 * element in the %forward_list. Iteration is done in ordinary
708 { return const_iterator(0); }
711 * Returns a read-only (constant) iterator that points to the
712 * first element in the %forward_list. Iteration is done in ordinary
717 { return const_iterator(this->_M_impl._M_head._M_next); }
720 * Returns a read-only (constant) iterator that points before the
721 * first element in the %forward_list. Iteration is done in ordinary
725 cbefore_begin() const
726 { return const_iterator(&this->_M_impl._M_head); }
729 * Returns a read-only (constant) iterator that points one past
730 * the last element in the %forward_list. Iteration is done in
731 * ordinary element order.
735 { return const_iterator(0); }
738 * Returns true if the %forward_list is empty. (Thus begin() would
743 { return this->_M_impl._M_head._M_next == 0; }
746 * Returns the largest possible size of %forward_list.
750 { return this->_M_get_Node_allocator().max_size(); }
752 // 23.2.3.3 element access:
755 * Returns a read/write reference to the data at the first
756 * element of the %forward_list.
761 _Node* __front = static_cast<_Node*>(this->_M_impl._M_head._M_next);
762 return __front->_M_value;
766 * Returns a read-only (constant) reference to the data at the first
767 * element of the %forward_list.
772 _Node* __front = static_cast<_Node*>(this->_M_impl._M_head._M_next);
773 return __front->_M_value;
776 // 23.2.3.4 modifiers:
779 * @brief Constructs object in %forward_list at the front of the
781 * @param args Arguments.
783 * This function will insert an object of type Tp constructed
784 * with Tp(std::forward<Args>(args)...) at the front of the list
785 * Due to the nature of a %forward_list this operation can
786 * be done in constant time, and does not invalidate iterators
789 template<typename... _Args>
791 emplace_front(_Args&&... __args)
792 { this->_M_insert_after(cbefore_begin(),
793 std::forward<_Args>(__args)...); }
796 * @brief Add data to the front of the %forward_list.
797 * @param val Data to be added.
799 * This is a typical stack operation. The function creates an
800 * element at the front of the %forward_list and assigns the given
801 * data to it. Due to the nature of a %forward_list this operation
802 * can be done in constant time, and does not invalidate iterators
806 push_front(const _Tp& __val)
807 { this->_M_insert_after(cbefore_begin(), __val); }
813 push_front(_Tp&& __val)
814 { this->_M_insert_after(cbefore_begin(), std::move(__val)); }
817 * @brief Removes first element.
819 * This is a typical stack operation. It shrinks the %forward_list
820 * by one. Due to the nature of a %forward_list this operation can
821 * be done in constant time, and only invalidates iterators/references
822 * to the element being removed.
824 * Note that no data is returned, and if the first element's data
825 * is needed, it should be retrieved before pop_front() is
830 { this->_M_erase_after(&this->_M_impl._M_head); }
833 * @brief Constructs object in %forward_list after the specified
835 * @param pos A const_iterator into the %forward_list.
836 * @param args Arguments.
837 * @return An iterator that points to the inserted data.
839 * This function will insert an object of type T constructed
840 * with T(std::forward<Args>(args)...) after the specified
841 * location. Due to the nature of a %forward_list this operation can
842 * be done in constant time, and does not invalidate iterators
845 template<typename... _Args>
847 emplace_after(const_iterator __pos, _Args&&... __args)
848 { return iterator(this->_M_insert_after(__pos,
849 std::forward<_Args>(__args)...)); }
852 * @brief Inserts given value into %forward_list after specified
854 * @param pos An iterator into the %forward_list.
855 * @param val Data to be inserted.
856 * @return An iterator that points to the inserted data.
858 * This function will insert a copy of the given value after
859 * the specified location. Due to the nature of a %forward_list this
860 * operation can be done in constant time, and does not
861 * invalidate iterators and references.
864 insert_after(const_iterator __pos, const _Tp& __val)
865 { return iterator(this->_M_insert_after(__pos, __val)); }
871 insert_after(const_iterator __pos, _Tp&& __val)
872 { return iterator(this->_M_insert_after(__pos, std::move(__val))); }
875 * @brief Inserts a number of copies of given data into the
877 * @param pos An iterator into the %forward_list.
878 * @param n Number of elements to be inserted.
879 * @param val Data to be inserted.
881 * This function will insert a specified number of copies of the
882 * given data after the location specified by @a pos.
884 * This operation is linear in the number of elements inserted and
885 * does not invalidate iterators and references.
888 insert_after(const_iterator __pos, size_type __n, const _Tp& __val)
890 forward_list __tmp(__n, __val, this->get_allocator());
891 this->splice_after(__pos, std::move(__tmp));
895 * @brief Inserts a range into the %forward_list.
896 * @param position An iterator into the %forward_list.
897 * @param first An input iterator.
898 * @param last An input iterator.
900 * This function will insert copies of the data in the range [@a
901 * first,@a last) into the %forward_list after the location specified
904 * This operation is linear in the number of elements inserted and
905 * does not invalidate iterators and references.
907 template<typename _InputIterator>
909 insert_after(const_iterator __pos,
910 _InputIterator __first, _InputIterator __last)
912 forward_list __tmp(__first, __last, this->get_allocator());
913 this->splice_after(__pos, std::move(__tmp));
917 * @brief Inserts the contents of an initializer_list into
918 * %forward_list after the specified iterator.
919 * @param pos An iterator into the %forward_list.
920 * @param il An initializer_list of value_type.
922 * This function will insert copies of the data in the
923 * initializer_list @a il into the %forward_list before the location
924 * specified by @a pos.
926 * This operation is linear in the number of elements inserted and
927 * does not invalidate iterators and references.
930 insert_after(const_iterator __pos, std::initializer_list<_Tp> __il)
932 forward_list __tmp(__il, this->get_allocator());
933 this->splice_after(__pos, std::move(__tmp));
937 * @brief Removes the element pointed to by the iterator following
939 * @param pos Iterator pointing to element to be erased.
940 * @return An iterator pointing to the next element (or end()).
942 * This function will erase the element at the given position and
943 * thus shorten the %forward_list by one.
945 * Due to the nature of a %forward_list this operation can be done
946 * in constant time, and only invalidates iterators/references to
947 * the element being removed. The user is also cautioned that
948 * this function only erases the element, and that if the element
949 * is itself a pointer, the pointed-to memory is not touched in
950 * any way. Managing the pointer is the user's responsibility.
953 erase_after(const_iterator __pos)
955 _Fwd_list_node_base* __tmp
956 = const_cast<_Fwd_list_node_base*>(__pos._M_node);
958 return iterator(this->_M_erase_after(__tmp));
964 * @brief Remove a range of elements.
965 * @param pos Iterator pointing before the first element to be
967 * @param last Iterator pointing to one past the last element to be
969 * @return An iterator pointing to the element pointed to by @a last
970 * prior to erasing (or end()).
972 * This function will erase the elements in the range @a
973 * (pos,last) and shorten the %forward_list accordingly.
975 * This operation is linear time in the size of the range and only
976 * invalidates iterators/references to the element being removed.
977 * The user is also cautioned that this function only erases the
978 * elements, and that if the elements themselves are pointers, the
979 * pointed-to memory is not touched in any way. Managing the pointer
980 * is the user's responsibility.
983 erase_after(const_iterator __pos, iterator __last)
985 _Fwd_list_node_base* __tmp
986 = const_cast<_Fwd_list_node_base*>(__pos._M_node);
987 return iterator(this->_M_erase_after(__tmp, __last._M_node));
991 * @brief Swaps data with another %forward_list.
992 * @param list A %forward_list of the same element and allocator
995 * This exchanges the elements between two lists in constant
996 * time. Note that the global std::swap() function is
997 * specialized such that std::swap(l1,l2) will feed to this
1001 swap(forward_list&& __list)
1002 { _Fwd_list_node_base::swap(this->_M_impl._M_head,
1003 __list._M_impl._M_head); }
1006 * @brief Resizes the %forward_list to the specified number of
1008 * @param sz Number of elements the %forward_list should contain.
1010 * This function will %resize the %forward_list to the specified
1011 * number of elements. If the number is smaller than the
1012 * %forward_list's current size the %forward_list is truncated,
1013 * otherwise the %forward_list is extended and new elements are
1014 * populated with given data.
1017 resize(size_type __sz)
1018 { resize(__sz, _Tp()); }
1021 * @brief Resizes the %forward_list to the specified number of
1023 * @param sz Number of elements the %forward_list should contain.
1024 * @param val Data with which new elements should be populated.
1026 * This function will %resize the %forward_list to the specified
1027 * number of elements. If the number is smaller than the
1028 * %forward_list's current size the %forward_list is truncated,
1029 * otherwise the %forward_list is extended and new elements are
1030 * populated with given data.
1033 resize(size_type __sz, value_type __val);
1036 * @brief Erases all the elements.
1038 * Note that this function only erases
1039 * the elements, and that if the elements themselves are
1040 * pointers, the pointed-to memory is not touched in any way.
1041 * Managing the pointer is the user's responsibility.
1045 { this->_M_erase_after(&this->_M_impl._M_head, 0); }
1047 // 23.2.3.5 forward_list operations:
1050 * @brief Insert contents of another %forward_list.
1051 * @param pos Iterator referencing the element to insert after.
1052 * @param list Source list.
1054 * The elements of @a list are inserted in constant time after
1055 * the element referenced by @a pos. @a list becomes an empty
1058 * Requires this != @a x.
1061 splice_after(const_iterator __pos, forward_list&& __list);
1064 * @brief Insert element from another %forward_list.
1065 * @param pos Iterator referencing the element to insert after.
1066 * @param list Source list.
1067 * @param it Iterator referencing the element before the element
1070 * Removes the element in list @a list referenced by @a i and
1071 * inserts it into the current list after @a pos.
1074 splice_after(const_iterator __pos, forward_list&& __list,
1075 const_iterator __it)
1076 { this->splice_after(__pos, __list, __it, __it._M_next()); }
1079 * @brief Insert range from another %forward_list.
1080 * @param pos Iterator referencing the element to insert after.
1081 * @param list Source list.
1082 * @param before Iterator referencing before the start of range
1084 * @param last Iterator referencing the end of range in list.
1086 * Removes elements in the range (before,last) and inserts them
1087 * after @a pos in constant time.
1089 * Undefined if @a pos is in (before,last).
1092 splice_after(const_iterator __pos, forward_list&& __list,
1093 const_iterator __before, const_iterator __last);
1096 * @brief Remove all elements equal to value.
1097 * @param val The value to remove.
1099 * Removes every element in the list equal to @a value.
1100 * Remaining elements stay in list order. Note that this
1101 * function only erases the elements, and that if the elements
1102 * themselves are pointers, the pointed-to memory is not
1103 * touched in any way. Managing the pointer is the user's
1107 remove(const _Tp& __val);
1110 * @brief Remove all elements satisfying a predicate.
1111 * @param pred Unary predicate function or object.
1113 * Removes every element in the list for which the predicate
1114 * returns true. Remaining elements stay in list order. Note
1115 * that this function only erases the elements, and that if the
1116 * elements themselves are pointers, the pointed-to memory is
1117 * not touched in any way. Managing the pointer is the user's
1120 template<typename _Pred>
1122 remove_if(_Pred __pred);
1125 * @brief Remove consecutive duplicate elements.
1127 * For each consecutive set of elements with the same value,
1128 * remove all but the first one. Remaining elements stay in
1129 * list order. Note that this function only erases the
1130 * elements, and that if the elements themselves are pointers,
1131 * the pointed-to memory is not touched in any way. Managing
1132 * the pointer is the user's responsibility.
1136 { this->unique(std::equal_to<_Tp>()); }
1139 * @brief Remove consecutive elements satisfying a predicate.
1140 * @param binary_pred Binary predicate function or object.
1142 * For each consecutive set of elements [first,last) that
1143 * satisfy predicate(first,i) where i is an iterator in
1144 * [first,last), remove all but the first one. Remaining
1145 * elements stay in list order. Note that this function only
1146 * erases the elements, and that if the elements themselves are
1147 * pointers, the pointed-to memory is not touched in any way.
1148 * Managing the pointer is the user's responsibility.
1150 template<typename _BinPred>
1152 unique(_BinPred __binary_pred);
1155 * @brief Merge sorted lists.
1156 * @param list Sorted list to merge.
1158 * Assumes that both @a list and this list are sorted according to
1159 * operator<(). Merges elements of @a list into this list in
1160 * sorted order, leaving @a list empty when complete. Elements in
1161 * this list precede elements in @a list that are equal.
1164 merge(forward_list&& __list)
1165 { this->merge(__list, std::less<_Tp>()); }
1168 * @brief Merge sorted lists according to comparison function.
1169 * @param list Sorted list to merge.
1170 * @param comp Comparison function defining sort order.
1172 * Assumes that both @a list and this list are sorted according to
1173 * comp. Merges elements of @a list into this list
1174 * in sorted order, leaving @a list empty when complete. Elements
1175 * in this list precede elements in @a list that are equivalent
1176 * according to comp().
1178 template<typename _Comp>
1180 merge(forward_list&& __list, _Comp __comp);
1183 * @brief Sort the elements of the list.
1185 * Sorts the elements of this list in NlogN time. Equivalent
1186 * elements remain in list order.
1191 _Node* __tmp = static_cast<_Node*>(&this->_M_impl._M_head);
1192 __tmp->_M_sort_after(std::less<_Tp>());
1196 * @brief Sort the forward_list using a comparison function.
1198 * Sorts the elements of this list in NlogN time. Equivalent
1199 * elements remain in list order.
1201 template<typename _Comp>
1205 _Node* __tmp = static_cast<_Node*>(&this->_M_impl._M_head);
1206 __tmp->_M_sort_after(__comp);
1210 * @brief Reverse the elements in list.
1212 * Reverse the order of elements in the list in linear time.
1218 template<typename _Integer>
1220 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1221 { _M_fill_initialize(static_cast<size_type>(__n), __x); }
1223 // Called by the range constructor to implement [23.1.1]/9
1224 template<typename _InputIterator>
1226 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1229 // Called by forward_list(n,v,a), and the range constructor when it
1230 // turns out to be the same thing.
1232 _M_fill_initialize(size_type __n, const value_type& __value);
1236 * @brief Forward list equality comparison.
1237 * @param lx A %forward_list
1238 * @param ly A %forward_list of the same type as @a lx.
1239 * @return True iff the size and elements of the forward lists are equal.
1241 * This is an equivalence relation. It is linear in the size of the
1242 * forward lists. Deques are considered equivalent if corresponding
1243 * elements compare equal.
1245 template<typename _Tp, typename _Alloc>
1247 operator==(const forward_list<_Tp, _Alloc>& __lx,
1248 const forward_list<_Tp, _Alloc>& __ly);
1251 * @brief Forward list ordering relation.
1252 * @param lx A %forward_list.
1253 * @param ly A %forward_list of the same type as @a lx.
1254 * @return True iff @a lx is lexicographically less than @a ly.
1256 * This is a total ordering relation. It is linear in the size of the
1257 * forward lists. The elements must be comparable with @c <.
1259 * See std::lexicographical_compare() for how the determination is made.
1261 template<typename _Tp, typename _Alloc>
1263 operator<(const forward_list<_Tp, _Alloc>& __lx,
1264 const forward_list<_Tp, _Alloc>& __ly)
1265 { return std::lexicographical_compare(__lx.cbegin(), __lx.cend(),
1266 __ly.cbegin(), __ly.cend()); }
1268 /// Based on operator==
1269 template<typename _Tp, typename _Alloc>
1271 operator!=(const forward_list<_Tp, _Alloc>& __lx,
1272 const forward_list<_Tp, _Alloc>& __ly)
1273 { return !(__lx == __ly); }
1275 /// Based on operator<
1276 template<typename _Tp, typename _Alloc>
1278 operator>(const forward_list<_Tp, _Alloc>& __lx,
1279 const forward_list<_Tp, _Alloc>& __ly)
1280 { return (__ly < __lx); }
1282 /// Based on operator<
1283 template<typename _Tp, typename _Alloc>
1285 operator>=(const forward_list<_Tp, _Alloc>& __lx,
1286 const forward_list<_Tp, _Alloc>& __ly)
1287 { return !(__lx < __ly); }
1289 /// Based on operator<
1290 template<typename _Tp, typename _Alloc>
1292 operator<=(const forward_list<_Tp, _Alloc>& __lx,
1293 const forward_list<_Tp, _Alloc>& __ly)
1294 { return !(__ly < __lx); }
1296 /// See std::forward_list::forward_swap().
1297 template<typename _Tp, typename _Alloc>
1299 swap(forward_list<_Tp, _Alloc>& __lx,
1300 forward_list<_Tp, _Alloc>& __ly)
1301 { __lx.swap(__ly); }
1303 /// See std::forward_list::forward_swap().
1304 template<typename _Tp, typename _Alloc>
1306 swap(forward_list<_Tp, _Alloc>&& __lx,
1307 forward_list<_Tp, _Alloc>& __ly)
1308 { __lx.swap(__ly); }
1310 /// See std::forward_list::forward_swap().
1311 template<typename _Tp, typename _Alloc>
1313 swap(forward_list<_Tp, _Alloc>& __lx,
1314 forward_list<_Tp, _Alloc>&& __ly)
1315 { __lx.swap(__ly); }
1317 _GLIBCXX_END_NAMESPACE // namespace std
1319 #endif // __GXX_EXPERIMENTAL_CXX0X__
1321 #endif // _FORWARD_LIST_H