OSDN Git Service

3c70c5cc1d5d87f5133578e95b0cce0c69d3d307
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / ext / slist
1 /*
2  * Copyright (c) 1997
3  * Silicon Graphics Computer Systems, Inc.
4  *
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.
12  *
13  */
14
15 /* NOTE: This is an internal header file, included by other STL headers.
16  *   You should not attempt to use it directly.
17  */
18
19 #ifndef __SGI_STL_INTERNAL_SLIST_H
20 #define __SGI_STL_INTERNAL_SLIST_H
21
22 #include <bits/concept_checks.h>
23
24 namespace std
25
26
27 struct _Slist_node_base
28 {
29   _Slist_node_base* _M_next;
30 };
31
32 inline _Slist_node_base*
33 __slist_make_link(_Slist_node_base* __prev_node,
34                   _Slist_node_base* __new_node)
35 {
36   __new_node->_M_next = __prev_node->_M_next;
37   __prev_node->_M_next = __new_node;
38   return __new_node;
39 }
40
41 inline _Slist_node_base* 
42 __slist_previous(_Slist_node_base* __head,
43                  const _Slist_node_base* __node)
44 {
45   while (__head && __head->_M_next != __node)
46     __head = __head->_M_next;
47   return __head;
48 }
49
50 inline const _Slist_node_base* 
51 __slist_previous(const _Slist_node_base* __head,
52                  const _Slist_node_base* __node)
53 {
54   while (__head && __head->_M_next != __node)
55     __head = __head->_M_next;
56   return __head;
57 }
58
59 inline void __slist_splice_after(_Slist_node_base* __pos,
60                                  _Slist_node_base* __before_first,
61                                  _Slist_node_base* __before_last)
62 {
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;
69   }
70 }
71
72 inline void
73 __slist_splice_after(_Slist_node_base* __pos, _Slist_node_base* __head)
74 {
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;
79     __head->_M_next = 0;
80     __before_last->_M_next = __after;
81   }
82 }
83
84 inline _Slist_node_base* __slist_reverse(_Slist_node_base* __node)
85 {
86   _Slist_node_base* __result = __node;
87   __node = __node->_M_next;
88   __result->_M_next = 0;
89   while(__node) {
90     _Slist_node_base* __next = __node->_M_next;
91     __node->_M_next = __result;
92     __result = __node;
93     __node = __next;
94   }
95   return __result;
96 }
97
98 inline size_t __slist_size(_Slist_node_base* __node)
99 {
100   size_t __result = 0;
101   for ( ; __node != 0; __node = __node->_M_next)
102     ++__result;
103   return __result;
104 }
105
106 template <class _Tp>
107 struct _Slist_node : public _Slist_node_base
108 {
109   _Tp _M_data;
110 };
111
112 struct _Slist_iterator_base
113 {
114   typedef size_t               size_type;
115   typedef ptrdiff_t            difference_type;
116   typedef forward_iterator_tag iterator_category;
117
118   _Slist_node_base* _M_node;
119
120   _Slist_iterator_base(_Slist_node_base* __x) : _M_node(__x) {}
121   void _M_incr() { _M_node = _M_node->_M_next; }
122
123   bool operator==(const _Slist_iterator_base& __x) const {
124     return _M_node == __x._M_node;
125   }
126   bool operator!=(const _Slist_iterator_base& __x) const {
127     return _M_node != __x._M_node;
128   }
129 };
130
131 template <class _Tp, class _Ref, class _Ptr>
132 struct _Slist_iterator : public _Slist_iterator_base
133 {
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;
137
138   typedef _Tp              value_type;
139   typedef _Ptr             pointer;
140   typedef _Ref             reference;
141   typedef _Slist_node<_Tp> _Node;
142
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) {}
146
147   reference operator*() const { return ((_Node*) _M_node)->_M_data; }
148   pointer operator->() const { return &(operator*()); }
149
150   _Self& operator++()
151   {
152     _M_incr();
153     return *this;
154   }
155   _Self operator++(int)
156   {
157     _Self __tmp = *this;
158     _M_incr();
159     return __tmp;
160   }
161 };
162
163
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.
170
171 // Base for general standard-conforming allocators.
172 template <class _Tp, class _Allocator, bool _IsStatic>
173 class _Slist_alloc_base {
174 public:
175   typedef typename _Alloc_traits<_Tp,_Allocator>::allocator_type
176           allocator_type;
177   allocator_type get_allocator() const { return _M_node_allocator; }
178
179   _Slist_alloc_base(const allocator_type& __a) : _M_node_allocator(__a) {}
180
181 protected:
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); }
186
187 protected:
188   typename _Alloc_traits<_Slist_node<_Tp>,_Allocator>::allocator_type
189            _M_node_allocator;
190   _Slist_node_base _M_head;
191 };
192
193 // Specialization for instanceless allocators.
194 template <class _Tp, class _Allocator>
195 class _Slist_alloc_base<_Tp,_Allocator, true> {
196 public:
197   typedef typename _Alloc_traits<_Tp,_Allocator>::allocator_type
198           allocator_type;
199   allocator_type get_allocator() const { return allocator_type(); }
200
201   _Slist_alloc_base(const allocator_type&) {}
202
203 protected:
204   typedef typename _Alloc_traits<_Slist_node<_Tp>, _Allocator>::_Alloc_type
205           _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); }
208
209 protected:
210   _Slist_node_base _M_head;
211 };
212
213
214 template <class _Tp, class _Alloc>
215 struct _Slist_base
216   : public _Slist_alloc_base<_Tp, _Alloc,
217                              _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
218 {
219   typedef _Slist_alloc_base<_Tp, _Alloc,
220                             _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
221           _Base;
222   typedef typename _Base::allocator_type allocator_type;
223
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); }
227
228 protected:
229
230   _Slist_node_base* _M_erase_after(_Slist_node_base* __pos)
231   {
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);
236     _M_put_node(__next);
237     return __next_next;
238   }
239   _Slist_node_base* _M_erase_after(_Slist_node_base*, _Slist_node_base*);
240 };
241
242 template <class _Tp, class _Alloc> 
243 _Slist_node_base*
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);
251     _M_put_node(__tmp);
252   }
253   __before_first->_M_next = __last_node;
254   return __last_node;
255 }
256
257 template <class _Tp, class _Alloc = allocator<_Tp> >
258 class slist : private _Slist_base<_Tp,_Alloc>
259 {
260   // requirements:
261
262   __STL_CLASS_REQUIRES(_Tp, _Assignable);
263
264 private:
265   typedef _Slist_base<_Tp,_Alloc> _Base;
266 public:
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;
274
275   typedef _Slist_iterator<_Tp, _Tp&, _Tp*>             iterator;
276   typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
277
278   typedef typename _Base::allocator_type allocator_type;
279   allocator_type get_allocator() const { return _Base::get_allocator(); }
280
281 private:
282   typedef _Slist_node<_Tp>      _Node;
283   typedef _Slist_node_base      _Node_base;
284   typedef _Slist_iterator_base  _Iterator_base;
285
286   _Node* _M_create_node(const value_type& __x) {
287     _Node* __node = this->_M_get_node();
288     __STL_TRY {
289       construct(&__node->_M_data, __x);
290       __node->_M_next = 0;
291     }
292     __STL_UNWIND(this->_M_put_node(__node));
293     return __node;
294   }
295   
296   _Node* _M_create_node() {
297     _Node* __node = this->_M_get_node();
298     __STL_TRY {
299       construct(&__node->_M_data);
300       __node->_M_next = 0;
301     }
302     __STL_UNWIND(this->_M_put_node(__node));
303     return __node;
304   }
305
306 public:
307   explicit slist(const allocator_type& __a = allocator_type()) : _Base(__a) {}
308
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); }
312
313   explicit slist(size_type __n) : _Base(allocator_type())
314     { _M_insert_after_fill(&this->_M_head, __n, value_type()); }
315
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); }
322
323   slist(const slist& __x) : _Base(__x.get_allocator())
324     { _M_insert_after_range(&this->_M_head, __x.begin(), __x.end()); }
325
326   slist& operator= (const slist& __x);
327
328   ~slist() {}
329
330 public:
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.
335
336   void assign(size_type __n, const _Tp& __val)
337     { _M_fill_assign(__n, __val); }
338
339   void _M_fill_assign(size_type __n, const _Tp& __val);
340
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());
345   }
346
347   template <class _Integer>
348   void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
349     { _M_fill_assign((size_type) __n, (_Tp) __val); }
350
351   template <class _InputIterator>
352   void _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
353                           __false_type);
354
355 public:
356
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);}
360
361   iterator end() { return iterator(0); }
362   const_iterator end() const { return const_iterator(0); }
363
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
370   // obtain end().
371   iterator before_begin() { return iterator((_Node*) &this->_M_head); }
372   const_iterator before_begin() const
373     { return const_iterator((_Node*) &this->_M_head); }
374
375   size_type size() const { return __slist_size(this->_M_head._M_next); }
376   
377   size_type max_size() const { return size_type(-1); }
378
379   bool empty() const { return this->_M_head._M_next == 0; }
380
381   void swap(slist& __x)
382     { std::swap(this->_M_head._M_next, __x._M_head._M_next); }
383
384 public:
385
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));
391   }
392   void push_front() { __slist_make_link(&this->_M_head, _M_create_node()); }
393   void pop_front() {
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);
398   }
399
400   iterator previous(const_iterator __pos) {
401     return iterator((_Node*) __slist_previous(&this->_M_head, __pos._M_node));
402   }
403   const_iterator previous(const_iterator __pos) const {
404     return const_iterator((_Node*) __slist_previous(&this->_M_head,
405                                                     __pos._M_node));
406   }
407
408 private:
409   _Node* _M_insert_after(_Node_base* __pos, const value_type& __x) {
410     return (_Node*) (__slist_make_link(__pos, _M_create_node(__x)));
411   }
412
413   _Node* _M_insert_after(_Node_base* __pos) {
414     return (_Node*) (__slist_make_link(__pos, _M_create_node()));
415   }
416
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));
421   }
422
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());
429   }
430
431   template <class _Integer>
432   void _M_insert_after_range(_Node_base* __pos, _Integer __n, _Integer __x,
433                              __true_type) {
434     _M_insert_after_fill(__pos, __n, __x);
435   }
436
437   template <class _InIter>
438   void _M_insert_after_range(_Node_base* __pos,
439                              _InIter __first, _InIter __last,
440                              __false_type) {
441     while (__first != __last) {
442       __pos = __slist_make_link(__pos, _M_create_node(*__first));
443       ++__first;
444     }
445   }
446
447 public:
448
449   iterator insert_after(iterator __pos, const value_type& __x) {
450     return iterator(_M_insert_after(__pos._M_node, __x));
451   }
452
453   iterator insert_after(iterator __pos) {
454     return insert_after(__pos, value_type());
455   }
456
457   void insert_after(iterator __pos, size_type __n, const value_type& __x) {
458     _M_insert_after_fill(__pos._M_node, __n, __x);
459   }
460
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);
466   }
467
468   iterator insert(iterator __pos, const value_type& __x) {
469     return iterator(_M_insert_after(__slist_previous(&this->_M_head,
470                                                      __pos._M_node),
471                     __x));
472   }
473
474   iterator insert(iterator __pos) {
475     return iterator(_M_insert_after(__slist_previous(&this->_M_head,
476                                                      __pos._M_node),
477                                     value_type()));
478   }
479
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),
482                          __n, __x);
483   } 
484     
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), 
490                           __first, __last);
491   }
492
493 public:
494   iterator erase_after(iterator __pos) {
495     return iterator((_Node*) this->_M_erase_after(__pos._M_node));
496   }
497   iterator erase_after(iterator __before_first, iterator __last) {
498     return iterator((_Node*) this->_M_erase_after(__before_first._M_node, 
499                                                   __last._M_node));
500   } 
501
502   iterator erase(iterator __pos) {
503     return (_Node*) this->_M_erase_after(__slist_previous(&this->_M_head, 
504                                                           __pos._M_node));
505   }
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);
509   }
510
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); }
514
515 public:
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)
520   {
521     if (__before_first != __before_last) 
522       __slist_splice_after(__pos._M_node, __before_first._M_node, 
523                            __before_last._M_node);
524   }
525
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)
529   {
530     __slist_splice_after(__pos._M_node,
531                          __prev._M_node, __prev._M_node->_M_next);
532   }
533
534
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)
539   {
540     __slist_splice_after(__pos._M_node, &__x._M_head);
541   }
542
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));
548   }
549
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),
554                          __i._M_node);
555   }
556
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)
560   {
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));
565   }
566
567 public:
568   void reverse() { 
569     if (this->_M_head._M_next)
570       this->_M_head._M_next = __slist_reverse(this->_M_head._M_next);
571   }
572
573   void remove(const _Tp& __val); 
574   void unique(); 
575   void merge(slist& __x);
576   void sort();     
577
578   template <class _Predicate> 
579   void remove_if(_Predicate __pred);
580
581   template <class _BinaryPredicate> 
582   void unique(_BinaryPredicate __pred); 
583
584   template <class _StrictWeakOrdering> 
585   void merge(slist&, _StrictWeakOrdering);
586
587   template <class _StrictWeakOrdering> 
588   void sort(_StrictWeakOrdering __comp); 
589 };
590
591 template <class _Tp, class _Alloc>
592 slist<_Tp,_Alloc>& slist<_Tp,_Alloc>::operator=(const slist<_Tp,_Alloc>& __x)
593 {
594   if (&__x != this) {
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;
600       __p1 = __n1;
601       __n1 = (_Node*) __n1->_M_next;
602       __n2 = (const _Node*) __n2->_M_next;
603     }
604     if (__n2 == 0)
605       this->_M_erase_after(__p1, 0);
606     else
607       _M_insert_after_range(__p1, const_iterator((_Node*)__n2), 
608                                   const_iterator(0));
609   }
610   return *this;
611 }
612
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;
619     __prev = __node;
620     __node = (_Node*) __node->_M_next;
621   }
622   if (__n > 0)
623     _M_insert_after_fill(__prev, __n, __val);
624   else
625     this->_M_erase_after(__prev, 0);
626 }
627
628 template <class _Tp, class _Alloc> template <class _InputIter>
629 void
630 slist<_Tp, _Alloc>::_M_assign_dispatch(_InputIter __first, _InputIter __last,
631                                        __false_type)
632 {
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;
637     __prev = __node;
638     __node = (_Node*) __node->_M_next;
639     ++__first;
640   }
641   if (__first != __last)
642     _M_insert_after_range(__prev, __first, __last);
643   else
644     this->_M_erase_after(__prev, 0);
645 }
646
647 template <class _Tp, class _Alloc>
648 inline bool 
649 operator==(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2)
650 {
651   typedef typename slist<_Tp,_Alloc>::const_iterator const_iterator;
652   const_iterator __end1 = _SL1.end();
653   const_iterator __end2 = _SL2.end();
654
655   const_iterator __i1 = _SL1.begin();
656   const_iterator __i2 = _SL2.begin();
657   while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2) {
658     ++__i1;
659     ++__i2;
660   }
661   return __i1 == __end1 && __i2 == __end2;
662 }
663
664
665 template <class _Tp, class _Alloc>
666 inline bool
667 operator<(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2)
668 {
669   return lexicographical_compare(_SL1.begin(), _SL1.end(), 
670                                  _SL2.begin(), _SL2.end());
671 }
672
673 template <class _Tp, class _Alloc>
674 inline bool 
675 operator!=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
676   return !(_SL1 == _SL2);
677 }
678
679 template <class _Tp, class _Alloc>
680 inline bool 
681 operator>(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
682   return _SL2 < _SL1;
683 }
684
685 template <class _Tp, class _Alloc>
686 inline bool 
687 operator<=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
688   return !(_SL2 < _SL1);
689 }
690
691 template <class _Tp, class _Alloc>
692 inline bool 
693 operator>=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
694   return !(_SL1 < _SL2);
695 }
696
697 template <class _Tp, class _Alloc>
698 inline void swap(slist<_Tp,_Alloc>& __x, slist<_Tp,_Alloc>& __y) {
699   __x.swap(__y);
700 }
701
702
703 template <class _Tp, class _Alloc>
704 void slist<_Tp,_Alloc>::resize(size_type __len, const _Tp& __x)
705 {
706   _Node_base* __cur = &this->_M_head;
707   while (__cur->_M_next != 0 && __len > 0) {
708     --__len;
709     __cur = __cur->_M_next;
710   }
711   if (__cur->_M_next) 
712     this->_M_erase_after(__cur, 0);
713   else
714     _M_insert_after_fill(__cur, __len, __x);
715 }
716
717 template <class _Tp, class _Alloc>
718 void slist<_Tp,_Alloc>::remove(const _Tp& __val)
719 {
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);
724     else
725       __cur = __cur->_M_next;
726   }
727 }
728
729 template <class _Tp, class _Alloc> 
730 void slist<_Tp,_Alloc>::unique()
731 {
732   _Node_base* __cur = this->_M_head._M_next;
733   if (__cur) {
734     while (__cur->_M_next) {
735       if (((_Node*)__cur)->_M_data == 
736           ((_Node*)(__cur->_M_next))->_M_data)
737         this->_M_erase_after(__cur);
738       else
739         __cur = __cur->_M_next;
740     }
741   }
742 }
743
744 template <class _Tp, class _Alloc>
745 void slist<_Tp,_Alloc>::merge(slist<_Tp,_Alloc>& __x)
746 {
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;
753   }
754   if (__x._M_head._M_next) {
755     __n1->_M_next = __x._M_head._M_next;
756     __x._M_head._M_next = 0;
757   }
758 }
759
760 template <class _Tp, class _Alloc>
761 void slist<_Tp,_Alloc>::sort()
762 {
763   if (this->_M_head._M_next && this->_M_head._M_next->_M_next) {
764     slist __carry;
765     slist __counter[64];
766     int __fill = 0;
767     while (!empty()) {
768       __slist_splice_after(&__carry._M_head,
769                            &this->_M_head, this->_M_head._M_next);
770       int __i = 0;
771       while (__i < __fill && !__counter[__i].empty()) {
772         __counter[__i].merge(__carry);
773         __carry.swap(__counter[__i]);
774         ++__i;
775       }
776       __carry.swap(__counter[__i]);
777       if (__i == __fill)
778         ++__fill;
779     }
780
781     for (int __i = 1; __i < __fill; ++__i)
782       __counter[__i].merge(__counter[__i-1]);
783     this->swap(__counter[__fill-1]);
784   }
785 }
786
787 template <class _Tp, class _Alloc> 
788 template <class _Predicate>
789 void slist<_Tp,_Alloc>::remove_if(_Predicate __pred)
790 {
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);
795     else
796       __cur = __cur->_M_next;
797   }
798 }
799
800 template <class _Tp, class _Alloc> template <class _BinaryPredicate> 
801 void slist<_Tp,_Alloc>::unique(_BinaryPredicate __pred)
802 {
803   _Node* __cur = (_Node*) this->_M_head._M_next;
804   if (__cur) {
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);
809       else
810         __cur = (_Node*) __cur->_M_next;
811     }
812   }
813 }
814
815 template <class _Tp, class _Alloc> template <class _StrictWeakOrdering>
816 void slist<_Tp,_Alloc>::merge(slist<_Tp,_Alloc>& __x,
817                               _StrictWeakOrdering __comp)
818 {
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;
825   }
826   if (__x._M_head._M_next) {
827     __n1->_M_next = __x._M_head._M_next;
828     __x._M_head._M_next = 0;
829   }
830 }
831
832 template <class _Tp, class _Alloc> template <class _StrictWeakOrdering> 
833 void slist<_Tp,_Alloc>::sort(_StrictWeakOrdering __comp)
834 {
835   if (this->_M_head._M_next && this->_M_head._M_next->_M_next) {
836     slist __carry;
837     slist __counter[64];
838     int __fill = 0;
839     while (!empty()) {
840       __slist_splice_after(&__carry._M_head,
841                            &this->_M_head, this->_M_head._M_next);
842       int __i = 0;
843       while (__i < __fill && !__counter[__i].empty()) {
844         __counter[__i].merge(__carry, __comp);
845         __carry.swap(__counter[__i]);
846         ++__i;
847       }
848       __carry.swap(__counter[__i]);
849       if (__i == __fill)
850         ++__fill;
851     }
852
853     for (int __i = 1; __i < __fill; ++__i)
854       __counter[__i].merge(__counter[__i-1], __comp);
855     this->swap(__counter[__fill-1]);
856   }
857 }
858
859 // Specialization of insert_iterator so that insertions will be constant
860 // time rather than linear time.
861
862 template <class _Tp, class _Alloc>
863 class insert_iterator<slist<_Tp, _Alloc> > {
864 protected:
865   typedef slist<_Tp, _Alloc> _Container;
866   _Container* container;
867   typename _Container::iterator iter;
868 public:
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;
875
876   insert_iterator(_Container& __x, typename _Container::iterator __i) 
877     : container(&__x) {
878     if (__i == __x.begin())
879       iter = __x.before_begin();
880     else
881       iter = __x.previous(__i);
882   }
883
884   insert_iterator<_Container>&
885   operator=(const typename _Container::value_type& __value) { 
886     iter = container->insert_after(iter, __value);
887     return *this;
888   }
889   insert_iterator<_Container>& operator*() { return *this; }
890   insert_iterator<_Container>& operator++() { return *this; }
891   insert_iterator<_Container>& operator++(int) { return *this; }
892 };
893
894 } // namespace std 
895
896 #endif /* __SGI_STL_INTERNAL_SLIST_H */
897
898 // Local Variables:
899 // mode:C++
900 // End: