53 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
55 _GLIBCXX_BEGIN_NAMESPACE_VERSION
62 using std::__true_type;
63 using std::__false_type;
65 struct _Slist_node_base
67 _Slist_node_base* _M_next;
70 inline _Slist_node_base*
71 __slist_make_link(_Slist_node_base* __prev_node,
72 _Slist_node_base* __new_node)
74 __new_node->_M_next = __prev_node->_M_next;
75 __prev_node->_M_next = __new_node;
79 inline _Slist_node_base*
80 __slist_previous(_Slist_node_base* __head,
81 const _Slist_node_base* __node)
83 while (__head && __head->_M_next != __node)
84 __head = __head->_M_next;
88 inline const _Slist_node_base*
89 __slist_previous(
const _Slist_node_base* __head,
90 const _Slist_node_base* __node)
92 while (__head && __head->_M_next != __node)
93 __head = __head->_M_next;
98 __slist_splice_after(_Slist_node_base* __pos,
99 _Slist_node_base* __before_first,
100 _Slist_node_base* __before_last)
102 if (__pos != __before_first && __pos != __before_last)
104 _Slist_node_base* __first = __before_first->_M_next;
105 _Slist_node_base* __after = __pos->_M_next;
106 __before_first->_M_next = __before_last->_M_next;
107 __pos->_M_next = __first;
108 __before_last->_M_next = __after;
113 __slist_splice_after(_Slist_node_base* __pos, _Slist_node_base* __head)
115 _Slist_node_base* __before_last = __slist_previous(__head, 0);
116 if (__before_last != __head)
118 _Slist_node_base* __after = __pos->_M_next;
119 __pos->_M_next = __head->_M_next;
121 __before_last->_M_next = __after;
125 inline _Slist_node_base*
126 __slist_reverse(_Slist_node_base* __node)
128 _Slist_node_base* __result = __node;
129 __node = __node->_M_next;
130 __result->_M_next = 0;
133 _Slist_node_base* __next = __node->_M_next;
134 __node->_M_next = __result;
142 __slist_size(_Slist_node_base* __node)
145 for (; __node != 0; __node = __node->_M_next)
151 struct _Slist_node :
public _Slist_node_base
156 struct _Slist_iterator_base
158 typedef size_t size_type;
159 typedef ptrdiff_t difference_type;
162 _Slist_node_base* _M_node;
164 _Slist_iterator_base(_Slist_node_base* __x)
169 { _M_node = _M_node->_M_next; }
172 operator==(
const _Slist_iterator_base& __x)
const
173 {
return _M_node == __x._M_node; }
176 operator!=(
const _Slist_iterator_base& __x)
const
177 {
return _M_node != __x._M_node; }
180 template <
class _Tp,
class _Ref,
class _Ptr>
181 struct _Slist_iterator :
public _Slist_iterator_base
183 typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
184 typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
185 typedef _Slist_iterator<_Tp, _Ref, _Ptr> _Self;
187 typedef _Tp value_type;
188 typedef _Ptr pointer;
189 typedef _Ref reference;
190 typedef _Slist_node<_Tp> _Node;
193 _Slist_iterator(_Node* __x)
194 : _Slist_iterator_base(__x) {}
197 : _Slist_iterator_base(0) {}
199 _Slist_iterator(
const iterator& __x)
200 : _Slist_iterator_base(__x._M_node) {}
204 {
return ((_Node*) _M_node)->_M_data; }
208 {
return &(operator*()); }
226 template <
class _Tp,
class _Alloc>
228 :
public _Alloc::template rebind<_Slist_node<_Tp> >::other
230 typedef typename _Alloc::template rebind<_Slist_node<_Tp> >::other
232 typedef _Alloc allocator_type;
235 get_allocator()
const
236 {
return *
static_cast<const _Node_alloc*
>(
this); }
238 _Slist_base(
const allocator_type& __a)
240 { this->_M_head._M_next = 0; }
243 { _M_erase_after(&this->_M_head, 0); }
246 _Slist_node_base _M_head;
250 {
return _Node_alloc::allocate(1); }
253 _M_put_node(_Slist_node<_Tp>* __p)
254 { _Node_alloc::deallocate(__p, 1); }
257 _Slist_node_base* _M_erase_after(_Slist_node_base* __pos)
259 _Slist_node<_Tp>* __next = (_Slist_node<_Tp>*) (__pos->_M_next);
260 _Slist_node_base* __next_next = __next->_M_next;
261 __pos->_M_next = __next_next;
262 get_allocator().destroy(&__next->_M_data);
266 _Slist_node_base* _M_erase_after(_Slist_node_base*, _Slist_node_base*);
269 template <
class _Tp,
class _Alloc>
271 _Slist_base<_Tp,_Alloc>::_M_erase_after(_Slist_node_base* __before_first,
272 _Slist_node_base* __last_node)
274 _Slist_node<_Tp>* __cur = (_Slist_node<_Tp>*) (__before_first->_M_next);
275 while (__cur != __last_node)
277 _Slist_node<_Tp>* __tmp = __cur;
278 __cur = (_Slist_node<_Tp>*) __cur->_M_next;
279 get_allocator().destroy(&__tmp->_M_data);
282 __before_first->_M_next = __last_node;
291 template <
class _Tp,
class _Alloc = allocator<_Tp> >
292 class slist :
private _Slist_base<_Tp,_Alloc>
295 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
298 typedef _Slist_base<_Tp,_Alloc> _Base;
301 typedef _Tp value_type;
302 typedef value_type* pointer;
303 typedef const value_type* const_pointer;
304 typedef value_type& reference;
305 typedef const value_type& const_reference;
306 typedef size_t size_type;
307 typedef ptrdiff_t difference_type;
309 typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
310 typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
312 typedef typename _Base::allocator_type allocator_type;
315 get_allocator()
const
316 {
return _Base::get_allocator(); }
319 typedef _Slist_node<_Tp> _Node;
320 typedef _Slist_node_base _Node_base;
321 typedef _Slist_iterator_base _Iterator_base;
324 _M_create_node(
const value_type& __x)
326 _Node* __node = this->_M_get_node();
329 get_allocator().construct(&__node->_M_data, __x);
334 this->_M_put_node(__node);
335 __throw_exception_again;
343 _Node* __node = this->_M_get_node();
346 get_allocator().construct(&__node->_M_data, value_type());
351 this->_M_put_node(__node);
352 __throw_exception_again;
359 slist(
const allocator_type& __a = allocator_type())
362 slist(size_type __n,
const value_type& __x,
363 const allocator_type& __a = allocator_type())
365 { _M_insert_after_fill(&this->_M_head, __n, __x); }
369 : _Base(allocator_type())
370 { _M_insert_after_fill(&this->_M_head, __n, value_type()); }
374 template <
class _InputIterator>
375 slist(_InputIterator __first, _InputIterator __last,
376 const allocator_type& __a = allocator_type())
378 { _M_insert_after_range(&this->_M_head, __first, __last); }
381 : _Base(__x.get_allocator())
382 { _M_insert_after_range(&this->_M_head, __x.begin(), __x.end()); }
385 operator= (
const slist& __x);
396 assign(size_type __n,
const _Tp& __val)
397 { _M_fill_assign(__n, __val); }
400 _M_fill_assign(size_type __n,
const _Tp& __val);
402 template <
class _InputIterator>
404 assign(_InputIterator __first, _InputIterator __last)
406 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
407 _M_assign_dispatch(__first, __last, _Integral());
410 template <
class _Integer>
412 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
413 { _M_fill_assign((size_type) __n, (_Tp) __val); }
415 template <
class _InputIterator>
417 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
424 {
return iterator((_Node*)this->_M_head._M_next); }
428 {
return const_iterator((_Node*)this->_M_head._M_next);}
432 {
return iterator(0); }
436 {
return const_iterator(0); }
447 {
return iterator((_Node*) &this->_M_head); }
451 {
return const_iterator((_Node*) &this->_M_head); }
455 {
return __slist_size(this->_M_head._M_next); }
459 {
return size_type(-1); }
463 {
return this->_M_head._M_next == 0; }
467 {
std::swap(this->_M_head._M_next, __x._M_head._M_next); }
473 {
return ((_Node*) this->_M_head._M_next)->_M_data; }
477 {
return ((_Node*) this->_M_head._M_next)->_M_data; }
480 push_front(
const value_type& __x)
481 { __slist_make_link(&this->_M_head, _M_create_node(__x)); }
485 { __slist_make_link(&this->_M_head, _M_create_node()); }
490 _Node* __node = (_Node*) this->_M_head._M_next;
491 this->_M_head._M_next = __node->_M_next;
492 get_allocator().destroy(&__node->_M_data);
493 this->_M_put_node(__node);
497 previous(const_iterator __pos)
498 {
return iterator((_Node*) __slist_previous(&this->_M_head,
502 previous(const_iterator __pos)
const
503 {
return const_iterator((_Node*) __slist_previous(&this->_M_head,
508 _M_insert_after(_Node_base* __pos,
const value_type& __x)
509 {
return (_Node*) (__slist_make_link(__pos, _M_create_node(__x))); }
512 _M_insert_after(_Node_base* __pos)
513 {
return (_Node*) (__slist_make_link(__pos, _M_create_node())); }
516 _M_insert_after_fill(_Node_base* __pos,
517 size_type __n,
const value_type& __x)
519 for (size_type __i = 0; __i < __n; ++__i)
520 __pos = __slist_make_link(__pos, _M_create_node(__x));
524 template <
class _InIterator>
526 _M_insert_after_range(_Node_base* __pos,
527 _InIterator __first, _InIterator __last)
529 typedef typename std::__is_integer<_InIterator>::__type _Integral;
530 _M_insert_after_range(__pos, __first, __last, _Integral());
533 template <
class _Integer>
535 _M_insert_after_range(_Node_base* __pos, _Integer __n, _Integer __x,
537 { _M_insert_after_fill(__pos, __n, __x); }
539 template <
class _InIterator>
541 _M_insert_after_range(_Node_base* __pos,
542 _InIterator __first, _InIterator __last,
545 while (__first != __last)
547 __pos = __slist_make_link(__pos, _M_create_node(*__first));
554 insert_after(iterator __pos,
const value_type& __x)
555 {
return iterator(_M_insert_after(__pos._M_node, __x)); }
558 insert_after(iterator __pos)
559 {
return insert_after(__pos, value_type()); }
562 insert_after(iterator __pos, size_type __n,
const value_type& __x)
563 { _M_insert_after_fill(__pos._M_node, __n, __x); }
567 template <
class _InIterator>
569 insert_after(iterator __pos, _InIterator __first, _InIterator __last)
570 { _M_insert_after_range(__pos._M_node, __first, __last); }
573 insert(iterator __pos,
const value_type& __x)
574 {
return iterator(_M_insert_after(__slist_previous(&this->_M_head,
579 insert(iterator __pos)
580 {
return iterator(_M_insert_after(__slist_previous(&this->_M_head,
585 insert(iterator __pos, size_type __n,
const value_type& __x)
586 { _M_insert_after_fill(__slist_previous(&this->_M_head, __pos._M_node),
591 template <
class _InIterator>
593 insert(iterator __pos, _InIterator __first, _InIterator __last)
594 { _M_insert_after_range(__slist_previous(&this->_M_head, __pos._M_node),
599 erase_after(iterator __pos)
600 {
return iterator((_Node*) this->_M_erase_after(__pos._M_node)); }
603 erase_after(iterator __before_first, iterator __last)
605 return iterator((_Node*) this->_M_erase_after(__before_first._M_node,
610 erase(iterator __pos)
612 return iterator((_Node*) this->_M_erase_after
613 (__slist_previous(&this->_M_head, __pos._M_node)));
617 erase(iterator __first, iterator __last)
619 return iterator((_Node*) this->_M_erase_after
620 (__slist_previous(&this->_M_head, __first._M_node),
625 resize(size_type new_size,
const _Tp& __x);
628 resize(size_type new_size)
629 { resize(new_size, _Tp()); }
633 { this->_M_erase_after(&this->_M_head, 0); }
639 splice_after(iterator __pos,
640 iterator __before_first, iterator __before_last)
642 if (__before_first != __before_last)
643 __slist_splice_after(__pos._M_node, __before_first._M_node,
644 __before_last._M_node);
650 splice_after(iterator __pos, iterator __prev)
651 { __slist_splice_after(__pos._M_node,
652 __prev._M_node, __prev._M_node->_M_next); }
658 splice_after(iterator __pos,
slist& __x)
659 { __slist_splice_after(__pos._M_node, &__x._M_head); }
663 splice(iterator __pos,
slist& __x)
665 if (__x._M_head._M_next)
666 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
668 __slist_previous(&__x._M_head, 0)); }
672 splice(iterator __pos,
slist& __x, iterator __i)
673 { __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
674 __slist_previous(&__x._M_head, __i._M_node),
680 splice(iterator __pos,
slist& __x, iterator __first, iterator __last)
682 if (__first != __last)
683 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
684 __slist_previous(&__x._M_head, __first._M_node),
685 __slist_previous(__first._M_node,
693 if (this->_M_head._M_next)
694 this->_M_head._M_next = __slist_reverse(this->_M_head._M_next);
698 remove(
const _Tp& __val);
709 template <
class _Predicate>
711 remove_if(_Predicate __pred);
713 template <
class _BinaryPredicate>
715 unique(_BinaryPredicate __pred);
717 template <
class _StrictWeakOrdering>
719 merge(
slist&, _StrictWeakOrdering);
721 template <
class _StrictWeakOrdering>
723 sort(_StrictWeakOrdering __comp);
726 template <
class _Tp,
class _Alloc>
732 _Node_base* __p1 = &this->_M_head;
733 _Node* __n1 = (_Node*) this->_M_head._M_next;
734 const _Node* __n2 = (
const _Node*) __x._M_head._M_next;
737 __n1->_M_data = __n2->_M_data;
739 __n1 = (_Node*) __n1->_M_next;
740 __n2 = (
const _Node*) __n2->_M_next;
743 this->_M_erase_after(__p1, 0);
745 _M_insert_after_range(__p1, const_iterator((_Node*)__n2),
751 template <
class _Tp,
class _Alloc>
753 slist<_Tp, _Alloc>::_M_fill_assign(size_type __n,
const _Tp& __val)
755 _Node_base* __prev = &this->_M_head;
756 _Node* __node = (_Node*) this->_M_head._M_next;
757 for (; __node != 0 && __n > 0; --__n)
759 __node->_M_data = __val;
761 __node = (_Node*) __node->_M_next;
764 _M_insert_after_fill(__prev, __n, __val);
766 this->_M_erase_after(__prev, 0);
769 template <
class _Tp,
class _Alloc>
770 template <
class _InputIterator>
772 slist<_Tp, _Alloc>::_M_assign_dispatch(_InputIterator __first,
773 _InputIterator __last,
776 _Node_base* __prev = &this->_M_head;
777 _Node* __node = (_Node*) this->_M_head._M_next;
778 while (__node != 0 && __first != __last)
780 __node->_M_data = *__first;
782 __node = (_Node*) __node->_M_next;
785 if (__first != __last)
786 _M_insert_after_range(__prev, __first, __last);
788 this->_M_erase_after(__prev, 0);
791 template <
class _Tp,
class _Alloc>
793 operator==(
const slist<_Tp, _Alloc>& _SL1,
const slist<_Tp, _Alloc>& _SL2)
795 typedef typename slist<_Tp,_Alloc>::const_iterator const_iterator;
796 const_iterator __end1 = _SL1.end();
797 const_iterator __end2 = _SL2.end();
799 const_iterator __i1 = _SL1.begin();
800 const_iterator __i2 = _SL2.begin();
801 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2)
806 return __i1 == __end1 && __i2 == __end2;
810 template <
class _Tp,
class _Alloc>
812 operator<(const slist<_Tp, _Alloc>& _SL1,
const slist<_Tp, _Alloc>& _SL2)
814 _SL2.begin(), _SL2.end()); }
816 template <
class _Tp,
class _Alloc>
818 operator!=(
const slist<_Tp, _Alloc>& _SL1,
const slist<_Tp, _Alloc>& _SL2)
819 {
return !(_SL1 == _SL2); }
821 template <
class _Tp,
class _Alloc>
823 operator>(
const slist<_Tp, _Alloc>& _SL1,
const slist<_Tp, _Alloc>& _SL2)
824 {
return _SL2 < _SL1; }
826 template <
class _Tp,
class _Alloc>
828 operator<=(const slist<_Tp, _Alloc>& _SL1,
const slist<_Tp, _Alloc>& _SL2)
829 {
return !(_SL2 < _SL1); }
831 template <
class _Tp,
class _Alloc>
833 operator>=(
const slist<_Tp, _Alloc>& _SL1,
const slist<_Tp, _Alloc>& _SL2)
834 {
return !(_SL1 < _SL2); }
836 template <
class _Tp,
class _Alloc>
838 swap(slist<_Tp, _Alloc>& __x, slist<_Tp, _Alloc>& __y)
841 template <
class _Tp,
class _Alloc>
843 slist<_Tp, _Alloc>::resize(size_type __len,
const _Tp& __x)
845 _Node_base* __cur = &this->_M_head;
846 while (__cur->_M_next != 0 && __len > 0)
849 __cur = __cur->_M_next;
852 this->_M_erase_after(__cur, 0);
854 _M_insert_after_fill(__cur, __len, __x);
857 template <
class _Tp,
class _Alloc>
859 slist<_Tp, _Alloc>::remove(
const _Tp& __val)
861 _Node_base* __cur = &this->_M_head;
862 while (__cur && __cur->_M_next)
864 if (((_Node*) __cur->_M_next)->_M_data == __val)
865 this->_M_erase_after(__cur);
867 __cur = __cur->_M_next;
871 template <
class _Tp,
class _Alloc>
873 slist<_Tp, _Alloc>::unique()
875 _Node_base* __cur = this->_M_head._M_next;
878 while (__cur->_M_next)
880 if (((_Node*)__cur)->_M_data
881 == ((_Node*)(__cur->_M_next))->_M_data)
882 this->_M_erase_after(__cur);
884 __cur = __cur->_M_next;
889 template <
class _Tp,
class _Alloc>
891 slist<_Tp, _Alloc>::merge(slist<_Tp, _Alloc>& __x)
893 _Node_base* __n1 = &this->_M_head;
894 while (__n1->_M_next && __x._M_head._M_next)
896 if (((_Node*) __x._M_head._M_next)->_M_data
897 < ((_Node*) __n1->_M_next)->_M_data)
898 __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
899 __n1 = __n1->_M_next;
901 if (__x._M_head._M_next)
903 __n1->_M_next = __x._M_head._M_next;
904 __x._M_head._M_next = 0;
908 template <
class _Tp,
class _Alloc>
910 slist<_Tp, _Alloc>::sort()
912 if (this->_M_head._M_next && this->_M_head._M_next->_M_next)
919 __slist_splice_after(&__carry._M_head,
920 &this->_M_head, this->_M_head._M_next);
922 while (__i < __fill && !__counter[__i].empty())
924 __counter[__i].merge(__carry);
925 __carry.swap(__counter[__i]);
928 __carry.swap(__counter[__i]);
933 for (
int __i = 1; __i < __fill; ++__i)
934 __counter[__i].merge(__counter[__i-1]);
935 this->
swap(__counter[__fill-1]);
939 template <
class _Tp,
class _Alloc>
940 template <
class _Predicate>
941 void slist<_Tp, _Alloc>::remove_if(_Predicate __pred)
943 _Node_base* __cur = &this->_M_head;
944 while (__cur->_M_next)
946 if (__pred(((_Node*) __cur->_M_next)->_M_data))
947 this->_M_erase_after(__cur);
949 __cur = __cur->_M_next;
953 template <
class _Tp,
class _Alloc>
954 template <
class _BinaryPredicate>
956 slist<_Tp, _Alloc>::unique(_BinaryPredicate __pred)
958 _Node* __cur = (_Node*) this->_M_head._M_next;
961 while (__cur->_M_next)
963 if (__pred(((_Node*)__cur)->_M_data,
964 ((_Node*)(__cur->_M_next))->_M_data))
965 this->_M_erase_after(__cur);
967 __cur = (_Node*) __cur->_M_next;
972 template <
class _Tp,
class _Alloc>
973 template <
class _StrictWeakOrdering>
975 slist<_Tp, _Alloc>::merge(slist<_Tp, _Alloc>& __x,
976 _StrictWeakOrdering __comp)
978 _Node_base* __n1 = &this->_M_head;
979 while (__n1->_M_next && __x._M_head._M_next)
981 if (__comp(((_Node*) __x._M_head._M_next)->_M_data,
982 ((_Node*) __n1->_M_next)->_M_data))
983 __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
984 __n1 = __n1->_M_next;
986 if (__x._M_head._M_next)
988 __n1->_M_next = __x._M_head._M_next;
989 __x._M_head._M_next = 0;
993 template <
class _Tp,
class _Alloc>
994 template <
class _StrictWeakOrdering>
996 slist<_Tp, _Alloc>::sort(_StrictWeakOrdering __comp)
998 if (this->_M_head._M_next && this->_M_head._M_next->_M_next)
1001 slist __counter[64];
1005 __slist_splice_after(&__carry._M_head,
1006 &this->_M_head, this->_M_head._M_next);
1008 while (__i < __fill && !__counter[__i].empty())
1010 __counter[__i].merge(__carry, __comp);
1011 __carry.swap(__counter[__i]);
1014 __carry.swap(__counter[__i]);
1019 for (
int __i = 1; __i < __fill; ++__i)
1020 __counter[__i].merge(__counter[__i-1], __comp);
1021 this->
swap(__counter[__fill-1]);
1025 _GLIBCXX_END_NAMESPACE_VERSION
1028 namespace std _GLIBCXX_VISIBILITY(default)
1030 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1034 template <
class _Tp,
class _Alloc>
1035 class insert_iterator<__gnu_cxx::slist<_Tp, _Alloc> >
1039 _Container* container;
1040 typename _Container::iterator iter;
1053 if (__i == __x.begin())
1054 iter = __x.before_begin();
1056 iter = __x.previous(__i);
1059 insert_iterator<_Container>&
1060 operator=(
const typename _Container::value_type& __value)
1062 iter = container->insert_after(iter, __value);
1066 insert_iterator<_Container>&
1070 insert_iterator<_Container>&
1074 insert_iterator<_Container>&
1079 _GLIBCXX_END_NAMESPACE_VERSION
Forward iterators support a superset of input iterator operations.
output_iterator_tag iterator_category
One of the tag types.
void reference
This type represents a reference-to-value_type.
insert_iterator & operator=(const typename _Container::value_type &__value)
void _Construct(_T1 *__p, _Args &&...__args)
_Container container_type
A nested typedef for the type of whatever container you used.
insert_iterator & operator*()
Simply returns *this.
void value_type
The type "pointed to" by the iterator.
insert_iterator & operator++()
Simply returns *this. (This iterator does not move.)
The standard allocator, as per [20.4].
void swap(function< _Res(_Args...)> &__x, function< _Res(_Args...)> &__y)
Swap the targets of two polymorphic function object wrappers.
void _Destroy(_Tp *__pointer)
bool lexicographical_compare(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2, _Compare __comp)
Performs dictionary comparison on ranges.
void pointer
This type represents a pointer-to-value_type.
void difference_type
Distance between iterators is represented as this type.
insert_iterator(_Container &__x, typename _Container::iterator __i)