1 // <forward_list.h> -*- C++ -*-
3 // Copyright (C) 2008-2016 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/forward_list.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{forward_list}
30 #ifndef _FORWARD_LIST_H
31 #define _FORWARD_LIST_H 1
33 #pragma GCC system_header
35 #include <initializer_list>
36 #include <bits/stl_iterator_base_types.h>
37 #include <bits/stl_iterator.h>
38 #include <bits/stl_algobase.h>
39 #include <bits/stl_function.h>
40 #include <bits/allocator.h>
41 #include <ext/alloc_traits.h>
42 #include <ext/aligned_buffer.h>
44 namespace std
_GLIBCXX_VISIBILITY(default)
46 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
49 * @brief A helper basic node class for %forward_list.
50 * This is just a linked list with nothing inside it.
51 * There are purely list shuffling utility methods here.
53 struct _Fwd_list_node_base
55 _Fwd_list_node_base() = default;
57 _Fwd_list_node_base
* _M_next
= nullptr;
60 _M_transfer_after(_Fwd_list_node_base
* __begin
,
61 _Fwd_list_node_base
* __end
) noexcept
63 _Fwd_list_node_base
* __keep
= __begin
->_M_next
;
66 __begin
->_M_next
= __end
->_M_next
;
67 __end
->_M_next
= _M_next
;
76 _M_reverse_after() noexcept
78 _Fwd_list_node_base
* __tail
= _M_next
;
81 while (_Fwd_list_node_base
* __temp
= __tail
->_M_next
)
83 _Fwd_list_node_base
* __keep
= _M_next
;
85 __tail
->_M_next
= __temp
->_M_next
;
86 _M_next
->_M_next
= __keep
;
92 * @brief A helper node class for %forward_list.
93 * This is just a linked list with uninitialized storage for a
94 * data value in each node.
95 * There is a sorting utility method.
97 template<typename _Tp
>
99 : public _Fwd_list_node_base
101 _Fwd_list_node() = default;
103 __gnu_cxx::__aligned_buffer
<_Tp
> _M_storage
;
107 { return _M_storage
._M_ptr(); }
110 _M_valptr() const noexcept
111 { return _M_storage
._M_ptr(); }
115 * @brief A forward_list::iterator.
117 * All the functions are op overloads.
119 template<typename _Tp
>
120 struct _Fwd_list_iterator
122 typedef _Fwd_list_iterator
<_Tp
> _Self
;
123 typedef _Fwd_list_node
<_Tp
> _Node
;
125 typedef _Tp value_type
;
126 typedef _Tp
* pointer
;
127 typedef _Tp
& reference
;
128 typedef ptrdiff_t difference_type
;
129 typedef std::forward_iterator_tag iterator_category
;
131 _Fwd_list_iterator() noexcept
135 _Fwd_list_iterator(_Fwd_list_node_base
* __n
) noexcept
139 operator*() const noexcept
140 { return *static_cast<_Node
*>(this->_M_node
)->_M_valptr(); }
143 operator->() const noexcept
144 { return static_cast<_Node
*>(this->_M_node
)->_M_valptr(); }
147 operator++() noexcept
149 _M_node
= _M_node
->_M_next
;
154 operator++(int) noexcept
157 _M_node
= _M_node
->_M_next
;
162 operator==(const _Self
& __x
) const noexcept
163 { return _M_node
== __x
._M_node
; }
166 operator!=(const _Self
& __x
) const noexcept
167 { return _M_node
!= __x
._M_node
; }
170 _M_next() const noexcept
173 return _Fwd_list_iterator(_M_node
->_M_next
);
175 return _Fwd_list_iterator(0);
178 _Fwd_list_node_base
* _M_node
;
182 * @brief A forward_list::const_iterator.
184 * All the functions are op overloads.
186 template<typename _Tp
>
187 struct _Fwd_list_const_iterator
189 typedef _Fwd_list_const_iterator
<_Tp
> _Self
;
190 typedef const _Fwd_list_node
<_Tp
> _Node
;
191 typedef _Fwd_list_iterator
<_Tp
> iterator
;
193 typedef _Tp value_type
;
194 typedef const _Tp
* pointer
;
195 typedef const _Tp
& reference
;
196 typedef ptrdiff_t difference_type
;
197 typedef std::forward_iterator_tag iterator_category
;
199 _Fwd_list_const_iterator() noexcept
203 _Fwd_list_const_iterator(const _Fwd_list_node_base
* __n
) noexcept
206 _Fwd_list_const_iterator(const iterator
& __iter
) noexcept
207 : _M_node(__iter
._M_node
) { }
210 operator*() const noexcept
211 { return *static_cast<_Node
*>(this->_M_node
)->_M_valptr(); }
214 operator->() const noexcept
215 { return static_cast<_Node
*>(this->_M_node
)->_M_valptr(); }
218 operator++() noexcept
220 _M_node
= _M_node
->_M_next
;
225 operator++(int) noexcept
228 _M_node
= _M_node
->_M_next
;
233 operator==(const _Self
& __x
) const noexcept
234 { return _M_node
== __x
._M_node
; }
237 operator!=(const _Self
& __x
) const noexcept
238 { return _M_node
!= __x
._M_node
; }
241 _M_next() const noexcept
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
) noexcept
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
) noexcept
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 __alloc_rebind
<_Alloc
, _Tp
> _Tp_alloc_type
;
278 typedef __alloc_rebind
<_Alloc
, _Fwd_list_node
<_Tp
>> _Node_alloc_type
;
279 typedef __gnu_cxx::__alloc_traits
<_Node_alloc_type
> _Node_alloc_traits
;
281 struct _Fwd_list_impl
282 : public _Node_alloc_type
284 _Fwd_list_node_base _M_head
;
287 : _Node_alloc_type(), _M_head()
290 _Fwd_list_impl(const _Node_alloc_type
& __a
)
291 : _Node_alloc_type(__a
), _M_head()
294 _Fwd_list_impl(_Node_alloc_type
&& __a
)
295 : _Node_alloc_type(std::move(__a
)), _M_head()
299 _Fwd_list_impl _M_impl
;
302 typedef _Fwd_list_iterator
<_Tp
> iterator
;
303 typedef _Fwd_list_const_iterator
<_Tp
> const_iterator
;
304 typedef _Fwd_list_node
<_Tp
> _Node
;
307 _M_get_Node_allocator() noexcept
308 { return this->_M_impl
; }
310 const _Node_alloc_type
&
311 _M_get_Node_allocator() const noexcept
312 { return this->_M_impl
; }
317 _Fwd_list_base(_Node_alloc_type
&& __a
)
318 : _M_impl(std::move(__a
)) { }
320 _Fwd_list_base(_Fwd_list_base
&& __lst
, _Node_alloc_type
&& __a
);
322 _Fwd_list_base(_Fwd_list_base
&& __lst
)
323 : _M_impl(std::move(__lst
._M_get_Node_allocator()))
325 this->_M_impl
._M_head
._M_next
= __lst
._M_impl
._M_head
._M_next
;
326 __lst
._M_impl
._M_head
._M_next
= 0;
330 { _M_erase_after(&_M_impl
._M_head
, 0); }
337 auto __ptr
= _Node_alloc_traits::allocate(_M_get_Node_allocator(), 1);
338 return std::__addressof(*__ptr
);
341 template<typename
... _Args
>
343 _M_create_node(_Args
&&... __args
)
345 _Node
* __node
= this->_M_get_node();
348 _Tp_alloc_type
__a(_M_get_Node_allocator());
349 typedef allocator_traits
<_Tp_alloc_type
> _Alloc_traits
;
350 ::new ((void*)__node
) _Node
;
351 _Alloc_traits::construct(__a
, __node
->_M_valptr(),
352 std::forward
<_Args
>(__args
)...);
356 this->_M_put_node(__node
);
357 __throw_exception_again
;
362 template<typename
... _Args
>
364 _M_insert_after(const_iterator __pos
, _Args
&&... __args
);
367 _M_put_node(_Node
* __p
)
369 typedef typename
_Node_alloc_traits::pointer _Ptr
;
370 auto __ptr
= std::pointer_traits
<_Ptr
>::pointer_to(*__p
);
371 _Node_alloc_traits::deallocate(_M_get_Node_allocator(), __ptr
, 1);
375 _M_erase_after(_Fwd_list_node_base
* __pos
);
378 _M_erase_after(_Fwd_list_node_base
* __pos
,
379 _Fwd_list_node_base
* __last
);
383 * @brief A standard container with linear time access to elements,
384 * and fixed time insertion/deletion at any point in the sequence.
388 * @tparam _Tp Type of element.
389 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
391 * Meets the requirements of a <a href="tables.html#65">container</a>, a
392 * <a href="tables.html#67">sequence</a>, including the
393 * <a href="tables.html#68">optional sequence requirements</a> with the
394 * %exception of @c at and @c operator[].
396 * This is a @e singly @e linked %list. Traversal up the
397 * %list requires linear time, but adding and removing elements (or
398 * @e nodes) is done in constant time, regardless of where the
399 * change takes place. Unlike std::vector and std::deque,
400 * random-access iterators are not provided, so subscripting ( @c
401 * [] ) access is not allowed. For algorithms which only need
402 * sequential access, this lack makes no difference.
404 * Also unlike the other standard containers, std::forward_list provides
405 * specialized algorithms %unique to linked lists, such as
406 * splicing, sorting, and in-place reversal.
408 template<typename _Tp
, typename _Alloc
= allocator
<_Tp
> >
409 class forward_list
: private _Fwd_list_base
<_Tp
, _Alloc
>
412 typedef _Fwd_list_base
<_Tp
, _Alloc
> _Base
;
413 typedef _Fwd_list_node
<_Tp
> _Node
;
414 typedef _Fwd_list_node_base _Node_base
;
415 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
416 typedef typename
_Base::_Node_alloc_type _Node_alloc_type
;
417 typedef typename
_Base::_Node_alloc_traits _Node_alloc_traits
;
418 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Alloc_traits
;
422 typedef _Tp value_type
;
423 typedef typename
_Alloc_traits::pointer pointer
;
424 typedef typename
_Alloc_traits::const_pointer const_pointer
;
425 typedef value_type
& reference
;
426 typedef const value_type
& const_reference
;
428 typedef _Fwd_list_iterator
<_Tp
> iterator
;
429 typedef _Fwd_list_const_iterator
<_Tp
> const_iterator
;
430 typedef std::size_t size_type
;
431 typedef std::ptrdiff_t difference_type
;
432 typedef _Alloc allocator_type
;
434 // 23.3.4.2 construct/copy/destroy:
437 * @brief Creates a %forward_list with no elements.
440 noexcept(is_nothrow_default_constructible
<_Node_alloc_type
>::value
)
445 * @brief Creates a %forward_list with no elements.
446 * @param __al An allocator object.
449 forward_list(const _Alloc
& __al
) noexcept
450 : _Base(_Node_alloc_type(__al
))
455 * @brief Copy constructor with allocator argument.
456 * @param __list Input list to copy.
457 * @param __al An allocator object.
459 forward_list(const forward_list
& __list
, const _Alloc
& __al
)
460 : _Base(_Node_alloc_type(__al
))
461 { _M_range_initialize(__list
.begin(), __list
.end()); }
464 * @brief Move constructor with allocator argument.
465 * @param __list Input list to move.
466 * @param __al An allocator object.
468 forward_list(forward_list
&& __list
, const _Alloc
& __al
)
469 noexcept(_Node_alloc_traits::_S_always_equal())
470 : _Base(std::move(__list
), _Node_alloc_type(__al
))
472 // If __list is not empty it means its allocator is not equal to __a,
473 // so we need to move from each element individually.
474 insert_after(cbefore_begin(),
475 std::__make_move_if_noexcept_iterator(__list
.begin()),
476 std::__make_move_if_noexcept_iterator(__list
.end()));
480 * @brief Creates a %forward_list with default constructed elements.
481 * @param __n The number of elements to initially create.
482 * @param __al An allocator object.
484 * This constructor creates the %forward_list with @a __n default
485 * constructed elements.
488 forward_list(size_type __n
, const _Alloc
& __al
= _Alloc())
489 : _Base(_Node_alloc_type(__al
))
490 { _M_default_initialize(__n
); }
493 * @brief Creates a %forward_list with copies of an exemplar element.
494 * @param __n The number of elements to initially create.
495 * @param __value An element to copy.
496 * @param __al An allocator object.
498 * This constructor fills the %forward_list with @a __n copies of
501 forward_list(size_type __n
, const _Tp
& __value
,
502 const _Alloc
& __al
= _Alloc())
503 : _Base(_Node_alloc_type(__al
))
504 { _M_fill_initialize(__n
, __value
); }
507 * @brief Builds a %forward_list from a range.
508 * @param __first An input iterator.
509 * @param __last An input iterator.
510 * @param __al An allocator object.
512 * Create a %forward_list consisting of copies of the elements from
513 * [@a __first,@a __last). This is linear in N (where N is
514 * distance(@a __first,@a __last)).
516 template<typename _InputIterator
,
517 typename
= std::_RequireInputIter
<_InputIterator
>>
518 forward_list(_InputIterator __first
, _InputIterator __last
,
519 const _Alloc
& __al
= _Alloc())
520 : _Base(_Node_alloc_type(__al
))
521 { _M_range_initialize(__first
, __last
); }
524 * @brief The %forward_list copy constructor.
525 * @param __list A %forward_list of identical element and allocator
528 forward_list(const forward_list
& __list
)
529 : _Base(_Node_alloc_traits::_S_select_on_copy(
530 __list
._M_get_Node_allocator()))
531 { _M_range_initialize(__list
.begin(), __list
.end()); }
534 * @brief The %forward_list move constructor.
535 * @param __list A %forward_list of identical element and allocator
538 * The newly-created %forward_list contains the exact contents of @a
539 * __list. The contents of @a __list are a valid, but unspecified
542 forward_list(forward_list
&& __list
) noexcept
543 : _Base(std::move(__list
)) { }
546 * @brief Builds a %forward_list from an initializer_list
547 * @param __il An initializer_list of value_type.
548 * @param __al An allocator object.
550 * Create a %forward_list consisting of copies of the elements
551 * in the initializer_list @a __il. This is linear in __il.size().
553 forward_list(std::initializer_list
<_Tp
> __il
,
554 const _Alloc
& __al
= _Alloc())
555 : _Base(_Node_alloc_type(__al
))
556 { _M_range_initialize(__il
.begin(), __il
.end()); }
559 * @brief The forward_list dtor.
561 ~forward_list() noexcept
565 * @brief The %forward_list assignment operator.
566 * @param __list A %forward_list of identical element and allocator
569 * All the elements of @a __list are copied.
571 * Whether the allocator is copied depends on the allocator traits.
574 operator=(const forward_list
& __list
);
577 * @brief The %forward_list move assignment operator.
578 * @param __list A %forward_list of identical element and allocator
581 * The contents of @a __list are moved into this %forward_list
582 * (without copying, if the allocators permit it).
584 * Afterwards @a __list is a valid, but unspecified %forward_list
586 * Whether the allocator is moved depends on the allocator traits.
589 operator=(forward_list
&& __list
)
590 noexcept(_Node_alloc_traits::_S_nothrow_move())
592 constexpr bool __move_storage
=
593 _Node_alloc_traits::_S_propagate_on_move_assign()
594 || _Node_alloc_traits::_S_always_equal();
595 _M_move_assign(std::move(__list
), __bool_constant
<__move_storage
>());
600 * @brief The %forward_list initializer list assignment operator.
601 * @param __il An initializer_list of value_type.
603 * Replace the contents of the %forward_list with copies of the
604 * elements in the initializer_list @a __il. This is linear in
608 operator=(std::initializer_list
<_Tp
> __il
)
615 * @brief Assigns a range to a %forward_list.
616 * @param __first An input iterator.
617 * @param __last An input iterator.
619 * This function fills a %forward_list with copies of the elements
620 * in the range [@a __first,@a __last).
622 * Note that the assignment completely changes the %forward_list and
623 * that the number of elements of the resulting %forward_list is the
624 * same as the number of elements assigned.
626 template<typename _InputIterator
,
627 typename
= std::_RequireInputIter
<_InputIterator
>>
629 assign(_InputIterator __first
, _InputIterator __last
)
631 typedef is_assignable
<_Tp
, decltype(*__first
)> __assignable
;
632 _M_assign(__first
, __last
, __assignable());
636 * @brief Assigns a given value to a %forward_list.
637 * @param __n Number of elements to be assigned.
638 * @param __val Value to be assigned.
640 * This function fills a %forward_list with @a __n copies of the
641 * given value. Note that the assignment completely changes the
642 * %forward_list, and that the resulting %forward_list has __n
646 assign(size_type __n
, const _Tp
& __val
)
647 { _M_assign_n(__n
, __val
, is_copy_assignable
<_Tp
>()); }
650 * @brief Assigns an initializer_list to a %forward_list.
651 * @param __il An initializer_list of value_type.
653 * Replace the contents of the %forward_list with copies of the
654 * elements in the initializer_list @a __il. This is linear in
658 assign(std::initializer_list
<_Tp
> __il
)
659 { assign(__il
.begin(), __il
.end()); }
661 /// Get a copy of the memory allocation object.
663 get_allocator() const noexcept
664 { return allocator_type(this->_M_get_Node_allocator()); }
666 // 23.3.4.3 iterators:
669 * Returns a read/write iterator that points before the first element
670 * in the %forward_list. Iteration is done in ordinary element order.
673 before_begin() noexcept
674 { return iterator(&this->_M_impl
._M_head
); }
677 * Returns a read-only (constant) iterator that points before the
678 * first element in the %forward_list. Iteration is done in ordinary
682 before_begin() const noexcept
683 { return const_iterator(&this->_M_impl
._M_head
); }
686 * Returns a read/write iterator that points to the first element
687 * in the %forward_list. Iteration is done in ordinary element order.
691 { return iterator(this->_M_impl
._M_head
._M_next
); }
694 * Returns a read-only (constant) iterator that points to the first
695 * element in the %forward_list. Iteration is done in ordinary
699 begin() const noexcept
700 { return const_iterator(this->_M_impl
._M_head
._M_next
); }
703 * Returns a read/write iterator that points one past the last
704 * element in the %forward_list. Iteration is done in ordinary
709 { return iterator(0); }
712 * Returns a read-only iterator that points one past the last
713 * element in the %forward_list. Iteration is done in ordinary
718 { return const_iterator(0); }
721 * Returns a read-only (constant) iterator that points to the
722 * first element in the %forward_list. Iteration is done in ordinary
726 cbegin() const noexcept
727 { return const_iterator(this->_M_impl
._M_head
._M_next
); }
730 * Returns a read-only (constant) iterator that points before the
731 * first element in the %forward_list. Iteration is done in ordinary
735 cbefore_begin() const noexcept
736 { return const_iterator(&this->_M_impl
._M_head
); }
739 * Returns a read-only (constant) iterator that points one past
740 * the last element in the %forward_list. Iteration is done in
741 * ordinary element order.
744 cend() const noexcept
745 { return const_iterator(0); }
748 * Returns true if the %forward_list is empty. (Thus begin() would
752 empty() const noexcept
753 { return this->_M_impl
._M_head
._M_next
== 0; }
756 * Returns the largest possible number of elements of %forward_list.
759 max_size() const noexcept
760 { return _Node_alloc_traits::max_size(this->_M_get_Node_allocator()); }
762 // 23.3.4.4 element access:
765 * Returns a read/write reference to the data at the first
766 * element of the %forward_list.
771 _Node
* __front
= static_cast<_Node
*>(this->_M_impl
._M_head
._M_next
);
772 return *__front
->_M_valptr();
776 * Returns a read-only (constant) reference to the data at the first
777 * element of the %forward_list.
782 _Node
* __front
= static_cast<_Node
*>(this->_M_impl
._M_head
._M_next
);
783 return *__front
->_M_valptr();
786 // 23.3.4.5 modifiers:
789 * @brief Constructs object in %forward_list at the front of the
791 * @param __args Arguments.
793 * This function will insert an object of type Tp constructed
794 * with Tp(std::forward<Args>(args)...) at the front of the list
795 * Due to the nature of a %forward_list this operation can
796 * be done in constant time, and does not invalidate iterators
799 template<typename
... _Args
>
800 #if __cplusplus > 201402L
805 emplace_front(_Args
&&... __args
)
807 this->_M_insert_after(cbefore_begin(),
808 std::forward
<_Args
>(__args
)...);
809 #if __cplusplus > 201402L
815 * @brief Add data to the front of the %forward_list.
816 * @param __val Data to be added.
818 * This is a typical stack operation. The function creates an
819 * element at the front of the %forward_list and assigns the given
820 * data to it. Due to the nature of a %forward_list this operation
821 * can be done in constant time, and does not invalidate iterators
825 push_front(const _Tp
& __val
)
826 { this->_M_insert_after(cbefore_begin(), __val
); }
832 push_front(_Tp
&& __val
)
833 { this->_M_insert_after(cbefore_begin(), std::move(__val
)); }
836 * @brief Removes first element.
838 * This is a typical stack operation. It shrinks the %forward_list
839 * by one. Due to the nature of a %forward_list this operation can
840 * be done in constant time, and only invalidates iterators/references
841 * to the element being removed.
843 * Note that no data is returned, and if the first element's data
844 * is needed, it should be retrieved before pop_front() is
849 { this->_M_erase_after(&this->_M_impl
._M_head
); }
852 * @brief Constructs object in %forward_list after the specified
854 * @param __pos A const_iterator into the %forward_list.
855 * @param __args Arguments.
856 * @return An iterator that points to the inserted data.
858 * This function will insert an object of type T constructed
859 * with T(std::forward<Args>(args)...) after the specified
860 * location. Due to the nature of a %forward_list this operation can
861 * be done in constant time, and does not invalidate iterators
864 template<typename
... _Args
>
866 emplace_after(const_iterator __pos
, _Args
&&... __args
)
867 { return iterator(this->_M_insert_after(__pos
,
868 std::forward
<_Args
>(__args
)...)); }
871 * @brief Inserts given value into %forward_list after specified
873 * @param __pos An iterator into the %forward_list.
874 * @param __val Data to be inserted.
875 * @return An iterator that points to the inserted data.
877 * This function will insert a copy of the given value after
878 * the specified location. Due to the nature of a %forward_list this
879 * operation can be done in constant time, and does not
880 * invalidate iterators and references.
883 insert_after(const_iterator __pos
, const _Tp
& __val
)
884 { return iterator(this->_M_insert_after(__pos
, __val
)); }
890 insert_after(const_iterator __pos
, _Tp
&& __val
)
891 { return iterator(this->_M_insert_after(__pos
, std::move(__val
))); }
894 * @brief Inserts a number of copies of given data into the
896 * @param __pos An iterator into the %forward_list.
897 * @param __n Number of elements to be inserted.
898 * @param __val Data to be inserted.
899 * @return An iterator pointing to the last inserted copy of
900 * @a val or @a pos if @a n == 0.
902 * This function will insert a specified number of copies of the
903 * given data after the location specified by @a pos.
905 * This operation is linear in the number of elements inserted and
906 * does not invalidate iterators and references.
909 insert_after(const_iterator __pos
, size_type __n
, const _Tp
& __val
);
912 * @brief Inserts a range into the %forward_list.
913 * @param __pos An iterator into the %forward_list.
914 * @param __first An input iterator.
915 * @param __last An input iterator.
916 * @return An iterator pointing to the last inserted element or
917 * @a __pos if @a __first == @a __last.
919 * This function will insert copies of the data in the range
920 * [@a __first,@a __last) into the %forward_list after the
921 * location specified by @a __pos.
923 * This operation is linear in the number of elements inserted and
924 * does not invalidate iterators and references.
926 template<typename _InputIterator
,
927 typename
= std::_RequireInputIter
<_InputIterator
>>
929 insert_after(const_iterator __pos
,
930 _InputIterator __first
, _InputIterator __last
);
933 * @brief Inserts the contents of an initializer_list into
934 * %forward_list after the specified iterator.
935 * @param __pos An iterator into the %forward_list.
936 * @param __il An initializer_list of value_type.
937 * @return An iterator pointing to the last inserted element
938 * or @a __pos if @a __il is empty.
940 * This function will insert copies of the data in the
941 * initializer_list @a __il into the %forward_list before the location
942 * specified by @a __pos.
944 * This operation is linear in the number of elements inserted and
945 * does not invalidate iterators and references.
948 insert_after(const_iterator __pos
, std::initializer_list
<_Tp
> __il
)
949 { return insert_after(__pos
, __il
.begin(), __il
.end()); }
952 * @brief Removes the element pointed to by the iterator following
954 * @param __pos Iterator pointing before element to be erased.
955 * @return An iterator pointing to the element following the one
956 * that was erased, or end() if no such element exists.
958 * This function will erase the element at the given position and
959 * thus shorten the %forward_list by one.
961 * Due to the nature of a %forward_list this operation can be done
962 * in constant time, and only invalidates iterators/references to
963 * the element being removed. The user is also cautioned that
964 * this function only erases the element, and that if the element
965 * is itself a pointer, the pointed-to memory is not touched in
966 * any way. Managing the pointer is the user's responsibility.
969 erase_after(const_iterator __pos
)
970 { return iterator(this->_M_erase_after(const_cast<_Node_base
*>
974 * @brief Remove a range of elements.
975 * @param __pos Iterator pointing before the first element to be
977 * @param __last Iterator pointing to one past the last element to be
981 * This function will erase the elements in the range
982 * @a (__pos,__last) and shorten the %forward_list accordingly.
984 * This operation is linear time in the size of the range and only
985 * invalidates iterators/references to the element being removed.
986 * The user is also cautioned that this function only erases the
987 * elements, and that if the elements themselves are pointers, the
988 * pointed-to memory is not touched in any way. Managing the pointer
989 * is the user's responsibility.
992 erase_after(const_iterator __pos
, const_iterator __last
)
993 { return iterator(this->_M_erase_after(const_cast<_Node_base
*>
995 const_cast<_Node_base
*>
996 (__last
._M_node
))); }
999 * @brief Swaps data with another %forward_list.
1000 * @param __list A %forward_list of the same element and allocator
1003 * This exchanges the elements between two lists in constant
1004 * time. Note that the global std::swap() function is
1005 * specialized such that std::swap(l1,l2) will feed to this
1008 * Whether the allocators are swapped depends on the allocator traits.
1011 swap(forward_list
& __list
) noexcept
1013 std::swap(this->_M_impl
._M_head
._M_next
,
1014 __list
._M_impl
._M_head
._M_next
);
1015 _Node_alloc_traits::_S_on_swap(this->_M_get_Node_allocator(),
1016 __list
._M_get_Node_allocator());
1020 * @brief Resizes the %forward_list to the specified number of
1022 * @param __sz Number of elements the %forward_list should contain.
1024 * This function will %resize the %forward_list to the specified
1025 * number of elements. If the number is smaller than the
1026 * %forward_list's current number of elements the %forward_list
1027 * is truncated, otherwise the %forward_list is extended and the
1028 * new elements are default constructed.
1031 resize(size_type __sz
);
1034 * @brief Resizes the %forward_list to the specified number of
1036 * @param __sz Number of elements the %forward_list should contain.
1037 * @param __val Data with which new elements should be populated.
1039 * This function will %resize the %forward_list to the specified
1040 * number of elements. If the number is smaller than the
1041 * %forward_list's current number of elements the %forward_list
1042 * is truncated, otherwise the %forward_list is extended and new
1043 * elements are populated with given data.
1046 resize(size_type __sz
, const value_type
& __val
);
1049 * @brief Erases all the elements.
1051 * Note that this function only erases
1052 * the elements, and that if the elements themselves are
1053 * pointers, the pointed-to memory is not touched in any way.
1054 * Managing the pointer is the user's responsibility.
1058 { this->_M_erase_after(&this->_M_impl
._M_head
, 0); }
1060 // 23.3.4.6 forward_list operations:
1063 * @brief Insert contents of another %forward_list.
1064 * @param __pos Iterator referencing the element to insert after.
1065 * @param __list Source list.
1067 * The elements of @a list are inserted in constant time after
1068 * the element referenced by @a pos. @a list becomes an empty
1071 * Requires this != @a x.
1074 splice_after(const_iterator __pos
, forward_list
&& __list
) noexcept
1076 if (!__list
.empty())
1077 _M_splice_after(__pos
, __list
.before_begin(), __list
.end());
1081 splice_after(const_iterator __pos
, forward_list
& __list
) noexcept
1082 { splice_after(__pos
, std::move(__list
)); }
1085 * @brief Insert element from another %forward_list.
1086 * @param __pos Iterator referencing the element to insert after.
1087 * @param __list Source list.
1088 * @param __i Iterator referencing the element before the element
1091 * Removes the element in list @a list referenced by @a i and
1092 * inserts it into the current list after @a pos.
1095 splice_after(const_iterator __pos
, forward_list
&& __list
,
1096 const_iterator __i
) noexcept
;
1099 splice_after(const_iterator __pos
, forward_list
& __list
,
1100 const_iterator __i
) noexcept
1101 { splice_after(__pos
, std::move(__list
), __i
); }
1104 * @brief Insert range from another %forward_list.
1105 * @param __pos Iterator referencing the element to insert after.
1106 * @param __list Source list.
1107 * @param __before Iterator referencing before the start of range
1109 * @param __last Iterator referencing the end of range in list.
1111 * Removes elements in the range (__before,__last) and inserts them
1112 * after @a __pos in constant time.
1114 * Undefined if @a __pos is in (__before,__last).
1118 splice_after(const_iterator __pos
, forward_list
&&,
1119 const_iterator __before
, const_iterator __last
) noexcept
1120 { _M_splice_after(__pos
, __before
, __last
); }
1123 splice_after(const_iterator __pos
, forward_list
&,
1124 const_iterator __before
, const_iterator __last
) noexcept
1125 { _M_splice_after(__pos
, __before
, __last
); }
1129 * @brief Remove all elements equal to value.
1130 * @param __val The value to remove.
1132 * Removes every element in the list equal to @a __val.
1133 * Remaining elements stay in list order. Note that this
1134 * function only erases the elements, and that if the elements
1135 * themselves are pointers, the pointed-to memory is not
1136 * touched in any way. Managing the pointer is the user's
1140 remove(const _Tp
& __val
);
1143 * @brief Remove all elements satisfying a predicate.
1144 * @param __pred Unary predicate function or object.
1146 * Removes every element in the list for which the predicate
1147 * returns true. Remaining elements stay in list order. Note
1148 * that this function only erases the elements, and that if the
1149 * elements themselves are pointers, the pointed-to memory is
1150 * not touched in any way. Managing the pointer is the user's
1153 template<typename _Pred
>
1155 remove_if(_Pred __pred
);
1158 * @brief Remove consecutive duplicate elements.
1160 * For each consecutive set of elements with the same value,
1161 * remove all but the first one. Remaining elements stay in
1162 * list order. Note that this function only erases the
1163 * elements, and that if the elements themselves are pointers,
1164 * the pointed-to memory is not touched in any way. Managing
1165 * the pointer is the user's responsibility.
1169 { unique(std::equal_to
<_Tp
>()); }
1172 * @brief Remove consecutive elements satisfying a predicate.
1173 * @param __binary_pred Binary predicate function or object.
1175 * For each consecutive set of elements [first,last) that
1176 * satisfy predicate(first,i) where i is an iterator in
1177 * [first,last), remove all but the first one. Remaining
1178 * elements stay in list order. Note that this function only
1179 * erases the elements, and that if the elements themselves are
1180 * pointers, the pointed-to memory is not touched in any way.
1181 * Managing the pointer is the user's responsibility.
1183 template<typename _BinPred
>
1185 unique(_BinPred __binary_pred
);
1188 * @brief Merge sorted lists.
1189 * @param __list Sorted list to merge.
1191 * Assumes that both @a list and this list are sorted according to
1192 * operator<(). Merges elements of @a __list into this list in
1193 * sorted order, leaving @a __list empty when complete. Elements in
1194 * this list precede elements in @a __list that are equal.
1197 merge(forward_list
&& __list
)
1198 { merge(std::move(__list
), std::less
<_Tp
>()); }
1201 merge(forward_list
& __list
)
1202 { merge(std::move(__list
)); }
1205 * @brief Merge sorted lists according to comparison function.
1206 * @param __list Sorted list to merge.
1207 * @param __comp Comparison function defining sort order.
1209 * Assumes that both @a __list and this list are sorted according to
1210 * comp. Merges elements of @a __list into this list
1211 * in sorted order, leaving @a __list empty when complete. Elements
1212 * in this list precede elements in @a __list that are equivalent
1213 * according to comp().
1215 template<typename _Comp
>
1217 merge(forward_list
&& __list
, _Comp __comp
);
1219 template<typename _Comp
>
1221 merge(forward_list
& __list
, _Comp __comp
)
1222 { merge(std::move(__list
), __comp
); }
1225 * @brief Sort the elements of the list.
1227 * Sorts the elements of this list in NlogN time. Equivalent
1228 * elements remain in list order.
1232 { sort(std::less
<_Tp
>()); }
1235 * @brief Sort the forward_list using a comparison function.
1237 * Sorts the elements of this list in NlogN time. Equivalent
1238 * elements remain in list order.
1240 template<typename _Comp
>
1245 * @brief Reverse the elements in list.
1247 * Reverse the order of elements in the list in linear time.
1251 { this->_M_impl
._M_head
._M_reverse_after(); }
1254 // Called by the range constructor to implement [23.3.4.2]/9
1255 template<typename _InputIterator
>
1257 _M_range_initialize(_InputIterator __first
, _InputIterator __last
);
1259 // Called by forward_list(n,v,a), and the range constructor when it
1260 // turns out to be the same thing.
1262 _M_fill_initialize(size_type __n
, const value_type
& __value
);
1264 // Called by splice_after and insert_after.
1266 _M_splice_after(const_iterator __pos
, const_iterator __before
,
1267 const_iterator __last
);
1269 // Called by forward_list(n).
1271 _M_default_initialize(size_type __n
);
1273 // Called by resize(sz).
1275 _M_default_insert_after(const_iterator __pos
, size_type __n
);
1277 // Called by operator=(forward_list&&)
1279 _M_move_assign(forward_list
&& __list
, std::true_type
) noexcept
1282 this->_M_impl
._M_head
._M_next
= __list
._M_impl
._M_head
._M_next
;
1283 __list
._M_impl
._M_head
._M_next
= nullptr;
1284 std::__alloc_on_move(this->_M_get_Node_allocator(),
1285 __list
._M_get_Node_allocator());
1288 // Called by operator=(forward_list&&)
1290 _M_move_assign(forward_list
&& __list
, std::false_type
)
1292 if (__list
._M_get_Node_allocator() == this->_M_get_Node_allocator())
1293 _M_move_assign(std::move(__list
), std::true_type());
1295 // The rvalue's allocator cannot be moved, or is not equal,
1296 // so we need to individually move each element.
1297 this->assign(std::__make_move_if_noexcept_iterator(__list
.begin()),
1298 std::__make_move_if_noexcept_iterator(__list
.end()));
1301 // Called by assign(_InputIterator, _InputIterator) if _Tp is
1303 template<typename _InputIterator
>
1305 _M_assign(_InputIterator __first
, _InputIterator __last
, true_type
)
1307 auto __prev
= before_begin();
1308 auto __curr
= begin();
1310 while (__curr
!= __end
&& __first
!= __last
)
1317 if (__first
!= __last
)
1318 insert_after(__prev
, __first
, __last
);
1319 else if (__curr
!= __end
)
1320 erase_after(__prev
, __end
);
1323 // Called by assign(_InputIterator, _InputIterator) if _Tp is not
1325 template<typename _InputIterator
>
1327 _M_assign(_InputIterator __first
, _InputIterator __last
, false_type
)
1330 insert_after(cbefore_begin(), __first
, __last
);
1333 // Called by assign(size_type, const _Tp&) if Tp is CopyAssignable
1335 _M_assign_n(size_type __n
, const _Tp
& __val
, true_type
)
1337 auto __prev
= before_begin();
1338 auto __curr
= begin();
1340 while (__curr
!= __end
&& __n
> 0)
1348 insert_after(__prev
, __n
, __val
);
1349 else if (__curr
!= __end
)
1350 erase_after(__prev
, __end
);
1353 // Called by assign(size_type, const _Tp&) if Tp is non-CopyAssignable
1355 _M_assign_n(size_type __n
, const _Tp
& __val
, false_type
)
1358 insert_after(cbefore_begin(), __n
, __val
);
1363 * @brief Forward list equality comparison.
1364 * @param __lx A %forward_list
1365 * @param __ly A %forward_list of the same type as @a __lx.
1366 * @return True iff the elements of the forward lists are equal.
1368 * This is an equivalence relation. It is linear in the number of
1369 * elements of the forward lists. Deques are considered equivalent
1370 * if corresponding elements compare equal.
1372 template<typename _Tp
, typename _Alloc
>
1374 operator==(const forward_list
<_Tp
, _Alloc
>& __lx
,
1375 const forward_list
<_Tp
, _Alloc
>& __ly
);
1378 * @brief Forward list ordering relation.
1379 * @param __lx A %forward_list.
1380 * @param __ly A %forward_list of the same type as @a __lx.
1381 * @return True iff @a __lx is lexicographically less than @a __ly.
1383 * This is a total ordering relation. It is linear in the number of
1384 * elements of the forward lists. The elements must be comparable
1387 * See std::lexicographical_compare() for how the determination is made.
1389 template<typename _Tp
, typename _Alloc
>
1391 operator<(const forward_list
<_Tp
, _Alloc
>& __lx
,
1392 const forward_list
<_Tp
, _Alloc
>& __ly
)
1393 { return std::lexicographical_compare(__lx
.cbegin(), __lx
.cend(),
1394 __ly
.cbegin(), __ly
.cend()); }
1396 /// Based on operator==
1397 template<typename _Tp
, typename _Alloc
>
1399 operator!=(const forward_list
<_Tp
, _Alloc
>& __lx
,
1400 const forward_list
<_Tp
, _Alloc
>& __ly
)
1401 { return !(__lx
== __ly
); }
1403 /// Based on operator<
1404 template<typename _Tp
, typename _Alloc
>
1406 operator>(const forward_list
<_Tp
, _Alloc
>& __lx
,
1407 const forward_list
<_Tp
, _Alloc
>& __ly
)
1408 { return (__ly
< __lx
); }
1410 /// Based on operator<
1411 template<typename _Tp
, typename _Alloc
>
1413 operator>=(const forward_list
<_Tp
, _Alloc
>& __lx
,
1414 const forward_list
<_Tp
, _Alloc
>& __ly
)
1415 { return !(__lx
< __ly
); }
1417 /// Based on operator<
1418 template<typename _Tp
, typename _Alloc
>
1420 operator<=(const forward_list
<_Tp
, _Alloc
>& __lx
,
1421 const forward_list
<_Tp
, _Alloc
>& __ly
)
1422 { return !(__ly
< __lx
); }
1424 /// See std::forward_list::swap().
1425 template<typename _Tp
, typename _Alloc
>
1427 swap(forward_list
<_Tp
, _Alloc
>& __lx
,
1428 forward_list
<_Tp
, _Alloc
>& __ly
)
1429 noexcept(noexcept(__lx
.swap(__ly
)))
1430 { __lx
.swap(__ly
); }
1432 _GLIBCXX_END_NAMESPACE_CONTAINER
1435 #endif // _FORWARD_LIST_H