# 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, 2008, 2009
    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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
    00018 // permissions described in the GCC Runtime Library Exception, version
    00019 // 3.1, as published by the Free Software Foundation.
    00020
    00021 // You should have received a copy of the GNU General Public License and
    00022 // a copy of the GCC Runtime Library Exception along with this program;
    00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
    00024 // <http://www.gnu.org/licenses/>.
    00025
    00026 /*
    00027  *
    00028  * Copyright (c) 1994
    00029  * Hewlett-Packard Company
    00030  *
    00031  * Permission to use, copy, modify, distribute and sell this software
    00032  * and its documentation for any purpose is hereby granted without fee,
    00033  * provided that the above copyright notice appear in all copies and
    00034  * that both that copyright notice and this permission notice appear
    00035  * in supporting documentation.  Hewlett-Packard Company makes no
    00036  * representations about the suitability of this software for any
    00037  * purpose.  It is provided "as is" without express or implied warranty.
    00038  *
    00039  *
    00040  * Copyright (c) 1996,1997
    00041  * Silicon Graphics Computer Systems, Inc.
    00042  *
    00043  * Permission to use, copy, modify, distribute and sell this software
    00044  * and its documentation for any purpose is hereby granted without fee,
    00045  * provided that the above copyright notice appear in all copies and
    00046  * that both that copyright notice and this permission notice appear
    00047  * in supporting documentation.  Silicon Graphics makes no
    00048  * representations about the suitability of this software for any
    00049  * purpose.  It is provided "as is" without express or implied warranty.
    00050  */
    00051
    00052 /** @file stl_numeric.h
    00053  *  This is an internal header file, included by other library headers.
    00054  *  You should not attempt to use it directly.
    00055  */
    00056
    00057 #ifndef _STL_NUMERIC_H
    00058 #define _STL_NUMERIC_H 1
    00059
    00060 #include <bits/concept_check.h>
    00061 #include <debug/debug.h>
    00062 #include <bits/move.h> // For _GLIBCXX_MOVE
    00063
    00064 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00065
    00066 _GLIBCXX_BEGIN_NAMESPACE(std)
    00067
    00068   /**
    00069    *  @brief  Create a range of sequentially increasing values.
    00070    *
    00071    *  For each element in the range @p [first,last) assigns @p value and
    00072    *  increments @p value as if by @p ++value.
    00073    *
    00074    *  @param  first  Start of range.
    00075    *  @param  last  End of range.
    00076    *  @param  value  Starting value.
    00077    *  @return  Nothing.
    00078    */
    00079   template<typename _ForwardIterator, typename _Tp>
    00080     void
    00081     iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value)
    00082     {
    00083       // concept requirements
    00084       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
    00085                   _ForwardIterator>)
    00086       __glibcxx_function_requires(_ConvertibleConcept<_Tp,
    00087         typename iterator_traits<_ForwardIterator>::value_type>)
    00088       __glibcxx_requires_valid_range(__first, __last);
    00089
    00090       for (; __first != __last; ++__first)
    00091     {
    00092       *__first = __value;
    00093       ++__value;
    00094     }
    00095     }
    00096
    00097 _GLIBCXX_END_NAMESPACE
    00098
    00099 #endif
    00100
    00101 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
    00102
    00103   /**
    00104    *  @brief  Accumulate values in a range.
    00105    *
    00106    *  Accumulates the values in the range [first,last) using operator+().  The
    00107    *  initial value is @a init.  The values are processed in order.
    00108    *
    00109    *  @param  first  Start of range.
    00110    *  @param  last  End of range.
    00111    *  @param  init  Starting value to add other values to.
    00112    *  @return  The final sum.
    00113    */
    00114   template<typename _InputIterator, typename _Tp>
    00115     inline _Tp
    00116     accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
    00117     {
    00118       // concept requirements
    00119       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
    00120       __glibcxx_requires_valid_range(__first, __last);
    00121
    00122       for (; __first != __last; ++__first)
    00123     __init = __init + *__first;
    00124       return __init;
    00125     }
    00126
    00127   /**
    00128    *  @brief  Accumulate values in a range with operation.
    00129    *
    00130    *  Accumulates the values in the range [first,last) using the function
    00131    *  object @a binary_op.  The initial value is @a init.  The values are
    00132    *  processed in order.
    00133    *
    00134    *  @param  first  Start of range.
    00135    *  @param  last  End of range.
    00136    *  @param  init  Starting value to add other values to.
    00137    *  @param  binary_op  Function object to accumulate with.
    00138    *  @return  The final sum.
    00139    */
    00140   template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
    00141     inline _Tp
    00142     accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
    00143            _BinaryOperation __binary_op)
    00144     {
    00145       // concept requirements
    00146       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
    00147       __glibcxx_requires_valid_range(__first, __last);
    00148
    00149       for (; __first != __last; ++__first)
    00150     __init = __binary_op(__init, *__first);
    00151       return __init;
    00152     }
    00153
    00154   /**
    00155    *  @brief  Compute inner product of two ranges.
    00156    *
    00157    *  Starting with an initial value of @a init, multiplies successive
    00158    *  elements from the two ranges and adds each product into the accumulated
    00159    *  value using operator+().  The values in the ranges are processed in
    00160    *  order.
    00161    *
    00162    *  @param  first1  Start of range 1.
    00163    *  @param  last1  End of range 1.
    00164    *  @param  first2  Start of range 2.
    00165    *  @param  init  Starting value to add other values to.
    00166    *  @return  The final inner product.
    00167    */
    00168   template<typename _InputIterator1, typename _InputIterator2, typename _Tp>
    00169     inline _Tp
    00170     inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
    00171           _InputIterator2 __first2, _Tp __init)
    00172     {
    00173       // concept requirements
    00174       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
    00175       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
    00176       __glibcxx_requires_valid_range(__first1, __last1);
    00177
    00178       for (; __first1 != __last1; ++__first1, ++__first2)
    00179     __init = __init + (*__first1 * *__first2);
    00180       return __init;
    00181     }
    00182
    00183   /**
    00184    *  @brief  Compute inner product of two ranges.
    00185    *
    00186    *  Starting with an initial value of @a init, applies @a binary_op2 to
    00187    *  successive elements from the two ranges and accumulates each result into
    00188    *  the accumulated value using @a binary_op1.  The values in the ranges are
    00189    *  processed in order.
    00190    *
    00191    *  @param  first1  Start of range 1.
    00192    *  @param  last1  End of range 1.
    00193    *  @param  first2  Start of range 2.
    00194    *  @param  init  Starting value to add other values to.
    00195    *  @param  binary_op1  Function object to accumulate with.
    00196    *  @param  binary_op2  Function object to apply to pairs of input values.
    00197    *  @return  The final inner product.
    00198    */
    00199   template<typename _InputIterator1, typename _InputIterator2, typename _Tp,
    00200        typename _BinaryOperation1, typename _BinaryOperation2>
    00201     inline _Tp
    00202     inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
    00203           _InputIterator2 __first2, _Tp __init,
    00204           _BinaryOperation1 __binary_op1,
    00205           _BinaryOperation2 __binary_op2)
    00206     {
    00207       // concept requirements
    00208       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
    00209       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
    00210       __glibcxx_requires_valid_range(__first1, __last1);
    00211
    00212       for (; __first1 != __last1; ++__first1, ++__first2)
    00213     __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
    00214       return __init;
    00215     }
    00216
    00217   /**
    00218    *  @brief  Return list of partial sums
    00219    *
    00220    *  Accumulates the values in the range [first,last) using operator+().
    00221    *  As each successive input value is added into the total, that partial sum
    00222    *  is written to @a result.  Therefore, the first value in result is the
    00223    *  first value of the input, the second value in result is the sum of the
    00224    *  first and second input values, and so on.
    00225    *
    00226    *  @param  first  Start of input range.
    00227    *  @param  last  End of input range.
    00228    *  @param  result  Output to write sums to.
    00229    *  @return  Iterator pointing just beyond the values written to result.
    00230    */
    00231   template<typename _InputIterator, typename _OutputIterator>
    00232     _OutputIterator
    00233     partial_sum(_InputIterator __first, _InputIterator __last,
    00234         _OutputIterator __result)
    00235     {
    00236       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
    00237
    00238       // concept requirements
    00239       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
    00240       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
    00241                                          _ValueType>)
    00242       __glibcxx_requires_valid_range(__first, __last);
    00243
    00244       if (__first == __last)
    00245     return __result;
    00246       _ValueType __value = *__first;
    00247       *__result = __value;
    00248       while (++__first != __last)
    00249     {
    00250       __value = __value + *__first;
    00251       *++__result = __value;
    00252     }
    00253       return ++__result;
    00254     }
    00255
    00256   /**
    00257    *  @brief  Return list of partial sums
    00258    *
    00259    *  Accumulates the values in the range [first,last) using operator+().
    00260    *  As each successive input value is added into the total, that partial sum
    00261    *  is written to @a result.  Therefore, the first value in result is the
    00262    *  first value of the input, the second value in result is the sum of the
    00263    *  first and second input values, and so on.
    00264    *
    00265    *  @param  first  Start of input range.
    00266    *  @param  last  End of input range.
    00267    *  @param  result  Output to write sums to.
    00268    *  @return  Iterator pointing just beyond the values written to result.
    00269    */
    00270   template<typename _InputIterator, typename _OutputIterator,
    00271        typename _BinaryOperation>
    00272     _OutputIterator
    00273     partial_sum(_InputIterator __first, _InputIterator __last,
    00274         _OutputIterator __result, _BinaryOperation __binary_op)
    00275     {
    00276       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
    00277
    00278       // concept requirements
    00279       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
    00280       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
    00281                                          _ValueType>)
    00282       __glibcxx_requires_valid_range(__first, __last);
    00283
    00284       if (__first == __last)
    00285     return __result;
    00286       _ValueType __value = *__first;
    00287       *__result = __value;
    00288       while (++__first != __last)
    00289     {
    00290       __value = __binary_op(__value, *__first);
    00291       *++__result = __value;
    00292     }
    00293       return ++__result;
    00294     }
    00295
    00296   /**
    00297    *  @brief  Return differences between adjacent values.
    00298    *
    00299    *  Computes the difference between adjacent values in the range
    00300    *  [first,last) using operator-() and writes the result to @a result.
    00301    *
    00302    *  @param  first  Start of input range.
    00303    *  @param  last  End of input range.
    00304    *  @param  result  Output to write sums to.
    00305    *  @return  Iterator pointing just beyond the values written to result.
    00306    *
    00307    *  _GLIBCXX_RESOLVE_LIB_DEFECTS
    00308    *  DR 539. partial_sum and adjacent_difference should mention requirements
    00309    */
    00310   template<typename _InputIterator, typename _OutputIterator>
    00311     _OutputIterator
    00312     adjacent_difference(_InputIterator __first,
    00313             _InputIterator __last, _OutputIterator __result)
    00314     {
    00315       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
    00316
    00317       // concept requirements
    00318       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
    00319       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
    00320                                          _ValueType>)
    00321       __glibcxx_requires_valid_range(__first, __last);
    00322
    00323       if (__first == __last)
    00324     return __result;
    00325       _ValueType __value = *__first;
    00326       *__result = __value;
    00327       while (++__first != __last)
    00328     {
    00329       _ValueType __tmp = *__first;
    00330       *++__result = __tmp - __value;
    00331       __value = _GLIBCXX_MOVE(__tmp);
    00332     }
    00333       return ++__result;
    00334     }
    00335
    00336   /**
    00337    *  @brief  Return differences between adjacent values.
    00338    *
    00339    *  Computes the difference between adjacent values in the range
    00340    *  [first,last) using the function object @a binary_op and writes the
    00341    *  result to @a result.
    00342    *
    00343    *  @param  first  Start of input range.
    00344    *  @param  last  End of input range.
    00345    *  @param  result  Output to write sums to.
    00346    *  @return  Iterator pointing just beyond the values written to result.
    00347    *
    00348    *  _GLIBCXX_RESOLVE_LIB_DEFECTS
    00349    *  DR 539. partial_sum and adjacent_difference should mention requirements
    00350    */
    00351   template<typename _InputIterator, typename _OutputIterator,
    00352        typename _BinaryOperation>
    00353     _OutputIterator
    00354     adjacent_difference(_InputIterator __first, _InputIterator __last,
    00355             _OutputIterator __result, _BinaryOperation __binary_op)
    00356     {
    00357       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
    00358
    00359       // concept requirements
    00360       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
    00361       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
    00362                                          _ValueType>)
    00363       __glibcxx_requires_valid_range(__first, __last);
    00364
    00365       if (__first == __last)
    00366     return __result;
    00367       _ValueType __value = *__first;
    00368       *__result = __value;
    00369       while (++__first != __last)
    00370     {
    00371       _ValueType __tmp = *__first;
    00372       *++__result = __binary_op(__tmp, __value);
    00373       __value = _GLIBCXX_MOVE(__tmp);
    00374     }
    00375       return ++__result;
    00376     }
    00377
    00378 _GLIBCXX_END_NESTED_NAMESPACE
    00379
    00380 #endif /* _STL_NUMERIC_H */