stl_numeric.h

Go to the documentation of this file.
00001 // Numeric functions implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2004 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 
00061 #ifndef _STL_NUMERIC_H
00062 #define _STL_NUMERIC_H 1
00063 
00064 #include <debug/debug.h>
00065 
00066 namespace std
00067 {
00068 
00080   template<typename _InputIterator, typename _Tp>
00081     _Tp
00082     accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
00083     {
00084       // concept requirements
00085       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00086       __glibcxx_requires_valid_range(__first, __last);
00087 
00088       for ( ; __first != __last; ++__first)
00089     __init = __init + *__first;
00090       return __init;
00091     }
00092 
00106   template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
00107     _Tp
00108     accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
00109            _BinaryOperation __binary_op)
00110     {
00111       // concept requirements
00112       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00113       __glibcxx_requires_valid_range(__first, __last);
00114 
00115       for ( ; __first != __last; ++__first)
00116     __init = __binary_op(__init, *__first);
00117       return __init;
00118     }
00119 
00134   template<typename _InputIterator1, typename _InputIterator2, typename _Tp>
00135     _Tp
00136     inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
00137           _InputIterator2 __first2, _Tp __init)
00138     {
00139       // concept requirements
00140       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
00141       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
00142       __glibcxx_requires_valid_range(__first1, __last1);
00143 
00144       for ( ; __first1 != __last1; ++__first1, ++__first2)
00145     __init = __init + (*__first1 * *__first2);
00146       return __init;
00147     }
00148 
00165   template<typename _InputIterator1, typename _InputIterator2, typename _Tp,
00166         typename _BinaryOperation1, typename _BinaryOperation2>
00167     _Tp
00168     inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
00169           _InputIterator2 __first2, _Tp __init,
00170           _BinaryOperation1 __binary_op1,
00171           _BinaryOperation2 __binary_op2)
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 = __binary_op1(__init, __binary_op2(*__first1, *__first2));
00180       return __init;
00181     }
00182 
00197   template<typename _InputIterator, typename _OutputIterator>
00198     _OutputIterator
00199     partial_sum(_InputIterator __first, _InputIterator __last,
00200         _OutputIterator __result)
00201     {
00202       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
00203 
00204       // concept requirements
00205       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00206       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
00207       __glibcxx_requires_valid_range(__first, __last);
00208 
00209       if (__first == __last) return __result;
00210       *__result = *__first;
00211       _ValueType __value = *__first;
00212       while (++__first != __last) {
00213     __value = __value + *__first;
00214     *++__result = __value;
00215       }
00216       return ++__result;
00217     }
00218 
00233   template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation>
00234     _OutputIterator
00235     partial_sum(_InputIterator __first, _InputIterator __last,
00236         _OutputIterator __result, _BinaryOperation __binary_op)
00237     {
00238       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
00239 
00240       // concept requirements
00241       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00242       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
00243       __glibcxx_requires_valid_range(__first, __last);
00244 
00245       if (__first == __last) return __result;
00246       *__result = *__first;
00247       _ValueType __value = *__first;
00248       while (++__first != __last) {
00249     __value = __binary_op(__value, *__first);
00250     *++__result = __value;
00251       }
00252       return ++__result;
00253     }
00254 
00266   template<typename _InputIterator, typename _OutputIterator>
00267     _OutputIterator
00268     adjacent_difference(_InputIterator __first,
00269             _InputIterator __last, _OutputIterator __result)
00270     {
00271       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
00272 
00273       // concept requirements
00274       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00275       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
00276       __glibcxx_requires_valid_range(__first, __last);
00277 
00278       if (__first == __last) return __result;
00279       *__result = *__first;
00280       _ValueType __value = *__first;
00281       while (++__first != __last) {
00282     _ValueType __tmp = *__first;
00283     *++__result = __tmp - __value;
00284     __value = __tmp;
00285       }
00286       return ++__result;
00287     }
00288 
00301   template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation>
00302     _OutputIterator
00303     adjacent_difference(_InputIterator __first, _InputIterator __last,
00304             _OutputIterator __result, _BinaryOperation __binary_op)
00305     {
00306       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
00307 
00308       // concept requirements
00309       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00310       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
00311       __glibcxx_requires_valid_range(__first, __last);
00312 
00313       if (__first == __last) return __result;
00314       *__result = *__first;
00315       _ValueType __value = *__first;
00316       while (++__first != __last) {
00317     _ValueType __tmp = *__first;
00318     *++__result = __binary_op(__tmp, __value);
00319     __value = __tmp;
00320       }
00321       return ++__result;
00322     }
00323 
00324 } // namespace std
00325 
00326 #endif /* _STL_NUMERIC_H */

Generated on Tue Jan 30 17:31:54 2007 for GNU C++ STL by doxygen 1.3.6