/home/ntakagi/work/STLport-5.1.5/stlport/stl/_slist.c

Go to the documentation of this file.
00001 /*
00002  *
00003  * Copyright (c) 1996,1997
00004  * Silicon Graphics Computer Systems, Inc.
00005  *
00006  * Copyright (c) 1999
00007  * Boris Fomitchev
00008  *
00009  * This material is provided "as is", with absolutely no warranty expressed
00010  * or implied. Any use is at your own risk.
00011  *
00012  * Permission to use or copy this software for any purpose is hereby granted
00013  * without fee, provided the above notices are retained on all copies.
00014  * Permission to modify the code and to distribute modified code is granted,
00015  * provided the above notices are retained, and a notice that the code was
00016  * modified is included with the above copyright notice.
00017  *
00018  */
00019 #ifndef _STLP_SLIST_C
00020 #define _STLP_SLIST_C
00021 
00022 #ifndef _STLP_INTERNAL_SLIST_H
00023 #  include <stl/_slist.h>
00024 #endif
00025 
00026 #ifndef _STLP_CARRAY_H
00027 #  include <stl/_carray.h>
00028 #endif
00029 
00030 #ifndef _STLP_RANGE_ERRORS_H
00031 #  include <stl/_range_errors.h>
00032 #endif
00033 
00034 #if defined (_STLP_NESTED_TYPE_PARAM_BUG)
00035 #  define size_type size_t
00036 #endif
00037 
00038 _STLP_BEGIN_NAMESPACE
00039 
00040 _STLP_MOVE_TO_PRIV_NAMESPACE
00041 
00042 template <class _Tp, class _Alloc>
00043 _Slist_node_base*
00044 _Slist_base<_Tp,_Alloc>::_M_erase_after(_Slist_node_base* __before_first,
00045                                         _Slist_node_base* __last_node) {
00046   _Slist_node_base* __cur = __before_first->_M_next;
00047   while (__cur != __last_node) {
00048     _Node* __tmp = __STATIC_CAST(_Node*, __cur);
00049     __cur = __cur->_M_next;
00050     _STLP_STD::_Destroy(&__tmp->_M_data);
00051     _M_head.deallocate(__tmp,1);
00052   }
00053   __before_first->_M_next = __last_node;
00054   return __last_node;
00055 }
00056 
00057 #if defined (_STLP_USE_PTR_SPECIALIZATIONS)
00058 #  define slist _STLP_PTR_IMPL_NAME(slist)
00059 #elif defined (_STLP_DEBUG)
00060 #  define slist _STLP_NON_DBG_NAME(slist)
00061 #else
00062 _STLP_MOVE_TO_STD_NAMESPACE
00063 #endif
00064 
00065 /* When building STLport lib Digital Mars Compiler complains on the _M_data assignment
00066  * problem which would be perfertly right if we were using it. Hiding it during build
00067  * fix this issue.
00068  */
00069 template <class _Tp, class _Alloc>
00070 slist<_Tp,_Alloc>& slist<_Tp,_Alloc>::operator=(const slist<_Tp,_Alloc>& __x) {
00071   if (&__x != this) {
00072     _Node_base* __p1 = &this->_M_head._M_data;
00073     _Node_base* __n1 = this->_M_head._M_data._M_next;
00074     const _Node_base* __n2 = __x._M_head._M_data._M_next;
00075     while (__n1 && __n2) {
00076       __STATIC_CAST(_Node*, __n1)->_M_data = __STATIC_CAST(const _Node*, __n2)->_M_data;
00077       __p1 = __n1;
00078       __n1 = __n1->_M_next;
00079       __n2 = __n2->_M_next;
00080     }
00081     if (__n2 == 0)
00082       this->_M_erase_after(__p1, 0);
00083     else
00084       _M_insert_after_range(__p1, const_iterator(__CONST_CAST(_Node_base*, __n2)),
00085                                   const_iterator(0));
00086   }
00087   return *this;
00088 }
00089 
00090 template <class _Tp, class _Alloc>
00091 void slist<_Tp, _Alloc>::_M_fill_assign(size_type __n, const _Tp& __val) {
00092   _Node_base* __prev = &this->_M_head._M_data;
00093   _Node_base* __node = this->_M_head._M_data._M_next;
00094   for ( ; __node != 0 && __n > 0 ; --__n) {
00095     __STATIC_CAST(_Node*, __node)->_M_data = __val;
00096     __prev = __node;
00097     __node = __node->_M_next;
00098   }
00099   if (__n > 0)
00100     _M_insert_after_fill(__prev, __n, __val);
00101   else
00102     this->_M_erase_after(__prev, 0);
00103 }
00104 
00105 template <class _Tp, class _Alloc>
00106 void slist<_Tp,_Alloc>::resize(size_type __len, const _Tp& __x) {
00107   _Node_base* __cur = &this->_M_head._M_data;
00108   while (__cur->_M_next != 0 && __len > 0) {
00109     --__len;
00110     __cur = __cur->_M_next;
00111   }
00112   if (__cur->_M_next)
00113     this->_M_erase_after(__cur, 0);
00114   else
00115     _M_insert_after_fill(__cur, __len, __x);
00116 }
00117 
00118 template <class _Tp, class _Alloc>
00119 void slist<_Tp,_Alloc>::remove(const _Tp& __val) {
00120   _Node_base* __cur = &this->_M_head._M_data;
00121   while (__cur && __cur->_M_next) {
00122     if (__STATIC_CAST(_Node*, __cur->_M_next)->_M_data == __val)
00123       this->_M_erase_after(__cur);
00124     else
00125       __cur = __cur->_M_next;
00126   }
00127 }
00128 
00129 #if !defined (slist)
00130 _STLP_MOVE_TO_PRIV_NAMESPACE
00131 #endif
00132 
00133 template <class _Tp, class _Alloc, class _BinaryPredicate>
00134 void _Slist_unique(slist<_Tp, _Alloc>& __that, _BinaryPredicate __pred) {
00135   typedef _Slist_node<_Tp> _Node;
00136   typename slist<_Tp, _Alloc>::iterator __ite(__that.begin());
00137   if (__ite != __that.end()) {
00138     while (__ite._M_node->_M_next) {
00139       if (__pred(*__ite, __STATIC_CAST(_Node*, __ite._M_node->_M_next)->_M_data))
00140         __that.erase_after(__ite);
00141       else
00142         ++__ite;
00143     }
00144   }
00145 }
00146 
00147 template <class _Tp, class _Alloc, class _StrictWeakOrdering>
00148 void _Slist_merge(slist<_Tp, _Alloc>& __that, slist<_Tp, _Alloc>& __x,
00149                   _StrictWeakOrdering __comp) {
00150   typedef _Slist_node<_Tp> _Node;
00151   typedef _STLP_PRIV _Slist_node_base _Node_base;
00152   if (__that.get_allocator() == __x.get_allocator()) {
00153     typename slist<_Tp, _Alloc>::iterator __ite(__that.before_begin());
00154     while (__ite._M_node->_M_next && !__x.empty()) {
00155       if (__comp(__x.front(), __STATIC_CAST(_Node*, __ite._M_node->_M_next)->_M_data)) {
00156         _STLP_VERBOSE_ASSERT(!__comp(__STATIC_CAST(_Node*, __ite._M_node->_M_next)->_M_data, __x.front()),
00157                              _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
00158         __that.splice_after(__ite, __x, __x.before_begin());
00159       }
00160       ++__ite;
00161     }
00162     if (!__x.empty()) {
00163       __that.splice_after(__ite, __x);
00164     }
00165   }
00166   else {
00167     typename slist<_Tp, _Alloc>::iterator __i1(__that.before_begin()), __i2(__x.begin());
00168     while (__i1._M_node->_M_next && __i2._M_node) {
00169       if (__comp(__STATIC_CAST(_Node*, __i1._M_node->_M_next)->_M_data, *__i2)) {
00170         _STLP_VERBOSE_ASSERT(!__comp(*__i2, __STATIC_CAST(_Node*, __i1._M_node->_M_next)->_M_data),
00171                              _StlMsg_INVALID_STRICT_WEAK_PREDICATE)
00172         ++__i1;
00173       }
00174       else {
00175         __i1 = __that.insert_after(__i1, *(__i2++));
00176       }
00177     }
00178     __that.insert_after(__i1, __i2, __x.end());
00179     __x.clear();
00180   }
00181 }
00182 
00183 template <class _Tp, class _Alloc, class _StrictWeakOrdering>
00184 void _Slist_sort(slist<_Tp, _Alloc>& __that, _StrictWeakOrdering __comp) {
00185   if (!__that.begin()._M_node || !__that.begin()._M_node->_M_next)
00186     return;
00187 
00188   slist<_Tp, _Alloc> __carry(__that.get_allocator());
00189   const int NB = 64;
00190   _STLP_PRIV _CArray<slist<_Tp, _Alloc>, NB> __counter(__carry);
00191   int __fill = 0;
00192   while (!__that.empty()) {
00193     __carry.splice_after(__carry.before_begin(), __that, __that.before_begin());
00194     int __i = 0;
00195     while (__i < __fill && !__counter[__i].empty()) {
00196       _STLP_PRIV _Slist_merge(__counter[__i], __carry, __comp);
00197       __carry.swap(__counter[__i]);
00198       ++__i;
00199     }
00200     __carry.swap(__counter[__i]);
00201     if (__i == __fill) {
00202       ++__fill;
00203       if (__fill >= NB) {
00204         //Looks like the slist has too many elements to be sorted with this algorithm:
00205         __stl_throw_overflow_error("slist::sort");
00206       }
00207     }
00208   }
00209 
00210   for (int __i = 1; __i < __fill; ++__i)
00211     _STLP_PRIV _Slist_merge(__counter[__i], __counter[__i - 1], __comp);
00212   __that.swap(__counter[__fill-1]);
00213 }
00214 
00215 #if defined (slist)
00216 #  undef slist
00217 #endif
00218 
00219 _STLP_MOVE_TO_STD_NAMESPACE
00220 
00221 _STLP_END_NAMESPACE
00222 
00223 #if defined (_STLP_NESTED_TYPE_PARAM_BUG)
00224 #  undef size_type
00225 #endif
00226 
00227 #endif /*  _STLP_SLIST_C */
00228 
00229 // Local Variables:
00230 // mode:C++
00231 // End:



Generated on Mon Mar 10 15:32:34 2008 by  doxygen 1.5.1