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>
38 _GLIBCXX_BEGIN_NAMESPACE(std)
40 using __gnu_cxx::__static_pointer_cast;
41 using __gnu_cxx::__const_pointer_cast;
44 * @brief A helper basic node class for %forward_list.
45 * This is just a linked list with nothing inside it.
46 * There are purely list shuffling utility methods here.
48 template<typename _Alloc>
49 struct _Fwd_list_node_base
51 // The type allocated by _Alloc cannot be this type, so we rebind
52 typedef typename _Alloc::template rebind<_Fwd_list_node_base<_Alloc> >
53 ::other::pointer _Pointer;
54 typedef typename _Alloc::template rebind<_Fwd_list_node_base<_Alloc> >
55 ::other::const_pointer _Const_pointer;
59 _Fwd_list_node_base() : _M_next(0) { }
62 swap(_Fwd_list_node_base& __x, _Fwd_list_node_base& __y)
63 { std::swap(__x._M_next, __y._M_next); }
66 _M_transfer_after(_Pointer __bbegin);
69 _M_transfer_after(_Pointer __bbegin, _Pointer __bend);
76 * @brief A helper node class for %forward_list.
77 * This is just a linked list with a data value in each node.
78 * There is a sorting utility method.
80 template<typename _Tp, typename _Alloc>
81 struct _Fwd_list_node : public _Fwd_list_node_base<_Alloc>
83 typedef typename _Alloc::template rebind<_Fwd_list_node<_Tp, _Alloc> >
84 ::other::pointer _Pointer;
86 template<typename... _Args>
87 _Fwd_list_node(_Args&&... __args)
88 : _Fwd_list_node_base<_Alloc>(),
89 _M_value(std::forward<_Args>(__args)...) { }
95 * @brief A forward_list::iterator.
97 * All the functions are op overloads.
99 template<typename _Tp, typename _Alloc>
100 struct _Fwd_list_iterator
102 typedef _Fwd_list_iterator<_Tp, _Alloc> _Self;
103 typedef _Fwd_list_node<_Tp, _Alloc> _Node;
104 typedef _Fwd_list_node_base<_Alloc> _Node_base;
106 typedef _Tp value_type;
107 typedef typename _Alloc::pointer pointer;
108 typedef typename _Alloc::reference reference;
109 typedef typename _Alloc::difference_type difference_type;
110 typedef std::forward_iterator_tag iterator_category;
112 _Fwd_list_iterator() : _M_node() { }
115 _Fwd_list_iterator(typename _Node_base::_Pointer __n)
120 { return __static_pointer_cast<_Node*>(_M_node)->_M_value; }
124 { return &__static_pointer_cast<_Node*>(_M_node)->_M_value; }
129 _M_node = _M_node->_M_next;
137 _M_node = _M_node->_M_next;
142 operator==(const _Self& __x) const
143 { return _M_node == __x._M_node; }
146 operator!=(const _Self& __x) const
147 { return _M_node != __x._M_node; }
153 return _Fwd_list_iterator(_M_node->_M_next);
155 return _Fwd_list_iterator(0);
158 typename _Node_base::_Pointer _M_node;
162 * @brief A forward_list::const_iterator.
164 * All the functions are op overloads.
166 template<typename _Tp, typename _Alloc>
167 struct _Fwd_list_const_iterator
169 typedef _Fwd_list_const_iterator<_Tp, _Alloc> _Self;
170 typedef const _Fwd_list_node<_Tp, _Alloc> _Node;
171 typedef const _Fwd_list_node_base<_Alloc> _Node_base;
172 typedef _Fwd_list_iterator<_Tp, _Alloc> iterator;
174 typedef _Tp value_type;
175 typedef typename _Alloc::const_pointer pointer;
176 typedef typename _Alloc::const_reference reference;
177 typedef typename _Alloc::difference_type difference_type;
178 typedef std::forward_iterator_tag iterator_category;
180 _Fwd_list_const_iterator() : _M_node() { }
183 _Fwd_list_const_iterator(typename _Node_base::_Const_pointer __n)
186 _Fwd_list_const_iterator(const iterator& __iter)
187 : _M_node(__iter._M_node) { }
191 { return __static_pointer_cast<_Node*>(_M_node)->_M_value; }
195 { return &__static_pointer_cast<_Node*>(_M_node)->_M_value; }
200 _M_node = _M_node->_M_next;
208 _M_node = _M_node->_M_next;
213 operator==(const _Self& __x) const
214 { return _M_node == __x._M_node; }
217 operator!=(const _Self& __x) const
218 { return _M_node != __x._M_node; }
224 return _Fwd_list_const_iterator(_M_node->_M_next);
226 return _Fwd_list_const_iterator(0);
229 typename _Node_base::_Const_pointer _M_node;
233 * @brief Forward list iterator equality comparison.
235 template<typename _Tp, typename _Alloc>
237 operator==(const _Fwd_list_iterator<_Tp, _Alloc>& __x,
238 const _Fwd_list_const_iterator<_Tp, _Alloc>& __y)
239 { return __x._M_node == __y._M_node; }
242 * @brief Forward list iterator inequality comparison.
244 template<typename _Tp, typename _Alloc>
246 operator!=(const _Fwd_list_iterator<_Tp, _Alloc>& __x,
247 const _Fwd_list_const_iterator<_Tp, _Alloc>& __y)
248 { return __x._M_node != __y._M_node; }
251 * @brief Base class for %forward_list.
253 template<typename _Tp, typename _Alloc>
254 struct _Fwd_list_base
257 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
259 typedef typename _Alloc::template
260 rebind<_Fwd_list_node<_Tp, _Tp_alloc_type>>::other _Node_alloc_type;
262 struct _Fwd_list_impl
263 : public _Node_alloc_type
265 _Fwd_list_node_base<_Tp_alloc_type> _M_head;
268 : _Node_alloc_type(), _M_head()
271 _Fwd_list_impl(const _Node_alloc_type& __a)
272 : _Node_alloc_type(__a), _M_head()
276 _Fwd_list_impl _M_impl;
279 typedef _Fwd_list_iterator<_Tp, _Tp_alloc_type> iterator;
280 typedef _Fwd_list_const_iterator<_Tp, _Tp_alloc_type> const_iterator;
282 typedef _Fwd_list_node<_Tp, _Tp_alloc_type> _Node;
283 typedef _Fwd_list_node_base<_Tp_alloc_type> _Node_base;
286 _M_get_Node_allocator()
287 { return *static_cast<_Node_alloc_type*>(&this->_M_impl); }
289 const _Node_alloc_type&
290 _M_get_Node_allocator() const
291 { return *static_cast<const _Node_alloc_type*>(&this->_M_impl); }
295 { this->_M_impl._M_head._M_next = 0; }
297 _Fwd_list_base(const _Alloc& __a)
299 { this->_M_impl._M_head._M_next = 0; }
301 _Fwd_list_base(const _Fwd_list_base& __lst, const _Alloc& __a);
303 _Fwd_list_base(_Fwd_list_base&& __lst, const _Alloc& __a)
305 { _Node_base::swap(this->_M_impl._M_head,
306 __lst._M_impl._M_head); }
308 _Fwd_list_base(_Fwd_list_base&& __lst)
309 : _M_impl(__lst._M_get_Node_allocator())
310 { _Node_base::swap(this->_M_impl._M_head,
311 __lst._M_impl._M_head); }
314 { _M_erase_after(&_M_impl._M_head, 0); }
318 typename _Node::_Pointer
320 { return _M_get_Node_allocator().allocate(1); }
322 template<typename... _Args>
323 typename _Node::_Pointer
324 _M_create_node(_Args&&... __args)
326 typename _Node::_Pointer __node = this->_M_get_node();
329 _M_get_Node_allocator().construct(__node,
330 std::forward<_Args>(__args)...);
335 this->_M_put_node(__node);
336 __throw_exception_again;
341 template<typename... _Args>
342 typename _Node_base::_Pointer
343 _M_insert_after(const_iterator __pos, _Args&&... __args);
346 _M_put_node(typename _Node::_Pointer __p)
347 { _M_get_Node_allocator().deallocate(__p, 1); }
350 _M_erase_after(typename _Node_base::_Pointer __pos);
353 _M_erase_after(typename _Node_base::_Pointer __pos,
354 typename _Node_base::_Pointer __last);
358 * @brief A standard container with linear time access to elements,
359 * and fixed time insertion/deletion at any point in the sequence.
363 * Meets the requirements of a <a href="tables.html#65">container</a>, a
364 * <a href="tables.html#67">sequence</a>, including the
365 * <a href="tables.html#68">optional sequence requirements</a> with the
366 * %exception of @c at and @c operator[].
368 * This is a @e singly @e linked %list. Traversal up the
369 * %list requires linear time, but adding and removing elements (or
370 * @e nodes) is done in constant time, regardless of where the
371 * change takes place. Unlike std::vector and std::deque,
372 * random-access iterators are not provided, so subscripting ( @c
373 * [] ) access is not allowed. For algorithms which only need
374 * sequential access, this lack makes no difference.
376 * Also unlike the other standard containers, std::forward_list provides
377 * specialized algorithms %unique to linked lists, such as
378 * splicing, sorting, and in-place reversal.
380 * A couple points on memory allocation for forward_list<Tp>:
382 * First, we never actually allocate a Tp, we allocate
383 * Fwd_list_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
384 * that after elements from %forward_list<X,Alloc1> are spliced into
385 * %forward_list<X,Alloc2>, destroying the memory of the second %list is a
386 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
388 template<typename _Tp, typename _Alloc = allocator<_Tp> >
389 class forward_list : private _Fwd_list_base<_Tp, _Alloc>
392 typedef _Fwd_list_base<_Tp, _Alloc> _Base;
393 typedef typename _Base::_Node _Node;
394 typedef typename _Base::_Node_base _Node_base;
395 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
399 typedef _Tp value_type;
400 typedef typename _Tp_alloc_type::pointer pointer;
401 typedef typename _Tp_alloc_type::const_pointer const_pointer;
402 typedef typename _Tp_alloc_type::reference reference;
403 typedef typename _Tp_alloc_type::const_reference const_reference;
405 typedef typename _Base::iterator iterator;
406 typedef typename _Base::const_iterator const_iterator;
407 typedef std::size_t size_type;
408 typedef std::ptrdiff_t difference_type;
409 typedef _Alloc allocator_type;
411 // 23.2.3.1 construct/copy/destroy:
414 * @brief Creates a %forward_list with no elements.
415 * @param al An allocator object.
418 forward_list(const _Alloc& __al = _Alloc())
423 * @brief Copy constructor with allocator argument.
424 * @param list Input list to copy.
425 * @param al An allocator object.
427 forward_list(const forward_list& __list, const _Alloc& __al)
428 : _Base(__list, __al)
432 * @brief Move constructor with allocator argument.
433 * @param list Input list to move.
434 * @param al An allocator object.
436 forward_list(forward_list&& __list, const _Alloc& __al)
437 : _Base(std::forward<_Base>(__list), __al)
441 * @brief Creates a %forward_list with default constructed elements.
442 * @param n The number of elements to initially create.
444 * This constructor creates the %forward_list with @a n default
445 * constructed elements.
448 forward_list(size_type __n);
451 * @brief Creates a %forward_list with copies of an exemplar element.
452 * @param n The number of elements to initially create.
453 * @param value An element to copy.
454 * @param al An allocator object.
456 * This constructor fills the %forward_list with @a n copies of @a
459 forward_list(size_type __n, const _Tp& __value,
460 const _Alloc& __al = _Alloc())
462 { _M_fill_initialize(__n, __value); }
465 * @brief Builds a %forward_list from a range.
466 * @param first An input iterator.
467 * @param last An input iterator.
468 * @param al An allocator object.
470 * Create a %forward_list consisting of copies of the elements from
471 * [@a first,@a last). This is linear in N (where N is
472 * distance(@a first,@a last)).
474 template<typename _InputIterator>
475 forward_list(_InputIterator __first, _InputIterator __last,
476 const _Alloc& __al = _Alloc())
479 // Check whether it's an integral type. If so, it's not an iterator.
480 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
481 _M_initialize_dispatch(__first, __last, _Integral());
485 * @brief The %forward_list copy constructor.
486 * @param list A %forward_list of identical element and allocator
489 * The newly-created %forward_list uses a copy of the allocation
490 * object used by @a list.
492 forward_list(const forward_list& __list)
493 : _Base(__list._M_get_Node_allocator())
494 { _M_initialize_dispatch(__list.begin(), __list.end(), __false_type()); }
497 * @brief The %forward_list move constructor.
498 * @param list A %forward_list of identical element and allocator
501 * The newly-created %forward_list contains the exact contents of @a
502 * forward_list. The contents of @a list are a valid, but unspecified
505 forward_list(forward_list&& __list)
506 : _Base(std::forward<_Base>(__list)) { }
509 * @brief Builds a %forward_list from an initializer_list
510 * @param il An initializer_list of value_type.
511 * @param al An allocator object.
513 * Create a %forward_list consisting of copies of the elements
514 * in the initializer_list @a il. This is linear in il.size().
516 forward_list(std::initializer_list<_Tp> __il,
517 const _Alloc& __al = _Alloc())
519 { _M_initialize_dispatch(__il.begin(), __il.end(), __false_type()); }
522 * @brief The forward_list dtor.
528 * @brief The %forward_list assignment operator.
529 * @param list A %forward_list of identical element and allocator
532 * All the elements of @a list are copied, but unlike the copy
533 * constructor, the allocator object is not copied.
536 operator=(const forward_list& __list);
539 * @brief The %forward_list move assignment operator.
540 * @param list A %forward_list of identical element and allocator
543 * The contents of @a list are moved into this %forward_list
544 * (without copying). @a list is a valid, but unspecified
548 operator=(forward_list&& __list)
558 * @brief The %forward_list initializer list assignment operator.
559 * @param il An initializer_list of value_type.
561 * Replace the contents of the %forward_list with copies of the
562 * elements in the initializer_list @a il. This is linear in
566 operator=(std::initializer_list<_Tp> __il)
573 * @brief Assigns a range to a %forward_list.
574 * @param first An input iterator.
575 * @param last An input iterator.
577 * This function fills a %forward_list with copies of the elements
578 * in the range [@a first,@a last).
580 * Note that the assignment completely changes the %forward_list and
581 * that the resulting %forward_list's size is the same as the number
582 * of elements assigned. Old data may be lost.
584 template<typename _InputIterator>
586 assign(_InputIterator __first, _InputIterator __last)
589 insert_after(cbefore_begin(), __first, __last);
593 * @brief Assigns a given value to a %forward_list.
594 * @param n Number of elements to be assigned.
595 * @param val Value to be assigned.
597 * This function fills a %forward_list with @a n copies of the given
598 * value. Note that the assignment completely changes the
599 * %forward_list and that the resulting %forward_list's size is the
600 * same as the number of elements assigned. Old data may be lost.
603 assign(size_type __n, const _Tp& __val)
606 insert_after(cbefore_begin(), __n, __val);
610 * @brief Assigns an initializer_list to a %forward_list.
611 * @param il An initializer_list of value_type.
613 * Replace the contents of the %forward_list with copies of the
614 * elements in the initializer_list @a il. This is linear in
618 assign(std::initializer_list<_Tp> __il)
621 insert_after(cbefore_begin(), __il);
624 /// Get a copy of the memory allocation object.
626 get_allocator() const
627 { return this->_M_get_Node_allocator(); }
629 // 23.2.3.2 iterators:
632 * Returns a read/write iterator that points before the first element
633 * in the %forward_list. Iteration is done in ordinary element order.
637 { return iterator(&this->_M_impl._M_head); }
640 * Returns a read-only (constant) iterator that points before the
641 * first element in the %forward_list. Iteration is done in ordinary
646 { return const_iterator(&this->_M_impl._M_head); }
649 * Returns a read/write iterator that points to the first element
650 * in the %forward_list. Iteration is done in ordinary element order.
654 { return iterator(this->_M_impl._M_head._M_next); }
657 * Returns a read-only (constant) iterator that points to the first
658 * element in the %forward_list. Iteration is done in ordinary
663 { return const_iterator(this->_M_impl._M_head._M_next); }
666 * Returns a read/write iterator that points one past the last
667 * element in the %forward_list. Iteration is done in ordinary
672 { return iterator(0); }
675 * Returns a read-only iterator that points one past the last
676 * element in the %forward_list. Iteration is done in ordinary
681 { return const_iterator(0); }
684 * Returns a read-only (constant) iterator that points to the
685 * first element in the %forward_list. Iteration is done in ordinary
690 { return const_iterator(this->_M_impl._M_head._M_next); }
693 * Returns a read-only (constant) iterator that points before the
694 * first element in the %forward_list. Iteration is done in ordinary
698 cbefore_begin() const
699 { return const_iterator(&this->_M_impl._M_head); }
702 * Returns a read-only (constant) iterator that points one past
703 * the last element in the %forward_list. Iteration is done in
704 * ordinary element order.
708 { return const_iterator(0); }
711 * Returns true if the %forward_list is empty. (Thus begin() would
716 { return this->_M_impl._M_head._M_next == 0; }
719 * Returns the largest possible size of %forward_list.
723 { return this->_M_get_Node_allocator().max_size(); }
725 // 23.2.3.3 element access:
728 * Returns a read/write reference to the data at the first
729 * element of the %forward_list.
735 __static_pointer_cast<_Node*>(this->_M_impl._M_head._M_next);
736 return __front->_M_value;
740 * Returns a read-only (constant) reference to the data at the first
741 * element of the %forward_list.
747 __static_pointer_cast<_Node*>(this->_M_impl._M_head._M_next);
748 return __front->_M_value;
751 // 23.2.3.4 modifiers:
754 * @brief Constructs object in %forward_list at the front of the
756 * @param args Arguments.
758 * This function will insert an object of type Tp constructed
759 * with Tp(std::forward<Args>(args)...) at the front of the list
760 * Due to the nature of a %forward_list this operation can
761 * be done in constant time, and does not invalidate iterators
764 template<typename... _Args>
766 emplace_front(_Args&&... __args)
767 { this->_M_insert_after(cbefore_begin(),
768 std::forward<_Args>(__args)...); }
771 * @brief Add data to the front of the %forward_list.
772 * @param val Data to be added.
774 * This is a typical stack operation. The function creates an
775 * element at the front of the %forward_list and assigns the given
776 * data to it. Due to the nature of a %forward_list this operation
777 * can be done in constant time, and does not invalidate iterators
781 push_front(const _Tp& __val)
782 { this->_M_insert_after(cbefore_begin(), __val); }
788 push_front(_Tp&& __val)
789 { this->_M_insert_after(cbefore_begin(), std::move(__val)); }
792 * @brief Removes first element.
794 * This is a typical stack operation. It shrinks the %forward_list
795 * by one. Due to the nature of a %forward_list this operation can
796 * be done in constant time, and only invalidates iterators/references
797 * to the element being removed.
799 * Note that no data is returned, and if the first element's data
800 * is needed, it should be retrieved before pop_front() is
805 { this->_M_erase_after(&this->_M_impl._M_head); }
808 * @brief Constructs object in %forward_list after the specified
810 * @param pos A const_iterator into the %forward_list.
811 * @param args Arguments.
812 * @return An iterator that points to the inserted data.
814 * This function will insert an object of type T constructed
815 * with T(std::forward<Args>(args)...) after the specified
816 * location. Due to the nature of a %forward_list this operation can
817 * be done in constant time, and does not invalidate iterators
820 template<typename... _Args>
822 emplace_after(const_iterator __pos, _Args&&... __args)
823 { return iterator(this->_M_insert_after(__pos,
824 std::forward<_Args>(__args)...)); }
827 * @brief Inserts given value into %forward_list after specified
829 * @param pos An iterator into the %forward_list.
830 * @param val Data to be inserted.
831 * @return An iterator that points to the inserted data.
833 * This function will insert a copy of the given value after
834 * the specified location. Due to the nature of a %forward_list this
835 * operation can be done in constant time, and does not
836 * invalidate iterators and references.
839 insert_after(const_iterator __pos, const _Tp& __val)
840 { return iterator(this->_M_insert_after(__pos, __val)); }
846 insert_after(const_iterator __pos, _Tp&& __val)
847 { return iterator(this->_M_insert_after(__pos, std::move(__val))); }
850 * @brief Inserts a number of copies of given data into the
852 * @param pos An iterator into the %forward_list.
853 * @param n Number of elements to be inserted.
854 * @param val Data to be inserted.
857 * This function will insert a specified number of copies of the
858 * given data after the location specified by @a pos.
860 * This operation is linear in the number of elements inserted and
861 * does not invalidate iterators and references.
864 insert_after(const_iterator __pos, size_type __n, const _Tp& __val)
866 forward_list __tmp(__n, __val, this->_M_get_Node_allocator());
867 splice_after(__pos, std::move(__tmp));
868 return iterator(__const_pointer_cast<typename _Node_base::_Pointer>
873 * @brief Inserts a range into the %forward_list.
874 * @param position An iterator into the %forward_list.
875 * @param first An input iterator.
876 * @param last An input iterator.
879 * This function will insert copies of the data in the range [@a
880 * first,@a last) into the %forward_list after the location specified
883 * This operation is linear in the number of elements inserted and
884 * does not invalidate iterators and references.
886 template<typename _InputIterator>
888 insert_after(const_iterator __pos,
889 _InputIterator __first, _InputIterator __last)
891 forward_list __tmp(__first, __last, this->_M_get_Node_allocator());
892 splice_after(__pos, std::move(__tmp));
893 return iterator(__const_pointer_cast<typename _Node_base::_Pointer>
898 * @brief Inserts the contents of an initializer_list into
899 * %forward_list after the specified iterator.
900 * @param pos An iterator into the %forward_list.
901 * @param il An initializer_list of value_type.
904 * This function will insert copies of the data in the
905 * initializer_list @a il into the %forward_list before the location
906 * specified by @a pos.
908 * This operation is linear in the number of elements inserted and
909 * does not invalidate iterators and references.
912 insert_after(const_iterator __pos, std::initializer_list<_Tp> __il)
914 forward_list __tmp(__il, this->_M_get_Node_allocator());
915 splice_after(__pos, std::move(__tmp));
916 return iterator(__const_pointer_cast<typename _Node_base::_Pointer>
921 * @brief Removes the element pointed to by the iterator following
923 * @param pos Iterator pointing before element to be erased.
925 * This function will erase the element at the given position and
926 * thus shorten the %forward_list by one.
928 * Due to the nature of a %forward_list this operation can be done
929 * in constant time, and only invalidates iterators/references to
930 * the element being removed. The user is also cautioned that
931 * this function only erases the element, and that if the element
932 * is itself a pointer, the pointed-to memory is not touched in
933 * any way. Managing the pointer is the user's responsibility.
936 erase_after(const_iterator __pos)
938 _Node_base* __tmp = __const_pointer_cast<_Node_base*>(__pos._M_node);
939 this->_M_erase_after(__tmp);
943 * @brief Remove a range of elements.
944 * @param pos Iterator pointing before the first element to be
946 * @param last Iterator pointing to one past the last element to be
949 * This function will erase the elements in the range @a
950 * (pos,last) and shorten the %forward_list accordingly.
952 * This operation is linear time in the size of the range and only
953 * invalidates iterators/references to the element being removed.
954 * The user is also cautioned that this function only erases the
955 * elements, and that if the elements themselves are pointers, the
956 * pointed-to memory is not touched in any way. Managing the pointer
957 * is the user's responsibility.
960 erase_after(const_iterator __pos, const_iterator __last)
962 _Node_base* __tmpp = __const_pointer_cast<_Node_base*>(__pos._M_node);
963 _Node_base* __tmpl = __const_pointer_cast<_Node_base*>(__last._M_node);
964 this->_M_erase_after(__tmpp, __tmpl);
968 * @brief Swaps data with another %forward_list.
969 * @param list A %forward_list of the same element and allocator
972 * This exchanges the elements between two lists in constant
973 * time. Note that the global std::swap() function is
974 * specialized such that std::swap(l1,l2) will feed to this
978 swap(forward_list& __list)
979 { _Node_base::swap(this->_M_impl._M_head, __list._M_impl._M_head); }
982 * @brief Resizes the %forward_list to the specified number of
984 * @param sz Number of elements the %forward_list should contain.
986 * This function will %resize the %forward_list to the specified
987 * number of elements. If the number is smaller than the
988 * %forward_list's current size the %forward_list is truncated,
989 * otherwise the %forward_list is extended and the new elements
990 * are default constructed.
993 resize(size_type __sz);
996 * @brief Resizes the %forward_list to the specified number of
998 * @param sz Number of elements the %forward_list should contain.
999 * @param val Data with which new elements should be populated.
1001 * This function will %resize the %forward_list to the specified
1002 * number of elements. If the number is smaller than the
1003 * %forward_list's current size the %forward_list is truncated,
1004 * otherwise the %forward_list is extended and new elements are
1005 * populated with given data.
1008 resize(size_type __sz, value_type __val);
1011 * @brief Erases all the elements.
1013 * Note that this function only erases
1014 * the elements, and that if the elements themselves are
1015 * pointers, the pointed-to memory is not touched in any way.
1016 * Managing the pointer is the user's responsibility.
1020 { this->_M_erase_after(&this->_M_impl._M_head, 0); }
1022 // 23.2.3.5 forward_list operations:
1025 * @brief Insert contents of another %forward_list.
1026 * @param pos Iterator referencing the element to insert after.
1027 * @param list Source list.
1029 * The elements of @a list are inserted in constant time after
1030 * the element referenced by @a pos. @a list becomes an empty
1033 * Requires this != @a x.
1036 splice_after(const_iterator __pos, forward_list&& __list);
1039 * @brief Insert element from another %forward_list.
1040 * @param pos Iterator referencing the element to insert after.
1041 * @param list Source list.
1042 * @param i Iterator referencing the element before the element
1045 * Removes the element in list @a list referenced by @a i and
1046 * inserts it into the current list after @a pos.
1049 splice_after(const_iterator __pos, forward_list&& __list,
1052 const_iterator __j = __i;
1054 if (__pos == __i || __pos == __j)
1057 splice_after(__pos, std::move(__list), __i, __j);
1061 * @brief Insert range from another %forward_list.
1062 * @param pos Iterator referencing the element to insert after.
1063 * @param list Source list.
1064 * @param before Iterator referencing before the start of range
1066 * @param last Iterator referencing the end of range in list.
1068 * Removes elements in the range (before,last) and inserts them
1069 * after @a pos in constant time.
1071 * Undefined if @a pos is in (before,last).
1074 splice_after(const_iterator __pos, forward_list&& __list,
1075 const_iterator __before, const_iterator __last);
1078 * @brief Remove all elements equal to value.
1079 * @param val The value to remove.
1081 * Removes every element in the list equal to @a value.
1082 * Remaining elements stay in list order. Note that this
1083 * function only erases the elements, and that if the elements
1084 * themselves are pointers, the pointed-to memory is not
1085 * touched in any way. Managing the pointer is the user's
1089 remove(const _Tp& __val);
1092 * @brief Remove all elements satisfying a predicate.
1093 * @param pred Unary predicate function or object.
1095 * Removes every element in the list for which the predicate
1096 * returns true. Remaining elements stay in list order. Note
1097 * that this function only erases the elements, and that if the
1098 * elements themselves are pointers, the pointed-to memory is
1099 * not touched in any way. Managing the pointer is the user's
1102 template<typename _Pred>
1104 remove_if(_Pred __pred);
1107 * @brief Remove consecutive duplicate elements.
1109 * For each consecutive set of elements with the same value,
1110 * remove all but the first one. Remaining elements stay in
1111 * list order. Note that this function only erases the
1112 * elements, and that if the elements themselves are pointers,
1113 * the pointed-to memory is not touched in any way. Managing
1114 * the pointer is the user's responsibility.
1118 { this->unique(std::equal_to<_Tp>()); }
1121 * @brief Remove consecutive elements satisfying a predicate.
1122 * @param binary_pred Binary predicate function or object.
1124 * For each consecutive set of elements [first,last) that
1125 * satisfy predicate(first,i) where i is an iterator in
1126 * [first,last), remove all but the first one. Remaining
1127 * elements stay in list order. Note that this function only
1128 * erases the elements, and that if the elements themselves are
1129 * pointers, the pointed-to memory is not touched in any way.
1130 * Managing the pointer is the user's responsibility.
1132 template<typename _BinPred>
1134 unique(_BinPred __binary_pred);
1137 * @brief Merge sorted lists.
1138 * @param list Sorted list to merge.
1140 * Assumes that both @a list and this list are sorted according to
1141 * operator<(). Merges elements of @a list into this list in
1142 * sorted order, leaving @a list empty when complete. Elements in
1143 * this list precede elements in @a list that are equal.
1146 merge(forward_list&& __list)
1147 { this->merge(std::move(__list), std::less<_Tp>()); }
1150 * @brief Merge sorted lists according to comparison function.
1151 * @param list Sorted list to merge.
1152 * @param comp Comparison function defining sort order.
1154 * Assumes that both @a list and this list are sorted according to
1155 * comp. Merges elements of @a list into this list
1156 * in sorted order, leaving @a list empty when complete. Elements
1157 * in this list precede elements in @a list that are equivalent
1158 * according to comp().
1160 template<typename _Comp>
1162 merge(forward_list&& __list, _Comp __comp);
1165 * @brief Sort the elements of the list.
1167 * Sorts the elements of this list in NlogN time. Equivalent
1168 * elements remain in list order.
1172 { this->sort(std::less<_Tp>()); }
1175 * @brief Sort the forward_list using a comparison function.
1177 * Sorts the elements of this list in NlogN time. Equivalent
1178 * elements remain in list order.
1180 template<typename _Comp>
1185 * @brief Reverse the elements in list.
1187 * Reverse the order of elements in the list in linear time.
1191 { this->_M_impl._M_head._M_reverse_after(); }
1194 template<typename _Integer>
1196 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1197 { _M_fill_initialize(static_cast<size_type>(__n), __x); }
1199 // Called by the range constructor to implement [23.1.1]/9
1200 template<typename _InputIterator>
1202 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1205 // Called by forward_list(n,v,a), and the range constructor when it
1206 // turns out to be the same thing.
1208 _M_fill_initialize(size_type __n, const value_type& __value);
1212 * @brief Forward list equality comparison.
1213 * @param lx A %forward_list
1214 * @param ly A %forward_list of the same type as @a lx.
1215 * @return True iff the size and elements of the forward lists are equal.
1217 * This is an equivalence relation. It is linear in the size of the
1218 * forward lists. Deques are considered equivalent if corresponding
1219 * elements compare equal.
1221 template<typename _Tp, typename _Alloc>
1223 operator==(const forward_list<_Tp, _Alloc>& __lx,
1224 const forward_list<_Tp, _Alloc>& __ly);
1227 * @brief Forward list ordering relation.
1228 * @param lx A %forward_list.
1229 * @param ly A %forward_list of the same type as @a lx.
1230 * @return True iff @a lx is lexicographically less than @a ly.
1232 * This is a total ordering relation. It is linear in the size of the
1233 * forward lists. The elements must be comparable with @c <.
1235 * See std::lexicographical_compare() for how the determination is made.
1237 template<typename _Tp, typename _Alloc>
1239 operator<(const forward_list<_Tp, _Alloc>& __lx,
1240 const forward_list<_Tp, _Alloc>& __ly)
1241 { return std::lexicographical_compare(__lx.cbegin(), __lx.cend(),
1242 __ly.cbegin(), __ly.cend()); }
1244 /// Based on operator==
1245 template<typename _Tp, typename _Alloc>
1247 operator!=(const forward_list<_Tp, _Alloc>& __lx,
1248 const forward_list<_Tp, _Alloc>& __ly)
1249 { return !(__lx == __ly); }
1251 /// Based on operator<
1252 template<typename _Tp, typename _Alloc>
1254 operator>(const forward_list<_Tp, _Alloc>& __lx,
1255 const forward_list<_Tp, _Alloc>& __ly)
1256 { return (__ly < __lx); }
1258 /// Based on operator<
1259 template<typename _Tp, typename _Alloc>
1261 operator>=(const forward_list<_Tp, _Alloc>& __lx,
1262 const forward_list<_Tp, _Alloc>& __ly)
1263 { return !(__lx < __ly); }
1265 /// Based on operator<
1266 template<typename _Tp, typename _Alloc>
1268 operator<=(const forward_list<_Tp, _Alloc>& __lx,
1269 const forward_list<_Tp, _Alloc>& __ly)
1270 { return !(__ly < __lx); }
1272 /// See std::forward_list::swap().
1273 template<typename _Tp, typename _Alloc>
1275 swap(forward_list<_Tp, _Alloc>& __lx,
1276 forward_list<_Tp, _Alloc>& __ly)
1277 { __lx.swap(__ly); }
1279 _GLIBCXX_END_NAMESPACE // namespace std
1281 #endif // _FORWARD_LIST_H