1 // <forward_list.h> -*- C++ -*-
3 // Copyright (C) 2008, 2009, 2010, 2011, 2012 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 bits/forward_list.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{forward_list}
30 #ifndef _FORWARD_LIST_H
31 #define _FORWARD_LIST_H 1
33 #pragma GCC system_header
36 #ifdef __GXX_EXPERIMENTAL_CXX0X__
37 #include <initializer_list>
40 namespace std _GLIBCXX_VISIBILITY(default)
42 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
45 * @brief A helper basic node class for %forward_list.
46 * This is just a linked list with nothing inside it.
47 * There are purely list shuffling utility methods here.
49 struct _Fwd_list_node_base
51 _Fwd_list_node_base() : _M_next(0) { }
53 _Fwd_list_node_base* _M_next;
56 _M_transfer_after(_Fwd_list_node_base* __begin)
58 _Fwd_list_node_base* __end = __begin;
59 while (__end && __end->_M_next)
60 __end = __end->_M_next;
61 return _M_transfer_after(__begin, __end);
65 _M_transfer_after(_Fwd_list_node_base* __begin,
66 _Fwd_list_node_base* __end)
68 _Fwd_list_node_base* __keep = __begin->_M_next;
71 __begin->_M_next = __end->_M_next;
72 __end->_M_next = _M_next;
81 _M_reverse_after() noexcept
83 _Fwd_list_node_base* __tail = _M_next;
86 while (_Fwd_list_node_base* __temp = __tail->_M_next)
88 _Fwd_list_node_base* __keep = _M_next;
90 __tail->_M_next = __temp->_M_next;
91 _M_next->_M_next = __keep;
97 * @brief A helper node class for %forward_list.
98 * This is just a linked list with a data value in each node.
99 * There is a sorting utility method.
101 template<typename _Tp>
102 struct _Fwd_list_node
103 : public _Fwd_list_node_base
105 template<typename... _Args>
106 _Fwd_list_node(_Args&&... __args)
107 : _Fwd_list_node_base(),
108 _M_value(std::forward<_Args>(__args)...) { }
114 * @brief A forward_list::iterator.
116 * All the functions are op overloads.
118 template<typename _Tp>
119 struct _Fwd_list_iterator
121 typedef _Fwd_list_iterator<_Tp> _Self;
122 typedef _Fwd_list_node<_Tp> _Node;
124 typedef _Tp value_type;
125 typedef _Tp* pointer;
126 typedef _Tp& reference;
127 typedef ptrdiff_t difference_type;
128 typedef std::forward_iterator_tag iterator_category;
134 _Fwd_list_iterator(_Fwd_list_node_base* __n)
139 { return static_cast<_Node*>(this->_M_node)->_M_value; }
143 { return std::__addressof(static_cast<_Node*>
144 (this->_M_node)->_M_value); }
149 _M_node = _M_node->_M_next;
157 _M_node = _M_node->_M_next;
162 operator==(const _Self& __x) const
163 { return _M_node == __x._M_node; }
166 operator!=(const _Self& __x) const
167 { return _M_node != __x._M_node; }
173 return _Fwd_list_iterator(_M_node->_M_next);
175 return _Fwd_list_iterator(0);
178 _Fwd_list_node_base* _M_node;
182 * @brief A forward_list::const_iterator.
184 * All the functions are op overloads.
186 template<typename _Tp>
187 struct _Fwd_list_const_iterator
189 typedef _Fwd_list_const_iterator<_Tp> _Self;
190 typedef const _Fwd_list_node<_Tp> _Node;
191 typedef _Fwd_list_iterator<_Tp> iterator;
193 typedef _Tp value_type;
194 typedef const _Tp* pointer;
195 typedef const _Tp& reference;
196 typedef ptrdiff_t difference_type;
197 typedef std::forward_iterator_tag iterator_category;
199 _Fwd_list_const_iterator()
203 _Fwd_list_const_iterator(const _Fwd_list_node_base* __n)
206 _Fwd_list_const_iterator(const iterator& __iter)
207 : _M_node(__iter._M_node) { }
211 { return static_cast<_Node*>(this->_M_node)->_M_value; }
215 { return std::__addressof(static_cast<_Node*>
216 (this->_M_node)->_M_value); }
221 _M_node = _M_node->_M_next;
229 _M_node = _M_node->_M_next;
234 operator==(const _Self& __x) const
235 { return _M_node == __x._M_node; }
238 operator!=(const _Self& __x) const
239 { return _M_node != __x._M_node; }
245 return _Fwd_list_const_iterator(_M_node->_M_next);
247 return _Fwd_list_const_iterator(0);
250 const _Fwd_list_node_base* _M_node;
254 * @brief Forward list iterator equality comparison.
256 template<typename _Tp>
258 operator==(const _Fwd_list_iterator<_Tp>& __x,
259 const _Fwd_list_const_iterator<_Tp>& __y)
260 { return __x._M_node == __y._M_node; }
263 * @brief Forward list iterator inequality comparison.
265 template<typename _Tp>
267 operator!=(const _Fwd_list_iterator<_Tp>& __x,
268 const _Fwd_list_const_iterator<_Tp>& __y)
269 { return __x._M_node != __y._M_node; }
272 * @brief Base class for %forward_list.
274 template<typename _Tp, typename _Alloc>
275 struct _Fwd_list_base
278 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
280 typedef typename _Alloc::template
281 rebind<_Fwd_list_node<_Tp>>::other _Node_alloc_type;
283 struct _Fwd_list_impl
284 : public _Node_alloc_type
286 _Fwd_list_node_base _M_head;
289 : _Node_alloc_type(), _M_head()
292 _Fwd_list_impl(const _Node_alloc_type& __a)
293 : _Node_alloc_type(__a), _M_head()
296 _Fwd_list_impl(_Node_alloc_type&& __a)
297 : _Node_alloc_type(std::move(__a)), _M_head()
301 _Fwd_list_impl _M_impl;
304 typedef _Fwd_list_iterator<_Tp> iterator;
305 typedef _Fwd_list_const_iterator<_Tp> const_iterator;
306 typedef _Fwd_list_node<_Tp> _Node;
309 _M_get_Node_allocator() noexcept
310 { return *static_cast<_Node_alloc_type*>(&this->_M_impl); }
312 const _Node_alloc_type&
313 _M_get_Node_allocator() const noexcept
314 { return *static_cast<const _Node_alloc_type*>(&this->_M_impl); }
319 _Fwd_list_base(const _Node_alloc_type& __a)
322 _Fwd_list_base(const _Fwd_list_base& __lst, const _Node_alloc_type& __a);
324 _Fwd_list_base(_Fwd_list_base&& __lst, const _Node_alloc_type& __a)
327 this->_M_impl._M_head._M_next = __lst._M_impl._M_head._M_next;
328 __lst._M_impl._M_head._M_next = 0;
331 _Fwd_list_base(_Fwd_list_base&& __lst)
332 : _M_impl(std::move(__lst._M_get_Node_allocator()))
334 this->_M_impl._M_head._M_next = __lst._M_impl._M_head._M_next;
335 __lst._M_impl._M_head._M_next = 0;
339 { _M_erase_after(&_M_impl._M_head, 0); }
345 { return _M_get_Node_allocator().allocate(1); }
347 template<typename... _Args>
349 _M_create_node(_Args&&... __args)
351 _Node* __node = this->_M_get_node();
354 _M_get_Node_allocator().construct(__node,
355 std::forward<_Args>(__args)...);
360 this->_M_put_node(__node);
361 __throw_exception_again;
366 template<typename... _Args>
368 _M_insert_after(const_iterator __pos, _Args&&... __args);
371 _M_put_node(_Node* __p)
372 { _M_get_Node_allocator().deallocate(__p, 1); }
375 _M_erase_after(_Fwd_list_node_base* __pos);
378 _M_erase_after(_Fwd_list_node_base* __pos,
379 _Fwd_list_node_base* __last);
383 * @brief A standard container with linear time access to elements,
384 * and fixed time insertion/deletion at any point in the sequence.
388 * Meets the requirements of a <a href="tables.html#65">container</a>, a
389 * <a href="tables.html#67">sequence</a>, including the
390 * <a href="tables.html#68">optional sequence requirements</a> with the
391 * %exception of @c at and @c operator[].
393 * This is a @e singly @e linked %list. Traversal up the
394 * %list requires linear time, but adding and removing elements (or
395 * @e nodes) is done in constant time, regardless of where the
396 * change takes place. Unlike std::vector and std::deque,
397 * random-access iterators are not provided, so subscripting ( @c
398 * [] ) access is not allowed. For algorithms which only need
399 * sequential access, this lack makes no difference.
401 * Also unlike the other standard containers, std::forward_list provides
402 * specialized algorithms %unique to linked lists, such as
403 * splicing, sorting, and in-place reversal.
405 * A couple points on memory allocation for forward_list<Tp>:
407 * First, we never actually allocate a Tp, we allocate
408 * Fwd_list_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
409 * that after elements from %forward_list<X,Alloc1> are spliced into
410 * %forward_list<X,Alloc2>, destroying the memory of the second %list is a
411 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
413 template<typename _Tp, typename _Alloc = allocator<_Tp> >
414 class forward_list : private _Fwd_list_base<_Tp, _Alloc>
417 typedef _Fwd_list_base<_Tp, _Alloc> _Base;
418 typedef _Fwd_list_node<_Tp> _Node;
419 typedef _Fwd_list_node_base _Node_base;
420 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
421 typedef typename _Base::_Node_alloc_type _Node_alloc_type;
425 typedef _Tp value_type;
426 typedef typename _Tp_alloc_type::pointer pointer;
427 typedef typename _Tp_alloc_type::const_pointer const_pointer;
428 typedef typename _Tp_alloc_type::reference reference;
429 typedef typename _Tp_alloc_type::const_reference const_reference;
431 typedef _Fwd_list_iterator<_Tp> iterator;
432 typedef _Fwd_list_const_iterator<_Tp> const_iterator;
433 typedef std::size_t size_type;
434 typedef std::ptrdiff_t difference_type;
435 typedef _Alloc allocator_type;
437 // 23.2.3.1 construct/copy/destroy:
440 * @brief Creates a %forward_list with no elements.
441 * @param __al An allocator object.
444 forward_list(const _Alloc& __al = _Alloc())
445 : _Base(_Node_alloc_type(__al))
449 * @brief Copy constructor with allocator argument.
450 * @param __list Input list to copy.
451 * @param __al An allocator object.
453 forward_list(const forward_list& __list, const _Alloc& __al)
454 : _Base(__list, _Node_alloc_type(__al))
458 * @brief Move constructor with allocator argument.
459 * @param __list Input list to move.
460 * @param __al An allocator object.
462 forward_list(forward_list&& __list, const _Alloc& __al)
463 : _Base(std::move(__list), _Node_alloc_type(__al))
467 * @brief Creates a %forward_list with default constructed elements.
468 * @param __n The number of elements to initially create.
470 * This constructor creates the %forward_list with @a __n default
471 * constructed elements.
474 forward_list(size_type __n)
476 { _M_default_initialize(__n); }
479 * @brief Creates a %forward_list with copies of an exemplar element.
480 * @param __n The number of elements to initially create.
481 * @param __value An element to copy.
482 * @param __al An allocator object.
484 * This constructor fills the %forward_list with @a __n copies of
487 forward_list(size_type __n, const _Tp& __value,
488 const _Alloc& __al = _Alloc())
489 : _Base(_Node_alloc_type(__al))
490 { _M_fill_initialize(__n, __value); }
493 * @brief Builds a %forward_list from a range.
494 * @param __first An input iterator.
495 * @param __last An input iterator.
496 * @param __al An allocator object.
498 * Create a %forward_list consisting of copies of the elements from
499 * [@a __first,@a __last). This is linear in N (where N is
500 * distance(@a __first,@a __last)).
502 template<typename _InputIterator>
503 forward_list(_InputIterator __first, _InputIterator __last,
504 const _Alloc& __al = _Alloc())
505 : _Base(_Node_alloc_type(__al))
507 // Check whether it's an integral type. If so, it's not an iterator.
508 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
509 _M_initialize_dispatch(__first, __last, _Integral());
513 * @brief The %forward_list copy constructor.
514 * @param __list A %forward_list of identical element and allocator
517 * The newly-created %forward_list uses a copy of the allocation
518 * object used by @a __list.
520 forward_list(const forward_list& __list)
521 : _Base(__list._M_get_Node_allocator())
522 { _M_initialize_dispatch(__list.begin(), __list.end(), __false_type()); }
525 * @brief The %forward_list move constructor.
526 * @param __list A %forward_list of identical element and allocator
529 * The newly-created %forward_list contains the exact contents of @a
530 * forward_list. The contents of @a __list are a valid, but unspecified
533 forward_list(forward_list&& __list) noexcept
534 : _Base(std::move(__list)) { }
537 * @brief Builds a %forward_list from an initializer_list
538 * @param __il An initializer_list of value_type.
539 * @param __al An allocator object.
541 * Create a %forward_list consisting of copies of the elements
542 * in the initializer_list @a __il. This is linear in __il.size().
544 forward_list(std::initializer_list<_Tp> __il,
545 const _Alloc& __al = _Alloc())
546 : _Base(_Node_alloc_type(__al))
547 { _M_initialize_dispatch(__il.begin(), __il.end(), __false_type()); }
550 * @brief The forward_list dtor.
552 ~forward_list() noexcept
556 * @brief The %forward_list assignment operator.
557 * @param __list A %forward_list of identical element and allocator
560 * All the elements of @a __list are copied, but unlike the copy
561 * constructor, the allocator object is not copied.
564 operator=(const forward_list& __list);
567 * @brief The %forward_list move assignment operator.
568 * @param __list A %forward_list of identical element and allocator
571 * The contents of @a __list are moved into this %forward_list
572 * (without copying). @a __list is a valid, but unspecified
576 operator=(forward_list&& __list)
586 * @brief The %forward_list initializer list assignment operator.
587 * @param __il An initializer_list of value_type.
589 * Replace the contents of the %forward_list with copies of the
590 * elements in the initializer_list @a __il. This is linear in
594 operator=(std::initializer_list<_Tp> __il)
601 * @brief Assigns a range to a %forward_list.
602 * @param __first An input iterator.
603 * @param __last An input iterator.
605 * This function fills a %forward_list with copies of the elements
606 * in the range [@a __first,@a __last).
608 * Note that the assignment completely changes the %forward_list and
609 * that the number of elements of the resulting %forward_list's is the
610 * same as the number of elements assigned. Old data is lost.
612 template<typename _InputIterator>
614 assign(_InputIterator __first, _InputIterator __last)
617 insert_after(cbefore_begin(), __first, __last);
621 * @brief Assigns a given value to a %forward_list.
622 * @param __n Number of elements to be assigned.
623 * @param __val Value to be assigned.
625 * This function fills a %forward_list with @a __n copies of the
626 * given value. Note that the assignment completely changes the
627 * %forward_list, and that the resulting %forward_list has __n
628 * elements. Old data is lost.
631 assign(size_type __n, const _Tp& __val)
634 insert_after(cbefore_begin(), __n, __val);
638 * @brief Assigns an initializer_list to a %forward_list.
639 * @param __il An initializer_list of value_type.
641 * Replace the contents of the %forward_list with copies of the
642 * elements in the initializer_list @a __il. This is linear in
646 assign(std::initializer_list<_Tp> __il)
649 insert_after(cbefore_begin(), __il);
652 /// Get a copy of the memory allocation object.
654 get_allocator() const noexcept
655 { return allocator_type(this->_M_get_Node_allocator()); }
657 // 23.2.3.2 iterators:
660 * Returns a read/write iterator that points before the first element
661 * in the %forward_list. Iteration is done in ordinary element order.
664 before_begin() noexcept
665 { return iterator(&this->_M_impl._M_head); }
668 * Returns a read-only (constant) iterator that points before the
669 * first element in the %forward_list. Iteration is done in ordinary
673 before_begin() const noexcept
674 { return const_iterator(&this->_M_impl._M_head); }
677 * Returns a read/write iterator that points to the first element
678 * in the %forward_list. Iteration is done in ordinary element order.
682 { return iterator(this->_M_impl._M_head._M_next); }
685 * Returns a read-only (constant) iterator that points to the first
686 * element in the %forward_list. Iteration is done in ordinary
690 begin() const noexcept
691 { return const_iterator(this->_M_impl._M_head._M_next); }
694 * Returns a read/write iterator that points one past the last
695 * element in the %forward_list. Iteration is done in ordinary
700 { return iterator(0); }
703 * Returns a read-only iterator that points one past the last
704 * element in the %forward_list. Iteration is done in ordinary
709 { return const_iterator(0); }
712 * Returns a read-only (constant) iterator that points to the
713 * first element in the %forward_list. Iteration is done in ordinary
717 cbegin() const noexcept
718 { return const_iterator(this->_M_impl._M_head._M_next); }
721 * Returns a read-only (constant) iterator that points before the
722 * first element in the %forward_list. Iteration is done in ordinary
726 cbefore_begin() const noexcept
727 { return const_iterator(&this->_M_impl._M_head); }
730 * Returns a read-only (constant) iterator that points one past
731 * the last element in the %forward_list. Iteration is done in
732 * ordinary element order.
735 cend() const noexcept
736 { return const_iterator(0); }
739 * Returns true if the %forward_list is empty. (Thus begin() would
743 empty() const noexcept
744 { return this->_M_impl._M_head._M_next == 0; }
747 * Returns the largest possible number of elements of %forward_list.
750 max_size() const noexcept
751 { return this->_M_get_Node_allocator().max_size(); }
753 // 23.2.3.3 element access:
756 * Returns a read/write reference to the data at the first
757 * element of the %forward_list.
762 _Node* __front = static_cast<_Node*>(this->_M_impl._M_head._M_next);
763 return __front->_M_value;
767 * Returns a read-only (constant) reference to the data at the first
768 * element of the %forward_list.
773 _Node* __front = static_cast<_Node*>(this->_M_impl._M_head._M_next);
774 return __front->_M_value;
777 // 23.2.3.4 modifiers:
780 * @brief Constructs object in %forward_list at the front of the
782 * @param __args Arguments.
784 * This function will insert an object of type Tp constructed
785 * with Tp(std::forward<Args>(args)...) at the front of the list
786 * Due to the nature of a %forward_list this operation can
787 * be done in constant time, and does not invalidate iterators
790 template<typename... _Args>
792 emplace_front(_Args&&... __args)
793 { this->_M_insert_after(cbefore_begin(),
794 std::forward<_Args>(__args)...); }
797 * @brief Add data to the front of the %forward_list.
798 * @param __val Data to be added.
800 * This is a typical stack operation. The function creates an
801 * element at the front of the %forward_list and assigns the given
802 * data to it. Due to the nature of a %forward_list this operation
803 * can be done in constant time, and does not invalidate iterators
807 push_front(const _Tp& __val)
808 { this->_M_insert_after(cbefore_begin(), __val); }
814 push_front(_Tp&& __val)
815 { this->_M_insert_after(cbefore_begin(), std::move(__val)); }
818 * @brief Removes first element.
820 * This is a typical stack operation. It shrinks the %forward_list
821 * by one. Due to the nature of a %forward_list this operation can
822 * be done in constant time, and only invalidates iterators/references
823 * to the element being removed.
825 * Note that no data is returned, and if the first element's data
826 * is needed, it should be retrieved before pop_front() is
831 { this->_M_erase_after(&this->_M_impl._M_head); }
834 * @brief Constructs object in %forward_list after the specified
836 * @param __pos A const_iterator into the %forward_list.
837 * @param __args Arguments.
838 * @return An iterator that points to the inserted data.
840 * This function will insert an object of type T constructed
841 * with T(std::forward<Args>(args)...) after the specified
842 * location. Due to the nature of a %forward_list this operation can
843 * be done in constant time, and does not invalidate iterators
846 template<typename... _Args>
848 emplace_after(const_iterator __pos, _Args&&... __args)
849 { return iterator(this->_M_insert_after(__pos,
850 std::forward<_Args>(__args)...)); }
853 * @brief Inserts given value into %forward_list after specified
855 * @param __pos An iterator into the %forward_list.
856 * @param __val Data to be inserted.
857 * @return An iterator that points to the inserted data.
859 * This function will insert a copy of the given value after
860 * the specified location. Due to the nature of a %forward_list this
861 * operation can be done in constant time, and does not
862 * invalidate iterators and references.
865 insert_after(const_iterator __pos, const _Tp& __val)
866 { return iterator(this->_M_insert_after(__pos, __val)); }
872 insert_after(const_iterator __pos, _Tp&& __val)
873 { return iterator(this->_M_insert_after(__pos, std::move(__val))); }
876 * @brief Inserts a number of copies of given data into the
878 * @param __pos An iterator into the %forward_list.
879 * @param __n Number of elements to be inserted.
880 * @param __val Data to be inserted.
881 * @return An iterator pointing to the last inserted copy of
882 * @a val or @a pos if @a n == 0.
884 * This function will insert a specified number of copies of the
885 * given data after the location specified by @a pos.
887 * This operation is linear in the number of elements inserted and
888 * does not invalidate iterators and references.
891 insert_after(const_iterator __pos, size_type __n, const _Tp& __val);
894 * @brief Inserts a range into the %forward_list.
895 * @param __pos An iterator into the %forward_list.
896 * @param __first An input iterator.
897 * @param __last An input iterator.
898 * @return An iterator pointing to the last inserted element or
899 * @a __pos if @a __first == @a __last.
901 * This function will insert copies of the data in the range
902 * [@a __first,@a __last) into the %forward_list after the
903 * location specified by @a __pos.
905 * This operation is linear in the number of elements inserted and
906 * does not invalidate iterators and references.
908 template<typename _InputIterator>
910 insert_after(const_iterator __pos,
911 _InputIterator __first, _InputIterator __last);
914 * @brief Inserts the contents of an initializer_list into
915 * %forward_list after the specified iterator.
916 * @param __pos An iterator into the %forward_list.
917 * @param __il An initializer_list of value_type.
918 * @return An iterator pointing to the last inserted element
919 * or @a __pos if @a __il is empty.
921 * This function will insert copies of the data in the
922 * initializer_list @a __il into the %forward_list before the location
923 * specified by @a __pos.
925 * This operation is linear in the number of elements inserted and
926 * does not invalidate iterators and references.
929 insert_after(const_iterator __pos, std::initializer_list<_Tp> __il);
932 * @brief Removes the element pointed to by the iterator following
934 * @param __pos Iterator pointing before element to be erased.
935 * @return An iterator pointing to the element following the one
936 * that was erased, or end() if no such element exists.
938 * This function will erase the element at the given position and
939 * thus shorten the %forward_list by one.
941 * Due to the nature of a %forward_list this operation can be done
942 * in constant time, and only invalidates iterators/references to
943 * the element being removed. The user is also cautioned that
944 * this function only erases the element, and that if the element
945 * is itself a pointer, the pointed-to memory is not touched in
946 * any way. Managing the pointer is the user's responsibility.
949 erase_after(const_iterator __pos)
950 { return iterator(this->_M_erase_after(const_cast<_Node_base*>
954 * @brief Remove a range of elements.
955 * @param __pos Iterator pointing before the first element to be
957 * @param __last Iterator pointing to one past the last element to be
961 * This function will erase the elements in the range
962 * @a (__pos,__last) and shorten the %forward_list accordingly.
964 * This operation is linear time in the size of the range and only
965 * invalidates iterators/references to the element being removed.
966 * The user is also cautioned that this function only erases the
967 * elements, and that if the elements themselves are pointers, the
968 * pointed-to memory is not touched in any way. Managing the pointer
969 * is the user's responsibility.
972 erase_after(const_iterator __pos, const_iterator __last)
973 { return iterator(this->_M_erase_after(const_cast<_Node_base*>
975 const_cast<_Node_base*>
976 (__last._M_node))); }
979 * @brief Swaps data with another %forward_list.
980 * @param __list A %forward_list of the same element and allocator
983 * This exchanges the elements between two lists in constant
984 * time. Note that the global std::swap() function is
985 * specialized such that std::swap(l1,l2) will feed to this
989 swap(forward_list& __list)
990 { std::swap(this->_M_impl._M_head._M_next,
991 __list._M_impl._M_head._M_next); }
994 * @brief Resizes the %forward_list to the specified number of
996 * @param __sz Number of elements the %forward_list should contain.
998 * This function will %resize the %forward_list to the specified
999 * number of elements. If the number is smaller than the
1000 * %forward_list's current number of elements the %forward_list
1001 * is truncated, otherwise the %forward_list is extended and the
1002 * new elements are default constructed.
1005 resize(size_type __sz);
1008 * @brief Resizes the %forward_list to the specified number of
1010 * @param __sz Number of elements the %forward_list should contain.
1011 * @param __val Data with which new elements should be populated.
1013 * This function will %resize the %forward_list to the specified
1014 * number of elements. If the number is smaller than the
1015 * %forward_list's current number of elements the %forward_list
1016 * is truncated, otherwise the %forward_list is extended and new
1017 * elements are populated with given data.
1020 resize(size_type __sz, const value_type& __val);
1023 * @brief Erases all the elements.
1025 * Note that this function only erases
1026 * the elements, and that if the elements themselves are
1027 * pointers, the pointed-to memory is not touched in any way.
1028 * Managing the pointer is the user's responsibility.
1032 { this->_M_erase_after(&this->_M_impl._M_head, 0); }
1034 // 23.2.3.5 forward_list operations:
1037 * @brief Insert contents of another %forward_list.
1038 * @param __pos Iterator referencing the element to insert after.
1039 * @param __list Source list.
1041 * The elements of @a list are inserted in constant time after
1042 * the element referenced by @a pos. @a list becomes an empty
1045 * Requires this != @a x.
1048 splice_after(const_iterator __pos, forward_list&& __list)
1050 if (!__list.empty())
1051 _M_splice_after(__pos, std::move(__list));
1055 * @brief Insert element from another %forward_list.
1056 * @param __pos Iterator referencing the element to insert after.
1057 * @param __list Source list.
1058 * @param __i Iterator referencing the element before the element
1061 * Removes the element in list @a list referenced by @a i and
1062 * inserts it into the current list after @a pos.
1065 splice_after(const_iterator __pos, forward_list&& __list,
1068 const_iterator __j = __i;
1070 if (__pos == __i || __pos == __j)
1073 splice_after(__pos, std::move(__list), __i, __j);
1077 * @brief Insert range from another %forward_list.
1078 * @param __pos Iterator referencing the element to insert after.
1079 * @param __list Source list.
1080 * @param __before Iterator referencing before the start of range
1082 * @param __last Iterator referencing the end of range in list.
1084 * Removes elements in the range (__before,__last) and inserts them
1085 * after @a __pos in constant time.
1087 * Undefined if @a __pos is in (__before,__last).
1090 splice_after(const_iterator __pos, forward_list&& __list,
1091 const_iterator __before, const_iterator __last);
1094 * @brief Remove all elements equal to value.
1095 * @param __val The value to remove.
1097 * Removes every element in the list equal to @a __val.
1098 * Remaining elements stay in list order. Note that this
1099 * function only erases the elements, and that if the elements
1100 * themselves are pointers, the pointed-to memory is not
1101 * touched in any way. Managing the pointer is the user's
1105 remove(const _Tp& __val);
1108 * @brief Remove all elements satisfying a predicate.
1109 * @param __pred Unary predicate function or object.
1111 * Removes every element in the list for which the predicate
1112 * returns true. Remaining elements stay in list order. Note
1113 * that this function only erases the elements, and that if the
1114 * elements themselves are pointers, the pointed-to memory is
1115 * not touched in any way. Managing the pointer is the user's
1118 template<typename _Pred>
1120 remove_if(_Pred __pred);
1123 * @brief Remove consecutive duplicate elements.
1125 * For each consecutive set of elements with the same value,
1126 * remove all but the first one. Remaining elements stay in
1127 * list order. Note that this function only erases the
1128 * elements, and that if the elements themselves are pointers,
1129 * the pointed-to memory is not touched in any way. Managing
1130 * the pointer is the user's responsibility.
1134 { this->unique(std::equal_to<_Tp>()); }
1137 * @brief Remove consecutive elements satisfying a predicate.
1138 * @param __binary_pred Binary predicate function or object.
1140 * For each consecutive set of elements [first,last) that
1141 * satisfy predicate(first,i) where i is an iterator in
1142 * [first,last), remove all but the first one. Remaining
1143 * elements stay in list order. Note that this function only
1144 * erases the elements, and that if the elements themselves are
1145 * pointers, the pointed-to memory is not touched in any way.
1146 * Managing the pointer is the user's responsibility.
1148 template<typename _BinPred>
1150 unique(_BinPred __binary_pred);
1153 * @brief Merge sorted lists.
1154 * @param __list Sorted list to merge.
1156 * Assumes that both @a list and this list are sorted according to
1157 * operator<(). Merges elements of @a __list into this list in
1158 * sorted order, leaving @a __list empty when complete. Elements in
1159 * this list precede elements in @a __list that are equal.
1162 merge(forward_list&& __list)
1163 { this->merge(std::move(__list), std::less<_Tp>()); }
1166 * @brief Merge sorted lists according to comparison function.
1167 * @param __list Sorted list to merge.
1168 * @param __comp Comparison function defining sort order.
1170 * Assumes that both @a __list and this list are sorted according to
1171 * comp. Merges elements of @a __list into this list
1172 * in sorted order, leaving @a __list empty when complete. Elements
1173 * in this list precede elements in @a __list that are equivalent
1174 * according to comp().
1176 template<typename _Comp>
1178 merge(forward_list&& __list, _Comp __comp);
1181 * @brief Sort the elements of the list.
1183 * Sorts the elements of this list in NlogN time. Equivalent
1184 * elements remain in list order.
1188 { this->sort(std::less<_Tp>()); }
1191 * @brief Sort the forward_list using a comparison function.
1193 * Sorts the elements of this list in NlogN time. Equivalent
1194 * elements remain in list order.
1196 template<typename _Comp>
1201 * @brief Reverse the elements in list.
1203 * Reverse the order of elements in the list in linear time.
1207 { this->_M_impl._M_head._M_reverse_after(); }
1210 template<typename _Integer>
1212 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1213 { _M_fill_initialize(static_cast<size_type>(__n), __x); }
1215 // Called by the range constructor to implement [23.1.1]/9
1216 template<typename _InputIterator>
1218 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1221 // Called by forward_list(n,v,a), and the range constructor when it
1222 // turns out to be the same thing.
1224 _M_fill_initialize(size_type __n, const value_type& __value);
1226 // Called by splice_after and insert_after.
1228 _M_splice_after(const_iterator __pos, forward_list&& __list);
1230 // Called by forward_list(n).
1232 _M_default_initialize(size_type __n);
1234 // Called by resize(sz).
1236 _M_default_insert_after(const_iterator __pos, size_type __n);
1240 * @brief Forward list equality comparison.
1241 * @param __lx A %forward_list
1242 * @param __ly A %forward_list of the same type as @a __lx.
1243 * @return True iff the elements of the forward lists are equal.
1245 * This is an equivalence relation. It is linear in the number of
1246 * elements of the forward lists. Deques are considered equivalent
1247 * if corresponding elements compare equal.
1249 template<typename _Tp, typename _Alloc>
1251 operator==(const forward_list<_Tp, _Alloc>& __lx,
1252 const forward_list<_Tp, _Alloc>& __ly);
1255 * @brief Forward list ordering relation.
1256 * @param __lx A %forward_list.
1257 * @param __ly A %forward_list of the same type as @a __lx.
1258 * @return True iff @a __lx is lexicographically less than @a __ly.
1260 * This is a total ordering relation. It is linear in the number of
1261 * elements of the forward lists. The elements must be comparable
1264 * See std::lexicographical_compare() for how the determination is made.
1266 template<typename _Tp, typename _Alloc>
1268 operator<(const forward_list<_Tp, _Alloc>& __lx,
1269 const forward_list<_Tp, _Alloc>& __ly)
1270 { return std::lexicographical_compare(__lx.cbegin(), __lx.cend(),
1271 __ly.cbegin(), __ly.cend()); }
1273 /// Based on operator==
1274 template<typename _Tp, typename _Alloc>
1276 operator!=(const forward_list<_Tp, _Alloc>& __lx,
1277 const forward_list<_Tp, _Alloc>& __ly)
1278 { return !(__lx == __ly); }
1280 /// Based on operator<
1281 template<typename _Tp, typename _Alloc>
1283 operator>(const forward_list<_Tp, _Alloc>& __lx,
1284 const forward_list<_Tp, _Alloc>& __ly)
1285 { return (__ly < __lx); }
1287 /// Based on operator<
1288 template<typename _Tp, typename _Alloc>
1290 operator>=(const forward_list<_Tp, _Alloc>& __lx,
1291 const forward_list<_Tp, _Alloc>& __ly)
1292 { return !(__lx < __ly); }
1294 /// Based on operator<
1295 template<typename _Tp, typename _Alloc>
1297 operator<=(const forward_list<_Tp, _Alloc>& __lx,
1298 const forward_list<_Tp, _Alloc>& __ly)
1299 { return !(__ly < __lx); }
1301 /// See std::forward_list::swap().
1302 template<typename _Tp, typename _Alloc>
1304 swap(forward_list<_Tp, _Alloc>& __lx,
1305 forward_list<_Tp, _Alloc>& __ly)
1306 { __lx.swap(__ly); }
1308 _GLIBCXX_END_NAMESPACE_CONTAINER
1311 #endif // _FORWARD_LIST_H