1 // Numeric functions implementation -*- C++ -*-
3 // Copyright (C) 2001-2018 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
28 * Hewlett-Packard Company
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
39 * Copyright (c) 1996,1997
40 * Silicon Graphics Computer Systems, Inc.
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
51 /** @file bits/stl_numeric.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{numeric}
56 #ifndef _STL_NUMERIC_H
57 #define _STL_NUMERIC_H 1
59 #include <bits/concept_check.h>
60 #include <debug/debug.h>
61 #include <bits/move.h> // For _GLIBCXX_MOVE
63 #if __cplusplus >= 201103L
65 namespace std
_GLIBCXX_VISIBILITY(default)
67 _GLIBCXX_BEGIN_NAMESPACE_VERSION
70 * @brief Create a range of sequentially increasing values.
72 * For each element in the range @p [first,last) assigns @p value and
73 * increments @p value as if by @p ++value.
75 * @param __first Start of range.
76 * @param __last End of range.
77 * @param __value Starting value.
80 template<typename _ForwardIterator
, typename _Tp
>
82 iota(_ForwardIterator __first
, _ForwardIterator __last
, _Tp __value
)
84 // concept requirements
85 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
87 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
88 typename iterator_traits
<_ForwardIterator
>::value_type
>)
89 __glibcxx_requires_valid_range(__first
, __last
);
91 for (; __first
!= __last
; ++__first
)
98 _GLIBCXX_END_NAMESPACE_VERSION
103 namespace std
_GLIBCXX_VISIBILITY(default)
105 _GLIBCXX_BEGIN_NAMESPACE_ALGO
108 * @brief Accumulate values in a range.
110 * Accumulates the values in the range [first,last) using operator+(). The
111 * initial value is @a init. The values are processed in order.
113 * @param __first Start of range.
114 * @param __last End of range.
115 * @param __init Starting value to add other values to.
116 * @return The final sum.
118 template<typename _InputIterator
, typename _Tp
>
120 accumulate(_InputIterator __first
, _InputIterator __last
, _Tp __init
)
122 // concept requirements
123 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
124 __glibcxx_requires_valid_range(__first
, __last
);
126 for (; __first
!= __last
; ++__first
)
127 __init
= __init
+ *__first
;
132 * @brief Accumulate values in a range with operation.
134 * Accumulates the values in the range [first,last) using the function
135 * object @p __binary_op. The initial value is @p __init. The values are
136 * processed in order.
138 * @param __first Start of range.
139 * @param __last End of range.
140 * @param __init Starting value to add other values to.
141 * @param __binary_op Function object to accumulate with.
142 * @return The final sum.
144 template<typename _InputIterator
, typename _Tp
, typename _BinaryOperation
>
146 accumulate(_InputIterator __first
, _InputIterator __last
, _Tp __init
,
147 _BinaryOperation __binary_op
)
149 // concept requirements
150 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
151 __glibcxx_requires_valid_range(__first
, __last
);
153 for (; __first
!= __last
; ++__first
)
154 __init
= __binary_op(__init
, *__first
);
159 * @brief Compute inner product of two ranges.
161 * Starting with an initial value of @p __init, multiplies successive
162 * elements from the two ranges and adds each product into the accumulated
163 * value using operator+(). The values in the ranges are processed in
166 * @param __first1 Start of range 1.
167 * @param __last1 End of range 1.
168 * @param __first2 Start of range 2.
169 * @param __init Starting value to add other values to.
170 * @return The final inner product.
172 template<typename _InputIterator1
, typename _InputIterator2
, typename _Tp
>
174 inner_product(_InputIterator1 __first1
, _InputIterator1 __last1
,
175 _InputIterator2 __first2
, _Tp __init
)
177 // concept requirements
178 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
179 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
180 __glibcxx_requires_valid_range(__first1
, __last1
);
182 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
)
183 __init
= __init
+ (*__first1
* *__first2
);
188 * @brief Compute inner product of two ranges.
190 * Starting with an initial value of @p __init, applies @p __binary_op2 to
191 * successive elements from the two ranges and accumulates each result into
192 * the accumulated value using @p __binary_op1. The values in the ranges are
193 * processed in order.
195 * @param __first1 Start of range 1.
196 * @param __last1 End of range 1.
197 * @param __first2 Start of range 2.
198 * @param __init Starting value to add other values to.
199 * @param __binary_op1 Function object to accumulate with.
200 * @param __binary_op2 Function object to apply to pairs of input values.
201 * @return The final inner product.
203 template<typename _InputIterator1
, typename _InputIterator2
, typename _Tp
,
204 typename _BinaryOperation1
, typename _BinaryOperation2
>
206 inner_product(_InputIterator1 __first1
, _InputIterator1 __last1
,
207 _InputIterator2 __first2
, _Tp __init
,
208 _BinaryOperation1 __binary_op1
,
209 _BinaryOperation2 __binary_op2
)
211 // concept requirements
212 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
213 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
214 __glibcxx_requires_valid_range(__first1
, __last1
);
216 for (; __first1
!= __last1
; ++__first1
, (void)++__first2
)
217 __init
= __binary_op1(__init
, __binary_op2(*__first1
, *__first2
));
222 * @brief Return list of partial sums
224 * Accumulates the values in the range [first,last) using the @c + operator.
225 * As each successive input value is added into the total, that partial sum
226 * is written to @p __result. Therefore, the first value in @p __result is
227 * the first value of the input, the second value in @p __result is the sum
228 * of the first and second input values, and so on.
230 * @param __first Start of input range.
231 * @param __last End of input range.
232 * @param __result Output sum.
233 * @return Iterator pointing just beyond the values written to __result.
235 template<typename _InputIterator
, typename _OutputIterator
>
237 partial_sum(_InputIterator __first
, _InputIterator __last
,
238 _OutputIterator __result
)
240 typedef typename iterator_traits
<_InputIterator
>::value_type _ValueType
;
242 // concept requirements
243 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
244 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
246 __glibcxx_requires_valid_range(__first
, __last
);
248 if (__first
== __last
)
250 _ValueType __value
= *__first
;
252 while (++__first
!= __last
)
254 __value
= __value
+ *__first
;
255 *++__result
= __value
;
261 * @brief Return list of partial sums
263 * Accumulates the values in the range [first,last) using @p __binary_op.
264 * As each successive input value is added into the total, that partial sum
265 * is written to @p __result. Therefore, the first value in @p __result is
266 * the first value of the input, the second value in @p __result is the sum
267 * of the first and second input values, and so on.
269 * @param __first Start of input range.
270 * @param __last End of input range.
271 * @param __result Output sum.
272 * @param __binary_op Function object.
273 * @return Iterator pointing just beyond the values written to __result.
275 template<typename _InputIterator
, typename _OutputIterator
,
276 typename _BinaryOperation
>
278 partial_sum(_InputIterator __first
, _InputIterator __last
,
279 _OutputIterator __result
, _BinaryOperation __binary_op
)
281 typedef typename iterator_traits
<_InputIterator
>::value_type _ValueType
;
283 // concept requirements
284 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
285 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
287 __glibcxx_requires_valid_range(__first
, __last
);
289 if (__first
== __last
)
291 _ValueType __value
= *__first
;
293 while (++__first
!= __last
)
295 __value
= __binary_op(__value
, *__first
);
296 *++__result
= __value
;
302 * @brief Return differences between adjacent values.
304 * Computes the difference between adjacent values in the range
305 * [first,last) using operator-() and writes the result to @p __result.
307 * @param __first Start of input range.
308 * @param __last End of input range.
309 * @param __result Output sums.
310 * @return Iterator pointing just beyond the values written to result.
312 * _GLIBCXX_RESOLVE_LIB_DEFECTS
313 * DR 539. partial_sum and adjacent_difference should mention requirements
315 template<typename _InputIterator
, typename _OutputIterator
>
317 adjacent_difference(_InputIterator __first
,
318 _InputIterator __last
, _OutputIterator __result
)
320 typedef typename iterator_traits
<_InputIterator
>::value_type _ValueType
;
322 // concept requirements
323 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
324 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
326 __glibcxx_requires_valid_range(__first
, __last
);
328 if (__first
== __last
)
330 _ValueType __value
= *__first
;
332 while (++__first
!= __last
)
334 _ValueType __tmp
= *__first
;
335 *++__result
= __tmp
- __value
;
336 __value
= _GLIBCXX_MOVE(__tmp
);
342 * @brief Return differences between adjacent values.
344 * Computes the difference between adjacent values in the range
345 * [__first,__last) using the function object @p __binary_op and writes the
346 * result to @p __result.
348 * @param __first Start of input range.
349 * @param __last End of input range.
350 * @param __result Output sum.
351 * @param __binary_op Function object.
352 * @return Iterator pointing just beyond the values written to result.
354 * _GLIBCXX_RESOLVE_LIB_DEFECTS
355 * DR 539. partial_sum and adjacent_difference should mention requirements
357 template<typename _InputIterator
, typename _OutputIterator
,
358 typename _BinaryOperation
>
360 adjacent_difference(_InputIterator __first
, _InputIterator __last
,
361 _OutputIterator __result
, _BinaryOperation __binary_op
)
363 typedef typename iterator_traits
<_InputIterator
>::value_type _ValueType
;
365 // concept requirements
366 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
367 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
369 __glibcxx_requires_valid_range(__first
, __last
);
371 if (__first
== __last
)
373 _ValueType __value
= *__first
;
375 while (++__first
!= __last
)
377 _ValueType __tmp
= *__first
;
378 *++__result
= __binary_op(__tmp
, __value
);
379 __value
= _GLIBCXX_MOVE(__tmp
);
384 _GLIBCXX_END_NAMESPACE_ALGO
387 #endif /* _STL_NUMERIC_H */