1 // Singly-linked list implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002 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). You should only
47 * include this header if you are using GCC 3 or later.
53 #include <bits/stl_algobase.h>
54 #include <bits/allocator.h>
55 #include <bits/stl_construct.h>
56 #include <bits/stl_uninitialized.h>
57 #include <bits/concept_check.h>
63 using std::_Construct;
67 struct _Slist_node_base
69 _Slist_node_base* _M_next;
72 inline _Slist_node_base*
73 __slist_make_link(_Slist_node_base* __prev_node,
74 _Slist_node_base* __new_node)
76 __new_node->_M_next = __prev_node->_M_next;
77 __prev_node->_M_next = __new_node;
81 inline _Slist_node_base*
82 __slist_previous(_Slist_node_base* __head,
83 const _Slist_node_base* __node)
85 while (__head && __head->_M_next != __node)
86 __head = __head->_M_next;
90 inline const _Slist_node_base*
91 __slist_previous(const _Slist_node_base* __head,
92 const _Slist_node_base* __node)
94 while (__head && __head->_M_next != __node)
95 __head = __head->_M_next;
99 inline void __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) {
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) {
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* __slist_reverse(_Slist_node_base* __node)
126 _Slist_node_base* __result = __node;
127 __node = __node->_M_next;
128 __result->_M_next = 0;
130 _Slist_node_base* __next = __node->_M_next;
131 __node->_M_next = __result;
138 inline size_t __slist_size(_Slist_node_base* __node)
141 for ( ; __node != 0; __node = __node->_M_next)
147 struct _Slist_node : public _Slist_node_base
152 struct _Slist_iterator_base
154 typedef size_t size_type;
155 typedef ptrdiff_t difference_type;
156 typedef std::forward_iterator_tag iterator_category;
158 _Slist_node_base* _M_node;
160 _Slist_iterator_base(_Slist_node_base* __x) : _M_node(__x) {}
161 void _M_incr() { _M_node = _M_node->_M_next; }
163 bool operator==(const _Slist_iterator_base& __x) const {
164 return _M_node == __x._M_node;
166 bool operator!=(const _Slist_iterator_base& __x) const {
167 return _M_node != __x._M_node;
171 template <class _Tp, class _Ref, class _Ptr>
172 struct _Slist_iterator : public _Slist_iterator_base
174 typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
175 typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
176 typedef _Slist_iterator<_Tp, _Ref, _Ptr> _Self;
178 typedef _Tp value_type;
179 typedef _Ptr pointer;
180 typedef _Ref reference;
181 typedef _Slist_node<_Tp> _Node;
183 _Slist_iterator(_Node* __x) : _Slist_iterator_base(__x) {}
184 _Slist_iterator() : _Slist_iterator_base(0) {}
185 _Slist_iterator(const iterator& __x) : _Slist_iterator_base(__x._M_node) {}
187 reference operator*() const { return ((_Node*) _M_node)->_M_data; }
188 pointer operator->() const { return &(operator*()); }
195 _Self operator++(int)
203 template <class _Tp, class _Alloc>
205 : public _Alloc::template rebind<_Slist_node<_Tp> >::other
207 typedef typename _Alloc::template rebind<_Slist_node<_Tp> >::other _Node_alloc;
208 typedef _Alloc allocator_type;
209 allocator_type get_allocator() const {
210 return *static_cast<const _Node_alloc*>(this);
213 _Slist_base(const allocator_type& __a)
214 : _Node_alloc(__a) { this->_M_head._M_next = 0; }
215 ~_Slist_base() { _M_erase_after(&this->_M_head, 0); }
218 _Slist_node_base _M_head;
220 _Slist_node<_Tp>* _M_get_node() { return _Node_alloc::allocate(1); }
221 void _M_put_node(_Slist_node<_Tp>* __p) { _Node_alloc::deallocate(__p, 1); }
224 _Slist_node_base* _M_erase_after(_Slist_node_base* __pos)
226 _Slist_node<_Tp>* __next = (_Slist_node<_Tp>*) (__pos->_M_next);
227 _Slist_node_base* __next_next = __next->_M_next;
228 __pos->_M_next = __next_next;
229 _Destroy(&__next->_M_data);
233 _Slist_node_base* _M_erase_after(_Slist_node_base*, _Slist_node_base*);
236 template <class _Tp, class _Alloc>
238 _Slist_base<_Tp,_Alloc>::_M_erase_after(_Slist_node_base* __before_first,
239 _Slist_node_base* __last_node) {
240 _Slist_node<_Tp>* __cur = (_Slist_node<_Tp>*) (__before_first->_M_next);
241 while (__cur != __last_node) {
242 _Slist_node<_Tp>* __tmp = __cur;
243 __cur = (_Slist_node<_Tp>*) __cur->_M_next;
244 _Destroy(&__tmp->_M_data);
247 __before_first->_M_next = __last_node;
252 * This is an SGI extension.
253 * @ingroup SGIextensions
256 template <class _Tp, class _Alloc = allocator<_Tp> >
257 class slist : private _Slist_base<_Tp,_Alloc>
259 // concept requirements
260 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
263 typedef _Slist_base<_Tp,_Alloc> _Base;
265 typedef _Tp value_type;
266 typedef value_type* pointer;
267 typedef const value_type* const_pointer;
268 typedef value_type& reference;
269 typedef const value_type& const_reference;
270 typedef size_t size_type;
271 typedef ptrdiff_t difference_type;
273 typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
274 typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
276 typedef typename _Base::allocator_type allocator_type;
277 allocator_type get_allocator() const { return _Base::get_allocator(); }
280 typedef _Slist_node<_Tp> _Node;
281 typedef _Slist_node_base _Node_base;
282 typedef _Slist_iterator_base _Iterator_base;
284 _Node* _M_create_node(const value_type& __x) {
285 _Node* __node = this->_M_get_node();
287 _Construct(&__node->_M_data, __x);
292 this->_M_put_node(__node);
293 __throw_exception_again;
298 _Node* _M_create_node() {
299 _Node* __node = this->_M_get_node();
301 _Construct(&__node->_M_data);
306 this->_M_put_node(__node);
307 __throw_exception_again;
313 explicit slist(const allocator_type& __a = allocator_type()) : _Base(__a) {}
315 slist(size_type __n, const value_type& __x,
316 const allocator_type& __a = allocator_type()) : _Base(__a)
317 { _M_insert_after_fill(&this->_M_head, __n, __x); }
319 explicit slist(size_type __n) : _Base(allocator_type())
320 { _M_insert_after_fill(&this->_M_head, __n, value_type()); }
322 // We don't need any dispatching tricks here, because _M_insert_after_range
323 // already does them.
324 template <class _InputIterator>
325 slist(_InputIterator __first, _InputIterator __last,
326 const allocator_type& __a = allocator_type()) : _Base(__a)
327 { _M_insert_after_range(&this->_M_head, __first, __last); }
329 slist(const slist& __x) : _Base(__x.get_allocator())
330 { _M_insert_after_range(&this->_M_head, __x.begin(), __x.end()); }
332 slist& operator= (const slist& __x);
337 // assign(), a generalized assignment member function. Two
338 // versions: one that takes a count, and one that takes a range.
339 // The range version is a member template, so we dispatch on whether
340 // or not the type is an integer.
342 void assign(size_type __n, const _Tp& __val)
343 { _M_fill_assign(__n, __val); }
345 void _M_fill_assign(size_type __n, const _Tp& __val);
347 template <class _InputIterator>
348 void assign(_InputIterator __first, _InputIterator __last) {
349 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
350 _M_assign_dispatch(__first, __last, _Integral());
353 template <class _Integer>
354 void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
355 { _M_fill_assign((size_type) __n, (_Tp) __val); }
357 template <class _InputIterator>
358 void _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
363 iterator begin() { return iterator((_Node*)this->_M_head._M_next); }
364 const_iterator begin() const
365 { return const_iterator((_Node*)this->_M_head._M_next);}
367 iterator end() { return iterator(0); }
368 const_iterator end() const { return const_iterator(0); }
370 // Experimental new feature: before_begin() returns a
371 // non-dereferenceable iterator that, when incremented, yields
372 // begin(). This iterator may be used as the argument to
373 // insert_after, erase_after, etc. Note that even for an empty
374 // slist, before_begin() is not the same iterator as end(). It
375 // is always necessary to increment before_begin() at least once to
377 iterator before_begin() { return iterator((_Node*) &this->_M_head); }
378 const_iterator before_begin() const
379 { return const_iterator((_Node*) &this->_M_head); }
381 size_type size() const { return __slist_size(this->_M_head._M_next); }
383 size_type max_size() const { return size_type(-1); }
385 bool empty() const { return this->_M_head._M_next == 0; }
387 void swap(slist& __x)
388 { std::swap(this->_M_head._M_next, __x._M_head._M_next); }
392 reference front() { return ((_Node*) this->_M_head._M_next)->_M_data; }
393 const_reference front() const
394 { return ((_Node*) this->_M_head._M_next)->_M_data; }
395 void push_front(const value_type& __x) {
396 __slist_make_link(&this->_M_head, _M_create_node(__x));
398 void push_front() { __slist_make_link(&this->_M_head, _M_create_node()); }
400 _Node* __node = (_Node*) this->_M_head._M_next;
401 this->_M_head._M_next = __node->_M_next;
402 _Destroy(&__node->_M_data);
403 this->_M_put_node(__node);
406 iterator previous(const_iterator __pos) {
407 return iterator((_Node*) __slist_previous(&this->_M_head, __pos._M_node));
409 const_iterator previous(const_iterator __pos) const {
410 return const_iterator((_Node*) __slist_previous(&this->_M_head,
415 _Node* _M_insert_after(_Node_base* __pos, const value_type& __x) {
416 return (_Node*) (__slist_make_link(__pos, _M_create_node(__x)));
419 _Node* _M_insert_after(_Node_base* __pos) {
420 return (_Node*) (__slist_make_link(__pos, _M_create_node()));
423 void _M_insert_after_fill(_Node_base* __pos,
424 size_type __n, const value_type& __x) {
425 for (size_type __i = 0; __i < __n; ++__i)
426 __pos = __slist_make_link(__pos, _M_create_node(__x));
429 // Check whether it's an integral type. If so, it's not an iterator.
430 template <class _InIterator>
431 void _M_insert_after_range(_Node_base* __pos,
432 _InIterator __first, _InIterator __last) {
433 typedef typename _Is_integer<_InIterator>::_Integral _Integral;
434 _M_insert_after_range(__pos, __first, __last, _Integral());
437 template <class _Integer>
438 void _M_insert_after_range(_Node_base* __pos, _Integer __n, _Integer __x,
440 _M_insert_after_fill(__pos, __n, __x);
443 template <class _InIterator>
444 void _M_insert_after_range(_Node_base* __pos,
445 _InIterator __first, _InIterator __last,
447 while (__first != __last) {
448 __pos = __slist_make_link(__pos, _M_create_node(*__first));
455 iterator insert_after(iterator __pos, const value_type& __x) {
456 return iterator(_M_insert_after(__pos._M_node, __x));
459 iterator insert_after(iterator __pos) {
460 return insert_after(__pos, value_type());
463 void insert_after(iterator __pos, size_type __n, const value_type& __x) {
464 _M_insert_after_fill(__pos._M_node, __n, __x);
467 // We don't need any dispatching tricks here, because _M_insert_after_range
468 // already does them.
469 template <class _InIterator>
470 void insert_after(iterator __pos, _InIterator __first, _InIterator __last) {
471 _M_insert_after_range(__pos._M_node, __first, __last);
474 iterator insert(iterator __pos, const value_type& __x) {
475 return iterator(_M_insert_after(__slist_previous(&this->_M_head,
480 iterator insert(iterator __pos) {
481 return iterator(_M_insert_after(__slist_previous(&this->_M_head,
486 void insert(iterator __pos, size_type __n, const value_type& __x) {
487 _M_insert_after_fill(__slist_previous(&this->_M_head, __pos._M_node),
491 // We don't need any dispatching tricks here, because _M_insert_after_range
492 // already does them.
493 template <class _InIterator>
494 void insert(iterator __pos, _InIterator __first, _InIterator __last) {
495 _M_insert_after_range(__slist_previous(&this->_M_head, __pos._M_node),
500 iterator erase_after(iterator __pos) {
501 return iterator((_Node*) this->_M_erase_after(__pos._M_node));
503 iterator erase_after(iterator __before_first, iterator __last) {
504 return iterator((_Node*) this->_M_erase_after(__before_first._M_node,
508 iterator erase(iterator __pos) {
509 return (_Node*) this->_M_erase_after(__slist_previous(&this->_M_head,
512 iterator erase(iterator __first, iterator __last) {
513 return (_Node*) this->_M_erase_after(
514 __slist_previous(&this->_M_head, __first._M_node), __last._M_node);
517 void resize(size_type new_size, const _Tp& __x);
518 void resize(size_type new_size) { resize(new_size, _Tp()); }
519 void clear() { this->_M_erase_after(&this->_M_head, 0); }
522 // Moves the range [__before_first + 1, __before_last + 1) to *this,
523 // inserting it immediately after __pos. This is constant time.
524 void splice_after(iterator __pos,
525 iterator __before_first, iterator __before_last)
527 if (__before_first != __before_last)
528 __slist_splice_after(__pos._M_node, __before_first._M_node,
529 __before_last._M_node);
532 // Moves the element that follows __prev to *this, inserting it immediately
533 // after __pos. This is constant time.
534 void splice_after(iterator __pos, iterator __prev)
536 __slist_splice_after(__pos._M_node,
537 __prev._M_node, __prev._M_node->_M_next);
541 // Removes all of the elements from the list __x to *this, inserting
542 // them immediately after __pos. __x must not be *this. Complexity:
543 // linear in __x.size().
544 void splice_after(iterator __pos, slist& __x)
546 __slist_splice_after(__pos._M_node, &__x._M_head);
549 // Linear in distance(begin(), __pos), and linear in __x.size().
550 void splice(iterator __pos, slist& __x) {
551 if (__x._M_head._M_next)
552 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
553 &__x._M_head, __slist_previous(&__x._M_head, 0));
556 // Linear in distance(begin(), __pos), and in distance(__x.begin(), __i).
557 void splice(iterator __pos, slist& __x, iterator __i) {
558 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
559 __slist_previous(&__x._M_head, __i._M_node),
563 // Linear in distance(begin(), __pos), in distance(__x.begin(), __first),
564 // and in distance(__first, __last).
565 void splice(iterator __pos, slist& __x, iterator __first, iterator __last)
567 if (__first != __last)
568 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
569 __slist_previous(&__x._M_head, __first._M_node),
570 __slist_previous(__first._M_node, __last._M_node));
575 if (this->_M_head._M_next)
576 this->_M_head._M_next = __slist_reverse(this->_M_head._M_next);
579 void remove(const _Tp& __val);
581 void merge(slist& __x);
584 template <class _Predicate>
585 void remove_if(_Predicate __pred);
587 template <class _BinaryPredicate>
588 void unique(_BinaryPredicate __pred);
590 template <class _StrictWeakOrdering>
591 void merge(slist&, _StrictWeakOrdering);
593 template <class _StrictWeakOrdering>
594 void sort(_StrictWeakOrdering __comp);
597 template <class _Tp, class _Alloc>
598 slist<_Tp,_Alloc>& slist<_Tp,_Alloc>::operator=(const slist<_Tp,_Alloc>& __x)
601 _Node_base* __p1 = &this->_M_head;
602 _Node* __n1 = (_Node*) this->_M_head._M_next;
603 const _Node* __n2 = (const _Node*) __x._M_head._M_next;
604 while (__n1 && __n2) {
605 __n1->_M_data = __n2->_M_data;
607 __n1 = (_Node*) __n1->_M_next;
608 __n2 = (const _Node*) __n2->_M_next;
611 this->_M_erase_after(__p1, 0);
613 _M_insert_after_range(__p1, const_iterator((_Node*)__n2),
619 template <class _Tp, class _Alloc>
620 void slist<_Tp, _Alloc>::_M_fill_assign(size_type __n, const _Tp& __val) {
621 _Node_base* __prev = &this->_M_head;
622 _Node* __node = (_Node*) this->_M_head._M_next;
623 for ( ; __node != 0 && __n > 0 ; --__n) {
624 __node->_M_data = __val;
626 __node = (_Node*) __node->_M_next;
629 _M_insert_after_fill(__prev, __n, __val);
631 this->_M_erase_after(__prev, 0);
634 template <class _Tp, class _Alloc> template <class _InputIterator>
636 slist<_Tp, _Alloc>::_M_assign_dispatch(_InputIterator __first, _InputIterator __last,
639 _Node_base* __prev = &this->_M_head;
640 _Node* __node = (_Node*) this->_M_head._M_next;
641 while (__node != 0 && __first != __last) {
642 __node->_M_data = *__first;
644 __node = (_Node*) __node->_M_next;
647 if (__first != __last)
648 _M_insert_after_range(__prev, __first, __last);
650 this->_M_erase_after(__prev, 0);
653 template <class _Tp, class _Alloc>
655 operator==(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2)
657 typedef typename slist<_Tp,_Alloc>::const_iterator const_iterator;
658 const_iterator __end1 = _SL1.end();
659 const_iterator __end2 = _SL2.end();
661 const_iterator __i1 = _SL1.begin();
662 const_iterator __i2 = _SL2.begin();
663 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2) {
667 return __i1 == __end1 && __i2 == __end2;
671 template <class _Tp, class _Alloc>
673 operator<(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2)
675 return std::lexicographical_compare(_SL1.begin(), _SL1.end(),
676 _SL2.begin(), _SL2.end());
679 template <class _Tp, class _Alloc>
681 operator!=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
682 return !(_SL1 == _SL2);
685 template <class _Tp, class _Alloc>
687 operator>(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
691 template <class _Tp, class _Alloc>
693 operator<=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
694 return !(_SL2 < _SL1);
697 template <class _Tp, class _Alloc>
699 operator>=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
700 return !(_SL1 < _SL2);
703 template <class _Tp, class _Alloc>
704 inline void swap(slist<_Tp,_Alloc>& __x, slist<_Tp,_Alloc>& __y) {
709 template <class _Tp, class _Alloc>
710 void slist<_Tp,_Alloc>::resize(size_type __len, const _Tp& __x)
712 _Node_base* __cur = &this->_M_head;
713 while (__cur->_M_next != 0 && __len > 0) {
715 __cur = __cur->_M_next;
718 this->_M_erase_after(__cur, 0);
720 _M_insert_after_fill(__cur, __len, __x);
723 template <class _Tp, class _Alloc>
724 void slist<_Tp,_Alloc>::remove(const _Tp& __val)
726 _Node_base* __cur = &this->_M_head;
727 while (__cur && __cur->_M_next) {
728 if (((_Node*) __cur->_M_next)->_M_data == __val)
729 this->_M_erase_after(__cur);
731 __cur = __cur->_M_next;
735 template <class _Tp, class _Alloc>
736 void slist<_Tp,_Alloc>::unique()
738 _Node_base* __cur = this->_M_head._M_next;
740 while (__cur->_M_next) {
741 if (((_Node*)__cur)->_M_data ==
742 ((_Node*)(__cur->_M_next))->_M_data)
743 this->_M_erase_after(__cur);
745 __cur = __cur->_M_next;
750 template <class _Tp, class _Alloc>
751 void slist<_Tp,_Alloc>::merge(slist<_Tp,_Alloc>& __x)
753 _Node_base* __n1 = &this->_M_head;
754 while (__n1->_M_next && __x._M_head._M_next) {
755 if (((_Node*) __x._M_head._M_next)->_M_data <
756 ((_Node*) __n1->_M_next)->_M_data)
757 __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
758 __n1 = __n1->_M_next;
760 if (__x._M_head._M_next) {
761 __n1->_M_next = __x._M_head._M_next;
762 __x._M_head._M_next = 0;
766 template <class _Tp, class _Alloc>
767 void slist<_Tp,_Alloc>::sort()
769 if (this->_M_head._M_next && this->_M_head._M_next->_M_next) {
774 __slist_splice_after(&__carry._M_head,
775 &this->_M_head, this->_M_head._M_next);
777 while (__i < __fill && !__counter[__i].empty()) {
778 __counter[__i].merge(__carry);
779 __carry.swap(__counter[__i]);
782 __carry.swap(__counter[__i]);
787 for (int __i = 1; __i < __fill; ++__i)
788 __counter[__i].merge(__counter[__i-1]);
789 this->swap(__counter[__fill-1]);
793 template <class _Tp, class _Alloc>
794 template <class _Predicate>
795 void slist<_Tp,_Alloc>::remove_if(_Predicate __pred)
797 _Node_base* __cur = &this->_M_head;
798 while (__cur->_M_next) {
799 if (__pred(((_Node*) __cur->_M_next)->_M_data))
800 this->_M_erase_after(__cur);
802 __cur = __cur->_M_next;
806 template <class _Tp, class _Alloc> template <class _BinaryPredicate>
807 void slist<_Tp,_Alloc>::unique(_BinaryPredicate __pred)
809 _Node* __cur = (_Node*) this->_M_head._M_next;
811 while (__cur->_M_next) {
812 if (__pred(((_Node*)__cur)->_M_data,
813 ((_Node*)(__cur->_M_next))->_M_data))
814 this->_M_erase_after(__cur);
816 __cur = (_Node*) __cur->_M_next;
821 template <class _Tp, class _Alloc> template <class _StrictWeakOrdering>
822 void slist<_Tp,_Alloc>::merge(slist<_Tp,_Alloc>& __x,
823 _StrictWeakOrdering __comp)
825 _Node_base* __n1 = &this->_M_head;
826 while (__n1->_M_next && __x._M_head._M_next) {
827 if (__comp(((_Node*) __x._M_head._M_next)->_M_data,
828 ((_Node*) __n1->_M_next)->_M_data))
829 __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
830 __n1 = __n1->_M_next;
832 if (__x._M_head._M_next) {
833 __n1->_M_next = __x._M_head._M_next;
834 __x._M_head._M_next = 0;
838 template <class _Tp, class _Alloc> template <class _StrictWeakOrdering>
839 void slist<_Tp,_Alloc>::sort(_StrictWeakOrdering __comp)
841 if (this->_M_head._M_next && this->_M_head._M_next->_M_next) {
846 __slist_splice_after(&__carry._M_head,
847 &this->_M_head, this->_M_head._M_next);
849 while (__i < __fill && !__counter[__i].empty()) {
850 __counter[__i].merge(__carry, __comp);
851 __carry.swap(__counter[__i]);
854 __carry.swap(__counter[__i]);
859 for (int __i = 1; __i < __fill; ++__i)
860 __counter[__i].merge(__counter[__i-1], __comp);
861 this->swap(__counter[__fill-1]);
865 } // namespace __gnu_cxx
869 // Specialization of insert_iterator so that insertions will be constant
870 // time rather than linear time.
872 template <class _Tp, class _Alloc>
873 class insert_iterator<__gnu_cxx::slist<_Tp, _Alloc> > {
875 typedef __gnu_cxx::slist<_Tp, _Alloc> _Container;
876 _Container* container;
877 typename _Container::iterator iter;
879 typedef _Container container_type;
880 typedef output_iterator_tag iterator_category;
881 typedef void value_type;
882 typedef void difference_type;
883 typedef void pointer;
884 typedef void reference;
886 insert_iterator(_Container& __x, typename _Container::iterator __i)
888 if (__i == __x.begin())
889 iter = __x.before_begin();
891 iter = __x.previous(__i);
894 insert_iterator<_Container>&
895 operator=(const typename _Container::value_type& __value) {
896 iter = container->insert_after(iter, __value);
899 insert_iterator<_Container>& operator*() { return *this; }
900 insert_iterator<_Container>& operator++() { return *this; }
901 insert_iterator<_Container>& operator++(int) { return *this; }