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.
403 * @ingroup containers
406 * Meets the requirements of a <a href="tables.html#65">container</a>, a
407 * <a href="tables.html#67">sequence</a>, including the
408 * <a href="tables.html#68">optional sequence requirements</a> with the
409 * %exception of @c at and @c operator[].
411 * This is a @e singly @e linked %list. Traversal up the
412 * %list requires linear time, but adding and removing elements (or
413 * @e nodes) is done in constant time, regardless of where the
414 * change takes place. Unlike std::vector and std::deque,
415 * random-access iterators are not provided, so subscripting ( @c
416 * [] ) access is not allowed. For algorithms which only need
417 * sequential access, this lack makes no difference.
419 * Also unlike the other standard containers, std::forward_list provides
420 * specialized algorithms %unique to linked lists, such as
421 * splicing, sorting, and in-place reversal.
423 * A couple points on memory allocation for forward_list<Tp>:
425 * First, we never actually allocate a Tp, we allocate
426 * Fwd_list_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
427 * that after elements from %forward_list<X,Alloc1> are spliced into
428 * %forward_list<X,Alloc2>, destroying the memory of the second %list is a
429 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
431 template<typename _Tp, typename _Alloc = allocator<_Tp> >
432 class forward_list : private _Fwd_list_base<_Tp, _Alloc>
435 typedef _Fwd_list_base<_Tp, _Alloc> _Base;
436 typedef typename _Base::_Node _Node;
437 typedef typename _Base::_Node_base _Node_base;
438 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
442 typedef _Tp value_type;
443 typedef typename _Tp_alloc_type::pointer pointer;
444 typedef typename _Tp_alloc_type::const_pointer const_pointer;
445 typedef typename _Tp_alloc_type::reference reference;
446 typedef typename _Tp_alloc_type::const_reference const_reference;
448 typedef typename _Base::iterator iterator;
449 typedef typename _Base::const_iterator const_iterator;
450 typedef std::size_t size_type;
451 typedef std::ptrdiff_t difference_type;
452 typedef _Alloc allocator_type;
454 // 23.2.3.1 construct/copy/destroy:
457 * @brief Creates a %forward_list with no elements.
458 * @param al An allocator object.
461 forward_list(const _Alloc& __al = _Alloc())
466 * @brief Copy constructor with allocator argument.
467 * @param list Input list to copy.
468 * @param al An allocator object.
470 forward_list(const forward_list& __list, const _Alloc& __al)
471 : _Base(__list, __al)
475 * @brief Move constructor with allocator argument.
476 * @param list Input list to move.
477 * @param al An allocator object.
479 forward_list(forward_list&& __list, const _Alloc& __al)
480 : _Base(std::forward<_Base>(__list), __al)
484 * @brief Creates a %forward_list with copies of the default element
486 * @param n The number of elements to initially create.
488 * This constructor fills the %forward_list with @a n copies of
492 forward_list(size_type __n)
494 { _M_fill_initialize(__n, value_type()); }
497 * @brief Creates a %forward_list with copies of an exemplar element.
498 * @param n The number of elements to initially create.
499 * @param value An element to copy.
500 * @param al An allocator object.
502 * This constructor fills the %forward_list with @a n copies of @a
505 forward_list(size_type __n, const _Tp& __value,
506 const _Alloc& __al = _Alloc())
508 { _M_fill_initialize(__n, __value); }
511 * @brief Builds a %forward_list from a range.
512 * @param first An input iterator.
513 * @param last An input iterator.
514 * @param al An allocator object.
516 * Create a %forward_list consisting of copies of the elements from
517 * [@a first,@a last). This is linear in N (where N is
518 * distance(@a first,@a last)).
520 template<typename _InputIterator>
521 forward_list(_InputIterator __first, _InputIterator __last,
522 const _Alloc& __al = _Alloc())
525 // Check whether it's an integral type. If so, it's not an iterator.
526 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
527 _M_initialize_dispatch(__first, __last, _Integral());
531 * @brief The %forward_list copy constructor.
532 * @param list A %forward_list of identical element and allocator
535 * The newly-created %forward_list uses a copy of the allocation
536 * object used by @a list.
538 forward_list(const forward_list& __list)
539 : _Base(__list.get_allocator())
540 { _M_initialize_dispatch(__list.begin(), __list.end(), __false_type()); }
543 * @brief The %forward_list move constructor.
544 * @param list A %forward_list of identical element and allocator
547 * The newly-created %forward_list contains the exact contents of @a
548 * forward_list. The contents of @a list are a valid, but unspecified
551 forward_list(forward_list&& __list)
552 : _Base(std::forward<_Base>(__list)) { }
555 * @brief Builds a %forward_list from an initializer_list
556 * @param il An initializer_list of value_type.
557 * @param al An allocator object.
559 * Create a %forward_list consisting of copies of the elements
560 * in the initializer_list @a il. This is linear in il.size().
562 forward_list(std::initializer_list<_Tp> __il,
563 const _Alloc& __al = _Alloc())
565 { _M_initialize_dispatch(__il.begin(), __il.end(), __false_type()); }
568 * @brief The forward_list dtor.
571 { _M_erase_after(&this->_M_impl._M_head, 0); }
574 * @brief The %forward_list assignment operator.
575 * @param list A %forward_list of identical element and allocator
578 * All the elements of @a list are copied, but unlike the copy
579 * constructor, the allocator object is not copied.
582 operator=(const forward_list& __list);
585 * @brief The %forward_list move assignment operator.
586 * @param list A %forward_list of identical element and allocator
589 * The contents of @a list are moved into this %forward_list
590 * (without copying). @a list is a valid, but unspecified
594 operator=(forward_list&& __list)
605 * @brief The %forward_list initializer list assignment operator.
606 * @param il An initializer_list of value_type.
608 * Replace the contents of the %forward_list with copies of the
609 * elements in the initializer_list @a il. This is linear in
613 operator=(std::initializer_list<_Tp> __il)
620 * @brief Assigns a range to a %forward_list.
621 * @param first An input iterator.
622 * @param last An input iterator.
624 * This function fills a %forward_list with copies of the elements
625 * in the range [@a first,@a last).
627 * Note that the assignment completely changes the %forward_list and
628 * that the resulting %forward_list's size is the same as the number
629 * of elements assigned. Old data may be lost.
631 template<typename _InputIterator>
633 assign(_InputIterator __first, _InputIterator __last)
636 insert_after(cbefore_begin(), __first, __last);
640 * @brief Assigns a given value to a %forward_list.
641 * @param n Number of elements to be assigned.
642 * @param val Value to be assigned.
644 * This function fills a %forward_list with @a n copies of the given
645 * value. Note that the assignment completely changes the
646 * %forward_list and that the resulting %forward_list's size is the
647 * same as the number of elements assigned. Old data may be lost.
650 assign(size_type __n, const _Tp& __val)
653 insert_after(cbefore_begin(), __n, __val);
657 * @brief Assigns an initializer_list to a %forward_list.
658 * @param il An initializer_list of value_type.
660 * Replace the contents of the %forward_list with copies of the
661 * elements in the initializer_list @a il. This is linear in
665 assign(std::initializer_list<_Tp> __il)
668 insert_after(cbefore_begin(), __il);
671 /// Get a copy of the memory allocation object.
673 get_allocator() const
674 { return this->_M_get_Node_allocator(); }
676 // 23.2.3.2 iterators:
679 * Returns a read/write iterator that points before the first element
680 * in the %forward_list. Iteration is done in ordinary element order.
684 { return iterator(&this->_M_impl._M_head); }
687 * Returns a read-only (constant) iterator that points before the
688 * first element in the %forward_list. Iteration is done in ordinary
693 { return const_iterator(&this->_M_impl._M_head); }
696 * Returns a read/write iterator that points to the first element
697 * in the %forward_list. Iteration is done in ordinary element order.
701 { return iterator(this->_M_impl._M_head._M_next); }
704 * Returns a read-only (constant) iterator that points to the first
705 * element in the %forward_list. Iteration is done in ordinary
710 { return const_iterator(this->_M_impl._M_head._M_next); }
713 * Returns a read/write iterator that points one past the last
714 * element in the %forward_list. Iteration is done in ordinary
719 { return iterator(0); }
722 * Returns a read-only iterator that points one past the last
723 * element in the %forward_list. Iteration is done in ordinary
728 { return const_iterator(0); }
731 * Returns a read-only (constant) iterator that points to the
732 * first element in the %forward_list. Iteration is done in ordinary
737 { return const_iterator(this->_M_impl._M_head._M_next); }
740 * Returns a read-only (constant) iterator that points before the
741 * first element in the %forward_list. Iteration is done in ordinary
745 cbefore_begin() const
746 { return const_iterator(&this->_M_impl._M_head); }
749 * Returns a read-only (constant) iterator that points one past
750 * the last element in the %forward_list. Iteration is done in
751 * ordinary element order.
755 { return const_iterator(0); }
758 * Returns true if the %forward_list is empty. (Thus begin() would
763 { return this->_M_impl._M_head._M_next == 0; }
766 * Returns the largest possible size of %forward_list.
770 { return this->_M_get_Node_allocator().max_size(); }
772 // 23.2.3.3 element access:
775 * Returns a read/write reference to the data at the first
776 * element of the %forward_list.
781 _Node* __front = __static_pointer_cast<_Node*>(this->_M_impl._M_head._M_next);
782 return __front->_M_value;
786 * Returns a read-only (constant) reference to the data at the first
787 * element of the %forward_list.
793 __static_pointer_cast<_Node*>(this->_M_impl._M_head._M_next);
794 return __front->_M_value;
797 // 23.2.3.4 modifiers:
800 * @brief Constructs object in %forward_list at the front of the
802 * @param args Arguments.
804 * This function will insert an object of type Tp constructed
805 * with Tp(std::forward<Args>(args)...) at the front of the list
806 * Due to the nature of a %forward_list this operation can
807 * be done in constant time, and does not invalidate iterators
810 template<typename... _Args>
812 emplace_front(_Args&&... __args)
813 { this->_M_insert_after(cbefore_begin(),
814 std::forward<_Args>(__args)...); }
817 * @brief Add data to the front of the %forward_list.
818 * @param val Data to be added.
820 * This is a typical stack operation. The function creates an
821 * element at the front of the %forward_list and assigns the given
822 * data to it. Due to the nature of a %forward_list this operation
823 * can be done in constant time, and does not invalidate iterators
827 push_front(const _Tp& __val)
828 { this->_M_insert_after(cbefore_begin(), __val); }
834 push_front(_Tp&& __val)
835 { this->_M_insert_after(cbefore_begin(), std::move(__val)); }
838 * @brief Removes first element.
840 * This is a typical stack operation. It shrinks the %forward_list
841 * by one. Due to the nature of a %forward_list this operation can
842 * be done in constant time, and only invalidates iterators/references
843 * to the element being removed.
845 * Note that no data is returned, and if the first element's data
846 * is needed, it should be retrieved before pop_front() is
851 { this->_M_erase_after(&this->_M_impl._M_head); }
854 * @brief Constructs object in %forward_list after the specified
856 * @param pos A const_iterator into the %forward_list.
857 * @param args Arguments.
858 * @return An iterator that points to the inserted data.
860 * This function will insert an object of type T constructed
861 * with T(std::forward<Args>(args)...) after the specified
862 * location. Due to the nature of a %forward_list this operation can
863 * be done in constant time, and does not invalidate iterators
866 template<typename... _Args>
868 emplace_after(const_iterator __pos, _Args&&... __args)
869 { return iterator(this->_M_insert_after(__pos,
870 std::forward<_Args>(__args)...)); }
873 * @brief Inserts given value into %forward_list after specified
875 * @param pos An iterator into the %forward_list.
876 * @param val Data to be inserted.
877 * @return An iterator that points to the inserted data.
879 * This function will insert a copy of the given value after
880 * the specified location. Due to the nature of a %forward_list this
881 * operation can be done in constant time, and does not
882 * invalidate iterators and references.
885 insert_after(const_iterator __pos, const _Tp& __val)
886 { return iterator(this->_M_insert_after(__pos, __val)); }
892 insert_after(const_iterator __pos, _Tp&& __val)
893 { return iterator(this->_M_insert_after(__pos, std::move(__val))); }
896 * @brief Inserts a number of copies of given data into the
898 * @param pos An iterator into the %forward_list.
899 * @param n Number of elements to be inserted.
900 * @param val Data to be inserted.
902 * This function will insert a specified number of copies of the
903 * given data after the location specified by @a pos.
905 * This operation is linear in the number of elements inserted and
906 * does not invalidate iterators and references.
909 insert_after(const_iterator __pos, size_type __n, const _Tp& __val)
911 forward_list __tmp(__n, __val, this->get_allocator());
912 this->splice_after(__pos, std::move(__tmp));
916 * @brief Inserts a range into the %forward_list.
917 * @param position An iterator into the %forward_list.
918 * @param first An input iterator.
919 * @param last An input iterator.
921 * This function will insert copies of the data in the range [@a
922 * first,@a last) into the %forward_list after the location specified
925 * This operation is linear in the number of elements inserted and
926 * does not invalidate iterators and references.
928 template<typename _InputIterator>
930 insert_after(const_iterator __pos,
931 _InputIterator __first, _InputIterator __last)
933 forward_list __tmp(__first, __last, this->get_allocator());
934 this->splice_after(__pos, std::move(__tmp));
938 * @brief Inserts the contents of an initializer_list into
939 * %forward_list after the specified iterator.
940 * @param pos An iterator into the %forward_list.
941 * @param il An initializer_list of value_type.
943 * This function will insert copies of the data in the
944 * initializer_list @a il into the %forward_list before the location
945 * specified by @a pos.
947 * This operation is linear in the number of elements inserted and
948 * does not invalidate iterators and references.
951 insert_after(const_iterator __pos, std::initializer_list<_Tp> __il)
953 forward_list __tmp(__il, this->get_allocator());
954 this->splice_after(__pos, std::move(__tmp));
958 * @brief Removes the element pointed to by the iterator following
960 * @param pos Iterator pointing to element to be erased.
961 * @return An iterator pointing to the next element (or end()).
963 * This function will erase the element at the given position and
964 * thus shorten the %forward_list by one.
966 * Due to the nature of a %forward_list this operation can be done
967 * in constant time, and only invalidates iterators/references to
968 * the element being removed. The user is also cautioned that
969 * this function only erases the element, and that if the element
970 * is itself a pointer, the pointed-to memory is not touched in
971 * any way. Managing the pointer is the user's responsibility.
974 erase_after(const_iterator __pos)
976 _Node_base* __tmp = __const_pointer_cast<_Node_base*>(__pos._M_node);
978 return iterator(this->_M_erase_after(__tmp));
984 * @brief Remove a range of elements.
985 * @param pos Iterator pointing before the first element to be
987 * @param last Iterator pointing to one past the last element to be
989 * @return An iterator pointing to the element pointed to by @a last
990 * prior to erasing (or end()).
992 * This function will erase the elements in the range @a
993 * (pos,last) and shorten the %forward_list accordingly.
995 * This operation is linear time in the size of the range and only
996 * invalidates iterators/references to the element being removed.
997 * The user is also cautioned that this function only erases the
998 * elements, and that if the elements themselves are pointers, the
999 * pointed-to memory is not touched in any way. Managing the pointer
1000 * is the user's responsibility.
1003 erase_after(const_iterator __pos, iterator __last)
1005 _Node_base* __tmp = __const_pointer_cast<_Node_base*>(__pos._M_node);
1006 return iterator(this->_M_erase_after(__tmp, &*__last._M_node));
1010 * @brief Swaps data with another %forward_list.
1011 * @param list A %forward_list of the same element and allocator
1014 * This exchanges the elements between two lists in constant
1015 * time. Note that the global std::swap() function is
1016 * specialized such that std::swap(l1,l2) will feed to this
1020 swap(forward_list&& __list)
1021 { _Node_base::swap(this->_M_impl._M_head, __list._M_impl._M_head); }
1024 * @brief Resizes the %forward_list to the specified number of
1026 * @param sz Number of elements the %forward_list should contain.
1028 * This function will %resize the %forward_list to the specified
1029 * number of elements. If the number is smaller than the
1030 * %forward_list's current size the %forward_list is truncated,
1031 * otherwise the %forward_list is extended and new elements are
1032 * populated with given data.
1035 resize(size_type __sz)
1036 { resize(__sz, _Tp()); }
1039 * @brief Resizes the %forward_list to the specified number of
1041 * @param sz Number of elements the %forward_list should contain.
1042 * @param val Data with which new elements should be populated.
1044 * This function will %resize the %forward_list to the specified
1045 * number of elements. If the number is smaller than the
1046 * %forward_list's current size the %forward_list is truncated,
1047 * otherwise the %forward_list is extended and new elements are
1048 * populated with given data.
1051 resize(size_type __sz, value_type __val);
1054 * @brief Erases all the elements.
1056 * Note that this function only erases
1057 * the elements, and that if the elements themselves are
1058 * pointers, the pointed-to memory is not touched in any way.
1059 * Managing the pointer is the user's responsibility.
1063 { this->_M_erase_after(&this->_M_impl._M_head, 0); }
1065 // 23.2.3.5 forward_list operations:
1068 * @brief Insert contents of another %forward_list.
1069 * @param pos Iterator referencing the element to insert after.
1070 * @param list Source list.
1072 * The elements of @a list are inserted in constant time after
1073 * the element referenced by @a pos. @a list becomes an empty
1076 * Requires this != @a x.
1079 splice_after(const_iterator __pos, forward_list&& __list);
1082 * @brief Insert element from another %forward_list.
1083 * @param pos Iterator referencing the element to insert after.
1084 * @param list Source list.
1085 * @param it Iterator referencing the element before the element
1088 * Removes the element in list @a list referenced by @a i and
1089 * inserts it into the current list after @a pos.
1092 splice_after(const_iterator __pos, forward_list&& __list,
1093 const_iterator __it)
1094 { this->splice_after(__pos, __list, __it, __it._M_next()); }
1097 * @brief Insert range from another %forward_list.
1098 * @param pos Iterator referencing the element to insert after.
1099 * @param list Source list.
1100 * @param before Iterator referencing before the start of range
1102 * @param last Iterator referencing the end of range in list.
1104 * Removes elements in the range (before,last) and inserts them
1105 * after @a pos in constant time.
1107 * Undefined if @a pos is in (before,last).
1110 splice_after(const_iterator __pos, forward_list&& __list,
1111 const_iterator __before, const_iterator __last);
1114 * @brief Remove all elements equal to value.
1115 * @param val The value to remove.
1117 * Removes every element in the list equal to @a value.
1118 * Remaining elements stay in list order. Note that this
1119 * function only erases the elements, and that if the elements
1120 * themselves are pointers, the pointed-to memory is not
1121 * touched in any way. Managing the pointer is the user's
1125 remove(const _Tp& __val);
1128 * @brief Remove all elements satisfying a predicate.
1129 * @param pred Unary predicate function or object.
1131 * Removes every element in the list for which the predicate
1132 * returns true. Remaining elements stay in list order. Note
1133 * that this function only erases the elements, and that if the
1134 * elements themselves are pointers, the pointed-to memory is
1135 * not touched in any way. Managing the pointer is the user's
1138 template<typename _Pred>
1140 remove_if(_Pred __pred);
1143 * @brief Remove consecutive duplicate elements.
1145 * For each consecutive set of elements with the same value,
1146 * remove all but the first one. Remaining elements stay in
1147 * list order. Note that this function only erases the
1148 * elements, and that if the elements themselves are pointers,
1149 * the pointed-to memory is not touched in any way. Managing
1150 * the pointer is the user's responsibility.
1154 { this->unique(std::equal_to<_Tp>()); }
1157 * @brief Remove consecutive elements satisfying a predicate.
1158 * @param binary_pred Binary predicate function or object.
1160 * For each consecutive set of elements [first,last) that
1161 * satisfy predicate(first,i) where i is an iterator in
1162 * [first,last), remove all but the first one. Remaining
1163 * elements stay in list order. Note that this function only
1164 * erases the elements, and that if the elements themselves are
1165 * pointers, the pointed-to memory is not touched in any way.
1166 * Managing the pointer is the user's responsibility.
1168 template<typename _BinPred>
1170 unique(_BinPred __binary_pred);
1173 * @brief Merge sorted lists.
1174 * @param list Sorted list to merge.
1176 * Assumes that both @a list and this list are sorted according to
1177 * operator<(). Merges elements of @a list into this list in
1178 * sorted order, leaving @a list empty when complete. Elements in
1179 * this list precede elements in @a list that are equal.
1182 merge(forward_list&& __list)
1183 { this->merge(__list, std::less<_Tp>()); }
1186 * @brief Merge sorted lists according to comparison function.
1187 * @param list Sorted list to merge.
1188 * @param comp Comparison function defining sort order.
1190 * Assumes that both @a list and this list are sorted according to
1191 * comp. Merges elements of @a list into this list
1192 * in sorted order, leaving @a list empty when complete. Elements
1193 * in this list precede elements in @a list that are equivalent
1194 * according to comp().
1196 template<typename _Comp>
1198 merge(forward_list&& __list, _Comp __comp);
1201 * @brief Sort the elements of the list.
1203 * Sorts the elements of this list in NlogN time. Equivalent
1204 * elements remain in list order.
1209 _Node* __tmp = __static_pointer_cast<_Node*>(&this->_M_impl._M_head);
1210 __tmp->_M_sort_after(std::less<_Tp>());
1214 * @brief Sort the forward_list using a comparison function.
1216 * Sorts the elements of this list in NlogN time. Equivalent
1217 * elements remain in list order.
1219 template<typename _Comp>
1223 _Node* __tmp = __static_pointer_cast<_Node*>(&this->_M_impl._M_head);
1224 __tmp->_M_sort_after(__comp);
1228 * @brief Reverse the elements in list.
1230 * Reverse the order of elements in the list in linear time.
1236 template<typename _Integer>
1238 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1239 { _M_fill_initialize(static_cast<size_type>(__n), __x); }
1241 // Called by the range constructor to implement [23.1.1]/9
1242 template<typename _InputIterator>
1244 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1247 // Called by forward_list(n,v,a), and the range constructor when it
1248 // turns out to be the same thing.
1250 _M_fill_initialize(size_type __n, const value_type& __value);
1254 * @brief Forward list equality comparison.
1255 * @param lx A %forward_list
1256 * @param ly A %forward_list of the same type as @a lx.
1257 * @return True iff the size and elements of the forward lists are equal.
1259 * This is an equivalence relation. It is linear in the size of the
1260 * forward lists. Deques are considered equivalent if corresponding
1261 * elements compare equal.
1263 template<typename _Tp, typename _Alloc>
1265 operator==(const forward_list<_Tp, _Alloc>& __lx,
1266 const forward_list<_Tp, _Alloc>& __ly);
1269 * @brief Forward list ordering relation.
1270 * @param lx A %forward_list.
1271 * @param ly A %forward_list of the same type as @a lx.
1272 * @return True iff @a lx is lexicographically less than @a ly.
1274 * This is a total ordering relation. It is linear in the size of the
1275 * forward lists. The elements must be comparable with @c <.
1277 * See std::lexicographical_compare() for how the determination is made.
1279 template<typename _Tp, typename _Alloc>
1281 operator<(const forward_list<_Tp, _Alloc>& __lx,
1282 const forward_list<_Tp, _Alloc>& __ly)
1283 { return std::lexicographical_compare(__lx.cbegin(), __lx.cend(),
1284 __ly.cbegin(), __ly.cend()); }
1286 /// Based on operator==
1287 template<typename _Tp, typename _Alloc>
1289 operator!=(const forward_list<_Tp, _Alloc>& __lx,
1290 const forward_list<_Tp, _Alloc>& __ly)
1291 { return !(__lx == __ly); }
1293 /// Based on operator<
1294 template<typename _Tp, typename _Alloc>
1296 operator>(const forward_list<_Tp, _Alloc>& __lx,
1297 const forward_list<_Tp, _Alloc>& __ly)
1298 { return (__ly < __lx); }
1300 /// Based on operator<
1301 template<typename _Tp, typename _Alloc>
1303 operator>=(const forward_list<_Tp, _Alloc>& __lx,
1304 const forward_list<_Tp, _Alloc>& __ly)
1305 { return !(__lx < __ly); }
1307 /// Based on operator<
1308 template<typename _Tp, typename _Alloc>
1310 operator<=(const forward_list<_Tp, _Alloc>& __lx,
1311 const forward_list<_Tp, _Alloc>& __ly)
1312 { return !(__ly < __lx); }
1314 /// See std::forward_list::swap().
1315 template<typename _Tp, typename _Alloc>
1317 swap(forward_list<_Tp, _Alloc>& __lx,
1318 forward_list<_Tp, _Alloc>& __ly)
1319 { __lx.swap(__ly); }
1321 /// See std::forward_list::swap().
1322 template<typename _Tp, typename _Alloc>
1324 swap(forward_list<_Tp, _Alloc>&& __lx,
1325 forward_list<_Tp, _Alloc>& __ly)
1326 { __lx.swap(__ly); }
1328 /// See std::forward_list::swap().
1329 template<typename _Tp, typename _Alloc>
1331 swap(forward_list<_Tp, _Alloc>& __lx,
1332 forward_list<_Tp, _Alloc>&& __ly)
1333 { __lx.swap(__ly); }
1335 _GLIBCXX_END_NAMESPACE // namespace std
1337 #endif // __GXX_EXPERIMENTAL_CXX0X__
1339 #endif // _FORWARD_LIST_H