1 // <forward_list.h> -*- C++ -*-
3 // Copyright (C) 2008 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file forward_list.h
31 * This is a Standard C++ Library header.
34 #ifndef _GLIBCXX_FORWARD_LIST_H
35 #define _GLIBCXX_FORWARD_LIST_H 1
37 #pragma GCC system_header
39 #ifndef __GXX_EXPERIMENTAL_CXX0X__
40 # include <c++0x_warning.h>
44 #include <initializer_list>
46 _GLIBCXX_BEGIN_NAMESPACE(std)
49 * @brief A helper basic node class for @forward_list.
50 * This is just a linked list with nothing inside it.
51 * There are purely list shuffling utility methods here.
53 struct _Fwd_list_node_base
55 _Fwd_list_node_base() : _M_next(0) { }
57 _Fwd_list_node_base* _M_next;
60 swap(_Fwd_list_node_base& __x, _Fwd_list_node_base& __y)
61 { std::swap(__x._M_next, __y._M_next); }
64 _M_transfer_after(_Fwd_list_node_base* __bbegin,
65 _Fwd_list_node_base* __bend)
67 _Fwd_list_node_base* __keep = __bbegin->_M_next;
70 __bbegin->_M_next = __bend->_M_next;
71 __bend->_M_next = this->_M_next;
74 __bbegin->_M_next = 0;
75 this->_M_next = __keep;
79 _M_transfer_after(_Fwd_list_node_base* __bbegin)
81 _Fwd_list_node_base* __bend = __bbegin;
82 while (__bend && __bend->_M_next)
83 __bend = __bend->_M_next;
84 _M_transfer_after(__bbegin, __bend);
90 _Fwd_list_node_base* __tail = this->_M_next;
93 while (_Fwd_list_node_base* __temp = __tail->_M_next)
95 _Fwd_list_node_base* __keep = this->_M_next;
96 this->_M_next = __temp;
97 __tail->_M_next = __temp->_M_next;
98 this->_M_next->_M_next = __keep;
104 * @brief A helper node class for @forward_list.
105 * This is just a linked list with a data value in each node.
106 * There is a sorting utility method.
108 template<typename _Tp>
109 struct _Fwd_list_node : public _Fwd_list_node_base
111 template<typename... _Args>
112 _Fwd_list_node(_Args&&... __args)
113 : _Fwd_list_node_base(), _M_value(std::forward<_Args>(__args)...) { }
115 template<typename _Comp>
117 _M_sort_after(_Comp __comp);
123 * @brief A forward_list::iterator.
125 * All the functions are op overloads.
127 template<typename _Tp>
128 struct _Fwd_list_iterator
130 typedef _Fwd_list_iterator<_Tp> _Self;
131 typedef _Fwd_list_node<_Tp> _Node;
133 typedef _Tp value_type;
134 typedef _Tp* pointer;
135 typedef _Tp& reference;
136 typedef ptrdiff_t difference_type;
137 typedef std::forward_iterator_tag iterator_category;
139 _Fwd_list_iterator() : _M_node() { }
142 _Fwd_list_iterator(_Fwd_list_node_base* __n) : _M_node(__n) { }
146 { return static_cast<_Node*>(this->_M_node)->_M_value; }
150 { return &static_cast<_Node*>(this->_M_node)->_M_value; }
155 this->_M_node = this->_M_node->_M_next;
163 this->_M_node = this->_M_node->_M_next;
168 operator==(const _Self& __x) const
169 { return this->_M_node == __x._M_node; }
172 operator!=(const _Self& __x) const
173 { return this->_M_node != __x._M_node; }
179 return _Fwd_list_iterator(this->_M_node->_M_next);
181 return _Fwd_list_iterator(0);
184 _Fwd_list_node_base* _M_node;
188 * @brief A forward_list::const_iterator.
190 * All the functions are op overloads.
192 template<typename _Tp>
193 struct _Fwd_list_const_iterator
195 typedef _Fwd_list_const_iterator<_Tp> _Self;
196 typedef const _Fwd_list_node<_Tp> _Node;
197 typedef _Fwd_list_iterator<_Tp> iterator;
199 typedef _Tp value_type;
200 typedef const _Tp* pointer;
201 typedef const _Tp& reference;
202 typedef ptrdiff_t difference_type;
203 typedef std::forward_iterator_tag iterator_category;
205 _Fwd_list_const_iterator() : _M_node() { }
208 _Fwd_list_const_iterator(const _Fwd_list_node_base* __n)
211 _Fwd_list_const_iterator(const iterator& __iter)
212 : _M_node(__iter._M_node) { }
216 { return static_cast<_Node*>(this->_M_node)->_M_value; }
220 { return &static_cast<_Node*>(this->_M_node)->_M_value; }
225 this->_M_node = this->_M_node->_M_next;
233 this->_M_node = this->_M_node->_M_next;
238 operator==(const _Self& __x) const
239 { return this->_M_node == __x._M_node; }
242 operator!=(const _Self& __x) const
243 { return this->_M_node != __x._M_node; }
249 return _Fwd_list_const_iterator(this->_M_node->_M_next);
251 return _Fwd_list_const_iterator(0);
254 const _Fwd_list_node_base* _M_node;
258 * @brief Forward list iterator equality comparison.
260 template<typename _Tp>
262 operator==(const _Fwd_list_iterator<_Tp>& __x,
263 const _Fwd_list_const_iterator<_Tp>& __y)
264 { return __x._M_node == __y._M_node; }
267 * @brief Forward list iterator inequality comparison.
269 template<typename _Tp>
271 operator!=(const _Fwd_list_iterator<_Tp>& __x,
272 const _Fwd_list_const_iterator<_Tp>& __y)
273 { return __x._M_node != __y._M_node; }
276 * @brief Base class for @forward_list.
278 template<typename _Tp, typename _Alloc>
279 struct _Fwd_list_base
282 typedef typename _Alloc::template rebind<_Fwd_list_node<_Tp>>::other
285 struct _Fwd_list_impl
286 : public _Node_alloc_type
288 _Fwd_list_node_base _M_head;
291 : _Node_alloc_type(), _M_head()
295 _Fwd_list_impl(const _Node_alloc_type& __a)
296 : _Node_alloc_type(__a), _M_head()
300 _Fwd_list_impl _M_impl;
303 typedef _Alloc allocator_type;
304 typedef _Fwd_list_iterator<_Tp> iterator;
305 typedef _Fwd_list_const_iterator<_Tp> const_iterator;
307 typedef _Fwd_list_node<_Tp> _Node;
310 _M_get_Node_allocator()
311 { return *static_cast<_Node_alloc_type*>(&this->_M_impl); }
313 const _Node_alloc_type&
314 _M_get_Node_allocator() const
315 { return *static_cast<const _Node_alloc_type*>(&this->_M_impl); }
318 get_allocator() const
319 { return this->_M_get_Node_allocator(); }
323 { this->_M_impl._M_head._M_next = 0; }
325 _Fwd_list_base(const allocator_type& __a)
327 { this->_M_impl._M_head._M_next = 0; }
329 _Fwd_list_base(const _Fwd_list_base& __lst, const _Alloc& __a);
331 _Fwd_list_base(_Fwd_list_base&& __lst, const _Alloc& __a)
333 { _Fwd_list_node_base::swap(this->_M_impl._M_head,
334 __lst._M_impl._M_head); }
336 _Fwd_list_base(_Fwd_list_base&& __lst)
337 : _M_impl(__lst._M_get_Node_allocator())
338 { _Fwd_list_node_base::swap(this->_M_impl._M_head,
339 __lst._M_impl._M_head); }
342 { _M_erase_after(&_M_impl._M_head, 0); }
348 { return _M_get_Node_allocator().allocate(1); }
350 template<typename... _Args>
352 _M_create_node(_Args&&... __args)
354 _Node* __node = this->_M_get_node();
357 _M_get_Node_allocator().construct(__node,
358 std::forward<_Args>(__args)...);
363 this->_M_put_node(__node);
364 __throw_exception_again;
369 template<typename... _Args>
371 _M_insert_after(const_iterator __pos, _Args&&... __args)
373 _Fwd_list_node_base* __to
374 = const_cast<_Fwd_list_node_base* const>(__pos._M_node);
375 _Node* __thing = _M_create_node(std::forward<_Args>(__args)...);
376 __thing->_M_next = __to->_M_next;
377 __to->_M_next = __thing;
381 _M_put_node(_Node* __p)
382 { _M_get_Node_allocator().deallocate(__p, 1); }
385 _M_erase_after(_Fwd_list_node_base* __pos);
388 _M_erase_after(_Fwd_list_node_base* __pos,
389 _Fwd_list_node_base* __last);
393 * @brief A standard container with linear time access to elements,
394 * and fixed time insertion/deletion at any point in the sequence.
396 * @ingroup Containers
399 * Meets the requirements of a <a href="tables.html#65">container</a>, a
400 * <a href="tables.html#67">sequence</a>, including the
401 * <a href="tables.html#68">optional sequence requirements</a> with the
402 * %exception of @c at and @c operator[].
404 * This is a @e singly @e linked %list. Traversal up the
405 * %list requires linear time, but adding and removing elements (or
406 * @e nodes) is done in constant time, regardless of where the
407 * change takes place. Unlike std::vector and std::deque,
408 * random-access iterators are not provided, so subscripting ( @c
409 * [] ) access is not allowed. For algorithms which only need
410 * sequential access, this lack makes no difference.
412 * Also unlike the other standard containers, std::forward_list provides
413 * specialized algorithms %unique to linked lists, such as
414 * splicing, sorting, and in-place reversal.
416 * A couple points on memory allocation for forward_list<Tp>:
418 * First, we never actually allocate a Tp, we allocate
419 * Fwd_list_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
420 * that after elements from %forward_list<X,Alloc1> are spliced into
421 * %forward_list<X,Alloc2>, destroying the memory of the second %list is a
422 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
424 template<typename _Tp, typename _Alloc = allocator<_Tp> >
425 class forward_list : private _Fwd_list_base<_Tp, _Alloc>
428 typedef _Fwd_list_base<_Tp, _Alloc> _Base;
429 typedef _Fwd_list_node<_Tp> _Node;
433 typedef typename _Alloc::reference reference;
434 typedef typename _Alloc::const_reference const_reference;
435 typedef _Fwd_list_iterator<_Tp> iterator;
436 typedef _Fwd_list_const_iterator<_Tp> const_iterator;
437 typedef std::size_t size_type;
438 typedef std::ptrdiff_t difference_type;
439 typedef _Tp value_type;
440 typedef typename _Base::allocator_type allocator_type;
441 typedef typename _Alloc::pointer pointer;
442 typedef typename _Alloc::const_pointer const_pointer;
444 // 23.2.3.1 construct/copy/destroy:
447 * @brief Creates a %forward_list with no elements.
448 * @param al An allocator object.
451 forward_list(const _Alloc& __al = _Alloc())
456 * @brief Copy constructor with allocator argument.
457 * @param list Input list to copy.
458 * @param al An allocator object.
460 forward_list(const forward_list& __list, const _Alloc& __al)
461 : _Base(__list, __al)
465 * @brief Move constructor with allocator argument.
466 * @param list Input list to move.
467 * @param al An allocator object.
469 forward_list(forward_list&& __list, const _Alloc& __al)
470 : _Base(std::forward<_Base>(__list), __al)
474 * @brief Creates a %forward_list with copies of the default element
476 * @param n The number of elements to initially create.
478 * This constructor fills the %forward_list with @a n copies of
482 forward_list(size_type __n);
485 * @brief Creates a %forward_list with copies of an exemplar element.
486 * @param n The number of elements to initially create.
487 * @param value An element to copy.
488 * @param al An allocator object.
490 * This constructor fills the %forward_list with @a n copies of @a
493 forward_list(size_type __n, const _Tp& __value,
494 const _Alloc& __al = _Alloc());
497 * @brief Builds a %forward_list from a range.
498 * @param first An input iterator.
499 * @param last An input iterator.
500 * @param al An allocator object.
502 * Create a %forward_list consisting of copies of the elements from
503 * [@a first,@a last). This is linear in N (where N is
504 * distance(@a first,@a last)).
506 template<typename _InputIterator>
507 forward_list(_InputIterator __first, _InputIterator __last,
508 const _Alloc& __al = _Alloc());
511 * @brief The %forward_list copy constructor.
512 * @param list A %forward_list of identical element and allocator
515 * The newly-created %forward_list uses a copy of the allocation
516 * object used by @a list.
518 forward_list(const forward_list& __list);
521 * @brief The %forward_list move constructor.
522 * @param list A %forward_list of identical element and allocator
525 * The newly-created %forward_list contains the exact contents of @a
526 * forward_list. The contents of @a list are a valid, but unspecified
529 forward_list(forward_list&& __list)
530 : _Base(std::forward<_Base>(__list)) { }
533 * @brief Builds a %forward_list from an initializer_list
534 * @param il An initializer_list of value_type.
535 * @param al An allocator object.
537 * Create a %forward_list consisting of copies of the elements
538 * in the initializer_list @a il. This is linear in il.size().
540 forward_list(std::initializer_list<_Tp> __il,
541 const _Alloc& __al = _Alloc());
544 * @brief The forward_list dtor.
547 { _M_erase_after(&this->_M_impl._M_head, 0); }
550 * @brief The %forward_list assignment operator.
551 * @param list A %forward_list of identical element and allocator
554 * All the elements of @a list are copied, but unlike the copy
555 * constructor, the allocator object is not copied.
558 operator=(const forward_list& __list);
561 * @brief The %forward_list move assignment operator.
562 * @param list A %forward_list of identical element and allocator
565 * The contents of @a list are moved into this %forward_list
566 * (without copying). @a list is a valid, but unspecified
570 operator=(forward_list&& __list)
581 * @brief The %forward_list initializer list assignment operator.
582 * @param il An initializer_list of value_type.
584 * Replace the contents of the %forward_list with copies of the
585 * elements in the initializer_list @a il. This is linear in
589 operator=(std::initializer_list<_Tp> __il)
596 * @brief Assigns a range to a %forward_list.
597 * @param first An input iterator.
598 * @param last An input iterator.
600 * This function fills a %forward_list with copies of the elements
601 * in the range [@a first,@a last).
603 * Note that the assignment completely changes the %forward_list and
604 * that the resulting %forward_list's size is the same as the number
605 * of elements assigned. Old data may be lost.
607 template<typename _InputIterator>
609 assign(_InputIterator __first, _InputIterator __last)
612 insert_after(cbefore_begin(), __first, __last);
616 * @brief Assigns a given value to a %forward_list.
617 * @param n Number of elements to be assigned.
618 * @param val Value to be assigned.
620 * This function fills a %forward_list with @a n copies of the given
621 * value. Note that the assignment completely changes the
622 * %forward_list and that the resulting %forward_list's size is the
623 * same as the number of elements assigned. Old data may be lost.
626 assign(size_type __n, const _Tp& __val)
629 insert_after(cbefore_begin(), __n, __val);
633 * @brief Assigns an initializer_list to a %forward_list.
634 * @param il An initializer_list of value_type.
636 * Replace the contents of the %forward_list with copies of the
637 * elements in the initializer_list @a il. This is linear in
641 assign(std::initializer_list<_Tp> __il)
644 insert_after(cbefore_begin(), __il);
647 /// Get a copy of the memory allocation object.
649 get_allocator() const
650 { return _Base::get_allocator(); }
652 // 23.2.3.2 iterators:
655 * Returns a read/write iterator that points before the first element
656 * in the %forward_list. Iteration is done in ordinary element order.
660 { return iterator(&this->_M_impl._M_head); }
663 * Returns a read-only (constant) iterator that points before the
664 * first element in the %forward_list. Iteration is done in ordinary
669 { return const_iterator(&this->_M_impl._M_head); }
672 * Returns a read/write iterator that points to the first element
673 * in the %forward_list. Iteration is done in ordinary element order.
677 { return iterator(this->_M_impl._M_head._M_next); }
680 * Returns a read-only (constant) iterator that points to the first
681 * element in the %forward_list. Iteration is done in ordinary
686 { return const_iterator(this->_M_impl._M_head._M_next); }
689 * Returns a read/write iterator that points one past the last
690 * element in the %forward_list. Iteration is done in ordinary
695 { return iterator(0); }
698 * Returns a read-only iterator that points one past the last
699 * element in the %forward_list. Iteration is done in ordinary
704 { return const_iterator(0); }
707 * Returns a read-only (constant) iterator that points to the
708 * first element in the %forward_list. Iteration is done in ordinary
713 { return const_iterator(this->_M_impl._M_head._M_next); }
716 * Returns a read-only (constant) iterator that points before the
717 * first element in the %forward_list. Iteration is done in ordinary
721 cbefore_begin() const
722 { return const_iterator(&this->_M_impl._M_head); }
725 * Returns a read-only (constant) iterator that points one past
726 * the last element in the %forward_list. Iteration is done in
727 * ordinary element order.
731 { return const_iterator(0); }
734 * Returns true if the %forward_list is empty. (Thus begin() would
739 { return this->_M_impl._M_head._M_next == 0; }
742 * Returns the largest possible size of %forward_list.
746 { return this->_M_get_Node_allocator().max_size(); }
748 // 23.2.3.3 element access:
751 * Returns a read/write reference to the data at the first
752 * element of the %forward_list.
757 _Node* __front = static_cast<_Node*>(this->_M_impl._M_head._M_next);
758 return __front->_M_value;
762 * Returns a read-only (constant) reference to the data at the first
763 * element of the %forward_list.
768 _Node* __front = static_cast<_Node*>(this->_M_impl._M_head._M_next);
769 return __front->_M_value;
772 // 23.2.3.4 modifiers:
775 * @brief Constructs object in %forward_list at the front of the
777 * @param args Arguments.
779 * This function will insert an object of type Tp constructed
780 * with Tp(std::forward<Args>(args)...) at the front of the list
781 * Due to the nature of a %forward_list this operation can
782 * be done in constant time, and does not invalidate iterators
785 template<typename... _Args>
787 emplace_front(_Args&&... __args)
788 { _M_insert_after(cbefore_begin(), std::forward<_Args>(__args)...); }
791 * @brief Add data to the front of the %forward_list.
792 * @param val Data to be added.
794 * This is a typical stack operation. The function creates an
795 * element at the front of the %forward_list and assigns the given
796 * data to it. Due to the nature of a %forward_list this operation
797 * can be done in constant time, and does not invalidate iterators
801 push_front(const _Tp& __val)
802 { _M_insert_after(cbefore_begin(), __val); }
808 push_front(_Tp&& __val)
809 { _M_insert_after(cbefore_begin(), std::move(__val)); }
812 * @brief Removes first element.
814 * This is a typical stack operation. It shrinks the %forward_list
815 * by one. Due to the nature of a %forward_list this operation can
816 * be done in constant time, and only invalidates iterators/references
817 * to the element being removed.
819 * Note that no data is returned, and if the first element's data
820 * is needed, it should be retrieved before pop_front() is
825 { _M_erase_after(&this->_M_impl._M_head); }
828 * @brief Constructs object in %forward_list after the specified
830 * @param pos A const_iterator into the %forward_list.
831 * @param args Arguments.
832 * @return An iterator that points to the inserted data.
834 * This function will insert an object of type T constructed
835 * with T(std::forward<Args>(args)...) after the specified
836 * location. Due to the nature of a %forward_list this operation can
837 * be done in constant time, and does not invalidate iterators
840 template<typename... _Args>
842 emplace_after(const_iterator __pos, _Args&&... __args)
843 { _M_insert_after(__pos, std::forward<_Args>(__args)...); }
846 * @brief Inserts given value into %forward_list after specified
848 * @param pos An iterator into the %forward_list.
849 * @param val Data to be inserted.
850 * @return An iterator that points to the inserted data.
852 * This function will insert a copy of the given value after
853 * the specified location. Due to the nature of a %forward_list this
854 * operation can be done in constant time, and does not
855 * invalidate iterators and references.
858 insert_after(const_iterator __pos, const _Tp& __val)
860 _Fwd_list_node_base* __to
861 = const_cast<_Fwd_list_node_base* const>(__pos._M_node);
862 _Node* __thing = _M_create_node(__val);
863 __thing->_M_next = __to->_M_next;
864 __to->_M_next = __thing;
865 return iterator(__to->_M_next);
872 insert_after(const_iterator __pos, _Tp&& __val)
874 _Fwd_list_node_base* __to
875 = const_cast<_Fwd_list_node_base* const>(__pos._M_node);
876 _Node* __thing = _M_create_node(std::move(__val));
877 __thing->_M_next = __to->_M_next;
878 __to->_M_next = __thing;
879 return iterator(__to->_M_next);
883 * @brief Inserts a number of copies of given data into the
885 * @param pos An iterator into the %forward_list.
886 * @param n Number of elements to be inserted.
887 * @param val Data to be inserted.
889 * This function will insert a specified number of copies of the
890 * given data after the location specified by @a pos.
892 * This operation is linear in the number of elements inserted and
893 * does not invalidate iterators and references.
896 insert_after(const_iterator __pos, size_type __n,
900 * @brief Inserts a range into the %forward_list.
901 * @param position An iterator into the %forward_list.
902 * @param first An input iterator.
903 * @param last An input iterator.
905 * This function will insert copies of the data in the range [@a
906 * first,@a last) into the %forward_list after the location specified
909 * This operation is linear in the number of elements inserted and
910 * does not invalidate iterators and references.
912 template<typename _InputIterator>
914 insert_after(const_iterator __pos,
915 _InputIterator __first, _InputIterator __last);
918 * @brief Inserts the contents of an initializer_list into
919 * %forward_list after the specified iterator.
920 * @param pos An iterator into the %forward_list.
921 * @param il An initializer_list of value_type.
923 * This function will insert copies of the data in the
924 * initializer_list @a il into the %forward_list before the location
925 * specified by @a pos.
927 * This operation is linear in the number of elements inserted and
928 * does not invalidate iterators and references.
931 insert_after(const_iterator __pos, std::initializer_list<_Tp> __il);
934 * @brief Removes the element pointed to by the iterator following
936 * @param pos Iterator pointing to element to be erased.
937 * @return An iterator pointing to the next element (or end()).
939 * This function will erase the element at the given position and
940 * thus shorten the %forward_list by one.
942 * Due to the nature of a %forward_list this operation can be done
943 * in constant time, and only invalidates iterators/references to
944 * the element being removed. The user is also cautioned that
945 * this function only erases the element, and that if the element
946 * is itself a pointer, the pointed-to memory is not touched in
947 * any way. Managing the pointer is the user's responsibility.
950 erase_after(const_iterator __pos)
952 _Fwd_list_node_base* __tmp
953 = const_cast<_Fwd_list_node_base* const>(__pos._M_node);
955 return iterator(_Base::_M_erase_after(__tmp));
961 * @brief Remove a range of elements.
962 * @param pos Iterator pointing before the first element to be
964 * @param last Iterator pointing to one past the last element to be
966 * @return An iterator pointing to the element pointed to by @a last
967 * prior to erasing (or end()).
969 * This function will erase the elements in the range @a
970 * (pos,last) and shorten the %forward_list accordingly.
972 * This operation is linear time in the size of the range and only
973 * invalidates iterators/references to the element being removed.
974 * The user is also cautioned that this function only erases the
975 * elements, and that if the elements themselves are pointers, the
976 * pointed-to memory is not touched in any way. Managing the pointer
977 * is the user's responsibility.
980 erase_after(const_iterator __pos, iterator __last)
982 _Fwd_list_node_base* __tmp
983 = const_cast<_Fwd_list_node_base* const>(__pos._M_node);
984 return iterator(_M_erase_after(__tmp, __last._M_node));
988 * @brief Swaps data with another %forward_list.
989 * @param list A %forward_list of the same element and allocator
992 * This exchanges the elements between two lists in constant
993 * time. Note that the global std::swap() function is
994 * specialized such that std::swap(l1,l2) will feed to this
998 swap(forward_list&& __list)
999 { _Fwd_list_node_base::swap(this->_M_impl._M_head,
1000 __list._M_impl._M_head); }
1003 * @brief Resizes the %forward_list to the specified number of
1005 * @param sz Number of elements the %forward_list should contain.
1007 * This function will %resize the %forward_list to the specified
1008 * number of elements. If the number is smaller than the
1009 * %forward_list's current size the %forward_list is truncated,
1010 * otherwise the %forward_list is extended and new elements are
1011 * populated with given data.
1014 resize(size_type __sz)
1015 { resize(__sz, _Tp(0)); }
1018 * @brief Resizes the %forward_list to the specified number of
1020 * @param sz Number of elements the %forward_list should contain.
1021 * @param val Data with which new elements should be populated.
1023 * This function will %resize the %forward_list to the specified
1024 * number of elements. If the number is smaller than the
1025 * %forward_list's current size the %forward_list is truncated,
1026 * otherwise the %forward_list is extended and new elements are
1027 * populated with given data.
1030 resize(size_type __sz, value_type __val);
1033 * @brief Erases all the elements.
1035 * Note that this function only erases
1036 * the elements, and that if the elements themselves are
1037 * pointers, the pointed-to memory is not touched in any way.
1038 * Managing the pointer is the user's responsibility.
1042 { _M_erase_after(&this->_M_impl._M_head, 0); }
1044 // 23.2.3.5 forward_list operations:
1047 * @brief Insert contents of another %forward_list.
1048 * @param pos Iterator referencing the element to insert after.
1049 * @param list Source list.
1051 * The elements of @a list are inserted in constant time after
1052 * the element referenced by @a pos. @a list becomes an empty
1055 * Requires this != @a x.
1058 splice_after(const_iterator __pos, forward_list&& __list)
1060 if (!__list.empty() && &__list != this)
1062 _Fwd_list_node_base* __tmp
1063 = const_cast<_Fwd_list_node_base* const>(__pos._M_node);
1064 const_iterator __before = __list.cbefore_begin();
1065 __tmp->_M_transfer_after(const_cast<_Fwd_list_node_base* const>
1066 (__before._M_node));
1071 * @brief Insert element from another %forward_list.
1072 * @param pos Iterator referencing the element to insert after.
1073 * @param list Source list.
1074 * @param it Iterator referencing the element before the element
1077 * Removes the element in list @a list referenced by @a i and
1078 * inserts it into the current list after @a pos.
1081 splice_after(const_iterator __pos, forward_list&& __list,
1082 const_iterator __it)
1083 { this->splice_after(__pos, __list, __it, __it._M_next()); }
1086 * @brief Insert range from another %forward_list.
1087 * @param pos Iterator referencing the element to insert after.
1088 * @param list Source list.
1089 * @param before Iterator referencing before the start of range
1091 * @param last Iterator referencing the end of range in list.
1093 * Removes elements in the range (before,last) and inserts them
1094 * after @a pos in constant time.
1096 * Undefined if @a pos is in (before,last).
1099 splice_after(const_iterator __pos, forward_list&& __list,
1100 const_iterator __before, const_iterator __last)
1102 _Fwd_list_node_base* __tmp
1103 = const_cast<_Fwd_list_node_base* const>(__pos._M_node);
1104 __tmp->_M_transfer_after(const_cast<_Fwd_list_node_base* const>
1106 const_cast<_Fwd_list_node_base* const>
1111 * @brief Remove all elements equal to value.
1112 * @param val The value to remove.
1114 * Removes every element in the list equal to @a value.
1115 * Remaining elements stay in list order. Note that this
1116 * function only erases the elements, and that if the elements
1117 * themselves are pointers, the pointed-to memory is not
1118 * touched in any way. Managing the pointer is the user's
1122 remove(const _Tp& __val);
1125 * @brief Remove all elements satisfying a predicate.
1126 * @param pred Unary predicate function or object.
1128 * Removes every element in the list for which the predicate
1129 * returns true. Remaining elements stay in list order. Note
1130 * that this function only erases the elements, and that if the
1131 * elements themselves are pointers, the pointed-to memory is
1132 * not touched in any way. Managing the pointer is the user's
1135 template<typename _Pred>
1137 remove_if(_Pred __pred);
1140 * @brief Remove consecutive duplicate elements.
1142 * For each consecutive set of elements with the same value,
1143 * remove all but the first one. Remaining elements stay in
1144 * list order. Note that this function only erases the
1145 * elements, and that if the elements themselves are pointers,
1146 * the pointed-to memory is not touched in any way. Managing
1147 * the pointer is the user's responsibility.
1153 * @brief Remove consecutive elements satisfying a predicate.
1154 * @param binary_pred Binary predicate function or object.
1156 * For each consecutive set of elements [first,last) that
1157 * satisfy predicate(first,i) where i is an iterator in
1158 * [first,last), remove all but the first one. Remaining
1159 * elements stay in list order. Note that this function only
1160 * erases the elements, and that if the elements themselves are
1161 * pointers, the pointed-to memory is not touched in any way.
1162 * Managing the pointer is the user's responsibility.
1164 template<typename _BinPred>
1166 unique(_BinPred __binary_pred);
1169 * @brief Merge sorted lists.
1170 * @param list Sorted list to merge.
1172 * Assumes that both @a list and this list are sorted according to
1173 * operator<(). Merges elements of @a list into this list in
1174 * sorted order, leaving @a list empty when complete. Elements in
1175 * this list precede elements in @a list that are equal.
1178 merge(forward_list&& __list)
1179 { this->merge(__list, std::less<_Tp>()); }
1182 * @brief Merge sorted lists according to comparison function.
1183 * @param list Sorted list to merge.
1184 * @param comp Comparison function defining sort order.
1186 * Assumes that both @a list and this list are sorted according to
1187 * comp. Merges elements of @a list into this list
1188 * in sorted order, leaving @a list empty when complete. Elements
1189 * in this list precede elements in @a list that are equivalent
1190 * according to comp().
1192 template<typename _Comp>
1194 merge(forward_list&& __list, _Comp __comp);
1197 * @brief Sort the elements of the list.
1199 * Sorts the elements of this list in NlogN time. Equivalent
1200 * elements remain in list order.
1205 _Node* __tmp = static_cast<_Node*>(&this->_M_impl._M_head);
1206 __tmp->_M_sort_after(std::less<_Tp>());
1210 * @brief Sort the forward_list using a comparison function.
1212 * Sorts the elements of this list in NlogN time. Equivalent
1213 * elements remain in list order.
1215 template<typename _Comp>
1219 _Node* __tmp = static_cast<_Node*>(&this->_M_impl._M_head);
1220 __tmp->_M_sort_after(__comp);
1224 * @brief Reverse the elements in list.
1226 * Reverse the order of elements in the list in linear time.
1230 { this->_M_impl._M_head._M_reverse_after(); }
1234 * @brief Forward list equality comparison.
1235 * @param lx A %forward_list
1236 * @param ly A %forward_list of the same type as @a lx.
1237 * @return True iff the size and elements of the forward lists are equal.
1239 * This is an equivalence relation. It is linear in the size of the
1240 * forward lists. Deques are considered equivalent if corresponding
1241 * elements compare equal.
1243 template<typename _Tp, typename _Alloc>
1245 operator==(const forward_list<_Tp, _Alloc>& __lx,
1246 const forward_list<_Tp, _Alloc>& __ly)
1248 // We don't have size() so we need to walk through both lists
1249 // making sure both iterators are valid.
1250 typename std::forward_list<_Tp, _Alloc>::const_iterator __ix
1252 typename std::forward_list<_Tp, _Alloc>::const_iterator __iy
1254 while (__ix != __lx.cend() && __iy != __ly.cend())
1261 if (__ix == __lx.cend() && __iy == __ly.cend())
1268 * @brief Forward list ordering relation.
1269 * @param lx A %forward_list.
1270 * @param ly A %forward_list of the same type as @a lx.
1271 * @return True iff @a lx is lexicographically less than @a ly.
1273 * This is a total ordering relation. It is linear in the size of the
1274 * forward lists. The elements must be comparable with @c <.
1276 * See std::lexicographical_compare() for how the determination is made.
1278 template<typename _Tp, typename _Alloc>
1280 operator<(const forward_list<_Tp, _Alloc>& __lx,
1281 const forward_list<_Tp, _Alloc>& __ly)
1282 { return std::lexicographical_compare(__lx.cbegin(), __lx.cend(),
1283 __ly.cbegin(), __ly.cend()); }
1285 /// Based on operator==
1286 template<typename _Tp, typename _Alloc>
1288 operator!=(const forward_list<_Tp, _Alloc>& __lx,
1289 const forward_list<_Tp, _Alloc>& __ly)
1290 { return ! (__lx == __ly); }
1292 /// Based on operator<
1293 template<typename _Tp, typename _Alloc>
1295 operator>(const forward_list<_Tp, _Alloc>& __lx,
1296 const forward_list<_Tp, _Alloc>& __ly)
1297 { return (__ly < __lx); }
1299 /// Based on operator<
1300 template<typename _Tp, typename _Alloc>
1302 operator>=(const forward_list<_Tp, _Alloc>& __lx,
1303 const forward_list<_Tp, _Alloc>& __ly)
1304 { return ! (__lx < __ly); }
1306 /// Based on operator<
1307 template<typename _Tp, typename _Alloc>
1309 operator<=(const forward_list<_Tp, _Alloc>& __lx,
1310 const forward_list<_Tp, _Alloc>& __ly)
1311 { return ! (__ly < __lx); }
1313 /// See std::forward_list::forward_swap().
1314 template<typename _Tp, typename _Alloc>
1316 swap(forward_list<_Tp, _Alloc>& __lx,
1317 forward_list<_Tp, _Alloc>& __ly)
1318 { __lx.swap(__ly); }
1320 /// See std::forward_list::forward_swap().
1321 template<typename _Tp, typename _Alloc>
1323 swap(forward_list<_Tp, _Alloc>&& __lx,
1324 forward_list<_Tp, _Alloc>& __ly)
1325 { __lx.swap(__ly); }
1327 /// See std::forward_list::forward_swap().
1328 template<typename _Tp, typename _Alloc>
1330 swap(forward_list<_Tp, _Alloc>& __lx,
1331 forward_list<_Tp, _Alloc>&& __ly)
1332 { __lx.swap(__ly); }
1334 _GLIBCXX_END_NAMESPACE // namespace std
1336 #endif // __GXX_EXPERIMENTAL_CXX0X__
1338 #endif // _GLIBCXX_FORWARD_LIST_H