stl_slist.h

00001 /*
00002  * Copyright (c) 1997
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Permission to use, copy, modify, distribute and sell this software
00006  * and its documentation for any purpose is hereby granted without fee,
00007  * provided that the above copyright notice appear in all copies and
00008  * that both that copyright notice and this permission notice appear
00009  * in supporting documentation.  Silicon Graphics makes no
00010  * representations about the suitability of this software for any
00011  * purpose.  It is provided "as is" without express or implied warranty.
00012  *
00013  */
00014 
00015 /* NOTE: This is an internal header file, included by other STL headers.
00016  *   You should not attempt to use it directly.
00017  */
00018 
00019 #ifndef __SGI_STL_INTERNAL_SLIST_H
00020 #define __SGI_STL_INTERNAL_SLIST_H
00021 
00022 #include <concept_checks.h>
00023 
00024 __STL_BEGIN_NAMESPACE 
00025 
00026 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
00027 #pragma set woff 1174
00028 #pragma set woff 1375
00029 #endif
00030 
00031 struct _Slist_node_base
00032 {
00033   _Slist_node_base* _M_next;
00034 };
00035 
00036 inline _Slist_node_base*
00037 __slist_make_link(_Slist_node_base* __prev_node,
00038                   _Slist_node_base* __new_node)
00039 {
00040   __new_node->_M_next = __prev_node->_M_next;
00041   __prev_node->_M_next = __new_node;
00042   return __new_node;
00043 }
00044 
00045 inline _Slist_node_base* 
00046 __slist_previous(_Slist_node_base* __head,
00047                  const _Slist_node_base* __node)
00048 {
00049   while (__head && __head->_M_next != __node)
00050     __head = __head->_M_next;
00051   return __head;
00052 }
00053 
00054 inline const _Slist_node_base* 
00055 __slist_previous(const _Slist_node_base* __head,
00056                  const _Slist_node_base* __node)
00057 {
00058   while (__head && __head->_M_next != __node)
00059     __head = __head->_M_next;
00060   return __head;
00061 }
00062 
00063 inline void __slist_splice_after(_Slist_node_base* __pos,
00064                                  _Slist_node_base* __before_first,
00065                                  _Slist_node_base* __before_last)
00066 {
00067   if (__pos != __before_first && __pos != __before_last) {
00068     _Slist_node_base* __first = __before_first->_M_next;
00069     _Slist_node_base* __after = __pos->_M_next;
00070     __before_first->_M_next = __before_last->_M_next;
00071     __pos->_M_next = __first;
00072     __before_last->_M_next = __after;
00073   }
00074 }
00075 
00076 inline void
00077 __slist_splice_after(_Slist_node_base* __pos, _Slist_node_base* __head)
00078 {
00079   _Slist_node_base* __before_last = __slist_previous(__head, 0);
00080   if (__before_last != __head) {
00081     _Slist_node_base* __after = __pos->_M_next;
00082     __pos->_M_next = __head->_M_next;
00083     __head->_M_next = 0;
00084     __before_last->_M_next = __after;
00085   }
00086 }
00087 
00088 inline _Slist_node_base* __slist_reverse(_Slist_node_base* __node)
00089 {
00090   _Slist_node_base* __result = __node;
00091   __node = __node->_M_next;
00092   __result->_M_next = 0;
00093   while(__node) {
00094     _Slist_node_base* __next = __node->_M_next;
00095     __node->_M_next = __result;
00096     __result = __node;
00097     __node = __next;
00098   }
00099   return __result;
00100 }
00101 
00102 inline size_t __slist_size(_Slist_node_base* __node)
00103 {
00104   size_t __result = 0;
00105   for ( ; __node != 0; __node = __node->_M_next)
00106     ++__result;
00107   return __result;
00108 }
00109 
00110 template <class _Tp>
00111 struct _Slist_node : public _Slist_node_base
00112 {
00113   _Tp _M_data;
00114 };
00115 
00116 struct _Slist_iterator_base
00117 {
00118   typedef size_t               size_type;
00119   typedef ptrdiff_t            difference_type;
00120   typedef forward_iterator_tag iterator_category;
00121 
00122   _Slist_node_base* _M_node;
00123 
00124   _Slist_iterator_base(_Slist_node_base* __x) : _M_node(__x) {}
00125   void _M_incr() { _M_node = _M_node->_M_next; }
00126 
00127   bool operator==(const _Slist_iterator_base& __x) const {
00128     return _M_node == __x._M_node;
00129   }
00130   bool operator!=(const _Slist_iterator_base& __x) const {
00131     return _M_node != __x._M_node;
00132   }
00133 };
00134 
00135 template <class _Tp, class _Ref, class _Ptr>
00136 struct _Slist_iterator : public _Slist_iterator_base
00137 {
00138   typedef _Slist_iterator<_Tp, _Tp&, _Tp*>             iterator;
00139   typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
00140   typedef _Slist_iterator<_Tp, _Ref, _Ptr>             _Self;
00141 
00142   typedef _Tp              value_type;
00143   typedef _Ptr             pointer;
00144   typedef _Ref             reference;
00145   typedef _Slist_node<_Tp> _Node;
00146 
00147   _Slist_iterator(_Node* __x) : _Slist_iterator_base(__x) {}
00148   _Slist_iterator() : _Slist_iterator_base(0) {}
00149   _Slist_iterator(const iterator& __x) : _Slist_iterator_base(__x._M_node) {}
00150 
00151   reference operator*() const { return ((_Node*) _M_node)->_M_data; }
00152 #ifndef __SGI_STL_NO_ARROW_OPERATOR
00153   pointer operator->() const { return &(operator*()); }
00154 #endif /* __SGI_STL_NO_ARROW_OPERATOR */
00155 
00156   _Self& operator++()
00157   {
00158     _M_incr();
00159     return *this;
00160   }
00161   _Self operator++(int)
00162   {
00163     _Self __tmp = *this;
00164     _M_incr();
00165     return __tmp;
00166   }
00167 };
00168 
00169 #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
00170 
00171 inline ptrdiff_t* distance_type(const _Slist_iterator_base&) {
00172   return 0;
00173 }
00174 
00175 inline forward_iterator_tag iterator_category(const _Slist_iterator_base&) {
00176   return forward_iterator_tag();
00177 }
00178 
00179 template <class _Tp, class _Ref, class _Ptr> 
00180 inline _Tp* value_type(const _Slist_iterator<_Tp, _Ref, _Ptr>&) {
00181   return 0;
00182 }
00183 
00184 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
00185 
00186 // Base class that encapsulates details of allocators.  Three cases:
00187 // an ordinary standard-conforming allocator, a standard-conforming
00188 // allocator with no non-static data, and an SGI-style allocator.
00189 // This complexity is necessary only because we're worrying about backward
00190 // compatibility and because we want to avoid wasting storage on an 
00191 // allocator instance if it isn't necessary.
00192 
00193 #ifdef __STL_USE_STD_ALLOCATORS
00194 
00195 // Base for general standard-conforming allocators.
00196 template <class _Tp, class _Allocator, bool _IsStatic>
00197 class _Slist_alloc_base {
00198 public:
00199   typedef typename _Alloc_traits<_Tp,_Allocator>::allocator_type
00200           allocator_type;
00201   allocator_type get_allocator() const { return _M_node_allocator; }
00202 
00203   _Slist_alloc_base(const allocator_type& __a) : _M_node_allocator(__a) {}
00204 
00205 protected:
00206   _Slist_node<_Tp>* _M_get_node() 
00207     { return _M_node_allocator.allocate(1); }
00208   void _M_put_node(_Slist_node<_Tp>* __p) 
00209     { _M_node_allocator.deallocate(__p, 1); }
00210 
00211 protected:
00212   typename _Alloc_traits<_Slist_node<_Tp>,_Allocator>::allocator_type
00213            _M_node_allocator;
00214   _Slist_node_base _M_head;
00215 };
00216 
00217 // Specialization for instanceless allocators.
00218 template <class _Tp, class _Allocator>
00219 class _Slist_alloc_base<_Tp,_Allocator, true> {
00220 public:
00221   typedef typename _Alloc_traits<_Tp,_Allocator>::allocator_type
00222           allocator_type;
00223   allocator_type get_allocator() const { return allocator_type(); }
00224 
00225   _Slist_alloc_base(const allocator_type&) {}
00226 
00227 protected:
00228   typedef typename _Alloc_traits<_Slist_node<_Tp>, _Allocator>::_Alloc_type
00229           _Alloc_type;
00230   _Slist_node<_Tp>* _M_get_node() { return _Alloc_type::allocate(1); }
00231   void _M_put_node(_Slist_node<_Tp>* __p) { _Alloc_type::deallocate(__p, 1); }
00232 
00233 protected:
00234   _Slist_node_base _M_head;
00235 };
00236 
00237 
00238 template <class _Tp, class _Alloc>
00239 struct _Slist_base
00240   : public _Slist_alloc_base<_Tp, _Alloc,
00241                              _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
00242 {
00243   typedef _Slist_alloc_base<_Tp, _Alloc,
00244                             _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
00245           _Base;
00246   typedef typename _Base::allocator_type allocator_type;
00247 
00248   _Slist_base(const allocator_type& __a)
00249     : _Base(__a) { this->_M_head._M_next = 0; }
00250   ~_Slist_base() { _M_erase_after(&this->_M_head, 0); }
00251 
00252 protected:
00253 
00254   _Slist_node_base* _M_erase_after(_Slist_node_base* __pos)
00255   {
00256     _Slist_node<_Tp>* __next = (_Slist_node<_Tp>*) (__pos->_M_next);
00257     _Slist_node_base* __next_next = __next->_M_next;
00258     __pos->_M_next = __next_next;
00259     destroy(&__next->_M_data);
00260     _M_put_node(__next);
00261     return __next_next;
00262   }
00263   _Slist_node_base* _M_erase_after(_Slist_node_base*, _Slist_node_base*);
00264 };
00265 
00266 #else /* __STL_USE_STD_ALLOCATORS */
00267 
00268 template <class _Tp, class _Alloc> 
00269 struct _Slist_base {
00270   typedef _Alloc allocator_type;
00271   allocator_type get_allocator() const { return allocator_type(); }
00272 
00273   _Slist_base(const allocator_type&) { _M_head._M_next = 0; }
00274   ~_Slist_base() { _M_erase_after(&_M_head, 0); }
00275 
00276 protected:
00277   typedef simple_alloc<_Slist_node<_Tp>, _Alloc> _Alloc_type;
00278   _Slist_node<_Tp>* _M_get_node() { return _Alloc_type::allocate(1); }
00279   void _M_put_node(_Slist_node<_Tp>* __p) { _Alloc_type::deallocate(__p, 1); }
00280 
00281   _Slist_node_base* _M_erase_after(_Slist_node_base* __pos)
00282   {
00283     _Slist_node<_Tp>* __next = (_Slist_node<_Tp>*) (__pos->_M_next);
00284     _Slist_node_base* __next_next = __next->_M_next;
00285     __pos->_M_next = __next_next;
00286     destroy(&__next->_M_data);
00287     _M_put_node(__next);
00288     return __next_next;
00289   }
00290   _Slist_node_base* _M_erase_after(_Slist_node_base*, _Slist_node_base*);
00291 
00292 protected:
00293   _Slist_node_base _M_head;
00294 };  
00295 
00296 #endif /* __STL_USE_STD_ALLOCATORS */
00297 
00298 template <class _Tp, class _Alloc> 
00299 _Slist_node_base*
00300 _Slist_base<_Tp,_Alloc>::_M_erase_after(_Slist_node_base* __before_first,
00301                                         _Slist_node_base* __last_node) {
00302   _Slist_node<_Tp>* __cur = (_Slist_node<_Tp>*) (__before_first->_M_next);
00303   while (__cur != __last_node) {
00304     _Slist_node<_Tp>* __tmp = __cur;
00305     __cur = (_Slist_node<_Tp>*) __cur->_M_next;
00306     destroy(&__tmp->_M_data);
00307     _M_put_node(__tmp);
00308   }
00309   __before_first->_M_next = __last_node;
00310   return __last_node;
00311 }
00312 
00313 template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >
00314 class slist : private _Slist_base<_Tp,_Alloc>
00315 {
00316   // requirements:
00317 
00318   __STL_CLASS_REQUIRES(_Tp, _Assignable);
00319 
00320 private:
00321   typedef _Slist_base<_Tp,_Alloc> _Base;
00322 public:
00323   typedef _Tp                value_type;
00324   typedef value_type*       pointer;
00325   typedef const value_type* const_pointer;
00326   typedef value_type&       reference;
00327   typedef const value_type& const_reference;
00328   typedef size_t            size_type;
00329   typedef ptrdiff_t         difference_type;
00330 
00331   typedef _Slist_iterator<_Tp, _Tp&, _Tp*>             iterator;
00332   typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
00333 
00334   typedef typename _Base::allocator_type allocator_type;
00335   allocator_type get_allocator() const { return _Base::get_allocator(); }
00336 
00337 private:
00338   typedef _Slist_node<_Tp>      _Node;
00339   typedef _Slist_node_base      _Node_base;
00340   typedef _Slist_iterator_base  _Iterator_base;
00341 
00342   _Node* _M_create_node(const value_type& __x) {
00343     _Node* __node = this->_M_get_node();
00344     __STL_TRY {
00345       construct(&__node->_M_data, __x);
00346       __node->_M_next = 0;
00347     }
00348     __STL_UNWIND(this->_M_put_node(__node));
00349     return __node;
00350   }
00351   
00352   _Node* _M_create_node() {
00353     _Node* __node = this->_M_get_node();
00354     __STL_TRY {
00355       construct(&__node->_M_data);
00356       __node->_M_next = 0;
00357     }
00358     __STL_UNWIND(this->_M_put_node(__node));
00359     return __node;
00360   }
00361 
00362 public:
00363   explicit slist(const allocator_type& __a = allocator_type()) : _Base(__a) {}
00364 
00365   slist(size_type __n, const value_type& __x,
00366         const allocator_type& __a =  allocator_type()) : _Base(__a)
00367     { _M_insert_after_fill(&this->_M_head, __n, __x); }
00368 
00369   explicit slist(size_type __n) : _Base(allocator_type())
00370     { _M_insert_after_fill(&this->_M_head, __n, value_type()); }
00371 
00372 #ifdef __STL_MEMBER_TEMPLATES
00373   // We don't need any dispatching tricks here, because _M_insert_after_range
00374   // already does them.
00375   template <class _InputIterator>
00376   slist(_InputIterator __first, _InputIterator __last,
00377         const allocator_type& __a =  allocator_type()) : _Base(__a)
00378     { _M_insert_after_range(&this->_M_head, __first, __last); }
00379 
00380 #else /* __STL_MEMBER_TEMPLATES */
00381   slist(const_iterator __first, const_iterator __last,
00382         const allocator_type& __a =  allocator_type()) : _Base(__a)
00383     { _M_insert_after_range(&this->_M_head, __first, __last); }
00384   slist(const value_type* __first, const value_type* __last,
00385         const allocator_type& __a =  allocator_type()) : _Base(__a)
00386     { _M_insert_after_range(&this->_M_head, __first, __last); }
00387 #endif /* __STL_MEMBER_TEMPLATES */
00388 
00389   slist(const slist& __x) : _Base(__x.get_allocator())
00390     { _M_insert_after_range(&this->_M_head, __x.begin(), __x.end()); }
00391 
00392   slist& operator= (const slist& __x);
00393 
00394   ~slist() {}
00395 
00396 public:
00397   // assign(), a generalized assignment member function.  Two
00398   // versions: one that takes a count, and one that takes a range.
00399   // The range version is a member template, so we dispatch on whether
00400   // or not the type is an integer.
00401 
00402   void assign(size_type __n, const _Tp& __val)
00403     { _M_fill_assign(__n, __val); }
00404 
00405   void _M_fill_assign(size_type __n, const _Tp& __val);
00406 
00407 
00408 #ifdef __STL_MEMBER_TEMPLATES
00409 
00410   template <class _InputIterator>
00411   void assign(_InputIterator __first, _InputIterator __last) {
00412     typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
00413     _M_assign_dispatch(__first, __last, _Integral());
00414   }
00415 
00416   template <class _Integer>
00417   void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
00418     { _M_fill_assign((size_type) __n, (_Tp) __val); }
00419 
00420   template <class _InputIterator>
00421   void _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
00422                           __false_type);
00423 
00424 #endif /* __STL_MEMBER_TEMPLATES */
00425 
00426 public:
00427 
00428   iterator begin() { return iterator((_Node*)this->_M_head._M_next); }
00429   const_iterator begin() const 
00430     { return const_iterator((_Node*)this->_M_head._M_next);}
00431 
00432   iterator end() { return iterator(0); }
00433   const_iterator end() const { return const_iterator(0); }
00434 
00435   // Experimental new feature: before_begin() returns a
00436   // non-dereferenceable iterator that, when incremented, yields
00437   // begin().  This iterator may be used as the argument to
00438   // insert_after, erase_after, etc.  Note that even for an empty 
00439   // slist, before_begin() is not the same iterator as end().  It 
00440   // is always necessary to increment before_begin() at least once to
00441   // obtain end().
00442   iterator before_begin() { return iterator((_Node*) &this->_M_head); }
00443   const_iterator before_begin() const
00444     { return const_iterator((_Node*) &this->_M_head); }
00445 
00446   size_type size() const { return __slist_size(this->_M_head._M_next); }
00447   
00448   size_type max_size() const { return size_type(-1); }
00449 
00450   bool empty() const { return this->_M_head._M_next == 0; }
00451 
00452   void swap(slist& __x)
00453     { __STD::swap(this->_M_head._M_next, __x._M_head._M_next); }
00454 
00455 public:
00456 
00457   reference front() { return ((_Node*) this->_M_head._M_next)->_M_data; }
00458   const_reference front() const 
00459     { return ((_Node*) this->_M_head._M_next)->_M_data; }
00460   void push_front(const value_type& __x)   {
00461     __slist_make_link(&this->_M_head, _M_create_node(__x));
00462   }
00463   void push_front() { __slist_make_link(&this->_M_head, _M_create_node()); }
00464   void pop_front() {
00465     _Node* __node = (_Node*) this->_M_head._M_next;
00466     this->_M_head._M_next = __node->_M_next;
00467     destroy(&__node->_M_data);
00468     this->_M_put_node(__node);
00469   }
00470 
00471   iterator previous(const_iterator __pos) {
00472     return iterator((_Node*) __slist_previous(&this->_M_head, __pos._M_node));
00473   }
00474   const_iterator previous(const_iterator __pos) const {
00475     return const_iterator((_Node*) __slist_previous(&this->_M_head,
00476                                                     __pos._M_node));
00477   }
00478 
00479 private:
00480   _Node* _M_insert_after(_Node_base* __pos, const value_type& __x) {
00481     return (_Node*) (__slist_make_link(__pos, _M_create_node(__x)));
00482   }
00483 
00484   _Node* _M_insert_after(_Node_base* __pos) {
00485     return (_Node*) (__slist_make_link(__pos, _M_create_node()));
00486   }
00487 
00488   void _M_insert_after_fill(_Node_base* __pos,
00489                             size_type __n, const value_type& __x) {
00490     for (size_type __i = 0; __i < __n; ++__i)
00491       __pos = __slist_make_link(__pos, _M_create_node(__x));
00492   }
00493 
00494 #ifdef __STL_MEMBER_TEMPLATES
00495 
00496   // Check whether it's an integral type.  If so, it's not an iterator.
00497   template <class _InIter>
00498   void _M_insert_after_range(_Node_base* __pos, 
00499                              _InIter __first, _InIter __last) {
00500     typedef typename _Is_integer<_InIter>::_Integral _Integral;
00501     _M_insert_after_range(__pos, __first, __last, _Integral());
00502   }
00503 
00504   template <class _Integer>
00505   void _M_insert_after_range(_Node_base* __pos, _Integer __n, _Integer __x,
00506                              __true_type) {
00507     _M_insert_after_fill(__pos, __n, __x);
00508   }
00509 
00510   template <class _InIter>
00511   void _M_insert_after_range(_Node_base* __pos,
00512                              _InIter __first, _InIter __last,
00513                              __false_type) {
00514     while (__first != __last) {
00515       __pos = __slist_make_link(__pos, _M_create_node(*__first));
00516       ++__first;
00517     }
00518   }
00519 
00520 #else /* __STL_MEMBER_TEMPLATES */
00521 
00522   void _M_insert_after_range(_Node_base* __pos,
00523                              const_iterator __first, const_iterator __last) {
00524     while (__first != __last) {
00525       __pos = __slist_make_link(__pos, _M_create_node(*__first));
00526       ++__first;
00527     }
00528   }
00529   void _M_insert_after_range(_Node_base* __pos,
00530                              const value_type* __first,
00531                              const value_type* __last) {
00532     while (__first != __last) {
00533       __pos = __slist_make_link(__pos, _M_create_node(*__first));
00534       ++__first;
00535     }
00536   }
00537 
00538 #endif /* __STL_MEMBER_TEMPLATES */
00539 
00540 public:
00541 
00542   iterator insert_after(iterator __pos, const value_type& __x) {
00543     return iterator(_M_insert_after(__pos._M_node, __x));
00544   }
00545 
00546   iterator insert_after(iterator __pos) {
00547     return insert_after(__pos, value_type());
00548   }
00549 
00550   void insert_after(iterator __pos, size_type __n, const value_type& __x) {
00551     _M_insert_after_fill(__pos._M_node, __n, __x);
00552   }
00553 
00554 #ifdef __STL_MEMBER_TEMPLATES
00555 
00556   // We don't need any dispatching tricks here, because _M_insert_after_range
00557   // already does them.
00558   template <class _InIter>
00559   void insert_after(iterator __pos, _InIter __first, _InIter __last) {
00560     _M_insert_after_range(__pos._M_node, __first, __last);
00561   }
00562 
00563 #else /* __STL_MEMBER_TEMPLATES */
00564 
00565   void insert_after(iterator __pos,
00566                     const_iterator __first, const_iterator __last) {
00567     _M_insert_after_range(__pos._M_node, __first, __last);
00568   }
00569   void insert_after(iterator __pos,
00570                     const value_type* __first, const value_type* __last) {
00571     _M_insert_after_range(__pos._M_node, __first, __last);
00572   }
00573 
00574 #endif /* __STL_MEMBER_TEMPLATES */
00575 
00576   iterator insert(iterator __pos, const value_type& __x) {
00577     return iterator(_M_insert_after(__slist_previous(&this->_M_head,
00578                                                      __pos._M_node),
00579                     __x));
00580   }
00581 
00582   iterator insert(iterator __pos) {
00583     return iterator(_M_insert_after(__slist_previous(&this->_M_head,
00584                                                      __pos._M_node),
00585                                     value_type()));
00586   }
00587 
00588   void insert(iterator __pos, size_type __n, const value_type& __x) {
00589     _M_insert_after_fill(__slist_previous(&this->_M_head, __pos._M_node),
00590                          __n, __x);
00591   } 
00592     
00593 #ifdef __STL_MEMBER_TEMPLATES
00594 
00595   // We don't need any dispatching tricks here, because _M_insert_after_range
00596   // already does them.
00597   template <class _InIter>
00598   void insert(iterator __pos, _InIter __first, _InIter __last) {
00599     _M_insert_after_range(__slist_previous(&this->_M_head, __pos._M_node), 
00600                           __first, __last);
00601   }
00602 
00603 #else /* __STL_MEMBER_TEMPLATES */
00604 
00605   void insert(iterator __pos, const_iterator __first, const_iterator __last) {
00606     _M_insert_after_range(__slist_previous(&this->_M_head, __pos._M_node), 
00607                           __first, __last);
00608   }
00609   void insert(iterator __pos, const value_type* __first, 
00610                               const value_type* __last) {
00611     _M_insert_after_range(__slist_previous(&this->_M_head, __pos._M_node), 
00612                           __first, __last);
00613   }
00614 
00615 #endif /* __STL_MEMBER_TEMPLATES */
00616 
00617 
00618 public:
00619   iterator erase_after(iterator __pos) {
00620     return iterator((_Node*) this->_M_erase_after(__pos._M_node));
00621   }
00622   iterator erase_after(iterator __before_first, iterator __last) {
00623     return iterator((_Node*) this->_M_erase_after(__before_first._M_node, 
00624                                                   __last._M_node));
00625   } 
00626 
00627   iterator erase(iterator __pos) {
00628     return (_Node*) this->_M_erase_after(__slist_previous(&this->_M_head, 
00629                                                           __pos._M_node));
00630   }
00631   iterator erase(iterator __first, iterator __last) {
00632     return (_Node*) this->_M_erase_after(
00633       __slist_previous(&this->_M_head, __first._M_node), __last._M_node);
00634   }
00635 
00636   void resize(size_type new_size, const _Tp& __x);
00637   void resize(size_type new_size) { resize(new_size, _Tp()); }
00638   void clear() { this->_M_erase_after(&this->_M_head, 0); }
00639 
00640 public:
00641   // Moves the range [__before_first + 1, __before_last + 1) to *this,
00642   //  inserting it immediately after __pos.  This is constant time.
00643   void splice_after(iterator __pos, 
00644                     iterator __before_first, iterator __before_last)
00645   {
00646     if (__before_first != __before_last) 
00647       __slist_splice_after(__pos._M_node, __before_first._M_node, 
00648                            __before_last._M_node);
00649   }
00650 
00651   // Moves the element that follows __prev to *this, inserting it immediately
00652   //  after __pos.  This is constant time.
00653   void splice_after(iterator __pos, iterator __prev)
00654   {
00655     __slist_splice_after(__pos._M_node,
00656                          __prev._M_node, __prev._M_node->_M_next);
00657   }
00658 
00659 
00660   // Removes all of the elements from the list __x to *this, inserting
00661   // them immediately after __pos.  __x must not be *this.  Complexity:
00662   // linear in __x.size().
00663   void splice_after(iterator __pos, slist& __x)
00664   {
00665     __slist_splice_after(__pos._M_node, &__x._M_head);
00666   }
00667 
00668   // Linear in distance(begin(), __pos), and linear in __x.size().
00669   void splice(iterator __pos, slist& __x) {
00670     if (__x._M_head._M_next)
00671       __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
00672                            &__x._M_head, __slist_previous(&__x._M_head, 0));
00673   }
00674 
00675   // Linear in distance(begin(), __pos), and in distance(__x.begin(), __i).
00676   void splice(iterator __pos, slist& __x, iterator __i) {
00677     __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
00678                          __slist_previous(&__x._M_head, __i._M_node),
00679                          __i._M_node);
00680   }
00681 
00682   // Linear in distance(begin(), __pos), in distance(__x.begin(), __first),
00683   // and in distance(__first, __last).
00684   void splice(iterator __pos, slist& __x, iterator __first, iterator __last)
00685   {
00686     if (__first != __last)
00687       __slist_splice_after(__slist_previous(&this->_M_head, __pos._M_node),
00688                            __slist_previous(&__x._M_head, __first._M_node),
00689                            __slist_previous(__first._M_node, __last._M_node));
00690   }
00691 
00692 public:
00693   void reverse() { 
00694     if (this->_M_head._M_next)
00695       this->_M_head._M_next = __slist_reverse(this->_M_head._M_next);
00696   }
00697 
00698   void remove(const _Tp& __val); 
00699   void unique(); 
00700   void merge(slist& __x);
00701   void sort();     
00702 
00703 #ifdef __STL_MEMBER_TEMPLATES
00704   template <class _Predicate> 
00705   void remove_if(_Predicate __pred);
00706 
00707   template <class _BinaryPredicate> 
00708   void unique(_BinaryPredicate __pred); 
00709 
00710   template <class _StrictWeakOrdering> 
00711   void merge(slist&, _StrictWeakOrdering);
00712 
00713   template <class _StrictWeakOrdering> 
00714   void sort(_StrictWeakOrdering __comp); 
00715 #endif /* __STL_MEMBER_TEMPLATES */
00716 };
00717 
00718 template <class _Tp, class _Alloc>
00719 slist<_Tp,_Alloc>& slist<_Tp,_Alloc>::operator=(const slist<_Tp,_Alloc>& __x)
00720 {
00721   if (&__x != this) {
00722     _Node_base* __p1 = &this->_M_head;
00723     _Node* __n1 = (_Node*) this->_M_head._M_next;
00724     const _Node* __n2 = (const _Node*) __x._M_head._M_next;
00725     while (__n1 && __n2) {
00726       __n1->_M_data = __n2->_M_data;
00727       __p1 = __n1;
00728       __n1 = (_Node*) __n1->_M_next;
00729       __n2 = (const _Node*) __n2->_M_next;
00730     }
00731     if (__n2 == 0)
00732       this->_M_erase_after(__p1, 0);
00733     else
00734       _M_insert_after_range(__p1, const_iterator((_Node*)__n2), 
00735                                   const_iterator(0));
00736   }
00737   return *this;
00738 }
00739 
00740 template <class _Tp, class _Alloc>
00741 void slist<_Tp, _Alloc>::_M_fill_assign(size_type __n, const _Tp& __val) {
00742   _Node_base* __prev = &this->_M_head;
00743   _Node* __node = (_Node*) this->_M_head._M_next;
00744   for ( ; __node != 0 && __n > 0 ; --__n) {
00745     __node->_M_data = __val;
00746     __prev = __node;
00747     __node = (_Node*) __node->_M_next;
00748   }
00749   if (__n > 0)
00750     _M_insert_after_fill(__prev, __n, __val);
00751   else
00752     this->_M_erase_after(__prev, 0);
00753 }
00754 
00755 #ifdef __STL_MEMBER_TEMPLATES
00756 
00757 template <class _Tp, class _Alloc> template <class _InputIter>
00758 void
00759 slist<_Tp, _Alloc>::_M_assign_dispatch(_InputIter __first, _InputIter __last,
00760                                        __false_type)
00761 {
00762   _Node_base* __prev = &this->_M_head;
00763   _Node* __node = (_Node*) this->_M_head._M_next;
00764   while (__node != 0 && __first != __last) {
00765     __node->_M_data = *__first;
00766     __prev = __node;
00767     __node = (_Node*) __node->_M_next;
00768     ++__first;
00769   }
00770   if (__first != __last)
00771     _M_insert_after_range(__prev, __first, __last);
00772   else
00773     this->_M_erase_after(__prev, 0);
00774 }
00775 
00776 #endif /* __STL_MEMBER_TEMPLATES */
00777 
00778 template <class _Tp, class _Alloc>
00779 inline bool 
00780 operator==(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2)
00781 {
00782   typedef typename slist<_Tp,_Alloc>::const_iterator const_iterator;
00783   const_iterator __end1 = _SL1.end();
00784   const_iterator __end2 = _SL2.end();
00785 
00786   const_iterator __i1 = _SL1.begin();
00787   const_iterator __i2 = _SL2.begin();
00788   while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2) {
00789     ++__i1;
00790     ++__i2;
00791   }
00792   return __i1 == __end1 && __i2 == __end2;
00793 }
00794 
00795 
00796 template <class _Tp, class _Alloc>
00797 inline bool
00798 operator<(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2)
00799 {
00800   return lexicographical_compare(_SL1.begin(), _SL1.end(), 
00801                                  _SL2.begin(), _SL2.end());
00802 }
00803 
00804 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
00805 
00806 template <class _Tp, class _Alloc>
00807 inline bool 
00808 operator!=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
00809   return !(_SL1 == _SL2);
00810 }
00811 
00812 template <class _Tp, class _Alloc>
00813 inline bool 
00814 operator>(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
00815   return _SL2 < _SL1;
00816 }
00817 
00818 template <class _Tp, class _Alloc>
00819 inline bool 
00820 operator<=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
00821   return !(_SL2 < _SL1);
00822 }
00823 
00824 template <class _Tp, class _Alloc>
00825 inline bool 
00826 operator>=(const slist<_Tp,_Alloc>& _SL1, const slist<_Tp,_Alloc>& _SL2) {
00827   return !(_SL1 < _SL2);
00828 }
00829 
00830 template <class _Tp, class _Alloc>
00831 inline void swap(slist<_Tp,_Alloc>& __x, slist<_Tp,_Alloc>& __y) {
00832   __x.swap(__y);
00833 }
00834 
00835 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
00836 
00837 
00838 template <class _Tp, class _Alloc>
00839 void slist<_Tp,_Alloc>::resize(size_type __len, const _Tp& __x)
00840 {
00841   _Node_base* __cur = &this->_M_head;
00842   while (__cur->_M_next != 0 && __len > 0) {
00843     --__len;
00844     __cur = __cur->_M_next;
00845   }
00846   if (__cur->_M_next) 
00847     this->_M_erase_after(__cur, 0);
00848   else
00849     _M_insert_after_fill(__cur, __len, __x);
00850 }
00851 
00852 template <class _Tp, class _Alloc>
00853 void slist<_Tp,_Alloc>::remove(const _Tp& __val)
00854 {
00855   _Node_base* __cur = &this->_M_head;
00856   while (__cur && __cur->_M_next) {
00857     if (((_Node*) __cur->_M_next)->_M_data == __val)
00858       this->_M_erase_after(__cur);
00859     else
00860       __cur = __cur->_M_next;
00861   }
00862 }
00863 
00864 template <class _Tp, class _Alloc> 
00865 void slist<_Tp,_Alloc>::unique()
00866 {
00867   _Node_base* __cur = this->_M_head._M_next;
00868   if (__cur) {
00869     while (__cur->_M_next) {
00870       if (((_Node*)__cur)->_M_data == 
00871           ((_Node*)(__cur->_M_next))->_M_data)
00872         this->_M_erase_after(__cur);
00873       else
00874         __cur = __cur->_M_next;
00875     }
00876   }
00877 }
00878 
00879 template <class _Tp, class _Alloc>
00880 void slist<_Tp,_Alloc>::merge(slist<_Tp,_Alloc>& __x)
00881 {
00882   _Node_base* __n1 = &this->_M_head;
00883   while (__n1->_M_next && __x._M_head._M_next) {
00884     if (((_Node*) __x._M_head._M_next)->_M_data < 
00885         ((_Node*)       __n1->_M_next)->_M_data) 
00886       __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
00887     __n1 = __n1->_M_next;
00888   }
00889   if (__x._M_head._M_next) {
00890     __n1->_M_next = __x._M_head._M_next;
00891     __x._M_head._M_next = 0;
00892   }
00893 }
00894 
00895 template <class _Tp, class _Alloc>
00896 void slist<_Tp,_Alloc>::sort()
00897 {
00898   if (this->_M_head._M_next && this->_M_head._M_next->_M_next) {
00899     slist __carry;
00900     slist __counter[64];
00901     int __fill = 0;
00902     while (!empty()) {
00903       __slist_splice_after(&__carry._M_head,
00904                            &this->_M_head, this->_M_head._M_next);
00905       int __i = 0;
00906       while (__i < __fill && !__counter[__i].empty()) {
00907         __counter[__i].merge(__carry);
00908         __carry.swap(__counter[__i]);
00909         ++__i;
00910       }
00911       __carry.swap(__counter[__i]);
00912       if (__i == __fill)
00913         ++__fill;
00914     }
00915 
00916     for (int __i = 1; __i < __fill; ++__i)
00917       __counter[__i].merge(__counter[__i-1]);
00918     this->swap(__counter[__fill-1]);
00919   }
00920 }
00921 
00922 #ifdef __STL_MEMBER_TEMPLATES
00923 
00924 template <class _Tp, class _Alloc> 
00925 template <class _Predicate>
00926 void slist<_Tp,_Alloc>::remove_if(_Predicate __pred)
00927 {
00928   _Node_base* __cur = &this->_M_head;
00929   while (__cur->_M_next) {
00930     if (__pred(((_Node*) __cur->_M_next)->_M_data))
00931       this->_M_erase_after(__cur);
00932     else
00933       __cur = __cur->_M_next;
00934   }
00935 }
00936 
00937 template <class _Tp, class _Alloc> template <class _BinaryPredicate> 
00938 void slist<_Tp,_Alloc>::unique(_BinaryPredicate __pred)
00939 {
00940   _Node* __cur = (_Node*) this->_M_head._M_next;
00941   if (__cur) {
00942     while (__cur->_M_next) {
00943       if (__pred(((_Node*)__cur)->_M_data, 
00944                  ((_Node*)(__cur->_M_next))->_M_data))
00945         this->_M_erase_after(__cur);
00946       else
00947         __cur = (_Node*) __cur->_M_next;
00948     }
00949   }
00950 }
00951 
00952 template <class _Tp, class _Alloc> template <class _StrictWeakOrdering>
00953 void slist<_Tp,_Alloc>::merge(slist<_Tp,_Alloc>& __x,
00954                               _StrictWeakOrdering __comp)
00955 {
00956   _Node_base* __n1 = &this->_M_head;
00957   while (__n1->_M_next && __x._M_head._M_next) {
00958     if (__comp(((_Node*) __x._M_head._M_next)->_M_data,
00959                ((_Node*)       __n1->_M_next)->_M_data))
00960       __slist_splice_after(__n1, &__x._M_head, __x._M_head._M_next);
00961     __n1 = __n1->_M_next;
00962   }
00963   if (__x._M_head._M_next) {
00964     __n1->_M_next = __x._M_head._M_next;
00965     __x._M_head._M_next = 0;
00966   }
00967 }
00968 
00969 template <class _Tp, class _Alloc> template <class _StrictWeakOrdering> 
00970 void slist<_Tp,_Alloc>::sort(_StrictWeakOrdering __comp)
00971 {
00972   if (this->_M_head._M_next && this->_M_head._M_next->_M_next) {
00973     slist __carry;
00974     slist __counter[64];
00975     int __fill = 0;
00976     while (!empty()) {
00977       __slist_splice_after(&__carry._M_head,
00978                            &this->_M_head, this->_M_head._M_next);
00979       int __i = 0;
00980       while (__i < __fill && !__counter[__i].empty()) {
00981         __counter[__i].merge(__carry, __comp);
00982         __carry.swap(__counter[__i]);
00983         ++__i;
00984       }
00985       __carry.swap(__counter[__i]);
00986       if (__i == __fill)
00987         ++__fill;
00988     }
00989 
00990     for (int __i = 1; __i < __fill; ++__i)
00991       __counter[__i].merge(__counter[__i-1], __comp);
00992     this->swap(__counter[__fill-1]);
00993   }
00994 }
00995 
00996 #endif /* __STL_MEMBER_TEMPLATES */
00997 
00998 // Specialization of insert_iterator so that insertions will be constant
00999 // time rather than linear time.
01000 
01001 #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
01002 
01003 template <class _Tp, class _Alloc>
01004 class insert_iterator<slist<_Tp, _Alloc> > {
01005 protected:
01006   typedef slist<_Tp, _Alloc> _Container;
01007   _Container* container;
01008   typename _Container::iterator iter;
01009 public:
01010   typedef _Container          container_type;
01011   typedef output_iterator_tag iterator_category;
01012   typedef void                value_type;
01013   typedef void                difference_type;
01014   typedef void                pointer;
01015   typedef void                reference;
01016 
01017   insert_iterator(_Container& __x, typename _Container::iterator __i) 
01018     : container(&__x) {
01019     if (__i == __x.begin())
01020       iter = __x.before_begin();
01021     else
01022       iter = __x.previous(__i);
01023   }
01024 
01025   insert_iterator<_Container>&
01026   operator=(const typename _Container::value_type& __value) { 
01027     iter = container->insert_after(iter, __value);
01028     return *this;
01029   }
01030   insert_iterator<_Container>& operator*() { return *this; }
01031   insert_iterator<_Container>& operator++() { return *this; }
01032   insert_iterator<_Container>& operator++(int) { return *this; }
01033 };
01034 
01035 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
01036 
01037 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
01038 #pragma reset woff 1174
01039 #pragma reset woff 1375
01040 #endif
01041 
01042 __STL_END_NAMESPACE 
01043 
01044 #endif /* __SGI_STL_INTERNAL_SLIST_H */
01045 
01046 // Local Variables:
01047 // mode:C++
01048 // End:

Generated on Mon Jun 5 10:20:44 2006 for Intelligence.kdevelop by  doxygen 1.4.6