Whole document tree
    

Whole document tree

stl_numeric.h Source File
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 // Numeric functions implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 /*
00031  *
00032  * Copyright (c) 1994
00033  * Hewlett-Packard Company
00034  *
00035  * Permission to use, copy, modify, distribute and sell this software
00036  * and its documentation for any purpose is hereby granted without fee,
00037  * provided that the above copyright notice appear in all copies and
00038  * that both that copyright notice and this permission notice appear
00039  * in supporting documentation.  Hewlett-Packard Company makes no
00040  * representations about the suitability of this software for any
00041  * purpose.  It is provided "as is" without express or implied warranty.
00042  *
00043  *
00044  * Copyright (c) 1996,1997
00045  * Silicon Graphics Computer Systems, Inc.
00046  *
00047  * Permission to use, copy, modify, distribute and sell this software
00048  * and its documentation for any purpose is hereby granted without fee,
00049  * provided that the above copyright notice appear in all copies and
00050  * that both that copyright notice and this permission notice appear
00051  * in supporting documentation.  Silicon Graphics makes no
00052  * representations about the suitability of this software for any
00053  * purpose.  It is provided "as is" without express or implied warranty.
00054  */
00055 
00056 /* NOTE: This is an internal header file, included by other STL headers.
00057  *   You should not attempt to use it directly.
00058  */
00059 
00060 
00061 #ifndef _CPP_BITS_STL_NUMERIC_H
00062 #define _CPP_BITS_STL_NUMERIC_H 1
00063 
00064 namespace std
00065 {
00066 
00067 template <class _InputIterator, class _Tp>
00068 _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
00069 {
00070   // concept requirements
00071   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>);
00072 
00073   for ( ; __first != __last; ++__first)
00074     __init = __init + *__first;
00075   return __init;
00076 }
00077 
00078 template <class _InputIterator, class _Tp, class _BinaryOperation>
00079 _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
00080               _BinaryOperation __binary_op)
00081 {
00082   // concept requirements
00083   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>);
00084 
00085   for ( ; __first != __last; ++__first)
00086     __init = __binary_op(__init, *__first);
00087   return __init;
00088 }
00089 
00090 template <class _InputIterator1, class _InputIterator2, class _Tp>
00091 _Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
00092                  _InputIterator2 __first2, _Tp __init)
00093 {
00094   // concept requirements
00095   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>);
00096   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>);
00097 
00098   for ( ; __first1 != __last1; ++__first1, ++__first2)
00099     __init = __init + (*__first1 * *__first2);
00100   return __init;
00101 }
00102 
00103 template <class _InputIterator1, class _InputIterator2, class _Tp,
00104           class _BinaryOperation1, class _BinaryOperation2>
00105 _Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
00106                  _InputIterator2 __first2, _Tp __init, 
00107                  _BinaryOperation1 __binary_op1,
00108                  _BinaryOperation2 __binary_op2)
00109 {
00110   // concept requirements
00111   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>);
00112   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>);
00113 
00114   for ( ; __first1 != __last1; ++__first1, ++__first2)
00115     __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
00116   return __init;
00117 }
00118 
00119 template <class _InputIterator, class _OutputIterator, class _Tp>
00120 _OutputIterator 
00121 __partial_sum(_InputIterator __first, _InputIterator __last,
00122               _OutputIterator __result, _Tp*)
00123 {
00124   _Tp __value = *__first;
00125   while (++__first != __last) {
00126     __value = __value + *__first;
00127     *++__result = __value;
00128   }
00129   return ++__result;
00130 }
00131 
00132 template <class _InputIterator, class _OutputIterator>
00133 _OutputIterator 
00134 partial_sum(_InputIterator __first, _InputIterator __last,
00135             _OutputIterator __result)
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 }
00146 
00147 template <class _InputIterator, class _OutputIterator, class _Tp,
00148           class _BinaryOperation>
00149 _OutputIterator 
00150 __partial_sum(_InputIterator __first, _InputIterator __last, 
00151               _OutputIterator __result, _Tp*, _BinaryOperation __binary_op)
00152 {
00153   _Tp __value = *__first;
00154   while (++__first != __last) {
00155     __value = __binary_op(__value, *__first);
00156     *++__result = __value;
00157   }
00158   return ++__result;
00159 }
00160 
00161 template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
00162 _OutputIterator 
00163 partial_sum(_InputIterator __first, _InputIterator __last,
00164             _OutputIterator __result, _BinaryOperation __binary_op)
00165 {
00166   // concept requirements
00167   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>);
00168   __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
00169         typename iterator_traits<_InputIterator>::value_type>);
00170 
00171   if (__first == __last) return __result;
00172   *__result = *__first;
00173   return __partial_sum(__first, __last, __result, __value_type(__first), 
00174                        __binary_op);
00175 }
00176 
00177 template <class _InputIterator, class _OutputIterator, class _Tp>
00178 _OutputIterator 
00179 __adjacent_difference(_InputIterator __first, _InputIterator __last,
00180                       _OutputIterator __result, _Tp*)
00181 {
00182   _Tp __value = *__first;
00183   while (++__first != __last) {
00184     _Tp __tmp = *__first;
00185     *++__result = __tmp - __value;
00186     __value = __tmp;
00187   }
00188   return ++__result;
00189 }
00190 
00191 template <class _InputIterator, class _OutputIterator>
00192 _OutputIterator
00193 adjacent_difference(_InputIterator __first,
00194                     _InputIterator __last, _OutputIterator __result)
00195 {
00196   // concept requirements
00197   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>);
00198   __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
00199         typename iterator_traits<_InputIterator>::value_type>);
00200 
00201   if (__first == __last) return __result;
00202   *__result = *__first;
00203   return __adjacent_difference(__first, __last, __result,
00204                                __value_type(__first));
00205 }
00206 
00207 template <class _InputIterator, class _OutputIterator, class _Tp, 
00208           class _BinaryOperation>
00209 _OutputIterator
00210 __adjacent_difference(_InputIterator __first, _InputIterator __last, 
00211                       _OutputIterator __result, _Tp*,
00212                       _BinaryOperation __binary_op) {
00213   _Tp __value = *__first;
00214   while (++__first != __last) {
00215     _Tp __tmp = *__first;
00216     *++__result = __binary_op(__tmp, __value);
00217     __value = __tmp;
00218   }
00219   return ++__result;
00220 }
00221 
00222 template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
00223 _OutputIterator 
00224 adjacent_difference(_InputIterator __first, _InputIterator __last,
00225                     _OutputIterator __result, _BinaryOperation __binary_op)
00226 {
00227   // concept requirements
00228   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>);
00229   __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
00230         typename iterator_traits<_InputIterator>::value_type>);
00231 
00232   if (__first == __last) return __result;
00233   *__result = *__first;
00234   return __adjacent_difference(__first, __last, __result,
00235                                __value_type(__first),
00236                                __binary_op);
00237 }
00238 
00239 // Returns __x ** __n, where __n >= 0.  _Note that "multiplication"
00240 // is required to be associative, but not necessarily commutative.
00241 
00242  
00243 template <class _Tp, class _Integer, class _MonoidOperation>
00244 _Tp __power(_Tp __x, _Integer __n, _MonoidOperation __monoid_op)
00245 {
00246   if (__n == 0)
00247     return identity_element(__monoid_op);
00248   else {
00249     while ((__n & 1) == 0) {
00250       __n >>= 1;
00251       __x = __monoid_op(__x, __x);
00252     }
00253 
00254     _Tp __result = __x;
00255     __n >>= 1;
00256     while (__n != 0) {
00257       __x = __monoid_op(__x, __x);
00258       if ((__n & 1) != 0)
00259         __result = __monoid_op(__result, __x);
00260       __n >>= 1;
00261     }
00262     return __result;
00263   }
00264 }
00265 
00266 template <class _Tp, class _Integer>
00267 inline _Tp __power(_Tp __x, _Integer __n)
00268 {
00269   return __power(__x, __n, multiplies<_Tp>());
00270 }
00271 
00272 // Alias for the internal name __power.  Note that power is an extension,
00273 // not part of the C++ standard.
00274 
00275 template <class _Tp, class _Integer, class _MonoidOperation>
00276 inline _Tp power(_Tp __x, _Integer __n, _MonoidOperation __monoid_op)
00277 {
00278   return __power(__x, __n, __monoid_op);
00279 }
00280 
00281 template <class _Tp, class _Integer>
00282 inline _Tp power(_Tp __x, _Integer __n)
00283 {
00284   return __power(__x, __n);
00285 }
00286 
00287 // iota is not part of the C++ standard.  It is an extension.
00288 
00289 template <class _ForwardIter, class _Tp>
00290 void 
00291 iota(_ForwardIter __first, _ForwardIter __last, _Tp __value)
00292 {
00293   // concept requirements
00294   __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter>);
00295   __glibcpp_function_requires(_ConvertibleConcept<_Tp,
00296         typename iterator_traits<_ForwardIter>::value_type>);
00297 
00298   while (__first != __last)
00299     *__first++ = __value++;
00300 }
00301 
00302 } // namespace std
00303 
00304 #endif /* _CPP_BITS_STL_NUMERIC_H */
00305 
00306 // Local Variables:
00307 // mode:C++
00308 // End:

Generated on Mon Apr 8 03:11:42 2002 for libstdc++-v3 Source by doxygen1.2.15