Merge from mainline (168000:168310).
[official-gcc/graphite-test-results.git] / libstdc++-v3 / include / bits / forward_list.h
blobb7f071f365fd0982e0617b0ab5cfc395ec553f1f
1 // <forward_list.h> -*- C++ -*-
3 // Copyright (C) 2008, 2009, 2010 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 <memory>
36 #include <initializer_list>
38 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
40 /**
41 * @brief A helper basic node class for %forward_list.
42 * This is just a linked list with nothing inside it.
43 * There are purely list shuffling utility methods here.
45 struct _Fwd_list_node_base
47 _Fwd_list_node_base() : _M_next(0) { }
49 _Fwd_list_node_base* _M_next;
51 _Fwd_list_node_base*
52 _M_transfer_after(_Fwd_list_node_base* __begin)
54 _Fwd_list_node_base* __end = __begin;
55 while (__end && __end->_M_next)
56 __end = __end->_M_next;
57 return _M_transfer_after(__begin, __end);
60 _Fwd_list_node_base*
61 _M_transfer_after(_Fwd_list_node_base* __begin,
62 _Fwd_list_node_base* __end)
64 _Fwd_list_node_base* __keep = __begin->_M_next;
65 if (__end)
67 __begin->_M_next = __end->_M_next;
68 __end->_M_next = _M_next;
70 else
71 __begin->_M_next = 0;
72 _M_next = __keep;
73 return __end;
76 void
77 _M_reverse_after()
79 _Fwd_list_node_base* __tail = _M_next;
80 if (!__tail)
81 return;
82 while (_Fwd_list_node_base* __temp = __tail->_M_next)
84 _Fwd_list_node_base* __keep = _M_next;
85 _M_next = __temp;
86 __tail->_M_next = __temp->_M_next;
87 _M_next->_M_next = __keep;
92 /**
93 * @brief A helper node class for %forward_list.
94 * This is just a linked list with a 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 template<typename... _Args>
102 _Fwd_list_node(_Args&&... __args)
103 : _Fwd_list_node_base(),
104 _M_value(std::forward<_Args>(__args)...) { }
106 _Tp _M_value;
110 * @brief A forward_list::iterator.
112 * All the functions are op overloads.
114 template<typename _Tp>
115 struct _Fwd_list_iterator
117 typedef _Fwd_list_iterator<_Tp> _Self;
118 typedef _Fwd_list_node<_Tp> _Node;
120 typedef _Tp value_type;
121 typedef _Tp* pointer;
122 typedef _Tp& reference;
123 typedef ptrdiff_t difference_type;
124 typedef std::forward_iterator_tag iterator_category;
126 _Fwd_list_iterator()
127 : _M_node() { }
129 explicit
130 _Fwd_list_iterator(_Fwd_list_node_base* __n)
131 : _M_node(__n) { }
133 reference
134 operator*() const
135 { return static_cast<_Node*>(this->_M_node)->_M_value; }
137 pointer
138 operator->() const
139 { return std::__addressof(static_cast<_Node*>
140 (this->_M_node)->_M_value); }
142 _Self&
143 operator++()
145 _M_node = _M_node->_M_next;
146 return *this;
149 _Self
150 operator++(int)
152 _Self __tmp(*this);
153 _M_node = _M_node->_M_next;
154 return __tmp;
157 bool
158 operator==(const _Self& __x) const
159 { return _M_node == __x._M_node; }
161 bool
162 operator!=(const _Self& __x) const
163 { return _M_node != __x._M_node; }
165 _Self
166 _M_next() const
168 if (_M_node)
169 return _Fwd_list_iterator(_M_node->_M_next);
170 else
171 return _Fwd_list_iterator(0);
174 _Fwd_list_node_base* _M_node;
178 * @brief A forward_list::const_iterator.
180 * All the functions are op overloads.
182 template<typename _Tp>
183 struct _Fwd_list_const_iterator
185 typedef _Fwd_list_const_iterator<_Tp> _Self;
186 typedef const _Fwd_list_node<_Tp> _Node;
187 typedef _Fwd_list_iterator<_Tp> iterator;
189 typedef _Tp value_type;
190 typedef const _Tp* pointer;
191 typedef const _Tp& reference;
192 typedef ptrdiff_t difference_type;
193 typedef std::forward_iterator_tag iterator_category;
195 _Fwd_list_const_iterator()
196 : _M_node() { }
198 explicit
199 _Fwd_list_const_iterator(const _Fwd_list_node_base* __n)
200 : _M_node(__n) { }
202 _Fwd_list_const_iterator(const iterator& __iter)
203 : _M_node(__iter._M_node) { }
205 reference
206 operator*() const
207 { return static_cast<_Node*>(this->_M_node)->_M_value; }
209 pointer
210 operator->() const
211 { return std::__addressof(static_cast<_Node*>
212 (this->_M_node)->_M_value); }
214 _Self&
215 operator++()
217 _M_node = _M_node->_M_next;
218 return *this;
221 _Self
222 operator++(int)
224 _Self __tmp(*this);
225 _M_node = _M_node->_M_next;
226 return __tmp;
229 bool
230 operator==(const _Self& __x) const
231 { return _M_node == __x._M_node; }
233 bool
234 operator!=(const _Self& __x) const
235 { return _M_node != __x._M_node; }
237 _Self
238 _M_next() const
240 if (this->_M_node)
241 return _Fwd_list_const_iterator(_M_node->_M_next);
242 else
243 return _Fwd_list_const_iterator(0);
246 const _Fwd_list_node_base* _M_node;
250 * @brief Forward list iterator equality comparison.
252 template<typename _Tp>
253 inline bool
254 operator==(const _Fwd_list_iterator<_Tp>& __x,
255 const _Fwd_list_const_iterator<_Tp>& __y)
256 { return __x._M_node == __y._M_node; }
259 * @brief Forward list iterator inequality comparison.
261 template<typename _Tp>
262 inline bool
263 operator!=(const _Fwd_list_iterator<_Tp>& __x,
264 const _Fwd_list_const_iterator<_Tp>& __y)
265 { return __x._M_node != __y._M_node; }
268 * @brief Base class for %forward_list.
270 template<typename _Tp, typename _Alloc>
271 struct _Fwd_list_base
273 protected:
274 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
276 typedef typename _Alloc::template
277 rebind<_Fwd_list_node<_Tp>>::other _Node_alloc_type;
279 struct _Fwd_list_impl
280 : public _Node_alloc_type
282 _Fwd_list_node_base _M_head;
284 _Fwd_list_impl()
285 : _Node_alloc_type(), _M_head()
288 _Fwd_list_impl(const _Node_alloc_type& __a)
289 : _Node_alloc_type(__a), _M_head()
293 _Fwd_list_impl _M_impl;
295 public:
296 typedef _Fwd_list_iterator<_Tp> iterator;
297 typedef _Fwd_list_const_iterator<_Tp> const_iterator;
298 typedef _Fwd_list_node<_Tp> _Node;
300 _Node_alloc_type&
301 _M_get_Node_allocator()
302 { return *static_cast<_Node_alloc_type*>(&this->_M_impl); }
304 const _Node_alloc_type&
305 _M_get_Node_allocator() const
306 { return *static_cast<const _Node_alloc_type*>(&this->_M_impl); }
308 _Fwd_list_base()
309 : _M_impl() { }
311 _Fwd_list_base(const _Alloc& __a)
312 : _M_impl(__a) { }
314 _Fwd_list_base(const _Fwd_list_base& __lst, const _Alloc& __a);
316 _Fwd_list_base(_Fwd_list_base&& __lst, const _Alloc& __a)
317 : _M_impl(__a)
319 this->_M_impl._M_head._M_next = __lst._M_impl._M_head._M_next;
320 __lst._M_impl._M_head._M_next = 0;
323 _Fwd_list_base(_Fwd_list_base&& __lst)
324 : _M_impl(__lst._M_get_Node_allocator())
326 this->_M_impl._M_head._M_next = __lst._M_impl._M_head._M_next;
327 __lst._M_impl._M_head._M_next = 0;
330 ~_Fwd_list_base()
331 { _M_erase_after(&_M_impl._M_head, 0); }
333 protected:
335 _Node*
336 _M_get_node()
337 { return _M_get_Node_allocator().allocate(1); }
339 template<typename... _Args>
340 _Node*
341 _M_create_node(_Args&&... __args)
343 _Node* __node = this->_M_get_node();
344 __try
346 _M_get_Node_allocator().construct(__node,
347 std::forward<_Args>(__args)...);
348 __node->_M_next = 0;
350 __catch(...)
352 this->_M_put_node(__node);
353 __throw_exception_again;
355 return __node;
358 template<typename... _Args>
359 _Fwd_list_node_base*
360 _M_insert_after(const_iterator __pos, _Args&&... __args);
362 void
363 _M_put_node(_Node* __p)
364 { _M_get_Node_allocator().deallocate(__p, 1); }
366 _Fwd_list_node_base*
367 _M_erase_after(_Fwd_list_node_base* __pos);
369 _Fwd_list_node_base*
370 _M_erase_after(_Fwd_list_node_base* __pos,
371 _Fwd_list_node_base* __last);
375 * @brief A standard container with linear time access to elements,
376 * and fixed time insertion/deletion at any point in the sequence.
378 * @ingroup sequences
380 * Meets the requirements of a <a href="tables.html#65">container</a>, a
381 * <a href="tables.html#67">sequence</a>, including the
382 * <a href="tables.html#68">optional sequence requirements</a> with the
383 * %exception of @c at and @c operator[].
385 * This is a @e singly @e linked %list. Traversal up the
386 * %list requires linear time, but adding and removing elements (or
387 * @e nodes) is done in constant time, regardless of where the
388 * change takes place. Unlike std::vector and std::deque,
389 * random-access iterators are not provided, so subscripting ( @c
390 * [] ) access is not allowed. For algorithms which only need
391 * sequential access, this lack makes no difference.
393 * Also unlike the other standard containers, std::forward_list provides
394 * specialized algorithms %unique to linked lists, such as
395 * splicing, sorting, and in-place reversal.
397 * A couple points on memory allocation for forward_list<Tp>:
399 * First, we never actually allocate a Tp, we allocate
400 * Fwd_list_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
401 * that after elements from %forward_list<X,Alloc1> are spliced into
402 * %forward_list<X,Alloc2>, destroying the memory of the second %list is a
403 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
405 template<typename _Tp, typename _Alloc = allocator<_Tp> >
406 class forward_list : private _Fwd_list_base<_Tp, _Alloc>
408 private:
409 typedef _Fwd_list_base<_Tp, _Alloc> _Base;
410 typedef _Fwd_list_node<_Tp> _Node;
411 typedef _Fwd_list_node_base _Node_base;
412 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
414 public:
415 // types:
416 typedef _Tp value_type;
417 typedef typename _Tp_alloc_type::pointer pointer;
418 typedef typename _Tp_alloc_type::const_pointer const_pointer;
419 typedef typename _Tp_alloc_type::reference reference;
420 typedef typename _Tp_alloc_type::const_reference const_reference;
422 typedef _Fwd_list_iterator<_Tp> iterator;
423 typedef _Fwd_list_const_iterator<_Tp> const_iterator;
424 typedef std::size_t size_type;
425 typedef std::ptrdiff_t difference_type;
426 typedef _Alloc allocator_type;
428 // 23.2.3.1 construct/copy/destroy:
431 * @brief Creates a %forward_list with no elements.
432 * @param al An allocator object.
434 explicit
435 forward_list(const _Alloc& __al = _Alloc())
436 : _Base(__al)
440 * @brief Copy constructor with allocator argument.
441 * @param list Input list to copy.
442 * @param al An allocator object.
444 forward_list(const forward_list& __list, const _Alloc& __al)
445 : _Base(__list, __al)
449 * @brief Move constructor with allocator argument.
450 * @param list Input list to move.
451 * @param al An allocator object.
453 forward_list(forward_list&& __list, const _Alloc& __al)
454 : _Base(std::move(__list), __al)
458 * @brief Creates a %forward_list with default constructed elements.
459 * @param n The number of elements to initially create.
461 * This constructor creates the %forward_list with @a n default
462 * constructed elements.
464 explicit
465 forward_list(size_type __n)
466 : _Base()
467 { _M_default_initialize(__n); }
470 * @brief Creates a %forward_list with copies of an exemplar element.
471 * @param n The number of elements to initially create.
472 * @param value An element to copy.
473 * @param al An allocator object.
475 * This constructor fills the %forward_list with @a n copies of @a
476 * value.
478 forward_list(size_type __n, const _Tp& __value,
479 const _Alloc& __al = _Alloc())
480 : _Base(__al)
481 { _M_fill_initialize(__n, __value); }
484 * @brief Builds a %forward_list from a range.
485 * @param first An input iterator.
486 * @param last An input iterator.
487 * @param al An allocator object.
489 * Create a %forward_list consisting of copies of the elements from
490 * [@a first,@a last). This is linear in N (where N is
491 * distance(@a first,@a last)).
493 template<typename _InputIterator>
494 forward_list(_InputIterator __first, _InputIterator __last,
495 const _Alloc& __al = _Alloc())
496 : _Base(__al)
498 // Check whether it's an integral type. If so, it's not an iterator.
499 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
500 _M_initialize_dispatch(__first, __last, _Integral());
504 * @brief The %forward_list copy constructor.
505 * @param list A %forward_list of identical element and allocator
506 * types.
508 * The newly-created %forward_list uses a copy of the allocation
509 * object used by @a list.
511 forward_list(const forward_list& __list)
512 : _Base(__list._M_get_Node_allocator())
513 { _M_initialize_dispatch(__list.begin(), __list.end(), __false_type()); }
516 * @brief The %forward_list move constructor.
517 * @param list A %forward_list of identical element and allocator
518 * types.
520 * The newly-created %forward_list contains the exact contents of @a
521 * forward_list. The contents of @a list are a valid, but unspecified
522 * %forward_list.
524 forward_list(forward_list&& __list)
525 : _Base(std::move(__list)) { }
528 * @brief Builds a %forward_list from an initializer_list
529 * @param il An initializer_list of value_type.
530 * @param al An allocator object.
532 * Create a %forward_list consisting of copies of the elements
533 * in the initializer_list @a il. This is linear in il.size().
535 forward_list(std::initializer_list<_Tp> __il,
536 const _Alloc& __al = _Alloc())
537 : _Base(__al)
538 { _M_initialize_dispatch(__il.begin(), __il.end(), __false_type()); }
541 * @brief The forward_list dtor.
543 ~forward_list()
547 * @brief The %forward_list assignment operator.
548 * @param list A %forward_list of identical element and allocator
549 * types.
551 * All the elements of @a list are copied, but unlike the copy
552 * constructor, the allocator object is not copied.
554 forward_list&
555 operator=(const forward_list& __list);
558 * @brief The %forward_list move assignment operator.
559 * @param list A %forward_list of identical element and allocator
560 * types.
562 * The contents of @a list are moved into this %forward_list
563 * (without copying). @a list is a valid, but unspecified
564 * %forward_list
566 forward_list&
567 operator=(forward_list&& __list)
569 // NB: DR 1204.
570 // NB: DR 675.
571 this->clear();
572 this->swap(__list);
573 return *this;
577 * @brief The %forward_list initializer list assignment operator.
578 * @param il An initializer_list of value_type.
580 * Replace the contents of the %forward_list with copies of the
581 * elements in the initializer_list @a il. This is linear in
582 * il.size().
584 forward_list&
585 operator=(std::initializer_list<_Tp> __il)
587 assign(__il);
588 return *this;
592 * @brief Assigns a range to a %forward_list.
593 * @param first An input iterator.
594 * @param last An input iterator.
596 * This function fills a %forward_list with copies of the elements
597 * in the range [@a first,@a last).
599 * Note that the assignment completely changes the %forward_list and
600 * that the resulting %forward_list's size is the same as the number
601 * of elements assigned. Old data may be lost.
603 template<typename _InputIterator>
604 void
605 assign(_InputIterator __first, _InputIterator __last)
607 clear();
608 insert_after(cbefore_begin(), __first, __last);
612 * @brief Assigns a given value to a %forward_list.
613 * @param n Number of elements to be assigned.
614 * @param val Value to be assigned.
616 * This function fills a %forward_list with @a n copies of the given
617 * value. Note that the assignment completely changes the
618 * %forward_list and that the resulting %forward_list's size is the
619 * same as the number of elements assigned. Old data may be lost.
621 void
622 assign(size_type __n, const _Tp& __val)
624 clear();
625 insert_after(cbefore_begin(), __n, __val);
629 * @brief Assigns an initializer_list to a %forward_list.
630 * @param il An initializer_list of value_type.
632 * Replace the contents of the %forward_list with copies of the
633 * elements in the initializer_list @a il. This is linear in
634 * il.size().
636 void
637 assign(std::initializer_list<_Tp> __il)
639 clear();
640 insert_after(cbefore_begin(), __il);
643 /// Get a copy of the memory allocation object.
644 allocator_type
645 get_allocator() const
646 { return this->_M_get_Node_allocator(); }
648 // 23.2.3.2 iterators:
651 * Returns a read/write iterator that points before the first element
652 * in the %forward_list. Iteration is done in ordinary element order.
654 iterator
655 before_begin()
656 { return iterator(&this->_M_impl._M_head); }
659 * Returns a read-only (constant) iterator that points before the
660 * first element in the %forward_list. Iteration is done in ordinary
661 * element order.
663 const_iterator
664 before_begin() const
665 { return const_iterator(&this->_M_impl._M_head); }
668 * Returns a read/write iterator that points to the first element
669 * in the %forward_list. Iteration is done in ordinary element order.
671 iterator
672 begin()
673 { return iterator(this->_M_impl._M_head._M_next); }
676 * Returns a read-only (constant) iterator that points to the first
677 * element in the %forward_list. Iteration is done in ordinary
678 * element order.
680 const_iterator
681 begin() const
682 { return const_iterator(this->_M_impl._M_head._M_next); }
685 * Returns a read/write iterator that points one past the last
686 * element in the %forward_list. Iteration is done in ordinary
687 * element order.
689 iterator
690 end()
691 { return iterator(0); }
694 * Returns a read-only iterator that points one past the last
695 * element in the %forward_list. Iteration is done in ordinary
696 * element order.
698 const_iterator
699 end() const
700 { return const_iterator(0); }
703 * Returns a read-only (constant) iterator that points to the
704 * first element in the %forward_list. Iteration is done in ordinary
705 * element order.
707 const_iterator
708 cbegin() const
709 { return const_iterator(this->_M_impl._M_head._M_next); }
712 * Returns a read-only (constant) iterator that points before the
713 * first element in the %forward_list. Iteration is done in ordinary
714 * element order.
716 const_iterator
717 cbefore_begin() const
718 { return const_iterator(&this->_M_impl._M_head); }
721 * Returns a read-only (constant) iterator that points one past
722 * the last element in the %forward_list. Iteration is done in
723 * ordinary element order.
725 const_iterator
726 cend() const
727 { return const_iterator(0); }
730 * Returns true if the %forward_list is empty. (Thus begin() would
731 * equal end().)
733 bool
734 empty() const
735 { return this->_M_impl._M_head._M_next == 0; }
738 * Returns the largest possible size of %forward_list.
740 size_type
741 max_size() const
742 { return this->_M_get_Node_allocator().max_size(); }
744 // 23.2.3.3 element access:
747 * Returns a read/write reference to the data at the first
748 * element of the %forward_list.
750 reference
751 front()
753 _Node* __front = static_cast<_Node*>(this->_M_impl._M_head._M_next);
754 return __front->_M_value;
758 * Returns a read-only (constant) reference to the data at the first
759 * element of the %forward_list.
761 const_reference
762 front() const
764 _Node* __front = static_cast<_Node*>(this->_M_impl._M_head._M_next);
765 return __front->_M_value;
768 // 23.2.3.4 modifiers:
771 * @brief Constructs object in %forward_list at the front of the
772 * list.
773 * @param args Arguments.
775 * This function will insert an object of type Tp constructed
776 * with Tp(std::forward<Args>(args)...) at the front of the list
777 * Due to the nature of a %forward_list this operation can
778 * be done in constant time, and does not invalidate iterators
779 * and references.
781 template<typename... _Args>
782 void
783 emplace_front(_Args&&... __args)
784 { this->_M_insert_after(cbefore_begin(),
785 std::forward<_Args>(__args)...); }
788 * @brief Add data to the front of the %forward_list.
789 * @param val Data to be added.
791 * This is a typical stack operation. The function creates an
792 * element at the front of the %forward_list and assigns the given
793 * data to it. Due to the nature of a %forward_list this operation
794 * can be done in constant time, and does not invalidate iterators
795 * and references.
797 void
798 push_front(const _Tp& __val)
799 { this->_M_insert_after(cbefore_begin(), __val); }
804 void
805 push_front(_Tp&& __val)
806 { this->_M_insert_after(cbefore_begin(), std::move(__val)); }
809 * @brief Removes first element.
811 * This is a typical stack operation. It shrinks the %forward_list
812 * by one. Due to the nature of a %forward_list this operation can
813 * be done in constant time, and only invalidates iterators/references
814 * to the element being removed.
816 * Note that no data is returned, and if the first element's data
817 * is needed, it should be retrieved before pop_front() is
818 * called.
820 void
821 pop_front()
822 { this->_M_erase_after(&this->_M_impl._M_head); }
825 * @brief Constructs object in %forward_list after the specified
826 * iterator.
827 * @param pos A const_iterator into the %forward_list.
828 * @param args Arguments.
829 * @return An iterator that points to the inserted data.
831 * This function will insert an object of type T constructed
832 * with T(std::forward<Args>(args)...) after the specified
833 * location. Due to the nature of a %forward_list this operation can
834 * be done in constant time, and does not invalidate iterators
835 * and references.
837 template<typename... _Args>
838 iterator
839 emplace_after(const_iterator __pos, _Args&&... __args)
840 { return iterator(this->_M_insert_after(__pos,
841 std::forward<_Args>(__args)...)); }
844 * @brief Inserts given value into %forward_list after specified
845 * iterator.
846 * @param pos An iterator into the %forward_list.
847 * @param val Data to be inserted.
848 * @return An iterator that points to the inserted data.
850 * This function will insert a copy of the given value after
851 * the specified location. Due to the nature of a %forward_list this
852 * operation can be done in constant time, and does not
853 * invalidate iterators and references.
855 iterator
856 insert_after(const_iterator __pos, const _Tp& __val)
857 { return iterator(this->_M_insert_after(__pos, __val)); }
862 iterator
863 insert_after(const_iterator __pos, _Tp&& __val)
864 { return iterator(this->_M_insert_after(__pos, std::move(__val))); }
867 * @brief Inserts a number of copies of given data into the
868 * %forward_list.
869 * @param pos An iterator into the %forward_list.
870 * @param n Number of elements to be inserted.
871 * @param val Data to be inserted.
872 * @return An iterator pointing to the last inserted copy of
873 * @a val or @a pos if @a n == 0.
875 * This function will insert a specified number of copies of the
876 * given data after the location specified by @a pos.
878 * This operation is linear in the number of elements inserted and
879 * does not invalidate iterators and references.
881 iterator
882 insert_after(const_iterator __pos, size_type __n, const _Tp& __val);
885 * @brief Inserts a range into the %forward_list.
886 * @param position An iterator into the %forward_list.
887 * @param first An input iterator.
888 * @param last An input iterator.
889 * @return An iterator pointing to the last inserted element or
890 * @a pos if @a first == @a last.
892 * This function will insert copies of the data in the range [@a
893 * first,@a last) into the %forward_list after the location specified
894 * by @a pos.
896 * This operation is linear in the number of elements inserted and
897 * does not invalidate iterators and references.
899 template<typename _InputIterator>
900 iterator
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.
919 iterator
920 insert_after(const_iterator __pos, std::initializer_list<_Tp> __il);
923 * @brief Removes the element pointed to by the iterator following
924 * @c pos.
925 * @param pos Iterator pointing before element to be erased.
926 * @return An iterator pointing to the element following the one
927 * that was erased, or end() if no such element exists.
929 * This function will erase the element at the given position and
930 * thus shorten the %forward_list by one.
932 * Due to the nature of a %forward_list this operation can be done
933 * in constant time, and only invalidates iterators/references to
934 * the element being removed. The user is also cautioned that
935 * this function only erases the element, and that if the element
936 * is itself a pointer, the pointed-to memory is not touched in
937 * any way. Managing the pointer is the user's responsibility.
939 iterator
940 erase_after(const_iterator __pos)
941 { return iterator(this->_M_erase_after(const_cast<_Node_base*>
942 (__pos._M_node))); }
945 * @brief Remove a range of elements.
946 * @param pos Iterator pointing before the first element to be
947 * erased.
948 * @param last Iterator pointing to one past the last element to be
949 * erased.
950 * @return @last.
952 * This function will erase the elements in the range @a
953 * (pos,last) and shorten the %forward_list accordingly.
955 * This operation is linear time in the size of the range and only
956 * invalidates iterators/references to the element being removed.
957 * The user is also cautioned that this function only erases the
958 * elements, and that if the elements themselves are pointers, the
959 * pointed-to memory is not touched in any way. Managing the pointer
960 * is the user's responsibility.
962 iterator
963 erase_after(const_iterator __pos, const_iterator __last)
964 { return iterator(this->_M_erase_after(const_cast<_Node_base*>
965 (__pos._M_node),
966 const_cast<_Node_base*>
967 (__last._M_node))); }
970 * @brief Swaps data with another %forward_list.
971 * @param list A %forward_list of the same element and allocator
972 * types.
974 * This exchanges the elements between two lists in constant
975 * time. Note that the global std::swap() function is
976 * specialized such that std::swap(l1,l2) will feed to this
977 * function.
979 void
980 swap(forward_list& __list)
981 { std::swap(this->_M_impl._M_head._M_next,
982 __list._M_impl._M_head._M_next); }
985 * @brief Resizes the %forward_list to the specified number of
986 * elements.
987 * @param sz Number of elements the %forward_list should contain.
989 * This function will %resize the %forward_list to the specified
990 * number of elements. If the number is smaller than the
991 * %forward_list's current size the %forward_list is truncated,
992 * otherwise the %forward_list is extended and the new elements
993 * are default constructed.
995 void
996 resize(size_type __sz);
999 * @brief Resizes the %forward_list to the specified number of
1000 * elements.
1001 * @param sz Number of elements the %forward_list should contain.
1002 * @param val Data with which new elements should be populated.
1004 * This function will %resize the %forward_list to the specified
1005 * number of elements. If the number is smaller than the
1006 * %forward_list's current size the %forward_list is truncated,
1007 * otherwise the %forward_list is extended and new elements are
1008 * populated with given data.
1010 void
1011 resize(size_type __sz, const value_type& __val);
1014 * @brief Erases all the elements.
1016 * Note that this function only erases
1017 * the elements, and that if the elements themselves are
1018 * pointers, the pointed-to memory is not touched in any way.
1019 * Managing the pointer is the user's responsibility.
1021 void
1022 clear()
1023 { this->_M_erase_after(&this->_M_impl._M_head, 0); }
1025 // 23.2.3.5 forward_list operations:
1028 * @brief Insert contents of another %forward_list.
1029 * @param pos Iterator referencing the element to insert after.
1030 * @param list Source list.
1032 * The elements of @a list are inserted in constant time after
1033 * the element referenced by @a pos. @a list becomes an empty
1034 * list.
1036 * Requires this != @a x.
1038 void
1039 splice_after(const_iterator __pos, forward_list&& __list)
1041 if (!__list.empty())
1042 _M_splice_after(__pos, std::move(__list));
1046 * @brief Insert element from another %forward_list.
1047 * @param pos Iterator referencing the element to insert after.
1048 * @param list Source list.
1049 * @param i Iterator referencing the element before the element
1050 * to move.
1052 * Removes the element in list @a list referenced by @a i and
1053 * inserts it into the current list after @a pos.
1055 void
1056 splice_after(const_iterator __pos, forward_list&& __list,
1057 const_iterator __i)
1059 const_iterator __j = __i;
1060 ++__j;
1061 if (__pos == __i || __pos == __j)
1062 return;
1064 splice_after(__pos, std::move(__list), __i, __j);
1068 * @brief Insert range from another %forward_list.
1069 * @param pos Iterator referencing the element to insert after.
1070 * @param list Source list.
1071 * @param before Iterator referencing before the start of range
1072 * in list.
1073 * @param last Iterator referencing the end of range in list.
1075 * Removes elements in the range (before,last) and inserts them
1076 * after @a pos in constant time.
1078 * Undefined if @a pos is in (before,last).
1080 void
1081 splice_after(const_iterator __pos, forward_list&& __list,
1082 const_iterator __before, const_iterator __last);
1085 * @brief Remove all elements equal to value.
1086 * @param val The value to remove.
1088 * Removes every element in the list equal to @a value.
1089 * Remaining elements stay in list order. Note that this
1090 * function only erases the elements, and that if the elements
1091 * themselves are pointers, the pointed-to memory is not
1092 * touched in any way. Managing the pointer is the user's
1093 * responsibility.
1095 void
1096 remove(const _Tp& __val);
1099 * @brief Remove all elements satisfying a predicate.
1100 * @param pred Unary predicate function or object.
1102 * Removes every element in the list for which the predicate
1103 * returns true. Remaining elements stay in list order. Note
1104 * that this function only erases the elements, and that if the
1105 * elements themselves are pointers, the pointed-to memory is
1106 * not touched in any way. Managing the pointer is the user's
1107 * responsibility.
1109 template<typename _Pred>
1110 void
1111 remove_if(_Pred __pred);
1114 * @brief Remove consecutive duplicate elements.
1116 * For each consecutive set of elements with the same value,
1117 * remove all but the first one. Remaining elements stay in
1118 * list order. Note that this function only erases the
1119 * elements, and that if the elements themselves are pointers,
1120 * the pointed-to memory is not touched in any way. Managing
1121 * the pointer is the user's responsibility.
1123 void
1124 unique()
1125 { this->unique(std::equal_to<_Tp>()); }
1128 * @brief Remove consecutive elements satisfying a predicate.
1129 * @param binary_pred Binary predicate function or object.
1131 * For each consecutive set of elements [first,last) that
1132 * satisfy predicate(first,i) where i is an iterator in
1133 * [first,last), remove all but the first one. Remaining
1134 * elements stay in list order. Note that this function only
1135 * erases the elements, and that if the elements themselves are
1136 * pointers, the pointed-to memory is not touched in any way.
1137 * Managing the pointer is the user's responsibility.
1139 template<typename _BinPred>
1140 void
1141 unique(_BinPred __binary_pred);
1144 * @brief Merge sorted lists.
1145 * @param list Sorted list to merge.
1147 * Assumes that both @a list and this list are sorted according to
1148 * operator<(). Merges elements of @a list into this list in
1149 * sorted order, leaving @a list empty when complete. Elements in
1150 * this list precede elements in @a list that are equal.
1152 void
1153 merge(forward_list&& __list)
1154 { this->merge(std::move(__list), std::less<_Tp>()); }
1157 * @brief Merge sorted lists according to comparison function.
1158 * @param list Sorted list to merge.
1159 * @param comp Comparison function defining sort order.
1161 * Assumes that both @a list and this list are sorted according to
1162 * comp. Merges elements of @a list into this list
1163 * in sorted order, leaving @a list empty when complete. Elements
1164 * in this list precede elements in @a list that are equivalent
1165 * according to comp().
1167 template<typename _Comp>
1168 void
1169 merge(forward_list&& __list, _Comp __comp);
1172 * @brief Sort the elements of the list.
1174 * Sorts the elements of this list in NlogN time. Equivalent
1175 * elements remain in list order.
1177 void
1178 sort()
1179 { this->sort(std::less<_Tp>()); }
1182 * @brief Sort the forward_list using a comparison function.
1184 * Sorts the elements of this list in NlogN time. Equivalent
1185 * elements remain in list order.
1187 template<typename _Comp>
1188 void
1189 sort(_Comp __comp);
1192 * @brief Reverse the elements in list.
1194 * Reverse the order of elements in the list in linear time.
1196 void
1197 reverse()
1198 { this->_M_impl._M_head._M_reverse_after(); }
1200 private:
1201 template<typename _Integer>
1202 void
1203 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1204 { _M_fill_initialize(static_cast<size_type>(__n), __x); }
1206 // Called by the range constructor to implement [23.1.1]/9
1207 template<typename _InputIterator>
1208 void
1209 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1210 __false_type);
1212 // Called by forward_list(n,v,a), and the range constructor when it
1213 // turns out to be the same thing.
1214 void
1215 _M_fill_initialize(size_type __n, const value_type& __value);
1217 // Called by splice_after and insert_after.
1218 iterator
1219 _M_splice_after(const_iterator __pos, forward_list&& __list);
1221 // Called by forward_list(n).
1222 void
1223 _M_default_initialize(size_type __n);
1225 // Called by resize(sz).
1226 void
1227 _M_default_insert_after(const_iterator __pos, size_type __n);
1231 * @brief Forward list equality comparison.
1232 * @param lx A %forward_list
1233 * @param ly A %forward_list of the same type as @a lx.
1234 * @return True iff the size and elements of the forward lists are equal.
1236 * This is an equivalence relation. It is linear in the size of the
1237 * forward lists. Deques are considered equivalent if corresponding
1238 * elements compare equal.
1240 template<typename _Tp, typename _Alloc>
1241 bool
1242 operator==(const forward_list<_Tp, _Alloc>& __lx,
1243 const forward_list<_Tp, _Alloc>& __ly);
1246 * @brief Forward list ordering relation.
1247 * @param lx A %forward_list.
1248 * @param ly A %forward_list of the same type as @a lx.
1249 * @return True iff @a lx is lexicographically less than @a ly.
1251 * This is a total ordering relation. It is linear in the size of the
1252 * forward lists. The elements must be comparable with @c <.
1254 * See std::lexicographical_compare() for how the determination is made.
1256 template<typename _Tp, typename _Alloc>
1257 inline bool
1258 operator<(const forward_list<_Tp, _Alloc>& __lx,
1259 const forward_list<_Tp, _Alloc>& __ly)
1260 { return std::lexicographical_compare(__lx.cbegin(), __lx.cend(),
1261 __ly.cbegin(), __ly.cend()); }
1263 /// Based on operator==
1264 template<typename _Tp, typename _Alloc>
1265 inline bool
1266 operator!=(const forward_list<_Tp, _Alloc>& __lx,
1267 const forward_list<_Tp, _Alloc>& __ly)
1268 { return !(__lx == __ly); }
1270 /// Based on operator<
1271 template<typename _Tp, typename _Alloc>
1272 inline bool
1273 operator>(const forward_list<_Tp, _Alloc>& __lx,
1274 const forward_list<_Tp, _Alloc>& __ly)
1275 { return (__ly < __lx); }
1277 /// Based on operator<
1278 template<typename _Tp, typename _Alloc>
1279 inline bool
1280 operator>=(const forward_list<_Tp, _Alloc>& __lx,
1281 const forward_list<_Tp, _Alloc>& __ly)
1282 { return !(__lx < __ly); }
1284 /// Based on operator<
1285 template<typename _Tp, typename _Alloc>
1286 inline bool
1287 operator<=(const forward_list<_Tp, _Alloc>& __lx,
1288 const forward_list<_Tp, _Alloc>& __ly)
1289 { return !(__ly < __lx); }
1291 /// See std::forward_list::swap().
1292 template<typename _Tp, typename _Alloc>
1293 inline void
1294 swap(forward_list<_Tp, _Alloc>& __lx,
1295 forward_list<_Tp, _Alloc>& __ly)
1296 { __lx.swap(__ly); }
1298 _GLIBCXX_END_NESTED_NAMESPACE // namespace std
1300 #endif // _FORWARD_LIST_H