1 // <forward_list.h> -*- C++ -*-
3 // Copyright (C) 2008-2015 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 *static_cast<_Node_alloc_type
*>(&this->_M_impl
); }
310 const _Node_alloc_type
&
311 _M_get_Node_allocator() const noexcept
312 { return *static_cast<const _Node_alloc_type
*>(&this->_M_impl
); }
317 _Fwd_list_base(const _Node_alloc_type
& __a
)
320 _Fwd_list_base(_Fwd_list_base
&& __lst
, const _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.
438 * @param __al An allocator object.
441 forward_list(const _Alloc
& __al
= _Alloc())
442 : _Base(_Node_alloc_type(__al
))
446 * @brief Copy constructor with allocator argument.
447 * @param __list Input list to copy.
448 * @param __al An allocator object.
450 forward_list(const forward_list
& __list
, const _Alloc
& __al
)
451 : _Base(_Node_alloc_type(__al
))
452 { _M_range_initialize(__list
.begin(), __list
.end()); }
455 * @brief Move constructor with allocator argument.
456 * @param __list Input list to move.
457 * @param __al An allocator object.
459 forward_list(forward_list
&& __list
, const _Alloc
& __al
)
460 noexcept(_Node_alloc_traits::_S_always_equal())
461 : _Base(std::move(__list
), _Node_alloc_type(__al
))
465 * @brief Creates a %forward_list with default constructed elements.
466 * @param __n The number of elements to initially create.
468 * This constructor creates the %forward_list with @a __n default
469 * constructed elements.
472 forward_list(size_type __n
, const _Alloc
& __al
= _Alloc())
473 : _Base(_Node_alloc_type(__al
))
474 { _M_default_initialize(__n
); }
477 * @brief Creates a %forward_list with copies of an exemplar element.
478 * @param __n The number of elements to initially create.
479 * @param __value An element to copy.
480 * @param __al An allocator object.
482 * This constructor fills the %forward_list with @a __n copies of
485 forward_list(size_type __n
, const _Tp
& __value
,
486 const _Alloc
& __al
= _Alloc())
487 : _Base(_Node_alloc_type(__al
))
488 { _M_fill_initialize(__n
, __value
); }
491 * @brief Builds a %forward_list from a range.
492 * @param __first An input iterator.
493 * @param __last An input iterator.
494 * @param __al An allocator object.
496 * Create a %forward_list consisting of copies of the elements from
497 * [@a __first,@a __last). This is linear in N (where N is
498 * distance(@a __first,@a __last)).
500 template<typename _InputIterator
,
501 typename
= std::_RequireInputIter
<_InputIterator
>>
502 forward_list(_InputIterator __first
, _InputIterator __last
,
503 const _Alloc
& __al
= _Alloc())
504 : _Base(_Node_alloc_type(__al
))
505 { _M_range_initialize(__first
, __last
); }
508 * @brief The %forward_list copy constructor.
509 * @param __list A %forward_list of identical element and allocator
512 forward_list(const forward_list
& __list
)
513 : _Base(_Node_alloc_traits::_S_select_on_copy(
514 __list
._M_get_Node_allocator()))
515 { _M_range_initialize(__list
.begin(), __list
.end()); }
518 * @brief The %forward_list move constructor.
519 * @param __list A %forward_list of identical element and allocator
522 * The newly-created %forward_list contains the exact contents of @a
523 * __list. The contents of @a __list are a valid, but unspecified
526 forward_list(forward_list
&& __list
) noexcept
527 : _Base(std::move(__list
)) { }
530 * @brief Builds a %forward_list from an initializer_list
531 * @param __il An initializer_list of value_type.
532 * @param __al An allocator object.
534 * Create a %forward_list consisting of copies of the elements
535 * in the initializer_list @a __il. This is linear in __il.size().
537 forward_list(std::initializer_list
<_Tp
> __il
,
538 const _Alloc
& __al
= _Alloc())
539 : _Base(_Node_alloc_type(__al
))
540 { _M_range_initialize(__il
.begin(), __il
.end()); }
543 * @brief The forward_list dtor.
545 ~forward_list() noexcept
549 * @brief The %forward_list assignment operator.
550 * @param __list A %forward_list of identical element and allocator
553 * All the elements of @a __list are copied, but unlike the copy
554 * constructor, the allocator object is not copied.
557 operator=(const forward_list
& __list
);
560 * @brief The %forward_list move assignment operator.
561 * @param __list A %forward_list of identical element and allocator
564 * The contents of @a __list are moved into this %forward_list
565 * (without copying, if the allocators permit it).
566 * @a __list is a valid, but unspecified %forward_list
569 operator=(forward_list
&& __list
)
570 noexcept(_Node_alloc_traits::_S_nothrow_move())
572 constexpr bool __move_storage
=
573 _Node_alloc_traits::_S_propagate_on_move_assign()
574 || _Node_alloc_traits::_S_always_equal();
575 _M_move_assign(std::move(__list
),
576 integral_constant
<bool, __move_storage
>());
581 * @brief The %forward_list initializer list assignment operator.
582 * @param __il An initializer_list of value_type.
584 * Replace the contents of the %forward_list with copies of the
585 * elements in the initializer_list @a __il. This is linear in
589 operator=(std::initializer_list
<_Tp
> __il
)
596 * @brief Assigns a range to a %forward_list.
597 * @param __first An input iterator.
598 * @param __last An input iterator.
600 * This function fills a %forward_list with copies of the elements
601 * in the range [@a __first,@a __last).
603 * Note that the assignment completely changes the %forward_list and
604 * that the number of elements of the resulting %forward_list is the
605 * same as the number of elements assigned. Old data is lost.
607 template<typename _InputIterator
,
608 typename
= std::_RequireInputIter
<_InputIterator
>>
610 assign(_InputIterator __first
, _InputIterator __last
)
612 typedef is_assignable
<_Tp
, decltype(*__first
)> __assignable
;
613 _M_assign(__first
, __last
, __assignable());
617 * @brief Assigns a given value to a %forward_list.
618 * @param __n Number of elements to be assigned.
619 * @param __val Value to be assigned.
621 * This function fills a %forward_list with @a __n copies of the
622 * given value. Note that the assignment completely changes the
623 * %forward_list, and that the resulting %forward_list has __n
624 * elements. Old data is lost.
627 assign(size_type __n
, const _Tp
& __val
)
628 { _M_assign_n(__n
, __val
, is_copy_assignable
<_Tp
>()); }
631 * @brief Assigns an initializer_list to a %forward_list.
632 * @param __il An initializer_list of value_type.
634 * Replace the contents of the %forward_list with copies of the
635 * elements in the initializer_list @a __il. This is linear in
639 assign(std::initializer_list
<_Tp
> __il
)
640 { assign(__il
.begin(), __il
.end()); }
642 /// Get a copy of the memory allocation object.
644 get_allocator() const noexcept
645 { return allocator_type(this->_M_get_Node_allocator()); }
647 // 23.3.4.3 iterators:
650 * Returns a read/write iterator that points before the first element
651 * in the %forward_list. Iteration is done in ordinary element order.
654 before_begin() noexcept
655 { return iterator(&this->_M_impl
._M_head
); }
658 * Returns a read-only (constant) iterator that points before the
659 * first element in the %forward_list. Iteration is done in ordinary
663 before_begin() const noexcept
664 { return const_iterator(&this->_M_impl
._M_head
); }
667 * Returns a read/write iterator that points to the first element
668 * in the %forward_list. Iteration is done in ordinary element order.
672 { return iterator(this->_M_impl
._M_head
._M_next
); }
675 * Returns a read-only (constant) iterator that points to the first
676 * element in the %forward_list. Iteration is done in ordinary
680 begin() const noexcept
681 { return const_iterator(this->_M_impl
._M_head
._M_next
); }
684 * Returns a read/write iterator that points one past the last
685 * element in the %forward_list. Iteration is done in ordinary
690 { return iterator(0); }
693 * Returns a read-only iterator that points one past the last
694 * element in the %forward_list. Iteration is done in ordinary
699 { return const_iterator(0); }
702 * Returns a read-only (constant) iterator that points to the
703 * first element in the %forward_list. Iteration is done in ordinary
707 cbegin() const noexcept
708 { return const_iterator(this->_M_impl
._M_head
._M_next
); }
711 * Returns a read-only (constant) iterator that points before the
712 * first element in the %forward_list. Iteration is done in ordinary
716 cbefore_begin() const noexcept
717 { return const_iterator(&this->_M_impl
._M_head
); }
720 * Returns a read-only (constant) iterator that points one past
721 * the last element in the %forward_list. Iteration is done in
722 * ordinary element order.
725 cend() const noexcept
726 { return const_iterator(0); }
729 * Returns true if the %forward_list is empty. (Thus begin() would
733 empty() const noexcept
734 { return this->_M_impl
._M_head
._M_next
== 0; }
737 * Returns the largest possible number of elements of %forward_list.
740 max_size() const noexcept
741 { return _Node_alloc_traits::max_size(this->_M_get_Node_allocator()); }
743 // 23.3.4.4 element access:
746 * Returns a read/write reference to the data at the first
747 * element of the %forward_list.
752 _Node
* __front
= static_cast<_Node
*>(this->_M_impl
._M_head
._M_next
);
753 return *__front
->_M_valptr();
757 * Returns a read-only (constant) reference to the data at the first
758 * element of the %forward_list.
763 _Node
* __front
= static_cast<_Node
*>(this->_M_impl
._M_head
._M_next
);
764 return *__front
->_M_valptr();
767 // 23.3.4.5 modifiers:
770 * @brief Constructs object in %forward_list at the front of the
772 * @param __args Arguments.
774 * This function will insert an object of type Tp constructed
775 * with Tp(std::forward<Args>(args)...) at the front of the list
776 * Due to the nature of a %forward_list this operation can
777 * be done in constant time, and does not invalidate iterators
780 template<typename
... _Args
>
782 emplace_front(_Args
&&... __args
)
783 { this->_M_insert_after(cbefore_begin(),
784 std::forward
<_Args
>(__args
)...); }
787 * @brief Add data to the front of the %forward_list.
788 * @param __val Data to be added.
790 * This is a typical stack operation. The function creates an
791 * element at the front of the %forward_list and assigns the given
792 * data to it. Due to the nature of a %forward_list this operation
793 * can be done in constant time, and does not invalidate iterators
797 push_front(const _Tp
& __val
)
798 { this->_M_insert_after(cbefore_begin(), __val
); }
804 push_front(_Tp
&& __val
)
805 { this->_M_insert_after(cbefore_begin(), std::move(__val
)); }
808 * @brief Removes first element.
810 * This is a typical stack operation. It shrinks the %forward_list
811 * by one. Due to the nature of a %forward_list this operation can
812 * be done in constant time, and only invalidates iterators/references
813 * to the element being removed.
815 * Note that no data is returned, and if the first element's data
816 * is needed, it should be retrieved before pop_front() is
821 { this->_M_erase_after(&this->_M_impl
._M_head
); }
824 * @brief Constructs object in %forward_list after the specified
826 * @param __pos A const_iterator into the %forward_list.
827 * @param __args Arguments.
828 * @return An iterator that points to the inserted data.
830 * This function will insert an object of type T constructed
831 * with T(std::forward<Args>(args)...) after the specified
832 * location. Due to the nature of a %forward_list this operation can
833 * be done in constant time, and does not invalidate iterators
836 template<typename
... _Args
>
838 emplace_after(const_iterator __pos
, _Args
&&... __args
)
839 { return iterator(this->_M_insert_after(__pos
,
840 std::forward
<_Args
>(__args
)...)); }
843 * @brief Inserts given value into %forward_list after specified
845 * @param __pos An iterator into the %forward_list.
846 * @param __val Data to be inserted.
847 * @return An iterator that points to the inserted data.
849 * This function will insert a copy of the given value after
850 * the specified location. Due to the nature of a %forward_list this
851 * operation can be done in constant time, and does not
852 * invalidate iterators and references.
855 insert_after(const_iterator __pos
, const _Tp
& __val
)
856 { return iterator(this->_M_insert_after(__pos
, __val
)); }
862 insert_after(const_iterator __pos
, _Tp
&& __val
)
863 { return iterator(this->_M_insert_after(__pos
, std::move(__val
))); }
866 * @brief Inserts a number of copies of given data into the
868 * @param __pos An iterator into the %forward_list.
869 * @param __n Number of elements to be inserted.
870 * @param __val Data to be inserted.
871 * @return An iterator pointing to the last inserted copy of
872 * @a val or @a pos if @a n == 0.
874 * This function will insert a specified number of copies of the
875 * given data after the location specified by @a pos.
877 * This operation is linear in the number of elements inserted and
878 * does not invalidate iterators and references.
881 insert_after(const_iterator __pos
, size_type __n
, const _Tp
& __val
);
884 * @brief Inserts a range into the %forward_list.
885 * @param __pos An iterator into the %forward_list.
886 * @param __first An input iterator.
887 * @param __last An input iterator.
888 * @return An iterator pointing to the last inserted element or
889 * @a __pos if @a __first == @a __last.
891 * This function will insert copies of the data in the range
892 * [@a __first,@a __last) into the %forward_list after the
893 * location specified by @a __pos.
895 * This operation is linear in the number of elements inserted and
896 * does not invalidate iterators and references.
898 template<typename _InputIterator
,
899 typename
= std::_RequireInputIter
<_InputIterator
>>
901 insert_after(const_iterator __pos
,
902 _InputIterator __first
, _InputIterator __last
);
905 * @brief Inserts the contents of an initializer_list into
906 * %forward_list after the specified iterator.
907 * @param __pos An iterator into the %forward_list.
908 * @param __il An initializer_list of value_type.
909 * @return An iterator pointing to the last inserted element
910 * or @a __pos if @a __il is empty.
912 * This function will insert copies of the data in the
913 * initializer_list @a __il into the %forward_list before the location
914 * specified by @a __pos.
916 * This operation is linear in the number of elements inserted and
917 * does not invalidate iterators and references.
920 insert_after(const_iterator __pos
, std::initializer_list
<_Tp
> __il
)
921 { return insert_after(__pos
, __il
.begin(), __il
.end()); }
924 * @brief Removes the element pointed to by the iterator following
926 * @param __pos Iterator pointing before element to be erased.
927 * @return An iterator pointing to the element following the one
928 * that was erased, or end() if no such element exists.
930 * This function will erase the element at the given position and
931 * thus shorten the %forward_list by one.
933 * Due to the nature of a %forward_list this operation can be done
934 * in constant time, and only invalidates iterators/references to
935 * the element being removed. The user is also cautioned that
936 * this function only erases the element, and that if the element
937 * is itself a pointer, the pointed-to memory is not touched in
938 * any way. Managing the pointer is the user's responsibility.
941 erase_after(const_iterator __pos
)
942 { return iterator(this->_M_erase_after(const_cast<_Node_base
*>
946 * @brief Remove a range of elements.
947 * @param __pos Iterator pointing before the first element to be
949 * @param __last Iterator pointing to one past the last element to be
953 * This function will erase the elements in the range
954 * @a (__pos,__last) and shorten the %forward_list accordingly.
956 * This operation is linear time in the size of the range and only
957 * invalidates iterators/references to the element being removed.
958 * The user is also cautioned that this function only erases the
959 * elements, and that if the elements themselves are pointers, the
960 * pointed-to memory is not touched in any way. Managing the pointer
961 * is the user's responsibility.
964 erase_after(const_iterator __pos
, const_iterator __last
)
965 { return iterator(this->_M_erase_after(const_cast<_Node_base
*>
967 const_cast<_Node_base
*>
968 (__last
._M_node
))); }
971 * @brief Swaps data with another %forward_list.
972 * @param __list A %forward_list of the same element and allocator
975 * This exchanges the elements between two lists in constant
976 * time. Note that the global std::swap() function is
977 * specialized such that std::swap(l1,l2) will feed to this
981 swap(forward_list
& __list
)
982 noexcept(_Node_alloc_traits::_S_nothrow_swap())
984 std::swap(this->_M_impl
._M_head
._M_next
,
985 __list
._M_impl
._M_head
._M_next
);
986 _Node_alloc_traits::_S_on_swap(this->_M_get_Node_allocator(),
987 __list
._M_get_Node_allocator());
991 * @brief Resizes the %forward_list to the specified number of
993 * @param __sz Number of elements the %forward_list should contain.
995 * This function will %resize the %forward_list to the specified
996 * number of elements. If the number is smaller than the
997 * %forward_list's current number of elements the %forward_list
998 * is truncated, otherwise the %forward_list is extended and the
999 * new elements are default constructed.
1002 resize(size_type __sz
);
1005 * @brief Resizes the %forward_list to the specified number of
1007 * @param __sz Number of elements the %forward_list should contain.
1008 * @param __val Data with which new elements should be populated.
1010 * This function will %resize the %forward_list to the specified
1011 * number of elements. If the number is smaller than the
1012 * %forward_list's current number of elements the %forward_list
1013 * is truncated, otherwise the %forward_list is extended and new
1014 * elements are populated with given data.
1017 resize(size_type __sz
, const value_type
& __val
);
1020 * @brief Erases all the elements.
1022 * Note that this function only erases
1023 * the elements, and that if the elements themselves are
1024 * pointers, the pointed-to memory is not touched in any way.
1025 * Managing the pointer is the user's responsibility.
1029 { this->_M_erase_after(&this->_M_impl
._M_head
, 0); }
1031 // 23.3.4.6 forward_list operations:
1034 * @brief Insert contents of another %forward_list.
1035 * @param __pos Iterator referencing the element to insert after.
1036 * @param __list Source list.
1038 * The elements of @a list are inserted in constant time after
1039 * the element referenced by @a pos. @a list becomes an empty
1042 * Requires this != @a x.
1045 splice_after(const_iterator __pos
, forward_list
&& __list
) noexcept
1047 if (!__list
.empty())
1048 _M_splice_after(__pos
, __list
.before_begin(), __list
.end());
1052 splice_after(const_iterator __pos
, forward_list
& __list
) noexcept
1053 { splice_after(__pos
, std::move(__list
)); }
1056 * @brief Insert element from another %forward_list.
1057 * @param __pos Iterator referencing the element to insert after.
1058 * @param __list Source list.
1059 * @param __i Iterator referencing the element before the element
1062 * Removes the element in list @a list referenced by @a i and
1063 * inserts it into the current list after @a pos.
1066 splice_after(const_iterator __pos
, forward_list
&& __list
,
1067 const_iterator __i
) noexcept
;
1070 splice_after(const_iterator __pos
, forward_list
& __list
,
1071 const_iterator __i
) noexcept
1072 { splice_after(__pos
, std::move(__list
), __i
); }
1075 * @brief Insert range from another %forward_list.
1076 * @param __pos Iterator referencing the element to insert after.
1077 * @param __list Source list.
1078 * @param __before Iterator referencing before the start of range
1080 * @param __last Iterator referencing the end of range in list.
1082 * Removes elements in the range (__before,__last) and inserts them
1083 * after @a __pos in constant time.
1085 * Undefined if @a __pos is in (__before,__last).
1088 splice_after(const_iterator __pos
, forward_list
&&,
1089 const_iterator __before
, const_iterator __last
) noexcept
1090 { _M_splice_after(__pos
, __before
, __last
); }
1093 splice_after(const_iterator __pos
, forward_list
&,
1094 const_iterator __before
, const_iterator __last
) noexcept
1095 { _M_splice_after(__pos
, __before
, __last
); }
1098 * @brief Remove all elements equal to value.
1099 * @param __val The value to remove.
1101 * Removes every element in the list equal to @a __val.
1102 * Remaining elements stay in list order. Note that this
1103 * function only erases the elements, and that if the elements
1104 * themselves are pointers, the pointed-to memory is not
1105 * touched in any way. Managing the pointer is the user's
1109 remove(const _Tp
& __val
);
1112 * @brief Remove all elements satisfying a predicate.
1113 * @param __pred Unary predicate function or object.
1115 * Removes every element in the list for which the predicate
1116 * returns true. Remaining elements stay in list order. Note
1117 * that this function only erases the elements, and that if the
1118 * elements themselves are pointers, the pointed-to memory is
1119 * not touched in any way. Managing the pointer is the user's
1122 template<typename _Pred
>
1124 remove_if(_Pred __pred
);
1127 * @brief Remove consecutive duplicate elements.
1129 * For each consecutive set of elements with the same value,
1130 * remove all but the first one. Remaining elements stay in
1131 * list order. Note that this function only erases the
1132 * elements, and that if the elements themselves are pointers,
1133 * the pointed-to memory is not touched in any way. Managing
1134 * the pointer is the user's responsibility.
1138 { unique(std::equal_to
<_Tp
>()); }
1141 * @brief Remove consecutive elements satisfying a predicate.
1142 * @param __binary_pred Binary predicate function or object.
1144 * For each consecutive set of elements [first,last) that
1145 * satisfy predicate(first,i) where i is an iterator in
1146 * [first,last), remove all but the first one. Remaining
1147 * elements stay in list order. Note that this function only
1148 * erases the elements, and that if the elements themselves are
1149 * pointers, the pointed-to memory is not touched in any way.
1150 * Managing the pointer is the user's responsibility.
1152 template<typename _BinPred
>
1154 unique(_BinPred __binary_pred
);
1157 * @brief Merge sorted lists.
1158 * @param __list Sorted list to merge.
1160 * Assumes that both @a list and this list are sorted according to
1161 * operator<(). Merges elements of @a __list into this list in
1162 * sorted order, leaving @a __list empty when complete. Elements in
1163 * this list precede elements in @a __list that are equal.
1166 merge(forward_list
&& __list
)
1167 { merge(std::move(__list
), std::less
<_Tp
>()); }
1170 merge(forward_list
& __list
)
1171 { merge(std::move(__list
)); }
1174 * @brief Merge sorted lists according to comparison function.
1175 * @param __list Sorted list to merge.
1176 * @param __comp Comparison function defining sort order.
1178 * Assumes that both @a __list and this list are sorted according to
1179 * comp. Merges elements of @a __list into this list
1180 * in sorted order, leaving @a __list empty when complete. Elements
1181 * in this list precede elements in @a __list that are equivalent
1182 * according to comp().
1184 template<typename _Comp
>
1186 merge(forward_list
&& __list
, _Comp __comp
);
1188 template<typename _Comp
>
1190 merge(forward_list
& __list
, _Comp __comp
)
1191 { merge(std::move(__list
), __comp
); }
1194 * @brief Sort the elements of the list.
1196 * Sorts the elements of this list in NlogN time. Equivalent
1197 * elements remain in list order.
1201 { sort(std::less
<_Tp
>()); }
1204 * @brief Sort the forward_list using a comparison function.
1206 * Sorts the elements of this list in NlogN time. Equivalent
1207 * elements remain in list order.
1209 template<typename _Comp
>
1214 * @brief Reverse the elements in list.
1216 * Reverse the order of elements in the list in linear time.
1220 { this->_M_impl
._M_head
._M_reverse_after(); }
1223 // Called by the range constructor to implement [23.3.4.2]/9
1224 template<typename _InputIterator
>
1226 _M_range_initialize(_InputIterator __first
, _InputIterator __last
);
1228 // Called by forward_list(n,v,a), and the range constructor when it
1229 // turns out to be the same thing.
1231 _M_fill_initialize(size_type __n
, const value_type
& __value
);
1233 // Called by splice_after and insert_after.
1235 _M_splice_after(const_iterator __pos
, const_iterator __before
,
1236 const_iterator __last
);
1238 // Called by forward_list(n).
1240 _M_default_initialize(size_type __n
);
1242 // Called by resize(sz).
1244 _M_default_insert_after(const_iterator __pos
, size_type __n
);
1246 // Called by operator=(forward_list&&)
1248 _M_move_assign(forward_list
&& __list
, std::true_type
) noexcept
1251 std::swap(this->_M_impl
._M_head
._M_next
,
1252 __list
._M_impl
._M_head
._M_next
);
1253 std::__alloc_on_move(this->_M_get_Node_allocator(),
1254 __list
._M_get_Node_allocator());
1257 // Called by operator=(forward_list&&)
1259 _M_move_assign(forward_list
&& __list
, std::false_type
)
1261 if (__list
._M_get_Node_allocator() == this->_M_get_Node_allocator())
1262 _M_move_assign(std::move(__list
), std::true_type());
1264 // The rvalue's allocator cannot be moved, or is not equal,
1265 // so we need to individually move each element.
1266 this->assign(std::__make_move_if_noexcept_iterator(__list
.begin()),
1267 std::__make_move_if_noexcept_iterator(__list
.end()));
1270 // Called by assign(_InputIterator, _InputIterator) if _Tp is
1272 template<typename _InputIterator
>
1274 _M_assign(_InputIterator __first
, _InputIterator __last
, true_type
)
1276 auto __prev
= before_begin();
1277 auto __curr
= begin();
1279 while (__curr
!= __end
&& __first
!= __last
)
1286 if (__first
!= __last
)
1287 insert_after(__prev
, __first
, __last
);
1288 else if (__curr
!= __end
)
1289 erase_after(__prev
, __end
);
1292 // Called by assign(_InputIterator, _InputIterator) if _Tp is not
1294 template<typename _InputIterator
>
1296 _M_assign(_InputIterator __first
, _InputIterator __last
, false_type
)
1299 insert_after(cbefore_begin(), __first
, __last
);
1302 // Called by assign(size_type, const _Tp&) if Tp is CopyAssignable
1304 _M_assign_n(size_type __n
, const _Tp
& __val
, true_type
)
1306 auto __prev
= before_begin();
1307 auto __curr
= begin();
1309 while (__curr
!= __end
&& __n
> 0)
1317 insert_after(__prev
, __n
, __val
);
1318 else if (__curr
!= __end
)
1319 erase_after(__prev
, __end
);
1322 // Called by assign(size_type, const _Tp&) if Tp is non-CopyAssignable
1324 _M_assign_n(size_type __n
, const _Tp
& __val
, false_type
)
1327 insert_after(cbefore_begin(), __n
, __val
);
1332 * @brief Forward list equality comparison.
1333 * @param __lx A %forward_list
1334 * @param __ly A %forward_list of the same type as @a __lx.
1335 * @return True iff the elements of the forward lists are equal.
1337 * This is an equivalence relation. It is linear in the number of
1338 * elements of the forward lists. Deques are considered equivalent
1339 * if corresponding elements compare equal.
1341 template<typename _Tp
, typename _Alloc
>
1343 operator==(const forward_list
<_Tp
, _Alloc
>& __lx
,
1344 const forward_list
<_Tp
, _Alloc
>& __ly
);
1347 * @brief Forward list ordering relation.
1348 * @param __lx A %forward_list.
1349 * @param __ly A %forward_list of the same type as @a __lx.
1350 * @return True iff @a __lx is lexicographically less than @a __ly.
1352 * This is a total ordering relation. It is linear in the number of
1353 * elements of the forward lists. The elements must be comparable
1356 * See std::lexicographical_compare() for how the determination is made.
1358 template<typename _Tp
, typename _Alloc
>
1360 operator<(const forward_list
<_Tp
, _Alloc
>& __lx
,
1361 const forward_list
<_Tp
, _Alloc
>& __ly
)
1362 { return std::lexicographical_compare(__lx
.cbegin(), __lx
.cend(),
1363 __ly
.cbegin(), __ly
.cend()); }
1365 /// Based on operator==
1366 template<typename _Tp
, typename _Alloc
>
1368 operator!=(const forward_list
<_Tp
, _Alloc
>& __lx
,
1369 const forward_list
<_Tp
, _Alloc
>& __ly
)
1370 { return !(__lx
== __ly
); }
1372 /// Based on operator<
1373 template<typename _Tp
, typename _Alloc
>
1375 operator>(const forward_list
<_Tp
, _Alloc
>& __lx
,
1376 const forward_list
<_Tp
, _Alloc
>& __ly
)
1377 { return (__ly
< __lx
); }
1379 /// Based on operator<
1380 template<typename _Tp
, typename _Alloc
>
1382 operator>=(const forward_list
<_Tp
, _Alloc
>& __lx
,
1383 const forward_list
<_Tp
, _Alloc
>& __ly
)
1384 { return !(__lx
< __ly
); }
1386 /// Based on operator<
1387 template<typename _Tp
, typename _Alloc
>
1389 operator<=(const forward_list
<_Tp
, _Alloc
>& __lx
,
1390 const forward_list
<_Tp
, _Alloc
>& __ly
)
1391 { return !(__ly
< __lx
); }
1393 /// See std::forward_list::swap().
1394 template<typename _Tp
, typename _Alloc
>
1396 swap(forward_list
<_Tp
, _Alloc
>& __lx
,
1397 forward_list
<_Tp
, _Alloc
>& __ly
)
1398 { __lx
.swap(__ly
); }
1400 _GLIBCXX_END_NAMESPACE_CONTAINER
1403 #endif // _FORWARD_LIST_H