]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/stl_numeric.h
stl_numeric.h (power + helpers, iota): Move to...
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_numeric.h
1 // Numeric functions implementation -*- C++ -*-
2
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.
10
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.
15
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.
20
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.
29
30 /*
31 *
32 * Copyright (c) 1994
33 * Hewlett-Packard Company
34 *
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.
42 *
43 *
44 * Copyright (c) 1996,1997
45 * Silicon Graphics Computer Systems, Inc.
46 *
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.
54 */
55
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.
59 */
60
61 #ifndef _CPP_BITS_STL_NUMERIC_H
62 #define _CPP_BITS_STL_NUMERIC_H 1
63
64 namespace std
65 {
66
67 template<typename _InputIterator, typename _Tp>
68 _Tp
69 accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
70 {
71 // concept requirements
72 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
73
74 for ( ; __first != __last; ++__first)
75 __init = __init + *__first;
76 return __init;
77 }
78
79 template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
80 _Tp
81 accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
82 _BinaryOperation __binary_op)
83 {
84 // concept requirements
85 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
86
87 for ( ; __first != __last; ++__first)
88 __init = __binary_op(__init, *__first);
89 return __init;
90 }
91
92 template<typename _InputIterator1, typename _InputIterator2, typename _Tp>
93 _Tp
94 inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
95 _InputIterator2 __first2, _Tp __init)
96 {
97 // concept requirements
98 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
99 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
100
101 for ( ; __first1 != __last1; ++__first1, ++__first2)
102 __init = __init + (*__first1 * *__first2);
103 return __init;
104 }
105
106 template<typename _InputIterator1, typename _InputIterator2, typename _Tp,
107 typename _BinaryOperation1, typename _BinaryOperation2>
108 _Tp
109 inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
110 _InputIterator2 __first2, _Tp __init,
111 _BinaryOperation1 __binary_op1,
112 _BinaryOperation2 __binary_op2)
113 {
114 // concept requirements
115 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
116 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
117
118 for ( ; __first1 != __last1; ++__first1, ++__first2)
119 __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
120 return __init;
121 }
122
123 template<typename _InputIterator, typename _OutputIterator>
124 _OutputIterator
125 partial_sum(_InputIterator __first, _InputIterator __last,
126 _OutputIterator __result)
127 {
128 typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
129
130 // concept requirements
131 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
132 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
133
134 if (__first == __last) return __result;
135 *__result = *__first;
136 _ValueType __value = *__first;
137 while (++__first != __last) {
138 __value = __value + *__first;
139 *++__result = __value;
140 }
141 return ++__result;
142 }
143
144 template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation>
145 _OutputIterator
146 partial_sum(_InputIterator __first, _InputIterator __last,
147 _OutputIterator __result, _BinaryOperation __binary_op)
148 {
149 typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
150
151 // concept requirements
152 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
153 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
154
155 if (__first == __last) return __result;
156 *__result = *__first;
157 _ValueType __value = *__first;
158 while (++__first != __last) {
159 __value = __binary_op(__value, *__first);
160 *++__result = __value;
161 }
162 return ++__result;
163 }
164
165 template<typename _InputIterator, typename _OutputIterator>
166 _OutputIterator
167 adjacent_difference(_InputIterator __first,
168 _InputIterator __last, _OutputIterator __result)
169 {
170 typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
171
172 // concept requirements
173 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
174 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
175
176 if (__first == __last) return __result;
177 *__result = *__first;
178 _ValueType __value = *__first;
179 while (++__first != __last) {
180 _ValueType __tmp = *__first;
181 *++__result = __tmp - __value;
182 __value = __tmp;
183 }
184 return ++__result;
185 }
186
187 template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation>
188 _OutputIterator
189 adjacent_difference(_InputIterator __first, _InputIterator __last,
190 _OutputIterator __result, _BinaryOperation __binary_op)
191 {
192 typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
193
194 // concept requirements
195 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
196 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
197
198 if (__first == __last) return __result;
199 *__result = *__first;
200 _ValueType __value = *__first;
201 while (++__first != __last) {
202 _ValueType __tmp = *__first;
203 *++__result = __binary_op(__tmp, __value);
204 __value = __tmp;
205 }
206 return ++__result;
207 }
208
209 } // namespace std
210
211 #endif /* _CPP_BITS_STL_NUMERIC_H */
212
213 // Local Variables:
214 // mode:C++
215 // End: