1 // <forward_list.h> -*- C++ -*-
3 // Copyright (C) 2008, 2009 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 _FORWARD_LIST_H
35 #define _FORWARD_LIST_H 1
37 #pragma GCC system_header
39 #ifndef __GXX_EXPERIMENTAL_CXX0X__
40 # include <c++0x_warning.h>
44 #include <initializer_list>
47 _GLIBCXX_BEGIN_NAMESPACE(std
)
49 using __gnu_cxx::__static_pointer_cast
;
50 using __gnu_cxx::__const_pointer_cast
;
53 * @brief A helper basic node class for %forward_list.
54 * This is just a linked list with nothing inside it.
55 * There are purely list shuffling utility methods here.
57 template<typename _Alloc
>
58 struct _Fwd_list_node_base
60 // The type allocated by _Alloc cannot be this type, so we rebind
61 typedef typename
_Alloc::template rebind
<_Fwd_list_node_base
<_Alloc
> >
62 ::other::pointer _Pointer
;
63 typedef typename
_Alloc::template rebind
<_Fwd_list_node_base
<_Alloc
> >
64 ::other::const_pointer _Const_pointer
;
68 _Fwd_list_node_base() : _M_next(0) { }
71 swap(_Fwd_list_node_base
& __x
, _Fwd_list_node_base
& __y
)
72 { std::swap(__x
._M_next
, __y
._M_next
); }
75 _M_transfer_after(_Pointer __bbegin
);
78 _M_transfer_after(_Pointer __bbegin
, _Pointer __bend
);
85 * @brief A helper node class for %forward_list.
86 * This is just a linked list with a data value in each node.
87 * There is a sorting utility method.
89 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
90 struct _Fwd_list_node
: public _Fwd_list_node_base
<_Alloc
>
92 typedef typename
_Alloc::template rebind
<_Fwd_list_node
<_Tp
, _Alloc
> >
93 ::other::pointer _Pointer
;
95 template<typename
... _Args
>
96 _Fwd_list_node(_Args
&&... __args
)
97 : _Fwd_list_node_base
<_Alloc
>(),
98 _M_value(std::forward
<_Args
>(__args
)...) { }
100 template<typename _Comp
>
102 _M_sort_after(_Comp __comp
);
108 * @brief A forward_list::iterator.
110 * All the functions are op overloads.
112 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
113 struct _Fwd_list_iterator
115 typedef _Fwd_list_iterator
<_Tp
, _Alloc
> _Self
;
116 typedef _Fwd_list_node
<_Tp
, _Alloc
> _Node
;
117 typedef _Fwd_list_node_base
<_Alloc
> _Node_base
;
119 typedef _Tp value_type
;
120 typedef typename
_Alloc::pointer pointer
;
121 typedef typename
_Alloc::reference reference
;
122 typedef typename
_Alloc::difference_type difference_type
;
123 typedef std::forward_iterator_tag iterator_category
;
125 _Fwd_list_iterator() : _M_node() { }
128 _Fwd_list_iterator(typename
_Node_base::_Pointer __n
)
133 { return __static_pointer_cast
<_Node
*>(_M_node
)->_M_value
; }
137 { return &__static_pointer_cast
<_Node
*>(_M_node
)->_M_value
; }
142 _M_node
= _M_node
->_M_next
;
150 _M_node
= _M_node
->_M_next
;
155 operator==(const _Self
& __x
) const
156 { return _M_node
== __x
._M_node
; }
159 operator!=(const _Self
& __x
) const
160 { return _M_node
!= __x
._M_node
; }
166 return _Fwd_list_iterator(_M_node
->_M_next
);
168 return _Fwd_list_iterator(0);
171 typename
_Node_base::_Pointer _M_node
;
175 * @brief A forward_list::const_iterator.
177 * All the functions are op overloads.
179 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
180 struct _Fwd_list_const_iterator
182 typedef _Fwd_list_const_iterator
<_Tp
, _Alloc
> _Self
;
183 typedef const _Fwd_list_node
<_Tp
, _Alloc
> _Node
;
184 typedef const _Fwd_list_node_base
<_Alloc
> _Node_base
;
185 typedef _Fwd_list_iterator
<_Tp
, _Alloc
> iterator
;
187 typedef _Tp value_type
;
188 typedef typename
_Alloc::const_pointer pointer
;
189 typedef typename
_Alloc::const_reference reference
;
190 typedef typename
_Alloc::difference_type difference_type
;
191 typedef std::forward_iterator_tag iterator_category
;
193 _Fwd_list_const_iterator() : _M_node() { }
196 _Fwd_list_const_iterator(typename
_Node_base::_Const_pointer __n
)
199 _Fwd_list_const_iterator(const iterator
& __iter
)
200 : _M_node(__iter
._M_node
) { }
204 { return __static_pointer_cast
<_Node
*>(_M_node
)->_M_value
; }
208 { return &__static_pointer_cast
<_Node
*>(_M_node
)->_M_value
; }
213 _M_node
= _M_node
->_M_next
;
221 _M_node
= _M_node
->_M_next
;
226 operator==(const _Self
& __x
) const
227 { return _M_node
== __x
._M_node
; }
230 operator!=(const _Self
& __x
) const
231 { return _M_node
!= __x
._M_node
; }
237 return _Fwd_list_const_iterator(_M_node
->_M_next
);
239 return _Fwd_list_const_iterator(0);
242 typename
_Node_base::_Const_pointer _M_node
;
246 * @brief Forward list iterator equality comparison.
248 template<typename _Tp
,class _Alloc
>
250 operator==(const _Fwd_list_iterator
<_Tp
, _Alloc
>& __x
,
251 const _Fwd_list_const_iterator
<_Tp
, _Alloc
>& __y
)
252 { return __x
._M_node
== __y
._M_node
; }
255 * @brief Forward list iterator inequality comparison.
257 template<typename _Tp
,class _Alloc
>
259 operator!=(const _Fwd_list_iterator
<_Tp
, _Alloc
>& __x
,
260 const _Fwd_list_const_iterator
<_Tp
, _Alloc
>& __y
)
261 { return __x
._M_node
!= __y
._M_node
; }
264 * @brief Base class for %forward_list.
266 template<typename _Tp
, typename _Alloc
= allocator
<_Tp
> >
267 struct _Fwd_list_base
270 typedef typename
_Alloc::template rebind
<_Tp
>::other _Tp_alloc_type
;
272 typedef typename
_Alloc::template
273 rebind
<_Fwd_list_node
<_Tp
, _Tp_alloc_type
>>::other _Node_alloc_type
;
275 struct _Fwd_list_impl
276 : public _Node_alloc_type
278 _Fwd_list_node_base
<_Tp_alloc_type
> _M_head
;
281 : _Node_alloc_type(), _M_head()
284 _Fwd_list_impl(const _Node_alloc_type
& __a
)
285 : _Node_alloc_type(__a
), _M_head()
289 _Fwd_list_impl _M_impl
;
292 typedef _Fwd_list_iterator
<_Tp
, _Tp_alloc_type
> iterator
;
293 typedef _Fwd_list_const_iterator
<_Tp
, _Tp_alloc_type
> const_iterator
;
295 typedef _Fwd_list_node
<_Tp
, _Tp_alloc_type
> _Node
;
296 typedef _Fwd_list_node_base
<_Tp_alloc_type
> _Node_base
;
299 _M_get_Node_allocator()
300 { return *static_cast<_Node_alloc_type
*>(&this->_M_impl
); }
302 const _Node_alloc_type
&
303 _M_get_Node_allocator() const
304 { return *static_cast<const _Node_alloc_type
*>(&this->_M_impl
); }
308 { this->_M_impl
._M_head
._M_next
= 0; }
310 _Fwd_list_base(const _Alloc
& __a
)
312 { this->_M_impl
._M_head
._M_next
= 0; }
314 _Fwd_list_base(const _Fwd_list_base
& __lst
, const _Alloc
& __a
);
316 _Fwd_list_base(_Fwd_list_base
&& __lst
, const _Alloc
& __a
)
318 { _Node_base::swap(this->_M_impl
._M_head
,
319 __lst
._M_impl
._M_head
); }
321 _Fwd_list_base(_Fwd_list_base
&& __lst
)
322 : _M_impl(__lst
._M_get_Node_allocator())
323 { _Node_base::swap(this->_M_impl
._M_head
,
324 __lst
._M_impl
._M_head
); }
327 { _M_erase_after(&_M_impl
._M_head
, 0); }
331 typename
_Node::_Pointer
333 { return _M_get_Node_allocator().allocate(1); }
335 template<typename
... _Args
>
336 typename
_Node::_Pointer
337 _M_create_node(_Args
&&... __args
)
339 typename
_Node::_Pointer __node
= this->_M_get_node();
342 _M_get_Node_allocator().construct(__node
,
343 std::forward
<_Args
>(__args
)...);
348 this->_M_put_node(__node
);
349 __throw_exception_again
;
354 template<typename
... _Args
>
355 typename
_Node_base::_Pointer
356 _M_insert_after(const_iterator __pos
, _Args
&&... __args
);
359 _M_put_node(typename
_Node::_Pointer __p
)
360 { _M_get_Node_allocator().deallocate(__p
, 1); }
362 typename
_Node_base::_Pointer
363 _M_erase_after(typename
_Node_base::_Pointer __pos
);
365 typename
_Node_base::_Pointer
366 _M_erase_after(typename
_Node_base::_Pointer __pos
,
367 typename
_Node_base::_Pointer __last
);
371 * @brief A standard container with linear time access to elements,
372 * and fixed time insertion/deletion at any point in the sequence.
376 * Meets the requirements of a <a href="tables.html#65">container</a>, a
377 * <a href="tables.html#67">sequence</a>, including the
378 * <a href="tables.html#68">optional sequence requirements</a> with the
379 * %exception of @c at and @c operator[].
381 * This is a @e singly @e linked %list. Traversal up the
382 * %list requires linear time, but adding and removing elements (or
383 * @e nodes) is done in constant time, regardless of where the
384 * change takes place. Unlike std::vector and std::deque,
385 * random-access iterators are not provided, so subscripting ( @c
386 * [] ) access is not allowed. For algorithms which only need
387 * sequential access, this lack makes no difference.
389 * Also unlike the other standard containers, std::forward_list provides
390 * specialized algorithms %unique to linked lists, such as
391 * splicing, sorting, and in-place reversal.
393 * A couple points on memory allocation for forward_list<Tp>:
395 * First, we never actually allocate a Tp, we allocate
396 * Fwd_list_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
397 * that after elements from %forward_list<X,Alloc1> are spliced into
398 * %forward_list<X,Alloc2>, destroying the memory of the second %list is a
399 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
401 template<typename _Tp
, typename _Alloc
= allocator
<_Tp
> >
402 class forward_list
: private _Fwd_list_base
<_Tp
, _Alloc
>
405 typedef _Fwd_list_base
<_Tp
, _Alloc
> _Base
;
406 typedef typename
_Base::_Node _Node
;
407 typedef typename
_Base::_Node_base _Node_base
;
408 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
412 typedef _Tp value_type
;
413 typedef typename
_Tp_alloc_type::pointer pointer
;
414 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
415 typedef typename
_Tp_alloc_type::reference reference
;
416 typedef typename
_Tp_alloc_type::const_reference const_reference
;
418 typedef typename
_Base::iterator iterator
;
419 typedef typename
_Base::const_iterator const_iterator
;
420 typedef std::size_t size_type
;
421 typedef std::ptrdiff_t difference_type
;
422 typedef _Alloc allocator_type
;
424 // 23.2.3.1 construct/copy/destroy:
427 * @brief Creates a %forward_list with no elements.
428 * @param al An allocator object.
431 forward_list(const _Alloc
& __al
= _Alloc())
436 * @brief Copy constructor with allocator argument.
437 * @param list Input list to copy.
438 * @param al An allocator object.
440 forward_list(const forward_list
& __list
, const _Alloc
& __al
)
441 : _Base(__list
, __al
)
445 * @brief Move constructor with allocator argument.
446 * @param list Input list to move.
447 * @param al An allocator object.
449 forward_list(forward_list
&& __list
, const _Alloc
& __al
)
450 : _Base(std::forward
<_Base
>(__list
), __al
)
454 * @brief Creates a %forward_list with copies of the default element
456 * @param n The number of elements to initially create.
458 * This constructor fills the %forward_list with @a n copies of
462 forward_list(size_type __n
)
464 { _M_fill_initialize(__n
, value_type()); }
467 * @brief Creates a %forward_list with copies of an exemplar element.
468 * @param n The number of elements to initially create.
469 * @param value An element to copy.
470 * @param al An allocator object.
472 * This constructor fills the %forward_list with @a n copies of @a
475 forward_list(size_type __n
, const _Tp
& __value
,
476 const _Alloc
& __al
= _Alloc())
478 { _M_fill_initialize(__n
, __value
); }
481 * @brief Builds a %forward_list from a range.
482 * @param first An input iterator.
483 * @param last An input iterator.
484 * @param al An allocator object.
486 * Create a %forward_list consisting of copies of the elements from
487 * [@a first,@a last). This is linear in N (where N is
488 * distance(@a first,@a last)).
490 template<typename _InputIterator
>
491 forward_list(_InputIterator __first
, _InputIterator __last
,
492 const _Alloc
& __al
= _Alloc())
495 // Check whether it's an integral type. If so, it's not an iterator.
496 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
497 _M_initialize_dispatch(__first
, __last
, _Integral());
501 * @brief The %forward_list copy constructor.
502 * @param list A %forward_list of identical element and allocator
505 * The newly-created %forward_list uses a copy of the allocation
506 * object used by @a list.
508 forward_list(const forward_list
& __list
)
509 : _Base(__list
.get_allocator())
510 { _M_initialize_dispatch(__list
.begin(), __list
.end(), __false_type()); }
513 * @brief The %forward_list move constructor.
514 * @param list A %forward_list of identical element and allocator
517 * The newly-created %forward_list contains the exact contents of @a
518 * forward_list. The contents of @a list are a valid, but unspecified
521 forward_list(forward_list
&& __list
)
522 : _Base(std::forward
<_Base
>(__list
)) { }
525 * @brief Builds a %forward_list from an initializer_list
526 * @param il An initializer_list of value_type.
527 * @param al An allocator object.
529 * Create a %forward_list consisting of copies of the elements
530 * in the initializer_list @a il. This is linear in il.size().
532 forward_list(std::initializer_list
<_Tp
> __il
,
533 const _Alloc
& __al
= _Alloc())
535 { _M_initialize_dispatch(__il
.begin(), __il
.end(), __false_type()); }
538 * @brief The forward_list dtor.
541 { _M_erase_after(&this->_M_impl
._M_head
, 0); }
544 * @brief The %forward_list assignment operator.
545 * @param list A %forward_list of identical element and allocator
548 * All the elements of @a list are copied, but unlike the copy
549 * constructor, the allocator object is not copied.
552 operator=(const forward_list
& __list
);
555 * @brief The %forward_list move assignment operator.
556 * @param list A %forward_list of identical element and allocator
559 * The contents of @a list are moved into this %forward_list
560 * (without copying). @a list is a valid, but unspecified
564 operator=(forward_list
&& __list
)
575 * @brief The %forward_list initializer list assignment operator.
576 * @param il An initializer_list of value_type.
578 * Replace the contents of the %forward_list with copies of the
579 * elements in the initializer_list @a il. This is linear in
583 operator=(std::initializer_list
<_Tp
> __il
)
590 * @brief Assigns a range to a %forward_list.
591 * @param first An input iterator.
592 * @param last An input iterator.
594 * This function fills a %forward_list with copies of the elements
595 * in the range [@a first,@a last).
597 * Note that the assignment completely changes the %forward_list and
598 * that the resulting %forward_list's size is the same as the number
599 * of elements assigned. Old data may be lost.
601 template<typename _InputIterator
>
603 assign(_InputIterator __first
, _InputIterator __last
)
606 insert_after(cbefore_begin(), __first
, __last
);
610 * @brief Assigns a given value to a %forward_list.
611 * @param n Number of elements to be assigned.
612 * @param val Value to be assigned.
614 * This function fills a %forward_list with @a n copies of the given
615 * value. Note that the assignment completely changes the
616 * %forward_list and that the resulting %forward_list's size is the
617 * same as the number of elements assigned. Old data may be lost.
620 assign(size_type __n
, const _Tp
& __val
)
623 insert_after(cbefore_begin(), __n
, __val
);
627 * @brief Assigns an initializer_list to a %forward_list.
628 * @param il An initializer_list of value_type.
630 * Replace the contents of the %forward_list with copies of the
631 * elements in the initializer_list @a il. This is linear in
635 assign(std::initializer_list
<_Tp
> __il
)
638 insert_after(cbefore_begin(), __il
);
641 /// Get a copy of the memory allocation object.
643 get_allocator() const
644 { return this->_M_get_Node_allocator(); }
646 // 23.2.3.2 iterators:
649 * Returns a read/write iterator that points before the first element
650 * in the %forward_list. Iteration is done in ordinary element order.
654 { return iterator(&this->_M_impl
._M_head
); }
657 * Returns a read-only (constant) iterator that points before the
658 * first element in the %forward_list. Iteration is done in ordinary
663 { return const_iterator(&this->_M_impl
._M_head
); }
666 * Returns a read/write iterator that points to the first element
667 * in the %forward_list. Iteration is done in ordinary element order.
671 { return iterator(this->_M_impl
._M_head
._M_next
); }
674 * Returns a read-only (constant) iterator that points to the first
675 * element in the %forward_list. Iteration is done in ordinary
680 { return const_iterator(this->_M_impl
._M_head
._M_next
); }
683 * Returns a read/write iterator that points one past the last
684 * element in the %forward_list. Iteration is done in ordinary
689 { return iterator(0); }
692 * Returns a read-only iterator that points one past the last
693 * element in the %forward_list. Iteration is done in ordinary
698 { return const_iterator(0); }
701 * Returns a read-only (constant) iterator that points to the
702 * first element in the %forward_list. Iteration is done in ordinary
707 { return const_iterator(this->_M_impl
._M_head
._M_next
); }
710 * Returns a read-only (constant) iterator that points before the
711 * first element in the %forward_list. Iteration is done in ordinary
715 cbefore_begin() const
716 { return const_iterator(&this->_M_impl
._M_head
); }
719 * Returns a read-only (constant) iterator that points one past
720 * the last element in the %forward_list. Iteration is done in
721 * ordinary element order.
725 { return const_iterator(0); }
728 * Returns true if the %forward_list is empty. (Thus begin() would
733 { return this->_M_impl
._M_head
._M_next
== 0; }
736 * Returns the largest possible size of %forward_list.
740 { return this->_M_get_Node_allocator().max_size(); }
742 // 23.2.3.3 element access:
745 * Returns a read/write reference to the data at the first
746 * element of the %forward_list.
752 __static_pointer_cast
<_Node
*>(this->_M_impl
._M_head
._M_next
);
753 return __front
->_M_value
;
757 * Returns a read-only (constant) reference to the data at the first
758 * element of the %forward_list.
764 __static_pointer_cast
<_Node
*>(this->_M_impl
._M_head
._M_next
);
765 return __front
->_M_value
;
768 // 23.2.3.4 modifiers:
771 * @brief Constructs object in %forward_list at the front of the
773 * @param args Arguments.
775 * This function will insert an object of type Tp constructed
776 * with Tp(std::forward<Args>(args)...) at the front of the list
777 * Due to the nature of a %forward_list this operation can
778 * be done in constant time, and does not invalidate iterators
781 template<typename
... _Args
>
783 emplace_front(_Args
&&... __args
)
784 { this->_M_insert_after(cbefore_begin(),
785 std::forward
<_Args
>(__args
)...); }
788 * @brief Add data to the front of the %forward_list.
789 * @param val Data to be added.
791 * This is a typical stack operation. The function creates an
792 * element at the front of the %forward_list and assigns the given
793 * data to it. Due to the nature of a %forward_list this operation
794 * can be done in constant time, and does not invalidate iterators
798 push_front(const _Tp
& __val
)
799 { this->_M_insert_after(cbefore_begin(), __val
); }
805 push_front(_Tp
&& __val
)
806 { this->_M_insert_after(cbefore_begin(), std::move(__val
)); }
809 * @brief Removes first element.
811 * This is a typical stack operation. It shrinks the %forward_list
812 * by one. Due to the nature of a %forward_list this operation can
813 * be done in constant time, and only invalidates iterators/references
814 * to the element being removed.
816 * Note that no data is returned, and if the first element's data
817 * is needed, it should be retrieved before pop_front() is
822 { this->_M_erase_after(&this->_M_impl
._M_head
); }
825 * @brief Constructs object in %forward_list after the specified
827 * @param pos A const_iterator into the %forward_list.
828 * @param args Arguments.
829 * @return An iterator that points to the inserted data.
831 * This function will insert an object of type T constructed
832 * with T(std::forward<Args>(args)...) after the specified
833 * location. Due to the nature of a %forward_list this operation can
834 * be done in constant time, and does not invalidate iterators
837 template<typename
... _Args
>
839 emplace_after(const_iterator __pos
, _Args
&&... __args
)
840 { return iterator(this->_M_insert_after(__pos
,
841 std::forward
<_Args
>(__args
)...)); }
844 * @brief Inserts given value into %forward_list after specified
846 * @param pos An iterator into the %forward_list.
847 * @param val Data to be inserted.
848 * @return An iterator that points to the inserted data.
850 * This function will insert a copy of the given value after
851 * the specified location. Due to the nature of a %forward_list this
852 * operation can be done in constant time, and does not
853 * invalidate iterators and references.
856 insert_after(const_iterator __pos
, const _Tp
& __val
)
857 { return iterator(this->_M_insert_after(__pos
, __val
)); }
863 insert_after(const_iterator __pos
, _Tp
&& __val
)
864 { return iterator(this->_M_insert_after(__pos
, std::move(__val
))); }
867 * @brief Inserts a number of copies of given data into the
869 * @param pos An iterator into the %forward_list.
870 * @param n Number of elements to be inserted.
871 * @param val Data to be inserted.
873 * This function will insert a specified number of copies of the
874 * given data after the location specified by @a pos.
876 * This operation is linear in the number of elements inserted and
877 * does not invalidate iterators and references.
880 insert_after(const_iterator __pos
, size_type __n
, const _Tp
& __val
)
882 forward_list
__tmp(__n
, __val
, this->get_allocator());
883 this->splice_after(__pos
, std::move(__tmp
));
887 * @brief Inserts a range into the %forward_list.
888 * @param position An iterator into the %forward_list.
889 * @param first An input iterator.
890 * @param last An input iterator.
892 * This function will insert copies of the data in the range [@a
893 * first,@a last) into the %forward_list after the location specified
896 * This operation is linear in the number of elements inserted and
897 * does not invalidate iterators and references.
899 template<typename _InputIterator
>
901 insert_after(const_iterator __pos
,
902 _InputIterator __first
, _InputIterator __last
)
904 forward_list
__tmp(__first
, __last
, this->get_allocator());
905 this->splice_after(__pos
, std::move(__tmp
));
909 * @brief Inserts the contents of an initializer_list into
910 * %forward_list after the specified iterator.
911 * @param pos An iterator into the %forward_list.
912 * @param il An initializer_list of value_type.
914 * This function will insert copies of the data in the
915 * initializer_list @a il into the %forward_list before the location
916 * specified by @a pos.
918 * This operation is linear in the number of elements inserted and
919 * does not invalidate iterators and references.
922 insert_after(const_iterator __pos
, std::initializer_list
<_Tp
> __il
)
924 forward_list
__tmp(__il
, this->get_allocator());
925 this->splice_after(__pos
, std::move(__tmp
));
929 * @brief Removes the element pointed to by the iterator following
931 * @param pos Iterator pointing to element to be erased.
932 * @return An iterator pointing to the next element (or end()).
934 * This function will erase the element at the given position and
935 * thus shorten the %forward_list by one.
937 * Due to the nature of a %forward_list this operation can be done
938 * in constant time, and only invalidates iterators/references to
939 * the element being removed. The user is also cautioned that
940 * this function only erases the element, and that if the element
941 * is itself a pointer, the pointed-to memory is not touched in
942 * any way. Managing the pointer is the user's responsibility.
945 erase_after(const_iterator __pos
)
947 _Node_base
* __tmp
= __const_pointer_cast
<_Node_base
*>(__pos
._M_node
);
949 return iterator(this->_M_erase_after(__tmp
));
955 * @brief Remove a range of elements.
956 * @param pos Iterator pointing before the first element to be
958 * @param last Iterator pointing to one past the last element to be
960 * @return An iterator pointing to the element pointed to by @a last
961 * prior to erasing (or end()).
963 * This function will erase the elements in the range @a
964 * (pos,last) and shorten the %forward_list accordingly.
966 * This operation is linear time in the size of the range and only
967 * invalidates iterators/references to the element being removed.
968 * The user is also cautioned that this function only erases the
969 * elements, and that if the elements themselves are pointers, the
970 * pointed-to memory is not touched in any way. Managing the pointer
971 * is the user's responsibility.
974 erase_after(const_iterator __pos
, iterator __last
)
976 _Node_base
* __tmp
= __const_pointer_cast
<_Node_base
*>(__pos
._M_node
);
977 return iterator(this->_M_erase_after(__tmp
, &*__last
._M_node
));
981 * @brief Swaps data with another %forward_list.
982 * @param list A %forward_list of the same element and allocator
985 * This exchanges the elements between two lists in constant
986 * time. Note that the global std::swap() function is
987 * specialized such that std::swap(l1,l2) will feed to this
991 swap(forward_list
&& __list
)
992 { _Node_base::swap(this->_M_impl
._M_head
, __list
._M_impl
._M_head
); }
995 * @brief Resizes the %forward_list to the specified number of
997 * @param sz Number of elements the %forward_list should contain.
999 * This function will %resize the %forward_list to the specified
1000 * number of elements. If the number is smaller than the
1001 * %forward_list's current size the %forward_list is truncated,
1002 * otherwise the %forward_list is extended and new elements are
1003 * populated with given data.
1006 resize(size_type __sz
)
1007 { resize(__sz
, _Tp()); }
1010 * @brief Resizes the %forward_list to the specified number of
1012 * @param sz Number of elements the %forward_list should contain.
1013 * @param val Data with which new elements should be populated.
1015 * This function will %resize the %forward_list to the specified
1016 * number of elements. If the number is smaller than the
1017 * %forward_list's current size the %forward_list is truncated,
1018 * otherwise the %forward_list is extended and new elements are
1019 * populated with given data.
1022 resize(size_type __sz
, value_type __val
);
1025 * @brief Erases all the elements.
1027 * Note that this function only erases
1028 * the elements, and that if the elements themselves are
1029 * pointers, the pointed-to memory is not touched in any way.
1030 * Managing the pointer is the user's responsibility.
1034 { this->_M_erase_after(&this->_M_impl
._M_head
, 0); }
1036 // 23.2.3.5 forward_list operations:
1039 * @brief Insert contents of another %forward_list.
1040 * @param pos Iterator referencing the element to insert after.
1041 * @param list Source list.
1043 * The elements of @a list are inserted in constant time after
1044 * the element referenced by @a pos. @a list becomes an empty
1047 * Requires this != @a x.
1050 splice_after(const_iterator __pos
, forward_list
&& __list
);
1053 * @brief Insert element from another %forward_list.
1054 * @param pos Iterator referencing the element to insert after.
1055 * @param list Source list.
1056 * @param it Iterator referencing the element before the element
1059 * Removes the element in list @a list referenced by @a i and
1060 * inserts it into the current list after @a pos.
1063 splice_after(const_iterator __pos
, forward_list
&& __list
,
1064 const_iterator __it
)
1065 { this->splice_after(__pos
, __list
, __it
, __it
._M_next()); }
1068 * @brief Insert range from another %forward_list.
1069 * @param pos Iterator referencing the element to insert after.
1070 * @param list Source list.
1071 * @param before Iterator referencing before the start of range
1073 * @param last Iterator referencing the end of range in list.
1075 * Removes elements in the range (before,last) and inserts them
1076 * after @a pos in constant time.
1078 * Undefined if @a pos is in (before,last).
1081 splice_after(const_iterator __pos
, forward_list
&& __list
,
1082 const_iterator __before
, const_iterator __last
);
1085 * @brief Remove all elements equal to value.
1086 * @param val The value to remove.
1088 * Removes every element in the list equal to @a value.
1089 * Remaining elements stay in list order. Note that this
1090 * function only erases the elements, and that if the elements
1091 * themselves are pointers, the pointed-to memory is not
1092 * touched in any way. Managing the pointer is the user's
1096 remove(const _Tp
& __val
);
1099 * @brief Remove all elements satisfying a predicate.
1100 * @param pred Unary predicate function or object.
1102 * Removes every element in the list for which the predicate
1103 * returns true. Remaining elements stay in list order. Note
1104 * that this function only erases the elements, and that if the
1105 * elements themselves are pointers, the pointed-to memory is
1106 * not touched in any way. Managing the pointer is the user's
1109 template<typename _Pred
>
1111 remove_if(_Pred __pred
);
1114 * @brief Remove consecutive duplicate elements.
1116 * For each consecutive set of elements with the same value,
1117 * remove all but the first one. Remaining elements stay in
1118 * list order. Note that this function only erases the
1119 * elements, and that if the elements themselves are pointers,
1120 * the pointed-to memory is not touched in any way. Managing
1121 * the pointer is the user's responsibility.
1125 { this->unique(std::equal_to
<_Tp
>()); }
1128 * @brief Remove consecutive elements satisfying a predicate.
1129 * @param binary_pred Binary predicate function or object.
1131 * For each consecutive set of elements [first,last) that
1132 * satisfy predicate(first,i) where i is an iterator in
1133 * [first,last), remove all but the first one. Remaining
1134 * elements stay in list order. Note that this function only
1135 * erases the elements, and that if the elements themselves are
1136 * pointers, the pointed-to memory is not touched in any way.
1137 * Managing the pointer is the user's responsibility.
1139 template<typename _BinPred
>
1141 unique(_BinPred __binary_pred
);
1144 * @brief Merge sorted lists.
1145 * @param list Sorted list to merge.
1147 * Assumes that both @a list and this list are sorted according to
1148 * operator<(). Merges elements of @a list into this list in
1149 * sorted order, leaving @a list empty when complete. Elements in
1150 * this list precede elements in @a list that are equal.
1153 merge(forward_list
&& __list
)
1154 { this->merge(__list
, std::less
<_Tp
>()); }
1157 * @brief Merge sorted lists according to comparison function.
1158 * @param list Sorted list to merge.
1159 * @param comp Comparison function defining sort order.
1161 * Assumes that both @a list and this list are sorted according to
1162 * comp. Merges elements of @a list into this list
1163 * in sorted order, leaving @a list empty when complete. Elements
1164 * in this list precede elements in @a list that are equivalent
1165 * according to comp().
1167 template<typename _Comp
>
1169 merge(forward_list
&& __list
, _Comp __comp
);
1172 * @brief Sort the elements of the list.
1174 * Sorts the elements of this list in NlogN time. Equivalent
1175 * elements remain in list order.
1180 _Node
* __tmp
= __static_pointer_cast
<_Node
*>(&this->_M_impl
._M_head
);
1181 __tmp
->_M_sort_after(std::less
<_Tp
>());
1185 * @brief Sort the forward_list using a comparison function.
1187 * Sorts the elements of this list in NlogN time. Equivalent
1188 * elements remain in list order.
1190 template<typename _Comp
>
1194 _Node
* __tmp
= __static_pointer_cast
<_Node
*>(&this->_M_impl
._M_head
);
1195 __tmp
->_M_sort_after(__comp
);
1199 * @brief Reverse the elements in list.
1201 * Reverse the order of elements in the list in linear time.
1205 { this->_M_impl
._M_head
._M_reverse_after(); }
1208 template<typename _Integer
>
1210 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1211 { _M_fill_initialize(static_cast<size_type
>(__n
), __x
); }
1213 // Called by the range constructor to implement [23.1.1]/9
1214 template<typename _InputIterator
>
1216 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1219 // Called by forward_list(n,v,a), and the range constructor when it
1220 // turns out to be the same thing.
1222 _M_fill_initialize(size_type __n
, const value_type
& __value
);
1226 * @brief Forward list equality comparison.
1227 * @param lx A %forward_list
1228 * @param ly A %forward_list of the same type as @a lx.
1229 * @return True iff the size and elements of the forward lists are equal.
1231 * This is an equivalence relation. It is linear in the size of the
1232 * forward lists. Deques are considered equivalent if corresponding
1233 * elements compare equal.
1235 template<typename _Tp
, typename _Alloc
>
1237 operator==(const forward_list
<_Tp
, _Alloc
>& __lx
,
1238 const forward_list
<_Tp
, _Alloc
>& __ly
);
1241 * @brief Forward list ordering relation.
1242 * @param lx A %forward_list.
1243 * @param ly A %forward_list of the same type as @a lx.
1244 * @return True iff @a lx is lexicographically less than @a ly.
1246 * This is a total ordering relation. It is linear in the size of the
1247 * forward lists. The elements must be comparable with @c <.
1249 * See std::lexicographical_compare() for how the determination is made.
1251 template<typename _Tp
, typename _Alloc
>
1253 operator<(const forward_list
<_Tp
, _Alloc
>& __lx
,
1254 const forward_list
<_Tp
, _Alloc
>& __ly
)
1255 { return std::lexicographical_compare(__lx
.cbegin(), __lx
.cend(),
1256 __ly
.cbegin(), __ly
.cend()); }
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 /// Based on operator<
1273 template<typename _Tp
, typename _Alloc
>
1275 operator>=(const forward_list
<_Tp
, _Alloc
>& __lx
,
1276 const forward_list
<_Tp
, _Alloc
>& __ly
)
1277 { return !(__lx
< __ly
); }
1279 /// Based on operator<
1280 template<typename _Tp
, typename _Alloc
>
1282 operator<=(const forward_list
<_Tp
, _Alloc
>& __lx
,
1283 const forward_list
<_Tp
, _Alloc
>& __ly
)
1284 { return !(__ly
< __lx
); }
1286 /// See std::forward_list::swap().
1287 template<typename _Tp
, typename _Alloc
>
1289 swap(forward_list
<_Tp
, _Alloc
>& __lx
,
1290 forward_list
<_Tp
, _Alloc
>& __ly
)
1291 { __lx
.swap(__ly
); }
1293 /// See std::forward_list::swap().
1294 template<typename _Tp
, typename _Alloc
>
1296 swap(forward_list
<_Tp
, _Alloc
>&& __lx
,
1297 forward_list
<_Tp
, _Alloc
>& __ly
)
1298 { __lx
.swap(__ly
); }
1300 /// See std::forward_list::swap().
1301 template<typename _Tp
, typename _Alloc
>
1303 swap(forward_list
<_Tp
, _Alloc
>& __lx
,
1304 forward_list
<_Tp
, _Alloc
>&& __ly
)
1305 { __lx
.swap(__ly
); }
1307 _GLIBCXX_END_NAMESPACE
// namespace std
1309 #endif // __GXX_EXPERIMENTAL_CXX0X__
1311 #endif // _FORWARD_LIST_H