1 // Singly-linked list implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2004, 2005, 2007, 2008, 2009
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
28 * Silicon Graphics Computer Systems, Inc.
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Silicon Graphics makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
41 * This file is a GNU extension to the Standard C++ Library (possibly
42 * containing extensions from the HP/SGI STL subset).
49 #include <bits/allocator.h>
50 #include <bits/stl_construct.h>
51 #include <bits/stl_uninitialized.h>
52 #include <bits/concept_check.h>
54 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
58 using std::_Construct;
61 using std::__true_type;
62 using std::__false_type;
64 struct _Slist_node_base
66 _Slist_node_base* _M_next;
69 inline _Slist_node_base*
70 __slist_make_link(_Slist_node_base* __prev_node,
71 _Slist_node_base* __new_node)
73 __new_node->_M_next = __prev_node->_M_next;
74 __prev_node->_M_next = __new_node;
78 inline _Slist_node_base*
79 __slist_previous(_Slist_node_base* __head,
80 const _Slist_node_base* __node)
82 while (__head && __head->_M_next != __node)
83 __head = __head->_M_next;
87 inline const _Slist_node_base*
88 __slist_previous(const _Slist_node_base* __head,
89 const _Slist_node_base* __node)
91 while (__head && __head->_M_next != __node)
92 __head = __head->_M_next;
97 __slist_splice_after(_Slist_node_base* __pos,
98 _Slist_node_base* __before_first,
99 _Slist_node_base* __before_last)
101 if (__pos != __before_first && __pos != __before_last)
103 _Slist_node_base* __first = __before_first->_M_next;
104 _Slist_node_base* __after = __pos->_M_next;
105 __before_first->_M_next = __before_last->_M_next;
106 __pos->_M_next = __first;
107 __before_last->_M_next = __after;
112 __slist_splice_after(_Slist_node_base* __pos, _Slist_node_base* __head)
114 _Slist_node_base* __before_last = __slist_previous(__head, 0);
115 if (__before_last != __head)
117 _Slist_node_base* __after = __pos->_M_next;
118 __pos->_M_next = __head->_M_next;
120 __before_last->_M_next = __after;
124 inline _Slist_node_base*
125 __slist_reverse(_Slist_node_base* __node)
127 _Slist_node_base* __result = __node;
128 __node = __node->_M_next;
129 __result->_M_next = 0;
132 _Slist_node_base* __next = __node->_M_next;
133 __node->_M_next = __result;
141 __slist_size(_Slist_node_base* __node)
144 for (; __node != 0; __node = __node->_M_next)
150 struct _Slist_node : public _Slist_node_base
155 struct _Slist_iterator_base
157 typedef size_t size_type;
158 typedef ptrdiff_t difference_type;
159 typedef std::forward_iterator_tag iterator_category;
161 _Slist_node_base* _M_node;
163 _Slist_iterator_base(_Slist_node_base* __x)
168 { _M_node = _M_node->_M_next; }
171 operator==(const _Slist_iterator_base& __x) const
172 { return _M_node == __x._M_node; }
175 operator!=(const _Slist_iterator_base& __x) const
176 { return _M_node != __x._M_node; }
179 template <class _Tp, class _Ref, class _Ptr>
180 struct _Slist_iterator : public _Slist_iterator_base
182 typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
183 typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
184 typedef _Slist_iterator<_Tp, _Ref, _Ptr> _Self;
186 typedef _Tp value_type;
187 typedef _Ptr pointer;
188 typedef _Ref reference;
189 typedef _Slist_node<_Tp> _Node;
192 _Slist_iterator(_Node* __x)
193 : _Slist_iterator_base(__x) {}
196 : _Slist_iterator_base(0) {}
198 _Slist_iterator(const iterator& __x)
199 : _Slist_iterator_base(__x._M_node) {}
203 { return ((_Node*) _M_node)->_M_data; }
207 { return &(operator*()); }
225 template <class _Tp, class _Alloc>
227 : public _Alloc::template rebind<_Slist_node<_Tp> >::other
229 typedef typename _Alloc::template rebind<_Slist_node<_Tp> >::other
231 typedef _Alloc allocator_type;
234 get_allocator() const
235 { return *static_cast<const _Node_alloc*>(this); }
237 _Slist_base(const allocator_type& __a)
239 { this->_M_head._M_next = 0; }
242 { _M_erase_after(&this->_M_head, 0); }
245 _Slist_node_base _M_head;
249 { return _Node_alloc::allocate(1); }
252 _M_put_node(_Slist_node<_Tp>* __p)
253 { _Node_alloc::deallocate(__p, 1); }
256 _Slist_node_base* _M_erase_after(_Slist_node_base* __pos)
258 _Slist_node<_Tp>* __next = (_Slist_node<_Tp>*) (__pos->_M_next);
259 _Slist_node_base* __next_next = __next->_M_next;
260 __pos->_M_next = __next_next;
261 get_allocator().destroy(&__next->_M_data);
265 _Slist_node_base* _M_erase_after(_Slist_node_base*, _Slist_node_base*);
268 template <class _Tp, class _Alloc>
270 _Slist_base<_Tp,_Alloc>::_M_erase_after(_Slist_node_base* __before_first,
271 _Slist_node_base* __last_node)
273 _Slist_node<_Tp>* __cur = (_Slist_node<_Tp>*) (__before_first->_M_next);
274 while (__cur != __last_node)
276 _Slist_node<_Tp>* __tmp = __cur;
277 __cur = (_Slist_node<_Tp>*) __cur->_M_next;
278 get_allocator().destroy(&__tmp->_M_data);
281 __before_first->_M_next = __last_node;
286 * This is an SGI extension.
287 * @ingroup SGIextensions
290 template <class _Tp, class _Alloc = allocator<_Tp> >
291 class slist : private _Slist_base<_Tp,_Alloc>
293 // concept requirements
294 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
297 typedef _Slist_base<_Tp,_Alloc> _Base;
300 typedef _Tp value_type;
301 typedef value_type* pointer;
302 typedef const value_type* const_pointer;
303 typedef value_type& reference;
304 typedef const value_type& const_reference;
305 typedef size_t size_type;
306 typedef ptrdiff_t difference_type;
308 typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
309 typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
311 typedef typename _Base::allocator_type allocator_type;
314 get_allocator() const
315 { return _Base::get_allocator(); }
318 typedef _Slist_node<_Tp> _Node;
319 typedef _Slist_node_base _Node_base;
320 typedef _Slist_iterator_base _Iterator_base;
323 _M_create_node(const value_type& __x)
325 _Node* __node = this->_M_get_node();
328 get_allocator().construct(&__node->_M_data, __x);
333 this->_M_put_node(__node);
334 __throw_exception_again;
342 _Node* __node = this->_M_get_node();
345 get_allocator().construct(&__node->_M_data, value_type());
350 this->_M_put_node(__node);
351 __throw_exception_again;
358 slist(const allocator_type& __a = allocator_type())
361 slist(size_type __n, const value_type& __x,
362 const allocator_type& __a = allocator_type())
364 { _M_insert_after_fill(&this->_M_head, __n, __x); }
368 : _Base(allocator_type())
369 { _M_insert_after_fill(&this->_M_head, __n, value_type()); }
371 // We don't need any dispatching tricks here, because
372 // _M_insert_after_range already does them.
373 template <class _InputIterator>
374 slist(_InputIterator __first, _InputIterator __last,
375 const allocator_type& __a = allocator_type())
377 { _M_insert_after_range(&this->_M_head, __first, __last); }
379 slist(const slist& __x)
380 : _Base(__x.get_allocator())
381 { _M_insert_after_range(&this->_M_head, __x.begin(), __x.end()); }
384 operator= (const slist& __x);
389 // assign(), a generalized assignment member function. Two
390 // versions: one that takes a count, and one that takes a range.
391 // The range version is a member template, so we dispatch on whether
392 // or not the type is an integer.
395 assign(size_type __n, const _Tp& __val)
396 { _M_fill_assign(__n, __val); }
399 _M_fill_assign(size_type __n, const _Tp& __val);
401 template <class _InputIterator>
403 assign(_InputIterator __first, _InputIterator __last)
405 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
406 _M_assign_dispatch(__first, __last, _Integral());
409 template <class _Integer>
411 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
412 { _M_fill_assign((size_type) __n, (_Tp) __val); }
414 template <class _InputIterator>
416 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
423 { return iterator((_Node*)this->_M_head._M_next); }
427 { return const_iterator((_Node*)this->_M_head._M_next);}
431 { return iterator(0); }
435 { return const_iterator(0); }
437 // Experimental new feature: before_begin() returns a
438 // non-dereferenceable iterator that, when incremented, yields
439 // begin(). This iterator may be used as the argument to
440 // insert_after, erase_after, etc. Note that even for an empty
441 // slist, before_begin() is not the same iterator as end(). It
442 // is always necessary to increment before_begin() at least once to
446 { return iterator((_Node*) &this->_M_head); }
450 { return const_iterator((_Node*) &this->_M_head); }
454 { return __slist_size(this->_M_head._M_next); }
458 { return size_type(-1); }
462 { return this->_M_head._M_next == 0; }
466 { std::swap(this->_M_head._M_next, __x._M_head._M_next); }
472 { return ((_Node*) this->_M_head._M_next)->_M_data; }
476 { return ((_Node*) this->_M_head._M_next)->_M_data; }
479 push_front(const value_type& __x)
480 { __slist_make_link(&this->_M_head, _M_create_node(__x)); }
484 { __slist_make_link(&this->_M_head, _M_create_node()); }
489 _Node* __node = (_Node*) this->_M_head._M_next;
490 this->_M_head._M_next = __node->_M_next;
491 get_allocator().destroy(&__node->_M_data);
492 this->_M_put_node(__node);
496 previous(const_iterator __pos)
497 { return iterator((_Node*) __slist_previous(&this->_M_head,
501 previous(const_iterator __pos) const
502 { return const_iterator((_Node*) __slist_previous(&this->_M_head,
507 _M_insert_after(_Node_base* __pos, const value_type& __x)
508 { return (_Node*) (__slist_make_link(__pos, _M_create_node(__x))); }
511 _M_insert_after(_Node_base* __pos)
512 { return (_Node*) (__slist_make_link(__pos, _M_create_node())); }
515 _M_insert_after_fill(_Node_base* __pos,
516 size_type __n, const value_type& __x)
518 for (size_type __i = 0; __i < __n; ++__i)
519 __pos = __slist_make_link(__pos, _M_create_node(__x));
522 // Check whether it's an integral type. If so, it's not an iterator.
523 template <class _InIterator>
525 _M_insert_after_range(_Node_base* __pos,
526 _InIterator __first, _InIterator __last)
528 typedef typename std::__is_integer<_InIterator>::__type _Integral;
529 _M_insert_after_range(__pos, __first, __last, _Integral());
532 template <class _Integer>
534 _M_insert_after_range(_Node_base* __pos, _Integer __n, _Integer __x,
536 { _M_insert_after_fill(__pos, __n, __x); }
538 template <class _InIterator>
540 _M_insert_after_range(_Node_base* __pos,
541 _InIterator __first, _InIterator __last,
544 while (__first != __last)
546 __pos = __slist_make_link(__pos, _M_create_node(*__first));
553 insert_after(iterator __pos, const value_type& __x)
554 { return iterator(_M_insert_after(__pos._M_node, __x)); }
557 insert_after(iterator __pos)
558 { return insert_after(__pos, value_type()); }
561 insert_after(iterator __pos, size_type __n, const value_type& __x)
562 { _M_insert_after_fill(__pos._M_node, __n, __x); }
564 // We don't need any dispatching tricks here, because
565 // _M_insert_after_range already does them.
566 template <class _InIterator>
568 insert_after(iterator __pos, _InIterator __first, _InIterator __last)
569 { _M_insert_after_range(__pos._M_node, __first, __last); }
572 insert(iterator __pos, const value_type& __x)
573 { return iterator(_M_insert_after(__slist_previous(&this->_M_head,
578 insert(iterator __pos)
579 { return iterator(_M_insert_after(__slist_previous(&this->_M_head,
584 insert(iterator __pos, size_type __n, const value_type& __x)
585 { _M_insert_after_fill(__slist_previous(&this->_M_head, __pos._M_node),
588 // We don't need any dispatching tricks here, because
589 // _M_insert_after_range already does them.
590 template <class _InIterator>
592 insert(iterator __pos, _InIterator __first, _InIterator __last)
593 { _M_insert_after_range(__slist_previous(&this->_M_head, __pos._M_node),
598 erase_after(iterator __pos)
599 { return iterator((_Node*) this->_M_erase_after(__pos._M_node)); }
602 erase_after(iterator __before_first, iterator __last)
604 return iterator((_Node*) this->_M_erase_after(__before_first._M_node,
609 erase(iterator __pos)
611 return iterator((_Node*) this->_M_erase_after
612 (__slist_previous(&this->_M_head, __pos._M_node)));
616 erase(iterator __first, iterator __last)
618 return iterator((_Node*) this->_M_erase_after
619 (__slist_previous(&this->_M_head, __first._M_node),
624 resize(size_type new_size, const _Tp& __x);
627 resize(size_type new_size)
628 { resize(new_size, _Tp()); }
632 { this->_M_erase_after(&this->_M_head, 0); }
635 // Moves the range [__before_first + 1, __before_last + 1) to *this,
636 // inserting it immediately after __pos. This is constant time.
638 splice_after(iterator __pos,
639 iterator __before_first, iterator __before_last)
641 if (__before_first != __before_last)
642 __slist_splice_after(__pos._M_node, __before_first._M_node,
643 __before_last._M_node);
646 // Moves the element that follows __prev to *this, inserting it
647 // immediately after __pos. This is constant time.
649 splice_after(iterator __pos, iterator __prev)
650 { __slist_splice_after(__pos._M_node,
651 __prev._M_node, __prev._M_node->_M_next); }
653 // Removes all of the elements from the list __x to *this, inserting
654 // them immediately after __pos. __x must not be *this. Complexity:
655 // linear in __x.size().
657 splice_after(iterator __pos, slist& __x)
658 { __slist_splice_after(__pos._M_node, &__x._M_head); }
660 // Linear in distance(begin(), __pos), and linear in __x.size().
662 splice(iterator __pos, slist& __x)
664 if (__x._M_head._M_next)
665 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
667 __slist_previous(&__x._M_head, 0)); }
669 // Linear in distance(begin(), __pos), and in distance(__x.begin(), __i).
671 splice(iterator __pos, slist& __x, iterator __i)
672 { __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
673 __slist_previous(&__x._M_head, __i._M_node),
676 // Linear in distance(begin(), __pos), in distance(__x.begin(), __first),
677 // and in distance(__first, __last).
679 splice(iterator __pos, slist& __x, iterator __first, iterator __last)
681 if (__first != __last)
682 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
683 __slist_previous(&__x._M_head, __first._M_node),
684 __slist_previous(__first._M_node,
692 if (this->_M_head._M_next)
693 this->_M_head._M_next = __slist_reverse(this->_M_head._M_next);
697 remove(const _Tp& __val);
708 template <class _Predicate>
710 remove_if(_Predicate __pred);
712 template <class _BinaryPredicate>
714 unique(_BinaryPredicate __pred);
716 template <class _StrictWeakOrdering>
718 merge(slist&, _StrictWeakOrdering);
720 template <class _StrictWeakOrdering>
722 sort(_StrictWeakOrdering __comp);
725 template <class _Tp, class _Alloc>
727 slist<_Tp, _Alloc>::operator=(const slist<_Tp, _Alloc>& __x)
731 _Node_base* __p1 = &this->_M_head;
732 _Node* __n1 = (_Node*) this->_M_head._M_next;
733 const _Node* __n2 = (const _Node*) __x._M_head._M_next;
736 __n1->_M_data = __n2->_M_data;
738 __n1 = (_Node*) __n1->_M_next;
739 __n2 = (const _Node*) __n2->_M_next;
742 this->_M_erase_after(__p1, 0);
744 _M_insert_after_range(__p1, const_iterator((_Node*)__n2),
750 template <class _Tp, class _Alloc>
752 slist<_Tp, _Alloc>::_M_fill_assign(size_type __n, const _Tp& __val)
754 _Node_base* __prev = &this->_M_head;
755 _Node* __node = (_Node*) this->_M_head._M_next;
756 for (; __node != 0 && __n > 0; --__n)
758 __node->_M_data = __val;
760 __node = (_Node*) __node->_M_next;
763 _M_insert_after_fill(__prev, __n, __val);
765 this->_M_erase_after(__prev, 0);
768 template <class _Tp, class _Alloc>
769 template <class _InputIterator>
771 slist<_Tp, _Alloc>::_M_assign_dispatch(_InputIterator __first,
772 _InputIterator __last,
775 _Node_base* __prev = &this->_M_head;
776 _Node* __node = (_Node*) this->_M_head._M_next;
777 while (__node != 0 && __first != __last)
779 __node->_M_data = *__first;
781 __node = (_Node*) __node->_M_next;
784 if (__first != __last)
785 _M_insert_after_range(__prev, __first, __last);
787 this->_M_erase_after(__prev, 0);
790 template <class _Tp, class _Alloc>
792 operator==(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
794 typedef typename slist<_Tp,_Alloc>::const_iterator const_iterator;
795 const_iterator __end1 = _SL1.end();
796 const_iterator __end2 = _SL2.end();
798 const_iterator __i1 = _SL1.begin();
799 const_iterator __i2 = _SL2.begin();
800 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2)
805 return __i1 == __end1 && __i2 == __end2;
809 template <class _Tp, class _Alloc>
811 operator<(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
812 { return std::lexicographical_compare(_SL1.begin(), _SL1.end(),
813 _SL2.begin(), _SL2.end()); }
815 template <class _Tp, class _Alloc>
817 operator!=(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
818 { return !(_SL1 == _SL2); }
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 !(_SL2 < _SL1); }
830 template <class _Tp, class _Alloc>
832 operator>=(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
833 { return !(_SL1 < _SL2); }
835 template <class _Tp, class _Alloc>
837 swap(slist<_Tp, _Alloc>& __x, slist<_Tp, _Alloc>& __y)
840 template <class _Tp, class _Alloc>
842 slist<_Tp, _Alloc>::resize(size_type __len, const _Tp& __x)
844 _Node_base* __cur = &this->_M_head;
845 while (__cur->_M_next != 0 && __len > 0)
848 __cur = __cur->_M_next;
851 this->_M_erase_after(__cur, 0);
853 _M_insert_after_fill(__cur, __len, __x);
856 template <class _Tp, class _Alloc>
858 slist<_Tp, _Alloc>::remove(const _Tp& __val)
860 _Node_base* __cur = &this->_M_head;
861 while (__cur && __cur->_M_next)
863 if (((_Node*) __cur->_M_next)->_M_data == __val)
864 this->_M_erase_after(__cur);
866 __cur = __cur->_M_next;
870 template <class _Tp, class _Alloc>
872 slist<_Tp, _Alloc>::unique()
874 _Node_base* __cur = this->_M_head._M_next;
877 while (__cur->_M_next)
879 if (((_Node*)__cur)->_M_data
880 == ((_Node*)(__cur->_M_next))->_M_data)
881 this->_M_erase_after(__cur);
883 __cur = __cur->_M_next;
888 template <class _Tp, class _Alloc>
890 slist<_Tp, _Alloc>::merge(slist<_Tp, _Alloc>& __x)
892 _Node_base* __n1 = &this->_M_head;
893 while (__n1->_M_next && __x._M_head._M_next)
895 if (((_Node*) __x._M_head._M_next)->_M_data
896 < ((_Node*) __n1->_M_next)->_M_data)
897 __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
898 __n1 = __n1->_M_next;
900 if (__x._M_head._M_next)
902 __n1->_M_next = __x._M_head._M_next;
903 __x._M_head._M_next = 0;
907 template <class _Tp, class _Alloc>
909 slist<_Tp, _Alloc>::sort()
911 if (this->_M_head._M_next && this->_M_head._M_next->_M_next)
918 __slist_splice_after(&__carry._M_head,
919 &this->_M_head, this->_M_head._M_next);
921 while (__i < __fill && !__counter[__i].empty())
923 __counter[__i].merge(__carry);
924 __carry.swap(__counter[__i]);
927 __carry.swap(__counter[__i]);
932 for (int __i = 1; __i < __fill; ++__i)
933 __counter[__i].merge(__counter[__i-1]);
934 this->swap(__counter[__fill-1]);
938 template <class _Tp, class _Alloc>
939 template <class _Predicate>
940 void slist<_Tp, _Alloc>::remove_if(_Predicate __pred)
942 _Node_base* __cur = &this->_M_head;
943 while (__cur->_M_next)
945 if (__pred(((_Node*) __cur->_M_next)->_M_data))
946 this->_M_erase_after(__cur);
948 __cur = __cur->_M_next;
952 template <class _Tp, class _Alloc>
953 template <class _BinaryPredicate>
955 slist<_Tp, _Alloc>::unique(_BinaryPredicate __pred)
957 _Node* __cur = (_Node*) this->_M_head._M_next;
960 while (__cur->_M_next)
962 if (__pred(((_Node*)__cur)->_M_data,
963 ((_Node*)(__cur->_M_next))->_M_data))
964 this->_M_erase_after(__cur);
966 __cur = (_Node*) __cur->_M_next;
971 template <class _Tp, class _Alloc>
972 template <class _StrictWeakOrdering>
974 slist<_Tp, _Alloc>::merge(slist<_Tp, _Alloc>& __x,
975 _StrictWeakOrdering __comp)
977 _Node_base* __n1 = &this->_M_head;
978 while (__n1->_M_next && __x._M_head._M_next)
980 if (__comp(((_Node*) __x._M_head._M_next)->_M_data,
981 ((_Node*) __n1->_M_next)->_M_data))
982 __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
983 __n1 = __n1->_M_next;
985 if (__x._M_head._M_next)
987 __n1->_M_next = __x._M_head._M_next;
988 __x._M_head._M_next = 0;
992 template <class _Tp, class _Alloc>
993 template <class _StrictWeakOrdering>
995 slist<_Tp, _Alloc>::sort(_StrictWeakOrdering __comp)
997 if (this->_M_head._M_next && this->_M_head._M_next->_M_next)
1000 slist __counter[64];
1004 __slist_splice_after(&__carry._M_head,
1005 &this->_M_head, this->_M_head._M_next);
1007 while (__i < __fill && !__counter[__i].empty())
1009 __counter[__i].merge(__carry, __comp);
1010 __carry.swap(__counter[__i]);
1013 __carry.swap(__counter[__i]);
1018 for (int __i = 1; __i < __fill; ++__i)
1019 __counter[__i].merge(__counter[__i-1], __comp);
1020 this->swap(__counter[__fill-1]);
1024 _GLIBCXX_END_NAMESPACE
1026 _GLIBCXX_BEGIN_NAMESPACE(std)
1028 // Specialization of insert_iterator so that insertions will be constant
1029 // time rather than linear time.
1030 template <class _Tp, class _Alloc>
1031 class insert_iterator<__gnu_cxx::slist<_Tp, _Alloc> >
1034 typedef __gnu_cxx::slist<_Tp, _Alloc> _Container;
1035 _Container* container;
1036 typename _Container::iterator iter;
1039 typedef _Container container_type;
1040 typedef output_iterator_tag iterator_category;
1041 typedef void value_type;
1042 typedef void difference_type;
1043 typedef void pointer;
1044 typedef void reference;
1046 insert_iterator(_Container& __x, typename _Container::iterator __i)
1049 if (__i == __x.begin())
1050 iter = __x.before_begin();
1052 iter = __x.previous(__i);
1055 insert_iterator<_Container>&
1056 operator=(const typename _Container::value_type& __value)
1058 iter = container->insert_after(iter, __value);
1062 insert_iterator<_Container>&
1066 insert_iterator<_Container>&
1070 insert_iterator<_Container>&
1075 _GLIBCXX_END_NAMESPACE