xref: /aosp_15_r20/external/libcxx/include/numeric (revision 58b9f456b02922dfdb1fad8a988d5fd8765ecb80)
1*58b9f456SAndroid Build Coastguard Worker// -*- C++ -*-
2*58b9f456SAndroid Build Coastguard Worker//===---------------------------- numeric ---------------------------------===//
3*58b9f456SAndroid Build Coastguard Worker//
4*58b9f456SAndroid Build Coastguard Worker//                     The LLVM Compiler Infrastructure
5*58b9f456SAndroid Build Coastguard Worker//
6*58b9f456SAndroid Build Coastguard Worker// This file is dual licensed under the MIT and the University of Illinois Open
7*58b9f456SAndroid Build Coastguard Worker// Source Licenses. See LICENSE.TXT for details.
8*58b9f456SAndroid Build Coastguard Worker//
9*58b9f456SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===//
10*58b9f456SAndroid Build Coastguard Worker
11*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_NUMERIC
12*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP_NUMERIC
13*58b9f456SAndroid Build Coastguard Worker
14*58b9f456SAndroid Build Coastguard Worker/*
15*58b9f456SAndroid Build Coastguard Worker    numeric synopsis
16*58b9f456SAndroid Build Coastguard Worker
17*58b9f456SAndroid Build Coastguard Workernamespace std
18*58b9f456SAndroid Build Coastguard Worker{
19*58b9f456SAndroid Build Coastguard Worker
20*58b9f456SAndroid Build Coastguard Workertemplate <class InputIterator, class T>
21*58b9f456SAndroid Build Coastguard Worker    T
22*58b9f456SAndroid Build Coastguard Worker    accumulate(InputIterator first, InputIterator last, T init);
23*58b9f456SAndroid Build Coastguard Worker
24*58b9f456SAndroid Build Coastguard Workertemplate <class InputIterator, class T, class BinaryOperation>
25*58b9f456SAndroid Build Coastguard Worker    T
26*58b9f456SAndroid Build Coastguard Worker    accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);
27*58b9f456SAndroid Build Coastguard Worker
28*58b9f456SAndroid Build Coastguard Workertemplate<class InputIterator>
29*58b9f456SAndroid Build Coastguard Worker    typename iterator_traits<InputIterator>::value_type
30*58b9f456SAndroid Build Coastguard Worker    reduce(InputIterator first, InputIterator last);  // C++17
31*58b9f456SAndroid Build Coastguard Worker
32*58b9f456SAndroid Build Coastguard Workertemplate<class InputIterator, class T>
33*58b9f456SAndroid Build Coastguard Worker    T
34*58b9f456SAndroid Build Coastguard Worker    reduce(InputIterator first, InputIterator last, T init);  // C++17
35*58b9f456SAndroid Build Coastguard Worker
36*58b9f456SAndroid Build Coastguard Workertemplate<class InputIterator, class T, class BinaryOperation>
37*58b9f456SAndroid Build Coastguard Worker    T
38*58b9f456SAndroid Build Coastguard Worker    reduce(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);  // C++17
39*58b9f456SAndroid Build Coastguard Worker
40*58b9f456SAndroid Build Coastguard Workertemplate <class InputIterator1, class InputIterator2, class T>
41*58b9f456SAndroid Build Coastguard Worker    T
42*58b9f456SAndroid Build Coastguard Worker    inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init);
43*58b9f456SAndroid Build Coastguard Worker
44*58b9f456SAndroid Build Coastguard Workertemplate <class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
45*58b9f456SAndroid Build Coastguard Worker    T
46*58b9f456SAndroid Build Coastguard Worker    inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
47*58b9f456SAndroid Build Coastguard Worker                  T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);
48*58b9f456SAndroid Build Coastguard Worker
49*58b9f456SAndroid Build Coastguard Worker
50*58b9f456SAndroid Build Coastguard Workertemplate<class InputIterator1, class InputIterator2, class T>
51*58b9f456SAndroid Build Coastguard Worker    T
52*58b9f456SAndroid Build Coastguard Worker    transform_reduce(InputIterator1 first1, InputIterator1 last1,
53*58b9f456SAndroid Build Coastguard Worker                     InputIterator2 first2, T init);  // C++17
54*58b9f456SAndroid Build Coastguard Worker
55*58b9f456SAndroid Build Coastguard Workertemplate<class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
56*58b9f456SAndroid Build Coastguard Worker    T
57*58b9f456SAndroid Build Coastguard Worker    transform_reduce(InputIterator1 first1, InputIterator1 last1,
58*58b9f456SAndroid Build Coastguard Worker                     InputIterator2 first2, T init,
59*58b9f456SAndroid Build Coastguard Worker                     BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);  // C++17
60*58b9f456SAndroid Build Coastguard Worker
61*58b9f456SAndroid Build Coastguard Workertemplate<class InputIterator, class T, class BinaryOperation, class UnaryOperation>
62*58b9f456SAndroid Build Coastguard Worker    T
63*58b9f456SAndroid Build Coastguard Worker    transform_reduce(InputIterator first, InputIterator last, T init,
64*58b9f456SAndroid Build Coastguard Worker                     BinaryOperation binary_op, UnaryOperation unary_op);  // C++17
65*58b9f456SAndroid Build Coastguard Worker
66*58b9f456SAndroid Build Coastguard Workertemplate <class InputIterator, class OutputIterator>
67*58b9f456SAndroid Build Coastguard Worker    OutputIterator
68*58b9f456SAndroid Build Coastguard Worker    partial_sum(InputIterator first, InputIterator last, OutputIterator result);
69*58b9f456SAndroid Build Coastguard Worker
70*58b9f456SAndroid Build Coastguard Workertemplate <class InputIterator, class OutputIterator, class BinaryOperation>
71*58b9f456SAndroid Build Coastguard Worker    OutputIterator
72*58b9f456SAndroid Build Coastguard Worker    partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
73*58b9f456SAndroid Build Coastguard Worker
74*58b9f456SAndroid Build Coastguard Workertemplate<class InputIterator, class OutputIterator, class T>
75*58b9f456SAndroid Build Coastguard Worker    OutputIterator
76*58b9f456SAndroid Build Coastguard Worker    exclusive_scan(InputIterator first, InputIterator last,
77*58b9f456SAndroid Build Coastguard Worker                   OutputIterator result, T init); // C++17
78*58b9f456SAndroid Build Coastguard Worker
79*58b9f456SAndroid Build Coastguard Workertemplate<class InputIterator, class OutputIterator, class T, class BinaryOperation>
80*58b9f456SAndroid Build Coastguard Worker    OutputIterator
81*58b9f456SAndroid Build Coastguard Worker    exclusive_scan(InputIterator first, InputIterator last,
82*58b9f456SAndroid Build Coastguard Worker                   OutputIterator result, T init, BinaryOperation binary_op); // C++17
83*58b9f456SAndroid Build Coastguard Worker
84*58b9f456SAndroid Build Coastguard Workertemplate<class InputIterator, class OutputIterator>
85*58b9f456SAndroid Build Coastguard Worker    OutputIterator
86*58b9f456SAndroid Build Coastguard Worker    inclusive_scan(InputIterator first, InputIterator last, OutputIterator result);  // C++17
87*58b9f456SAndroid Build Coastguard Worker
88*58b9f456SAndroid Build Coastguard Workertemplate<class InputIterator, class OutputIterator, class BinaryOperation>
89*58b9f456SAndroid Build Coastguard Worker    OutputIterator
90*58b9f456SAndroid Build Coastguard Worker    inclusive_scan(InputIterator first, InputIterator last,
91*58b9f456SAndroid Build Coastguard Worker                   OutputIterator result, BinaryOperation binary_op);  // C++17
92*58b9f456SAndroid Build Coastguard Worker
93*58b9f456SAndroid Build Coastguard Workertemplate<class InputIterator, class OutputIterator, class BinaryOperation, class T>
94*58b9f456SAndroid Build Coastguard Worker    OutputIterator
95*58b9f456SAndroid Build Coastguard Worker    inclusive_scan(InputIterator first, InputIterator last,
96*58b9f456SAndroid Build Coastguard Worker                   OutputIterator result, BinaryOperation binary_op, T init);  // C++17
97*58b9f456SAndroid Build Coastguard Worker
98*58b9f456SAndroid Build Coastguard Workertemplate<class InputIterator, class OutputIterator, class T,
99*58b9f456SAndroid Build Coastguard Worker         class BinaryOperation, class UnaryOperation>
100*58b9f456SAndroid Build Coastguard Worker    OutputIterator
101*58b9f456SAndroid Build Coastguard Worker    transform_exclusive_scan(InputIterator first, InputIterator last,
102*58b9f456SAndroid Build Coastguard Worker                             OutputIterator result, T init,
103*58b9f456SAndroid Build Coastguard Worker                             BinaryOperation binary_op, UnaryOperation unary_op);  // C++17
104*58b9f456SAndroid Build Coastguard Worker
105*58b9f456SAndroid Build Coastguard Workertemplate<class InputIterator, class OutputIterator,
106*58b9f456SAndroid Build Coastguard Worker         class BinaryOperation, class UnaryOperation>
107*58b9f456SAndroid Build Coastguard Worker    OutputIterator
108*58b9f456SAndroid Build Coastguard Worker    transform_inclusive_scan(InputIterator first, InputIterator last,
109*58b9f456SAndroid Build Coastguard Worker                             OutputIterator result,
110*58b9f456SAndroid Build Coastguard Worker                             BinaryOperation binary_op, UnaryOperation unary_op);  // C++17
111*58b9f456SAndroid Build Coastguard Worker
112*58b9f456SAndroid Build Coastguard Workertemplate<class InputIterator, class OutputIterator,
113*58b9f456SAndroid Build Coastguard Worker         class BinaryOperation, class UnaryOperation, class T>
114*58b9f456SAndroid Build Coastguard Worker    OutputIterator
115*58b9f456SAndroid Build Coastguard Worker    transform_inclusive_scan(InputIterator first, InputIterator last,
116*58b9f456SAndroid Build Coastguard Worker                             OutputIterator result,
117*58b9f456SAndroid Build Coastguard Worker                             BinaryOperation binary_op, UnaryOperation unary_op,
118*58b9f456SAndroid Build Coastguard Worker                             T init);  // C++17
119*58b9f456SAndroid Build Coastguard Worker
120*58b9f456SAndroid Build Coastguard Workertemplate <class InputIterator, class OutputIterator>
121*58b9f456SAndroid Build Coastguard Worker    OutputIterator
122*58b9f456SAndroid Build Coastguard Worker    adjacent_difference(InputIterator first, InputIterator last, OutputIterator result);
123*58b9f456SAndroid Build Coastguard Worker
124*58b9f456SAndroid Build Coastguard Workertemplate <class InputIterator, class OutputIterator, class BinaryOperation>
125*58b9f456SAndroid Build Coastguard Worker    OutputIterator
126*58b9f456SAndroid Build Coastguard Worker    adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
127*58b9f456SAndroid Build Coastguard Worker
128*58b9f456SAndroid Build Coastguard Workertemplate <class ForwardIterator, class T>
129*58b9f456SAndroid Build Coastguard Worker    void iota(ForwardIterator first, ForwardIterator last, T value);
130*58b9f456SAndroid Build Coastguard Worker
131*58b9f456SAndroid Build Coastguard Workertemplate <class M, class N>
132*58b9f456SAndroid Build Coastguard Worker    constexpr common_type_t<M,N> gcd(M m, N n);    // C++17
133*58b9f456SAndroid Build Coastguard Worker
134*58b9f456SAndroid Build Coastguard Workertemplate <class M, class N>
135*58b9f456SAndroid Build Coastguard Worker    constexpr common_type_t<M,N> lcm(M m, N n);    // C++17
136*58b9f456SAndroid Build Coastguard Worker
137*58b9f456SAndroid Build Coastguard Worker}  // std
138*58b9f456SAndroid Build Coastguard Worker
139*58b9f456SAndroid Build Coastguard Worker*/
140*58b9f456SAndroid Build Coastguard Worker
141*58b9f456SAndroid Build Coastguard Worker#include <__config>
142*58b9f456SAndroid Build Coastguard Worker#include <iterator>
143*58b9f456SAndroid Build Coastguard Worker#include <limits> // for numeric_limits
144*58b9f456SAndroid Build Coastguard Worker#include <functional>
145*58b9f456SAndroid Build Coastguard Worker#include <version>
146*58b9f456SAndroid Build Coastguard Worker
147*58b9f456SAndroid Build Coastguard Worker#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
148*58b9f456SAndroid Build Coastguard Worker#pragma GCC system_header
149*58b9f456SAndroid Build Coastguard Worker#endif
150*58b9f456SAndroid Build Coastguard Worker
151*58b9f456SAndroid Build Coastguard Worker_LIBCPP_PUSH_MACROS
152*58b9f456SAndroid Build Coastguard Worker#include <__undef_macros>
153*58b9f456SAndroid Build Coastguard Worker
154*58b9f456SAndroid Build Coastguard Worker_LIBCPP_BEGIN_NAMESPACE_STD
155*58b9f456SAndroid Build Coastguard Worker
156*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator, class _Tp>
157*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
158*58b9f456SAndroid Build Coastguard Worker_Tp
159*58b9f456SAndroid Build Coastguard Workeraccumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
160*58b9f456SAndroid Build Coastguard Worker{
161*58b9f456SAndroid Build Coastguard Worker    for (; __first != __last; ++__first)
162*58b9f456SAndroid Build Coastguard Worker        __init = __init + *__first;
163*58b9f456SAndroid Build Coastguard Worker    return __init;
164*58b9f456SAndroid Build Coastguard Worker}
165*58b9f456SAndroid Build Coastguard Worker
166*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator, class _Tp, class _BinaryOperation>
167*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
168*58b9f456SAndroid Build Coastguard Worker_Tp
169*58b9f456SAndroid Build Coastguard Workeraccumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
170*58b9f456SAndroid Build Coastguard Worker{
171*58b9f456SAndroid Build Coastguard Worker    for (; __first != __last; ++__first)
172*58b9f456SAndroid Build Coastguard Worker        __init = __binary_op(__init, *__first);
173*58b9f456SAndroid Build Coastguard Worker    return __init;
174*58b9f456SAndroid Build Coastguard Worker}
175*58b9f456SAndroid Build Coastguard Worker
176*58b9f456SAndroid Build Coastguard Worker#if _LIBCPP_STD_VER > 14
177*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator, class _Tp, class _BinaryOp>
178*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
179*58b9f456SAndroid Build Coastguard Worker_Tp
180*58b9f456SAndroid Build Coastguard Workerreduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOp __b)
181*58b9f456SAndroid Build Coastguard Worker{
182*58b9f456SAndroid Build Coastguard Worker    for (; __first != __last; ++__first)
183*58b9f456SAndroid Build Coastguard Worker        __init = __b(__init, *__first);
184*58b9f456SAndroid Build Coastguard Worker    return __init;
185*58b9f456SAndroid Build Coastguard Worker}
186*58b9f456SAndroid Build Coastguard Worker
187*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator, class _Tp>
188*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
189*58b9f456SAndroid Build Coastguard Worker_Tp
190*58b9f456SAndroid Build Coastguard Workerreduce(_InputIterator __first, _InputIterator __last, _Tp __init)
191*58b9f456SAndroid Build Coastguard Worker{
192*58b9f456SAndroid Build Coastguard Worker    return _VSTD::reduce(__first, __last, __init, _VSTD::plus<>());
193*58b9f456SAndroid Build Coastguard Worker}
194*58b9f456SAndroid Build Coastguard Worker
195*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator>
196*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
197*58b9f456SAndroid Build Coastguard Workertypename iterator_traits<_InputIterator>::value_type
198*58b9f456SAndroid Build Coastguard Workerreduce(_InputIterator __first, _InputIterator __last)
199*58b9f456SAndroid Build Coastguard Worker{
200*58b9f456SAndroid Build Coastguard Worker    return _VSTD::reduce(__first, __last,
201*58b9f456SAndroid Build Coastguard Worker       typename iterator_traits<_InputIterator>::value_type{});
202*58b9f456SAndroid Build Coastguard Worker}
203*58b9f456SAndroid Build Coastguard Worker#endif
204*58b9f456SAndroid Build Coastguard Worker
205*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator1, class _InputIterator2, class _Tp>
206*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
207*58b9f456SAndroid Build Coastguard Worker_Tp
208*58b9f456SAndroid Build Coastguard Workerinner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init)
209*58b9f456SAndroid Build Coastguard Worker{
210*58b9f456SAndroid Build Coastguard Worker    for (; __first1 != __last1; ++__first1, (void) ++__first2)
211*58b9f456SAndroid Build Coastguard Worker        __init = __init + *__first1 * *__first2;
212*58b9f456SAndroid Build Coastguard Worker    return __init;
213*58b9f456SAndroid Build Coastguard Worker}
214*58b9f456SAndroid Build Coastguard Worker
215*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
216*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
217*58b9f456SAndroid Build Coastguard Worker_Tp
218*58b9f456SAndroid Build Coastguard Workerinner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
219*58b9f456SAndroid Build Coastguard Worker              _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2)
220*58b9f456SAndroid Build Coastguard Worker{
221*58b9f456SAndroid Build Coastguard Worker    for (; __first1 != __last1; ++__first1, (void) ++__first2)
222*58b9f456SAndroid Build Coastguard Worker        __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
223*58b9f456SAndroid Build Coastguard Worker    return __init;
224*58b9f456SAndroid Build Coastguard Worker}
225*58b9f456SAndroid Build Coastguard Worker
226*58b9f456SAndroid Build Coastguard Worker#if _LIBCPP_STD_VER > 14
227*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator, class _Tp, class _BinaryOp, class _UnaryOp>
228*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
229*58b9f456SAndroid Build Coastguard Worker_Tp
230*58b9f456SAndroid Build Coastguard Workertransform_reduce(_InputIterator __first, _InputIterator __last,
231*58b9f456SAndroid Build Coastguard Worker           _Tp __init,  _BinaryOp __b, _UnaryOp __u)
232*58b9f456SAndroid Build Coastguard Worker{
233*58b9f456SAndroid Build Coastguard Worker    for (; __first != __last; ++__first)
234*58b9f456SAndroid Build Coastguard Worker        __init = __b(__init, __u(*__first));
235*58b9f456SAndroid Build Coastguard Worker    return __init;
236*58b9f456SAndroid Build Coastguard Worker}
237*58b9f456SAndroid Build Coastguard Worker
238*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator1, class _InputIterator2,
239*58b9f456SAndroid Build Coastguard Worker          class _Tp, class _BinaryOp1, class _BinaryOp2>
240*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
241*58b9f456SAndroid Build Coastguard Worker_Tp
242*58b9f456SAndroid Build Coastguard Workertransform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
243*58b9f456SAndroid Build Coastguard Worker                 _InputIterator2 __first2, _Tp __init,  _BinaryOp1 __b1, _BinaryOp2 __b2)
244*58b9f456SAndroid Build Coastguard Worker{
245*58b9f456SAndroid Build Coastguard Worker    for (; __first1 != __last1; ++__first1, (void) ++__first2)
246*58b9f456SAndroid Build Coastguard Worker        __init = __b1(__init, __b2(*__first1, *__first2));
247*58b9f456SAndroid Build Coastguard Worker    return __init;
248*58b9f456SAndroid Build Coastguard Worker}
249*58b9f456SAndroid Build Coastguard Worker
250*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator1, class _InputIterator2, class _Tp>
251*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
252*58b9f456SAndroid Build Coastguard Worker_Tp
253*58b9f456SAndroid Build Coastguard Workertransform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
254*58b9f456SAndroid Build Coastguard Worker                 _InputIterator2 __first2, _Tp __init)
255*58b9f456SAndroid Build Coastguard Worker{
256*58b9f456SAndroid Build Coastguard Worker    return _VSTD::transform_reduce(__first1, __last1, __first2, _VSTD::move(__init),
257*58b9f456SAndroid Build Coastguard Worker                                   _VSTD::plus<>(), _VSTD::multiplies<>());
258*58b9f456SAndroid Build Coastguard Worker}
259*58b9f456SAndroid Build Coastguard Worker#endif
260*58b9f456SAndroid Build Coastguard Worker
261*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator, class _OutputIterator>
262*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
263*58b9f456SAndroid Build Coastguard Worker_OutputIterator
264*58b9f456SAndroid Build Coastguard Workerpartial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
265*58b9f456SAndroid Build Coastguard Worker{
266*58b9f456SAndroid Build Coastguard Worker    if (__first != __last)
267*58b9f456SAndroid Build Coastguard Worker    {
268*58b9f456SAndroid Build Coastguard Worker        typename iterator_traits<_InputIterator>::value_type __t(*__first);
269*58b9f456SAndroid Build Coastguard Worker        *__result = __t;
270*58b9f456SAndroid Build Coastguard Worker        for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
271*58b9f456SAndroid Build Coastguard Worker        {
272*58b9f456SAndroid Build Coastguard Worker            __t = __t + *__first;
273*58b9f456SAndroid Build Coastguard Worker            *__result = __t;
274*58b9f456SAndroid Build Coastguard Worker        }
275*58b9f456SAndroid Build Coastguard Worker    }
276*58b9f456SAndroid Build Coastguard Worker    return __result;
277*58b9f456SAndroid Build Coastguard Worker}
278*58b9f456SAndroid Build Coastguard Worker
279*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator, class _OutputIterator, class _BinaryOperation>
280*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
281*58b9f456SAndroid Build Coastguard Worker_OutputIterator
282*58b9f456SAndroid Build Coastguard Workerpartial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
283*58b9f456SAndroid Build Coastguard Worker              _BinaryOperation __binary_op)
284*58b9f456SAndroid Build Coastguard Worker{
285*58b9f456SAndroid Build Coastguard Worker    if (__first != __last)
286*58b9f456SAndroid Build Coastguard Worker    {
287*58b9f456SAndroid Build Coastguard Worker        typename iterator_traits<_InputIterator>::value_type __t(*__first);
288*58b9f456SAndroid Build Coastguard Worker        *__result = __t;
289*58b9f456SAndroid Build Coastguard Worker        for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
290*58b9f456SAndroid Build Coastguard Worker        {
291*58b9f456SAndroid Build Coastguard Worker            __t = __binary_op(__t, *__first);
292*58b9f456SAndroid Build Coastguard Worker            *__result = __t;
293*58b9f456SAndroid Build Coastguard Worker        }
294*58b9f456SAndroid Build Coastguard Worker    }
295*58b9f456SAndroid Build Coastguard Worker    return __result;
296*58b9f456SAndroid Build Coastguard Worker}
297*58b9f456SAndroid Build Coastguard Worker
298*58b9f456SAndroid Build Coastguard Worker#if _LIBCPP_STD_VER > 14
299*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
300*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
301*58b9f456SAndroid Build Coastguard Worker_OutputIterator
302*58b9f456SAndroid Build Coastguard Workerexclusive_scan(_InputIterator __first, _InputIterator __last,
303*58b9f456SAndroid Build Coastguard Worker               _OutputIterator __result, _Tp __init, _BinaryOp __b)
304*58b9f456SAndroid Build Coastguard Worker{
305*58b9f456SAndroid Build Coastguard Worker    if (__first != __last)
306*58b9f456SAndroid Build Coastguard Worker    {
307*58b9f456SAndroid Build Coastguard Worker        _Tp __saved = __init;
308*58b9f456SAndroid Build Coastguard Worker        do
309*58b9f456SAndroid Build Coastguard Worker        {
310*58b9f456SAndroid Build Coastguard Worker            __init = __b(__init, *__first);
311*58b9f456SAndroid Build Coastguard Worker            *__result = __saved;
312*58b9f456SAndroid Build Coastguard Worker            __saved = __init;
313*58b9f456SAndroid Build Coastguard Worker            ++__result;
314*58b9f456SAndroid Build Coastguard Worker        } while (++__first != __last);
315*58b9f456SAndroid Build Coastguard Worker    }
316*58b9f456SAndroid Build Coastguard Worker    return __result;
317*58b9f456SAndroid Build Coastguard Worker}
318*58b9f456SAndroid Build Coastguard Worker
319*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator, class _OutputIterator, class _Tp>
320*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
321*58b9f456SAndroid Build Coastguard Worker_OutputIterator
322*58b9f456SAndroid Build Coastguard Workerexclusive_scan(_InputIterator __first, _InputIterator __last,
323*58b9f456SAndroid Build Coastguard Worker               _OutputIterator __result, _Tp __init)
324*58b9f456SAndroid Build Coastguard Worker{
325*58b9f456SAndroid Build Coastguard Worker    return _VSTD::exclusive_scan(__first, __last, __result, __init, _VSTD::plus<>());
326*58b9f456SAndroid Build Coastguard Worker}
327*58b9f456SAndroid Build Coastguard Worker
328*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
329*58b9f456SAndroid Build Coastguard Worker_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
330*58b9f456SAndroid Build Coastguard Worker                               _OutputIterator __result, _BinaryOp __b,  _Tp __init)
331*58b9f456SAndroid Build Coastguard Worker{
332*58b9f456SAndroid Build Coastguard Worker    for (; __first != __last; ++__first, (void) ++__result) {
333*58b9f456SAndroid Build Coastguard Worker        __init = __b(__init, *__first);
334*58b9f456SAndroid Build Coastguard Worker        *__result = __init;
335*58b9f456SAndroid Build Coastguard Worker        }
336*58b9f456SAndroid Build Coastguard Worker    return __result;
337*58b9f456SAndroid Build Coastguard Worker}
338*58b9f456SAndroid Build Coastguard Worker
339*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator, class _OutputIterator, class _BinaryOp>
340*58b9f456SAndroid Build Coastguard Worker_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
341*58b9f456SAndroid Build Coastguard Worker                               _OutputIterator __result, _BinaryOp __b)
342*58b9f456SAndroid Build Coastguard Worker{
343*58b9f456SAndroid Build Coastguard Worker    if (__first != __last) {
344*58b9f456SAndroid Build Coastguard Worker        typename std::iterator_traits<_InputIterator>::value_type __init = *__first;
345*58b9f456SAndroid Build Coastguard Worker        *__result++ = __init;
346*58b9f456SAndroid Build Coastguard Worker        if (++__first != __last)
347*58b9f456SAndroid Build Coastguard Worker            return _VSTD::inclusive_scan(__first, __last, __result, __b, __init);
348*58b9f456SAndroid Build Coastguard Worker        }
349*58b9f456SAndroid Build Coastguard Worker
350*58b9f456SAndroid Build Coastguard Worker    return __result;
351*58b9f456SAndroid Build Coastguard Worker}
352*58b9f456SAndroid Build Coastguard Worker
353*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator, class _OutputIterator>
354*58b9f456SAndroid Build Coastguard Worker_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
355*58b9f456SAndroid Build Coastguard Worker                               _OutputIterator __result)
356*58b9f456SAndroid Build Coastguard Worker{
357*58b9f456SAndroid Build Coastguard Worker    return _VSTD::inclusive_scan(__first, __last, __result, std::plus<>());
358*58b9f456SAndroid Build Coastguard Worker}
359*58b9f456SAndroid Build Coastguard Worker
360*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator, class _OutputIterator, class _Tp,
361*58b9f456SAndroid Build Coastguard Worker          class _BinaryOp, class _UnaryOp>
362*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
363*58b9f456SAndroid Build Coastguard Worker_OutputIterator
364*58b9f456SAndroid Build Coastguard Workertransform_exclusive_scan(_InputIterator __first, _InputIterator __last,
365*58b9f456SAndroid Build Coastguard Worker                           _OutputIterator __result, _Tp __init,
366*58b9f456SAndroid Build Coastguard Worker                           _BinaryOp __b, _UnaryOp __u)
367*58b9f456SAndroid Build Coastguard Worker{
368*58b9f456SAndroid Build Coastguard Worker    if (__first != __last)
369*58b9f456SAndroid Build Coastguard Worker    {
370*58b9f456SAndroid Build Coastguard Worker        _Tp __saved = __init;
371*58b9f456SAndroid Build Coastguard Worker        do
372*58b9f456SAndroid Build Coastguard Worker        {
373*58b9f456SAndroid Build Coastguard Worker            __init = __b(__init, __u(*__first));
374*58b9f456SAndroid Build Coastguard Worker            *__result = __saved;
375*58b9f456SAndroid Build Coastguard Worker            __saved = __init;
376*58b9f456SAndroid Build Coastguard Worker            ++__result;
377*58b9f456SAndroid Build Coastguard Worker        } while (++__first != __last);
378*58b9f456SAndroid Build Coastguard Worker    }
379*58b9f456SAndroid Build Coastguard Worker    return __result;
380*58b9f456SAndroid Build Coastguard Worker}
381*58b9f456SAndroid Build Coastguard Worker
382*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp, class _UnaryOp>
383*58b9f456SAndroid Build Coastguard Worker_OutputIterator transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
384*58b9f456SAndroid Build Coastguard Worker                           _OutputIterator __result, _BinaryOp __b, _UnaryOp __u, _Tp __init)
385*58b9f456SAndroid Build Coastguard Worker{
386*58b9f456SAndroid Build Coastguard Worker    for (; __first != __last; ++__first, (void) ++__result) {
387*58b9f456SAndroid Build Coastguard Worker        __init = __b(__init, __u(*__first));
388*58b9f456SAndroid Build Coastguard Worker        *__result = __init;
389*58b9f456SAndroid Build Coastguard Worker        }
390*58b9f456SAndroid Build Coastguard Worker
391*58b9f456SAndroid Build Coastguard Worker    return __result;
392*58b9f456SAndroid Build Coastguard Worker}
393*58b9f456SAndroid Build Coastguard Worker
394*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator, class _OutputIterator, class _BinaryOp, class _UnaryOp>
395*58b9f456SAndroid Build Coastguard Worker_OutputIterator transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
396*58b9f456SAndroid Build Coastguard Worker                               _OutputIterator __result, _BinaryOp __b, _UnaryOp __u)
397*58b9f456SAndroid Build Coastguard Worker{
398*58b9f456SAndroid Build Coastguard Worker    if (__first != __last) {
399*58b9f456SAndroid Build Coastguard Worker        typename std::iterator_traits<_InputIterator>::value_type __init = __u(*__first);
400*58b9f456SAndroid Build Coastguard Worker        *__result++ = __init;
401*58b9f456SAndroid Build Coastguard Worker        if (++__first != __last)
402*58b9f456SAndroid Build Coastguard Worker            return _VSTD::transform_inclusive_scan(__first, __last, __result, __b, __u, __init);
403*58b9f456SAndroid Build Coastguard Worker        }
404*58b9f456SAndroid Build Coastguard Worker
405*58b9f456SAndroid Build Coastguard Worker    return __result;
406*58b9f456SAndroid Build Coastguard Worker}
407*58b9f456SAndroid Build Coastguard Worker#endif
408*58b9f456SAndroid Build Coastguard Worker
409*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator, class _OutputIterator>
410*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
411*58b9f456SAndroid Build Coastguard Worker_OutputIterator
412*58b9f456SAndroid Build Coastguard Workeradjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
413*58b9f456SAndroid Build Coastguard Worker{
414*58b9f456SAndroid Build Coastguard Worker    if (__first != __last)
415*58b9f456SAndroid Build Coastguard Worker    {
416*58b9f456SAndroid Build Coastguard Worker        typename iterator_traits<_InputIterator>::value_type __t1(*__first);
417*58b9f456SAndroid Build Coastguard Worker        *__result = __t1;
418*58b9f456SAndroid Build Coastguard Worker        for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
419*58b9f456SAndroid Build Coastguard Worker        {
420*58b9f456SAndroid Build Coastguard Worker            typename iterator_traits<_InputIterator>::value_type __t2(*__first);
421*58b9f456SAndroid Build Coastguard Worker            *__result = __t2 - __t1;
422*58b9f456SAndroid Build Coastguard Worker            __t1 = _VSTD::move(__t2);
423*58b9f456SAndroid Build Coastguard Worker        }
424*58b9f456SAndroid Build Coastguard Worker    }
425*58b9f456SAndroid Build Coastguard Worker    return __result;
426*58b9f456SAndroid Build Coastguard Worker}
427*58b9f456SAndroid Build Coastguard Worker
428*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator, class _OutputIterator, class _BinaryOperation>
429*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
430*58b9f456SAndroid Build Coastguard Worker_OutputIterator
431*58b9f456SAndroid Build Coastguard Workeradjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
432*58b9f456SAndroid Build Coastguard Worker                      _BinaryOperation __binary_op)
433*58b9f456SAndroid Build Coastguard Worker{
434*58b9f456SAndroid Build Coastguard Worker    if (__first != __last)
435*58b9f456SAndroid Build Coastguard Worker    {
436*58b9f456SAndroid Build Coastguard Worker        typename iterator_traits<_InputIterator>::value_type __t1(*__first);
437*58b9f456SAndroid Build Coastguard Worker        *__result = __t1;
438*58b9f456SAndroid Build Coastguard Worker        for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
439*58b9f456SAndroid Build Coastguard Worker        {
440*58b9f456SAndroid Build Coastguard Worker            typename iterator_traits<_InputIterator>::value_type __t2(*__first);
441*58b9f456SAndroid Build Coastguard Worker            *__result = __binary_op(__t2, __t1);
442*58b9f456SAndroid Build Coastguard Worker            __t1 = _VSTD::move(__t2);
443*58b9f456SAndroid Build Coastguard Worker        }
444*58b9f456SAndroid Build Coastguard Worker    }
445*58b9f456SAndroid Build Coastguard Worker    return __result;
446*58b9f456SAndroid Build Coastguard Worker}
447*58b9f456SAndroid Build Coastguard Worker
448*58b9f456SAndroid Build Coastguard Workertemplate <class _ForwardIterator, class _Tp>
449*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
450*58b9f456SAndroid Build Coastguard Workervoid
451*58b9f456SAndroid Build Coastguard Workeriota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_)
452*58b9f456SAndroid Build Coastguard Worker{
453*58b9f456SAndroid Build Coastguard Worker    for (; __first != __last; ++__first, (void) ++__value_)
454*58b9f456SAndroid Build Coastguard Worker        *__first = __value_;
455*58b9f456SAndroid Build Coastguard Worker}
456*58b9f456SAndroid Build Coastguard Worker
457*58b9f456SAndroid Build Coastguard Worker
458*58b9f456SAndroid Build Coastguard Worker#if _LIBCPP_STD_VER > 14
459*58b9f456SAndroid Build Coastguard Workertemplate <typename _Result, typename _Source, bool _IsSigned = is_signed<_Source>::value> struct __abs;
460*58b9f456SAndroid Build Coastguard Worker
461*58b9f456SAndroid Build Coastguard Workertemplate <typename _Result, typename _Source>
462*58b9f456SAndroid Build Coastguard Workerstruct __abs<_Result, _Source, true> {
463*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
464*58b9f456SAndroid Build Coastguard Worker    _Result operator()(_Source __t) const noexcept
465*58b9f456SAndroid Build Coastguard Worker    {
466*58b9f456SAndroid Build Coastguard Worker    if (__t >= 0) return __t;
467*58b9f456SAndroid Build Coastguard Worker    if (__t == numeric_limits<_Source>::min()) return -static_cast<_Result>(__t);
468*58b9f456SAndroid Build Coastguard Worker    return -__t;
469*58b9f456SAndroid Build Coastguard Worker    }
470*58b9f456SAndroid Build Coastguard Worker};
471*58b9f456SAndroid Build Coastguard Worker
472*58b9f456SAndroid Build Coastguard Workertemplate <typename _Result, typename _Source>
473*58b9f456SAndroid Build Coastguard Workerstruct __abs<_Result, _Source, false> {
474*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
475*58b9f456SAndroid Build Coastguard Worker    _Result operator()(_Source __t) const noexcept { return __t; }
476*58b9f456SAndroid Build Coastguard Worker};
477*58b9f456SAndroid Build Coastguard Worker
478*58b9f456SAndroid Build Coastguard Worker
479*58b9f456SAndroid Build Coastguard Workertemplate<class _Tp>
480*58b9f456SAndroid Build Coastguard Worker_LIBCPP_CONSTEXPR _LIBCPP_HIDDEN
481*58b9f456SAndroid Build Coastguard Worker_Tp __gcd(_Tp __m, _Tp __n)
482*58b9f456SAndroid Build Coastguard Worker{
483*58b9f456SAndroid Build Coastguard Worker    static_assert((!is_signed<_Tp>::value), "");
484*58b9f456SAndroid Build Coastguard Worker    return __n == 0 ? __m : _VSTD::__gcd<_Tp>(__n, __m % __n);
485*58b9f456SAndroid Build Coastguard Worker}
486*58b9f456SAndroid Build Coastguard Worker
487*58b9f456SAndroid Build Coastguard Worker
488*58b9f456SAndroid Build Coastguard Workertemplate<class _Tp, class _Up>
489*58b9f456SAndroid Build Coastguard Worker_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
490*58b9f456SAndroid Build Coastguard Workercommon_type_t<_Tp,_Up>
491*58b9f456SAndroid Build Coastguard Workergcd(_Tp __m, _Up __n)
492*58b9f456SAndroid Build Coastguard Worker{
493*58b9f456SAndroid Build Coastguard Worker    static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to gcd must be integer types");
494*58b9f456SAndroid Build Coastguard Worker    static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to gcd cannot be bool" );
495*58b9f456SAndroid Build Coastguard Worker    static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to gcd cannot be bool" );
496*58b9f456SAndroid Build Coastguard Worker    using _Rp = common_type_t<_Tp,_Up>;
497*58b9f456SAndroid Build Coastguard Worker    using _Wp = make_unsigned_t<_Rp>;
498*58b9f456SAndroid Build Coastguard Worker    return static_cast<_Rp>(_VSTD::__gcd(
499*58b9f456SAndroid Build Coastguard Worker        static_cast<_Wp>(__abs<_Rp, _Tp>()(__m)),
500*58b9f456SAndroid Build Coastguard Worker        static_cast<_Wp>(__abs<_Rp, _Up>()(__n))));
501*58b9f456SAndroid Build Coastguard Worker}
502*58b9f456SAndroid Build Coastguard Worker
503*58b9f456SAndroid Build Coastguard Workertemplate<class _Tp, class _Up>
504*58b9f456SAndroid Build Coastguard Worker_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
505*58b9f456SAndroid Build Coastguard Workercommon_type_t<_Tp,_Up>
506*58b9f456SAndroid Build Coastguard Workerlcm(_Tp __m, _Up __n)
507*58b9f456SAndroid Build Coastguard Worker{
508*58b9f456SAndroid Build Coastguard Worker    static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to lcm must be integer types");
509*58b9f456SAndroid Build Coastguard Worker    static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to lcm cannot be bool" );
510*58b9f456SAndroid Build Coastguard Worker    static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to lcm cannot be bool" );
511*58b9f456SAndroid Build Coastguard Worker    if (__m == 0 || __n == 0)
512*58b9f456SAndroid Build Coastguard Worker        return 0;
513*58b9f456SAndroid Build Coastguard Worker
514*58b9f456SAndroid Build Coastguard Worker    using _Rp = common_type_t<_Tp,_Up>;
515*58b9f456SAndroid Build Coastguard Worker    _Rp __val1 = __abs<_Rp, _Tp>()(__m) / _VSTD::gcd(__m, __n);
516*58b9f456SAndroid Build Coastguard Worker    _Rp __val2 = __abs<_Rp, _Up>()(__n);
517*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_ASSERT((numeric_limits<_Rp>::max() / __val1 > __val2), "Overflow in lcm");
518*58b9f456SAndroid Build Coastguard Worker    return __val1 * __val2;
519*58b9f456SAndroid Build Coastguard Worker}
520*58b9f456SAndroid Build Coastguard Worker
521*58b9f456SAndroid Build Coastguard Worker#endif /* _LIBCPP_STD_VER > 14 */
522*58b9f456SAndroid Build Coastguard Worker
523*58b9f456SAndroid Build Coastguard Worker_LIBCPP_END_NAMESPACE_STD
524*58b9f456SAndroid Build Coastguard Worker
525*58b9f456SAndroid Build Coastguard Worker_LIBCPP_POP_MACROS
526*58b9f456SAndroid Build Coastguard Worker
527*58b9f456SAndroid Build Coastguard Worker#endif  // _LIBCPP_NUMERIC
528