2 //===---------------------------- numeric ---------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_NUMERIC
12 #define _LIBCPP_NUMERIC
20 template <class InputIterator, class T>
22 accumulate(InputIterator first, InputIterator last, T init);
24 template <class InputIterator, class T, class BinaryOperation>
26 accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);
28 template <class InputIterator1, class InputIterator2, class T>
30 inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init);
32 template <class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
34 inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
35 T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);
37 template <class InputIterator, class OutputIterator>
39 partial_sum(InputIterator first, InputIterator last, OutputIterator result);
41 template <class InputIterator, class OutputIterator, class BinaryOperation>
43 partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
45 template <class InputIterator, class OutputIterator>
47 adjacent_difference(InputIterator first, InputIterator last, OutputIterator result);
49 template <class InputIterator, class OutputIterator, class BinaryOperation>
51 adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
53 template <class ForwardIterator, class T>
54 void iota(ForwardIterator first, ForwardIterator last, T value);
63 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
64 #pragma GCC system_header
67 _LIBCPP_BEGIN_NAMESPACE_STD
69 template <class _InputIterator, class _Tp>
70 inline _LIBCPP_INLINE_VISIBILITY
72 accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
74 for (; __first != __last; ++__first)
75 __init = __init + *__first;
79 template <class _InputIterator, class _Tp, class _BinaryOperation>
80 inline _LIBCPP_INLINE_VISIBILITY
82 accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
84 for (; __first != __last; ++__first)
85 __init = __binary_op(__init, *__first);
89 template <class _InputIterator1, class _InputIterator2, class _Tp>
90 inline _LIBCPP_INLINE_VISIBILITY
92 inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init)
94 for (; __first1 != __last1; ++__first1, (void) ++__first2)
95 __init = __init + *__first1 * *__first2;
99 template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
100 inline _LIBCPP_INLINE_VISIBILITY
102 inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
103 _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2)
105 for (; __first1 != __last1; ++__first1, (void) ++__first2)
106 __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
110 template <class _InputIterator, class _OutputIterator>
111 inline _LIBCPP_INLINE_VISIBILITY
113 partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
115 if (__first != __last)
117 typename iterator_traits<_InputIterator>::value_type __t(*__first);
119 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
121 __t = __t + *__first;
128 template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
129 inline _LIBCPP_INLINE_VISIBILITY
131 partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
132 _BinaryOperation __binary_op)
134 if (__first != __last)
136 typename iterator_traits<_InputIterator>::value_type __t(*__first);
138 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
140 __t = __binary_op(__t, *__first);
147 template <class _InputIterator, class _OutputIterator>
148 inline _LIBCPP_INLINE_VISIBILITY
150 adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
152 if (__first != __last)
154 typename iterator_traits<_InputIterator>::value_type __t1(*__first);
156 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
158 typename iterator_traits<_InputIterator>::value_type __t2(*__first);
159 *__result = __t2 - __t1;
160 __t1 = _VSTD::move(__t2);
166 template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
167 inline _LIBCPP_INLINE_VISIBILITY
169 adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
170 _BinaryOperation __binary_op)
172 if (__first != __last)
174 typename iterator_traits<_InputIterator>::value_type __t1(*__first);
176 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
178 typename iterator_traits<_InputIterator>::value_type __t2(*__first);
179 *__result = __binary_op(__t2, __t1);
180 __t1 = _VSTD::move(__t2);
186 template <class _ForwardIterator, class _Tp>
187 inline _LIBCPP_INLINE_VISIBILITY
189 iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_)
191 for (; __first != __last; ++__first, (void) ++__value_)
195 _LIBCPP_END_NAMESPACE_STD
197 #endif // _LIBCPP_NUMERIC