13e519524SHoward Hinnant// -*- C++ -*-
23e519524SHoward Hinnant//===---------------------------- numeric ---------------------------------===//
33e519524SHoward Hinnant//
45b08a8a4SHoward Hinnant//                     The LLVM Compiler Infrastructure
53e519524SHoward Hinnant//
6412dbebeSHoward Hinnant// This file is dual licensed under the MIT and the University of Illinois Open
7412dbebeSHoward Hinnant// Source Licenses. See LICENSE.TXT for details.
83e519524SHoward Hinnant//
93e519524SHoward Hinnant//===----------------------------------------------------------------------===//
103e519524SHoward Hinnant
113e519524SHoward Hinnant#ifndef _LIBCPP_NUMERIC
123e519524SHoward Hinnant#define _LIBCPP_NUMERIC
133e519524SHoward Hinnant
143e519524SHoward Hinnant/*
153e519524SHoward Hinnant    numeric synopsis
163e519524SHoward Hinnant
173e519524SHoward Hinnantnamespace std
183e519524SHoward Hinnant{
193e519524SHoward Hinnant
203e519524SHoward Hinnanttemplate <class InputIterator, class T>
213e519524SHoward Hinnant    T
223e519524SHoward Hinnant    accumulate(InputIterator first, InputIterator last, T init);
233e519524SHoward Hinnant
243e519524SHoward Hinnanttemplate <class InputIterator, class T, class BinaryOperation>
253e519524SHoward Hinnant    T
263e519524SHoward Hinnant    accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);
273e519524SHoward Hinnant
28f4ea23d3SMarshall Clowtemplate<class InputIterator>
29f4ea23d3SMarshall Clow    typename iterator_traits<InputIterator>::value_type
30f4ea23d3SMarshall Clow    reduce(InputIterator first, InputIterator last);  // C++17
31f4ea23d3SMarshall Clow
32f4ea23d3SMarshall Clowtemplate<class InputIterator, class T>
33f4ea23d3SMarshall Clow    T
34f4ea23d3SMarshall Clow    reduce(InputIterator first, InputIterator last, T init);  // C++17
35f4ea23d3SMarshall Clow
36f4ea23d3SMarshall Clowtemplate<class InputIterator, class T, class BinaryOperation>
37f4ea23d3SMarshall Clow    T
38f4ea23d3SMarshall Clow    reduce(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);  // C++17
39f4ea23d3SMarshall Clow
403e519524SHoward Hinnanttemplate <class InputIterator1, class InputIterator2, class T>
413e519524SHoward Hinnant    T
423e519524SHoward Hinnant    inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init);
433e519524SHoward Hinnant
443e519524SHoward Hinnanttemplate <class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
453e519524SHoward Hinnant    T
463e519524SHoward Hinnant    inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
473e519524SHoward Hinnant                  T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);
483e519524SHoward Hinnant
49f4ea23d3SMarshall Clow
50f4ea23d3SMarshall Clowtemplate<class InputIterator1, class InputIterator2, class T>
51f4ea23d3SMarshall Clow    T
52f4ea23d3SMarshall Clow    transform_reduce(InputIterator1 first1, InputIterator1 last1,
53f4ea23d3SMarshall Clow                     InputIterator2 first2, T init);  // C++17
54f4ea23d3SMarshall Clow
55f4ea23d3SMarshall Clowtemplate<class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
56f4ea23d3SMarshall Clow    T
57f4ea23d3SMarshall Clow    transform_reduce(InputIterator1 first1, InputIterator1 last1,
58f4ea23d3SMarshall Clow                     InputIterator2 first2, T init,
59f4ea23d3SMarshall Clow                     BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);  // C++17
60f4ea23d3SMarshall Clow
61f4ea23d3SMarshall Clowtemplate<class InputIterator, class T, class BinaryOperation, class UnaryOperation>
62f4ea23d3SMarshall Clow    T
63f4ea23d3SMarshall Clow    transform_reduce(InputIterator first, InputIterator last, T init,
64f4ea23d3SMarshall Clow                     BinaryOperation binary_op, UnaryOperation unary_op);  // C++17
65f4ea23d3SMarshall Clow
663e519524SHoward Hinnanttemplate <class InputIterator, class OutputIterator>
673e519524SHoward Hinnant    OutputIterator
683e519524SHoward Hinnant    partial_sum(InputIterator first, InputIterator last, OutputIterator result);
693e519524SHoward Hinnant
703e519524SHoward Hinnanttemplate <class InputIterator, class OutputIterator, class BinaryOperation>
713e519524SHoward Hinnant    OutputIterator
723e519524SHoward Hinnant    partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
733e519524SHoward Hinnant
74e948ba1cSMarshall Clowtemplate<class InputIterator, class OutputIterator, class T>
75e948ba1cSMarshall Clow    OutputIterator
76e948ba1cSMarshall Clow    exclusive_scan(InputIterator first, InputIterator last,
77e948ba1cSMarshall Clow                   OutputIterator result, T init); // C++17
78e948ba1cSMarshall Clow
79e948ba1cSMarshall Clowtemplate<class InputIterator, class OutputIterator, class T, class BinaryOperation>
80e948ba1cSMarshall Clow    OutputIterator
81e948ba1cSMarshall Clow    exclusive_scan(InputIterator first, InputIterator last,
82e948ba1cSMarshall Clow                   OutputIterator result, T init, BinaryOperation binary_op); // C++17
83e948ba1cSMarshall Clow
84ac8ea7c6SMarshall Clowtemplate<class InputIterator, class OutputIterator>
85ac8ea7c6SMarshall Clow    OutputIterator
86ac8ea7c6SMarshall Clow    inclusive_scan(InputIterator first, InputIterator last, OutputIterator result);  // C++17
87ac8ea7c6SMarshall Clow
88ac8ea7c6SMarshall Clowtemplate<class InputIterator, class OutputIterator, class BinaryOperation>
89ac8ea7c6SMarshall Clow    OutputIterator
90ac8ea7c6SMarshall Clow    inclusive_scan(InputIterator first, InputIterator last,
91ac8ea7c6SMarshall Clow                   OutputIterator result, BinaryOperation binary_op);  // C++17
92ac8ea7c6SMarshall Clow
93ac8ea7c6SMarshall Clowtemplate<class InputIterator, class OutputIterator, class BinaryOperation, class T>
94ac8ea7c6SMarshall Clow    OutputIterator
95ac8ea7c6SMarshall Clow    inclusive_scan(InputIterator first, InputIterator last,
96ac8ea7c6SMarshall Clow                   OutputIterator result, BinaryOperation binary_op, T init);  // C++17
97ac8ea7c6SMarshall Clow
98e948ba1cSMarshall Clowtemplate<class InputIterator, class OutputIterator, class T,
99e948ba1cSMarshall Clow         class BinaryOperation, class UnaryOperation>
100e948ba1cSMarshall Clow    OutputIterator
101e948ba1cSMarshall Clow    transform_exclusive_scan(InputIterator first, InputIterator last,
102e948ba1cSMarshall Clow                             OutputIterator result, T init,
103e948ba1cSMarshall Clow                             BinaryOperation binary_op, UnaryOperation unary_op);  // C++17
104e948ba1cSMarshall Clow
105ac8ea7c6SMarshall Clowtemplate<class InputIterator, class OutputIterator,
106ac8ea7c6SMarshall Clow         class BinaryOperation, class UnaryOperation>
107ac8ea7c6SMarshall Clow    OutputIterator
108ac8ea7c6SMarshall Clow    transform_inclusive_scan(InputIterator first, InputIterator last,
109ac8ea7c6SMarshall Clow                             OutputIterator result,
110ac8ea7c6SMarshall Clow                             BinaryOperation binary_op, UnaryOperation unary_op);  // C++17
111ac8ea7c6SMarshall Clow
112ac8ea7c6SMarshall Clowtemplate<class InputIterator, class OutputIterator,
113ac8ea7c6SMarshall Clow         class BinaryOperation, class UnaryOperation, class T>
114ac8ea7c6SMarshall Clow    OutputIterator
115ac8ea7c6SMarshall Clow    transform_inclusive_scan(InputIterator first, InputIterator last,
116ac8ea7c6SMarshall Clow                             OutputIterator result,
117ac8ea7c6SMarshall Clow                             BinaryOperation binary_op, UnaryOperation unary_op,
118ac8ea7c6SMarshall Clow                             T init);  // C++17
119ac8ea7c6SMarshall Clow
1203e519524SHoward Hinnanttemplate <class InputIterator, class OutputIterator>
1213e519524SHoward Hinnant    OutputIterator
1223e519524SHoward Hinnant    adjacent_difference(InputIterator first, InputIterator last, OutputIterator result);
1233e519524SHoward Hinnant
1243e519524SHoward Hinnanttemplate <class InputIterator, class OutputIterator, class BinaryOperation>
1253e519524SHoward Hinnant    OutputIterator
1263e519524SHoward Hinnant    adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
1273e519524SHoward Hinnant
12840c7ef90SHoward Hinnanttemplate <class ForwardIterator, class T>
12940c7ef90SHoward Hinnant    void iota(ForwardIterator first, ForwardIterator last, T value);
13040c7ef90SHoward Hinnant
13119b4035fSMarshall Clowtemplate <class M, class N>
13219b4035fSMarshall Clow    constexpr common_type_t<M,N> gcd(M m, N n);    // C++17
13319b4035fSMarshall Clow
13419b4035fSMarshall Clowtemplate <class M, class N>
13519b4035fSMarshall Clow    constexpr common_type_t<M,N> lcm(M m, N n);    // C++17
13619b4035fSMarshall Clow
1373e519524SHoward Hinnant}  // std
1383e519524SHoward Hinnant
1393e519524SHoward Hinnant*/
1403e519524SHoward Hinnant
1413e519524SHoward Hinnant#include <__config>
1423e519524SHoward Hinnant#include <iterator>
143d35528b3SEric Fiselier#include <limits> // for numeric_limits
144e948ba1cSMarshall Clow#include <functional>
145*f56972e2SMarshall Clow#include <version>
1463e519524SHoward Hinnant
147073458b1SHoward Hinnant#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1483e519524SHoward Hinnant#pragma GCC system_header
149073458b1SHoward Hinnant#endif
1503e519524SHoward Hinnant
151a016efb1SEric Fiselier_LIBCPP_PUSH_MACROS
152a016efb1SEric Fiselier#include <__undef_macros>
153a016efb1SEric Fiselier
1543e519524SHoward Hinnant_LIBCPP_BEGIN_NAMESPACE_STD
1553e519524SHoward Hinnant
1563e519524SHoward Hinnanttemplate <class _InputIterator, class _Tp>
1573e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
1583e519524SHoward Hinnant_Tp
1593e519524SHoward Hinnantaccumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
1603e519524SHoward Hinnant{
1613e519524SHoward Hinnant    for (; __first != __last; ++__first)
1623e519524SHoward Hinnant        __init = __init + *__first;
1633e519524SHoward Hinnant    return __init;
1643e519524SHoward Hinnant}
1653e519524SHoward Hinnant
1663e519524SHoward Hinnanttemplate <class _InputIterator, class _Tp, class _BinaryOperation>
1673e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
1683e519524SHoward Hinnant_Tp
1693e519524SHoward Hinnantaccumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
1703e519524SHoward Hinnant{
1713e519524SHoward Hinnant    for (; __first != __last; ++__first)
1723e519524SHoward Hinnant        __init = __binary_op(__init, *__first);
1733e519524SHoward Hinnant    return __init;
1743e519524SHoward Hinnant}
1753e519524SHoward Hinnant
176f4ea23d3SMarshall Clow#if _LIBCPP_STD_VER > 14
177f4ea23d3SMarshall Clowtemplate <class _InputIterator, class _Tp, class _BinaryOp>
178f4ea23d3SMarshall Clowinline _LIBCPP_INLINE_VISIBILITY
179f4ea23d3SMarshall Clow_Tp
180f4ea23d3SMarshall Clowreduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOp __b)
181f4ea23d3SMarshall Clow{
182f4ea23d3SMarshall Clow    for (; __first != __last; ++__first)
183f4ea23d3SMarshall Clow        __init = __b(__init, *__first);
184f4ea23d3SMarshall Clow    return __init;
185f4ea23d3SMarshall Clow}
186f4ea23d3SMarshall Clow
187f4ea23d3SMarshall Clowtemplate <class _InputIterator, class _Tp>
188f4ea23d3SMarshall Clowinline _LIBCPP_INLINE_VISIBILITY
189f4ea23d3SMarshall Clow_Tp
190f4ea23d3SMarshall Clowreduce(_InputIterator __first, _InputIterator __last, _Tp __init)
191f4ea23d3SMarshall Clow{
192f4ea23d3SMarshall Clow    return _VSTD::reduce(__first, __last, __init, _VSTD::plus<>());
193f4ea23d3SMarshall Clow}
194f4ea23d3SMarshall Clow
195f4ea23d3SMarshall Clowtemplate <class _InputIterator>
196f4ea23d3SMarshall Clowinline _LIBCPP_INLINE_VISIBILITY
197f4ea23d3SMarshall Clowtypename iterator_traits<_InputIterator>::value_type
198f4ea23d3SMarshall Clowreduce(_InputIterator __first, _InputIterator __last)
199f4ea23d3SMarshall Clow{
200f4ea23d3SMarshall Clow    return _VSTD::reduce(__first, __last,
201f4ea23d3SMarshall Clow       typename iterator_traits<_InputIterator>::value_type{});
202f4ea23d3SMarshall Clow}
203f4ea23d3SMarshall Clow#endif
204f4ea23d3SMarshall Clow
2053e519524SHoward Hinnanttemplate <class _InputIterator1, class _InputIterator2, class _Tp>
2063e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
2073e519524SHoward Hinnant_Tp
2083e519524SHoward Hinnantinner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init)
2093e519524SHoward Hinnant{
210910285b2SEric Fiselier    for (; __first1 != __last1; ++__first1, (void) ++__first2)
2113e519524SHoward Hinnant        __init = __init + *__first1 * *__first2;
2123e519524SHoward Hinnant    return __init;
2133e519524SHoward Hinnant}
2143e519524SHoward Hinnant
2153e519524SHoward Hinnanttemplate <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
2163e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
2173e519524SHoward Hinnant_Tp
2183e519524SHoward Hinnantinner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
2193e519524SHoward Hinnant              _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2)
2203e519524SHoward Hinnant{
221910285b2SEric Fiselier    for (; __first1 != __last1; ++__first1, (void) ++__first2)
2223e519524SHoward Hinnant        __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
2233e519524SHoward Hinnant    return __init;
2243e519524SHoward Hinnant}
2253e519524SHoward Hinnant
226f4ea23d3SMarshall Clow#if _LIBCPP_STD_VER > 14
227f4ea23d3SMarshall Clowtemplate <class _InputIterator, class _Tp, class _BinaryOp, class _UnaryOp>
228f4ea23d3SMarshall Clowinline _LIBCPP_INLINE_VISIBILITY
229f4ea23d3SMarshall Clow_Tp
230f4ea23d3SMarshall Clowtransform_reduce(_InputIterator __first, _InputIterator __last,
231f4ea23d3SMarshall Clow           _Tp __init,  _BinaryOp __b, _UnaryOp __u)
232f4ea23d3SMarshall Clow{
233f4ea23d3SMarshall Clow    for (; __first != __last; ++__first)
234f4ea23d3SMarshall Clow        __init = __b(__init, __u(*__first));
235f4ea23d3SMarshall Clow    return __init;
236f4ea23d3SMarshall Clow}
237f4ea23d3SMarshall Clow
238f4ea23d3SMarshall Clowtemplate <class _InputIterator1, class _InputIterator2,
239f4ea23d3SMarshall Clow          class _Tp, class _BinaryOp1, class _BinaryOp2>
240f4ea23d3SMarshall Clowinline _LIBCPP_INLINE_VISIBILITY
241f4ea23d3SMarshall Clow_Tp
242f4ea23d3SMarshall Clowtransform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
243f4ea23d3SMarshall Clow                 _InputIterator2 __first2, _Tp __init,  _BinaryOp1 __b1, _BinaryOp2 __b2)
244f4ea23d3SMarshall Clow{
245f4ea23d3SMarshall Clow    for (; __first1 != __last1; ++__first1, (void) ++__first2)
246f4ea23d3SMarshall Clow        __init = __b1(__init, __b2(*__first1, *__first2));
247f4ea23d3SMarshall Clow    return __init;
248f4ea23d3SMarshall Clow}
249f4ea23d3SMarshall Clow
250f4ea23d3SMarshall Clowtemplate <class _InputIterator1, class _InputIterator2, class _Tp>
251f4ea23d3SMarshall Clowinline _LIBCPP_INLINE_VISIBILITY
252f4ea23d3SMarshall Clow_Tp
253f4ea23d3SMarshall Clowtransform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
254f4ea23d3SMarshall Clow                 _InputIterator2 __first2, _Tp __init)
255f4ea23d3SMarshall Clow{
2564958692aSBilly Robert O'Neal III    return _VSTD::transform_reduce(__first1, __last1, __first2, _VSTD::move(__init),
257f4ea23d3SMarshall Clow                                   _VSTD::plus<>(), _VSTD::multiplies<>());
258f4ea23d3SMarshall Clow}
259f4ea23d3SMarshall Clow#endif
260f4ea23d3SMarshall Clow
2613e519524SHoward Hinnanttemplate <class _InputIterator, class _OutputIterator>
2623e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
2633e519524SHoward Hinnant_OutputIterator
2643e519524SHoward Hinnantpartial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2653e519524SHoward Hinnant{
2663e519524SHoward Hinnant    if (__first != __last)
2673e519524SHoward Hinnant    {
2683e519524SHoward Hinnant        typename iterator_traits<_InputIterator>::value_type __t(*__first);
2693e519524SHoward Hinnant        *__result = __t;
270910285b2SEric Fiselier        for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
2713e519524SHoward Hinnant        {
2723e519524SHoward Hinnant            __t = __t + *__first;
2733e519524SHoward Hinnant            *__result = __t;
2743e519524SHoward Hinnant        }
2753e519524SHoward Hinnant    }
2763e519524SHoward Hinnant    return __result;
2773e519524SHoward Hinnant}
2783e519524SHoward Hinnant
2793e519524SHoward Hinnanttemplate <class _InputIterator, class _OutputIterator, class _BinaryOperation>
2803e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
2813e519524SHoward Hinnant_OutputIterator
2823e519524SHoward Hinnantpartial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2833e519524SHoward Hinnant              _BinaryOperation __binary_op)
2843e519524SHoward Hinnant{
2853e519524SHoward Hinnant    if (__first != __last)
2863e519524SHoward Hinnant    {
2873e519524SHoward Hinnant        typename iterator_traits<_InputIterator>::value_type __t(*__first);
2883e519524SHoward Hinnant        *__result = __t;
289910285b2SEric Fiselier        for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
2903e519524SHoward Hinnant        {
2913e519524SHoward Hinnant            __t = __binary_op(__t, *__first);
2923e519524SHoward Hinnant            *__result = __t;
2933e519524SHoward Hinnant        }
2943e519524SHoward Hinnant    }
2953e519524SHoward Hinnant    return __result;
2963e519524SHoward Hinnant}
2973e519524SHoward Hinnant
298e948ba1cSMarshall Clow#if _LIBCPP_STD_VER > 14
299e948ba1cSMarshall Clowtemplate <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
300e948ba1cSMarshall Clowinline _LIBCPP_INLINE_VISIBILITY
301e948ba1cSMarshall Clow_OutputIterator
302e948ba1cSMarshall Clowexclusive_scan(_InputIterator __first, _InputIterator __last,
303e948ba1cSMarshall Clow               _OutputIterator __result, _Tp __init, _BinaryOp __b)
304e948ba1cSMarshall Clow{
305e948ba1cSMarshall Clow    if (__first != __last)
306e948ba1cSMarshall Clow    {
307e948ba1cSMarshall Clow        _Tp __saved = __init;
308e948ba1cSMarshall Clow        do
309e948ba1cSMarshall Clow        {
310e948ba1cSMarshall Clow            __init = __b(__init, *__first);
311e948ba1cSMarshall Clow            *__result = __saved;
312e948ba1cSMarshall Clow            __saved = __init;
313e948ba1cSMarshall Clow            ++__result;
314e948ba1cSMarshall Clow        } while (++__first != __last);
315e948ba1cSMarshall Clow    }
316e948ba1cSMarshall Clow    return __result;
317e948ba1cSMarshall Clow}
318e948ba1cSMarshall Clow
319e948ba1cSMarshall Clowtemplate <class _InputIterator, class _OutputIterator, class _Tp>
320e948ba1cSMarshall Clowinline _LIBCPP_INLINE_VISIBILITY
321e948ba1cSMarshall Clow_OutputIterator
322e948ba1cSMarshall Clowexclusive_scan(_InputIterator __first, _InputIterator __last,
323e948ba1cSMarshall Clow               _OutputIterator __result, _Tp __init)
324e948ba1cSMarshall Clow{
325e948ba1cSMarshall Clow    return _VSTD::exclusive_scan(__first, __last, __result, __init, _VSTD::plus<>());
326e948ba1cSMarshall Clow}
327e948ba1cSMarshall Clow
328ac8ea7c6SMarshall Clowtemplate <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
329ac8ea7c6SMarshall Clow_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
330ac8ea7c6SMarshall Clow                               _OutputIterator __result, _BinaryOp __b,  _Tp __init)
331ac8ea7c6SMarshall Clow{
332ac8ea7c6SMarshall Clow    for (; __first != __last; ++__first, (void) ++__result) {
333ac8ea7c6SMarshall Clow        __init = __b(__init, *__first);
334ac8ea7c6SMarshall Clow        *__result = __init;
335ac8ea7c6SMarshall Clow        }
336ac8ea7c6SMarshall Clow    return __result;
337ac8ea7c6SMarshall Clow}
338ac8ea7c6SMarshall Clow
339ac8ea7c6SMarshall Clowtemplate <class _InputIterator, class _OutputIterator, class _BinaryOp>
340ac8ea7c6SMarshall Clow_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
341ac8ea7c6SMarshall Clow                               _OutputIterator __result, _BinaryOp __b)
342ac8ea7c6SMarshall Clow{
343ac8ea7c6SMarshall Clow    if (__first != __last) {
344ac8ea7c6SMarshall Clow        typename std::iterator_traits<_InputIterator>::value_type __init = *__first;
345ac8ea7c6SMarshall Clow        *__result++ = __init;
346ac8ea7c6SMarshall Clow        if (++__first != __last)
347ac8ea7c6SMarshall Clow            return _VSTD::inclusive_scan(__first, __last, __result, __b, __init);
348ac8ea7c6SMarshall Clow        }
349ac8ea7c6SMarshall Clow
350ac8ea7c6SMarshall Clow    return __result;
351ac8ea7c6SMarshall Clow}
352ac8ea7c6SMarshall Clow
353ac8ea7c6SMarshall Clowtemplate <class _InputIterator, class _OutputIterator>
354ac8ea7c6SMarshall Clow_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
355ac8ea7c6SMarshall Clow                               _OutputIterator __result)
356ac8ea7c6SMarshall Clow{
357ac8ea7c6SMarshall Clow    return _VSTD::inclusive_scan(__first, __last, __result, std::plus<>());
358ac8ea7c6SMarshall Clow}
359ac8ea7c6SMarshall Clow
360e948ba1cSMarshall Clowtemplate <class _InputIterator, class _OutputIterator, class _Tp,
361e948ba1cSMarshall Clow          class _BinaryOp, class _UnaryOp>
362e948ba1cSMarshall Clowinline _LIBCPP_INLINE_VISIBILITY
363e948ba1cSMarshall Clow_OutputIterator
364e948ba1cSMarshall Clowtransform_exclusive_scan(_InputIterator __first, _InputIterator __last,
365e948ba1cSMarshall Clow                           _OutputIterator __result, _Tp __init,
366e948ba1cSMarshall Clow                           _BinaryOp __b, _UnaryOp __u)
367e948ba1cSMarshall Clow{
368e948ba1cSMarshall Clow    if (__first != __last)
369e948ba1cSMarshall Clow    {
370e948ba1cSMarshall Clow        _Tp __saved = __init;
371e948ba1cSMarshall Clow        do
372e948ba1cSMarshall Clow        {
373e948ba1cSMarshall Clow            __init = __b(__init, __u(*__first));
374e948ba1cSMarshall Clow            *__result = __saved;
375e948ba1cSMarshall Clow            __saved = __init;
376e948ba1cSMarshall Clow            ++__result;
377e948ba1cSMarshall Clow        } while (++__first != __last);
378e948ba1cSMarshall Clow    }
379e948ba1cSMarshall Clow    return __result;
380e948ba1cSMarshall Clow}
381ac8ea7c6SMarshall Clow
382ac8ea7c6SMarshall Clowtemplate <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp, class _UnaryOp>
383ac8ea7c6SMarshall Clow_OutputIterator transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
384ac8ea7c6SMarshall Clow                           _OutputIterator __result, _BinaryOp __b, _UnaryOp __u, _Tp __init)
385ac8ea7c6SMarshall Clow{
386ac8ea7c6SMarshall Clow    for (; __first != __last; ++__first, (void) ++__result) {
387ac8ea7c6SMarshall Clow        __init = __b(__init, __u(*__first));
388ac8ea7c6SMarshall Clow        *__result = __init;
389ac8ea7c6SMarshall Clow        }
390ac8ea7c6SMarshall Clow
391ac8ea7c6SMarshall Clow    return __result;
392ac8ea7c6SMarshall Clow}
393ac8ea7c6SMarshall Clow
394ac8ea7c6SMarshall Clowtemplate <class _InputIterator, class _OutputIterator, class _BinaryOp, class _UnaryOp>
395ac8ea7c6SMarshall Clow_OutputIterator transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
396ac8ea7c6SMarshall Clow                               _OutputIterator __result, _BinaryOp __b, _UnaryOp __u)
397ac8ea7c6SMarshall Clow{
398ac8ea7c6SMarshall Clow    if (__first != __last) {
399ac8ea7c6SMarshall Clow        typename std::iterator_traits<_InputIterator>::value_type __init = __u(*__first);
400ac8ea7c6SMarshall Clow        *__result++ = __init;
401ac8ea7c6SMarshall Clow        if (++__first != __last)
402ac8ea7c6SMarshall Clow            return _VSTD::transform_inclusive_scan(__first, __last, __result, __b, __u, __init);
403ac8ea7c6SMarshall Clow        }
404ac8ea7c6SMarshall Clow
405ac8ea7c6SMarshall Clow    return __result;
406ac8ea7c6SMarshall Clow}
407e948ba1cSMarshall Clow#endif
408e948ba1cSMarshall Clow
4093e519524SHoward Hinnanttemplate <class _InputIterator, class _OutputIterator>
4103e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
4113e519524SHoward Hinnant_OutputIterator
4123e519524SHoward Hinnantadjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
4133e519524SHoward Hinnant{
4143e519524SHoward Hinnant    if (__first != __last)
4153e519524SHoward Hinnant    {
4163e519524SHoward Hinnant        typename iterator_traits<_InputIterator>::value_type __t1(*__first);
4173e519524SHoward Hinnant        *__result = __t1;
418910285b2SEric Fiselier        for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
4193e519524SHoward Hinnant        {
4203e519524SHoward Hinnant            typename iterator_traits<_InputIterator>::value_type __t2(*__first);
4213e519524SHoward Hinnant            *__result = __t2 - __t1;
4229ff3203fSHoward Hinnant            __t1 = _VSTD::move(__t2);
4233e519524SHoward Hinnant        }
4243e519524SHoward Hinnant    }
4253e519524SHoward Hinnant    return __result;
4263e519524SHoward Hinnant}
4273e519524SHoward Hinnant
4283e519524SHoward Hinnanttemplate <class _InputIterator, class _OutputIterator, class _BinaryOperation>
4293e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
4303e519524SHoward Hinnant_OutputIterator
4313e519524SHoward Hinnantadjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
4323e519524SHoward Hinnant                      _BinaryOperation __binary_op)
4333e519524SHoward Hinnant{
4343e519524SHoward Hinnant    if (__first != __last)
4353e519524SHoward Hinnant    {
4363e519524SHoward Hinnant        typename iterator_traits<_InputIterator>::value_type __t1(*__first);
4373e519524SHoward Hinnant        *__result = __t1;
438910285b2SEric Fiselier        for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
4393e519524SHoward Hinnant        {
4403e519524SHoward Hinnant            typename iterator_traits<_InputIterator>::value_type __t2(*__first);
4413e519524SHoward Hinnant            *__result = __binary_op(__t2, __t1);
4429ff3203fSHoward Hinnant            __t1 = _VSTD::move(__t2);
4433e519524SHoward Hinnant        }
4443e519524SHoward Hinnant    }
4453e519524SHoward Hinnant    return __result;
4463e519524SHoward Hinnant}
4473e519524SHoward Hinnant
44840c7ef90SHoward Hinnanttemplate <class _ForwardIterator, class _Tp>
44940c7ef90SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
45040c7ef90SHoward Hinnantvoid
451e4383379SHoward Hinnantiota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_)
45240c7ef90SHoward Hinnant{
453910285b2SEric Fiselier    for (; __first != __last; ++__first, (void) ++__value_)
454e4383379SHoward Hinnant        *__first = __value_;
45540c7ef90SHoward Hinnant}
45640c7ef90SHoward Hinnant
45719b4035fSMarshall Clow
45819b4035fSMarshall Clow#if _LIBCPP_STD_VER > 14
45958fc1b50SMarshall Clowtemplate <typename _Result, typename _Source, bool _IsSigned = is_signed<_Source>::value> struct __abs;
46019b4035fSMarshall Clow
46158fc1b50SMarshall Clowtemplate <typename _Result, typename _Source>
46258fc1b50SMarshall Clowstruct __abs<_Result, _Source, true> {
46319b4035fSMarshall Clow    _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
46458fc1b50SMarshall Clow    _Result operator()(_Source __t) const noexcept
46558fc1b50SMarshall Clow    {
46658fc1b50SMarshall Clow    if (__t >= 0) return __t;
46758fc1b50SMarshall Clow    if (__t == numeric_limits<_Source>::min()) return -static_cast<_Result>(__t);
46858fc1b50SMarshall Clow    return -__t;
46958fc1b50SMarshall Clow    }
47019b4035fSMarshall Clow};
47119b4035fSMarshall Clow
47258fc1b50SMarshall Clowtemplate <typename _Result, typename _Source>
47358fc1b50SMarshall Clowstruct __abs<_Result, _Source, false> {
47419b4035fSMarshall Clow    _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
47558fc1b50SMarshall Clow    _Result operator()(_Source __t) const noexcept { return __t; }
47619b4035fSMarshall Clow};
47719b4035fSMarshall Clow
47819b4035fSMarshall Clow
47919b4035fSMarshall Clowtemplate<class _Tp>
48050d61da2SEric Fiselier_LIBCPP_CONSTEXPR _LIBCPP_HIDDEN
48119b4035fSMarshall Clow_Tp __gcd(_Tp __m, _Tp __n)
48219b4035fSMarshall Clow{
48319b4035fSMarshall Clow    static_assert((!is_signed<_Tp>::value), "");
48450d61da2SEric Fiselier    return __n == 0 ? __m : _VSTD::__gcd<_Tp>(__n, __m % __n);
48519b4035fSMarshall Clow}
48619b4035fSMarshall Clow
48719b4035fSMarshall Clow
48819b4035fSMarshall Clowtemplate<class _Tp, class _Up>
48919b4035fSMarshall Clow_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
49019b4035fSMarshall Clowcommon_type_t<_Tp,_Up>
49119b4035fSMarshall Clowgcd(_Tp __m, _Up __n)
49219b4035fSMarshall Clow{
49319b4035fSMarshall Clow    static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to gcd must be integer types");
4941c7fe126SMarshall Clow    static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to gcd cannot be bool" );
4951c7fe126SMarshall Clow    static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to gcd cannot be bool" );
49619b4035fSMarshall Clow    using _Rp = common_type_t<_Tp,_Up>;
49719b4035fSMarshall Clow    using _Wp = make_unsigned_t<_Rp>;
49850d61da2SEric Fiselier    return static_cast<_Rp>(_VSTD::__gcd(
49950d61da2SEric Fiselier        static_cast<_Wp>(__abs<_Rp, _Tp>()(__m)),
50058fc1b50SMarshall Clow        static_cast<_Wp>(__abs<_Rp, _Up>()(__n))));
50119b4035fSMarshall Clow}
50219b4035fSMarshall Clow
50319b4035fSMarshall Clowtemplate<class _Tp, class _Up>
50419b4035fSMarshall Clow_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
50519b4035fSMarshall Clowcommon_type_t<_Tp,_Up>
50619b4035fSMarshall Clowlcm(_Tp __m, _Up __n)
50719b4035fSMarshall Clow{
50819b4035fSMarshall Clow    static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to lcm must be integer types");
5091c7fe126SMarshall Clow    static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to lcm cannot be bool" );
5101c7fe126SMarshall Clow    static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to lcm cannot be bool" );
51119b4035fSMarshall Clow    if (__m == 0 || __n == 0)
51219b4035fSMarshall Clow        return 0;
51319b4035fSMarshall Clow
51419b4035fSMarshall Clow    using _Rp = common_type_t<_Tp,_Up>;
51550d61da2SEric Fiselier    _Rp __val1 = __abs<_Rp, _Tp>()(__m) / _VSTD::gcd(__m, __n);
51658fc1b50SMarshall Clow    _Rp __val2 = __abs<_Rp, _Up>()(__n);
51719b4035fSMarshall Clow    _LIBCPP_ASSERT((numeric_limits<_Rp>::max() / __val1 > __val2), "Overflow in lcm");
51819b4035fSMarshall Clow    return __val1 * __val2;
51919b4035fSMarshall Clow}
52019b4035fSMarshall Clow
52119b4035fSMarshall Clow#endif /* _LIBCPP_STD_VER > 14 */
52219b4035fSMarshall Clow
5233e519524SHoward Hinnant_LIBCPP_END_NAMESPACE_STD
5243e519524SHoward Hinnant
525a016efb1SEric Fiselier_LIBCPP_POP_MACROS
526a016efb1SEric Fiselier
5273e519524SHoward Hinnant#endif  // _LIBCPP_NUMERIC
528