blob: ff2853e917a6cc3e0c311fbb0006cd5b2e002a65 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- numeric ---------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_NUMERIC
12#define _LIBCPP_NUMERIC
13
14/*
15 numeric synopsis
16
17namespace std
18{
19
20template <class InputIterator, class T>
21 T
22 accumulate(InputIterator first, InputIterator last, T init);
23
24template <class InputIterator, class T, class BinaryOperation>
25 T
26 accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);
27
28template <class InputIterator1, class InputIterator2, class T>
29 T
30 inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init);
31
32template <class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
33 T
34 inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
35 T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);
36
37template <class InputIterator, class OutputIterator>
38 OutputIterator
39 partial_sum(InputIterator first, InputIterator last, OutputIterator result);
40
41template <class InputIterator, class OutputIterator, class BinaryOperation>
42 OutputIterator
43 partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
44
45template <class InputIterator, class OutputIterator>
46 OutputIterator
47 adjacent_difference(InputIterator first, InputIterator last, OutputIterator result);
48
49template <class InputIterator, class OutputIterator, class BinaryOperation>
50 OutputIterator
51 adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
52
Howard Hinnantebef7452010-05-26 18:53:44 +000053template <class ForwardIterator, class T>
54 void iota(ForwardIterator first, ForwardIterator last, T value);
55
Marshall Clow1c1e91d2016-07-26 14:29:45 +000056template <class M, class N>
57 constexpr common_type_t<M,N> gcd(M m, N n); // C++17
58
59template <class M, class N>
60 constexpr common_type_t<M,N> lcm(M m, N n); // C++17
61
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000062} // std
63
64*/
65
66#include <__config>
67#include <iterator>
Eric Fiselier34da9b52017-02-08 00:14:13 +000068#include <limits> // for numeric_limits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000069
Howard Hinnant08e17472011-10-17 20:05:10 +000070#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000071#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000072#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000073
74_LIBCPP_BEGIN_NAMESPACE_STD
75
76template <class _InputIterator, class _Tp>
77inline _LIBCPP_INLINE_VISIBILITY
78_Tp
79accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
80{
81 for (; __first != __last; ++__first)
82 __init = __init + *__first;
83 return __init;
84}
85
86template <class _InputIterator, class _Tp, class _BinaryOperation>
87inline _LIBCPP_INLINE_VISIBILITY
88_Tp
89accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
90{
91 for (; __first != __last; ++__first)
92 __init = __binary_op(__init, *__first);
93 return __init;
94}
95
96template <class _InputIterator1, class _InputIterator2, class _Tp>
97inline _LIBCPP_INLINE_VISIBILITY
98_Tp
99inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init)
100{
Eric Fiselierb9919752014-10-27 19:28:20 +0000101 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000102 __init = __init + *__first1 * *__first2;
103 return __init;
104}
105
106template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
107inline _LIBCPP_INLINE_VISIBILITY
108_Tp
109inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
110 _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2)
111{
Eric Fiselierb9919752014-10-27 19:28:20 +0000112 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000113 __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
114 return __init;
115}
116
117template <class _InputIterator, class _OutputIterator>
118inline _LIBCPP_INLINE_VISIBILITY
119_OutputIterator
120partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
121{
122 if (__first != __last)
123 {
124 typename iterator_traits<_InputIterator>::value_type __t(*__first);
125 *__result = __t;
Eric Fiselierb9919752014-10-27 19:28:20 +0000126 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000127 {
128 __t = __t + *__first;
129 *__result = __t;
130 }
131 }
132 return __result;
133}
134
135template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
136inline _LIBCPP_INLINE_VISIBILITY
137_OutputIterator
138partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
139 _BinaryOperation __binary_op)
140{
141 if (__first != __last)
142 {
143 typename iterator_traits<_InputIterator>::value_type __t(*__first);
144 *__result = __t;
Eric Fiselierb9919752014-10-27 19:28:20 +0000145 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000146 {
147 __t = __binary_op(__t, *__first);
148 *__result = __t;
149 }
150 }
151 return __result;
152}
153
154template <class _InputIterator, class _OutputIterator>
155inline _LIBCPP_INLINE_VISIBILITY
156_OutputIterator
157adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
158{
159 if (__first != __last)
160 {
161 typename iterator_traits<_InputIterator>::value_type __t1(*__first);
162 *__result = __t1;
Eric Fiselierb9919752014-10-27 19:28:20 +0000163 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000164 {
165 typename iterator_traits<_InputIterator>::value_type __t2(*__first);
166 *__result = __t2 - __t1;
Howard Hinnant1c44f512013-08-22 18:02:34 +0000167 __t1 = _VSTD::move(__t2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000168 }
169 }
170 return __result;
171}
172
173template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
174inline _LIBCPP_INLINE_VISIBILITY
175_OutputIterator
176adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
177 _BinaryOperation __binary_op)
178{
179 if (__first != __last)
180 {
181 typename iterator_traits<_InputIterator>::value_type __t1(*__first);
182 *__result = __t1;
Eric Fiselierb9919752014-10-27 19:28:20 +0000183 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000184 {
185 typename iterator_traits<_InputIterator>::value_type __t2(*__first);
186 *__result = __binary_op(__t2, __t1);
Howard Hinnant1c44f512013-08-22 18:02:34 +0000187 __t1 = _VSTD::move(__t2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000188 }
189 }
190 return __result;
191}
192
Howard Hinnantebef7452010-05-26 18:53:44 +0000193template <class _ForwardIterator, class _Tp>
194inline _LIBCPP_INLINE_VISIBILITY
195void
Howard Hinnant78b68282011-10-22 20:59:45 +0000196iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_)
Howard Hinnantebef7452010-05-26 18:53:44 +0000197{
Eric Fiselierb9919752014-10-27 19:28:20 +0000198 for (; __first != __last; ++__first, (void) ++__value_)
Howard Hinnant78b68282011-10-22 20:59:45 +0000199 *__first = __value_;
Howard Hinnantebef7452010-05-26 18:53:44 +0000200}
201
Marshall Clow1c1e91d2016-07-26 14:29:45 +0000202
203#if _LIBCPP_STD_VER > 14
204template <typename _Tp, bool _IsSigned = is_signed<_Tp>::value> struct __abs;
205
206template <typename _Tp>
207struct __abs<_Tp, true> {
208 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
209 _Tp operator()(_Tp __t) const noexcept { return __t >= 0 ? __t : -__t; }
210};
211
212template <typename _Tp>
213struct __abs<_Tp, false> {
214 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
215 _Tp operator()(_Tp __t) const noexcept { return __t; }
216};
217
218
219template<class _Tp>
220_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
221_Tp __gcd(_Tp __m, _Tp __n)
222{
223 static_assert((!is_signed<_Tp>::value), "" );
224 return __n == 0 ? __m : __gcd<_Tp>(__n, __m % __n);
225}
226
227
228template<class _Tp, class _Up>
229_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
230common_type_t<_Tp,_Up>
231gcd(_Tp __m, _Up __n)
232{
233 static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to gcd must be integer types");
Marshall Clowdb7fa112016-11-14 18:22:19 +0000234 static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to gcd cannot be bool" );
235 static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to gcd cannot be bool" );
Marshall Clow1c1e91d2016-07-26 14:29:45 +0000236 using _Rp = common_type_t<_Tp,_Up>;
237 using _Wp = make_unsigned_t<_Rp>;
238 return static_cast<_Rp>(__gcd(static_cast<_Wp>(__abs<_Tp>()(__m)),
239 static_cast<_Wp>(__abs<_Up>()(__n))));
240}
241
242template<class _Tp, class _Up>
243_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
244common_type_t<_Tp,_Up>
245lcm(_Tp __m, _Up __n)
246{
247 static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to lcm must be integer types");
Marshall Clowdb7fa112016-11-14 18:22:19 +0000248 static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to lcm cannot be bool" );
249 static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to lcm cannot be bool" );
Marshall Clow1c1e91d2016-07-26 14:29:45 +0000250 if (__m == 0 || __n == 0)
251 return 0;
252
253 using _Rp = common_type_t<_Tp,_Up>;
254 _Rp __val1 = __abs<_Tp>()(__m) / gcd(__m,__n);
255 _Up __val2 = __abs<_Up>()(__n);
256 _LIBCPP_ASSERT((numeric_limits<_Rp>::max() / __val1 > __val2), "Overflow in lcm");
257 return __val1 * __val2;
258}
259
260#endif /* _LIBCPP_STD_VER > 14 */
261
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000262_LIBCPP_END_NAMESPACE_STD
263
264#endif // _LIBCPP_NUMERIC