3 * Silicon Graphics Computer Systems, Inc.
5 * Permission to use, copy, modify, distribute and sell this software
6 * and its documentation for any purpose is hereby granted without fee,
7 * provided that the above copyright notice appear in all copies and
8 * that both that copyright notice and this permission notice appear
9 * in supporting documentation. Silicon Graphics makes no
10 * representations about the suitability of this software for any
11 * purpose. It is provided "as is" without express or implied warranty.
15 /* NOTE: This is an internal header file, included by other STL headers.
16 * You should not attempt to use it directly.
19 #ifndef __SGI_STL_INTERNAL_SLIST_H
20 #define __SGI_STL_INTERNAL_SLIST_H
22 #include <bits/concept_checks.h>
27 struct _Slist_node_base
29 _Slist_node_base* _M_next;
32 inline _Slist_node_base*
33 __slist_make_link(_Slist_node_base* __prev_node,
34 _Slist_node_base* __new_node)
36 __new_node->_M_next = __prev_node->_M_next;
37 __prev_node->_M_next = __new_node;
41 inline _Slist_node_base*
42 __slist_previous(_Slist_node_base* __head,
43 const _Slist_node_base* __node)
45 while (__head && __head->_M_next != __node)
46 __head = __head->_M_next;
50 inline const _Slist_node_base*
51 __slist_previous(const _Slist_node_base* __head,
52 const _Slist_node_base* __node)
54 while (__head && __head->_M_next != __node)
55 __head = __head->_M_next;
59 inline void __slist_splice_after(_Slist_node_base* __pos,
60 _Slist_node_base* __before_first,
61 _Slist_node_base* __before_last)
63 if (__pos != __before_first && __pos != __before_last) {
64 _Slist_node_base* __first = __before_first->_M_next;
65 _Slist_node_base* __after = __pos->_M_next;
66 __before_first->_M_next = __before_last->_M_next;
67 __pos->_M_next = __first;
68 __before_last->_M_next = __after;
73 __slist_splice_after(_Slist_node_base* __pos, _Slist_node_base* __head)
75 _Slist_node_base* __before_last = __slist_previous(__head, 0);
76 if (__before_last != __head) {
77 _Slist_node_base* __after = __pos->_M_next;
78 __pos->_M_next = __head->_M_next;
80 __before_last->_M_next = __after;
84 inline _Slist_node_base* __slist_reverse(_Slist_node_base* __node)
86 _Slist_node_base* __result = __node;
87 __node = __node->_M_next;
88 __result->_M_next = 0;
90 _Slist_node_base* __next = __node->_M_next;
91 __node->_M_next = __result;
98 inline size_t __slist_size(_Slist_node_base* __node)
101 for ( ; __node != 0; __node = __node->_M_next)
107 struct _Slist_node : public _Slist_node_base
112 struct _Slist_iterator_base
114 typedef size_t size_type;
115 typedef ptrdiff_t difference_type;
116 typedef forward_iterator_tag iterator_category;
118 _Slist_node_base* _M_node;
120 _Slist_iterator_base(_Slist_node_base* __x) : _M_node(__x) {}
121 void _M_incr() { _M_node = _M_node->_M_next; }
123 bool operator==(const _Slist_iterator_base& __x) const {
124 return _M_node == __x._M_node;
126 bool operator!=(const _Slist_iterator_base& __x) const {
127 return _M_node != __x._M_node;
131 template <class _Tp, class _Ref, class _Ptr>
132 struct _Slist_iterator : public _Slist_iterator_base
134 typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
135 typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
136 typedef _Slist_iterator<_Tp, _Ref, _Ptr> _Self;
138 typedef _Tp value_type;
139 typedef _Ptr pointer;
140 typedef _Ref reference;
141 typedef _Slist_node<_Tp> _Node;
143 _Slist_iterator(_Node* __x) : _Slist_iterator_base(__x) {}
144 _Slist_iterator() : _Slist_iterator_base(0) {}
145 _Slist_iterator(const iterator& __x) : _Slist_iterator_base(__x._M_node) {}
147 reference operator*() const { return ((_Node*) _M_node)->_M_data; }
148 pointer operator->() const { return &(operator*()); }
155 _Self operator++(int)
164 // Base class that encapsulates details of allocators. Three cases:
165 // an ordinary standard-conforming allocator, a standard-conforming
166 // allocator with no non-static data, and an SGI-style allocator.
167 // This complexity is necessary only because we're worrying about backward
168 // compatibility and because we want to avoid wasting storage on an
169 // allocator instance if it isn't necessary.
171 // Base for general standard-conforming allocators.
172 template <class _Tp, class _Allocator, bool _IsStatic>
173 class _Slist_alloc_base {
175 typedef typename _Alloc_traits<_Tp,_Allocator>::allocator_type
177 allocator_type get_allocator() const { return _M_node_allocator; }
179 _Slist_alloc_base(const allocator_type& __a) : _M_node_allocator(__a) {}
182 _Slist_node<_Tp>* _M_get_node()
183 { return _M_node_allocator.allocate(1); }
184 void _M_put_node(_Slist_node<_Tp>* __p)
185 { _M_node_allocator.deallocate(__p, 1); }
188 typename _Alloc_traits<_Slist_node<_Tp>,_Allocator>::allocator_type
190 _Slist_node_base _M_head;
193 // Specialization for instanceless allocators.
194 template <class _Tp, class _Allocator>
195 class _Slist_alloc_base<_Tp,_Allocator, true> {
197 typedef typename _Alloc_traits<_Tp,_Allocator>::allocator_type
199 allocator_type get_allocator() const { return allocator_type(); }
201 _Slist_alloc_base(const allocator_type&) {}
204 typedef typename _Alloc_traits<_Slist_node<_Tp>, _Allocator>::_Alloc_type
206 _Slist_node<_Tp>* _M_get_node() { return _Alloc_type::allocate(1); }
207 void _M_put_node(_Slist_node<_Tp>* __p) { _Alloc_type::deallocate(__p, 1); }
210 _Slist_node_base _M_head;
214 template <class _Tp, class _Alloc>
216 : public _Slist_alloc_base<_Tp, _Alloc,
217 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
219 typedef _Slist_alloc_base<_Tp, _Alloc,
220 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
222 typedef typename _Base::allocator_type allocator_type;
224 _Slist_base(const allocator_type& __a)
225 : _Base(__a) { this->_M_head._M_next = 0; }
226 ~_Slist_base() { _M_erase_after(&this->_M_head, 0); }
230 _Slist_node_base* _M_erase_after(_Slist_node_base* __pos)
232 _Slist_node<_Tp>* __next = (_Slist_node<_Tp>*) (__pos->_M_next);
233 _Slist_node_base* __next_next = __next->_M_next;
234 __pos->_M_next = __next_next;
235 destroy(&__next->_M_data);
239 _Slist_node_base* _M_erase_after(_Slist_node_base*, _Slist_node_base*);
242 template <class _Tp, class _Alloc>
244 _Slist_base<_Tp,_Alloc>::_M_erase_after(_Slist_node_base* __before_first,
245 _Slist_node_base* __last_node) {
246 _Slist_node<_Tp>* __cur = (_Slist_node<_Tp>*) (__before_first->_M_next);
247 while (__cur != __last_node) {
248 _Slist_node<_Tp>* __tmp = __cur;
249 __cur = (_Slist_node<_Tp>*) __cur->_M_next;
250 destroy(&__tmp->_M_data);
253 __before_first->_M_next = __last_node;
257 template <class _Tp, class _Alloc = allocator<_Tp> >
258 class slist : private _Slist_base<_Tp,_Alloc>
262 __STL_CLASS_REQUIRES(_Tp, _Assignable);
265 typedef _Slist_base<_Tp,_Alloc> _Base;
267 typedef _Tp value_type;
268 typedef value_type* pointer;
269 typedef const value_type* const_pointer;
270 typedef value_type& reference;
271 typedef const value_type& const_reference;
272 typedef size_t size_type;
273 typedef ptrdiff_t difference_type;
275 typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
276 typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
278 typedef typename _Base::allocator_type allocator_type;
279 allocator_type get_allocator() const { return _Base::get_allocator(); }
282 typedef _Slist_node<_Tp> _Node;
283 typedef _Slist_node_base _Node_base;
284 typedef _Slist_iterator_base _Iterator_base;
286 _Node* _M_create_node(const value_type& __x) {
287 _Node* __node = this->_M_get_node();
289 construct(&__node->_M_data, __x);
292 __STL_UNWIND(this->_M_put_node(__node));
296 _Node* _M_create_node() {
297 _Node* __node = this->_M_get_node();
299 construct(&__node->_M_data);
302 __STL_UNWIND(this->_M_put_node(__node));
307 explicit slist(const allocator_type& __a = allocator_type()) : _Base(__a) {}
309 slist(size_type __n, const value_type& __x,
310 const allocator_type& __a = allocator_type()) : _Base(__a)
311 { _M_insert_after_fill(&this->_M_head, __n, __x); }
313 explicit slist(size_type __n) : _Base(allocator_type())
314 { _M_insert_after_fill(&this->_M_head, __n, value_type()); }
316 // We don't need any dispatching tricks here, because _M_insert_after_range
317 // already does them.
318 template <class _InputIterator>
319 slist(_InputIterator __first, _InputIterator __last,
320 const allocator_type& __a = allocator_type()) : _Base(__a)
321 { _M_insert_after_range(&this->_M_head, __first, __last); }
323 slist(const slist& __x) : _Base(__x.get_allocator())
324 { _M_insert_after_range(&this->_M_head, __x.begin(), __x.end()); }
326 slist& operator= (const slist& __x);
331 // assign(), a generalized assignment member function. Two
332 // versions: one that takes a count, and one that takes a range.
333 // The range version is a member template, so we dispatch on whether
334 // or not the type is an integer.
336 void assign(size_type __n, const _Tp& __val)
337 { _M_fill_assign(__n, __val); }
339 void _M_fill_assign(size_type __n, const _Tp& __val);
341 template <class _InputIterator>
342 void assign(_InputIterator __first, _InputIterator __last) {
343 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
344 _M_assign_dispatch(__first, __last, _Integral());
347 template <class _Integer>
348 void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
349 { _M_fill_assign((size_type) __n, (_Tp) __val); }
351 template <class _InputIterator>
352 void _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
357 iterator begin() { return iterator((_Node*)this->_M_head._M_next); }
358 const_iterator begin() const
359 { return const_iterator((_Node*)this->_M_head._M_next);}
361 iterator end() { return iterator(0); }
362 const_iterator end() const { return const_iterator(0); }
364 // Experimental new feature: before_begin() returns a
365 // non-dereferenceable iterator that, when incremented, yields
366 // begin(). This iterator may be used as the argument to
367 // insert_after, erase_after, etc. Note that even for an empty
368 // slist, before_begin() is not the same iterator as end(). It
369 // is always necessary to increment before_begin() at least once to
371 iterator before_begin() { return iterator((_Node*) &this->_M_head); }
372 const_iterator before_begin() const
373 { return const_iterator((_Node*) &this->_M_head); }
375 size_type size() const { return __slist_size(this->_M_head._M_next); }
377 size_type max_size() const { return size_type(-1); }
379 bool empty() const { return this->_M_head._M_next == 0; }
381 void swap(slist& __x)
382 { std::swap(this->_M_head._M_next, __x._M_head._M_next); }
386 reference front() { return ((_Node*) this->_M_head._M_next)->_M_data; }
387 const_reference front() const
388 { return ((_Node*) this->_M_head._M_next)->_M_data; }
389 void push_front(const value_type& __x) {
390 __slist_make_link(&this->_M_head, _M_create_node(__x));
392 void push_front() { __slist_make_link(&this->_M_head, _M_create_node()); }
394 _Node* __node = (_Node*) this->_M_head._M_next;
395 this->_M_head._M_next = __node->_M_next;
396 destroy(&__node->_M_data);
397 this->_M_put_node(__node);
400 iterator previous(const_iterator __pos) {
401 return iterator((_Node*) __slist_previous(&this->_M_head, __pos._M_node));
403 const_iterator previous(const_iterator __pos) const {
404 return const_iterator((_Node*) __slist_previous(&this->_M_head,
409 _Node* _M_insert_after(_Node_base* __pos, const value_type& __x) {
410 return (_Node*) (__slist_make_link(__pos, _M_create_node(__x)));
413 _Node* _M_insert_after(_Node_base* __pos) {
414 return (_Node*) (__slist_make_link(__pos, _M_create_node()));
417 void _M_insert_after_fill(_Node_base* __pos,
418 size_type __n, const value_type& __x) {
419 for (size_type __i = 0; __i < __n; ++__i)
420 __pos = __slist_make_link(__pos, _M_create_node(__x));
423 // Check whether it's an integral type. If so, it's not an iterator.
424 template <class _InIter>
425 void _M_insert_after_range(_Node_base* __pos,
426 _InIter __first, _InIter __last) {
427 typedef typename _Is_integer<_InIter>::_Integral _Integral;
428 _M_insert_after_range(__pos, __first, __last, _Integral());
431 template <class _Integer>
432 void _M_insert_after_range(_Node_base* __pos, _Integer __n, _Integer __x,
434 _M_insert_after_fill(__pos, __n, __x);
437 template <class _InIter>
438 void _M_insert_after_range(_Node_base* __pos,
439 _InIter __first, _InIter __last,
441 while (__first != __last) {
442 __pos = __slist_make_link(__pos, _M_create_node(*__first));
449 iterator insert_after(iterator __pos, const value_type& __x) {
450 return iterator(_M_insert_after(__pos._M_node, __x));
453 iterator insert_after(iterator __pos) {
454 return insert_after(__pos, value_type());
457 void insert_after(iterator __pos, size_type __n, const value_type& __x) {
458 _M_insert_after_fill(__pos._M_node, __n, __x);
461 // We don't need any dispatching tricks here, because _M_insert_after_range
462 // already does them.
463 template <class _InIter>
464 void insert_after(iterator __pos, _InIter __first, _InIter __last) {
465 _M_insert_after_range(__pos._M_node, __first, __last);
468 iterator insert(iterator __pos, const value_type& __x) {
469 return iterator(_M_insert_after(__slist_previous(&this->_M_head,
474 iterator insert(iterator __pos) {
475 return iterator(_M_insert_after(__slist_previous(&this->_M_head,
480 void insert(iterator __pos, size_type __n, const value_type& __x) {
481 _M_insert_after_fill(__slist_previous(&this->_M_head, __pos._M_node),
485 // We don't need any dispatching tricks here, because _M_insert_after_range
486 // already does them.
487 template <class _InIter>
488 void insert(iterator __pos, _InIter __first, _InIter __last) {
489 _M_insert_after_range(__slist_previous(&this->_M_head, __pos._M_node),
494 iterator erase_after(iterator __pos) {
495 return iterator((_Node*) this->_M_erase_after(__pos._M_node));
497 iterator erase_after(iterator __before_first, iterator __last) {
498 return iterator((_Node*) this->_M_erase_after(__before_first._M_node,
502 iterator erase(iterator __pos) {
503 return (_Node*) this->_M_erase_after(__slist_previous(&this->_M_head,
506 iterator erase(iterator __first, iterator __last) {
507 return (_Node*) this->_M_erase_after(
508 __slist_previous(&this->_M_head, __first._M_node), __last._M_node);
511 void resize(size_type new_size, const _Tp& __x);
512 void resize(size_type new_size) { resize(new_size, _Tp()); }
513 void clear() { this->_M_erase_after(&this->_M_head, 0); }
516 // Moves the range [__before_first + 1, __before_last + 1) to *this,
517 // inserting it immediately after __pos. This is constant time.
518 void splice_after(iterator __pos,
519 iterator __before_first, iterator __before_last)
521 if (__before_first != __before_last)
522 __slist_splice_after(__pos._M_node, __before_first._M_node,
523 __before_last._M_node);
526 // Moves the element that follows __prev to *this, inserting it immediately
527 // after __pos. This is constant time.
528 void splice_after(iterator __pos, iterator __prev)
530 __slist_splice_after(__pos._M_node,
531 __prev._M_node, __prev._M_node->_M_next);
535 // Removes all of the elements from the list __x to *this, inserting
536 // them immediately after __pos. __x must not be *this. Complexity:
537 // linear in __x.size().
538 void splice_after(iterator __pos, slist& __x)
540 __slist_splice_after(__pos._M_node, &__x._M_head);
543 // Linear in distance(begin(), __pos), and linear in __x.size().
544 void splice(iterator __pos, slist& __x) {
545 if (__x._M_head._M_next)
546 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
547 &__x._M_head, __slist_previous(&__x._M_head, 0));
550 // Linear in distance(begin(), __pos), and in distance(__x.begin(), __i).
551 void splice(iterator __pos, slist& __x, iterator __i) {
552 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
553 __slist_previous(&__x._M_head, __i._M_node),
557 // Linear in distance(begin(), __pos), in distance(__x.begin(), __first),
558 // and in distance(__first, __last).
559 void splice(iterator __pos, slist& __x, iterator __first, iterator __last)
561 if (__first != __last)
562 __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
563 __slist_previous(&__x._M_head, __first._M_node),
564 __slist_previous(__first._M_node, __last._M_node));
569 if (this->_M_head._M_next)
570 this->_M_head._M_next = __slist_reverse(this->_M_head._M_next);
573 void remove(const _Tp& __val);
575 void merge(slist& __x);
578 template <class _Predicate>
579 void remove_if(_Predicate __pred);
581 template <class _BinaryPredicate>
582 void unique(_BinaryPredicate __pred);
584 template <class _StrictWeakOrdering>
585 void merge(slist&, _StrictWeakOrdering);
587 template <class _StrictWeakOrdering>
588 void sort(_StrictWeakOrdering __comp);
591 template <class _Tp, class _Alloc>
592 slist<_Tp,_Alloc>& slist<_Tp,_Alloc>::operator=(const slist<_Tp,_Alloc>& __x)
595 _Node_base* __p1 = &this->_M_head;
596 _Node* __n1 = (_Node*) this->_M_head._M_next;
597 const _Node* __n2 = (const _Node*) __x._M_head._M_next;
598 while (__n1 && __n2) {
599 __n1->_M_data = __n2->_M_data;
601 __n1 = (_Node*) __n1->_M_next;
602 __n2 = (const _Node*) __n2->_M_next;
605 this->_M_erase_after(__p1, 0);
607 _M_insert_after_range(__p1, const_iterator((_Node*)__n2),
613 template <class _Tp, class _Alloc>
614 void slist<_Tp, _Alloc>::_M_fill_assign(size_type __n, const _Tp& __val) {
615 _Node_base* __prev = &this->_M_head;
616 _Node* __node = (_Node*) this->_M_head._M_next;
617 for ( ; __node != 0 && __n > 0 ; --__n) {
618 __node->_M_data = __val;
620 __node = (_Node*) __node->_M_next;
623 _M_insert_after_fill(__prev, __n, __val);
625 this->_M_erase_after(__prev, 0);
628 template <class _Tp, class _Alloc> template <class _InputIter>
630 slist<_Tp, _Alloc>::_M_assign_dispatch(_InputIter __first, _InputIter __last,
633 _Node_base* __prev = &this->_M_head;
634 _Node* __node = (_Node*) this->_M_head._M_next;
635 while (__node != 0 && __first != __last) {
636 __node->_M_data = *__first;
638 __node = (_Node*) __node->_M_next;
641 if (__first != __last)
642 _M_insert_after_range(__prev, __first, __last);
644 this->_M_erase_after(__prev, 0);
647 template <class _Tp, class _Alloc>
649 operator==(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2)
651 typedef typename slist<_Tp,_Alloc>::const_iterator const_iterator;
652 const_iterator __end1 = _SL1.end();
653 const_iterator __end2 = _SL2.end();
655 const_iterator __i1 = _SL1.begin();
656 const_iterator __i2 = _SL2.begin();
657 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2) {
661 return __i1 == __end1 && __i2 == __end2;
665 template <class _Tp, class _Alloc>
667 operator<(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2)
669 return lexicographical_compare(_SL1.begin(), _SL1.end(),
670 _SL2.begin(), _SL2.end());
673 template <class _Tp, class _Alloc>
675 operator!=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
676 return !(_SL1 == _SL2);
679 template <class _Tp, class _Alloc>
681 operator>(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
685 template <class _Tp, class _Alloc>
687 operator<=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
688 return !(_SL2 < _SL1);
691 template <class _Tp, class _Alloc>
693 operator>=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
694 return !(_SL1 < _SL2);
697 template <class _Tp, class _Alloc>
698 inline void swap(slist<_Tp,_Alloc>& __x, slist<_Tp,_Alloc>& __y) {
703 template <class _Tp, class _Alloc>
704 void slist<_Tp,_Alloc>::resize(size_type __len, const _Tp& __x)
706 _Node_base* __cur = &this->_M_head;
707 while (__cur->_M_next != 0 && __len > 0) {
709 __cur = __cur->_M_next;
712 this->_M_erase_after(__cur, 0);
714 _M_insert_after_fill(__cur, __len, __x);
717 template <class _Tp, class _Alloc>
718 void slist<_Tp,_Alloc>::remove(const _Tp& __val)
720 _Node_base* __cur = &this->_M_head;
721 while (__cur && __cur->_M_next) {
722 if (((_Node*) __cur->_M_next)->_M_data == __val)
723 this->_M_erase_after(__cur);
725 __cur = __cur->_M_next;
729 template <class _Tp, class _Alloc>
730 void slist<_Tp,_Alloc>::unique()
732 _Node_base* __cur = this->_M_head._M_next;
734 while (__cur->_M_next) {
735 if (((_Node*)__cur)->_M_data ==
736 ((_Node*)(__cur->_M_next))->_M_data)
737 this->_M_erase_after(__cur);
739 __cur = __cur->_M_next;
744 template <class _Tp, class _Alloc>
745 void slist<_Tp,_Alloc>::merge(slist<_Tp,_Alloc>& __x)
747 _Node_base* __n1 = &this->_M_head;
748 while (__n1->_M_next && __x._M_head._M_next) {
749 if (((_Node*) __x._M_head._M_next)->_M_data <
750 ((_Node*) __n1->_M_next)->_M_data)
751 __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
752 __n1 = __n1->_M_next;
754 if (__x._M_head._M_next) {
755 __n1->_M_next = __x._M_head._M_next;
756 __x._M_head._M_next = 0;
760 template <class _Tp, class _Alloc>
761 void slist<_Tp,_Alloc>::sort()
763 if (this->_M_head._M_next && this->_M_head._M_next->_M_next) {
768 __slist_splice_after(&__carry._M_head,
769 &this->_M_head, this->_M_head._M_next);
771 while (__i < __fill && !__counter[__i].empty()) {
772 __counter[__i].merge(__carry);
773 __carry.swap(__counter[__i]);
776 __carry.swap(__counter[__i]);
781 for (int __i = 1; __i < __fill; ++__i)
782 __counter[__i].merge(__counter[__i-1]);
783 this->swap(__counter[__fill-1]);
787 template <class _Tp, class _Alloc>
788 template <class _Predicate>
789 void slist<_Tp,_Alloc>::remove_if(_Predicate __pred)
791 _Node_base* __cur = &this->_M_head;
792 while (__cur->_M_next) {
793 if (__pred(((_Node*) __cur->_M_next)->_M_data))
794 this->_M_erase_after(__cur);
796 __cur = __cur->_M_next;
800 template <class _Tp, class _Alloc> template <class _BinaryPredicate>
801 void slist<_Tp,_Alloc>::unique(_BinaryPredicate __pred)
803 _Node* __cur = (_Node*) this->_M_head._M_next;
805 while (__cur->_M_next) {
806 if (__pred(((_Node*)__cur)->_M_data,
807 ((_Node*)(__cur->_M_next))->_M_data))
808 this->_M_erase_after(__cur);
810 __cur = (_Node*) __cur->_M_next;
815 template <class _Tp, class _Alloc> template <class _StrictWeakOrdering>
816 void slist<_Tp,_Alloc>::merge(slist<_Tp,_Alloc>& __x,
817 _StrictWeakOrdering __comp)
819 _Node_base* __n1 = &this->_M_head;
820 while (__n1->_M_next && __x._M_head._M_next) {
821 if (__comp(((_Node*) __x._M_head._M_next)->_M_data,
822 ((_Node*) __n1->_M_next)->_M_data))
823 __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
824 __n1 = __n1->_M_next;
826 if (__x._M_head._M_next) {
827 __n1->_M_next = __x._M_head._M_next;
828 __x._M_head._M_next = 0;
832 template <class _Tp, class _Alloc> template <class _StrictWeakOrdering>
833 void slist<_Tp,_Alloc>::sort(_StrictWeakOrdering __comp)
835 if (this->_M_head._M_next && this->_M_head._M_next->_M_next) {
840 __slist_splice_after(&__carry._M_head,
841 &this->_M_head, this->_M_head._M_next);
843 while (__i < __fill && !__counter[__i].empty()) {
844 __counter[__i].merge(__carry, __comp);
845 __carry.swap(__counter[__i]);
848 __carry.swap(__counter[__i]);
853 for (int __i = 1; __i < __fill; ++__i)
854 __counter[__i].merge(__counter[__i-1], __comp);
855 this->swap(__counter[__fill-1]);
859 // Specialization of insert_iterator so that insertions will be constant
860 // time rather than linear time.
862 template <class _Tp, class _Alloc>
863 class insert_iterator<slist<_Tp, _Alloc> > {
865 typedef slist<_Tp, _Alloc> _Container;
866 _Container* container;
867 typename _Container::iterator iter;
869 typedef _Container container_type;
870 typedef output_iterator_tag iterator_category;
871 typedef void value_type;
872 typedef void difference_type;
873 typedef void pointer;
874 typedef void reference;
876 insert_iterator(_Container& __x, typename _Container::iterator __i)
878 if (__i == __x.begin())
879 iter = __x.before_begin();
881 iter = __x.previous(__i);
884 insert_iterator<_Container>&
885 operator=(const typename _Container::value_type& __value) {
886 iter = container->insert_after(iter, __value);
889 insert_iterator<_Container>& operator*() { return *this; }
890 insert_iterator<_Container>& operator++() { return *this; }
891 insert_iterator<_Container>& operator++(int) { return *this; }
896 #endif /* __SGI_STL_INTERNAL_SLIST_H */