30 #ifndef _FORWARD_LIST_H
31 #define _FORWARD_LIST_H 1
33 #pragma GCC system_header
36 #if __cplusplus >= 201103L
37 #include <initializer_list>
40 namespace std _GLIBCXX_VISIBILITY(default)
42 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
62 __begin->_M_next = __end->_M_next;
63 __end->_M_next = _M_next;
72 _M_reverse_after() noexcept
81 __tail->_M_next = __temp->_M_next;
82 _M_next->_M_next = __keep;
93 template<
typename _Tp>
99 typename aligned_storage<sizeof(_Tp), alignment_of<_Tp>::value>::type
105 return static_cast<_Tp*
>(
static_cast<void*
>(&_M_storage));
109 _M_valptr()
const noexcept
111 return static_cast<const _Tp*
>(
static_cast<const void*
>(&_M_storage));
120 template<
typename _Tp>
126 typedef _Tp value_type;
127 typedef _Tp* pointer;
128 typedef _Tp& reference;
129 typedef ptrdiff_t difference_type;
141 {
return *
static_cast<_Node*
>(this->_M_node)->_M_valptr(); }
145 {
return static_cast<_Node*
>(this->_M_node)->_M_valptr(); }
150 _M_node = _M_node->_M_next;
158 _M_node = _M_node->_M_next;
163 operator==(
const _Self& __x)
const
164 {
return _M_node == __x._M_node; }
167 operator!=(
const _Self& __x)
const
168 {
return _M_node != __x._M_node; }
187 template<
typename _Tp>
194 typedef _Tp value_type;
195 typedef const _Tp* pointer;
196 typedef const _Tp& reference;
197 typedef ptrdiff_t difference_type;
208 : _M_node(__iter._M_node) { }
212 {
return *
static_cast<_Node*
>(this->_M_node)->_M_valptr(); }
216 {
return static_cast<_Node*
>(this->_M_node)->_M_valptr(); }
221 _M_node = _M_node->_M_next;
229 _M_node = _M_node->_M_next;
234 operator==(
const _Self& __x)
const
235 {
return _M_node == __x._M_node; }
238 operator!=(
const _Self& __x)
const
239 {
return _M_node != __x._M_node; }
256 template<
typename _Tp>
260 {
return __x._M_node == __y._M_node; }
265 template<
typename _Tp>
269 {
return __x._M_node != __y._M_node; }
274 template<
typename _Tp,
typename _Alloc>
279 typedef typename _Alloc_traits::template rebind<_Tp>::other
282 typedef typename _Alloc_traits::template
283 rebind<_Fwd_list_node<_Tp>>::other _Node_alloc_type;
287 struct _Fwd_list_impl
288 :
public _Node_alloc_type
293 : _Node_alloc_type(), _M_head()
296 _Fwd_list_impl(
const _Node_alloc_type& __a)
297 : _Node_alloc_type(__a), _M_head()
300 _Fwd_list_impl(_Node_alloc_type&& __a)
301 : _Node_alloc_type(
std::move(__a)), _M_head()
305 _Fwd_list_impl _M_impl;
313 _M_get_Node_allocator() noexcept
314 {
return *
static_cast<_Node_alloc_type*
>(&this->_M_impl); }
316 const _Node_alloc_type&
317 _M_get_Node_allocator()
const noexcept
318 {
return *
static_cast<const _Node_alloc_type*
>(&this->_M_impl); }
329 : _M_impl(
std::move(__lst._M_get_Node_allocator()))
331 this->_M_impl._M_head._M_next = __lst._M_impl._M_head._M_next;
332 __lst._M_impl._M_head._M_next = 0;
336 { _M_erase_after(&_M_impl._M_head, 0); }
344 template<
typename... _Args>
346 _M_create_node(_Args&&... __args)
348 _Node* __node = this->_M_get_node();
351 _Tp_alloc_type __a(_M_get_Node_allocator());
353 ::new ((
void*)__node) _Node();
354 _Alloc_traits::construct(__a, __node->_M_valptr(),
355 std::forward<_Args>(__args)...);
359 this->_M_put_node(__node);
360 __throw_exception_again;
365 template<
typename... _Args>
367 _M_insert_after(const_iterator __pos, _Args&&... __args);
370 _M_put_node(_Node* __p)
407 template<
typename _Tp,
typename _Alloc = allocator<_Tp> >
414 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
415 typedef typename _Base::_Node_alloc_type _Node_alloc_type;
421 typedef _Tp value_type;
422 typedef typename _Alloc_traits::pointer pointer;
423 typedef typename _Alloc_traits::const_pointer const_pointer;
424 typedef typename _Alloc_traits::reference reference;
425 typedef typename _Alloc_traits::const_reference const_reference;
429 typedef std::size_t size_type;
430 typedef std::ptrdiff_t difference_type;
431 typedef _Alloc allocator_type;
441 : _Base(_Node_alloc_type(__al))
450 : _Base(_Node_alloc_type(__al))
451 { _M_range_initialize(__list.
begin(), __list.
end()); }
459 noexcept(_Node_alloc_traits::_S_always_equal())
460 : _Base(
std::
move(__list), _Node_alloc_type(__al))
472 : _Base(_Node_alloc_type(__al))
473 { _M_default_initialize(__n); }
485 const _Alloc& __al = _Alloc())
486 : _Base(_Node_alloc_type(__al))
487 { _M_fill_initialize(__n, __value); }
499 template<
typename _InputIterator,
500 typename = std::_RequireInputIter<_InputIterator>>
502 const _Alloc& __al = _Alloc())
503 : _Base(_Node_alloc_type(__al))
504 { _M_range_initialize(__first, __last); }
512 : _Base(_Node_alloc_traits::_S_select_on_copy(
513 __list._M_get_Node_allocator()))
514 { _M_range_initialize(__list.
begin(), __list.
end()); }
537 const _Alloc& __al = _Alloc())
538 : _Base(_Node_alloc_type(__al))
539 { _M_range_initialize(__il.begin(), __il.end()); }
569 noexcept(_Node_alloc_traits::_S_nothrow_move())
571 constexpr
bool __move_storage =
572 _Node_alloc_traits::_S_propagate_on_move_assign()
573 || _Node_alloc_traits::_S_always_equal();
575 integral_constant<bool, __move_storage>());
606 template<
typename _InputIterator,
607 typename = std::_RequireInputIter<_InputIterator>>
609 assign(_InputIterator __first, _InputIterator __last)
611 typedef is_assignable<_Tp, decltype(*__first)> __assignable;
612 _M_assign(__first, __last, __assignable());
627 { _M_assign_n(__n, __val, is_copy_assignable<_Tp>()); }
639 {
assign(__il.begin(), __il.end()); }
644 {
return allocator_type(this->_M_get_Node_allocator()); }
654 {
return iterator(&this->_M_impl._M_head); }
663 {
return const_iterator(&this->_M_impl._M_head); }
671 {
return iterator(this->_M_impl._M_head._M_next); }
680 {
return const_iterator(this->_M_impl._M_head._M_next); }
689 {
return iterator(0); }
698 {
return const_iterator(0); }
707 {
return const_iterator(this->_M_impl._M_head._M_next); }
716 {
return const_iterator(&this->_M_impl._M_head); }
725 {
return const_iterator(0); }
733 {
return this->_M_impl._M_head._M_next == 0; }
751 _Node* __front =
static_cast<_Node*
>(this->_M_impl._M_head._M_next);
752 return *__front->_M_valptr();
762 _Node* __front =
static_cast<_Node*
>(this->_M_impl._M_head._M_next);
763 return *__front->_M_valptr();
779 template<
typename... _Args>
783 std::forward<_Args>(__args)...); }
820 { this->_M_erase_after(&this->_M_impl._M_head); }
835 template<
typename... _Args>
838 {
return iterator(this->_M_insert_after(__pos,
839 std::forward<_Args>(__args)...)); }
855 {
return iterator(this->_M_insert_after(__pos, __val)); }
862 {
return iterator(this->_M_insert_after(__pos,
std::move(__val))); }
880 insert_after(const_iterator __pos, size_type __n,
const _Tp& __val);
897 template<
typename _InputIterator,
898 typename = std::_RequireInputIter<_InputIterator>>
901 _InputIterator __first, _InputIterator __last);
920 {
return insert_after(__pos, __il.begin(), __il.end()); }
941 {
return iterator(this->_M_erase_after(const_cast<_Node_base*>
964 {
return iterator(this->_M_erase_after(const_cast<_Node_base*>
966 const_cast<_Node_base*>
967 (__last._M_node))); }
981 noexcept(_Node_alloc_traits::_S_nothrow_swap())
984 __list._M_impl._M_head._M_next);
985 _Node_alloc_traits::_S_on_swap(this->_M_get_Node_allocator(),
986 __list._M_get_Node_allocator());
1016 resize(size_type __sz,
const value_type& __val);
1028 { this->_M_erase_after(&this->_M_impl._M_head, 0); }
1046 if (!__list.empty())
1047 _M_splice_after(__pos, __list.before_begin(), __list.end());
1066 const_iterator __i);
1088 const_iterator __before, const_iterator __last)
1089 { _M_splice_after(__pos, __before, __last); }
1093 const_iterator __before, const_iterator __last)
1094 { _M_splice_after(__pos, __before, __last); }
1108 remove(
const _Tp& __val);
1121 template<
typename _Pred>
1151 template<
typename _BinPred>
1153 unique(_BinPred __binary_pred);
1183 template<
typename _Comp>
1187 template<
typename _Comp>
1208 template<
typename _Comp>
1219 { this->_M_impl._M_head._M_reverse_after(); }
1223 template<
typename _InputIterator>
1225 _M_range_initialize(_InputIterator __first, _InputIterator __last);
1230 _M_fill_initialize(size_type __n,
const value_type& __value);
1234 _M_splice_after(const_iterator __pos, const_iterator __before,
1235 const_iterator __last);
1239 _M_default_initialize(size_type __n);
1243 _M_default_insert_after(const_iterator __pos, size_type __n);
1247 _M_move_assign(
forward_list&& __list, std::true_type) noexcept
1250 std::swap(this->_M_impl._M_head._M_next,
1251 __list._M_impl._M_head._M_next);
1252 std::__alloc_on_move(this->_M_get_Node_allocator(),
1253 __list._M_get_Node_allocator());
1260 if (__list._M_get_Node_allocator() == this->_M_get_Node_allocator())
1261 _M_move_assign(
std::move(__list), std::true_type());
1265 this->
assign(std::__make_move_if_noexcept_iterator(__list.begin()),
1266 std::__make_move_if_noexcept_iterator(__list.end()));
1271 template<
typename _InputIterator>
1273 _M_assign(_InputIterator __first, _InputIterator __last, true_type)
1276 auto __curr =
begin();
1278 while (__curr != __end && __first != __last)
1285 if (__first != __last)
1287 else if (__curr != __end)
1293 template<
typename _InputIterator>
1295 _M_assign(_InputIterator __first, _InputIterator __last, false_type)
1303 _M_assign_n(size_type __n,
const _Tp& __val, true_type)
1306 auto __curr =
begin();
1308 while (__curr != __end && __n > 0)
1317 else if (__curr != __end)
1323 _M_assign_n(size_type __n,
const _Tp& __val, false_type)
1340 template<
typename _Tp,
typename _Alloc>
1342 operator==(
const forward_list<_Tp, _Alloc>& __lx,
1343 const forward_list<_Tp, _Alloc>& __ly);
1357 template<
typename _Tp,
typename _Alloc>
1359 operator<(const forward_list<_Tp, _Alloc>& __lx,
1362 __ly.cbegin(), __ly.cend()); }
1365 template<
typename _Tp,
typename _Alloc>
1369 {
return !(__lx == __ly); }
1372 template<
typename _Tp,
typename _Alloc>
1376 {
return (__ly < __lx); }
1379 template<
typename _Tp,
typename _Alloc>
1383 {
return !(__lx < __ly); }
1386 template<
typename _Tp,
typename _Alloc>
1388 operator<=(const forward_list<_Tp, _Alloc>& __lx,
1390 {
return !(__ly < __lx); }
1393 template<
typename _Tp,
typename _Alloc>
1397 { __lx.
swap(__ly); }
1399 _GLIBCXX_END_NAMESPACE_CONTAINER
1402 #endif // _FORWARD_LIST_H
void assign(_InputIterator __first, _InputIterator __last)
Assigns a range to a forward_list.
const_iterator cend() const noexcept
void resize(size_type __sz)
Resizes the forward_list to the specified number of elements.
forward_list(forward_list &&__list, const _Alloc &__al) noexcept(_Node_alloc_traits::_S_always_equal())
Move constructor with allocator argument.
forward_list(forward_list &&__list) noexcept
The forward_list move constructor.
void remove_if(_Pred __pred)
Remove all elements satisfying a predicate.
iterator before_begin() noexcept
A helper basic node class for forward_list. This is just a linked list with nothing inside it...
Uniform interface to C++98 and C++0x allocators.
void clear() noexcept
Erases all the elements.
const_iterator cbefore_begin() const noexcept
A forward_list::iterator.
forward_list(size_type __n, const _Alloc &__al=_Alloc())
Creates a forward_list with default constructed elements.
One of the comparison functors.
static pointer allocate(_Alloc &__a, size_type __n)
Allocate memory.
One of the comparison functors.
forward_list(const _Alloc &__al=_Alloc())
Creates a forward_list with no elements.
const_iterator before_begin() const noexcept
A standard container with linear time access to elements, and fixed time insertion/deletion at any po...
void unique()
Remove consecutive duplicate elements.
iterator emplace_after(const_iterator __pos, _Args &&...__args)
Constructs object in forward_list after the specified iterator.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
void sort()
Sort the elements of the list.
bool operator>=(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Test if string doesn't precede string.
Base class for forward_list.
const_iterator begin() const noexcept
iterator erase_after(const_iterator __pos)
Removes the element pointed to by the iterator following pos.
allocator_type get_allocator() const noexcept
Get a copy of the memory allocation object.
void assign(size_type __n, const _Tp &__val)
Assigns a given value to a forward_list.
size_type max_size() const noexcept
forward_list(_InputIterator __first, _InputIterator __last, const _Alloc &__al=_Alloc())
Builds a forward_list from a range.
iterator erase_after(const_iterator __pos, const_iterator __last)
Remove a range of elements.
ISO C++ entities toplevel namespace is std.
void merge(forward_list &&__list)
Merge sorted lists.
forward_list & operator=(forward_list &&__list) noexcept(_Node_alloc_traits::_S_nothrow_move())
The forward_list move assignment operator.
void reverse() noexcept
Reverse the elements in list.
bool lexicographical_compare(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2, _Compare __comp)
Performs dictionary comparison on ranges.
void emplace_front(_Args &&...__args)
Constructs object in forward_list at the front of the list.
~forward_list() noexcept
The forward_list dtor.
forward_list(std::initializer_list< _Tp > __il, const _Alloc &__al=_Alloc())
Builds a forward_list from an initializer_list.
A forward_list::const_iterator.
void swap(forward_list &__list) noexcept(_Node_alloc_traits::_S_nothrow_swap())
Swaps data with another forward_list.
void splice_after(const_iterator __pos, forward_list &&, const_iterator __before, const_iterator __last)
Insert range from another forward_list.
static size_type max_size(const _Alloc &__a)
The maximum supported allocation size.
forward_list(const forward_list &__list, const _Alloc &__al)
Copy constructor with allocator argument.
const_reference front() const
const_iterator end() const noexcept
Forward iterators support a superset of input iterator operations.
static void deallocate(_Alloc &__a, pointer __p, size_type __n)
Deallocate memory.
bool empty() const noexcept
forward_list & operator=(const forward_list &__list)
The forward_list assignment operator.
void push_front(const _Tp &__val)
Add data to the front of the forward_list.
void assign(std::initializer_list< _Tp > __il)
Assigns an initializer_list to a forward_list.
void swap(_Tp &, _Tp &) noexcept(__and_< is_nothrow_move_constructible< _Tp >, is_nothrow_move_assignable< _Tp >>::value)
Swaps two values.
void splice_after(const_iterator __pos, forward_list &&__list)
Insert contents of another forward_list.
forward_list(const forward_list &__list)
The forward_list copy constructor.
forward_list & operator=(std::initializer_list< _Tp > __il)
The forward_list initializer list assignment operator.
const_iterator cbegin() const noexcept
void pop_front()
Removes first element.
iterator insert_after(const_iterator __pos, std::initializer_list< _Tp > __il)
Inserts the contents of an initializer_list into forward_list after the specified iterator...
A helper node class for forward_list. This is just a linked list with uninitialized storage for a dat...
iterator begin() noexcept
bool operator>(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Test if string follows string.
forward_list(size_type __n, const _Tp &__value, const _Alloc &__al=_Alloc())
Creates a forward_list with copies of an exemplar element.
iterator insert_after(const_iterator __pos, const _Tp &__val)
Inserts given value into forward_list after specified iterator.