* testsuite/26_numerics/headers/cmath/hypot.cc: XFAIL on AIX.
[official-gcc.git] / libstdc++-v3 / include / bits / forward_list.h
blob0ea88063d7ed0a87449e6a7acdf2a0c0db3cab96
1 // <forward_list.h> -*- C++ -*-
3 // Copyright (C) 2008-2016 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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
48 /**
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;
59 _Fwd_list_node_base*
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;
64 if (__end)
66 __begin->_M_next = __end->_M_next;
67 __end->_M_next = _M_next;
69 else
70 __begin->_M_next = 0;
71 _M_next = __keep;
72 return __end;
75 void
76 _M_reverse_after() noexcept
78 _Fwd_list_node_base* __tail = _M_next;
79 if (!__tail)
80 return;
81 while (_Fwd_list_node_base* __temp = __tail->_M_next)
83 _Fwd_list_node_base* __keep = _M_next;
84 _M_next = __temp;
85 __tail->_M_next = __temp->_M_next;
86 _M_next->_M_next = __keep;
91 /**
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>
98 struct _Fwd_list_node
99 : public _Fwd_list_node_base
101 _Fwd_list_node() = default;
103 __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
105 _Tp*
106 _M_valptr() noexcept
107 { return _M_storage._M_ptr(); }
109 const _Tp*
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
132 : _M_node() { }
134 explicit
135 _Fwd_list_iterator(_Fwd_list_node_base* __n) noexcept
136 : _M_node(__n) { }
138 reference
139 operator*() const noexcept
140 { return *static_cast<_Node*>(this->_M_node)->_M_valptr(); }
142 pointer
143 operator->() const noexcept
144 { return static_cast<_Node*>(this->_M_node)->_M_valptr(); }
146 _Self&
147 operator++() noexcept
149 _M_node = _M_node->_M_next;
150 return *this;
153 _Self
154 operator++(int) noexcept
156 _Self __tmp(*this);
157 _M_node = _M_node->_M_next;
158 return __tmp;
161 bool
162 operator==(const _Self& __x) const noexcept
163 { return _M_node == __x._M_node; }
165 bool
166 operator!=(const _Self& __x) const noexcept
167 { return _M_node != __x._M_node; }
169 _Self
170 _M_next() const noexcept
172 if (_M_node)
173 return _Fwd_list_iterator(_M_node->_M_next);
174 else
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
200 : _M_node() { }
202 explicit
203 _Fwd_list_const_iterator(const _Fwd_list_node_base* __n) noexcept
204 : _M_node(__n) { }
206 _Fwd_list_const_iterator(const iterator& __iter) noexcept
207 : _M_node(__iter._M_node) { }
209 reference
210 operator*() const noexcept
211 { return *static_cast<_Node*>(this->_M_node)->_M_valptr(); }
213 pointer
214 operator->() const noexcept
215 { return static_cast<_Node*>(this->_M_node)->_M_valptr(); }
217 _Self&
218 operator++() noexcept
220 _M_node = _M_node->_M_next;
221 return *this;
224 _Self
225 operator++(int) noexcept
227 _Self __tmp(*this);
228 _M_node = _M_node->_M_next;
229 return __tmp;
232 bool
233 operator==(const _Self& __x) const noexcept
234 { return _M_node == __x._M_node; }
236 bool
237 operator!=(const _Self& __x) const noexcept
238 { return _M_node != __x._M_node; }
240 _Self
241 _M_next() const noexcept
243 if (this->_M_node)
244 return _Fwd_list_const_iterator(_M_node->_M_next);
245 else
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>
256 inline bool
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>
265 inline bool
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
276 protected:
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;
286 _Fwd_list_impl()
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;
301 public:
302 typedef _Fwd_list_iterator<_Tp> iterator;
303 typedef _Fwd_list_const_iterator<_Tp> const_iterator;
304 typedef _Fwd_list_node<_Tp> _Node;
306 _Node_alloc_type&
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; }
314 _Fwd_list_base()
315 : _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;
329 ~_Fwd_list_base()
330 { _M_erase_after(&_M_impl._M_head, 0); }
332 protected:
334 _Node*
335 _M_get_node()
337 auto __ptr = _Node_alloc_traits::allocate(_M_get_Node_allocator(), 1);
338 return std::__addressof(*__ptr);
341 template<typename... _Args>
342 _Node*
343 _M_create_node(_Args&&... __args)
345 _Node* __node = this->_M_get_node();
346 __try
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)...);
354 __catch(...)
356 this->_M_put_node(__node);
357 __throw_exception_again;
359 return __node;
362 template<typename... _Args>
363 _Fwd_list_node_base*
364 _M_insert_after(const_iterator __pos, _Args&&... __args);
366 void
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);
374 _Fwd_list_node_base*
375 _M_erase_after(_Fwd_list_node_base* __pos);
377 _Fwd_list_node_base*
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.
386 * @ingroup sequences
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>
411 private:
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;
420 public:
421 // types:
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.
439 forward_list()
440 noexcept(is_nothrow_default_constructible<_Node_alloc_type>::value)
441 : _Base()
445 * @brief Creates a %forward_list with no elements.
446 * @param __al An allocator object.
448 explicit
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.
487 explicit
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
499 * @a __value.
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
526 * types.
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
536 * types.
538 * The newly-created %forward_list contains the exact contents of @a
539 * __list. The contents of @a __list are a valid, but unspecified
540 * %forward_list.
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
567 * types.
569 * All the elements of @a __list are copied.
571 * Whether the allocator is copied depends on the allocator traits.
573 forward_list&
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
579 * types.
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.
588 forward_list&
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>());
596 return *this;
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
605 * __il.size().
607 forward_list&
608 operator=(std::initializer_list<_Tp> __il)
610 assign(__il);
611 return *this;
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>>
628 void
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
643 * elements.
645 void
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
655 * il.size().
657 void
658 assign(std::initializer_list<_Tp> __il)
659 { assign(__il.begin(), __il.end()); }
661 /// Get a copy of the memory allocation object.
662 allocator_type
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.
672 iterator
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
679 * element order.
681 const_iterator
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.
689 iterator
690 begin() noexcept
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
696 * element order.
698 const_iterator
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
705 * element order.
707 iterator
708 end() noexcept
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
714 * element order.
716 const_iterator
717 end() const noexcept
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
723 * element order.
725 const_iterator
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
732 * element order.
734 const_iterator
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.
743 const_iterator
744 cend() const noexcept
745 { return const_iterator(0); }
748 * Returns true if the %forward_list is empty. (Thus begin() would
749 * equal end().)
751 bool
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.
758 size_type
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.
768 reference
769 front()
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.
779 const_reference
780 front() const
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
790 * list.
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
797 * and references.
799 template<typename... _Args>
800 #if __cplusplus > 201402L
801 reference
802 #else
803 void
804 #endif
805 emplace_front(_Args&&... __args)
807 this->_M_insert_after(cbefore_begin(),
808 std::forward<_Args>(__args)...);
809 #if __cplusplus > 201402L
810 return front();
811 #endif
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
822 * and references.
824 void
825 push_front(const _Tp& __val)
826 { this->_M_insert_after(cbefore_begin(), __val); }
831 void
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
845 * called.
847 void
848 pop_front()
849 { this->_M_erase_after(&this->_M_impl._M_head); }
852 * @brief Constructs object in %forward_list after the specified
853 * iterator.
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
862 * and references.
864 template<typename... _Args>
865 iterator
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
872 * iterator.
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.
882 iterator
883 insert_after(const_iterator __pos, const _Tp& __val)
884 { return iterator(this->_M_insert_after(__pos, __val)); }
889 iterator
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
895 * %forward_list.
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.
908 iterator
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>>
928 iterator
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.
947 iterator
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
953 * @c pos.
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.
968 iterator
969 erase_after(const_iterator __pos)
970 { return iterator(this->_M_erase_after(const_cast<_Node_base*>
971 (__pos._M_node))); }
974 * @brief Remove a range of elements.
975 * @param __pos Iterator pointing before the first element to be
976 * erased.
977 * @param __last Iterator pointing to one past the last element to be
978 * erased.
979 * @return @ __last.
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.
991 iterator
992 erase_after(const_iterator __pos, const_iterator __last)
993 { return iterator(this->_M_erase_after(const_cast<_Node_base*>
994 (__pos._M_node),
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
1001 * types.
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
1006 * function.
1008 * Whether the allocators are swapped depends on the allocator traits.
1010 void
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
1021 * elements.
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.
1030 void
1031 resize(size_type __sz);
1034 * @brief Resizes the %forward_list to the specified number of
1035 * elements.
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.
1045 void
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.
1056 void
1057 clear() noexcept
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
1069 * list.
1071 * Requires this != @a x.
1073 void
1074 splice_after(const_iterator __pos, forward_list&& __list) noexcept
1076 if (!__list.empty())
1077 _M_splice_after(__pos, __list.before_begin(), __list.end());
1080 void
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
1089 * to move.
1091 * Removes the element in list @a list referenced by @a i and
1092 * inserts it into the current list after @a pos.
1094 void
1095 splice_after(const_iterator __pos, forward_list&& __list,
1096 const_iterator __i) noexcept;
1098 void
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
1108 * in list.
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).
1115 * @{
1117 void
1118 splice_after(const_iterator __pos, forward_list&&,
1119 const_iterator __before, const_iterator __last) noexcept
1120 { _M_splice_after(__pos, __before, __last); }
1122 void
1123 splice_after(const_iterator __pos, forward_list&,
1124 const_iterator __before, const_iterator __last) noexcept
1125 { _M_splice_after(__pos, __before, __last); }
1126 // @}
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
1137 * responsibility.
1139 void
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
1151 * responsibility.
1153 template<typename _Pred>
1154 void
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.
1167 void
1168 unique()
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>
1184 void
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.
1196 void
1197 merge(forward_list&& __list)
1198 { merge(std::move(__list), std::less<_Tp>()); }
1200 void
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>
1216 void
1217 merge(forward_list&& __list, _Comp __comp);
1219 template<typename _Comp>
1220 void
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.
1230 void
1231 sort()
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>
1241 void
1242 sort(_Comp __comp);
1245 * @brief Reverse the elements in list.
1247 * Reverse the order of elements in the list in linear time.
1249 void
1250 reverse() noexcept
1251 { this->_M_impl._M_head._M_reverse_after(); }
1253 private:
1254 // Called by the range constructor to implement [23.3.4.2]/9
1255 template<typename _InputIterator>
1256 void
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.
1261 void
1262 _M_fill_initialize(size_type __n, const value_type& __value);
1264 // Called by splice_after and insert_after.
1265 iterator
1266 _M_splice_after(const_iterator __pos, const_iterator __before,
1267 const_iterator __last);
1269 // Called by forward_list(n).
1270 void
1271 _M_default_initialize(size_type __n);
1273 // Called by resize(sz).
1274 void
1275 _M_default_insert_after(const_iterator __pos, size_type __n);
1277 // Called by operator=(forward_list&&)
1278 void
1279 _M_move_assign(forward_list&& __list, std::true_type) noexcept
1281 clear();
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&&)
1289 void
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());
1294 else
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
1302 // CopyAssignable.
1303 template<typename _InputIterator>
1304 void
1305 _M_assign(_InputIterator __first, _InputIterator __last, true_type)
1307 auto __prev = before_begin();
1308 auto __curr = begin();
1309 auto __end = end();
1310 while (__curr != __end && __first != __last)
1312 *__curr = *__first;
1313 ++__prev;
1314 ++__curr;
1315 ++__first;
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
1324 // CopyAssignable.
1325 template<typename _InputIterator>
1326 void
1327 _M_assign(_InputIterator __first, _InputIterator __last, false_type)
1329 clear();
1330 insert_after(cbefore_begin(), __first, __last);
1333 // Called by assign(size_type, const _Tp&) if Tp is CopyAssignable
1334 void
1335 _M_assign_n(size_type __n, const _Tp& __val, true_type)
1337 auto __prev = before_begin();
1338 auto __curr = begin();
1339 auto __end = end();
1340 while (__curr != __end && __n > 0)
1342 *__curr = __val;
1343 ++__prev;
1344 ++__curr;
1345 --__n;
1347 if (__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
1354 void
1355 _M_assign_n(size_type __n, const _Tp& __val, false_type)
1357 clear();
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>
1373 bool
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
1385 * with @c <.
1387 * See std::lexicographical_compare() for how the determination is made.
1389 template<typename _Tp, typename _Alloc>
1390 inline bool
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>
1398 inline bool
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>
1405 inline bool
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>
1412 inline bool
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>
1419 inline bool
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>
1426 inline void
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
1433 } // namespace std
1435 #endif // _FORWARD_LIST_H