1*38fd1498Szrj // Numeric functions implementation -*- C++ -*-
2*38fd1498Szrj
3*38fd1498Szrj // Copyright (C) 2001-2018 Free Software Foundation, Inc.
4*38fd1498Szrj //
5*38fd1498Szrj // This file is part of the GNU ISO C++ Library. This library is free
6*38fd1498Szrj // software; you can redistribute it and/or modify it under the
7*38fd1498Szrj // terms of the GNU General Public License as published by the
8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj // any later version.
10*38fd1498Szrj
11*38fd1498Szrj // This library is distributed in the hope that it will be useful,
12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*38fd1498Szrj // GNU General Public License for more details.
15*38fd1498Szrj
16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj
20*38fd1498Szrj // You should have received a copy of the GNU General Public License and
21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23*38fd1498Szrj // <http://www.gnu.org/licenses/>.
24*38fd1498Szrj
25*38fd1498Szrj /*
26*38fd1498Szrj *
27*38fd1498Szrj * Copyright (c) 1994
28*38fd1498Szrj * Hewlett-Packard Company
29*38fd1498Szrj *
30*38fd1498Szrj * Permission to use, copy, modify, distribute and sell this software
31*38fd1498Szrj * and its documentation for any purpose is hereby granted without fee,
32*38fd1498Szrj * provided that the above copyright notice appear in all copies and
33*38fd1498Szrj * that both that copyright notice and this permission notice appear
34*38fd1498Szrj * in supporting documentation. Hewlett-Packard Company makes no
35*38fd1498Szrj * representations about the suitability of this software for any
36*38fd1498Szrj * purpose. It is provided "as is" without express or implied warranty.
37*38fd1498Szrj *
38*38fd1498Szrj *
39*38fd1498Szrj * Copyright (c) 1996,1997
40*38fd1498Szrj * Silicon Graphics Computer Systems, Inc.
41*38fd1498Szrj *
42*38fd1498Szrj * Permission to use, copy, modify, distribute and sell this software
43*38fd1498Szrj * and its documentation for any purpose is hereby granted without fee,
44*38fd1498Szrj * provided that the above copyright notice appear in all copies and
45*38fd1498Szrj * that both that copyright notice and this permission notice appear
46*38fd1498Szrj * in supporting documentation. Silicon Graphics makes no
47*38fd1498Szrj * representations about the suitability of this software for any
48*38fd1498Szrj * purpose. It is provided "as is" without express or implied warranty.
49*38fd1498Szrj */
50*38fd1498Szrj
51*38fd1498Szrj /** @file bits/stl_numeric.h
52*38fd1498Szrj * This is an internal header file, included by other library headers.
53*38fd1498Szrj * Do not attempt to use it directly. @headername{numeric}
54*38fd1498Szrj */
55*38fd1498Szrj
56*38fd1498Szrj #ifndef _STL_NUMERIC_H
57*38fd1498Szrj #define _STL_NUMERIC_H 1
58*38fd1498Szrj
59*38fd1498Szrj #include <bits/concept_check.h>
60*38fd1498Szrj #include <debug/debug.h>
61*38fd1498Szrj #include <bits/move.h> // For _GLIBCXX_MOVE
62*38fd1498Szrj
63*38fd1498Szrj #if __cplusplus >= 201103L
64*38fd1498Szrj
_GLIBCXX_VISIBILITY(default)65*38fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default)
66*38fd1498Szrj {
67*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION
68*38fd1498Szrj
69*38fd1498Szrj /**
70*38fd1498Szrj * @brief Create a range of sequentially increasing values.
71*38fd1498Szrj *
72*38fd1498Szrj * For each element in the range @p [first,last) assigns @p value and
73*38fd1498Szrj * increments @p value as if by @p ++value.
74*38fd1498Szrj *
75*38fd1498Szrj * @param __first Start of range.
76*38fd1498Szrj * @param __last End of range.
77*38fd1498Szrj * @param __value Starting value.
78*38fd1498Szrj * @return Nothing.
79*38fd1498Szrj */
80*38fd1498Szrj template<typename _ForwardIterator, typename _Tp>
81*38fd1498Szrj void
82*38fd1498Szrj iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value)
83*38fd1498Szrj {
84*38fd1498Szrj // concept requirements
85*38fd1498Szrj __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
86*38fd1498Szrj _ForwardIterator>)
87*38fd1498Szrj __glibcxx_function_requires(_ConvertibleConcept<_Tp,
88*38fd1498Szrj typename iterator_traits<_ForwardIterator>::value_type>)
89*38fd1498Szrj __glibcxx_requires_valid_range(__first, __last);
90*38fd1498Szrj
91*38fd1498Szrj for (; __first != __last; ++__first)
92*38fd1498Szrj {
93*38fd1498Szrj *__first = __value;
94*38fd1498Szrj ++__value;
95*38fd1498Szrj }
96*38fd1498Szrj }
97*38fd1498Szrj
98*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
99*38fd1498Szrj } // namespace std
100*38fd1498Szrj
101*38fd1498Szrj #endif
102*38fd1498Szrj
_GLIBCXX_VISIBILITY(default)103*38fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default)
104*38fd1498Szrj {
105*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_ALGO
106*38fd1498Szrj
107*38fd1498Szrj /**
108*38fd1498Szrj * @brief Accumulate values in a range.
109*38fd1498Szrj *
110*38fd1498Szrj * Accumulates the values in the range [first,last) using operator+(). The
111*38fd1498Szrj * initial value is @a init. The values are processed in order.
112*38fd1498Szrj *
113*38fd1498Szrj * @param __first Start of range.
114*38fd1498Szrj * @param __last End of range.
115*38fd1498Szrj * @param __init Starting value to add other values to.
116*38fd1498Szrj * @return The final sum.
117*38fd1498Szrj */
118*38fd1498Szrj template<typename _InputIterator, typename _Tp>
119*38fd1498Szrj inline _Tp
120*38fd1498Szrj accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
121*38fd1498Szrj {
122*38fd1498Szrj // concept requirements
123*38fd1498Szrj __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
124*38fd1498Szrj __glibcxx_requires_valid_range(__first, __last);
125*38fd1498Szrj
126*38fd1498Szrj for (; __first != __last; ++__first)
127*38fd1498Szrj __init = __init + *__first;
128*38fd1498Szrj return __init;
129*38fd1498Szrj }
130*38fd1498Szrj
131*38fd1498Szrj /**
132*38fd1498Szrj * @brief Accumulate values in a range with operation.
133*38fd1498Szrj *
134*38fd1498Szrj * Accumulates the values in the range [first,last) using the function
135*38fd1498Szrj * object @p __binary_op. The initial value is @p __init. The values are
136*38fd1498Szrj * processed in order.
137*38fd1498Szrj *
138*38fd1498Szrj * @param __first Start of range.
139*38fd1498Szrj * @param __last End of range.
140*38fd1498Szrj * @param __init Starting value to add other values to.
141*38fd1498Szrj * @param __binary_op Function object to accumulate with.
142*38fd1498Szrj * @return The final sum.
143*38fd1498Szrj */
144*38fd1498Szrj template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
145*38fd1498Szrj inline _Tp
146*38fd1498Szrj accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
147*38fd1498Szrj _BinaryOperation __binary_op)
148*38fd1498Szrj {
149*38fd1498Szrj // concept requirements
150*38fd1498Szrj __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
151*38fd1498Szrj __glibcxx_requires_valid_range(__first, __last);
152*38fd1498Szrj
153*38fd1498Szrj for (; __first != __last; ++__first)
154*38fd1498Szrj __init = __binary_op(__init, *__first);
155*38fd1498Szrj return __init;
156*38fd1498Szrj }
157*38fd1498Szrj
158*38fd1498Szrj /**
159*38fd1498Szrj * @brief Compute inner product of two ranges.
160*38fd1498Szrj *
161*38fd1498Szrj * Starting with an initial value of @p __init, multiplies successive
162*38fd1498Szrj * elements from the two ranges and adds each product into the accumulated
163*38fd1498Szrj * value using operator+(). The values in the ranges are processed in
164*38fd1498Szrj * order.
165*38fd1498Szrj *
166*38fd1498Szrj * @param __first1 Start of range 1.
167*38fd1498Szrj * @param __last1 End of range 1.
168*38fd1498Szrj * @param __first2 Start of range 2.
169*38fd1498Szrj * @param __init Starting value to add other values to.
170*38fd1498Szrj * @return The final inner product.
171*38fd1498Szrj */
172*38fd1498Szrj template<typename _InputIterator1, typename _InputIterator2, typename _Tp>
173*38fd1498Szrj inline _Tp
174*38fd1498Szrj inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
175*38fd1498Szrj _InputIterator2 __first2, _Tp __init)
176*38fd1498Szrj {
177*38fd1498Szrj // concept requirements
178*38fd1498Szrj __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
179*38fd1498Szrj __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
180*38fd1498Szrj __glibcxx_requires_valid_range(__first1, __last1);
181*38fd1498Szrj
182*38fd1498Szrj for (; __first1 != __last1; ++__first1, (void)++__first2)
183*38fd1498Szrj __init = __init + (*__first1 * *__first2);
184*38fd1498Szrj return __init;
185*38fd1498Szrj }
186*38fd1498Szrj
187*38fd1498Szrj /**
188*38fd1498Szrj * @brief Compute inner product of two ranges.
189*38fd1498Szrj *
190*38fd1498Szrj * Starting with an initial value of @p __init, applies @p __binary_op2 to
191*38fd1498Szrj * successive elements from the two ranges and accumulates each result into
192*38fd1498Szrj * the accumulated value using @p __binary_op1. The values in the ranges are
193*38fd1498Szrj * processed in order.
194*38fd1498Szrj *
195*38fd1498Szrj * @param __first1 Start of range 1.
196*38fd1498Szrj * @param __last1 End of range 1.
197*38fd1498Szrj * @param __first2 Start of range 2.
198*38fd1498Szrj * @param __init Starting value to add other values to.
199*38fd1498Szrj * @param __binary_op1 Function object to accumulate with.
200*38fd1498Szrj * @param __binary_op2 Function object to apply to pairs of input values.
201*38fd1498Szrj * @return The final inner product.
202*38fd1498Szrj */
203*38fd1498Szrj template<typename _InputIterator1, typename _InputIterator2, typename _Tp,
204*38fd1498Szrj typename _BinaryOperation1, typename _BinaryOperation2>
205*38fd1498Szrj inline _Tp
206*38fd1498Szrj inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
207*38fd1498Szrj _InputIterator2 __first2, _Tp __init,
208*38fd1498Szrj _BinaryOperation1 __binary_op1,
209*38fd1498Szrj _BinaryOperation2 __binary_op2)
210*38fd1498Szrj {
211*38fd1498Szrj // concept requirements
212*38fd1498Szrj __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
213*38fd1498Szrj __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
214*38fd1498Szrj __glibcxx_requires_valid_range(__first1, __last1);
215*38fd1498Szrj
216*38fd1498Szrj for (; __first1 != __last1; ++__first1, (void)++__first2)
217*38fd1498Szrj __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
218*38fd1498Szrj return __init;
219*38fd1498Szrj }
220*38fd1498Szrj
221*38fd1498Szrj /**
222*38fd1498Szrj * @brief Return list of partial sums
223*38fd1498Szrj *
224*38fd1498Szrj * Accumulates the values in the range [first,last) using the @c + operator.
225*38fd1498Szrj * As each successive input value is added into the total, that partial sum
226*38fd1498Szrj * is written to @p __result. Therefore, the first value in @p __result is
227*38fd1498Szrj * the first value of the input, the second value in @p __result is the sum
228*38fd1498Szrj * of the first and second input values, and so on.
229*38fd1498Szrj *
230*38fd1498Szrj * @param __first Start of input range.
231*38fd1498Szrj * @param __last End of input range.
232*38fd1498Szrj * @param __result Output sum.
233*38fd1498Szrj * @return Iterator pointing just beyond the values written to __result.
234*38fd1498Szrj */
235*38fd1498Szrj template<typename _InputIterator, typename _OutputIterator>
236*38fd1498Szrj _OutputIterator
237*38fd1498Szrj partial_sum(_InputIterator __first, _InputIterator __last,
238*38fd1498Szrj _OutputIterator __result)
239*38fd1498Szrj {
240*38fd1498Szrj typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
241*38fd1498Szrj
242*38fd1498Szrj // concept requirements
243*38fd1498Szrj __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
244*38fd1498Szrj __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
245*38fd1498Szrj _ValueType>)
246*38fd1498Szrj __glibcxx_requires_valid_range(__first, __last);
247*38fd1498Szrj
248*38fd1498Szrj if (__first == __last)
249*38fd1498Szrj return __result;
250*38fd1498Szrj _ValueType __value = *__first;
251*38fd1498Szrj *__result = __value;
252*38fd1498Szrj while (++__first != __last)
253*38fd1498Szrj {
254*38fd1498Szrj __value = __value + *__first;
255*38fd1498Szrj *++__result = __value;
256*38fd1498Szrj }
257*38fd1498Szrj return ++__result;
258*38fd1498Szrj }
259*38fd1498Szrj
260*38fd1498Szrj /**
261*38fd1498Szrj * @brief Return list of partial sums
262*38fd1498Szrj *
263*38fd1498Szrj * Accumulates the values in the range [first,last) using @p __binary_op.
264*38fd1498Szrj * As each successive input value is added into the total, that partial sum
265*38fd1498Szrj * is written to @p __result. Therefore, the first value in @p __result is
266*38fd1498Szrj * the first value of the input, the second value in @p __result is the sum
267*38fd1498Szrj * of the first and second input values, and so on.
268*38fd1498Szrj *
269*38fd1498Szrj * @param __first Start of input range.
270*38fd1498Szrj * @param __last End of input range.
271*38fd1498Szrj * @param __result Output sum.
272*38fd1498Szrj * @param __binary_op Function object.
273*38fd1498Szrj * @return Iterator pointing just beyond the values written to __result.
274*38fd1498Szrj */
275*38fd1498Szrj template<typename _InputIterator, typename _OutputIterator,
276*38fd1498Szrj typename _BinaryOperation>
277*38fd1498Szrj _OutputIterator
278*38fd1498Szrj partial_sum(_InputIterator __first, _InputIterator __last,
279*38fd1498Szrj _OutputIterator __result, _BinaryOperation __binary_op)
280*38fd1498Szrj {
281*38fd1498Szrj typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
282*38fd1498Szrj
283*38fd1498Szrj // concept requirements
284*38fd1498Szrj __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
285*38fd1498Szrj __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
286*38fd1498Szrj _ValueType>)
287*38fd1498Szrj __glibcxx_requires_valid_range(__first, __last);
288*38fd1498Szrj
289*38fd1498Szrj if (__first == __last)
290*38fd1498Szrj return __result;
291*38fd1498Szrj _ValueType __value = *__first;
292*38fd1498Szrj *__result = __value;
293*38fd1498Szrj while (++__first != __last)
294*38fd1498Szrj {
295*38fd1498Szrj __value = __binary_op(__value, *__first);
296*38fd1498Szrj *++__result = __value;
297*38fd1498Szrj }
298*38fd1498Szrj return ++__result;
299*38fd1498Szrj }
300*38fd1498Szrj
301*38fd1498Szrj /**
302*38fd1498Szrj * @brief Return differences between adjacent values.
303*38fd1498Szrj *
304*38fd1498Szrj * Computes the difference between adjacent values in the range
305*38fd1498Szrj * [first,last) using operator-() and writes the result to @p __result.
306*38fd1498Szrj *
307*38fd1498Szrj * @param __first Start of input range.
308*38fd1498Szrj * @param __last End of input range.
309*38fd1498Szrj * @param __result Output sums.
310*38fd1498Szrj * @return Iterator pointing just beyond the values written to result.
311*38fd1498Szrj *
312*38fd1498Szrj * _GLIBCXX_RESOLVE_LIB_DEFECTS
313*38fd1498Szrj * DR 539. partial_sum and adjacent_difference should mention requirements
314*38fd1498Szrj */
315*38fd1498Szrj template<typename _InputIterator, typename _OutputIterator>
316*38fd1498Szrj _OutputIterator
317*38fd1498Szrj adjacent_difference(_InputIterator __first,
318*38fd1498Szrj _InputIterator __last, _OutputIterator __result)
319*38fd1498Szrj {
320*38fd1498Szrj typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
321*38fd1498Szrj
322*38fd1498Szrj // concept requirements
323*38fd1498Szrj __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
324*38fd1498Szrj __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
325*38fd1498Szrj _ValueType>)
326*38fd1498Szrj __glibcxx_requires_valid_range(__first, __last);
327*38fd1498Szrj
328*38fd1498Szrj if (__first == __last)
329*38fd1498Szrj return __result;
330*38fd1498Szrj _ValueType __value = *__first;
331*38fd1498Szrj *__result = __value;
332*38fd1498Szrj while (++__first != __last)
333*38fd1498Szrj {
334*38fd1498Szrj _ValueType __tmp = *__first;
335*38fd1498Szrj *++__result = __tmp - __value;
336*38fd1498Szrj __value = _GLIBCXX_MOVE(__tmp);
337*38fd1498Szrj }
338*38fd1498Szrj return ++__result;
339*38fd1498Szrj }
340*38fd1498Szrj
341*38fd1498Szrj /**
342*38fd1498Szrj * @brief Return differences between adjacent values.
343*38fd1498Szrj *
344*38fd1498Szrj * Computes the difference between adjacent values in the range
345*38fd1498Szrj * [__first,__last) using the function object @p __binary_op and writes the
346*38fd1498Szrj * result to @p __result.
347*38fd1498Szrj *
348*38fd1498Szrj * @param __first Start of input range.
349*38fd1498Szrj * @param __last End of input range.
350*38fd1498Szrj * @param __result Output sum.
351*38fd1498Szrj * @param __binary_op Function object.
352*38fd1498Szrj * @return Iterator pointing just beyond the values written to result.
353*38fd1498Szrj *
354*38fd1498Szrj * _GLIBCXX_RESOLVE_LIB_DEFECTS
355*38fd1498Szrj * DR 539. partial_sum and adjacent_difference should mention requirements
356*38fd1498Szrj */
357*38fd1498Szrj template<typename _InputIterator, typename _OutputIterator,
358*38fd1498Szrj typename _BinaryOperation>
359*38fd1498Szrj _OutputIterator
360*38fd1498Szrj adjacent_difference(_InputIterator __first, _InputIterator __last,
361*38fd1498Szrj _OutputIterator __result, _BinaryOperation __binary_op)
362*38fd1498Szrj {
363*38fd1498Szrj typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
364*38fd1498Szrj
365*38fd1498Szrj // concept requirements
366*38fd1498Szrj __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
367*38fd1498Szrj __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
368*38fd1498Szrj _ValueType>)
369*38fd1498Szrj __glibcxx_requires_valid_range(__first, __last);
370*38fd1498Szrj
371*38fd1498Szrj if (__first == __last)
372*38fd1498Szrj return __result;
373*38fd1498Szrj _ValueType __value = *__first;
374*38fd1498Szrj *__result = __value;
375*38fd1498Szrj while (++__first != __last)
376*38fd1498Szrj {
377*38fd1498Szrj _ValueType __tmp = *__first;
378*38fd1498Szrj *++__result = __binary_op(__tmp, __value);
379*38fd1498Szrj __value = _GLIBCXX_MOVE(__tmp);
380*38fd1498Szrj }
381*38fd1498Szrj return ++__result;
382*38fd1498Szrj }
383*38fd1498Szrj
384*38fd1498Szrj _GLIBCXX_END_NAMESPACE_ALGO
385*38fd1498Szrj } // namespace std
386*38fd1498Szrj
387*38fd1498Szrj #endif /* _STL_NUMERIC_H */
388