stl_numeric.h

Go to the documentation of this file.
00001 // Numeric functions implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 /*
00032  *
00033  * Copyright (c) 1994
00034  * Hewlett-Packard Company
00035  *
00036  * Permission to use, copy, modify, distribute and sell this software
00037  * and its documentation for any purpose is hereby granted without fee,
00038  * provided that the above copyright notice appear in all copies and
00039  * that both that copyright notice and this permission notice appear
00040  * in supporting documentation.  Hewlett-Packard Company makes no
00041  * representations about the suitability of this software for any
00042  * purpose.  It is provided "as is" without express or implied warranty.
00043  *
00044  *
00045  * Copyright (c) 1996,1997
00046  * Silicon Graphics Computer Systems, Inc.
00047  *
00048  * Permission to use, copy, modify, distribute and sell this software
00049  * and its documentation for any purpose is hereby granted without fee,
00050  * provided that the above copyright notice appear in all copies and
00051  * that both that copyright notice and this permission notice appear
00052  * in supporting documentation.  Silicon Graphics makes no
00053  * representations about the suitability of this software for any
00054  * purpose.  It is provided "as is" without express or implied warranty.
00055  */
00056 
00057 /** @file stl_numeric.h
00058  *  This is an internal header file, included by other library headers.
00059  *  You should not attempt to use it directly.
00060  */
00061 
00062 #ifndef _STL_NUMERIC_H
00063 #define _STL_NUMERIC_H 1
00064 
00065 #include <bits/concept_check.h>
00066 #include <debug/debug.h>
00067 
00068 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
00069 
00070   /**
00071    *  @brief  Accumulate values in a range.
00072    *
00073    *  Accumulates the values in the range [first,last) using operator+().  The
00074    *  initial value is @a init.  The values are processed in order.
00075    *
00076    *  @param  first  Start of range.
00077    *  @param  last  End of range.
00078    *  @param  init  Starting value to add other values to.
00079    *  @return  The final sum.
00080    */
00081   template<typename _InputIterator, typename _Tp>
00082     inline _Tp
00083     accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
00084     {
00085       // concept requirements
00086       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00087       __glibcxx_requires_valid_range(__first, __last);
00088 
00089       for (; __first != __last; ++__first)
00090     __init = __init + *__first;
00091       return __init;
00092     }
00093 
00094   /**
00095    *  @brief  Accumulate values in a range with operation.
00096    *
00097    *  Accumulates the values in the range [first,last) using the function
00098    *  object @a binary_op.  The initial value is @a init.  The values are
00099    *  processed in order.
00100    *
00101    *  @param  first  Start of range.
00102    *  @param  last  End of range.
00103    *  @param  init  Starting value to add other values to.
00104    *  @param  binary_op  Function object to accumulate with.
00105    *  @return  The final sum.
00106    */
00107   template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
00108     inline _Tp
00109     accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
00110            _BinaryOperation __binary_op)
00111     {
00112       // concept requirements
00113       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00114       __glibcxx_requires_valid_range(__first, __last);
00115 
00116       for (; __first != __last; ++__first)
00117     __init = __binary_op(__init, *__first);
00118       return __init;
00119     }
00120 
00121   /**
00122    *  @brief  Compute inner product of two ranges.
00123    *
00124    *  Starting with an initial value of @a init, multiplies successive
00125    *  elements from the two ranges and adds each product into the accumulated
00126    *  value using operator+().  The values in the ranges are processed in
00127    *  order.
00128    *
00129    *  @param  first1  Start of range 1.
00130    *  @param  last1  End of range 1.
00131    *  @param  first2  Start of range 2.
00132    *  @param  init  Starting value to add other values to.
00133    *  @return  The final inner product.
00134    */
00135   template<typename _InputIterator1, typename _InputIterator2, typename _Tp>
00136     inline _Tp
00137     inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
00138           _InputIterator2 __first2, _Tp __init)
00139     {
00140       // concept requirements
00141       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
00142       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
00143       __glibcxx_requires_valid_range(__first1, __last1);
00144 
00145       for (; __first1 != __last1; ++__first1, ++__first2)
00146     __init = __init + (*__first1 * *__first2);
00147       return __init;
00148     }
00149 
00150   /**
00151    *  @brief  Compute inner product of two ranges.
00152    *
00153    *  Starting with an initial value of @a init, applies @a binary_op2 to
00154    *  successive elements from the two ranges and accumulates each result into
00155    *  the accumulated value using @a binary_op1.  The values in the ranges are
00156    *  processed in order.
00157    *
00158    *  @param  first1  Start of range 1.
00159    *  @param  last1  End of range 1.
00160    *  @param  first2  Start of range 2.
00161    *  @param  init  Starting value to add other values to.
00162    *  @param  binary_op1  Function object to accumulate with.
00163    *  @param  binary_op2  Function object to apply to pairs of input values.
00164    *  @return  The final inner product.
00165    */
00166   template<typename _InputIterator1, typename _InputIterator2, typename _Tp,
00167         typename _BinaryOperation1, typename _BinaryOperation2>
00168     inline _Tp
00169     inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
00170           _InputIterator2 __first2, _Tp __init,
00171           _BinaryOperation1 __binary_op1,
00172           _BinaryOperation2 __binary_op2)
00173     {
00174       // concept requirements
00175       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
00176       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
00177       __glibcxx_requires_valid_range(__first1, __last1);
00178 
00179       for (; __first1 != __last1; ++__first1, ++__first2)
00180     __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
00181       return __init;
00182     }
00183 
00184   /**
00185    *  @brief  Return list of partial sums
00186    *
00187    *  Accumulates the values in the range [first,last) using operator+().
00188    *  As each successive input value is added into the total, that partial sum
00189    *  is written to @a result.  Therefore, the first value in result is the
00190    *  first value of the input, the second value in result is the sum of the
00191    *  first and second input values, and so on.
00192    *
00193    *  @param  first  Start of input range.
00194    *  @param  last  End of input range.
00195    *  @param  result  Output to write sums to.
00196    *  @return  Iterator pointing just beyond the values written to result.
00197    */
00198   template<typename _InputIterator, typename _OutputIterator>
00199     _OutputIterator
00200     partial_sum(_InputIterator __first, _InputIterator __last,
00201         _OutputIterator __result)
00202     {
00203       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
00204 
00205       // concept requirements
00206       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00207       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
00208                                          _ValueType>)
00209       __glibcxx_requires_valid_range(__first, __last);
00210 
00211       if (__first == __last)
00212     return __result;
00213       _ValueType __value = *__first;
00214       *__result = __value;
00215       while (++__first != __last)
00216     {
00217       __value = __value + *__first;
00218       *++__result = __value;
00219     }
00220       return ++__result;
00221     }
00222 
00223   /**
00224    *  @brief  Return list of partial sums
00225    *
00226    *  Accumulates the values in the range [first,last) using operator+().
00227    *  As each successive input value is added into the total, that partial sum
00228    *  is written to @a result.  Therefore, the first value in result is the
00229    *  first value of the input, the second value in result is the sum of the
00230    *  first and second input values, and so on.
00231    *
00232    *  @param  first  Start of input range.
00233    *  @param  last  End of input range.
00234    *  @param  result  Output to write sums to.
00235    *  @return  Iterator pointing just beyond the values written to result.
00236    */
00237   template<typename _InputIterator, typename _OutputIterator,
00238        typename _BinaryOperation>
00239     _OutputIterator
00240     partial_sum(_InputIterator __first, _InputIterator __last,
00241         _OutputIterator __result, _BinaryOperation __binary_op)
00242     {
00243       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
00244 
00245       // concept requirements
00246       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00247       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
00248                                          _ValueType>)
00249       __glibcxx_requires_valid_range(__first, __last);
00250 
00251       if (__first == __last)
00252     return __result;
00253       _ValueType __value = *__first;
00254       *__result = __value;
00255       while (++__first != __last)
00256     {
00257       __value = __binary_op(__value, *__first);
00258       *++__result = __value;
00259     }
00260       return ++__result;
00261     }
00262 
00263   /**
00264    *  @brief  Return differences between adjacent values.
00265    *
00266    *  Computes the difference between adjacent values in the range
00267    *  [first,last) using operator-() and writes the result to @a result.
00268    *
00269    *  @param  first  Start of input range.
00270    *  @param  last  End of input range.
00271    *  @param  result  Output to write sums to.
00272    *  @return  Iterator pointing just beyond the values written to result.
00273    */
00274   template<typename _InputIterator, typename _OutputIterator>
00275     _OutputIterator
00276     adjacent_difference(_InputIterator __first,
00277             _InputIterator __last, _OutputIterator __result)
00278     {
00279       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
00280 
00281       // concept requirements
00282       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00283       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
00284                                          _ValueType>)
00285       __glibcxx_requires_valid_range(__first, __last);
00286 
00287       if (__first == __last)
00288     return __result;
00289       _ValueType __value = *__first;
00290       *__result = __value;
00291       while (++__first != __last)
00292     {
00293       _ValueType __tmp = *__first;
00294       *++__result = __tmp - __value;
00295       __value = __tmp;
00296     }
00297       return ++__result;
00298     }
00299 
00300   /**
00301    *  @brief  Return differences between adjacent values.
00302    *
00303    *  Computes the difference between adjacent values in the range
00304    *  [first,last) using the function object @a binary_op and writes the
00305    *  result to @a result.
00306    *
00307    *  @param  first  Start of input range.
00308    *  @param  last  End of input range.
00309    *  @param  result  Output to write sums to.
00310    *  @return  Iterator pointing just beyond the values written to result.
00311    */
00312   template<typename _InputIterator, typename _OutputIterator,
00313        typename _BinaryOperation>
00314     _OutputIterator
00315     adjacent_difference(_InputIterator __first, _InputIterator __last,
00316             _OutputIterator __result, _BinaryOperation __binary_op)
00317     {
00318       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
00319 
00320       // concept requirements
00321       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00322       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
00323                                          _ValueType>)
00324       __glibcxx_requires_valid_range(__first, __last);
00325 
00326       if (__first == __last)
00327     return __result;
00328       _ValueType __value = *__first;
00329       *__result = __value;
00330       while (++__first != __last)
00331     {
00332       _ValueType __tmp = *__first;
00333       *++__result = __binary_op(__tmp, __value);
00334       __value = __tmp;
00335     }
00336       return ++__result;
00337     }
00338 
00339 _GLIBCXX_END_NESTED_NAMESPACE
00340 
00341 #endif /* _STL_NUMERIC_H */

Generated on Wed Mar 26 00:43:15 2008 for libstdc++ by  doxygen 1.5.1