54 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
56 _GLIBCXX_BEGIN_NAMESPACE_VERSION
63 using std::__true_type;
64 using std::__false_type;
66 struct _Slist_node_base
68 _Slist_node_base* _M_next;
71 inline _Slist_node_base*
72 __slist_make_link(_Slist_node_base* __prev_node,
73 _Slist_node_base* __new_node)
75 __new_node->_M_next = __prev_node->_M_next;
76 __prev_node->_M_next = __new_node;
80 inline _Slist_node_base*
81 __slist_previous(_Slist_node_base* __head,
82 const _Slist_node_base* __node)
84 while (__head && __head->_M_next != __node)
85 __head = __head->_M_next;
89 inline const _Slist_node_base*
90 __slist_previous(
const _Slist_node_base* __head,
91 const _Slist_node_base* __node)
93 while (__head && __head->_M_next != __node)
94 __head = __head->_M_next;
99 __slist_splice_after(_Slist_node_base* __pos,
100 _Slist_node_base* __before_first,
101 _Slist_node_base* __before_last)
103 if (__pos != __before_first && __pos != __before_last)
105 _Slist_node_base* __first = __before_first->_M_next;
106 _Slist_node_base* __after = __pos->_M_next;
107 __before_first->_M_next = __before_last->_M_next;
108 __pos->_M_next = __first;
109 __before_last->_M_next = __after;
114 __slist_splice_after(_Slist_node_base* __pos, _Slist_node_base* __head)
116 _Slist_node_base* __before_last = __slist_previous(__head, 0);
117 if (__before_last != __head)
119 _Slist_node_base* __after = __pos->_M_next;
120 __pos->_M_next = __head->_M_next;
122 __before_last->_M_next = __after;
126 inline _Slist_node_base*
127 __slist_reverse(_Slist_node_base* __node)
129 _Slist_node_base* __result = __node;
130 __node = __node->_M_next;
131 __result->_M_next = 0;
134 _Slist_node_base* __next = __node->_M_next;
135 __node->_M_next = __result;
143 __slist_size(_Slist_node_base* __node)
146 for (; __node != 0; __node = __node->_M_next)
152 struct _Slist_node :
public _Slist_node_base
157 struct _Slist_iterator_base
159 typedef size_t size_type;
160 typedef ptrdiff_t difference_type;
163 _Slist_node_base* _M_node;
165 _Slist_iterator_base(_Slist_node_base* __x)
170 { _M_node = _M_node->_M_next; }
173 operator==(
const _Slist_iterator_base& __x)
const
174 {
return _M_node == __x._M_node; }
177 operator!=(
const _Slist_iterator_base& __x)
const
178 {
return _M_node != __x._M_node; }
181 template <
class _Tp,
class _Ref,
class _Ptr>
182 struct _Slist_iterator :
public _Slist_iterator_base
184 typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
185 typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
186 typedef _Slist_iterator<_Tp, _Ref, _Ptr> _Self;
188 typedef _Tp value_type;
189 typedef _Ptr pointer;
190 typedef _Ref reference;
191 typedef _Slist_node<_Tp> _Node;
194 _Slist_iterator(_Node* __x)
195 : _Slist_iterator_base(__x) {}
198 : _Slist_iterator_base(0) {}
200 _Slist_iterator(
const iterator& __x)
201 : _Slist_iterator_base(__x._M_node) {}
205 {
return ((_Node*) _M_node)->_M_data; }
209 {
return &(operator*()); }
227 template <
class _Tp,
class _Alloc>
229 :
public _Alloc::template rebind<_Slist_node<_Tp> >::other
231 typedef typename _Alloc::template rebind<_Slist_node<_Tp> >::other
233 typedef _Alloc allocator_type;
236 get_allocator()
const
237 {
return *
static_cast<const _Node_alloc*
>(
this); }
239 _Slist_base(
const allocator_type& __a)
241 { this->_M_head._M_next = 0; }
244 { _M_erase_after(&this->_M_head, 0); }
247 _Slist_node_base _M_head;
251 {
return _Node_alloc::allocate(1); }
254 _M_put_node(_Slist_node<_Tp>* __p)
255 { _Node_alloc::deallocate(__p, 1); }
258 _Slist_node_base* _M_erase_after(_Slist_node_base* __pos)
260 _Slist_node<_Tp>* __next = (_Slist_node<_Tp>*) (__pos->_M_next);
261 _Slist_node_base* __next_next = __next->_M_next;
262 __pos->_M_next = __next_next;
263 get_allocator().destroy(&__next->_M_data);
267 _Slist_node_base* _M_erase_after(_Slist_node_base*, _Slist_node_base*);
270 template <
class _Tp,
class _Alloc>
272 _Slist_base<_Tp,_Alloc>::_M_erase_after(_Slist_node_base* __before_first,
273 _Slist_node_base* __last_node)
275 _Slist_node<_Tp>* __cur = (_Slist_node<_Tp>*) (__before_first->_M_next);
276 while (__cur != __last_node)
278 _Slist_node<_Tp>* __tmp = __cur;
279 __cur = (_Slist_node<_Tp>*) __cur->_M_next;
280 get_allocator().destroy(&__tmp->_M_data);
283 __before_first->_M_next = __last_node;
292 template <
class _Tp,
class _Alloc = allocator<_Tp> >
293 class slist :
private _Slist_base<_Tp,_Alloc>
296 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
299 typedef _Slist_base<_Tp,_Alloc> _Base;
302 typedef _Tp value_type;
303 typedef value_type* pointer;
304 typedef const value_type* const_pointer;
305 typedef value_type& reference;
306 typedef const value_type& const_reference;
307 typedef size_t size_type;
308 typedef ptrdiff_t difference_type;
310 typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
311 typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
313 typedef typename _Base::allocator_type allocator_type;
316 get_allocator()
const
317 {
return _Base::get_allocator(); }
320 typedef _Slist_node<_Tp> _Node;
321 typedef _Slist_node_base _Node_base;
322 typedef _Slist_iterator_base _Iterator_base;
325 _M_create_node(
const value_type& __x)
327 _Node* __node = this->_M_get_node();
330 get_allocator().construct(&__node->_M_data, __x);
335 this->_M_put_node(__node);
336 __throw_exception_again;
344 _Node* __node = this->_M_get_node();
347 get_allocator().construct(&__node->_M_data, value_type());
352 this->_M_put_node(__node);
353 __throw_exception_again;
360 slist(
const allocator_type& __a = allocator_type())
363 slist(size_type __n,
const value_type& __x,
364 const allocator_type& __a = allocator_type())
366 { _M_insert_after_fill(&this->_M_head, __n, __x); }
370 : _Base(allocator_type())
371 { _M_insert_after_fill(&this->_M_head, __n, value_type()); }
375 template <
class _InputIterator>
376 slist(_InputIterator __first, _InputIterator __last,
377 const allocator_type& __a = allocator_type())
379 { _M_insert_after_range(&this->_M_head, __first, __last); }
382 : _Base(__x.get_allocator())
383 { _M_insert_after_range(&this->_M_head, __x.begin(), __x.end()); }
386 operator= (
const slist& __x);
397 assign(size_type __n,
const _Tp& __val)
398 { _M_fill_assign(__n, __val); }
401 _M_fill_assign(size_type __n,
const _Tp& __val);
403 template <
class _InputIterator>
405 assign(_InputIterator __first, _InputIterator __last)
407 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
408 _M_assign_dispatch(__first, __last, _Integral());
411 template <
class _Integer>
413 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
414 { _M_fill_assign((size_type) __n, (_Tp) __val); }
416 template <
class _InputIterator>
418 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
425 {
return iterator((_Node*)this->_M_head._M_next); }
429 {
return const_iterator((_Node*)this->_M_head._M_next);}
433 {
return iterator(0); }
437 {
return const_iterator(0); }
448 {
return iterator((_Node*) &this->_M_head); }
452 {
return const_iterator((_Node*) &this->_M_head); }
456 {
return __slist_size(this->_M_head._M_next); }
460 {
return size_type(-1); }
464 {
return this->_M_head._M_next == 0; }
468 { std::swap(this->_M_head._M_next, __x._M_head._M_next); }
474 {
return ((_Node*) this->_M_head._M_next)->_M_data; }
478 {
return ((_Node*) this->_M_head._M_next)->_M_data; }
481 push_front(
const value_type& __x)
482 { __slist_make_link(&this->_M_head, _M_create_node(__x)); }
486 { __slist_make_link(&this->_M_head, _M_create_node()); }
491 _Node* __node = (_Node*) this->_M_head._M_next;
492 this->_M_head._M_next = __node->_M_next;
493 get_allocator().destroy(&__node->_M_data);
494 this->_M_put_node(__node);
498 previous(const_iterator __pos)
499 {
return iterator((_Node*) __slist_previous(&this->_M_head,
503 previous(const_iterator __pos)
const
504 {
return const_iterator((_Node*) __slist_previous(&this->_M_head,
509 _M_insert_after(_Node_base* __pos,
const value_type& __x)
510 {
return (_Node*) (__slist_make_link(__pos, _M_create_node(__x))); }
513 _M_insert_after(_Node_base* __pos)
514 {
return (_Node*) (__slist_make_link(__pos, _M_create_node())); }
517 _M_insert_after_fill(_Node_base* __pos,
518 size_type __n,
const value_type& __x)
520 for (size_type __i = 0; __i < __n; ++__i)
521 __pos = __slist_make_link(__pos, _M_create_node(__x));
525 template <
class _InIterator>
527 _M_insert_after_range(_Node_base* __pos,
528 _InIterator __first, _InIterator __last)
530 typedef typename std::__is_integer<_InIterator>::__type _Integral;
531 _M_insert_after_range(__pos, __first, __last, _Integral());
534 template <
class _Integer>
536 _M_insert_after_range(_Node_base* __pos, _Integer __n, _Integer __x,
538 { _M_insert_after_fill(__pos, __n, __x); }
540 template <
class _InIterator>
542 _M_insert_after_range(_Node_base* __pos,
543 _InIterator __first, _InIterator __last,
546 while (__first != __last)
548 __pos = __slist_make_link(__pos, _M_create_node(*__first));
555 insert_after(iterator __pos,
const value_type& __x)
556 {
return iterator(_M_insert_after(__pos._M_node, __x)); }
559 insert_after(iterator __pos)
560 {
return insert_after(__pos, value_type()); }
563 insert_after(iterator __pos, size_type __n,
const value_type& __x)
564 { _M_insert_after_fill(__pos._M_node, __n, __x); }
568 template <
class _InIterator>
570 insert_after(iterator __pos, _InIterator __first, _InIterator __last)
571 { _M_insert_after_range(__pos._M_node, __first, __last); }
574 insert(iterator __pos,
const value_type& __x)
575 {
return iterator(_M_insert_after(__slist_previous(&this->_M_head,
580 insert(iterator __pos)
581 {
return iterator(_M_insert_after(__slist_previous(&this->_M_head,
586 insert(iterator __pos, size_type __n,
const value_type& __x)
587 { _M_insert_after_fill(__slist_previous(&this->_M_head, __pos._M_node),
592 template <
class _InIterator>
594 insert(iterator __pos, _InIterator __first, _InIterator __last)
595 { _M_insert_after_range(__slist_previous(&this->_M_head, __pos._M_node),
600 erase_after(iterator __pos)
601 {
return iterator((_Node*) this->_M_erase_after(__pos._M_node)); }
604 erase_after(iterator __before_first, iterator __last)
606 return iterator((_Node*) this->_M_erase_after(__before_first._M_node,
611 erase(iterator __pos)
613 return iterator((_Node*) this->_M_erase_after
614 (__slist_previous(&this->_M_head, __pos._M_node)));
618 erase(iterator __first, iterator __last)
620 return iterator((_Node*) this->_M_erase_after
621 (__slist_previous(&this->_M_head, __first._M_node),
626 resize(size_type new_size,
const _Tp& __x);
629 resize(size_type new_size)
630 { resize(new_size, _Tp()); }
634 { this->_M_erase_after(&this->_M_head, 0); }
640 splice_after(iterator __pos,
641 iterator __before_first, iterator __before_last)
643 if (__before_first != __before_last)
644 __slist_splice_after(__pos._M_node, __before_first._M_node,
645 __before_last._M_node);
651 splice_after(iterator __pos, iterator __prev)
652 { __slist_splice_after(__pos._M_node,
653 __prev._M_node, __prev._M_node->_M_next); }
659 splice_after(iterator __pos,
slist& __x)
660 { __slist_splice_after(__pos._M_node, &__x._M_head); }
664 splice(iterator __pos,
slist& __x)
666 if (__x._M_head._M_next)
667 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
669 __slist_previous(&__x._M_head, 0)); }
673 splice(iterator __pos,
slist& __x, iterator __i)
674 { __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
675 __slist_previous(&__x._M_head, __i._M_node),
681 splice(iterator __pos,
slist& __x, iterator __first, iterator __last)
683 if (__first != __last)
684 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
685 __slist_previous(&__x._M_head, __first._M_node),
686 __slist_previous(__first._M_node,
694 if (this->_M_head._M_next)
695 this->_M_head._M_next = __slist_reverse(this->_M_head._M_next);
699 remove(
const _Tp& __val);
710 template <
class _Predicate>
712 remove_if(_Predicate __pred);
714 template <
class _BinaryPredicate>
716 unique(_BinaryPredicate __pred);
718 template <
class _StrictWeakOrdering>
720 merge(
slist&, _StrictWeakOrdering);
722 template <
class _StrictWeakOrdering>
724 sort(_StrictWeakOrdering __comp);
727 template <
class _Tp,
class _Alloc>
733 _Node_base* __p1 = &this->_M_head;
734 _Node* __n1 = (_Node*) this->_M_head._M_next;
735 const _Node* __n2 = (
const _Node*) __x._M_head._M_next;
738 __n1->_M_data = __n2->_M_data;
740 __n1 = (_Node*) __n1->_M_next;
741 __n2 = (
const _Node*) __n2->_M_next;
744 this->_M_erase_after(__p1, 0);
746 _M_insert_after_range(__p1, const_iterator((_Node*)__n2),
752 template <
class _Tp,
class _Alloc>
754 slist<_Tp, _Alloc>::_M_fill_assign(size_type __n,
const _Tp& __val)
756 _Node_base* __prev = &this->_M_head;
757 _Node* __node = (_Node*) this->_M_head._M_next;
758 for (; __node != 0 && __n > 0; --__n)
760 __node->_M_data = __val;
762 __node = (_Node*) __node->_M_next;
765 _M_insert_after_fill(__prev, __n, __val);
767 this->_M_erase_after(__prev, 0);
770 template <
class _Tp,
class _Alloc>
771 template <
class _InputIterator>
773 slist<_Tp, _Alloc>::_M_assign_dispatch(_InputIterator __first,
774 _InputIterator __last,
777 _Node_base* __prev = &this->_M_head;
778 _Node* __node = (_Node*) this->_M_head._M_next;
779 while (__node != 0 && __first != __last)
781 __node->_M_data = *__first;
783 __node = (_Node*) __node->_M_next;
786 if (__first != __last)
787 _M_insert_after_range(__prev, __first, __last);
789 this->_M_erase_after(__prev, 0);
792 template <
class _Tp,
class _Alloc>
794 operator==(
const slist<_Tp, _Alloc>& _SL1,
const slist<_Tp, _Alloc>& _SL2)
796 typedef typename slist<_Tp,_Alloc>::const_iterator const_iterator;
797 const_iterator __end1 = _SL1.end();
798 const_iterator __end2 = _SL2.end();
800 const_iterator __i1 = _SL1.begin();
801 const_iterator __i2 = _SL2.begin();
802 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2)
807 return __i1 == __end1 && __i2 == __end2;
811 template <
class _Tp,
class _Alloc>
813 operator<(const slist<_Tp, _Alloc>& _SL1,
const slist<_Tp, _Alloc>& _SL2)
815 _SL2.begin(), _SL2.end()); }
817 template <
class _Tp,
class _Alloc>
819 operator!=(
const slist<_Tp, _Alloc>& _SL1,
const slist<_Tp, _Alloc>& _SL2)
820 {
return !(_SL1 == _SL2); }
822 template <
class _Tp,
class _Alloc>
824 operator>(
const slist<_Tp, _Alloc>& _SL1,
const slist<_Tp, _Alloc>& _SL2)
825 {
return _SL2 < _SL1; }
827 template <
class _Tp,
class _Alloc>
829 operator<=(const slist<_Tp, _Alloc>& _SL1,
const slist<_Tp, _Alloc>& _SL2)
830 {
return !(_SL2 < _SL1); }
832 template <
class _Tp,
class _Alloc>
834 operator>=(
const slist<_Tp, _Alloc>& _SL1,
const slist<_Tp, _Alloc>& _SL2)
835 {
return !(_SL1 < _SL2); }
837 template <
class _Tp,
class _Alloc>
839 swap(slist<_Tp, _Alloc>& __x, slist<_Tp, _Alloc>& __y)
842 template <
class _Tp,
class _Alloc>
844 slist<_Tp, _Alloc>::resize(size_type __len,
const _Tp& __x)
846 _Node_base* __cur = &this->_M_head;
847 while (__cur->_M_next != 0 && __len > 0)
850 __cur = __cur->_M_next;
853 this->_M_erase_after(__cur, 0);
855 _M_insert_after_fill(__cur, __len, __x);
858 template <
class _Tp,
class _Alloc>
860 slist<_Tp, _Alloc>::remove(
const _Tp& __val)
862 _Node_base* __cur = &this->_M_head;
863 while (__cur && __cur->_M_next)
865 if (((_Node*) __cur->_M_next)->_M_data == __val)
866 this->_M_erase_after(__cur);
868 __cur = __cur->_M_next;
872 template <
class _Tp,
class _Alloc>
874 slist<_Tp, _Alloc>::unique()
876 _Node_base* __cur = this->_M_head._M_next;
879 while (__cur->_M_next)
881 if (((_Node*)__cur)->_M_data
882 == ((_Node*)(__cur->_M_next))->_M_data)
883 this->_M_erase_after(__cur);
885 __cur = __cur->_M_next;
890 template <
class _Tp,
class _Alloc>
892 slist<_Tp, _Alloc>::merge(slist<_Tp, _Alloc>& __x)
894 _Node_base* __n1 = &this->_M_head;
895 while (__n1->_M_next && __x._M_head._M_next)
897 if (((_Node*) __x._M_head._M_next)->_M_data
898 < ((_Node*) __n1->_M_next)->_M_data)
899 __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
900 __n1 = __n1->_M_next;
902 if (__x._M_head._M_next)
904 __n1->_M_next = __x._M_head._M_next;
905 __x._M_head._M_next = 0;
909 template <
class _Tp,
class _Alloc>
911 slist<_Tp, _Alloc>::sort()
913 if (this->_M_head._M_next && this->_M_head._M_next->_M_next)
920 __slist_splice_after(&__carry._M_head,
921 &this->_M_head, this->_M_head._M_next);
923 while (__i < __fill && !__counter[__i].empty())
925 __counter[__i].merge(__carry);
926 __carry.swap(__counter[__i]);
929 __carry.swap(__counter[__i]);
934 for (
int __i = 1; __i < __fill; ++__i)
935 __counter[__i].merge(__counter[__i-1]);
936 this->swap(__counter[__fill-1]);
940 template <
class _Tp,
class _Alloc>
941 template <
class _Predicate>
942 void slist<_Tp, _Alloc>::remove_if(_Predicate __pred)
944 _Node_base* __cur = &this->_M_head;
945 while (__cur->_M_next)
947 if (__pred(((_Node*) __cur->_M_next)->_M_data))
948 this->_M_erase_after(__cur);
950 __cur = __cur->_M_next;
954 template <
class _Tp,
class _Alloc>
955 template <
class _BinaryPredicate>
957 slist<_Tp, _Alloc>::unique(_BinaryPredicate __pred)
959 _Node* __cur = (_Node*) this->_M_head._M_next;
962 while (__cur->_M_next)
964 if (__pred(((_Node*)__cur)->_M_data,
965 ((_Node*)(__cur->_M_next))->_M_data))
966 this->_M_erase_after(__cur);
968 __cur = (_Node*) __cur->_M_next;
973 template <
class _Tp,
class _Alloc>
974 template <
class _StrictWeakOrdering>
976 slist<_Tp, _Alloc>::merge(slist<_Tp, _Alloc>& __x,
977 _StrictWeakOrdering __comp)
979 _Node_base* __n1 = &this->_M_head;
980 while (__n1->_M_next && __x._M_head._M_next)
982 if (__comp(((_Node*) __x._M_head._M_next)->_M_data,
983 ((_Node*) __n1->_M_next)->_M_data))
984 __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
985 __n1 = __n1->_M_next;
987 if (__x._M_head._M_next)
989 __n1->_M_next = __x._M_head._M_next;
990 __x._M_head._M_next = 0;
994 template <
class _Tp,
class _Alloc>
995 template <
class _StrictWeakOrdering>
997 slist<_Tp, _Alloc>::sort(_StrictWeakOrdering __comp)
999 if (this->_M_head._M_next && this->_M_head._M_next->_M_next)
1002 slist __counter[64];
1006 __slist_splice_after(&__carry._M_head,
1007 &this->_M_head, this->_M_head._M_next);
1009 while (__i < __fill && !__counter[__i].empty())
1011 __counter[__i].merge(__carry, __comp);
1012 __carry.swap(__counter[__i]);
1015 __carry.swap(__counter[__i]);
1020 for (
int __i = 1; __i < __fill; ++__i)
1021 __counter[__i].merge(__counter[__i-1], __comp);
1022 this->swap(__counter[__fill-1]);
1026 _GLIBCXX_END_NAMESPACE_VERSION
1029 namespace std _GLIBCXX_VISIBILITY(default)
1031 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1035 template <
class _Tp,
class _Alloc>
1036 class insert_iterator<__gnu_cxx::slist<_Tp, _Alloc> >
1040 _Container* container;
1041 typename _Container::iterator iter;
1054 if (__i == __x.begin())
1055 iter = __x.before_begin();
1057 iter = __x.previous(__i);
1060 insert_iterator<_Container>&
1061 operator=(
const typename _Container::value_type& __value)
1063 iter = container->insert_after(iter, __value);
1067 insert_iterator<_Container>&
1071 insert_iterator<_Container>&
1075 insert_iterator<_Container>&
1080 _GLIBCXX_END_NAMESPACE_VERSION
insert_iterator(_Container &__x, typename _Container::iterator __i)
The standard allocator, as per [20.4].
insert_iterator & operator++()
Simply returns *this. (This iterator does not move.)
void _Destroy(_Tp *__pointer)
bool lexicographical_compare(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2, _Compare __comp)
Performs dictionary comparison on ranges.
insert_iterator & operator=(const typename _Container::value_type &__value)
insert_iterator & operator*()
Simply returns *this.
void value_type
The type "pointed to" by the iterator.
Forward iterators support a superset of input iterator operations.
void _Construct(_T1 *__p, _Args &&...__args)
output_iterator_tag iterator_category
One of the tag types.
_Container container_type
A nested typedef for the type of whatever container you used.
void reference
This type represents a reference-to-value_type.
void pointer
This type represents a pointer-to-value_type.
void difference_type
Distance between iterators is represented as this type.