1 // Singly-linked list implementation -*- C++ -*-
3 // Copyright (C) 2001 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 header file is an extension to the Standard C++ Library. You should
46 * use the "ext/" path prefix in your @c #include statements.
49 #ifndef __SGI_STL_INTERNAL_SLIST_H
50 #define __SGI_STL_INTERNAL_SLIST_H
52 #include <bits/stl_algobase.h>
53 #include <bits/stl_alloc.h>
54 #include <bits/stl_construct.h>
55 #include <bits/stl_uninitialized.h>
56 #include <bits/concept_check.h>
62 using std::_Alloc_traits;
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)
204 // Base class that encapsulates details of allocators. Three cases:
205 // an ordinary standard-conforming allocator, a standard-conforming
206 // allocator with no non-static data, and an SGI-style allocator.
207 // This complexity is necessary only because we're worrying about backward
208 // compatibility and because we want to avoid wasting storage on an
209 // allocator instance if it isn't necessary.
211 // Base for general standard-conforming allocators.
212 template <class _Tp, class _Allocator, bool _IsStatic>
213 class _Slist_alloc_base {
215 typedef typename _Alloc_traits<_Tp,_Allocator>::allocator_type
217 allocator_type get_allocator() const { return _M_node_allocator; }
219 _Slist_alloc_base(const allocator_type& __a) : _M_node_allocator(__a) {}
222 _Slist_node<_Tp>* _M_get_node()
223 { return _M_node_allocator.allocate(1); }
224 void _M_put_node(_Slist_node<_Tp>* __p)
225 { _M_node_allocator.deallocate(__p, 1); }
228 typename _Alloc_traits<_Slist_node<_Tp>,_Allocator>::allocator_type
230 _Slist_node_base _M_head;
233 // Specialization for instanceless allocators.
234 template <class _Tp, class _Allocator>
235 class _Slist_alloc_base<_Tp,_Allocator, true> {
237 typedef typename _Alloc_traits<_Tp,_Allocator>::allocator_type
239 allocator_type get_allocator() const { return allocator_type(); }
241 _Slist_alloc_base(const allocator_type&) {}
244 typedef typename _Alloc_traits<_Slist_node<_Tp>, _Allocator>::_Alloc_type
246 _Slist_node<_Tp>* _M_get_node() { return _Alloc_type::allocate(1); }
247 void _M_put_node(_Slist_node<_Tp>* __p) { _Alloc_type::deallocate(__p, 1); }
250 _Slist_node_base _M_head;
254 template <class _Tp, class _Alloc>
256 : public _Slist_alloc_base<_Tp, _Alloc,
257 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
259 typedef _Slist_alloc_base<_Tp, _Alloc,
260 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
262 typedef typename _Base::allocator_type allocator_type;
264 _Slist_base(const allocator_type& __a)
265 : _Base(__a) { this->_M_head._M_next = 0; }
266 ~_Slist_base() { _M_erase_after(&this->_M_head, 0); }
270 _Slist_node_base* _M_erase_after(_Slist_node_base* __pos)
272 _Slist_node<_Tp>* __next = (_Slist_node<_Tp>*) (__pos->_M_next);
273 _Slist_node_base* __next_next = __next->_M_next;
274 __pos->_M_next = __next_next;
275 _Destroy(&__next->_M_data);
279 _Slist_node_base* _M_erase_after(_Slist_node_base*, _Slist_node_base*);
282 template <class _Tp, class _Alloc>
284 _Slist_base<_Tp,_Alloc>::_M_erase_after(_Slist_node_base* __before_first,
285 _Slist_node_base* __last_node) {
286 _Slist_node<_Tp>* __cur = (_Slist_node<_Tp>*) (__before_first->_M_next);
287 while (__cur != __last_node) {
288 _Slist_node<_Tp>* __tmp = __cur;
289 __cur = (_Slist_node<_Tp>*) __cur->_M_next;
290 _Destroy(&__tmp->_M_data);
293 __before_first->_M_next = __last_node;
297 template <class _Tp, class _Alloc = allocator<_Tp> >
298 class slist : private _Slist_base<_Tp,_Alloc>
300 // concept requirements
301 __glibcpp_class_requires(_Tp, _SGIAssignableConcept)
304 typedef _Slist_base<_Tp,_Alloc> _Base;
306 typedef _Tp value_type;
307 typedef value_type* pointer;
308 typedef const value_type* const_pointer;
309 typedef value_type& reference;
310 typedef const value_type& const_reference;
311 typedef size_t size_type;
312 typedef ptrdiff_t difference_type;
314 typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
315 typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
317 typedef typename _Base::allocator_type allocator_type;
318 allocator_type get_allocator() const { return _Base::get_allocator(); }
321 typedef _Slist_node<_Tp> _Node;
322 typedef _Slist_node_base _Node_base;
323 typedef _Slist_iterator_base _Iterator_base;
325 _Node* _M_create_node(const value_type& __x) {
326 _Node* __node = this->_M_get_node();
328 _Construct(&__node->_M_data, __x);
333 this->_M_put_node(__node);
334 __throw_exception_again;
339 _Node* _M_create_node() {
340 _Node* __node = this->_M_get_node();
342 _Construct(&__node->_M_data);
347 this->_M_put_node(__node);
348 __throw_exception_again;
354 explicit slist(const allocator_type& __a = allocator_type()) : _Base(__a) {}
356 slist(size_type __n, const value_type& __x,
357 const allocator_type& __a = allocator_type()) : _Base(__a)
358 { _M_insert_after_fill(&this->_M_head, __n, __x); }
360 explicit slist(size_type __n) : _Base(allocator_type())
361 { _M_insert_after_fill(&this->_M_head, __n, value_type()); }
363 // We don't need any dispatching tricks here, because _M_insert_after_range
364 // already does them.
365 template <class _InputIterator>
366 slist(_InputIterator __first, _InputIterator __last,
367 const allocator_type& __a = allocator_type()) : _Base(__a)
368 { _M_insert_after_range(&this->_M_head, __first, __last); }
370 slist(const slist& __x) : _Base(__x.get_allocator())
371 { _M_insert_after_range(&this->_M_head, __x.begin(), __x.end()); }
373 slist& operator= (const slist& __x);
378 // assign(), a generalized assignment member function. Two
379 // versions: one that takes a count, and one that takes a range.
380 // The range version is a member template, so we dispatch on whether
381 // or not the type is an integer.
383 void assign(size_type __n, const _Tp& __val)
384 { _M_fill_assign(__n, __val); }
386 void _M_fill_assign(size_type __n, const _Tp& __val);
388 template <class _InputIterator>
389 void assign(_InputIterator __first, _InputIterator __last) {
390 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
391 _M_assign_dispatch(__first, __last, _Integral());
394 template <class _Integer>
395 void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
396 { _M_fill_assign((size_type) __n, (_Tp) __val); }
398 template <class _InputIterator>
399 void _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
404 iterator begin() { return iterator((_Node*)this->_M_head._M_next); }
405 const_iterator begin() const
406 { return const_iterator((_Node*)this->_M_head._M_next);}
408 iterator end() { return iterator(0); }
409 const_iterator end() const { return const_iterator(0); }
411 // Experimental new feature: before_begin() returns a
412 // non-dereferenceable iterator that, when incremented, yields
413 // begin(). This iterator may be used as the argument to
414 // insert_after, erase_after, etc. Note that even for an empty
415 // slist, before_begin() is not the same iterator as end(). It
416 // is always necessary to increment before_begin() at least once to
418 iterator before_begin() { return iterator((_Node*) &this->_M_head); }
419 const_iterator before_begin() const
420 { return const_iterator((_Node*) &this->_M_head); }
422 size_type size() const { return __slist_size(this->_M_head._M_next); }
424 size_type max_size() const { return size_type(-1); }
426 bool empty() const { return this->_M_head._M_next == 0; }
428 void swap(slist& __x)
429 { std::swap(this->_M_head._M_next, __x._M_head._M_next); }
433 reference front() { return ((_Node*) this->_M_head._M_next)->_M_data; }
434 const_reference front() const
435 { return ((_Node*) this->_M_head._M_next)->_M_data; }
436 void push_front(const value_type& __x) {
437 __slist_make_link(&this->_M_head, _M_create_node(__x));
439 void push_front() { __slist_make_link(&this->_M_head, _M_create_node()); }
441 _Node* __node = (_Node*) this->_M_head._M_next;
442 this->_M_head._M_next = __node->_M_next;
443 _Destroy(&__node->_M_data);
444 this->_M_put_node(__node);
447 iterator previous(const_iterator __pos) {
448 return iterator((_Node*) __slist_previous(&this->_M_head, __pos._M_node));
450 const_iterator previous(const_iterator __pos) const {
451 return const_iterator((_Node*) __slist_previous(&this->_M_head,
456 _Node* _M_insert_after(_Node_base* __pos, const value_type& __x) {
457 return (_Node*) (__slist_make_link(__pos, _M_create_node(__x)));
460 _Node* _M_insert_after(_Node_base* __pos) {
461 return (_Node*) (__slist_make_link(__pos, _M_create_node()));
464 void _M_insert_after_fill(_Node_base* __pos,
465 size_type __n, const value_type& __x) {
466 for (size_type __i = 0; __i < __n; ++__i)
467 __pos = __slist_make_link(__pos, _M_create_node(__x));
470 // Check whether it's an integral type. If so, it's not an iterator.
471 template <class _InIter>
472 void _M_insert_after_range(_Node_base* __pos,
473 _InIter __first, _InIter __last) {
474 typedef typename _Is_integer<_InIter>::_Integral _Integral;
475 _M_insert_after_range(__pos, __first, __last, _Integral());
478 template <class _Integer>
479 void _M_insert_after_range(_Node_base* __pos, _Integer __n, _Integer __x,
481 _M_insert_after_fill(__pos, __n, __x);
484 template <class _InIter>
485 void _M_insert_after_range(_Node_base* __pos,
486 _InIter __first, _InIter __last,
488 while (__first != __last) {
489 __pos = __slist_make_link(__pos, _M_create_node(*__first));
496 iterator insert_after(iterator __pos, const value_type& __x) {
497 return iterator(_M_insert_after(__pos._M_node, __x));
500 iterator insert_after(iterator __pos) {
501 return insert_after(__pos, value_type());
504 void insert_after(iterator __pos, size_type __n, const value_type& __x) {
505 _M_insert_after_fill(__pos._M_node, __n, __x);
508 // We don't need any dispatching tricks here, because _M_insert_after_range
509 // already does them.
510 template <class _InIter>
511 void insert_after(iterator __pos, _InIter __first, _InIter __last) {
512 _M_insert_after_range(__pos._M_node, __first, __last);
515 iterator insert(iterator __pos, const value_type& __x) {
516 return iterator(_M_insert_after(__slist_previous(&this->_M_head,
521 iterator insert(iterator __pos) {
522 return iterator(_M_insert_after(__slist_previous(&this->_M_head,
527 void insert(iterator __pos, size_type __n, const value_type& __x) {
528 _M_insert_after_fill(__slist_previous(&this->_M_head, __pos._M_node),
532 // We don't need any dispatching tricks here, because _M_insert_after_range
533 // already does them.
534 template <class _InIter>
535 void insert(iterator __pos, _InIter __first, _InIter __last) {
536 _M_insert_after_range(__slist_previous(&this->_M_head, __pos._M_node),
541 iterator erase_after(iterator __pos) {
542 return iterator((_Node*) this->_M_erase_after(__pos._M_node));
544 iterator erase_after(iterator __before_first, iterator __last) {
545 return iterator((_Node*) this->_M_erase_after(__before_first._M_node,
549 iterator erase(iterator __pos) {
550 return (_Node*) this->_M_erase_after(__slist_previous(&this->_M_head,
553 iterator erase(iterator __first, iterator __last) {
554 return (_Node*) this->_M_erase_after(
555 __slist_previous(&this->_M_head, __first._M_node), __last._M_node);
558 void resize(size_type new_size, const _Tp& __x);
559 void resize(size_type new_size) { resize(new_size, _Tp()); }
560 void clear() { this->_M_erase_after(&this->_M_head, 0); }
563 // Moves the range [__before_first + 1, __before_last + 1) to *this,
564 // inserting it immediately after __pos. This is constant time.
565 void splice_after(iterator __pos,
566 iterator __before_first, iterator __before_last)
568 if (__before_first != __before_last)
569 __slist_splice_after(__pos._M_node, __before_first._M_node,
570 __before_last._M_node);
573 // Moves the element that follows __prev to *this, inserting it immediately
574 // after __pos. This is constant time.
575 void splice_after(iterator __pos, iterator __prev)
577 __slist_splice_after(__pos._M_node,
578 __prev._M_node, __prev._M_node->_M_next);
582 // Removes all of the elements from the list __x to *this, inserting
583 // them immediately after __pos. __x must not be *this. Complexity:
584 // linear in __x.size().
585 void splice_after(iterator __pos, slist& __x)
587 __slist_splice_after(__pos._M_node, &__x._M_head);
590 // Linear in distance(begin(), __pos), and linear in __x.size().
591 void splice(iterator __pos, slist& __x) {
592 if (__x._M_head._M_next)
593 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
594 &__x._M_head, __slist_previous(&__x._M_head, 0));
597 // Linear in distance(begin(), __pos), and in distance(__x.begin(), __i).
598 void splice(iterator __pos, slist& __x, iterator __i) {
599 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
600 __slist_previous(&__x._M_head, __i._M_node),
604 // Linear in distance(begin(), __pos), in distance(__x.begin(), __first),
605 // and in distance(__first, __last).
606 void splice(iterator __pos, slist& __x, iterator __first, iterator __last)
608 if (__first != __last)
609 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
610 __slist_previous(&__x._M_head, __first._M_node),
611 __slist_previous(__first._M_node, __last._M_node));
616 if (this->_M_head._M_next)
617 this->_M_head._M_next = __slist_reverse(this->_M_head._M_next);
620 void remove(const _Tp& __val);
622 void merge(slist& __x);
625 template <class _Predicate>
626 void remove_if(_Predicate __pred);
628 template <class _BinaryPredicate>
629 void unique(_BinaryPredicate __pred);
631 template <class _StrictWeakOrdering>
632 void merge(slist&, _StrictWeakOrdering);
634 template <class _StrictWeakOrdering>
635 void sort(_StrictWeakOrdering __comp);
638 template <class _Tp, class _Alloc>
639 slist<_Tp,_Alloc>& slist<_Tp,_Alloc>::operator=(const slist<_Tp,_Alloc>& __x)
642 _Node_base* __p1 = &this->_M_head;
643 _Node* __n1 = (_Node*) this->_M_head._M_next;
644 const _Node* __n2 = (const _Node*) __x._M_head._M_next;
645 while (__n1 && __n2) {
646 __n1->_M_data = __n2->_M_data;
648 __n1 = (_Node*) __n1->_M_next;
649 __n2 = (const _Node*) __n2->_M_next;
652 this->_M_erase_after(__p1, 0);
654 _M_insert_after_range(__p1, const_iterator((_Node*)__n2),
660 template <class _Tp, class _Alloc>
661 void slist<_Tp, _Alloc>::_M_fill_assign(size_type __n, const _Tp& __val) {
662 _Node_base* __prev = &this->_M_head;
663 _Node* __node = (_Node*) this->_M_head._M_next;
664 for ( ; __node != 0 && __n > 0 ; --__n) {
665 __node->_M_data = __val;
667 __node = (_Node*) __node->_M_next;
670 _M_insert_after_fill(__prev, __n, __val);
672 this->_M_erase_after(__prev, 0);
675 template <class _Tp, class _Alloc> template <class _InputIter>
677 slist<_Tp, _Alloc>::_M_assign_dispatch(_InputIter __first, _InputIter __last,
680 _Node_base* __prev = &this->_M_head;
681 _Node* __node = (_Node*) this->_M_head._M_next;
682 while (__node != 0 && __first != __last) {
683 __node->_M_data = *__first;
685 __node = (_Node*) __node->_M_next;
688 if (__first != __last)
689 _M_insert_after_range(__prev, __first, __last);
691 this->_M_erase_after(__prev, 0);
694 template <class _Tp, class _Alloc>
696 operator==(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2)
698 typedef typename slist<_Tp,_Alloc>::const_iterator const_iterator;
699 const_iterator __end1 = _SL1.end();
700 const_iterator __end2 = _SL2.end();
702 const_iterator __i1 = _SL1.begin();
703 const_iterator __i2 = _SL2.begin();
704 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2) {
708 return __i1 == __end1 && __i2 == __end2;
712 template <class _Tp, class _Alloc>
714 operator<(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2)
716 return std::lexicographical_compare(_SL1.begin(), _SL1.end(),
717 _SL2.begin(), _SL2.end());
720 template <class _Tp, class _Alloc>
722 operator!=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
723 return !(_SL1 == _SL2);
726 template <class _Tp, class _Alloc>
728 operator>(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
732 template <class _Tp, class _Alloc>
734 operator<=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
735 return !(_SL2 < _SL1);
738 template <class _Tp, class _Alloc>
740 operator>=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
741 return !(_SL1 < _SL2);
744 template <class _Tp, class _Alloc>
745 inline void swap(slist<_Tp,_Alloc>& __x, slist<_Tp,_Alloc>& __y) {
750 template <class _Tp, class _Alloc>
751 void slist<_Tp,_Alloc>::resize(size_type __len, const _Tp& __x)
753 _Node_base* __cur = &this->_M_head;
754 while (__cur->_M_next != 0 && __len > 0) {
756 __cur = __cur->_M_next;
759 this->_M_erase_after(__cur, 0);
761 _M_insert_after_fill(__cur, __len, __x);
764 template <class _Tp, class _Alloc>
765 void slist<_Tp,_Alloc>::remove(const _Tp& __val)
767 _Node_base* __cur = &this->_M_head;
768 while (__cur && __cur->_M_next) {
769 if (((_Node*) __cur->_M_next)->_M_data == __val)
770 this->_M_erase_after(__cur);
772 __cur = __cur->_M_next;
776 template <class _Tp, class _Alloc>
777 void slist<_Tp,_Alloc>::unique()
779 _Node_base* __cur = this->_M_head._M_next;
781 while (__cur->_M_next) {
782 if (((_Node*)__cur)->_M_data ==
783 ((_Node*)(__cur->_M_next))->_M_data)
784 this->_M_erase_after(__cur);
786 __cur = __cur->_M_next;
791 template <class _Tp, class _Alloc>
792 void slist<_Tp,_Alloc>::merge(slist<_Tp,_Alloc>& __x)
794 _Node_base* __n1 = &this->_M_head;
795 while (__n1->_M_next && __x._M_head._M_next) {
796 if (((_Node*) __x._M_head._M_next)->_M_data <
797 ((_Node*) __n1->_M_next)->_M_data)
798 __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
799 __n1 = __n1->_M_next;
801 if (__x._M_head._M_next) {
802 __n1->_M_next = __x._M_head._M_next;
803 __x._M_head._M_next = 0;
807 template <class _Tp, class _Alloc>
808 void slist<_Tp,_Alloc>::sort()
810 if (this->_M_head._M_next && this->_M_head._M_next->_M_next) {
815 __slist_splice_after(&__carry._M_head,
816 &this->_M_head, this->_M_head._M_next);
818 while (__i < __fill && !__counter[__i].empty()) {
819 __counter[__i].merge(__carry);
820 __carry.swap(__counter[__i]);
823 __carry.swap(__counter[__i]);
828 for (int __i = 1; __i < __fill; ++__i)
829 __counter[__i].merge(__counter[__i-1]);
830 this->swap(__counter[__fill-1]);
834 template <class _Tp, class _Alloc>
835 template <class _Predicate>
836 void slist<_Tp,_Alloc>::remove_if(_Predicate __pred)
838 _Node_base* __cur = &this->_M_head;
839 while (__cur->_M_next) {
840 if (__pred(((_Node*) __cur->_M_next)->_M_data))
841 this->_M_erase_after(__cur);
843 __cur = __cur->_M_next;
847 template <class _Tp, class _Alloc> template <class _BinaryPredicate>
848 void slist<_Tp,_Alloc>::unique(_BinaryPredicate __pred)
850 _Node* __cur = (_Node*) this->_M_head._M_next;
852 while (__cur->_M_next) {
853 if (__pred(((_Node*)__cur)->_M_data,
854 ((_Node*)(__cur->_M_next))->_M_data))
855 this->_M_erase_after(__cur);
857 __cur = (_Node*) __cur->_M_next;
862 template <class _Tp, class _Alloc> template <class _StrictWeakOrdering>
863 void slist<_Tp,_Alloc>::merge(slist<_Tp,_Alloc>& __x,
864 _StrictWeakOrdering __comp)
866 _Node_base* __n1 = &this->_M_head;
867 while (__n1->_M_next && __x._M_head._M_next) {
868 if (__comp(((_Node*) __x._M_head._M_next)->_M_data,
869 ((_Node*) __n1->_M_next)->_M_data))
870 __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
871 __n1 = __n1->_M_next;
873 if (__x._M_head._M_next) {
874 __n1->_M_next = __x._M_head._M_next;
875 __x._M_head._M_next = 0;
879 template <class _Tp, class _Alloc> template <class _StrictWeakOrdering>
880 void slist<_Tp,_Alloc>::sort(_StrictWeakOrdering __comp)
882 if (this->_M_head._M_next && this->_M_head._M_next->_M_next) {
887 __slist_splice_after(&__carry._M_head,
888 &this->_M_head, this->_M_head._M_next);
890 while (__i < __fill && !__counter[__i].empty()) {
891 __counter[__i].merge(__carry, __comp);
892 __carry.swap(__counter[__i]);
895 __carry.swap(__counter[__i]);
900 for (int __i = 1; __i < __fill; ++__i)
901 __counter[__i].merge(__counter[__i-1], __comp);
902 this->swap(__counter[__fill-1]);
906 } // namespace __gnu_cxx
910 // Specialization of insert_iterator so that insertions will be constant
911 // time rather than linear time.
913 template <class _Tp, class _Alloc>
914 class insert_iterator<__gnu_cxx::slist<_Tp, _Alloc> > {
916 typedef __gnu_cxx::slist<_Tp, _Alloc> _Container;
917 _Container* container;
918 typename _Container::iterator iter;
920 typedef _Container container_type;
921 typedef output_iterator_tag iterator_category;
922 typedef void value_type;
923 typedef void difference_type;
924 typedef void pointer;
925 typedef void reference;
927 insert_iterator(_Container& __x, typename _Container::iterator __i)
929 if (__i == __x.begin())
930 iter = __x.before_begin();
932 iter = __x.previous(__i);
935 insert_iterator<_Container>&
936 operator=(const typename _Container::value_type& __value) {
937 iter = container->insert_after(iter, __value);
940 insert_iterator<_Container>& operator*() { return *this; }
941 insert_iterator<_Container>& operator++() { return *this; }
942 insert_iterator<_Container>& operator++(int) { return *this; }
947 #endif /* __SGI_STL_INTERNAL_SLIST_H */