stl_numeric.h

00001 /*
00002  *
00003  * Copyright (c) 1994
00004  * Hewlett-Packard Company
00005  *
00006  * Permission to use, copy, modify, distribute and sell this software
00007  * and its documentation for any purpose is hereby granted without fee,
00008  * provided that the above copyright notice appear in all copies and
00009  * that both that copyright notice and this permission notice appear
00010  * in supporting documentation.  Hewlett-Packard Company makes no
00011  * representations about the suitability of this software for any
00012  * purpose.  It is provided "as is" without express or implied warranty.
00013  *
00014  *
00015  * Copyright (c) 1996,1997
00016  * Silicon Graphics Computer Systems, Inc.
00017  *
00018  * Permission to use, copy, modify, distribute and sell this software
00019  * and its documentation for any purpose is hereby granted without fee,
00020  * provided that the above copyright notice appear in all copies and
00021  * that both that copyright notice and this permission notice appear
00022  * in supporting documentation.  Silicon Graphics makes no
00023  * representations about the suitability of this software for any
00024  * purpose.  It is provided "as is" without express or implied warranty.
00025  */
00026 
00027 /* NOTE: This is an internal header file, included by other STL headers.
00028  *   You should not attempt to use it directly.
00029  */
00030 
00031 
00032 #ifndef __SGI_STL_INTERNAL_NUMERIC_H
00033 #define __SGI_STL_INTERNAL_NUMERIC_H
00034 
00035 __STL_BEGIN_NAMESPACE
00036 
00037 template <class _InputIterator, class _Tp>
00038 _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
00039 {
00040   __STL_REQUIRES(_InputIterator, _InputIterator);
00041   for ( ; __first != __last; ++__first)
00042     __init = __init + *__first;
00043   return __init;
00044 }
00045 
00046 template <class _InputIterator, class _Tp, class _BinaryOperation>
00047 _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
00048                _BinaryOperation __binary_op)
00049 {
00050   __STL_REQUIRES(_InputIterator, _InputIterator);
00051   for ( ; __first != __last; ++__first)
00052     __init = __binary_op(__init, *__first);
00053   return __init;
00054 }
00055 
00056 template <class _InputIterator1, class _InputIterator2, class _Tp>
00057 _Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
00058                   _InputIterator2 __first2, _Tp __init)
00059 {
00060   __STL_REQUIRES(_InputIterator2, _InputIterator);
00061   __STL_REQUIRES(_InputIterator2, _InputIterator);
00062   for ( ; __first1 != __last1; ++__first1, ++__first2)
00063     __init = __init + (*__first1 * *__first2);
00064   return __init;
00065 }
00066 
00067 template <class _InputIterator1, class _InputIterator2, class _Tp,
00068           class _BinaryOperation1, class _BinaryOperation2>
00069 _Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
00070                   _InputIterator2 __first2, _Tp __init, 
00071                   _BinaryOperation1 __binary_op1,
00072                   _BinaryOperation2 __binary_op2)
00073 {
00074   __STL_REQUIRES(_InputIterator2, _InputIterator);
00075   __STL_REQUIRES(_InputIterator2, _InputIterator);
00076   for ( ; __first1 != __last1; ++__first1, ++__first2)
00077     __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
00078   return __init;
00079 }
00080 
00081 template <class _InputIterator, class _OutputIterator, class _Tp>
00082 _OutputIterator 
00083 __partial_sum(_InputIterator __first, _InputIterator __last,
00084               _OutputIterator __result, _Tp*)
00085 {
00086   _Tp __value = *__first;
00087   while (++__first != __last) {
00088     __value = __value + *__first;
00089     *++__result = __value;
00090   }
00091   return ++__result;
00092 }
00093 
00094 template <class _InputIterator, class _OutputIterator>
00095 _OutputIterator 
00096 partial_sum(_InputIterator __first, _InputIterator __last,
00097             _OutputIterator __result)
00098 {
00099   __STL_REQUIRES(_InputIterator, _InputIterator);
00100   __STL_REQUIRES(_OutputIterator, _OutputIterator);
00101   if (__first == __last) return __result;
00102   *__result = *__first;
00103   return __partial_sum(__first, __last, __result, __VALUE_TYPE(__first));
00104 }
00105 
00106 template <class _InputIterator, class _OutputIterator, class _Tp,
00107           class _BinaryOperation>
00108 _OutputIterator 
00109 __partial_sum(_InputIterator __first, _InputIterator __last, 
00110               _OutputIterator __result, _Tp*, _BinaryOperation __binary_op)
00111 {
00112   _Tp __value = *__first;
00113   while (++__first != __last) {
00114     __value = __binary_op(__value, *__first);
00115     *++__result = __value;
00116   }
00117   return ++__result;
00118 }
00119 
00120 template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
00121 _OutputIterator 
00122 partial_sum(_InputIterator __first, _InputIterator __last,
00123             _OutputIterator __result, _BinaryOperation __binary_op)
00124 {
00125   __STL_REQUIRES(_InputIterator, _InputIterator);
00126   __STL_REQUIRES(_OutputIterator, _OutputIterator);
00127   if (__first == __last) return __result;
00128   *__result = *__first;
00129   return __partial_sum(__first, __last, __result, __VALUE_TYPE(__first), 
00130                        __binary_op);
00131 }
00132 
00133 template <class _InputIterator, class _OutputIterator, class _Tp>
00134 _OutputIterator 
00135 __adjacent_difference(_InputIterator __first, _InputIterator __last,
00136                       _OutputIterator __result, _Tp*)
00137 {
00138   _Tp __value = *__first;
00139   while (++__first != __last) {
00140     _Tp __tmp = *__first;
00141     *++__result = __tmp - __value;
00142     __value = __tmp;
00143   }
00144   return ++__result;
00145 }
00146 
00147 template <class _InputIterator, class _OutputIterator>
00148 _OutputIterator
00149 adjacent_difference(_InputIterator __first,
00150                     _InputIterator __last, _OutputIterator __result)
00151 {
00152   __STL_REQUIRES(_InputIterator, _InputIterator);
00153   __STL_REQUIRES(_OutputIterator, _OutputIterator);
00154   if (__first == __last) return __result;
00155   *__result = *__first;
00156   return __adjacent_difference(__first, __last, __result,
00157                                __VALUE_TYPE(__first));
00158 }
00159 
00160 template <class _InputIterator, class _OutputIterator, class _Tp, 
00161           class _BinaryOperation>
00162 _OutputIterator
00163 __adjacent_difference(_InputIterator __first, _InputIterator __last, 
00164                       _OutputIterator __result, _Tp*,
00165                       _BinaryOperation __binary_op) {
00166   _Tp __value = *__first;
00167   while (++__first != __last) {
00168     _Tp __tmp = *__first;
00169     *++__result = __binary_op(__tmp, __value);
00170     __value = __tmp;
00171   }
00172   return ++__result;
00173 }
00174 
00175 template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
00176 _OutputIterator 
00177 adjacent_difference(_InputIterator __first, _InputIterator __last,
00178                     _OutputIterator __result, _BinaryOperation __binary_op)
00179 {
00180   __STL_REQUIRES(_InputIterator, _InputIterator);
00181   __STL_REQUIRES(_OutputIterator, _OutputIterator);
00182   if (__first == __last) return __result;
00183   *__result = *__first;
00184   return __adjacent_difference(__first, __last, __result,
00185                                __VALUE_TYPE(__first),
00186                                __binary_op);
00187 }
00188 
00189 // Returns __x ** __n, where __n >= 0.  _Note that "multiplication"
00190 // is required to be associative, but not necessarily commutative.
00191 
00192  
00193 template <class _Tp, class _Integer, class _MonoidOperation>
00194 _Tp __power(_Tp __x, _Integer __n, _MonoidOperation __opr)
00195 {
00196   if (__n == 0)
00197     return identity_element(__opr);
00198   else {
00199     while ((__n & 1) == 0) {
00200       __n >>= 1;
00201       __x = __opr(__x, __x);
00202     }
00203 
00204     _Tp __result = __x;
00205     __n >>= 1;
00206     while (__n != 0) {
00207       __x = __opr(__x, __x);
00208       if ((__n & 1) != 0)
00209         __result = __opr(__result, __x);
00210       __n >>= 1;
00211     }
00212     return __result;
00213   }
00214 }
00215 
00216 template <class _Tp, class _Integer>
00217 inline _Tp __power(_Tp __x, _Integer __n)
00218 {
00219   return __power(__x, __n, multiplies<_Tp>());
00220 }
00221 
00222 // Alias for the internal name __power.  Note that power is an extension,
00223 // not part of the C++ standard.
00224 
00225 template <class _Tp, class _Integer, class _MonoidOperation>
00226 inline _Tp power(_Tp __x, _Integer __n, _MonoidOperation __opr)
00227 {
00228   return __power(__x, __n, __opr);
00229 }
00230 
00231 template <class _Tp, class _Integer>
00232 inline _Tp power(_Tp __x, _Integer __n)
00233 {
00234   return __power(__x, __n);
00235 }
00236 
00237 // iota is not part of the C++ standard.  It is an extension.
00238 
00239 template <class _ForwardIter, class _Tp>
00240 void 
00241 iota(_ForwardIter __first, _ForwardIter __last, _Tp __value)
00242 {
00243   __STL_REQUIRES(_ForwardIter, _Mutable_ForwardIterator);
00244   __STL_CONVERTIBLE(_Tp, typename iterator_traits<_ForwardIter>::value_type);
00245   while (__first != __last)
00246     *__first++ = __value++;
00247 }
00248 
00249 __STL_END_NAMESPACE
00250 
00251 #endif /* __SGI_STL_INTERNAL_NUMERIC_H */
00252 
00253 // Local Variables:
00254 // mode:C++
00255 // End:

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