Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

stl_numeric.h

Go to the documentation of this file.
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 _CPP_BITS_STL_NUMERIC_H
00033 #define _CPP_BITS_STL_NUMERIC_H 1
00034 
00035 namespace std
00036 {
00037 
00038 template <class _InputIterator, class _Tp>
00039 _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
00040 {
00041   // concept requirements
00042   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>);
00043 
00044   for ( ; __first != __last; ++__first)
00045     __init = __init + *__first;
00046   return __init;
00047 }
00048 
00049 template <class _InputIterator, class _Tp, class _BinaryOperation>
00050 _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
00051               _BinaryOperation __binary_op)
00052 {
00053   // concept requirements
00054   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>);
00055 
00056   for ( ; __first != __last; ++__first)
00057     __init = __binary_op(__init, *__first);
00058   return __init;
00059 }
00060 
00061 template <class _InputIterator1, class _InputIterator2, class _Tp>
00062 _Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
00063                  _InputIterator2 __first2, _Tp __init)
00064 {
00065   // concept requirements
00066   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>);
00067   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>);
00068 
00069   for ( ; __first1 != __last1; ++__first1, ++__first2)
00070     __init = __init + (*__first1 * *__first2);
00071   return __init;
00072 }
00073 
00074 template <class _InputIterator1, class _InputIterator2, class _Tp,
00075           class _BinaryOperation1, class _BinaryOperation2>
00076 _Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
00077                  _InputIterator2 __first2, _Tp __init, 
00078                  _BinaryOperation1 __binary_op1,
00079                  _BinaryOperation2 __binary_op2)
00080 {
00081   // concept requirements
00082   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>);
00083   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>);
00084 
00085   for ( ; __first1 != __last1; ++__first1, ++__first2)
00086     __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
00087   return __init;
00088 }
00089 
00090 template <class _InputIterator, class _OutputIterator, class _Tp>
00091 _OutputIterator 
00092 __partial_sum(_InputIterator __first, _InputIterator __last,
00093               _OutputIterator __result, _Tp*)
00094 {
00095   _Tp __value = *__first;
00096   while (++__first != __last) {
00097     __value = __value + *__first;
00098     *++__result = __value;
00099   }
00100   return ++__result;
00101 }
00102 
00103 template <class _InputIterator, class _OutputIterator>
00104 _OutputIterator 
00105 partial_sum(_InputIterator __first, _InputIterator __last,
00106             _OutputIterator __result)
00107 {
00108   // concept requirements
00109   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>);
00110   __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
00111         typename iterator_traits<_InputIterator>::value_type>);
00112 
00113   if (__first == __last) return __result;
00114   *__result = *__first;
00115   return __partial_sum(__first, __last, __result, __value_type(__first));
00116 }
00117 
00118 template <class _InputIterator, class _OutputIterator, class _Tp,
00119           class _BinaryOperation>
00120 _OutputIterator 
00121 __partial_sum(_InputIterator __first, _InputIterator __last, 
00122               _OutputIterator __result, _Tp*, _BinaryOperation __binary_op)
00123 {
00124   _Tp __value = *__first;
00125   while (++__first != __last) {
00126     __value = __binary_op(__value, *__first);
00127     *++__result = __value;
00128   }
00129   return ++__result;
00130 }
00131 
00132 template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
00133 _OutputIterator 
00134 partial_sum(_InputIterator __first, _InputIterator __last,
00135             _OutputIterator __result, _BinaryOperation __binary_op)
00136 {
00137   // concept requirements
00138   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>);
00139   __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
00140         typename iterator_traits<_InputIterator>::value_type>);
00141 
00142   if (__first == __last) return __result;
00143   *__result = *__first;
00144   return __partial_sum(__first, __last, __result, __value_type(__first), 
00145                        __binary_op);
00146 }
00147 
00148 template <class _InputIterator, class _OutputIterator, class _Tp>
00149 _OutputIterator 
00150 __adjacent_difference(_InputIterator __first, _InputIterator __last,
00151                       _OutputIterator __result, _Tp*)
00152 {
00153   _Tp __value = *__first;
00154   while (++__first != __last) {
00155     _Tp __tmp = *__first;
00156     *++__result = __tmp - __value;
00157     __value = __tmp;
00158   }
00159   return ++__result;
00160 }
00161 
00162 template <class _InputIterator, class _OutputIterator>
00163 _OutputIterator
00164 adjacent_difference(_InputIterator __first,
00165                     _InputIterator __last, _OutputIterator __result)
00166 {
00167   // concept requirements
00168   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>);
00169   __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
00170         typename iterator_traits<_InputIterator>::value_type>);
00171 
00172   if (__first == __last) return __result;
00173   *__result = *__first;
00174   return __adjacent_difference(__first, __last, __result,
00175                                __value_type(__first));
00176 }
00177 
00178 template <class _InputIterator, class _OutputIterator, class _Tp, 
00179           class _BinaryOperation>
00180 _OutputIterator
00181 __adjacent_difference(_InputIterator __first, _InputIterator __last, 
00182                       _OutputIterator __result, _Tp*,
00183                       _BinaryOperation __binary_op) {
00184   _Tp __value = *__first;
00185   while (++__first != __last) {
00186     _Tp __tmp = *__first;
00187     *++__result = __binary_op(__tmp, __value);
00188     __value = __tmp;
00189   }
00190   return ++__result;
00191 }
00192 
00193 template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
00194 _OutputIterator 
00195 adjacent_difference(_InputIterator __first, _InputIterator __last,
00196                     _OutputIterator __result, _BinaryOperation __binary_op)
00197 {
00198   // concept requirements
00199   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>);
00200   __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
00201         typename iterator_traits<_InputIterator>::value_type>);
00202 
00203   if (__first == __last) return __result;
00204   *__result = *__first;
00205   return __adjacent_difference(__first, __last, __result,
00206                                __value_type(__first),
00207                                __binary_op);
00208 }
00209 
00210 // Returns __x ** __n, where __n >= 0.  _Note that "multiplication"
00211 // is required to be associative, but not necessarily commutative.
00212 
00213  
00214 template <class _Tp, class _Integer, class _MonoidOperation>
00215 _Tp __power(_Tp __x, _Integer __n, _MonoidOperation __monoid_op)
00216 {
00217   if (__n == 0)
00218     return identity_element(__monoid_op);
00219   else {
00220     while ((__n & 1) == 0) {
00221       __n >>= 1;
00222       __x = __monoid_op(__x, __x);
00223     }
00224 
00225     _Tp __result = __x;
00226     __n >>= 1;
00227     while (__n != 0) {
00228       __x = __monoid_op(__x, __x);
00229       if ((__n & 1) != 0)
00230         __result = __monoid_op(__result, __x);
00231       __n >>= 1;
00232     }
00233     return __result;
00234   }
00235 }
00236 
00237 template <class _Tp, class _Integer>
00238 inline _Tp __power(_Tp __x, _Integer __n)
00239 {
00240   return __power(__x, __n, multiplies<_Tp>());
00241 }
00242 
00243 // Alias for the internal name __power.  Note that power is an extension,
00244 // not part of the C++ standard.
00245 
00246 template <class _Tp, class _Integer, class _MonoidOperation>
00247 inline _Tp power(_Tp __x, _Integer __n, _MonoidOperation __monoid_op)
00248 {
00249   return __power(__x, __n, __monoid_op);
00250 }
00251 
00252 template <class _Tp, class _Integer>
00253 inline _Tp power(_Tp __x, _Integer __n)
00254 {
00255   return __power(__x, __n);
00256 }
00257 
00258 // iota is not part of the C++ standard.  It is an extension.
00259 
00260 template <class _ForwardIter, class _Tp>
00261 void 
00262 iota(_ForwardIter __first, _ForwardIter __last, _Tp __value)
00263 {
00264   // concept requirements
00265   __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter>);
00266   __glibcpp_function_requires(_ConvertibleConcept<_Tp,
00267         typename iterator_traits<_ForwardIter>::value_type>);
00268 
00269   while (__first != __last)
00270     *__first++ = __value++;
00271 }
00272 
00273 } // namespace std
00274 
00275 #endif /* _CPP_BITS_STL_NUMERIC_H */
00276 
00277 // Local Variables:
00278 // mode:C++
00279 // End:

Generated at Tue May 1 16:28:39 2001 for libstdc++-v3 by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001