1 // <forward_list.h> -*- C++ -*-
3 // Copyright (C) 2008, 2009, 2010 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
35 #include <initializer_list>
37 _GLIBCXX_BEGIN_NAMESPACE(std)
40 * @brief A helper basic node class for %forward_list.
41 * This is just a linked list with nothing inside it.
42 * There are purely list shuffling utility methods here.
44 struct _Fwd_list_node_base
46 _Fwd_list_node_base() : _M_next(0) { }
48 _Fwd_list_node_base* _M_next;
51 swap(_Fwd_list_node_base& __x, _Fwd_list_node_base& __y)
52 { std::swap(__x._M_next, __y._M_next); }
55 _M_transfer_after(_Fwd_list_node_base* __begin)
57 _Fwd_list_node_base* __end = __begin;
58 while (__end && __end->_M_next)
59 __end = __end->_M_next;
60 return _M_transfer_after(__begin, __end);
64 _M_transfer_after(_Fwd_list_node_base* __begin,
65 _Fwd_list_node_base* __end)
67 _Fwd_list_node_base* __keep = __begin->_M_next;
70 __begin->_M_next = __end->_M_next;
71 __end->_M_next = _M_next;
82 _Fwd_list_node_base* __tail = _M_next;
85 while (_Fwd_list_node_base* __temp = __tail->_M_next)
87 _Fwd_list_node_base* __keep = _M_next;
89 __tail->_M_next = __temp->_M_next;
90 _M_next->_M_next = __keep;
96 * @brief A helper node class for %forward_list.
97 * This is just a linked list with a data value in each node.
98 * There is a sorting utility method.
100 template<typename _Tp>
101 struct _Fwd_list_node
102 : public _Fwd_list_node_base
104 template<typename... _Args>
105 _Fwd_list_node(_Args&&... __args)
106 : _Fwd_list_node_base(),
107 _M_value(std::forward<_Args>(__args)...) { }
113 * @brief A forward_list::iterator.
115 * All the functions are op overloads.
117 template<typename _Tp>
118 struct _Fwd_list_iterator
120 typedef _Fwd_list_iterator<_Tp> _Self;
121 typedef _Fwd_list_node<_Tp> _Node;
123 typedef _Tp value_type;
124 typedef _Tp* pointer;
125 typedef _Tp& reference;
126 typedef ptrdiff_t difference_type;
127 typedef std::forward_iterator_tag iterator_category;
133 _Fwd_list_iterator(_Fwd_list_node_base* __n)
138 { return static_cast<_Node*>(this->_M_node)->_M_value; }
142 { return &static_cast<_Node*>(this->_M_node)->_M_value; }
147 _M_node = _M_node->_M_next;
155 _M_node = _M_node->_M_next;
160 operator==(const _Self& __x) const
161 { return _M_node == __x._M_node; }
164 operator!=(const _Self& __x) const
165 { return _M_node != __x._M_node; }
171 return _Fwd_list_iterator(_M_node->_M_next);
173 return _Fwd_list_iterator(0);
176 _Fwd_list_node_base* _M_node;
180 * @brief A forward_list::const_iterator.
182 * All the functions are op overloads.
184 template<typename _Tp>
185 struct _Fwd_list_const_iterator
187 typedef _Fwd_list_const_iterator<_Tp> _Self;
188 typedef const _Fwd_list_node<_Tp> _Node;
189 typedef _Fwd_list_iterator<_Tp> iterator;
191 typedef _Tp value_type;
192 typedef const _Tp* pointer;
193 typedef const _Tp& reference;
194 typedef ptrdiff_t difference_type;
195 typedef std::forward_iterator_tag iterator_category;
197 _Fwd_list_const_iterator()
201 _Fwd_list_const_iterator(const _Fwd_list_node_base* __n)
204 _Fwd_list_const_iterator(const iterator& __iter)
205 : _M_node(__iter._M_node) { }
209 { return static_cast<_Node*>(this->_M_node)->_M_value; }
213 { return &static_cast<_Node*>(this->_M_node)->_M_value; }
218 _M_node = _M_node->_M_next;
226 _M_node = _M_node->_M_next;
231 operator==(const _Self& __x) const
232 { return _M_node == __x._M_node; }
235 operator!=(const _Self& __x) const
236 { return _M_node != __x._M_node; }
242 return _Fwd_list_const_iterator(_M_node->_M_next);
244 return _Fwd_list_const_iterator(0);
247 const _Fwd_list_node_base* _M_node;
251 * @brief Forward list iterator equality comparison.
253 template<typename _Tp>
255 operator==(const _Fwd_list_iterator<_Tp>& __x,
256 const _Fwd_list_const_iterator<_Tp>& __y)
257 { return __x._M_node == __y._M_node; }
260 * @brief Forward list iterator inequality comparison.
262 template<typename _Tp>
264 operator!=(const _Fwd_list_iterator<_Tp>& __x,
265 const _Fwd_list_const_iterator<_Tp>& __y)
266 { return __x._M_node != __y._M_node; }
269 * @brief Base class for %forward_list.
271 template<typename _Tp, typename _Alloc>
272 struct _Fwd_list_base
275 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
277 typedef typename _Alloc::template
278 rebind<_Fwd_list_node<_Tp>>::other _Node_alloc_type;
280 struct _Fwd_list_impl
281 : public _Node_alloc_type
283 _Fwd_list_node_base _M_head;
286 : _Node_alloc_type(), _M_head()
289 _Fwd_list_impl(const _Node_alloc_type& __a)
290 : _Node_alloc_type(__a), _M_head()
294 _Fwd_list_impl _M_impl;
297 typedef _Fwd_list_iterator<_Tp> iterator;
298 typedef _Fwd_list_const_iterator<_Tp> const_iterator;
299 typedef _Fwd_list_node<_Tp> _Node;
302 _M_get_Node_allocator()
303 { return *static_cast<_Node_alloc_type*>(&this->_M_impl); }
305 const _Node_alloc_type&
306 _M_get_Node_allocator() const
307 { return *static_cast<const _Node_alloc_type*>(&this->_M_impl); }
311 { this->_M_impl._M_head._M_next = 0; }
313 _Fwd_list_base(const _Alloc& __a)
315 { this->_M_impl._M_head._M_next = 0; }
317 _Fwd_list_base(const _Fwd_list_base& __lst, const _Alloc& __a);
319 _Fwd_list_base(_Fwd_list_base&& __lst, const _Alloc& __a)
321 { _Fwd_list_node_base::swap(this->_M_impl._M_head,
322 __lst._M_impl._M_head); }
324 _Fwd_list_base(_Fwd_list_base&& __lst)
325 : _M_impl(__lst._M_get_Node_allocator())
326 { _Fwd_list_node_base::swap(this->_M_impl._M_head,
327 __lst._M_impl._M_head); }
330 { _M_erase_after(&_M_impl._M_head, 0); }
336 { return _M_get_Node_allocator().allocate(1); }
338 template<typename... _Args>
340 _M_create_node(_Args&&... __args)
342 _Node* __node = this->_M_get_node();
345 _M_get_Node_allocator().construct(__node,
346 std::forward<_Args>(__args)...);
351 this->_M_put_node(__node);
352 __throw_exception_again;
357 template<typename... _Args>
359 _M_insert_after(const_iterator __pos, _Args&&... __args);
362 _M_put_node(_Node* __p)
363 { _M_get_Node_allocator().deallocate(__p, 1); }
366 _M_erase_after(_Fwd_list_node_base* __pos);
369 _M_erase_after(_Fwd_list_node_base* __pos,
370 _Fwd_list_node_base* __last);
374 * @brief A standard container with linear time access to elements,
375 * and fixed time insertion/deletion at any point in the sequence.
379 * Meets the requirements of a <a href="tables.html#65">container</a>, a
380 * <a href="tables.html#67">sequence</a>, including the
381 * <a href="tables.html#68">optional sequence requirements</a> with the
382 * %exception of @c at and @c operator[].
384 * This is a @e singly @e linked %list. Traversal up the
385 * %list requires linear time, but adding and removing elements (or
386 * @e nodes) is done in constant time, regardless of where the
387 * change takes place. Unlike std::vector and std::deque,
388 * random-access iterators are not provided, so subscripting ( @c
389 * [] ) access is not allowed. For algorithms which only need
390 * sequential access, this lack makes no difference.
392 * Also unlike the other standard containers, std::forward_list provides
393 * specialized algorithms %unique to linked lists, such as
394 * splicing, sorting, and in-place reversal.
396 * A couple points on memory allocation for forward_list<Tp>:
398 * First, we never actually allocate a Tp, we allocate
399 * Fwd_list_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
400 * that after elements from %forward_list<X,Alloc1> are spliced into
401 * %forward_list<X,Alloc2>, destroying the memory of the second %list is a
402 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
404 template<typename _Tp, typename _Alloc = allocator<_Tp> >
405 class forward_list : private _Fwd_list_base<_Tp, _Alloc>
408 typedef _Fwd_list_base<_Tp, _Alloc> _Base;
409 typedef _Fwd_list_node<_Tp> _Node;
410 typedef _Fwd_list_node_base _Node_base;
411 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
415 typedef _Tp value_type;
416 typedef typename _Tp_alloc_type::pointer pointer;
417 typedef typename _Tp_alloc_type::const_pointer const_pointer;
418 typedef typename _Tp_alloc_type::reference reference;
419 typedef typename _Tp_alloc_type::const_reference const_reference;
421 typedef _Fwd_list_iterator<_Tp> iterator;
422 typedef _Fwd_list_const_iterator<_Tp> const_iterator;
423 typedef std::size_t size_type;
424 typedef std::ptrdiff_t difference_type;
425 typedef _Alloc allocator_type;
427 // 23.2.3.1 construct/copy/destroy:
430 * @brief Creates a %forward_list with no elements.
431 * @param al An allocator object.
434 forward_list(const _Alloc& __al = _Alloc())
439 * @brief Copy constructor with allocator argument.
440 * @param list Input list to copy.
441 * @param al An allocator object.
443 forward_list(const forward_list& __list, const _Alloc& __al)
444 : _Base(__list, __al)
448 * @brief Move constructor with allocator argument.
449 * @param list Input list to move.
450 * @param al An allocator object.
452 forward_list(forward_list&& __list, const _Alloc& __al)
453 : _Base(std::forward<_Base>(__list), __al)
457 * @brief Creates a %forward_list with default constructed elements.
458 * @param n The number of elements to initially create.
460 * This constructor creates the %forward_list with @a n default
461 * constructed elements.
464 forward_list(size_type __n);
467 * @brief Creates a %forward_list with copies of an exemplar element.
468 * @param n The number of elements to initially create.
469 * @param value An element to copy.
470 * @param al An allocator object.
472 * This constructor fills the %forward_list with @a n copies of @a
475 forward_list(size_type __n, const _Tp& __value,
476 const _Alloc& __al = _Alloc())
478 { _M_fill_initialize(__n, __value); }
481 * @brief Builds a %forward_list from a range.
482 * @param first An input iterator.
483 * @param last An input iterator.
484 * @param al An allocator object.
486 * Create a %forward_list consisting of copies of the elements from
487 * [@a first,@a last). This is linear in N (where N is
488 * distance(@a first,@a last)).
490 template<typename _InputIterator>
491 forward_list(_InputIterator __first, _InputIterator __last,
492 const _Alloc& __al = _Alloc())
495 // Check whether it's an integral type. If so, it's not an iterator.
496 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
497 _M_initialize_dispatch(__first, __last, _Integral());
501 * @brief The %forward_list copy constructor.
502 * @param list A %forward_list of identical element and allocator
505 * The newly-created %forward_list uses a copy of the allocation
506 * object used by @a list.
508 forward_list(const forward_list& __list)
509 : _Base(__list._M_get_Node_allocator())
510 { _M_initialize_dispatch(__list.begin(), __list.end(), __false_type()); }
513 * @brief The %forward_list move constructor.
514 * @param list A %forward_list of identical element and allocator
517 * The newly-created %forward_list contains the exact contents of @a
518 * forward_list. The contents of @a list are a valid, but unspecified
521 forward_list(forward_list&& __list)
522 : _Base(std::forward<_Base>(__list)) { }
525 * @brief Builds a %forward_list from an initializer_list
526 * @param il An initializer_list of value_type.
527 * @param al An allocator object.
529 * Create a %forward_list consisting of copies of the elements
530 * in the initializer_list @a il. This is linear in il.size().
532 forward_list(std::initializer_list<_Tp> __il,
533 const _Alloc& __al = _Alloc())
535 { _M_initialize_dispatch(__il.begin(), __il.end(), __false_type()); }
538 * @brief The forward_list dtor.
544 * @brief The %forward_list assignment operator.
545 * @param list A %forward_list of identical element and allocator
548 * All the elements of @a list are copied, but unlike the copy
549 * constructor, the allocator object is not copied.
552 operator=(const forward_list& __list);
555 * @brief The %forward_list move assignment operator.
556 * @param list A %forward_list of identical element and allocator
559 * The contents of @a list are moved into this %forward_list
560 * (without copying). @a list is a valid, but unspecified
564 operator=(forward_list&& __list)
574 * @brief The %forward_list initializer list assignment operator.
575 * @param il An initializer_list of value_type.
577 * Replace the contents of the %forward_list with copies of the
578 * elements in the initializer_list @a il. This is linear in
582 operator=(std::initializer_list<_Tp> __il)
589 * @brief Assigns a range to a %forward_list.
590 * @param first An input iterator.
591 * @param last An input iterator.
593 * This function fills a %forward_list with copies of the elements
594 * in the range [@a first,@a last).
596 * Note that the assignment completely changes the %forward_list and
597 * that the resulting %forward_list's size is the same as the number
598 * of elements assigned. Old data may be lost.
600 template<typename _InputIterator>
602 assign(_InputIterator __first, _InputIterator __last)
605 insert_after(cbefore_begin(), __first, __last);
609 * @brief Assigns a given value to a %forward_list.
610 * @param n Number of elements to be assigned.
611 * @param val Value to be assigned.
613 * This function fills a %forward_list with @a n copies of the given
614 * value. Note that the assignment completely changes the
615 * %forward_list and that the resulting %forward_list's size is the
616 * same as the number of elements assigned. Old data may be lost.
619 assign(size_type __n, const _Tp& __val)
622 insert_after(cbefore_begin(), __n, __val);
626 * @brief Assigns an initializer_list to a %forward_list.
627 * @param il An initializer_list of value_type.
629 * Replace the contents of the %forward_list with copies of the
630 * elements in the initializer_list @a il. This is linear in
634 assign(std::initializer_list<_Tp> __il)
637 insert_after(cbefore_begin(), __il);
640 /// Get a copy of the memory allocation object.
642 get_allocator() const
643 { return this->_M_get_Node_allocator(); }
645 // 23.2.3.2 iterators:
648 * Returns a read/write iterator that points before the first element
649 * in the %forward_list. Iteration is done in ordinary element order.
653 { return iterator(&this->_M_impl._M_head); }
656 * Returns a read-only (constant) iterator that points before the
657 * first element in the %forward_list. Iteration is done in ordinary
662 { return const_iterator(&this->_M_impl._M_head); }
665 * Returns a read/write iterator that points to the first element
666 * in the %forward_list. Iteration is done in ordinary element order.
670 { return iterator(this->_M_impl._M_head._M_next); }
673 * Returns a read-only (constant) iterator that points to the first
674 * element in the %forward_list. Iteration is done in ordinary
679 { return const_iterator(this->_M_impl._M_head._M_next); }
682 * Returns a read/write iterator that points one past the last
683 * element in the %forward_list. Iteration is done in ordinary
688 { return iterator(0); }
691 * Returns a read-only iterator that points one past the last
692 * element in the %forward_list. Iteration is done in ordinary
697 { return const_iterator(0); }
700 * Returns a read-only (constant) iterator that points to the
701 * first element in the %forward_list. Iteration is done in ordinary
706 { return const_iterator(this->_M_impl._M_head._M_next); }
709 * Returns a read-only (constant) iterator that points before the
710 * first element in the %forward_list. Iteration is done in ordinary
714 cbefore_begin() const
715 { return const_iterator(&this->_M_impl._M_head); }
718 * Returns a read-only (constant) iterator that points one past
719 * the last element in the %forward_list. Iteration is done in
720 * ordinary element order.
724 { return const_iterator(0); }
727 * Returns true if the %forward_list is empty. (Thus begin() would
732 { return this->_M_impl._M_head._M_next == 0; }
735 * Returns the largest possible size of %forward_list.
739 { return this->_M_get_Node_allocator().max_size(); }
741 // 23.2.3.3 element access:
744 * Returns a read/write reference to the data at the first
745 * element of the %forward_list.
750 _Node* __front = static_cast<_Node*>(this->_M_impl._M_head._M_next);
751 return __front->_M_value;
755 * Returns a read-only (constant) 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;
765 // 23.2.3.4 modifiers:
768 * @brief Constructs object in %forward_list at the front of the
770 * @param args Arguments.
772 * This function will insert an object of type Tp constructed
773 * with Tp(std::forward<Args>(args)...) at the front of the list
774 * Due to the nature of a %forward_list this operation can
775 * be done in constant time, and does not invalidate iterators
778 template<typename... _Args>
780 emplace_front(_Args&&... __args)
781 { this->_M_insert_after(cbefore_begin(),
782 std::forward<_Args>(__args)...); }
785 * @brief Add data to the front of the %forward_list.
786 * @param val Data to be added.
788 * This is a typical stack operation. The function creates an
789 * element at the front of the %forward_list and assigns the given
790 * data to it. Due to the nature of a %forward_list this operation
791 * can be done in constant time, and does not invalidate iterators
795 push_front(const _Tp& __val)
796 { this->_M_insert_after(cbefore_begin(), __val); }
802 push_front(_Tp&& __val)
803 { this->_M_insert_after(cbefore_begin(), std::move(__val)); }
806 * @brief Removes first element.
808 * This is a typical stack operation. It shrinks the %forward_list
809 * by one. Due to the nature of a %forward_list this operation can
810 * be done in constant time, and only invalidates iterators/references
811 * to the element being removed.
813 * Note that no data is returned, and if the first element's data
814 * is needed, it should be retrieved before pop_front() is
819 { this->_M_erase_after(&this->_M_impl._M_head); }
822 * @brief Constructs object in %forward_list after the specified
824 * @param pos A const_iterator into the %forward_list.
825 * @param args Arguments.
826 * @return An iterator that points to the inserted data.
828 * This function will insert an object of type T constructed
829 * with T(std::forward<Args>(args)...) after the specified
830 * location. Due to the nature of a %forward_list this operation can
831 * be done in constant time, and does not invalidate iterators
834 template<typename... _Args>
836 emplace_after(const_iterator __pos, _Args&&... __args)
837 { return iterator(this->_M_insert_after(__pos,
838 std::forward<_Args>(__args)...)); }
841 * @brief Inserts given value into %forward_list after specified
843 * @param pos An iterator into the %forward_list.
844 * @param val Data to be inserted.
845 * @return An iterator that points to the inserted data.
847 * This function will insert a copy of the given value after
848 * the specified location. Due to the nature of a %forward_list this
849 * operation can be done in constant time, and does not
850 * invalidate iterators and references.
853 insert_after(const_iterator __pos, const _Tp& __val)
854 { return iterator(this->_M_insert_after(__pos, __val)); }
860 insert_after(const_iterator __pos, _Tp&& __val)
861 { return iterator(this->_M_insert_after(__pos, std::move(__val))); }
864 * @brief Inserts a number of copies of given data into the
866 * @param pos An iterator into the %forward_list.
867 * @param n Number of elements to be inserted.
868 * @param val Data to be inserted.
869 * @return An iterator pointing to the last inserted copy of
870 * @a val or @a pos if @a n == 0.
872 * This function will insert a specified number of copies of the
873 * given data after the location specified by @a pos.
875 * This operation is linear in the number of elements inserted and
876 * does not invalidate iterators and references.
879 insert_after(const_iterator __pos, size_type __n, const _Tp& __val);
882 * @brief Inserts a range into the %forward_list.
883 * @param position An iterator into the %forward_list.
884 * @param first An input iterator.
885 * @param last An input iterator.
886 * @return An iterator pointing to the last inserted element or
887 * @a pos if @a first == @a last.
889 * This function will insert copies of the data in the range [@a
890 * first,@a last) into the %forward_list after the location specified
893 * This operation is linear in the number of elements inserted and
894 * does not invalidate iterators and references.
896 template<typename _InputIterator>
898 insert_after(const_iterator __pos,
899 _InputIterator __first, _InputIterator __last);
902 * @brief Inserts the contents of an initializer_list into
903 * %forward_list after the specified iterator.
904 * @param pos An iterator into the %forward_list.
905 * @param il An initializer_list of value_type.
906 * @return An iterator pointing to the last inserted element
907 * or @a pos if @a il is empty.
909 * This function will insert copies of the data in the
910 * initializer_list @a il into the %forward_list before the location
911 * specified by @a pos.
913 * This operation is linear in the number of elements inserted and
914 * does not invalidate iterators and references.
917 insert_after(const_iterator __pos, std::initializer_list<_Tp> __il);
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)
936 { this->_M_erase_after(const_cast<_Node_base*>(__pos._M_node)); }
939 * @brief Remove a range of elements.
940 * @param pos Iterator pointing before the first element to be
942 * @param last Iterator pointing to one past the last element to be
945 * This function will erase the elements in the range @a
946 * (pos,last) and shorten the %forward_list accordingly.
948 * This operation is linear time in the size of the range and only
949 * invalidates iterators/references to the element being removed.
950 * The user is also cautioned that this function only erases the
951 * elements, and that if the elements themselves are pointers, the
952 * pointed-to memory is not touched in any way. Managing the pointer
953 * is the user's responsibility.
956 erase_after(const_iterator __pos, const_iterator __last)
957 { this->_M_erase_after(const_cast<_Node_base*>(__pos._M_node),
958 const_cast<_Node_base*>(__last._M_node)); }
961 * @brief Swaps data with another %forward_list.
962 * @param list A %forward_list of the same element and allocator
965 * This exchanges the elements between two lists in constant
966 * time. Note that the global std::swap() function is
967 * specialized such that std::swap(l1,l2) will feed to this
971 swap(forward_list& __list)
972 { _Node_base::swap(this->_M_impl._M_head, __list._M_impl._M_head); }
975 * @brief Resizes the %forward_list to the specified number of
977 * @param sz Number of elements the %forward_list should contain.
979 * This function will %resize the %forward_list to the specified
980 * number of elements. If the number is smaller than the
981 * %forward_list's current size the %forward_list is truncated,
982 * otherwise the %forward_list is extended and the new elements
983 * are default constructed.
986 resize(size_type __sz);
989 * @brief Resizes the %forward_list to the specified number of
991 * @param sz Number of elements the %forward_list should contain.
992 * @param val Data with which new elements should be populated.
994 * This function will %resize the %forward_list to the specified
995 * number of elements. If the number is smaller than the
996 * %forward_list's current size the %forward_list is truncated,
997 * otherwise the %forward_list is extended and new elements are
998 * populated with given data.
1001 resize(size_type __sz, value_type __val);
1004 * @brief Erases all the elements.
1006 * Note that this function only erases
1007 * the elements, and that if the elements themselves are
1008 * pointers, the pointed-to memory is not touched in any way.
1009 * Managing the pointer is the user's responsibility.
1013 { this->_M_erase_after(&this->_M_impl._M_head, 0); }
1015 // 23.2.3.5 forward_list operations:
1018 * @brief Insert contents of another %forward_list.
1019 * @param pos Iterator referencing the element to insert after.
1020 * @param list Source list.
1022 * The elements of @a list are inserted in constant time after
1023 * the element referenced by @a pos. @a list becomes an empty
1026 * Requires this != @a x.
1029 splice_after(const_iterator __pos, forward_list&& __list)
1031 if (!__list.empty())
1032 _M_splice_after(__pos, std::move(__list));
1036 * @brief Insert element from another %forward_list.
1037 * @param pos Iterator referencing the element to insert after.
1038 * @param list Source list.
1039 * @param i Iterator referencing the element before the element
1042 * Removes the element in list @a list referenced by @a i and
1043 * inserts it into the current list after @a pos.
1046 splice_after(const_iterator __pos, forward_list&& __list,
1049 const_iterator __j = __i;
1051 if (__pos == __i || __pos == __j)
1054 splice_after(__pos, std::move(__list), __i, __j);
1058 * @brief Insert range from another %forward_list.
1059 * @param pos Iterator referencing the element to insert after.
1060 * @param list Source list.
1061 * @param before Iterator referencing before the start of range
1063 * @param last Iterator referencing the end of range in list.
1065 * Removes elements in the range (before,last) and inserts them
1066 * after @a pos in constant time.
1068 * Undefined if @a pos is in (before,last).
1071 splice_after(const_iterator __pos, forward_list&& __list,
1072 const_iterator __before, const_iterator __last);
1075 * @brief Remove all elements equal to value.
1076 * @param val The value to remove.
1078 * Removes every element in the list equal to @a value.
1079 * Remaining elements stay in list order. Note that this
1080 * function only erases the elements, and that if the elements
1081 * themselves are pointers, the pointed-to memory is not
1082 * touched in any way. Managing the pointer is the user's
1086 remove(const _Tp& __val);
1089 * @brief Remove all elements satisfying a predicate.
1090 * @param pred Unary predicate function or object.
1092 * Removes every element in the list for which the predicate
1093 * returns true. Remaining elements stay in list order. Note
1094 * that this function only erases the elements, and that if the
1095 * elements themselves are pointers, the pointed-to memory is
1096 * not touched in any way. Managing the pointer is the user's
1099 template<typename _Pred>
1101 remove_if(_Pred __pred);
1104 * @brief Remove consecutive duplicate elements.
1106 * For each consecutive set of elements with the same value,
1107 * remove all but the first one. Remaining elements stay in
1108 * list order. Note that this function only erases the
1109 * elements, and that if the elements themselves are pointers,
1110 * the pointed-to memory is not touched in any way. Managing
1111 * the pointer is the user's responsibility.
1115 { this->unique(std::equal_to<_Tp>()); }
1118 * @brief Remove consecutive elements satisfying a predicate.
1119 * @param binary_pred Binary predicate function or object.
1121 * For each consecutive set of elements [first,last) that
1122 * satisfy predicate(first,i) where i is an iterator in
1123 * [first,last), remove all but the first one. Remaining
1124 * elements stay in list order. Note that this function only
1125 * erases the elements, and that if the elements themselves are
1126 * pointers, the pointed-to memory is not touched in any way.
1127 * Managing the pointer is the user's responsibility.
1129 template<typename _BinPred>
1131 unique(_BinPred __binary_pred);
1134 * @brief Merge sorted lists.
1135 * @param list Sorted list to merge.
1137 * Assumes that both @a list and this list are sorted according to
1138 * operator<(). Merges elements of @a list into this list in
1139 * sorted order, leaving @a list empty when complete. Elements in
1140 * this list precede elements in @a list that are equal.
1143 merge(forward_list&& __list)
1144 { this->merge(std::move(__list), std::less<_Tp>()); }
1147 * @brief Merge sorted lists according to comparison function.
1148 * @param list Sorted list to merge.
1149 * @param comp Comparison function defining sort order.
1151 * Assumes that both @a list and this list are sorted according to
1152 * comp. Merges elements of @a list into this list
1153 * in sorted order, leaving @a list empty when complete. Elements
1154 * in this list precede elements in @a list that are equivalent
1155 * according to comp().
1157 template<typename _Comp>
1159 merge(forward_list&& __list, _Comp __comp);
1162 * @brief Sort the elements of the list.
1164 * Sorts the elements of this list in NlogN time. Equivalent
1165 * elements remain in list order.
1169 { this->sort(std::less<_Tp>()); }
1172 * @brief Sort the forward_list using a comparison function.
1174 * Sorts the elements of this list in NlogN time. Equivalent
1175 * elements remain in list order.
1177 template<typename _Comp>
1182 * @brief Reverse the elements in list.
1184 * Reverse the order of elements in the list in linear time.
1188 { this->_M_impl._M_head._M_reverse_after(); }
1191 template<typename _Integer>
1193 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1194 { _M_fill_initialize(static_cast<size_type>(__n), __x); }
1196 // Called by the range constructor to implement [23.1.1]/9
1197 template<typename _InputIterator>
1199 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1202 // Called by forward_list(n,v,a), and the range constructor when it
1203 // turns out to be the same thing.
1205 _M_fill_initialize(size_type __n, const value_type& __value);
1207 // Called by splice_after and insert_after.
1209 _M_splice_after(const_iterator __pos, forward_list&& __list);
1213 * @brief Forward list equality comparison.
1214 * @param lx A %forward_list
1215 * @param ly A %forward_list of the same type as @a lx.
1216 * @return True iff the size and elements of the forward lists are equal.
1218 * This is an equivalence relation. It is linear in the size of the
1219 * forward lists. Deques are considered equivalent if corresponding
1220 * elements compare equal.
1222 template<typename _Tp, typename _Alloc>
1224 operator==(const forward_list<_Tp, _Alloc>& __lx,
1225 const forward_list<_Tp, _Alloc>& __ly);
1228 * @brief Forward list ordering relation.
1229 * @param lx A %forward_list.
1230 * @param ly A %forward_list of the same type as @a lx.
1231 * @return True iff @a lx is lexicographically less than @a ly.
1233 * This is a total ordering relation. It is linear in the size of the
1234 * forward lists. The elements must be comparable with @c <.
1236 * See std::lexicographical_compare() for how the determination is made.
1238 template<typename _Tp, typename _Alloc>
1240 operator<(const forward_list<_Tp, _Alloc>& __lx,
1241 const forward_list<_Tp, _Alloc>& __ly)
1242 { return std::lexicographical_compare(__lx.cbegin(), __lx.cend(),
1243 __ly.cbegin(), __ly.cend()); }
1245 /// Based on operator==
1246 template<typename _Tp, typename _Alloc>
1248 operator!=(const forward_list<_Tp, _Alloc>& __lx,
1249 const forward_list<_Tp, _Alloc>& __ly)
1250 { return !(__lx == __ly); }
1252 /// Based on operator<
1253 template<typename _Tp, typename _Alloc>
1255 operator>(const forward_list<_Tp, _Alloc>& __lx,
1256 const forward_list<_Tp, _Alloc>& __ly)
1257 { return (__ly < __lx); }
1259 /// Based on operator<
1260 template<typename _Tp, typename _Alloc>
1262 operator>=(const forward_list<_Tp, _Alloc>& __lx,
1263 const forward_list<_Tp, _Alloc>& __ly)
1264 { return !(__lx < __ly); }
1266 /// Based on operator<
1267 template<typename _Tp, typename _Alloc>
1269 operator<=(const forward_list<_Tp, _Alloc>& __lx,
1270 const forward_list<_Tp, _Alloc>& __ly)
1271 { return !(__ly < __lx); }
1273 /// See std::forward_list::swap().
1274 template<typename _Tp, typename _Alloc>
1276 swap(forward_list<_Tp, _Alloc>& __lx,
1277 forward_list<_Tp, _Alloc>& __ly)
1278 { __lx.swap(__ly); }
1280 _GLIBCXX_END_NAMESPACE // namespace std
1282 #endif // _FORWARD_LIST_H