1 // <forward_list.h> -*- C++ -*-
3 // Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file forward_list.h
26 * This is a Standard C++ Library header.
29 #ifndef _FORWARD_LIST_H
30 #define _FORWARD_LIST_H 1
32 #pragma GCC system_header
35 #include <initializer_list>
37 _GLIBCXX_BEGIN_NAMESPACE(std
)
40 * @brief A helper basic node class for %forward_list.
41 * This is just a linked list with nothing inside it.
42 * There are purely list shuffling utility methods here.
44 struct _Fwd_list_node_base
46 _Fwd_list_node_base() : _M_next(0) { }
48 _Fwd_list_node_base
* _M_next
;
51 swap(_Fwd_list_node_base
& __x
, _Fwd_list_node_base
& __y
)
52 { std::swap(__x
._M_next
, __y
._M_next
); }
55 _M_transfer_after(_Fwd_list_node_base
* __begin
)
57 _Fwd_list_node_base
* __end
= __begin
;
58 while (__end
&& __end
->_M_next
)
59 __end
= __end
->_M_next
;
60 return _M_transfer_after(__begin
, __end
);
64 _M_transfer_after(_Fwd_list_node_base
* __begin
,
65 _Fwd_list_node_base
* __end
)
67 _Fwd_list_node_base
* __keep
= __begin
->_M_next
;
70 __begin
->_M_next
= __end
->_M_next
;
71 __end
->_M_next
= _M_next
;
82 _Fwd_list_node_base
* __tail
= _M_next
;
85 while (_Fwd_list_node_base
* __temp
= __tail
->_M_next
)
87 _Fwd_list_node_base
* __keep
= _M_next
;
89 __tail
->_M_next
= __temp
->_M_next
;
90 _M_next
->_M_next
= __keep
;
96 * @brief A helper node class for %forward_list.
97 * This is just a linked list with a data value in each node.
98 * There is a sorting utility method.
100 template<typename _Tp
>
101 struct _Fwd_list_node
102 : public _Fwd_list_node_base
104 template<typename
... _Args
>
105 _Fwd_list_node(_Args
&&... __args
)
106 : _Fwd_list_node_base(),
107 _M_value(std::forward
<_Args
>(__args
)...) { }
113 * @brief A forward_list::iterator.
115 * All the functions are op overloads.
117 template<typename _Tp
>
118 struct _Fwd_list_iterator
120 typedef _Fwd_list_iterator
<_Tp
> _Self
;
121 typedef _Fwd_list_node
<_Tp
> _Node
;
123 typedef _Tp value_type
;
124 typedef _Tp
* pointer
;
125 typedef _Tp
& reference
;
126 typedef ptrdiff_t difference_type
;
127 typedef std::forward_iterator_tag iterator_category
;
133 _Fwd_list_iterator(_Fwd_list_node_base
* __n
)
138 { return static_cast<_Node
*>(this->_M_node
)->_M_value
; }
142 { return std::__addressof(static_cast<_Node
*>
143 (this->_M_node
)->_M_value
); }
148 _M_node
= _M_node
->_M_next
;
156 _M_node
= _M_node
->_M_next
;
161 operator==(const _Self
& __x
) const
162 { return _M_node
== __x
._M_node
; }
165 operator!=(const _Self
& __x
) const
166 { return _M_node
!= __x
._M_node
; }
172 return _Fwd_list_iterator(_M_node
->_M_next
);
174 return _Fwd_list_iterator(0);
177 _Fwd_list_node_base
* _M_node
;
181 * @brief A forward_list::const_iterator.
183 * All the functions are op overloads.
185 template<typename _Tp
>
186 struct _Fwd_list_const_iterator
188 typedef _Fwd_list_const_iterator
<_Tp
> _Self
;
189 typedef const _Fwd_list_node
<_Tp
> _Node
;
190 typedef _Fwd_list_iterator
<_Tp
> iterator
;
192 typedef _Tp value_type
;
193 typedef const _Tp
* pointer
;
194 typedef const _Tp
& reference
;
195 typedef ptrdiff_t difference_type
;
196 typedef std::forward_iterator_tag iterator_category
;
198 _Fwd_list_const_iterator()
202 _Fwd_list_const_iterator(const _Fwd_list_node_base
* __n
)
205 _Fwd_list_const_iterator(const iterator
& __iter
)
206 : _M_node(__iter
._M_node
) { }
210 { return static_cast<_Node
*>(this->_M_node
)->_M_value
; }
214 { return std::__addressof(static_cast<_Node
*>
215 (this->_M_node
)->_M_value
); }
220 _M_node
= _M_node
->_M_next
;
228 _M_node
= _M_node
->_M_next
;
233 operator==(const _Self
& __x
) const
234 { return _M_node
== __x
._M_node
; }
237 operator!=(const _Self
& __x
) const
238 { return _M_node
!= __x
._M_node
; }
244 return _Fwd_list_const_iterator(_M_node
->_M_next
);
246 return _Fwd_list_const_iterator(0);
249 const _Fwd_list_node_base
* _M_node
;
253 * @brief Forward list iterator equality comparison.
255 template<typename _Tp
>
257 operator==(const _Fwd_list_iterator
<_Tp
>& __x
,
258 const _Fwd_list_const_iterator
<_Tp
>& __y
)
259 { return __x
._M_node
== __y
._M_node
; }
262 * @brief Forward list iterator inequality comparison.
264 template<typename _Tp
>
266 operator!=(const _Fwd_list_iterator
<_Tp
>& __x
,
267 const _Fwd_list_const_iterator
<_Tp
>& __y
)
268 { return __x
._M_node
!= __y
._M_node
; }
271 * @brief Base class for %forward_list.
273 template<typename _Tp
, typename _Alloc
>
274 struct _Fwd_list_base
277 typedef typename
_Alloc::template rebind
<_Tp
>::other _Tp_alloc_type
;
279 typedef typename
_Alloc::template
280 rebind
<_Fwd_list_node
<_Tp
>>::other _Node_alloc_type
;
282 struct _Fwd_list_impl
283 : public _Node_alloc_type
285 _Fwd_list_node_base _M_head
;
288 : _Node_alloc_type(), _M_head()
291 _Fwd_list_impl(const _Node_alloc_type
& __a
)
292 : _Node_alloc_type(__a
), _M_head()
296 _Fwd_list_impl _M_impl
;
299 typedef _Fwd_list_iterator
<_Tp
> iterator
;
300 typedef _Fwd_list_const_iterator
<_Tp
> const_iterator
;
301 typedef _Fwd_list_node
<_Tp
> _Node
;
304 _M_get_Node_allocator()
305 { return *static_cast<_Node_alloc_type
*>(&this->_M_impl
); }
307 const _Node_alloc_type
&
308 _M_get_Node_allocator() const
309 { return *static_cast<const _Node_alloc_type
*>(&this->_M_impl
); }
313 { this->_M_impl
._M_head
._M_next
= 0; }
315 _Fwd_list_base(const _Alloc
& __a
)
317 { this->_M_impl
._M_head
._M_next
= 0; }
319 _Fwd_list_base(const _Fwd_list_base
& __lst
, const _Alloc
& __a
);
321 _Fwd_list_base(_Fwd_list_base
&& __lst
, const _Alloc
& __a
)
323 { _Fwd_list_node_base::swap(this->_M_impl
._M_head
,
324 __lst
._M_impl
._M_head
); }
326 _Fwd_list_base(_Fwd_list_base
&& __lst
)
327 : _M_impl(__lst
._M_get_Node_allocator())
328 { _Fwd_list_node_base::swap(this->_M_impl
._M_head
,
329 __lst
._M_impl
._M_head
); }
332 { _M_erase_after(&_M_impl
._M_head
, 0); }
338 { return _M_get_Node_allocator().allocate(1); }
340 template<typename
... _Args
>
342 _M_create_node(_Args
&&... __args
)
344 _Node
* __node
= this->_M_get_node();
347 _M_get_Node_allocator().construct(__node
,
348 std::forward
<_Args
>(__args
)...);
353 this->_M_put_node(__node
);
354 __throw_exception_again
;
359 template<typename
... _Args
>
361 _M_insert_after(const_iterator __pos
, _Args
&&... __args
);
364 _M_put_node(_Node
* __p
)
365 { _M_get_Node_allocator().deallocate(__p
, 1); }
368 _M_erase_after(_Fwd_list_node_base
* __pos
);
371 _M_erase_after(_Fwd_list_node_base
* __pos
,
372 _Fwd_list_node_base
* __last
);
376 * @brief A standard container with linear time access to elements,
377 * and fixed time insertion/deletion at any point in the sequence.
381 * Meets the requirements of a <a href="tables.html#65">container</a>, a
382 * <a href="tables.html#67">sequence</a>, including the
383 * <a href="tables.html#68">optional sequence requirements</a> with the
384 * %exception of @c at and @c operator[].
386 * This is a @e singly @e linked %list. Traversal up the
387 * %list requires linear time, but adding and removing elements (or
388 * @e nodes) is done in constant time, regardless of where the
389 * change takes place. Unlike std::vector and std::deque,
390 * random-access iterators are not provided, so subscripting ( @c
391 * [] ) access is not allowed. For algorithms which only need
392 * sequential access, this lack makes no difference.
394 * Also unlike the other standard containers, std::forward_list provides
395 * specialized algorithms %unique to linked lists, such as
396 * splicing, sorting, and in-place reversal.
398 * A couple points on memory allocation for forward_list<Tp>:
400 * First, we never actually allocate a Tp, we allocate
401 * Fwd_list_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
402 * that after elements from %forward_list<X,Alloc1> are spliced into
403 * %forward_list<X,Alloc2>, destroying the memory of the second %list is a
404 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
406 template<typename _Tp
, typename _Alloc
= allocator
<_Tp
> >
407 class forward_list
: private _Fwd_list_base
<_Tp
, _Alloc
>
410 typedef _Fwd_list_base
<_Tp
, _Alloc
> _Base
;
411 typedef _Fwd_list_node
<_Tp
> _Node
;
412 typedef _Fwd_list_node_base _Node_base
;
413 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
417 typedef _Tp value_type
;
418 typedef typename
_Tp_alloc_type::pointer pointer
;
419 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
420 typedef typename
_Tp_alloc_type::reference reference
;
421 typedef typename
_Tp_alloc_type::const_reference const_reference
;
423 typedef _Fwd_list_iterator
<_Tp
> iterator
;
424 typedef _Fwd_list_const_iterator
<_Tp
> const_iterator
;
425 typedef std::size_t size_type
;
426 typedef std::ptrdiff_t difference_type
;
427 typedef _Alloc allocator_type
;
429 // 23.2.3.1 construct/copy/destroy:
432 * @brief Creates a %forward_list with no elements.
433 * @param al An allocator object.
436 forward_list(const _Alloc
& __al
= _Alloc())
441 * @brief Copy constructor with allocator argument.
442 * @param list Input list to copy.
443 * @param al An allocator object.
445 forward_list(const forward_list
& __list
, const _Alloc
& __al
)
446 : _Base(__list
, __al
)
450 * @brief Move constructor with allocator argument.
451 * @param list Input list to move.
452 * @param al An allocator object.
454 forward_list(forward_list
&& __list
, const _Alloc
& __al
)
455 : _Base(std::forward
<_Base
>(__list
), __al
)
459 * @brief Creates a %forward_list with default constructed elements.
460 * @param n The number of elements to initially create.
462 * This constructor creates the %forward_list with @a n default
463 * constructed elements.
466 forward_list(size_type __n
)
468 { _M_default_initialize(__n
); }
471 * @brief Creates a %forward_list with copies of an exemplar element.
472 * @param n The number of elements to initially create.
473 * @param value An element to copy.
474 * @param al An allocator object.
476 * This constructor fills the %forward_list with @a n copies of @a
479 forward_list(size_type __n
, const _Tp
& __value
,
480 const _Alloc
& __al
= _Alloc())
482 { _M_fill_initialize(__n
, __value
); }
485 * @brief Builds a %forward_list from a range.
486 * @param first An input iterator.
487 * @param last An input iterator.
488 * @param al An allocator object.
490 * Create a %forward_list consisting of copies of the elements from
491 * [@a first,@a last). This is linear in N (where N is
492 * distance(@a first,@a last)).
494 template<typename _InputIterator
>
495 forward_list(_InputIterator __first
, _InputIterator __last
,
496 const _Alloc
& __al
= _Alloc())
499 // Check whether it's an integral type. If so, it's not an iterator.
500 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
501 _M_initialize_dispatch(__first
, __last
, _Integral());
505 * @brief The %forward_list copy constructor.
506 * @param list A %forward_list of identical element and allocator
509 * The newly-created %forward_list uses a copy of the allocation
510 * object used by @a list.
512 forward_list(const forward_list
& __list
)
513 : _Base(__list
._M_get_Node_allocator())
514 { _M_initialize_dispatch(__list
.begin(), __list
.end(), __false_type()); }
517 * @brief The %forward_list move constructor.
518 * @param list A %forward_list of identical element and allocator
521 * The newly-created %forward_list contains the exact contents of @a
522 * forward_list. The contents of @a list are a valid, but unspecified
525 forward_list(forward_list
&& __list
)
526 : _Base(std::forward
<_Base
>(__list
)) { }
529 * @brief Builds a %forward_list from an initializer_list
530 * @param il An initializer_list of value_type.
531 * @param al An allocator object.
533 * Create a %forward_list consisting of copies of the elements
534 * in the initializer_list @a il. This is linear in il.size().
536 forward_list(std::initializer_list
<_Tp
> __il
,
537 const _Alloc
& __al
= _Alloc())
539 { _M_initialize_dispatch(__il
.begin(), __il
.end(), __false_type()); }
542 * @brief The forward_list dtor.
548 * @brief The %forward_list assignment operator.
549 * @param list A %forward_list of identical element and allocator
552 * All the elements of @a list are copied, but unlike the copy
553 * constructor, the allocator object is not copied.
556 operator=(const forward_list
& __list
);
559 * @brief The %forward_list move assignment operator.
560 * @param list A %forward_list of identical element and allocator
563 * The contents of @a list are moved into this %forward_list
564 * (without copying). @a list is a valid, but unspecified
568 operator=(forward_list
&& __list
)
578 * @brief The %forward_list initializer list assignment operator.
579 * @param il An initializer_list of value_type.
581 * Replace the contents of the %forward_list with copies of the
582 * elements in the initializer_list @a il. This is linear in
586 operator=(std::initializer_list
<_Tp
> __il
)
593 * @brief Assigns a range to a %forward_list.
594 * @param first An input iterator.
595 * @param last An input iterator.
597 * This function fills a %forward_list with copies of the elements
598 * in the range [@a first,@a last).
600 * Note that the assignment completely changes the %forward_list and
601 * that the resulting %forward_list's size is the same as the number
602 * of elements assigned. Old data may be lost.
604 template<typename _InputIterator
>
606 assign(_InputIterator __first
, _InputIterator __last
)
609 insert_after(cbefore_begin(), __first
, __last
);
613 * @brief Assigns a given value to a %forward_list.
614 * @param n Number of elements to be assigned.
615 * @param val Value to be assigned.
617 * This function fills a %forward_list with @a n copies of the given
618 * value. Note that the assignment completely changes the
619 * %forward_list and that the resulting %forward_list's size is the
620 * same as the number of elements assigned. Old data may be lost.
623 assign(size_type __n
, const _Tp
& __val
)
626 insert_after(cbefore_begin(), __n
, __val
);
630 * @brief Assigns an initializer_list to a %forward_list.
631 * @param il An initializer_list of value_type.
633 * Replace the contents of the %forward_list with copies of the
634 * elements in the initializer_list @a il. This is linear in
638 assign(std::initializer_list
<_Tp
> __il
)
641 insert_after(cbefore_begin(), __il
);
644 /// Get a copy of the memory allocation object.
646 get_allocator() const
647 { return this->_M_get_Node_allocator(); }
649 // 23.2.3.2 iterators:
652 * Returns a read/write iterator that points before the first element
653 * in the %forward_list. Iteration is done in ordinary element order.
657 { return iterator(&this->_M_impl
._M_head
); }
660 * Returns a read-only (constant) iterator that points before the
661 * first element in the %forward_list. Iteration is done in ordinary
666 { return const_iterator(&this->_M_impl
._M_head
); }
669 * Returns a read/write iterator that points to the first element
670 * in the %forward_list. Iteration is done in ordinary element order.
674 { return iterator(this->_M_impl
._M_head
._M_next
); }
677 * Returns a read-only (constant) iterator that points to the first
678 * element in the %forward_list. Iteration is done in ordinary
683 { return const_iterator(this->_M_impl
._M_head
._M_next
); }
686 * Returns a read/write iterator that points one past the last
687 * element in the %forward_list. Iteration is done in ordinary
692 { return iterator(0); }
695 * Returns a read-only iterator that points one past the last
696 * element in the %forward_list. Iteration is done in ordinary
701 { return const_iterator(0); }
704 * Returns a read-only (constant) iterator that points to the
705 * first element in the %forward_list. Iteration is done in ordinary
710 { return const_iterator(this->_M_impl
._M_head
._M_next
); }
713 * Returns a read-only (constant) iterator that points before the
714 * first element in the %forward_list. Iteration is done in ordinary
718 cbefore_begin() const
719 { return const_iterator(&this->_M_impl
._M_head
); }
722 * Returns a read-only (constant) iterator that points one past
723 * the last element in the %forward_list. Iteration is done in
724 * ordinary element order.
728 { return const_iterator(0); }
731 * Returns true if the %forward_list is empty. (Thus begin() would
736 { return this->_M_impl
._M_head
._M_next
== 0; }
739 * Returns the largest possible size of %forward_list.
743 { return this->_M_get_Node_allocator().max_size(); }
745 // 23.2.3.3 element access:
748 * Returns a read/write reference to the data at the first
749 * element of the %forward_list.
754 _Node
* __front
= static_cast<_Node
*>(this->_M_impl
._M_head
._M_next
);
755 return __front
->_M_value
;
759 * Returns a read-only (constant) reference to the data at the first
760 * element of the %forward_list.
765 _Node
* __front
= static_cast<_Node
*>(this->_M_impl
._M_head
._M_next
);
766 return __front
->_M_value
;
769 // 23.2.3.4 modifiers:
772 * @brief Constructs object in %forward_list at the front of the
774 * @param args Arguments.
776 * This function will insert an object of type Tp constructed
777 * with Tp(std::forward<Args>(args)...) at the front of the list
778 * Due to the nature of a %forward_list this operation can
779 * be done in constant time, and does not invalidate iterators
782 template<typename
... _Args
>
784 emplace_front(_Args
&&... __args
)
785 { this->_M_insert_after(cbefore_begin(),
786 std::forward
<_Args
>(__args
)...); }
789 * @brief Add data to the front of the %forward_list.
790 * @param val Data to be added.
792 * This is a typical stack operation. The function creates an
793 * element at the front of the %forward_list and assigns the given
794 * data to it. Due to the nature of a %forward_list this operation
795 * can be done in constant time, and does not invalidate iterators
799 push_front(const _Tp
& __val
)
800 { this->_M_insert_after(cbefore_begin(), __val
); }
806 push_front(_Tp
&& __val
)
807 { this->_M_insert_after(cbefore_begin(), std::move(__val
)); }
810 * @brief Removes first element.
812 * This is a typical stack operation. It shrinks the %forward_list
813 * by one. Due to the nature of a %forward_list this operation can
814 * be done in constant time, and only invalidates iterators/references
815 * to the element being removed.
817 * Note that no data is returned, and if the first element's data
818 * is needed, it should be retrieved before pop_front() is
823 { this->_M_erase_after(&this->_M_impl
._M_head
); }
826 * @brief Constructs object in %forward_list after the specified
828 * @param pos A const_iterator into the %forward_list.
829 * @param args Arguments.
830 * @return An iterator that points to the inserted data.
832 * This function will insert an object of type T constructed
833 * with T(std::forward<Args>(args)...) after the specified
834 * location. Due to the nature of a %forward_list this operation can
835 * be done in constant time, and does not invalidate iterators
838 template<typename
... _Args
>
840 emplace_after(const_iterator __pos
, _Args
&&... __args
)
841 { return iterator(this->_M_insert_after(__pos
,
842 std::forward
<_Args
>(__args
)...)); }
845 * @brief Inserts given value into %forward_list after specified
847 * @param pos An iterator into the %forward_list.
848 * @param val Data to be inserted.
849 * @return An iterator that points to the inserted data.
851 * This function will insert a copy of the given value after
852 * the specified location. Due to the nature of a %forward_list this
853 * operation can be done in constant time, and does not
854 * invalidate iterators and references.
857 insert_after(const_iterator __pos
, const _Tp
& __val
)
858 { return iterator(this->_M_insert_after(__pos
, __val
)); }
864 insert_after(const_iterator __pos
, _Tp
&& __val
)
865 { return iterator(this->_M_insert_after(__pos
, std::move(__val
))); }
868 * @brief Inserts a number of copies of given data into the
870 * @param pos An iterator into the %forward_list.
871 * @param n Number of elements to be inserted.
872 * @param val Data to be inserted.
873 * @return An iterator pointing to the last inserted copy of
874 * @a val or @a pos if @a n == 0.
876 * This function will insert a specified number of copies of the
877 * given data after the location specified by @a pos.
879 * This operation is linear in the number of elements inserted and
880 * does not invalidate iterators and references.
883 insert_after(const_iterator __pos
, size_type __n
, const _Tp
& __val
);
886 * @brief Inserts a range into the %forward_list.
887 * @param position An iterator into the %forward_list.
888 * @param first An input iterator.
889 * @param last An input iterator.
890 * @return An iterator pointing to the last inserted element or
891 * @a pos if @a first == @a last.
893 * This function will insert copies of the data in the range [@a
894 * first,@a last) into the %forward_list after the location specified
897 * This operation is linear in the number of elements inserted and
898 * does not invalidate iterators and references.
900 template<typename _InputIterator
>
902 insert_after(const_iterator __pos
,
903 _InputIterator __first
, _InputIterator __last
);
906 * @brief Inserts the contents of an initializer_list into
907 * %forward_list after the specified iterator.
908 * @param pos An iterator into the %forward_list.
909 * @param il An initializer_list of value_type.
910 * @return An iterator pointing to the last inserted element
911 * or @a pos if @a il is empty.
913 * This function will insert copies of the data in the
914 * initializer_list @a il into the %forward_list before the location
915 * specified by @a pos.
917 * This operation is linear in the number of elements inserted and
918 * does not invalidate iterators and references.
921 insert_after(const_iterator __pos
, std::initializer_list
<_Tp
> __il
);
924 * @brief Removes the element pointed to by the iterator following
926 * @param pos Iterator pointing before element to be erased.
928 * This function will erase the element at the given position and
929 * thus shorten the %forward_list by one.
931 * Due to the nature of a %forward_list this operation can be done
932 * in constant time, and only invalidates iterators/references to
933 * the element being removed. The user is also cautioned that
934 * this function only erases the element, and that if the element
935 * is itself a pointer, the pointed-to memory is not touched in
936 * any way. Managing the pointer is the user's responsibility.
939 erase_after(const_iterator __pos
)
940 { this->_M_erase_after(const_cast<_Node_base
*>(__pos
._M_node
)); }
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
)
961 { this->_M_erase_after(const_cast<_Node_base
*>(__pos
._M_node
),
962 const_cast<_Node_base
*>(__last
._M_node
)); }
965 * @brief Swaps data with another %forward_list.
966 * @param list A %forward_list of the same element and allocator
969 * This exchanges the elements between two lists in constant
970 * time. Note that the global std::swap() function is
971 * specialized such that std::swap(l1,l2) will feed to this
975 swap(forward_list
& __list
)
976 { _Node_base::swap(this->_M_impl
._M_head
, __list
._M_impl
._M_head
); }
979 * @brief Resizes the %forward_list to the specified number of
981 * @param sz Number of elements the %forward_list should contain.
983 * This function will %resize the %forward_list to the specified
984 * number of elements. If the number is smaller than the
985 * %forward_list's current size the %forward_list is truncated,
986 * otherwise the %forward_list is extended and the new elements
987 * are default constructed.
990 resize(size_type __sz
);
993 * @brief Resizes the %forward_list to the specified number of
995 * @param sz Number of elements the %forward_list should contain.
996 * @param val Data with which new elements should be populated.
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 size the %forward_list is truncated,
1001 * otherwise the %forward_list is extended and new elements are
1002 * populated with given data.
1005 resize(size_type __sz
, value_type __val
);
1008 * @brief Erases all the elements.
1010 * Note that this function only erases
1011 * the elements, and that if the elements themselves are
1012 * pointers, the pointed-to memory is not touched in any way.
1013 * Managing the pointer is the user's responsibility.
1017 { this->_M_erase_after(&this->_M_impl
._M_head
, 0); }
1019 // 23.2.3.5 forward_list operations:
1022 * @brief Insert contents of another %forward_list.
1023 * @param pos Iterator referencing the element to insert after.
1024 * @param list Source list.
1026 * The elements of @a list are inserted in constant time after
1027 * the element referenced by @a pos. @a list becomes an empty
1030 * Requires this != @a x.
1033 splice_after(const_iterator __pos
, forward_list
&& __list
)
1035 if (!__list
.empty())
1036 _M_splice_after(__pos
, std::move(__list
));
1040 * @brief Insert element from another %forward_list.
1041 * @param pos Iterator referencing the element to insert after.
1042 * @param list Source list.
1043 * @param i Iterator referencing the element before the element
1046 * Removes the element in list @a list referenced by @a i and
1047 * inserts it into the current list after @a pos.
1050 splice_after(const_iterator __pos
, forward_list
&& __list
,
1053 const_iterator __j
= __i
;
1055 if (__pos
== __i
|| __pos
== __j
)
1058 splice_after(__pos
, std::move(__list
), __i
, __j
);
1062 * @brief Insert range from another %forward_list.
1063 * @param pos Iterator referencing the element to insert after.
1064 * @param list Source list.
1065 * @param before Iterator referencing before the start of range
1067 * @param last Iterator referencing the end of range in list.
1069 * Removes elements in the range (before,last) and inserts them
1070 * after @a pos in constant time.
1072 * Undefined if @a pos is in (before,last).
1075 splice_after(const_iterator __pos
, forward_list
&& __list
,
1076 const_iterator __before
, const_iterator __last
);
1079 * @brief Remove all elements equal to value.
1080 * @param val The value to remove.
1082 * Removes every element in the list equal to @a value.
1083 * Remaining elements stay in list order. Note that this
1084 * function only erases the elements, and that if the elements
1085 * themselves are pointers, the pointed-to memory is not
1086 * touched in any way. Managing the pointer is the user's
1090 remove(const _Tp
& __val
);
1093 * @brief Remove all elements satisfying a predicate.
1094 * @param pred Unary predicate function or object.
1096 * Removes every element in the list for which the predicate
1097 * returns true. Remaining elements stay in list order. Note
1098 * that this function only erases the elements, and that if the
1099 * elements themselves are pointers, the pointed-to memory is
1100 * not touched in any way. Managing the pointer is the user's
1103 template<typename _Pred
>
1105 remove_if(_Pred __pred
);
1108 * @brief Remove consecutive duplicate elements.
1110 * For each consecutive set of elements with the same value,
1111 * remove all but the first one. Remaining elements stay in
1112 * list order. Note that this function only erases the
1113 * elements, and that if the elements themselves are pointers,
1114 * the pointed-to memory is not touched in any way. Managing
1115 * the pointer is the user's responsibility.
1119 { this->unique(std::equal_to
<_Tp
>()); }
1122 * @brief Remove consecutive elements satisfying a predicate.
1123 * @param binary_pred Binary predicate function or object.
1125 * For each consecutive set of elements [first,last) that
1126 * satisfy predicate(first,i) where i is an iterator in
1127 * [first,last), remove all but the first one. Remaining
1128 * elements stay in list order. Note that this function only
1129 * erases the elements, and that if the elements themselves are
1130 * pointers, the pointed-to memory is not touched in any way.
1131 * Managing the pointer is the user's responsibility.
1133 template<typename _BinPred
>
1135 unique(_BinPred __binary_pred
);
1138 * @brief Merge sorted lists.
1139 * @param list Sorted list to merge.
1141 * Assumes that both @a list and this list are sorted according to
1142 * operator<(). Merges elements of @a list into this list in
1143 * sorted order, leaving @a list empty when complete. Elements in
1144 * this list precede elements in @a list that are equal.
1147 merge(forward_list
&& __list
)
1148 { this->merge(std::move(__list
), std::less
<_Tp
>()); }
1151 * @brief Merge sorted lists according to comparison function.
1152 * @param list Sorted list to merge.
1153 * @param comp Comparison function defining sort order.
1155 * Assumes that both @a list and this list are sorted according to
1156 * comp. Merges elements of @a list into this list
1157 * in sorted order, leaving @a list empty when complete. Elements
1158 * in this list precede elements in @a list that are equivalent
1159 * according to comp().
1161 template<typename _Comp
>
1163 merge(forward_list
&& __list
, _Comp __comp
);
1166 * @brief Sort the elements of the list.
1168 * Sorts the elements of this list in NlogN time. Equivalent
1169 * elements remain in list order.
1173 { this->sort(std::less
<_Tp
>()); }
1176 * @brief Sort the forward_list using a comparison function.
1178 * Sorts the elements of this list in NlogN time. Equivalent
1179 * elements remain in list order.
1181 template<typename _Comp
>
1186 * @brief Reverse the elements in list.
1188 * Reverse the order of elements in the list in linear time.
1192 { this->_M_impl
._M_head
._M_reverse_after(); }
1195 template<typename _Integer
>
1197 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1198 { _M_fill_initialize(static_cast<size_type
>(__n
), __x
); }
1200 // Called by the range constructor to implement [23.1.1]/9
1201 template<typename _InputIterator
>
1203 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1206 // Called by forward_list(n,v,a), and the range constructor when it
1207 // turns out to be the same thing.
1209 _M_fill_initialize(size_type __n
, const value_type
& __value
);
1211 // Called by splice_after and insert_after.
1213 _M_splice_after(const_iterator __pos
, forward_list
&& __list
);
1215 // Called by forward_list(n).
1217 _M_default_initialize(size_type __n
);
1219 // Called by resize(sz).
1221 _M_default_insert_after(const_iterator __pos
, size_type __n
);
1225 * @brief Forward list equality comparison.
1226 * @param lx A %forward_list
1227 * @param ly A %forward_list of the same type as @a lx.
1228 * @return True iff the size and elements of the forward lists are equal.
1230 * This is an equivalence relation. It is linear in the size of the
1231 * forward lists. Deques are considered equivalent if corresponding
1232 * elements compare equal.
1234 template<typename _Tp
, typename _Alloc
>
1236 operator==(const forward_list
<_Tp
, _Alloc
>& __lx
,
1237 const forward_list
<_Tp
, _Alloc
>& __ly
);
1240 * @brief Forward list ordering relation.
1241 * @param lx A %forward_list.
1242 * @param ly A %forward_list of the same type as @a lx.
1243 * @return True iff @a lx is lexicographically less than @a ly.
1245 * This is a total ordering relation. It is linear in the size of the
1246 * forward lists. The elements must be comparable with @c <.
1248 * See std::lexicographical_compare() for how the determination is made.
1250 template<typename _Tp
, typename _Alloc
>
1252 operator<(const forward_list
<_Tp
, _Alloc
>& __lx
,
1253 const forward_list
<_Tp
, _Alloc
>& __ly
)
1254 { return std::lexicographical_compare(__lx
.cbegin(), __lx
.cend(),
1255 __ly
.cbegin(), __ly
.cend()); }
1257 /// Based on operator==
1258 template<typename _Tp
, typename _Alloc
>
1260 operator!=(const forward_list
<_Tp
, _Alloc
>& __lx
,
1261 const forward_list
<_Tp
, _Alloc
>& __ly
)
1262 { return !(__lx
== __ly
); }
1264 /// Based on operator<
1265 template<typename _Tp
, typename _Alloc
>
1267 operator>(const forward_list
<_Tp
, _Alloc
>& __lx
,
1268 const forward_list
<_Tp
, _Alloc
>& __ly
)
1269 { return (__ly
< __lx
); }
1271 /// Based on operator<
1272 template<typename _Tp
, typename _Alloc
>
1274 operator>=(const forward_list
<_Tp
, _Alloc
>& __lx
,
1275 const forward_list
<_Tp
, _Alloc
>& __ly
)
1276 { return !(__lx
< __ly
); }
1278 /// Based on operator<
1279 template<typename _Tp
, typename _Alloc
>
1281 operator<=(const forward_list
<_Tp
, _Alloc
>& __lx
,
1282 const forward_list
<_Tp
, _Alloc
>& __ly
)
1283 { return !(__ly
< __lx
); }
1285 /// See std::forward_list::swap().
1286 template<typename _Tp
, typename _Alloc
>
1288 swap(forward_list
<_Tp
, _Alloc
>& __lx
,
1289 forward_list
<_Tp
, _Alloc
>& __ly
)
1290 { __lx
.swap(__ly
); }
1292 _GLIBCXX_END_NAMESPACE
// namespace std
1294 #endif // _FORWARD_LIST_H