1// Singly-linked list implementation -*- C++ -*-
3// Copyright (C) 2001-2023 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 3, 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// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
27 * Silicon Graphics Computer Systems, Inc.
29 * Permission to use, copy, modify, distribute and sell this software
30 * and its documentation for any purpose is hereby granted without fee,
31 * provided that the above copyright notice appear in all copies and
32 * that both that copyright notice and this permission notice appear
33 * in supporting documentation. Silicon Graphics makes no
34 * representations about the suitability of this software for any
35 * purpose. It is provided "as is" without express or implied warranty.
40 * This file is a GNU extension to the Standard C++ Library (possibly
41 * containing extensions from the HP/SGI STL subset).
47#include <bits/requires_hosted.h> // std::allocator
50#include <bits/allocator.h>
51#include <bits/stl_construct.h>
52#include <bits/stl_uninitialized.h>
53#include <bits/concept_check.h>
54#include <ext/alloc_traits.h>
56namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
58_GLIBCXX_BEGIN_NAMESPACE_VERSION
60 struct _Slist_node_base
62 _Slist_node_base* _M_next;
65 inline _Slist_node_base*
66 __slist_make_link(_Slist_node_base* __prev_node,
67 _Slist_node_base* __new_node)
69 __new_node->_M_next = __prev_node->_M_next;
70 __prev_node->_M_next = __new_node;
74 inline _Slist_node_base*
75 __slist_previous(_Slist_node_base* __head,
76 const _Slist_node_base* __node)
78 while (__head && __head->_M_next != __node)
79 __head = __head->_M_next;
83 inline const _Slist_node_base*
84 __slist_previous(const _Slist_node_base* __head,
85 const _Slist_node_base* __node)
87 while (__head && __head->_M_next != __node)
88 __head = __head->_M_next;
93 __slist_splice_after(_Slist_node_base* __pos,
94 _Slist_node_base* __before_first,
95 _Slist_node_base* __before_last)
97 if (__pos != __before_first && __pos != __before_last)
99 _Slist_node_base* __first = __before_first->_M_next;
100 _Slist_node_base* __after = __pos->_M_next;
101 __before_first->_M_next = __before_last->_M_next;
102 __pos->_M_next = __first;
103 __before_last->_M_next = __after;
108 __slist_splice_after(_Slist_node_base* __pos, _Slist_node_base* __head)
110 _Slist_node_base* __before_last = __slist_previous(__head, 0);
111 if (__before_last != __head)
113 _Slist_node_base* __after = __pos->_M_next;
114 __pos->_M_next = __head->_M_next;
116 __before_last->_M_next = __after;
120 inline _Slist_node_base*
121 __slist_reverse(_Slist_node_base* __node)
123 _Slist_node_base* __result = __node;
124 __node = __node->_M_next;
125 __result->_M_next = 0;
128 _Slist_node_base* __next = __node->_M_next;
129 __node->_M_next = __result;
137 __slist_size(_Slist_node_base* __node)
139 std::size_t __result = 0;
140 for (; __node != 0; __node = __node->_M_next)
146 struct _Slist_node : public _Slist_node_base
151 struct _Slist_iterator_base
153 typedef std::size_t size_type;
154 typedef std::ptrdiff_t difference_type;
155 typedef std::forward_iterator_tag iterator_category;
157 _Slist_node_base* _M_node;
159 _Slist_iterator_base(_Slist_node_base* __x)
164 { _M_node = _M_node->_M_next; }
167 operator==(const _Slist_iterator_base& __x) const
168 { return _M_node == __x._M_node; }
171 operator!=(const _Slist_iterator_base& __x) const
172 { return _M_node != __x._M_node; }
175 template <class _Tp, class _Ref, class _Ptr>
176 struct _Slist_iterator : public _Slist_iterator_base
178 typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
179 typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
180 typedef _Slist_iterator<_Tp, _Ref, _Ptr> _Self;
182 typedef _Tp value_type;
183 typedef _Ptr pointer;
184 typedef _Ref reference;
185 typedef _Slist_node<_Tp> _Node;
188 _Slist_iterator(_Node* __x)
189 : _Slist_iterator_base(__x) {}
192 : _Slist_iterator_base(0) {}
194 _Slist_iterator(const iterator& __x)
195 : _Slist_iterator_base(__x._M_node) {}
199 { return ((_Node*) _M_node)->_M_data; }
203 { return &(operator*()); }
221 template <class _Tp, class _Alloc>
223 : public __alloc_traits<_Alloc>::template rebind<_Slist_node<_Tp> >::other
225 typedef typename __alloc_traits<_Alloc>::template
226 rebind<_Slist_node<_Tp> >::other _Node_alloc;
227 typedef _Alloc allocator_type;
230 get_allocator() const
231 { return *static_cast<const _Node_alloc*>(this); }
233 _Slist_base(const allocator_type& __a)
235 { this->_M_head._M_next = 0; }
238 { _M_erase_after(&this->_M_head, 0); }
241 _Slist_node_base _M_head;
245 { return _Node_alloc::allocate(1); }
248 _M_put_node(_Slist_node<_Tp>* __p)
249 { _Node_alloc::deallocate(__p, 1); }
252 _Slist_node_base* _M_erase_after(_Slist_node_base* __pos)
254 _Slist_node<_Tp>* __next = (_Slist_node<_Tp>*) (__pos->_M_next);
255 _Slist_node_base* __next_next = __next->_M_next;
256 __pos->_M_next = __next_next;
257 allocator_type __a = get_allocator();
258 __alloc_traits<allocator_type>::destroy(__a, &__next->_M_data);
262 _Slist_node_base* _M_erase_after(_Slist_node_base*, _Slist_node_base*);
265 template <class _Tp, class _Alloc>
267 _Slist_base<_Tp,_Alloc>::_M_erase_after(_Slist_node_base* __before_first,
268 _Slist_node_base* __last_node)
270 _Slist_node<_Tp>* __cur = (_Slist_node<_Tp>*) (__before_first->_M_next);
271 while (__cur != __last_node)
273 _Slist_node<_Tp>* __tmp = __cur;
274 __cur = (_Slist_node<_Tp>*) __cur->_M_next;
275 allocator_type __a = get_allocator();
276 __alloc_traits<allocator_type>::destroy(__a, &__tmp->_M_data);
279 __before_first->_M_next = __last_node;
284 * This is an SGI extension.
285 * @ingroup SGIextensions
288 template <class _Tp, class _Alloc = std::allocator<_Tp> >
289 class slist : private _Slist_base<_Tp,_Alloc>
291 // concept requirements
292 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
295 typedef _Slist_base<_Tp,_Alloc> _Base;
298 typedef _Tp value_type;
299 typedef value_type* pointer;
300 typedef const value_type* const_pointer;
301 typedef value_type& reference;
302 typedef const value_type& const_reference;
303 typedef std::size_t size_type;
304 typedef std::ptrdiff_t difference_type;
306 typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
307 typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
309 typedef typename _Base::allocator_type allocator_type;
312 get_allocator() const
313 { return _Base::get_allocator(); }
316 typedef _Slist_node<_Tp> _Node;
317 typedef _Slist_node_base _Node_base;
318 typedef _Slist_iterator_base _Iterator_base;
321 _M_create_node(const value_type& __x)
323 _Node* __node = this->_M_get_node();
326 allocator_type __a = get_allocator();
327 __alloc_traits<allocator_type>::construct(__a, &__node->_M_data,
333 this->_M_put_node(__node);
334 __throw_exception_again;
342 _Node* __node = this->_M_get_node();
345 allocator_type __a = get_allocator();
346 __alloc_traits<allocator_type>::construct(__a, &__node->_M_data,
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()); }
373 // We don't need any dispatching tricks here, because
374 // _M_insert_after_range already does them.
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); }
381 slist(const slist& __x)
382 : _Base(__x.get_allocator())
383 { _M_insert_after_range(&this->_M_head, __x.begin(), __x.end()); }
386 operator= (const slist& __x);
391 // assign(), a generalized assignment member function. Two
392 // versions: one that takes a count, and one that takes a range.
393 // The range version is a member template, so we dispatch on whether
394 // or not the type is an integer.
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, std::__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); }
439 // Experimental new feature: before_begin() returns a
440 // non-dereferenceable iterator that, when incremented, yields
441 // begin(). This iterator may be used as the argument to
442 // insert_after, erase_after, etc. Note that even for an empty
443 // slist, before_begin() is not the same iterator as end(). It
444 // is always necessary to increment before_begin() at least once to
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); }
462 _GLIBCXX_NODISCARD bool
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 allocator_type __a = get_allocator();
494 __alloc_traits<allocator_type>::destroy(__a, &__node->_M_data);
495 this->_M_put_node(__node);
499 previous(const_iterator __pos)
500 { return iterator((_Node*) __slist_previous(&this->_M_head,
504 previous(const_iterator __pos) const
505 { return const_iterator((_Node*) __slist_previous(&this->_M_head,
510 _M_insert_after(_Node_base* __pos, const value_type& __x)
511 { return (_Node*) (__slist_make_link(__pos, _M_create_node(__x))); }
514 _M_insert_after(_Node_base* __pos)
515 { return (_Node*) (__slist_make_link(__pos, _M_create_node())); }
518 _M_insert_after_fill(_Node_base* __pos,
519 size_type __n, const value_type& __x)
521 for (size_type __i = 0; __i < __n; ++__i)
522 __pos = __slist_make_link(__pos, _M_create_node(__x));
525 // Check whether it's an integral type. If so, it's not an iterator.
526 template <class _InIterator>
528 _M_insert_after_range(_Node_base* __pos,
529 _InIterator __first, _InIterator __last)
531 typedef typename std::__is_integer<_InIterator>::__type _Integral;
532 _M_insert_after_range(__pos, __first, __last, _Integral());
535 template <class _Integer>
537 _M_insert_after_range(_Node_base* __pos, _Integer __n, _Integer __x,
539 { _M_insert_after_fill(__pos, __n, __x); }
541 template <class _InIterator>
543 _M_insert_after_range(_Node_base* __pos,
544 _InIterator __first, _InIterator __last,
547 while (__first != __last)
549 __pos = __slist_make_link(__pos, _M_create_node(*__first));
556 insert_after(iterator __pos, const value_type& __x)
557 { return iterator(_M_insert_after(__pos._M_node, __x)); }
560 insert_after(iterator __pos)
561 { return insert_after(__pos, value_type()); }
564 insert_after(iterator __pos, size_type __n, const value_type& __x)
565 { _M_insert_after_fill(__pos._M_node, __n, __x); }
567 // We don't need any dispatching tricks here, because
568 // _M_insert_after_range already does them.
569 template <class _InIterator>
571 insert_after(iterator __pos, _InIterator __first, _InIterator __last)
572 { _M_insert_after_range(__pos._M_node, __first, __last); }
575 insert(iterator __pos, const value_type& __x)
576 { return iterator(_M_insert_after(__slist_previous(&this->_M_head,
581 insert(iterator __pos)
582 { return iterator(_M_insert_after(__slist_previous(&this->_M_head,
587 insert(iterator __pos, size_type __n, const value_type& __x)
588 { _M_insert_after_fill(__slist_previous(&this->_M_head, __pos._M_node),
591 // We don't need any dispatching tricks here, because
592 // _M_insert_after_range already does them.
593 template <class _InIterator>
595 insert(iterator __pos, _InIterator __first, _InIterator __last)
596 { _M_insert_after_range(__slist_previous(&this->_M_head, __pos._M_node),
601 erase_after(iterator __pos)
602 { return iterator((_Node*) this->_M_erase_after(__pos._M_node)); }
605 erase_after(iterator __before_first, iterator __last)
607 return iterator((_Node*) this->_M_erase_after(__before_first._M_node,
612 erase(iterator __pos)
614 return iterator((_Node*) this->_M_erase_after
615 (__slist_previous(&this->_M_head, __pos._M_node)));
619 erase(iterator __first, iterator __last)
621 return iterator((_Node*) this->_M_erase_after
622 (__slist_previous(&this->_M_head, __first._M_node),
627 resize(size_type new_size, const _Tp& __x);
630 resize(size_type new_size)
631 { resize(new_size, _Tp()); }
635 { this->_M_erase_after(&this->_M_head, 0); }
638 // Moves the range [__before_first + 1, __before_last + 1) to *this,
639 // inserting it immediately after __pos. This is constant time.
641 splice_after(iterator __pos,
642 iterator __before_first, iterator __before_last)
644 if (__before_first != __before_last)
645 __slist_splice_after(__pos._M_node, __before_first._M_node,
646 __before_last._M_node);
649 // Moves the element that follows __prev to *this, inserting it
650 // immediately after __pos. This is constant time.
652 splice_after(iterator __pos, iterator __prev)
653 { __slist_splice_after(__pos._M_node,
654 __prev._M_node, __prev._M_node->_M_next); }
656 // Removes all of the elements from the list __x to *this, inserting
657 // them immediately after __pos. __x must not be *this. Complexity:
658 // linear in __x.size().
660 splice_after(iterator __pos, slist& __x)
661 { __slist_splice_after(__pos._M_node, &__x._M_head); }
663 // Linear in distance(begin(), __pos), and linear in __x.size().
665 splice(iterator __pos, slist& __x)
667 if (__x._M_head._M_next)
668 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
670 __slist_previous(&__x._M_head, 0)); }
672 // Linear in distance(begin(), __pos), and in distance(__x.begin(), __i).
674 splice(iterator __pos, slist& __x, iterator __i)
675 { __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
676 __slist_previous(&__x._M_head, __i._M_node),
679 // Linear in distance(begin(), __pos), in distance(__x.begin(), __first),
680 // and in distance(__first, __last).
682 splice(iterator __pos, slist& __x, iterator __first, iterator __last)
684 if (__first != __last)
685 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
686 __slist_previous(&__x._M_head, __first._M_node),
687 __slist_previous(__first._M_node,
695 if (this->_M_head._M_next)
696 this->_M_head._M_next = __slist_reverse(this->_M_head._M_next);
700 remove(const _Tp& __val);
711 template <class _Predicate>
713 remove_if(_Predicate __pred);
715 template <class _BinaryPredicate>
717 unique(_BinaryPredicate __pred);
719 template <class _StrictWeakOrdering>
721 merge(slist&, _StrictWeakOrdering);
723 template <class _StrictWeakOrdering>
725 sort(_StrictWeakOrdering __comp);
728 template <class _Tp, class _Alloc>
730 slist<_Tp, _Alloc>::operator=(const slist<_Tp, _Alloc>& __x)
734 _Node_base* __p1 = &this->_M_head;
735 _Node* __n1 = (_Node*) this->_M_head._M_next;
736 const _Node* __n2 = (const _Node*) __x._M_head._M_next;
739 __n1->_M_data = __n2->_M_data;
741 __n1 = (_Node*) __n1->_M_next;
742 __n2 = (const _Node*) __n2->_M_next;
745 this->_M_erase_after(__p1, 0);
747 _M_insert_after_range(__p1, const_iterator((_Node*)__n2),
753 template <class _Tp, class _Alloc>
755 slist<_Tp, _Alloc>::_M_fill_assign(size_type __n, const _Tp& __val)
757 _Node_base* __prev = &this->_M_head;
758 _Node* __node = (_Node*) this->_M_head._M_next;
759 for (; __node != 0 && __n > 0; --__n)
761 __node->_M_data = __val;
763 __node = (_Node*) __node->_M_next;
766 _M_insert_after_fill(__prev, __n, __val);
768 this->_M_erase_after(__prev, 0);
771 template <class _Tp, class _Alloc>
772 template <class _InputIterator>
774 slist<_Tp, _Alloc>::_M_assign_dispatch(_InputIterator __first,
775 _InputIterator __last,
778 _Node_base* __prev = &this->_M_head;
779 _Node* __node = (_Node*) this->_M_head._M_next;
780 while (__node != 0 && __first != __last)
782 __node->_M_data = *__first;
784 __node = (_Node*) __node->_M_next;
787 if (__first != __last)
788 _M_insert_after_range(__prev, __first, __last);
790 this->_M_erase_after(__prev, 0);
793 template <class _Tp, class _Alloc>
795 operator==(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
797 typedef typename slist<_Tp,_Alloc>::const_iterator const_iterator;
798 const_iterator __end1 = _SL1.end();
799 const_iterator __end2 = _SL2.end();
801 const_iterator __i1 = _SL1.begin();
802 const_iterator __i2 = _SL2.begin();
803 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2)
808 return __i1 == __end1 && __i2 == __end2;
812 template <class _Tp, class _Alloc>
814 operator<(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
815 { return std::lexicographical_compare(_SL1.begin(), _SL1.end(),
816 _SL2.begin(), _SL2.end()); }
818 template <class _Tp, class _Alloc>
820 operator!=(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
821 { return !(_SL1 == _SL2); }
823 template <class _Tp, class _Alloc>
825 operator>(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
826 { return _SL2 < _SL1; }
828 template <class _Tp, class _Alloc>
830 operator<=(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
831 { return !(_SL2 < _SL1); }
833 template <class _Tp, class _Alloc>
835 operator>=(const slist<_Tp, _Alloc>& _SL1, const slist<_Tp, _Alloc>& _SL2)
836 { return !(_SL1 < _SL2); }
838 template <class _Tp, class _Alloc>
840 swap(slist<_Tp, _Alloc>& __x, slist<_Tp, _Alloc>& __y)
843 template <class _Tp, class _Alloc>
845 slist<_Tp, _Alloc>::resize(size_type __len, const _Tp& __x)
847 _Node_base* __cur = &this->_M_head;
848 while (__cur->_M_next != 0 && __len > 0)
851 __cur = __cur->_M_next;
854 this->_M_erase_after(__cur, 0);
856 _M_insert_after_fill(__cur, __len, __x);
859 template <class _Tp, class _Alloc>
861 slist<_Tp, _Alloc>::remove(const _Tp& __val)
863 _Node_base* __cur = &this->_M_head;
864 while (__cur && __cur->_M_next)
866 if (((_Node*) __cur->_M_next)->_M_data == __val)
867 this->_M_erase_after(__cur);
869 __cur = __cur->_M_next;
873 template <class _Tp, class _Alloc>
875 slist<_Tp, _Alloc>::unique()
877 _Node_base* __cur = this->_M_head._M_next;
880 while (__cur->_M_next)
882 if (((_Node*)__cur)->_M_data
883 == ((_Node*)(__cur->_M_next))->_M_data)
884 this->_M_erase_after(__cur);
886 __cur = __cur->_M_next;
891 template <class _Tp, class _Alloc>
893 slist<_Tp, _Alloc>::merge(slist<_Tp, _Alloc>& __x)
895 _Node_base* __n1 = &this->_M_head;
896 while (__n1->_M_next && __x._M_head._M_next)
898 if (((_Node*) __x._M_head._M_next)->_M_data
899 < ((_Node*) __n1->_M_next)->_M_data)
900 __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
901 __n1 = __n1->_M_next;
903 if (__x._M_head._M_next)
905 __n1->_M_next = __x._M_head._M_next;
906 __x._M_head._M_next = 0;
910 template <class _Tp, class _Alloc>
912 slist<_Tp, _Alloc>::sort()
914 if (this->_M_head._M_next && this->_M_head._M_next->_M_next)
921 __slist_splice_after(&__carry._M_head,
922 &this->_M_head, this->_M_head._M_next);
924 while (__i < __fill && !__counter[__i].empty())
926 __counter[__i].merge(__carry);
927 __carry.swap(__counter[__i]);
930 __carry.swap(__counter[__i]);
935 for (int __i = 1; __i < __fill; ++__i)
936 __counter[__i].merge(__counter[__i-1]);
937 this->swap(__counter[__fill-1]);
941 template <class _Tp, class _Alloc>
942 template <class _Predicate>
943 void slist<_Tp, _Alloc>::remove_if(_Predicate __pred)
945 _Node_base* __cur = &this->_M_head;
946 while (__cur->_M_next)
948 if (__pred(((_Node*) __cur->_M_next)->_M_data))
949 this->_M_erase_after(__cur);
951 __cur = __cur->_M_next;
955 template <class _Tp, class _Alloc>
956 template <class _BinaryPredicate>
958 slist<_Tp, _Alloc>::unique(_BinaryPredicate __pred)
960 _Node* __cur = (_Node*) this->_M_head._M_next;
963 while (__cur->_M_next)
965 if (__pred(((_Node*)__cur)->_M_data,
966 ((_Node*)(__cur->_M_next))->_M_data))
967 this->_M_erase_after(__cur);
969 __cur = (_Node*) __cur->_M_next;
974 template <class _Tp, class _Alloc>
975 template <class _StrictWeakOrdering>
977 slist<_Tp, _Alloc>::merge(slist<_Tp, _Alloc>& __x,
978 _StrictWeakOrdering __comp)
980 _Node_base* __n1 = &this->_M_head;
981 while (__n1->_M_next && __x._M_head._M_next)
983 if (__comp(((_Node*) __x._M_head._M_next)->_M_data,
984 ((_Node*) __n1->_M_next)->_M_data))
985 __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
986 __n1 = __n1->_M_next;
988 if (__x._M_head._M_next)
990 __n1->_M_next = __x._M_head._M_next;
991 __x._M_head._M_next = 0;
995 template <class _Tp, class _Alloc>
996 template <class _StrictWeakOrdering>
998 slist<_Tp, _Alloc>::sort(_StrictWeakOrdering __comp)
1000 if (this->_M_head._M_next && this->_M_head._M_next->_M_next)
1003 slist __counter[64];
1007 __slist_splice_after(&__carry._M_head,
1008 &this->_M_head, this->_M_head._M_next);
1010 while (__i < __fill && !__counter[__i].empty())
1012 __counter[__i].merge(__carry, __comp);
1013 __carry.swap(__counter[__i]);
1016 __carry.swap(__counter[__i]);
1021 for (int __i = 1; __i < __fill; ++__i)
1022 __counter[__i].merge(__counter[__i-1], __comp);
1023 this->swap(__counter[__fill-1]);
1027_GLIBCXX_END_NAMESPACE_VERSION
1030namespace std _GLIBCXX_VISIBILITY(default)
1032_GLIBCXX_BEGIN_NAMESPACE_VERSION
1034 // Specialization of insert_iterator so that insertions will be constant
1035 // time rather than linear time.
1036 template <class _Tp, class _Alloc>
1037 class insert_iterator<__gnu_cxx::slist<_Tp, _Alloc> >
1040 typedef __gnu_cxx::slist<_Tp, _Alloc> _Container;
1041 _Container* container;
1042 typename _Container::iterator iter;
1045 typedef _Container container_type;
1046 typedef output_iterator_tag iterator_category;
1047 typedef void value_type;
1048 typedef void difference_type;
1049 typedef void pointer;
1050 typedef void reference;
1052 insert_iterator(_Container& __x, typename _Container::iterator __i)
1055 if (__i == __x.begin())
1056 iter = __x.before_begin();
1058 iter = __x.previous(__i);
1061 insert_iterator<_Container>&
1062 operator=(const typename _Container::value_type& __value)
1064 iter = container->insert_after(iter, __value);
1068 insert_iterator<_Container>&
1072 insert_iterator<_Container>&
1076 insert_iterator<_Container>&
1081_GLIBCXX_END_NAMESPACE_VERSION