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 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.h
26 * This is a Standard C++ Library header.
29 #ifndef _FORWARD_LIST_H
30 #define _FORWARD_LIST_H 1
32 #pragma GCC system_header
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
35 # include <c++0x_warning.h>
39 #include <initializer_list>
42 _GLIBCXX_BEGIN_NAMESPACE(std)
44 using __gnu_cxx::__static_pointer_cast;
45 using __gnu_cxx::__const_pointer_cast;
48 * @brief A helper basic node class for %forward_list.
49 * This is just a linked list with nothing inside it.
50 * There are purely list shuffling utility methods here.
52 template<typename _Alloc>
53 struct _Fwd_list_node_base
55 // The type allocated by _Alloc cannot be this type, so we rebind
56 typedef typename _Alloc::template rebind<_Fwd_list_node_base<_Alloc> >
57 ::other::pointer _Pointer;
58 typedef typename _Alloc::template rebind<_Fwd_list_node_base<_Alloc> >
59 ::other::const_pointer _Const_pointer;
63 _Fwd_list_node_base() : _M_next(0) { }
66 swap(_Fwd_list_node_base& __x, _Fwd_list_node_base& __y)
67 { std::swap(__x._M_next, __y._M_next); }
70 _M_transfer_after(_Pointer __bbegin);
73 _M_transfer_after(_Pointer __bbegin, _Pointer __bend);
80 * @brief A helper node class for %forward_list.
81 * This is just a linked list with a data value in each node.
82 * There is a sorting utility method.
84 template<typename _Tp, typename _Alloc>
85 struct _Fwd_list_node : public _Fwd_list_node_base<_Alloc>
87 typedef typename _Alloc::template rebind<_Fwd_list_node<_Tp, _Alloc> >
88 ::other::pointer _Pointer;
90 template<typename... _Args>
91 _Fwd_list_node(_Args&&... __args)
92 : _Fwd_list_node_base<_Alloc>(),
93 _M_value(std::forward<_Args>(__args)...) { }
99 * @brief A forward_list::iterator.
101 * All the functions are op overloads.
103 template<typename _Tp, typename _Alloc>
104 struct _Fwd_list_iterator
106 typedef _Fwd_list_iterator<_Tp, _Alloc> _Self;
107 typedef _Fwd_list_node<_Tp, _Alloc> _Node;
108 typedef _Fwd_list_node_base<_Alloc> _Node_base;
110 typedef _Tp value_type;
111 typedef typename _Alloc::pointer pointer;
112 typedef typename _Alloc::reference reference;
113 typedef typename _Alloc::difference_type difference_type;
114 typedef std::forward_iterator_tag iterator_category;
116 _Fwd_list_iterator() : _M_node() { }
119 _Fwd_list_iterator(typename _Node_base::_Pointer __n)
124 { return __static_pointer_cast<_Node*>(_M_node)->_M_value; }
128 { return &__static_pointer_cast<_Node*>(_M_node)->_M_value; }
133 _M_node = _M_node->_M_next;
141 _M_node = _M_node->_M_next;
146 operator==(const _Self& __x) const
147 { return _M_node == __x._M_node; }
150 operator!=(const _Self& __x) const
151 { return _M_node != __x._M_node; }
157 return _Fwd_list_iterator(_M_node->_M_next);
159 return _Fwd_list_iterator(0);
162 typename _Node_base::_Pointer _M_node;
166 * @brief A forward_list::const_iterator.
168 * All the functions are op overloads.
170 template<typename _Tp, typename _Alloc>
171 struct _Fwd_list_const_iterator
173 typedef _Fwd_list_const_iterator<_Tp, _Alloc> _Self;
174 typedef const _Fwd_list_node<_Tp, _Alloc> _Node;
175 typedef const _Fwd_list_node_base<_Alloc> _Node_base;
176 typedef _Fwd_list_iterator<_Tp, _Alloc> iterator;
178 typedef _Tp value_type;
179 typedef typename _Alloc::const_pointer pointer;
180 typedef typename _Alloc::const_reference reference;
181 typedef typename _Alloc::difference_type difference_type;
182 typedef std::forward_iterator_tag iterator_category;
184 _Fwd_list_const_iterator() : _M_node() { }
187 _Fwd_list_const_iterator(typename _Node_base::_Const_pointer __n)
190 _Fwd_list_const_iterator(const iterator& __iter)
191 : _M_node(__iter._M_node) { }
195 { return __static_pointer_cast<_Node*>(_M_node)->_M_value; }
199 { return &__static_pointer_cast<_Node*>(_M_node)->_M_value; }
204 _M_node = _M_node->_M_next;
212 _M_node = _M_node->_M_next;
217 operator==(const _Self& __x) const
218 { return _M_node == __x._M_node; }
221 operator!=(const _Self& __x) const
222 { return _M_node != __x._M_node; }
228 return _Fwd_list_const_iterator(_M_node->_M_next);
230 return _Fwd_list_const_iterator(0);
233 typename _Node_base::_Const_pointer _M_node;
237 * @brief Forward list iterator equality comparison.
239 template<typename _Tp, typename _Alloc>
241 operator==(const _Fwd_list_iterator<_Tp, _Alloc>& __x,
242 const _Fwd_list_const_iterator<_Tp, _Alloc>& __y)
243 { return __x._M_node == __y._M_node; }
246 * @brief Forward list iterator inequality comparison.
248 template<typename _Tp, typename _Alloc>
250 operator!=(const _Fwd_list_iterator<_Tp, _Alloc>& __x,
251 const _Fwd_list_const_iterator<_Tp, _Alloc>& __y)
252 { return __x._M_node != __y._M_node; }
255 * @brief Base class for %forward_list.
257 template<typename _Tp, typename _Alloc>
258 struct _Fwd_list_base
261 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
263 typedef typename _Alloc::template
264 rebind<_Fwd_list_node<_Tp, _Tp_alloc_type>>::other _Node_alloc_type;
266 struct _Fwd_list_impl
267 : public _Node_alloc_type
269 _Fwd_list_node_base<_Tp_alloc_type> _M_head;
272 : _Node_alloc_type(), _M_head()
275 _Fwd_list_impl(const _Node_alloc_type& __a)
276 : _Node_alloc_type(__a), _M_head()
280 _Fwd_list_impl _M_impl;
283 typedef _Fwd_list_iterator<_Tp, _Tp_alloc_type> iterator;
284 typedef _Fwd_list_const_iterator<_Tp, _Tp_alloc_type> const_iterator;
286 typedef _Fwd_list_node<_Tp, _Tp_alloc_type> _Node;
287 typedef _Fwd_list_node_base<_Tp_alloc_type> _Node_base;
290 _M_get_Node_allocator()
291 { return *static_cast<_Node_alloc_type*>(&this->_M_impl); }
293 const _Node_alloc_type&
294 _M_get_Node_allocator() const
295 { return *static_cast<const _Node_alloc_type*>(&this->_M_impl); }
299 { this->_M_impl._M_head._M_next = 0; }
301 _Fwd_list_base(const _Alloc& __a)
303 { this->_M_impl._M_head._M_next = 0; }
305 _Fwd_list_base(const _Fwd_list_base& __lst, const _Alloc& __a);
307 _Fwd_list_base(_Fwd_list_base&& __lst, const _Alloc& __a)
309 { _Node_base::swap(this->_M_impl._M_head,
310 __lst._M_impl._M_head); }
312 _Fwd_list_base(_Fwd_list_base&& __lst)
313 : _M_impl(__lst._M_get_Node_allocator())
314 { _Node_base::swap(this->_M_impl._M_head,
315 __lst._M_impl._M_head); }
318 { _M_erase_after(&_M_impl._M_head, 0); }
322 typename _Node::_Pointer
324 { return _M_get_Node_allocator().allocate(1); }
326 template<typename... _Args>
327 typename _Node::_Pointer
328 _M_create_node(_Args&&... __args)
330 typename _Node::_Pointer __node = this->_M_get_node();
333 _M_get_Node_allocator().construct(__node,
334 std::forward<_Args>(__args)...);
339 this->_M_put_node(__node);
340 __throw_exception_again;
345 template<typename... _Args>
346 typename _Node_base::_Pointer
347 _M_insert_after(const_iterator __pos, _Args&&... __args);
350 _M_put_node(typename _Node::_Pointer __p)
351 { _M_get_Node_allocator().deallocate(__p, 1); }
354 _M_erase_after(typename _Node_base::_Pointer __pos);
357 _M_erase_after(typename _Node_base::_Pointer __pos,
358 typename _Node_base::_Pointer __last);
362 * @brief A standard container with linear time access to elements,
363 * and fixed time insertion/deletion at any point in the sequence.
367 * Meets the requirements of a <a href="tables.html#65">container</a>, a
368 * <a href="tables.html#67">sequence</a>, including the
369 * <a href="tables.html#68">optional sequence requirements</a> with the
370 * %exception of @c at and @c operator[].
372 * This is a @e singly @e linked %list. Traversal up the
373 * %list requires linear time, but adding and removing elements (or
374 * @e nodes) is done in constant time, regardless of where the
375 * change takes place. Unlike std::vector and std::deque,
376 * random-access iterators are not provided, so subscripting ( @c
377 * [] ) access is not allowed. For algorithms which only need
378 * sequential access, this lack makes no difference.
380 * Also unlike the other standard containers, std::forward_list provides
381 * specialized algorithms %unique to linked lists, such as
382 * splicing, sorting, and in-place reversal.
384 * A couple points on memory allocation for forward_list<Tp>:
386 * First, we never actually allocate a Tp, we allocate
387 * Fwd_list_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
388 * that after elements from %forward_list<X,Alloc1> are spliced into
389 * %forward_list<X,Alloc2>, destroying the memory of the second %list is a
390 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
392 template<typename _Tp, typename _Alloc = allocator<_Tp> >
393 class forward_list : private _Fwd_list_base<_Tp, _Alloc>
396 typedef _Fwd_list_base<_Tp, _Alloc> _Base;
397 typedef typename _Base::_Node _Node;
398 typedef typename _Base::_Node_base _Node_base;
399 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
403 typedef _Tp value_type;
404 typedef typename _Tp_alloc_type::pointer pointer;
405 typedef typename _Tp_alloc_type::const_pointer const_pointer;
406 typedef typename _Tp_alloc_type::reference reference;
407 typedef typename _Tp_alloc_type::const_reference const_reference;
409 typedef typename _Base::iterator iterator;
410 typedef typename _Base::const_iterator const_iterator;
411 typedef std::size_t size_type;
412 typedef std::ptrdiff_t difference_type;
413 typedef _Alloc allocator_type;
415 // 23.2.3.1 construct/copy/destroy:
418 * @brief Creates a %forward_list with no elements.
419 * @param al An allocator object.
422 forward_list(const _Alloc& __al = _Alloc())
427 * @brief Copy constructor with allocator argument.
428 * @param list Input list to copy.
429 * @param al An allocator object.
431 forward_list(const forward_list& __list, const _Alloc& __al)
432 : _Base(__list, __al)
436 * @brief Move constructor with allocator argument.
437 * @param list Input list to move.
438 * @param al An allocator object.
440 forward_list(forward_list&& __list, const _Alloc& __al)
441 : _Base(std::forward<_Base>(__list), __al)
445 * @brief Creates a %forward_list with copies of the default element
447 * @param n The number of elements to initially create.
449 * This constructor fills the %forward_list with @a n copies of
453 forward_list(size_type __n)
455 { _M_fill_initialize(__n, value_type()); }
458 * @brief Creates a %forward_list with copies of an exemplar element.
459 * @param n The number of elements to initially create.
460 * @param value An element to copy.
461 * @param al An allocator object.
463 * This constructor fills the %forward_list with @a n copies of @a
466 forward_list(size_type __n, const _Tp& __value,
467 const _Alloc& __al = _Alloc())
469 { _M_fill_initialize(__n, __value); }
472 * @brief Builds a %forward_list from a range.
473 * @param first An input iterator.
474 * @param last An input iterator.
475 * @param al An allocator object.
477 * Create a %forward_list consisting of copies of the elements from
478 * [@a first,@a last). This is linear in N (where N is
479 * distance(@a first,@a last)).
481 template<typename _InputIterator>
482 forward_list(_InputIterator __first, _InputIterator __last,
483 const _Alloc& __al = _Alloc())
486 // Check whether it's an integral type. If so, it's not an iterator.
487 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
488 _M_initialize_dispatch(__first, __last, _Integral());
492 * @brief The %forward_list copy constructor.
493 * @param list A %forward_list of identical element and allocator
496 * The newly-created %forward_list uses a copy of the allocation
497 * object used by @a list.
499 forward_list(const forward_list& __list)
500 : _Base(__list.get_allocator())
501 { _M_initialize_dispatch(__list.begin(), __list.end(), __false_type()); }
504 * @brief The %forward_list move constructor.
505 * @param list A %forward_list of identical element and allocator
508 * The newly-created %forward_list contains the exact contents of @a
509 * forward_list. The contents of @a list are a valid, but unspecified
512 forward_list(forward_list&& __list)
513 : _Base(std::forward<_Base>(__list)) { }
516 * @brief Builds a %forward_list from an initializer_list
517 * @param il An initializer_list of value_type.
518 * @param al An allocator object.
520 * Create a %forward_list consisting of copies of the elements
521 * in the initializer_list @a il. This is linear in il.size().
523 forward_list(std::initializer_list<_Tp> __il,
524 const _Alloc& __al = _Alloc())
526 { _M_initialize_dispatch(__il.begin(), __il.end(), __false_type()); }
529 * @brief The forward_list dtor.
535 * @brief The %forward_list assignment operator.
536 * @param list A %forward_list of identical element and allocator
539 * All the elements of @a list are copied, but unlike the copy
540 * constructor, the allocator object is not copied.
543 operator=(const forward_list& __list);
546 * @brief The %forward_list move assignment operator.
547 * @param list A %forward_list of identical element and allocator
550 * The contents of @a list are moved into this %forward_list
551 * (without copying). @a list is a valid, but unspecified
555 operator=(forward_list&& __list)
566 * @brief The %forward_list initializer list assignment operator.
567 * @param il An initializer_list of value_type.
569 * Replace the contents of the %forward_list with copies of the
570 * elements in the initializer_list @a il. This is linear in
574 operator=(std::initializer_list<_Tp> __il)
581 * @brief Assigns a range to a %forward_list.
582 * @param first An input iterator.
583 * @param last An input iterator.
585 * This function fills a %forward_list with copies of the elements
586 * in the range [@a first,@a last).
588 * Note that the assignment completely changes the %forward_list and
589 * that the resulting %forward_list's size is the same as the number
590 * of elements assigned. Old data may be lost.
592 template<typename _InputIterator>
594 assign(_InputIterator __first, _InputIterator __last)
597 insert_after(cbefore_begin(), __first, __last);
601 * @brief Assigns a given value to a %forward_list.
602 * @param n Number of elements to be assigned.
603 * @param val Value to be assigned.
605 * This function fills a %forward_list with @a n copies of the given
606 * value. Note that the assignment completely changes the
607 * %forward_list and that the resulting %forward_list's size is the
608 * same as the number of elements assigned. Old data may be lost.
611 assign(size_type __n, const _Tp& __val)
614 insert_after(cbefore_begin(), __n, __val);
618 * @brief Assigns an initializer_list to a %forward_list.
619 * @param il An initializer_list of value_type.
621 * Replace the contents of the %forward_list with copies of the
622 * elements in the initializer_list @a il. This is linear in
626 assign(std::initializer_list<_Tp> __il)
629 insert_after(cbefore_begin(), __il);
632 /// Get a copy of the memory allocation object.
634 get_allocator() const
635 { return this->_M_get_Node_allocator(); }
637 // 23.2.3.2 iterators:
640 * Returns a read/write iterator that points before the first element
641 * in the %forward_list. Iteration is done in ordinary element order.
645 { return iterator(&this->_M_impl._M_head); }
648 * Returns a read-only (constant) iterator that points before the
649 * first element in the %forward_list. Iteration is done in ordinary
654 { return const_iterator(&this->_M_impl._M_head); }
657 * Returns a read/write iterator that points to the first element
658 * in the %forward_list. Iteration is done in ordinary element order.
662 { return iterator(this->_M_impl._M_head._M_next); }
665 * Returns a read-only (constant) iterator that points to the first
666 * element in the %forward_list. Iteration is done in ordinary
671 { return const_iterator(this->_M_impl._M_head._M_next); }
674 * Returns a read/write iterator that points one past the last
675 * element in the %forward_list. Iteration is done in ordinary
680 { return iterator(0); }
683 * Returns a read-only iterator that points one past the last
684 * element in the %forward_list. Iteration is done in ordinary
689 { return const_iterator(0); }
692 * Returns a read-only (constant) iterator that points to the
693 * first element in the %forward_list. Iteration is done in ordinary
698 { return const_iterator(this->_M_impl._M_head._M_next); }
701 * Returns a read-only (constant) iterator that points before the
702 * first element in the %forward_list. Iteration is done in ordinary
706 cbefore_begin() const
707 { return const_iterator(&this->_M_impl._M_head); }
710 * Returns a read-only (constant) iterator that points one past
711 * the last element in the %forward_list. Iteration is done in
712 * ordinary element order.
716 { return const_iterator(0); }
719 * Returns true if the %forward_list is empty. (Thus begin() would
724 { return this->_M_impl._M_head._M_next == 0; }
727 * Returns the largest possible size of %forward_list.
731 { return this->_M_get_Node_allocator().max_size(); }
733 // 23.2.3.3 element access:
736 * Returns a read/write reference to the data at the first
737 * element of the %forward_list.
743 __static_pointer_cast<_Node*>(this->_M_impl._M_head._M_next);
744 return __front->_M_value;
748 * Returns a read-only (constant) reference to the data at the first
749 * element of the %forward_list.
755 __static_pointer_cast<_Node*>(this->_M_impl._M_head._M_next);
756 return __front->_M_value;
759 // 23.2.3.4 modifiers:
762 * @brief Constructs object in %forward_list at the front of the
764 * @param args Arguments.
766 * This function will insert an object of type Tp constructed
767 * with Tp(std::forward<Args>(args)...) at the front of the list
768 * Due to the nature of a %forward_list this operation can
769 * be done in constant time, and does not invalidate iterators
772 template<typename... _Args>
774 emplace_front(_Args&&... __args)
775 { this->_M_insert_after(cbefore_begin(),
776 std::forward<_Args>(__args)...); }
779 * @brief Add data to the front of the %forward_list.
780 * @param val Data to be added.
782 * This is a typical stack operation. The function creates an
783 * element at the front of the %forward_list and assigns the given
784 * data to it. Due to the nature of a %forward_list this operation
785 * can be done in constant time, and does not invalidate iterators
789 push_front(const _Tp& __val)
790 { this->_M_insert_after(cbefore_begin(), __val); }
796 push_front(_Tp&& __val)
797 { this->_M_insert_after(cbefore_begin(), std::move(__val)); }
800 * @brief Removes first element.
802 * This is a typical stack operation. It shrinks the %forward_list
803 * by one. Due to the nature of a %forward_list this operation can
804 * be done in constant time, and only invalidates iterators/references
805 * to the element being removed.
807 * Note that no data is returned, and if the first element's data
808 * is needed, it should be retrieved before pop_front() is
813 { this->_M_erase_after(&this->_M_impl._M_head); }
816 * @brief Constructs object in %forward_list after the specified
818 * @param pos A const_iterator into the %forward_list.
819 * @param args Arguments.
820 * @return An iterator that points to the inserted data.
822 * This function will insert an object of type T constructed
823 * with T(std::forward<Args>(args)...) after the specified
824 * location. Due to the nature of a %forward_list this operation can
825 * be done in constant time, and does not invalidate iterators
828 template<typename... _Args>
830 emplace_after(const_iterator __pos, _Args&&... __args)
831 { return iterator(this->_M_insert_after(__pos,
832 std::forward<_Args>(__args)...)); }
835 * @brief Inserts given value into %forward_list after specified
837 * @param pos An iterator into the %forward_list.
838 * @param val Data to be inserted.
839 * @return An iterator that points to the inserted data.
841 * This function will insert a copy of the given value after
842 * the specified location. Due to the nature of a %forward_list this
843 * operation can be done in constant time, and does not
844 * invalidate iterators and references.
847 insert_after(const_iterator __pos, const _Tp& __val)
848 { return iterator(this->_M_insert_after(__pos, __val)); }
854 insert_after(const_iterator __pos, _Tp&& __val)
855 { return iterator(this->_M_insert_after(__pos, std::move(__val))); }
858 * @brief Inserts a number of copies of given data into the
860 * @param pos An iterator into the %forward_list.
861 * @param n Number of elements to be inserted.
862 * @param val Data to be inserted.
864 * This function will insert a specified number of copies of the
865 * given data after the location specified by @a pos.
867 * This operation is linear in the number of elements inserted and
868 * does not invalidate iterators and references.
871 insert_after(const_iterator __pos, size_type __n, const _Tp& __val)
873 forward_list __tmp(__n, __val, this->get_allocator());
874 splice_after(__pos, std::move(__tmp));
878 * @brief Inserts a range into the %forward_list.
879 * @param position An iterator into the %forward_list.
880 * @param first An input iterator.
881 * @param last An input iterator.
883 * This function will insert copies of the data in the range [@a
884 * first,@a last) into the %forward_list after the location specified
887 * This operation is linear in the number of elements inserted and
888 * does not invalidate iterators and references.
890 template<typename _InputIterator>
892 insert_after(const_iterator __pos,
893 _InputIterator __first, _InputIterator __last)
895 forward_list __tmp(__first, __last, this->get_allocator());
896 splice_after(__pos, std::move(__tmp));
900 * @brief Inserts the contents of an initializer_list into
901 * %forward_list after the specified iterator.
902 * @param pos An iterator into the %forward_list.
903 * @param il An initializer_list of value_type.
905 * This function will insert copies of the data in the
906 * initializer_list @a il into the %forward_list before the location
907 * specified by @a pos.
909 * This operation is linear in the number of elements inserted and
910 * does not invalidate iterators and references.
913 insert_after(const_iterator __pos, std::initializer_list<_Tp> __il)
915 forward_list __tmp(__il, this->get_allocator());
916 splice_after(__pos, std::move(__tmp));
920 * @brief Removes the element pointed to by the iterator following
922 * @param pos Iterator pointing before element to be erased.
924 * This function will erase the element at the given position and
925 * thus shorten the %forward_list by one.
927 * Due to the nature of a %forward_list this operation can be done
928 * in constant time, and only invalidates iterators/references to
929 * the element being removed. The user is also cautioned that
930 * this function only erases the element, and that if the element
931 * is itself a pointer, the pointed-to memory is not touched in
932 * any way. Managing the pointer is the user's responsibility.
935 erase_after(const_iterator __pos)
937 _Node_base* __tmp = __const_pointer_cast<_Node_base*>(__pos._M_node);
938 this->_M_erase_after(__tmp);
942 * @brief Remove a range of elements.
943 * @param pos Iterator pointing before the first element to be
945 * @param last Iterator pointing to one past the last element to be
948 * This function will erase the elements in the range @a
949 * (pos,last) and shorten the %forward_list accordingly.
951 * This operation is linear time in the size of the range and only
952 * invalidates iterators/references to the element being removed.
953 * The user is also cautioned that this function only erases the
954 * elements, and that if the elements themselves are pointers, the
955 * pointed-to memory is not touched in any way. Managing the pointer
956 * is the user's responsibility.
959 erase_after(const_iterator __pos, const_iterator __last)
961 _Node_base* __tmpp = __const_pointer_cast<_Node_base*>(__pos._M_node);
962 _Node_base* __tmpl = __const_pointer_cast<_Node_base*>(__last._M_node);
963 this->_M_erase_after(__tmpp, __tmpl);
967 * @brief Swaps data with another %forward_list.
968 * @param list A %forward_list of the same element and allocator
971 * This exchanges the elements between two lists in constant
972 * time. Note that the global std::swap() function is
973 * specialized such that std::swap(l1,l2) will feed to this
977 swap(forward_list& __list)
978 { _Node_base::swap(this->_M_impl._M_head, __list._M_impl._M_head); }
981 * @brief Resizes the %forward_list to the specified number of
983 * @param sz Number of elements the %forward_list should contain.
985 * This function will %resize the %forward_list to the specified
986 * number of elements. If the number is smaller than the
987 * %forward_list's current size the %forward_list is truncated,
988 * otherwise the %forward_list is extended and new elements are
989 * populated with given data.
992 resize(size_type __sz)
993 { resize(__sz, _Tp()); }
996 * @brief Resizes the %forward_list to the specified number of
998 * @param sz Number of elements the %forward_list should contain.
999 * @param val Data with which new elements should be populated.
1001 * This function will %resize the %forward_list to the specified
1002 * number of elements. If the number is smaller than the
1003 * %forward_list's current size the %forward_list is truncated,
1004 * otherwise the %forward_list is extended and new elements are
1005 * populated with given data.
1008 resize(size_type __sz, value_type __val);
1011 * @brief Erases all the elements.
1013 * Note that this function only erases
1014 * the elements, and that if the elements themselves are
1015 * pointers, the pointed-to memory is not touched in any way.
1016 * Managing the pointer is the user's responsibility.
1020 { this->_M_erase_after(&this->_M_impl._M_head, 0); }
1022 // 23.2.3.5 forward_list operations:
1025 * @brief Insert contents of another %forward_list.
1026 * @param pos Iterator referencing the element to insert after.
1027 * @param list Source list.
1029 * The elements of @a list are inserted in constant time after
1030 * the element referenced by @a pos. @a list becomes an empty
1033 * Requires this != @a x.
1036 splice_after(const_iterator __pos, forward_list&& __list);
1039 * @brief Insert element from another %forward_list.
1040 * @param pos Iterator referencing the element to insert after.
1041 * @param list Source list.
1042 * @param i Iterator referencing the element before the element
1045 * Removes the element in list @a list referenced by @a i and
1046 * inserts it into the current list after @a pos.
1049 splice_after(const_iterator __pos, forward_list&& __list,
1052 const_iterator __j = __i;
1054 if (__pos == __i || __pos == __j)
1057 splice_after(__pos, std::move(__list), __i, __j);
1061 * @brief Insert range from another %forward_list.
1062 * @param pos Iterator referencing the element to insert after.
1063 * @param list Source list.
1064 * @param before Iterator referencing before the start of range
1066 * @param last Iterator referencing the end of range in list.
1068 * Removes elements in the range (before,last) and inserts them
1069 * after @a pos in constant time.
1071 * Undefined if @a pos is in (before,last).
1074 splice_after(const_iterator __pos, forward_list&& __list,
1075 const_iterator __before, const_iterator __last);
1078 * @brief Remove all elements equal to value.
1079 * @param val The value to remove.
1081 * Removes every element in the list equal to @a value.
1082 * Remaining elements stay in list order. Note that this
1083 * function only erases the elements, and that if the elements
1084 * themselves are pointers, the pointed-to memory is not
1085 * touched in any way. Managing the pointer is the user's
1089 remove(const _Tp& __val);
1092 * @brief Remove all elements satisfying a predicate.
1093 * @param pred Unary predicate function or object.
1095 * Removes every element in the list for which the predicate
1096 * returns true. Remaining elements stay in list order. Note
1097 * that this function only erases the elements, and that if the
1098 * elements themselves are pointers, the pointed-to memory is
1099 * not touched in any way. Managing the pointer is the user's
1102 template<typename _Pred>
1104 remove_if(_Pred __pred);
1107 * @brief Remove consecutive duplicate elements.
1109 * For each consecutive set of elements with the same value,
1110 * remove all but the first one. Remaining elements stay in
1111 * list order. Note that this function only erases the
1112 * elements, and that if the elements themselves are pointers,
1113 * the pointed-to memory is not touched in any way. Managing
1114 * the pointer is the user's responsibility.
1118 { this->unique(std::equal_to<_Tp>()); }
1121 * @brief Remove consecutive elements satisfying a predicate.
1122 * @param binary_pred Binary predicate function or object.
1124 * For each consecutive set of elements [first,last) that
1125 * satisfy predicate(first,i) where i is an iterator in
1126 * [first,last), remove all but the first one. Remaining
1127 * elements stay in list order. Note that this function only
1128 * erases the elements, and that if the elements themselves are
1129 * pointers, the pointed-to memory is not touched in any way.
1130 * Managing the pointer is the user's responsibility.
1132 template<typename _BinPred>
1134 unique(_BinPred __binary_pred);
1137 * @brief Merge sorted lists.
1138 * @param list Sorted list to merge.
1140 * Assumes that both @a list and this list are sorted according to
1141 * operator<(). Merges elements of @a list into this list in
1142 * sorted order, leaving @a list empty when complete. Elements in
1143 * this list precede elements in @a list that are equal.
1146 merge(forward_list&& __list)
1147 { this->merge(std::move(__list), std::less<_Tp>()); }
1150 * @brief Merge sorted lists according to comparison function.
1151 * @param list Sorted list to merge.
1152 * @param comp Comparison function defining sort order.
1154 * Assumes that both @a list and this list are sorted according to
1155 * comp. Merges elements of @a list into this list
1156 * in sorted order, leaving @a list empty when complete. Elements
1157 * in this list precede elements in @a list that are equivalent
1158 * according to comp().
1160 template<typename _Comp>
1162 merge(forward_list&& __list, _Comp __comp);
1165 * @brief Sort the elements of the list.
1167 * Sorts the elements of this list in NlogN time. Equivalent
1168 * elements remain in list order.
1172 { this->sort(std::less<_Tp>()); }
1175 * @brief Sort the forward_list using a comparison function.
1177 * Sorts the elements of this list in NlogN time. Equivalent
1178 * elements remain in list order.
1180 template<typename _Comp>
1185 * @brief Reverse the elements in list.
1187 * Reverse the order of elements in the list in linear time.
1191 { this->_M_impl._M_head._M_reverse_after(); }
1194 template<typename _Integer>
1196 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1197 { _M_fill_initialize(static_cast<size_type>(__n), __x); }
1199 // Called by the range constructor to implement [23.1.1]/9
1200 template<typename _InputIterator>
1202 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1205 // Called by forward_list(n,v,a), and the range constructor when it
1206 // turns out to be the same thing.
1208 _M_fill_initialize(size_type __n, const value_type& __value);
1212 * @brief Forward list equality comparison.
1213 * @param lx A %forward_list
1214 * @param ly A %forward_list of the same type as @a lx.
1215 * @return True iff the size and elements of the forward lists are equal.
1217 * This is an equivalence relation. It is linear in the size of the
1218 * forward lists. Deques are considered equivalent if corresponding
1219 * elements compare equal.
1221 template<typename _Tp, typename _Alloc>
1223 operator==(const forward_list<_Tp, _Alloc>& __lx,
1224 const forward_list<_Tp, _Alloc>& __ly);
1227 * @brief Forward list ordering relation.
1228 * @param lx A %forward_list.
1229 * @param ly A %forward_list of the same type as @a lx.
1230 * @return True iff @a lx is lexicographically less than @a ly.
1232 * This is a total ordering relation. It is linear in the size of the
1233 * forward lists. The elements must be comparable with @c <.
1235 * See std::lexicographical_compare() for how the determination is made.
1237 template<typename _Tp, typename _Alloc>
1239 operator<(const forward_list<_Tp, _Alloc>& __lx,
1240 const forward_list<_Tp, _Alloc>& __ly)
1241 { return std::lexicographical_compare(__lx.cbegin(), __lx.cend(),
1242 __ly.cbegin(), __ly.cend()); }
1244 /// Based on operator==
1245 template<typename _Tp, typename _Alloc>
1247 operator!=(const forward_list<_Tp, _Alloc>& __lx,
1248 const forward_list<_Tp, _Alloc>& __ly)
1249 { return !(__lx == __ly); }
1251 /// Based on operator<
1252 template<typename _Tp, typename _Alloc>
1254 operator>(const forward_list<_Tp, _Alloc>& __lx,
1255 const forward_list<_Tp, _Alloc>& __ly)
1256 { return (__ly < __lx); }
1258 /// Based on operator<
1259 template<typename _Tp, typename _Alloc>
1261 operator>=(const forward_list<_Tp, _Alloc>& __lx,
1262 const forward_list<_Tp, _Alloc>& __ly)
1263 { return !(__lx < __ly); }
1265 /// Based on operator<
1266 template<typename _Tp, typename _Alloc>
1268 operator<=(const forward_list<_Tp, _Alloc>& __lx,
1269 const forward_list<_Tp, _Alloc>& __ly)
1270 { return !(__ly < __lx); }
1272 /// See std::forward_list::swap().
1273 template<typename _Tp, typename _Alloc>
1275 swap(forward_list<_Tp, _Alloc>& __lx,
1276 forward_list<_Tp, _Alloc>& __ly)
1277 { __lx.swap(__ly); }
1279 _GLIBCXX_END_NAMESPACE // namespace std
1281 #endif // __GXX_EXPERIMENTAL_CXX0X__
1283 #endif // _FORWARD_LIST_H