1 // Numeric functions implementation -*- C++ -*-
3 // Copyright (C) 2001 Free Software Foundation, Inc.
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)
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,
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.
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 /* NOTE: This is an internal header file, included by other STL headers.
57 * You should not attempt to use it directly.
61 #ifndef _CPP_BITS_STL_NUMERIC_H
62 #define _CPP_BITS_STL_NUMERIC_H 1
67 template<typename _InputIterator, typename _Tp>
69 accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
71 // concept requirements
72 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
74 for ( ; __first != __last; ++__first)
75 __init = __init + *__first;
79 template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
81 accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
82 _BinaryOperation __binary_op)
84 // concept requirements
85 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
87 for ( ; __first != __last; ++__first)
88 __init = __binary_op(__init, *__first);
92 template<typename _InputIterator1, typename _InputIterator2, typename _Tp>
94 inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
95 _InputIterator2 __first2, _Tp __init)
97 // concept requirements
98 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
99 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
101 for ( ; __first1 != __last1; ++__first1, ++__first2)
102 __init = __init + (*__first1 * *__first2);
106 template<typename _InputIterator1, typename _InputIterator2, typename _Tp,
107 typename _BinaryOperation1, typename _BinaryOperation2>
109 inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
110 _InputIterator2 __first2, _Tp __init,
111 _BinaryOperation1 __binary_op1,
112 _BinaryOperation2 __binary_op2)
114 // concept requirements
115 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
116 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
118 for ( ; __first1 != __last1; ++__first1, ++__first2)
119 __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
123 template<typename _InputIterator, typename _OutputIterator>
125 partial_sum(_InputIterator __first, _InputIterator __last,
126 _OutputIterator __result)
128 typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
130 // concept requirements
131 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
132 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
134 if (__first == __last) return __result;
135 *__result = *__first;
136 _ValueType __value = *__first;
137 while (++__first != __last) {
138 __value = __value + *__first;
139 *++__result = __value;
144 template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation>
146 partial_sum(_InputIterator __first, _InputIterator __last,
147 _OutputIterator __result, _BinaryOperation __binary_op)
149 typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
151 // concept requirements
152 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
153 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
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;
165 template<typename _InputIterator, typename _OutputIterator>
167 adjacent_difference(_InputIterator __first,
168 _InputIterator __last, _OutputIterator __result)
170 typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
172 // concept requirements
173 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
174 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
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;
187 template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation>
189 adjacent_difference(_InputIterator __first, _InputIterator __last,
190 _OutputIterator __result, _BinaryOperation __binary_op)
192 typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
194 // concept requirements
195 __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
196 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
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);
209 // Returns __x ** __n, where __n >= 0. _Note that "multiplication"
210 // is required to be associative, but not necessarily commutative.
213 template<typename _Tp, typename _Integer, typename _MonoidOperation>
215 __power(_Tp __x, _Integer __n, _MonoidOperation __monoid_op)
218 return identity_element(__monoid_op);
220 while ((__n & 1) == 0) {
222 __x = __monoid_op(__x, __x);
228 __x = __monoid_op(__x, __x);
230 __result = __monoid_op(__result, __x);
237 template<typename _Tp, typename _Integer>
239 __power(_Tp __x, _Integer __n)
240 { return __power(__x, __n, multiplies<_Tp>()); }
242 // Alias for the internal name __power. Note that power is an extension,
243 // not part of the C++ standard.
245 template<typename _Tp, typename _Integer, typename _MonoidOperation>
247 power(_Tp __x, _Integer __n, _MonoidOperation __monoid_op)
248 { return __power(__x, __n, __monoid_op); }
250 template<typename _Tp, typename _Integer>
252 power(_Tp __x, _Integer __n)
253 { return __power(__x, __n); }
255 // iota is not part of the C++ standard. It is an extension.
257 template<typename _ForwardIter, typename _Tp>
259 iota(_ForwardIter __first, _ForwardIter __last, _Tp __value)
261 // concept requirements
262 __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter>)
263 __glibcpp_function_requires(_ConvertibleConcept<_Tp,
264 typename iterator_traits<_ForwardIter>::value_type>)
266 while (__first != __last)
267 *__first++ = __value++;
272 #endif /* _CPP_BITS_STL_NUMERIC_H */