xref: /openbsd-src/gnu/gcc/libstdc++-v3/include/bits/stl_numeric.h (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert // Numeric functions implementation -*- C++ -*-
2*404b540aSrobert 
3*404b540aSrobert // Copyright (C) 2001, 2004, 2005 Free Software Foundation, Inc.
4*404b540aSrobert //
5*404b540aSrobert // This file is part of the GNU ISO C++ Library.  This library is free
6*404b540aSrobert // software; you can redistribute it and/or modify it under the
7*404b540aSrobert // terms of the GNU General Public License as published by the
8*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
9*404b540aSrobert // any later version.
10*404b540aSrobert 
11*404b540aSrobert // This library is distributed in the hope that it will be useful,
12*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*404b540aSrobert // GNU General Public License for more details.
15*404b540aSrobert 
16*404b540aSrobert // You should have received a copy of the GNU General Public License along
17*404b540aSrobert // with this library; see the file COPYING.  If not, write to the Free
18*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19*404b540aSrobert // USA.
20*404b540aSrobert 
21*404b540aSrobert // As a special exception, you may use this file as part of a free software
22*404b540aSrobert // library without restriction.  Specifically, if other files instantiate
23*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
24*404b540aSrobert // this file and link it with other files to produce an executable, this
25*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
26*404b540aSrobert // the GNU General Public License.  This exception does not however
27*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
28*404b540aSrobert // the GNU General Public License.
29*404b540aSrobert 
30*404b540aSrobert /*
31*404b540aSrobert  *
32*404b540aSrobert  * Copyright (c) 1994
33*404b540aSrobert  * Hewlett-Packard Company
34*404b540aSrobert  *
35*404b540aSrobert  * Permission to use, copy, modify, distribute and sell this software
36*404b540aSrobert  * and its documentation for any purpose is hereby granted without fee,
37*404b540aSrobert  * provided that the above copyright notice appear in all copies and
38*404b540aSrobert  * that both that copyright notice and this permission notice appear
39*404b540aSrobert  * in supporting documentation.  Hewlett-Packard Company makes no
40*404b540aSrobert  * representations about the suitability of this software for any
41*404b540aSrobert  * purpose.  It is provided "as is" without express or implied warranty.
42*404b540aSrobert  *
43*404b540aSrobert  *
44*404b540aSrobert  * Copyright (c) 1996,1997
45*404b540aSrobert  * Silicon Graphics Computer Systems, Inc.
46*404b540aSrobert  *
47*404b540aSrobert  * Permission to use, copy, modify, distribute and sell this software
48*404b540aSrobert  * and its documentation for any purpose is hereby granted without fee,
49*404b540aSrobert  * provided that the above copyright notice appear in all copies and
50*404b540aSrobert  * that both that copyright notice and this permission notice appear
51*404b540aSrobert  * in supporting documentation.  Silicon Graphics makes no
52*404b540aSrobert  * representations about the suitability of this software for any
53*404b540aSrobert  * purpose.  It is provided "as is" without express or implied warranty.
54*404b540aSrobert  */
55*404b540aSrobert 
56*404b540aSrobert /** @file stl_numeric.h
57*404b540aSrobert  *  This is an internal header file, included by other library headers.
58*404b540aSrobert  *  You should not attempt to use it directly.
59*404b540aSrobert  */
60*404b540aSrobert 
61*404b540aSrobert #ifndef _STL_NUMERIC_H
62*404b540aSrobert #define _STL_NUMERIC_H 1
63*404b540aSrobert 
64*404b540aSrobert #include <debug/debug.h>
65*404b540aSrobert 
_GLIBCXX_BEGIN_NAMESPACE(std)66*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(std)
67*404b540aSrobert 
68*404b540aSrobert   /**
69*404b540aSrobert    *  @brief  Accumulate values in a range.
70*404b540aSrobert    *
71*404b540aSrobert    *  Accumulates the values in the range [first,last) using operator+().  The
72*404b540aSrobert    *  initial value is @a init.  The values are processed in order.
73*404b540aSrobert    *
74*404b540aSrobert    *  @param  first  Start of range.
75*404b540aSrobert    *  @param  last  End of range.
76*404b540aSrobert    *  @param  init  Starting value to add other values to.
77*404b540aSrobert    *  @return  The final sum.
78*404b540aSrobert    */
79*404b540aSrobert   template<typename _InputIterator, typename _Tp>
80*404b540aSrobert     _Tp
81*404b540aSrobert     accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
82*404b540aSrobert     {
83*404b540aSrobert       // concept requirements
84*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
85*404b540aSrobert       __glibcxx_requires_valid_range(__first, __last);
86*404b540aSrobert 
87*404b540aSrobert       for (; __first != __last; ++__first)
88*404b540aSrobert 	__init = __init + *__first;
89*404b540aSrobert       return __init;
90*404b540aSrobert     }
91*404b540aSrobert 
92*404b540aSrobert   /**
93*404b540aSrobert    *  @brief  Accumulate values in a range with operation.
94*404b540aSrobert    *
95*404b540aSrobert    *  Accumulates the values in the range [first,last) using the function
96*404b540aSrobert    *  object @a binary_op.  The initial value is @a init.  The values are
97*404b540aSrobert    *  processed in order.
98*404b540aSrobert    *
99*404b540aSrobert    *  @param  first  Start of range.
100*404b540aSrobert    *  @param  last  End of range.
101*404b540aSrobert    *  @param  init  Starting value to add other values to.
102*404b540aSrobert    *  @param  binary_op  Function object to accumulate with.
103*404b540aSrobert    *  @return  The final sum.
104*404b540aSrobert    */
105*404b540aSrobert   template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
106*404b540aSrobert     _Tp
accumulate(_InputIterator __first,_InputIterator __last,_Tp __init,_BinaryOperation __binary_op)107*404b540aSrobert     accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
108*404b540aSrobert 	       _BinaryOperation __binary_op)
109*404b540aSrobert     {
110*404b540aSrobert       // concept requirements
111*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
112*404b540aSrobert       __glibcxx_requires_valid_range(__first, __last);
113*404b540aSrobert 
114*404b540aSrobert       for (; __first != __last; ++__first)
115*404b540aSrobert 	__init = __binary_op(__init, *__first);
116*404b540aSrobert       return __init;
117*404b540aSrobert     }
118*404b540aSrobert 
119*404b540aSrobert   /**
120*404b540aSrobert    *  @brief  Compute inner product of two ranges.
121*404b540aSrobert    *
122*404b540aSrobert    *  Starting with an initial value of @a init, multiplies successive
123*404b540aSrobert    *  elements from the two ranges and adds each product into the accumulated
124*404b540aSrobert    *  value using operator+().  The values in the ranges are processed in
125*404b540aSrobert    *  order.
126*404b540aSrobert    *
127*404b540aSrobert    *  @param  first1  Start of range 1.
128*404b540aSrobert    *  @param  last1  End of range 1.
129*404b540aSrobert    *  @param  first2  Start of range 2.
130*404b540aSrobert    *  @param  init  Starting value to add other values to.
131*404b540aSrobert    *  @return  The final inner product.
132*404b540aSrobert    */
133*404b540aSrobert   template<typename _InputIterator1, typename _InputIterator2, typename _Tp>
134*404b540aSrobert     _Tp
inner_product(_InputIterator1 __first1,_InputIterator1 __last1,_InputIterator2 __first2,_Tp __init)135*404b540aSrobert     inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
136*404b540aSrobert 		  _InputIterator2 __first2, _Tp __init)
137*404b540aSrobert     {
138*404b540aSrobert       // concept requirements
139*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
140*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
141*404b540aSrobert       __glibcxx_requires_valid_range(__first1, __last1);
142*404b540aSrobert 
143*404b540aSrobert       for (; __first1 != __last1; ++__first1, ++__first2)
144*404b540aSrobert 	__init = __init + (*__first1 * *__first2);
145*404b540aSrobert       return __init;
146*404b540aSrobert     }
147*404b540aSrobert 
148*404b540aSrobert   /**
149*404b540aSrobert    *  @brief  Compute inner product of two ranges.
150*404b540aSrobert    *
151*404b540aSrobert    *  Starting with an initial value of @a init, applies @a binary_op2 to
152*404b540aSrobert    *  successive elements from the two ranges and accumulates each result into
153*404b540aSrobert    *  the accumulated value using @a binary_op1.  The values in the ranges are
154*404b540aSrobert    *  processed in order.
155*404b540aSrobert    *
156*404b540aSrobert    *  @param  first1  Start of range 1.
157*404b540aSrobert    *  @param  last1  End of range 1.
158*404b540aSrobert    *  @param  first2  Start of range 2.
159*404b540aSrobert    *  @param  init  Starting value to add other values to.
160*404b540aSrobert    *  @param  binary_op1  Function object to accumulate with.
161*404b540aSrobert    *  @param  binary_op2  Function object to apply to pairs of input values.
162*404b540aSrobert    *  @return  The final inner product.
163*404b540aSrobert    */
164*404b540aSrobert   template<typename _InputIterator1, typename _InputIterator2, typename _Tp,
165*404b540aSrobert 	    typename _BinaryOperation1, typename _BinaryOperation2>
166*404b540aSrobert     _Tp
inner_product(_InputIterator1 __first1,_InputIterator1 __last1,_InputIterator2 __first2,_Tp __init,_BinaryOperation1 __binary_op1,_BinaryOperation2 __binary_op2)167*404b540aSrobert     inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
168*404b540aSrobert 		  _InputIterator2 __first2, _Tp __init,
169*404b540aSrobert 		  _BinaryOperation1 __binary_op1,
170*404b540aSrobert 		  _BinaryOperation2 __binary_op2)
171*404b540aSrobert     {
172*404b540aSrobert       // concept requirements
173*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
174*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
175*404b540aSrobert       __glibcxx_requires_valid_range(__first1, __last1);
176*404b540aSrobert 
177*404b540aSrobert       for (; __first1 != __last1; ++__first1, ++__first2)
178*404b540aSrobert 	__init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
179*404b540aSrobert       return __init;
180*404b540aSrobert     }
181*404b540aSrobert 
182*404b540aSrobert   /**
183*404b540aSrobert    *  @brief  Return list of partial sums
184*404b540aSrobert    *
185*404b540aSrobert    *  Accumulates the values in the range [first,last) using operator+().
186*404b540aSrobert    *  As each successive input value is added into the total, that partial sum
187*404b540aSrobert    *  is written to @a result.  Therefore, the first value in result is the
188*404b540aSrobert    *  first value of the input, the second value in result is the sum of the
189*404b540aSrobert    *  first and second input values, and so on.
190*404b540aSrobert    *
191*404b540aSrobert    *  @param  first  Start of input range.
192*404b540aSrobert    *  @param  last  End of input range.
193*404b540aSrobert    *  @param  result  Output to write sums to.
194*404b540aSrobert    *  @return  Iterator pointing just beyond the values written to result.
195*404b540aSrobert    */
196*404b540aSrobert   template<typename _InputIterator, typename _OutputIterator>
197*404b540aSrobert     _OutputIterator
partial_sum(_InputIterator __first,_InputIterator __last,_OutputIterator __result)198*404b540aSrobert     partial_sum(_InputIterator __first, _InputIterator __last,
199*404b540aSrobert 		_OutputIterator __result)
200*404b540aSrobert     {
201*404b540aSrobert       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
202*404b540aSrobert 
203*404b540aSrobert       // concept requirements
204*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
205*404b540aSrobert       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
206*404b540aSrobert 				                         _ValueType>)
207*404b540aSrobert       __glibcxx_requires_valid_range(__first, __last);
208*404b540aSrobert 
209*404b540aSrobert       if (__first == __last)
210*404b540aSrobert 	return __result;
211*404b540aSrobert       _ValueType __value = *__first;
212*404b540aSrobert       *__result = __value;
213*404b540aSrobert       while (++__first != __last)
214*404b540aSrobert 	{
215*404b540aSrobert 	  __value = __value + *__first;
216*404b540aSrobert 	  *++__result = __value;
217*404b540aSrobert 	}
218*404b540aSrobert       return ++__result;
219*404b540aSrobert     }
220*404b540aSrobert 
221*404b540aSrobert   /**
222*404b540aSrobert    *  @brief  Return list of partial sums
223*404b540aSrobert    *
224*404b540aSrobert    *  Accumulates the values in the range [first,last) using operator+().
225*404b540aSrobert    *  As each successive input value is added into the total, that partial sum
226*404b540aSrobert    *  is written to @a result.  Therefore, the first value in result is the
227*404b540aSrobert    *  first value of the input, the second value in result is the sum of the
228*404b540aSrobert    *  first and second input values, and so on.
229*404b540aSrobert    *
230*404b540aSrobert    *  @param  first  Start of input range.
231*404b540aSrobert    *  @param  last  End of input range.
232*404b540aSrobert    *  @param  result  Output to write sums to.
233*404b540aSrobert    *  @return  Iterator pointing just beyond the values written to result.
234*404b540aSrobert    */
235*404b540aSrobert   template<typename _InputIterator, typename _OutputIterator,
236*404b540aSrobert 	   typename _BinaryOperation>
237*404b540aSrobert     _OutputIterator
partial_sum(_InputIterator __first,_InputIterator __last,_OutputIterator __result,_BinaryOperation __binary_op)238*404b540aSrobert     partial_sum(_InputIterator __first, _InputIterator __last,
239*404b540aSrobert 		_OutputIterator __result, _BinaryOperation __binary_op)
240*404b540aSrobert     {
241*404b540aSrobert       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
242*404b540aSrobert 
243*404b540aSrobert       // concept requirements
244*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
245*404b540aSrobert       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
246*404b540aSrobert 				                         _ValueType>)
247*404b540aSrobert       __glibcxx_requires_valid_range(__first, __last);
248*404b540aSrobert 
249*404b540aSrobert       if (__first == __last)
250*404b540aSrobert 	return __result;
251*404b540aSrobert       _ValueType __value = *__first;
252*404b540aSrobert       *__result = __value;
253*404b540aSrobert       while (++__first != __last)
254*404b540aSrobert 	{
255*404b540aSrobert 	  __value = __binary_op(__value, *__first);
256*404b540aSrobert 	  *++__result = __value;
257*404b540aSrobert 	}
258*404b540aSrobert       return ++__result;
259*404b540aSrobert     }
260*404b540aSrobert 
261*404b540aSrobert   /**
262*404b540aSrobert    *  @brief  Return differences between adjacent values.
263*404b540aSrobert    *
264*404b540aSrobert    *  Computes the difference between adjacent values in the range
265*404b540aSrobert    *  [first,last) using operator-() and writes the result to @a result.
266*404b540aSrobert    *
267*404b540aSrobert    *  @param  first  Start of input range.
268*404b540aSrobert    *  @param  last  End of input range.
269*404b540aSrobert    *  @param  result  Output to write sums to.
270*404b540aSrobert    *  @return  Iterator pointing just beyond the values written to result.
271*404b540aSrobert    */
272*404b540aSrobert   template<typename _InputIterator, typename _OutputIterator>
273*404b540aSrobert     _OutputIterator
adjacent_difference(_InputIterator __first,_InputIterator __last,_OutputIterator __result)274*404b540aSrobert     adjacent_difference(_InputIterator __first,
275*404b540aSrobert 			_InputIterator __last, _OutputIterator __result)
276*404b540aSrobert     {
277*404b540aSrobert       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
278*404b540aSrobert 
279*404b540aSrobert       // concept requirements
280*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
281*404b540aSrobert       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
282*404b540aSrobert 				                         _ValueType>)
283*404b540aSrobert       __glibcxx_requires_valid_range(__first, __last);
284*404b540aSrobert 
285*404b540aSrobert       if (__first == __last)
286*404b540aSrobert 	return __result;
287*404b540aSrobert       _ValueType __value = *__first;
288*404b540aSrobert       *__result = __value;
289*404b540aSrobert       while (++__first != __last)
290*404b540aSrobert 	{
291*404b540aSrobert 	  _ValueType __tmp = *__first;
292*404b540aSrobert 	  *++__result = __tmp - __value;
293*404b540aSrobert 	  __value = __tmp;
294*404b540aSrobert 	}
295*404b540aSrobert       return ++__result;
296*404b540aSrobert     }
297*404b540aSrobert 
298*404b540aSrobert   /**
299*404b540aSrobert    *  @brief  Return differences between adjacent values.
300*404b540aSrobert    *
301*404b540aSrobert    *  Computes the difference between adjacent values in the range
302*404b540aSrobert    *  [first,last) using the function object @a binary_op and writes the
303*404b540aSrobert    *  result to @a result.
304*404b540aSrobert    *
305*404b540aSrobert    *  @param  first  Start of input range.
306*404b540aSrobert    *  @param  last  End of input range.
307*404b540aSrobert    *  @param  result  Output to write sums to.
308*404b540aSrobert    *  @return  Iterator pointing just beyond the values written to result.
309*404b540aSrobert    */
310*404b540aSrobert   template<typename _InputIterator, typename _OutputIterator,
311*404b540aSrobert 	   typename _BinaryOperation>
312*404b540aSrobert     _OutputIterator
adjacent_difference(_InputIterator __first,_InputIterator __last,_OutputIterator __result,_BinaryOperation __binary_op)313*404b540aSrobert     adjacent_difference(_InputIterator __first, _InputIterator __last,
314*404b540aSrobert 			_OutputIterator __result, _BinaryOperation __binary_op)
315*404b540aSrobert     {
316*404b540aSrobert       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
317*404b540aSrobert 
318*404b540aSrobert       // concept requirements
319*404b540aSrobert       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
320*404b540aSrobert       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
321*404b540aSrobert 				                         _ValueType>)
322*404b540aSrobert       __glibcxx_requires_valid_range(__first, __last);
323*404b540aSrobert 
324*404b540aSrobert       if (__first == __last)
325*404b540aSrobert 	return __result;
326*404b540aSrobert       _ValueType __value = *__first;
327*404b540aSrobert       *__result = __value;
328*404b540aSrobert       while (++__first != __last)
329*404b540aSrobert 	{
330*404b540aSrobert 	  _ValueType __tmp = *__first;
331*404b540aSrobert 	  *++__result = __binary_op(__tmp, __value);
332*404b540aSrobert 	  __value = __tmp;
333*404b540aSrobert 	}
334*404b540aSrobert       return ++__result;
335*404b540aSrobert     }
336*404b540aSrobert 
337*404b540aSrobert _GLIBCXX_END_NAMESPACE
338*404b540aSrobert 
339*404b540aSrobert #endif /* _STL_NUMERIC_H */
340