2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / include / bits / stl_numeric.h
blob7b901a54315081ca0f361a5aeee7847bf47791ce
1 // Numeric functions implementation -*- C++ -*-
3 // Copyright (C) 2001 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
32 * Copyright (c) 1994
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
44 * Copyright (c) 1996,1997
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
56 /** @file stl_numeric.h
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
61 #ifndef _STL_NUMERIC_H
62 #define _STL_NUMERIC_H 1
64 #include <debug/debug.h>
66 namespace std
69 template<typename _InputIterator, typename _Tp>
70 _Tp
71 accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
73 // concept requirements
74 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
75 __glibcxx_requires_valid_range(__first, __last);
77 for ( ; __first != __last; ++__first)
78 __init = __init + *__first;
79 return __init;
82 template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
83 _Tp
84 accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
85 _BinaryOperation __binary_op)
87 // concept requirements
88 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
89 __glibcxx_requires_valid_range(__first, __last);
91 for ( ; __first != __last; ++__first)
92 __init = __binary_op(__init, *__first);
93 return __init;
96 template<typename _InputIterator1, typename _InputIterator2, typename _Tp>
97 _Tp
98 inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
99 _InputIterator2 __first2, _Tp __init)
101 // concept requirements
102 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
103 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
104 __glibcxx_requires_valid_range(__first1, __last1);
106 for ( ; __first1 != __last1; ++__first1, ++__first2)
107 __init = __init + (*__first1 * *__first2);
108 return __init;
111 template<typename _InputIterator1, typename _InputIterator2, typename _Tp,
112 typename _BinaryOperation1, typename _BinaryOperation2>
114 inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
115 _InputIterator2 __first2, _Tp __init,
116 _BinaryOperation1 __binary_op1,
117 _BinaryOperation2 __binary_op2)
119 // concept requirements
120 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
121 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
122 __glibcxx_requires_valid_range(__first1, __last1);
124 for ( ; __first1 != __last1; ++__first1, ++__first2)
125 __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
126 return __init;
129 template<typename _InputIterator, typename _OutputIterator>
130 _OutputIterator
131 partial_sum(_InputIterator __first, _InputIterator __last,
132 _OutputIterator __result)
134 typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
136 // concept requirements
137 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
138 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
139 __glibcxx_requires_valid_range(__first, __last);
141 if (__first == __last) return __result;
142 *__result = *__first;
143 _ValueType __value = *__first;
144 while (++__first != __last) {
145 __value = __value + *__first;
146 *++__result = __value;
148 return ++__result;
151 template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation>
152 _OutputIterator
153 partial_sum(_InputIterator __first, _InputIterator __last,
154 _OutputIterator __result, _BinaryOperation __binary_op)
156 typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
158 // concept requirements
159 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
160 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
161 __glibcxx_requires_valid_range(__first, __last);
163 if (__first == __last) return __result;
164 *__result = *__first;
165 _ValueType __value = *__first;
166 while (++__first != __last) {
167 __value = __binary_op(__value, *__first);
168 *++__result = __value;
170 return ++__result;
173 template<typename _InputIterator, typename _OutputIterator>
174 _OutputIterator
175 adjacent_difference(_InputIterator __first,
176 _InputIterator __last, _OutputIterator __result)
178 typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
180 // concept requirements
181 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
182 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
183 __glibcxx_requires_valid_range(__first, __last);
185 if (__first == __last) return __result;
186 *__result = *__first;
187 _ValueType __value = *__first;
188 while (++__first != __last) {
189 _ValueType __tmp = *__first;
190 *++__result = __tmp - __value;
191 __value = __tmp;
193 return ++__result;
196 template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation>
197 _OutputIterator
198 adjacent_difference(_InputIterator __first, _InputIterator __last,
199 _OutputIterator __result, _BinaryOperation __binary_op)
201 typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
203 // concept requirements
204 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
205 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
206 __glibcxx_requires_valid_range(__first, __last);
208 if (__first == __last) return __result;
209 *__result = *__first;
210 _ValueType __value = *__first;
211 while (++__first != __last) {
212 _ValueType __tmp = *__first;
213 *++__result = __binary_op(__tmp, __value);
214 __value = __tmp;
216 return ++__result;
219 } // namespace std
221 #endif /* _STL_NUMERIC_H */