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 /** @file stl_numeric.h
00057  *  This is an internal header file, included by other library headers.
00058  *  You should not attempt to use it directly.
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<typename _InputIterator, typename _Tp>
00068     _Tp
00069     accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
00070     {
00071       // concept requirements
00072       __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
00073 
00074       for ( ; __first != __last; ++__first)
00075     __init = __init + *__first;
00076       return __init;
00077     }
00078 
00079   template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
00080     _Tp
00081     accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
00082            _BinaryOperation __binary_op)
00083     {
00084       // concept requirements
00085       __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
00086 
00087       for ( ; __first != __last; ++__first)
00088     __init = __binary_op(__init, *__first);
00089       return __init;
00090     }
00091 
00092   template<typename _InputIterator1, typename _InputIterator2, typename _Tp>
00093     _Tp
00094     inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
00095           _InputIterator2 __first2, _Tp __init)
00096     {
00097       // concept requirements
00098       __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
00099       __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
00100 
00101       for ( ; __first1 != __last1; ++__first1, ++__first2)
00102     __init = __init + (*__first1 * *__first2);
00103       return __init;
00104     }
00105 
00106   template<typename _InputIterator1, typename _InputIterator2, typename _Tp,
00107         typename _BinaryOperation1, typename _BinaryOperation2>
00108     _Tp
00109     inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
00110           _InputIterator2 __first2, _Tp __init, 
00111           _BinaryOperation1 __binary_op1,
00112           _BinaryOperation2 __binary_op2)
00113     {
00114       // concept requirements
00115       __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
00116       __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
00117 
00118       for ( ; __first1 != __last1; ++__first1, ++__first2)
00119     __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
00120       return __init;
00121     }
00122 
00123   template<typename _InputIterator, typename _OutputIterator>
00124     _OutputIterator 
00125     partial_sum(_InputIterator __first, _InputIterator __last,
00126         _OutputIterator __result)
00127     {
00128       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
00129 
00130       // concept requirements
00131       __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
00132       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
00133 
00134       if (__first == __last) return __result;
00135       *__result = *__first;
00136       _ValueType __value = *__first;
00137       while (++__first != __last) {
00138     __value = __value + *__first;
00139     *++__result = __value;
00140       }
00141       return ++__result;
00142     }
00143 
00144   template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation>
00145     _OutputIterator 
00146     partial_sum(_InputIterator __first, _InputIterator __last,
00147         _OutputIterator __result, _BinaryOperation __binary_op)
00148     {
00149       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
00150 
00151       // concept requirements
00152       __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
00153       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
00154 
00155       if (__first == __last) return __result;
00156       *__result = *__first;
00157       _ValueType __value = *__first;
00158       while (++__first != __last) {
00159     __value = __binary_op(__value, *__first);
00160     *++__result = __value;
00161       }
00162       return ++__result;
00163     }
00164 
00165   template<typename _InputIterator, typename _OutputIterator>
00166     _OutputIterator
00167     adjacent_difference(_InputIterator __first,
00168             _InputIterator __last, _OutputIterator __result)
00169     {
00170       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
00171 
00172       // concept requirements
00173       __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
00174       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
00175 
00176       if (__first == __last) return __result;
00177       *__result = *__first;
00178       _ValueType __value = *__first;
00179       while (++__first != __last) {
00180     _ValueType __tmp = *__first;
00181     *++__result = __tmp - __value;
00182     __value = __tmp;
00183       }
00184       return ++__result;
00185     }
00186 
00187   template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation>
00188     _OutputIterator 
00189     adjacent_difference(_InputIterator __first, _InputIterator __last,
00190             _OutputIterator __result, _BinaryOperation __binary_op)
00191     {
00192       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
00193 
00194       // concept requirements
00195       __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
00196       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
00197 
00198       if (__first == __last) return __result;
00199       *__result = *__first;
00200       _ValueType __value = *__first;
00201       while (++__first != __last) {
00202     _ValueType __tmp = *__first;
00203     *++__result = __binary_op(__tmp, __value);
00204     __value = __tmp;
00205       }
00206       return ++__result;
00207     }
00208 
00209 } // namespace std
00210 
00211 #endif /* _CPP_BITS_STL_NUMERIC_H */
00212 
00213 // Local Variables:
00214 // mode:C++
00215 // End:

Generated on Thu Feb 10 23:22:59 2005 for libstdc++-v3 Source by  doxygen 1.4.0