1 // Singly-linked list implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
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 2, or (at your option)
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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
32 * Silicon Graphics Computer Systems, Inc.
34 * Permission to use, copy, modify, distribute and sell this software
35 * and its documentation for any purpose is hereby granted without fee,
36 * provided that the above copyright notice appear in all copies and
37 * that both that copyright notice and this permission notice appear
38 * in supporting documentation. Silicon Graphics makes no
39 * representations about the suitability of this software for any
40 * purpose. It is provided "as is" without express or implied warranty.
45 * This file is a GNU extension to the Standard C++ Library (possibly
46 * containing extensions from the HP/SGI STL subset).
52 #include <bits/stl_algobase.h>
53 #include <bits/allocator.h>
54 #include <bits/stl_construct.h>
55 #include <bits/stl_uninitialized.h>
56 #include <bits/concept_check.h>
62 using std::_Construct;
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;
161 typedef std::forward_iterator_tag iterator_category;
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;
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;
287 * This is an SGI extension.
288 * @ingroup SGIextensions
291 template <class _Tp, class _Alloc = allocator<_Tp> >
292 class slist : private _Slist_base<_Tp,_Alloc>
294 // concept requirements
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()); }
372 // We don't need any dispatching tricks here, because
373 // _M_insert_after_range already does them.
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); }
380 slist(const slist& __x)
381 : _Base(__x.get_allocator())
382 { _M_insert_after_range(&this->_M_head, __x.begin(), __x.end()); }
385 operator= (const slist& __x);
390 // assign(), a generalized assignment member function. Two
391 // versions: one that takes a count, and one that takes a range.
392 // The range version is a member template, so we dispatch on whether
393 // or not the type is an integer.
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); }
438 // Experimental new feature: before_begin() returns a
439 // non-dereferenceable iterator that, when incremented, yields
440 // begin(). This iterator may be used as the argument to
441 // insert_after, erase_after, etc. Note that even for an empty
442 // slist, before_begin() is not the same iterator as end(). It
443 // is always necessary to increment before_begin() at least once to
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));
523 // Check whether it's an integral type. If so, it's not an iterator.
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); }
565 // We don't need any dispatching tricks here, because
566 // _M_insert_after_range already does them.
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),
589 // We don't need any dispatching tricks here, because
590 // _M_insert_after_range already does them.
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)
604 { return iterator((_Node*) this->_M_erase_after(__before_first._M_node,
608 erase(iterator __pos)
609 { return (_Node*) this->_M_erase_after(__slist_previous(&this->_M_head,
613 erase(iterator __first, iterator __last)
614 { return (_Node*) this->_M_erase_after(__slist_previous(&this->_M_head,
619 resize(size_type new_size, const _Tp& __x);
622 resize(size_type new_size)
623 { resize(new_size, _Tp()); }
627 { this->_M_erase_after(&this->_M_head, 0); }
630 // Moves the range [__before_first + 1, __before_last + 1) to *this,
631 // inserting it immediately after __pos. This is constant time.
633 splice_after(iterator __pos,
634 iterator __before_first, iterator __before_last)
636 if (__before_first != __before_last)
637 __slist_splice_after(__pos._M_node, __before_first._M_node,
638 __before_last._M_node);
641 // Moves the element that follows __prev to *this, inserting it
642 // immediately after __pos. This is constant time.
644 splice_after(iterator __pos, iterator __prev)
645 { __slist_splice_after(__pos._M_node,
646 __prev._M_node, __prev._M_node->_M_next); }
648 // Removes all of the elements from the list __x to *this, inserting
649 // them immediately after __pos. __x must not be *this. Complexity:
650 // linear in __x.size().
652 splice_after(iterator __pos, slist& __x)
653 { __slist_splice_after(__pos._M_node, &__x._M_head); }
655 // Linear in distance(begin(), __pos), and linear in __x.size().
657 splice(iterator __pos, slist& __x)
659 if (__x._M_head._M_next)
660 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
662 __slist_previous(&__x._M_head, 0)); }
664 // Linear in distance(begin(), __pos), and in distance(__x.begin(), __i).
666 splice(iterator __pos, slist& __x, iterator __i)
667 { __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
668 __slist_previous(&__x._M_head, __i._M_node),
671 // Linear in distance(begin(), __pos), in distance(__x.begin(), __first),
672 // and in distance(__first, __last).
674 splice(iterator __pos, slist& __x, iterator __first, iterator __last)
676 if (__first != __last)
677 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
678 __slist_previous(&__x._M_head, __first._M_node),
679 __slist_previous(__first._M_node,
687 if (this->_M_head._M_next)
688 this->_M_head._M_next = __slist_reverse(this->_M_head._M_next);
692 remove(const _Tp& __val);
703 template <class _Predicate>
705 remove_if(_Predicate __pred);
707 template <class _BinaryPredicate>
709 unique(_BinaryPredicate __pred);
711 template <class _StrictWeakOrdering>
713 merge(slist&, _StrictWeakOrdering);
715 template <class _StrictWeakOrdering>
717 sort(_StrictWeakOrdering __comp);
720 template <class _Tp, class _Alloc>
722 slist<_Tp, _Alloc>::operator=(const slist<_Tp, _Alloc>& __x)
726 _Node_base* __p1 = &this->_M_head;
727 _Node* __n1 = (_Node*) this->_M_head._M_next;
728 const _Node* __n2 = (const _Node*) __x._M_head._M_next;
731 __n1->_M_data = __n2->_M_data;
733 __n1 = (_Node*) __n1->_M_next;
734 __n2 = (const _Node*) __n2->_M_next;
737 this->_M_erase_after(__p1, 0);
739 _M_insert_after_range(__p1, const_iterator((_Node*)__n2),
745 template <class _Tp, class _Alloc>
747 slist<_Tp, _Alloc>::_M_fill_assign(size_type __n, const _Tp& __val)
749 _Node_base* __prev = &this->_M_head;
750 _Node* __node = (_Node*) this->_M_head._M_next;
751 for (; __node != 0 && __n > 0; --__n)
753 __node->_M_data = __val;
755 __node = (_Node*) __node->_M_next;
758 _M_insert_after_fill(__prev, __n, __val);
760 this->_M_erase_after(__prev, 0);
763 template <class _Tp, class _Alloc>
764 template <class _InputIterator>
766 slist<_Tp, _Alloc>::_M_assign_dispatch(_InputIterator __first,
767 _InputIterator __last,
770 _Node_base* __prev = &this->_M_head;
771 _Node* __node = (_Node*) this->_M_head._M_next;
772 while (__node != 0 && __first != __last)
774 __node->_M_data = *__first;
776 __node = (_Node*) __node->_M_next;
779 if (__first != __last)
780 _M_insert_after_range(__prev, __first, __last);
782 this->_M_erase_after(__prev, 0);
785 template <class _Tp, class _Alloc>
787 operator==(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
789 typedef typename slist<_Tp,_Alloc>::const_iterator const_iterator;
790 const_iterator __end1 = _SL1.end();
791 const_iterator __end2 = _SL2.end();
793 const_iterator __i1 = _SL1.begin();
794 const_iterator __i2 = _SL2.begin();
795 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2)
800 return __i1 == __end1 && __i2 == __end2;
804 template <class _Tp, class _Alloc>
806 operator<(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
807 { return std::lexicographical_compare(_SL1.begin(), _SL1.end(),
808 _SL2.begin(), _SL2.end()); }
810 template <class _Tp, class _Alloc>
812 operator!=(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
813 { return !(_SL1 == _SL2); }
815 template <class _Tp, class _Alloc>
817 operator>(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
818 { return _SL2 < _SL1; }
820 template <class _Tp, class _Alloc>
822 operator<=(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
823 { return !(_SL2 < _SL1); }
825 template <class _Tp, class _Alloc>
827 operator>=(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
828 { return !(_SL1 < _SL2); }
830 template <class _Tp, class _Alloc>
832 swap(slist<_Tp, _Alloc>& __x, slist<_Tp, _Alloc>& __y)
835 template <class _Tp, class _Alloc>
837 slist<_Tp, _Alloc>::resize(size_type __len, const _Tp& __x)
839 _Node_base* __cur = &this->_M_head;
840 while (__cur->_M_next != 0 && __len > 0)
843 __cur = __cur->_M_next;
846 this->_M_erase_after(__cur, 0);
848 _M_insert_after_fill(__cur, __len, __x);
851 template <class _Tp, class _Alloc>
853 slist<_Tp, _Alloc>::remove(const _Tp& __val)
855 _Node_base* __cur = &this->_M_head;
856 while (__cur && __cur->_M_next)
858 if (((_Node*) __cur->_M_next)->_M_data == __val)
859 this->_M_erase_after(__cur);
861 __cur = __cur->_M_next;
865 template <class _Tp, class _Alloc>
867 slist<_Tp, _Alloc>::unique()
869 _Node_base* __cur = this->_M_head._M_next;
872 while (__cur->_M_next)
874 if (((_Node*)__cur)->_M_data
875 == ((_Node*)(__cur->_M_next))->_M_data)
876 this->_M_erase_after(__cur);
878 __cur = __cur->_M_next;
883 template <class _Tp, class _Alloc>
885 slist<_Tp, _Alloc>::merge(slist<_Tp, _Alloc>& __x)
887 _Node_base* __n1 = &this->_M_head;
888 while (__n1->_M_next && __x._M_head._M_next)
890 if (((_Node*) __x._M_head._M_next)->_M_data
891 < ((_Node*) __n1->_M_next)->_M_data)
892 __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
893 __n1 = __n1->_M_next;
895 if (__x._M_head._M_next)
897 __n1->_M_next = __x._M_head._M_next;
898 __x._M_head._M_next = 0;
902 template <class _Tp, class _Alloc>
904 slist<_Tp, _Alloc>::sort()
906 if (this->_M_head._M_next && this->_M_head._M_next->_M_next)
913 __slist_splice_after(&__carry._M_head,
914 &this->_M_head, this->_M_head._M_next);
916 while (__i < __fill && !__counter[__i].empty())
918 __counter[__i].merge(__carry);
919 __carry.swap(__counter[__i]);
922 __carry.swap(__counter[__i]);
927 for (int __i = 1; __i < __fill; ++__i)
928 __counter[__i].merge(__counter[__i-1]);
929 this->swap(__counter[__fill-1]);
933 template <class _Tp, class _Alloc>
934 template <class _Predicate>
935 void slist<_Tp, _Alloc>::remove_if(_Predicate __pred)
937 _Node_base* __cur = &this->_M_head;
938 while (__cur->_M_next)
940 if (__pred(((_Node*) __cur->_M_next)->_M_data))
941 this->_M_erase_after(__cur);
943 __cur = __cur->_M_next;
947 template <class _Tp, class _Alloc>
948 template <class _BinaryPredicate>
950 slist<_Tp, _Alloc>::unique(_BinaryPredicate __pred)
952 _Node* __cur = (_Node*) this->_M_head._M_next;
955 while (__cur->_M_next)
957 if (__pred(((_Node*)__cur)->_M_data,
958 ((_Node*)(__cur->_M_next))->_M_data))
959 this->_M_erase_after(__cur);
961 __cur = (_Node*) __cur->_M_next;
966 template <class _Tp, class _Alloc>
967 template <class _StrictWeakOrdering>
969 slist<_Tp, _Alloc>::merge(slist<_Tp, _Alloc>& __x,
970 _StrictWeakOrdering __comp)
972 _Node_base* __n1 = &this->_M_head;
973 while (__n1->_M_next && __x._M_head._M_next)
975 if (__comp(((_Node*) __x._M_head._M_next)->_M_data,
976 ((_Node*) __n1->_M_next)->_M_data))
977 __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
978 __n1 = __n1->_M_next;
980 if (__x._M_head._M_next)
982 __n1->_M_next = __x._M_head._M_next;
983 __x._M_head._M_next = 0;
987 template <class _Tp, class _Alloc>
988 template <class _StrictWeakOrdering>
990 slist<_Tp, _Alloc>::sort(_StrictWeakOrdering __comp)
992 if (this->_M_head._M_next && this->_M_head._M_next->_M_next)
999 __slist_splice_after(&__carry._M_head,
1000 &this->_M_head, this->_M_head._M_next);
1002 while (__i < __fill && !__counter[__i].empty())
1004 __counter[__i].merge(__carry, __comp);
1005 __carry.swap(__counter[__i]);
1008 __carry.swap(__counter[__i]);
1013 for (int __i = 1; __i < __fill; ++__i)
1014 __counter[__i].merge(__counter[__i-1], __comp);
1015 this->swap(__counter[__fill-1]);
1019 } // namespace __gnu_cxx
1023 // Specialization of insert_iterator so that insertions will be constant
1024 // time rather than linear time.
1026 template <class _Tp, class _Alloc>
1027 class insert_iterator<__gnu_cxx::slist<_Tp, _Alloc> >
1030 typedef __gnu_cxx::slist<_Tp, _Alloc> _Container;
1031 _Container* container;
1032 typename _Container::iterator iter;
1035 typedef _Container container_type;
1036 typedef output_iterator_tag iterator_category;
1037 typedef void value_type;
1038 typedef void difference_type;
1039 typedef void pointer;
1040 typedef void reference;
1042 insert_iterator(_Container& __x, typename _Container::iterator __i)
1045 if (__i == __x.begin())
1046 iter = __x.before_begin();
1048 iter = __x.previous(__i);
1051 insert_iterator<_Container>&
1052 operator=(const typename _Container::value_type& __value)
1054 iter = container->insert_after(iter, __value);
1058 insert_iterator<_Container>&
1062 insert_iterator<_Container>&
1066 insert_iterator<_Container>&