Home | History | Annotate | Line # | Download | only in include
numeric revision 1.1
      1  1.1  joerg // -*- C++ -*-
      2  1.1  joerg //===---------------------------- numeric ---------------------------------===//
      3  1.1  joerg //
      4  1.1  joerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
      5  1.1  joerg // See https://llvm.org/LICENSE.txt for license information.
      6  1.1  joerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
      7  1.1  joerg //
      8  1.1  joerg //===----------------------------------------------------------------------===//
      9  1.1  joerg 
     10  1.1  joerg #ifndef _LIBCPP_NUMERIC
     11  1.1  joerg #define _LIBCPP_NUMERIC
     12  1.1  joerg 
     13  1.1  joerg /*
     14  1.1  joerg     numeric synopsis
     15  1.1  joerg 
     16  1.1  joerg namespace std
     17  1.1  joerg {
     18  1.1  joerg 
     19  1.1  joerg template <class InputIterator, class T>
     20  1.1  joerg     constexpr T  // constexpr since C++20
     21  1.1  joerg     accumulate(InputIterator first, InputIterator last, T init);
     22  1.1  joerg 
     23  1.1  joerg template <class InputIterator, class T, class BinaryOperation>
     24  1.1  joerg     constexpr T  // constexpr since C++20
     25  1.1  joerg     accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);
     26  1.1  joerg 
     27  1.1  joerg template<class InputIterator>
     28  1.1  joerg     constexpr typename iterator_traits<InputIterator>::value_type  // constexpr since C++20
     29  1.1  joerg     reduce(InputIterator first, InputIterator last);  // C++17
     30  1.1  joerg 
     31  1.1  joerg template<class InputIterator, class T>
     32  1.1  joerg     constexpr T  // constexpr since C++20
     33  1.1  joerg     reduce(InputIterator first, InputIterator last, T init);  // C++17
     34  1.1  joerg 
     35  1.1  joerg template<class InputIterator, class T, class BinaryOperation>
     36  1.1  joerg     constexpr T  // constexpr since C++20
     37  1.1  joerg     reduce(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);  // C++17
     38  1.1  joerg 
     39  1.1  joerg template <class InputIterator1, class InputIterator2, class T>
     40  1.1  joerg     constexpr T  // constexpr since C++20
     41  1.1  joerg     inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init);
     42  1.1  joerg 
     43  1.1  joerg template <class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
     44  1.1  joerg     constexpr T  // constexpr since C++20
     45  1.1  joerg     inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
     46  1.1  joerg                   T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);
     47  1.1  joerg 
     48  1.1  joerg 
     49  1.1  joerg template<class InputIterator1, class InputIterator2, class T>
     50  1.1  joerg     constexpr T  // constexpr since C++20
     51  1.1  joerg     transform_reduce(InputIterator1 first1, InputIterator1 last1,
     52  1.1  joerg                      InputIterator2 first2, T init);  // C++17
     53  1.1  joerg 
     54  1.1  joerg template<class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
     55  1.1  joerg     constexpr T  // constexpr since C++20
     56  1.1  joerg     transform_reduce(InputIterator1 first1, InputIterator1 last1,
     57  1.1  joerg                      InputIterator2 first2, T init,
     58  1.1  joerg                      BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);  // C++17
     59  1.1  joerg 
     60  1.1  joerg template<class InputIterator, class T, class BinaryOperation, class UnaryOperation>
     61  1.1  joerg     constexpr T  // constexpr since C++20
     62  1.1  joerg     transform_reduce(InputIterator first, InputIterator last, T init,
     63  1.1  joerg                      BinaryOperation binary_op, UnaryOperation unary_op);  // C++17
     64  1.1  joerg 
     65  1.1  joerg template <class InputIterator, class OutputIterator>
     66  1.1  joerg     constexpr OutputIterator  // constexpr since C++20
     67  1.1  joerg     partial_sum(InputIterator first, InputIterator last, OutputIterator result);
     68  1.1  joerg 
     69  1.1  joerg template <class InputIterator, class OutputIterator, class BinaryOperation>
     70  1.1  joerg     constexpr OutputIterator  // constexpr since C++20
     71  1.1  joerg     partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
     72  1.1  joerg 
     73  1.1  joerg template<class InputIterator, class OutputIterator, class T>
     74  1.1  joerg     constexpr OutputIterator  // constexpr since C++20
     75  1.1  joerg     exclusive_scan(InputIterator first, InputIterator last,
     76  1.1  joerg                    OutputIterator result, T init); // C++17
     77  1.1  joerg 
     78  1.1  joerg template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
     79  1.1  joerg     constexpr OutputIterator  // constexpr since C++20
     80  1.1  joerg     exclusive_scan(InputIterator first, InputIterator last,
     81  1.1  joerg                    OutputIterator result, T init, BinaryOperation binary_op); // C++17
     82  1.1  joerg 
     83  1.1  joerg template<class InputIterator, class OutputIterator>
     84  1.1  joerg     constexpr OutputIterator  // constexpr since C++20
     85  1.1  joerg     inclusive_scan(InputIterator first, InputIterator last, OutputIterator result);  // C++17
     86  1.1  joerg 
     87  1.1  joerg template<class InputIterator, class OutputIterator, class BinaryOperation>
     88  1.1  joerg     constexpr OutputIterator  // constexpr since C++20
     89  1.1  joerg     inclusive_scan(InputIterator first, InputIterator last,
     90  1.1  joerg                    OutputIterator result, BinaryOperation binary_op);  // C++17
     91  1.1  joerg 
     92  1.1  joerg template<class InputIterator, class OutputIterator, class BinaryOperation, class T>
     93  1.1  joerg     constexpr OutputIterator  // constexpr since C++20
     94  1.1  joerg     inclusive_scan(InputIterator first, InputIterator last,
     95  1.1  joerg                    OutputIterator result, BinaryOperation binary_op, T init);  // C++17
     96  1.1  joerg 
     97  1.1  joerg template<class InputIterator, class OutputIterator, class T,
     98  1.1  joerg          class BinaryOperation, class UnaryOperation>
     99  1.1  joerg     constexpr OutputIterator  // constexpr since C++20
    100  1.1  joerg     transform_exclusive_scan(InputIterator first, InputIterator last,
    101  1.1  joerg                              OutputIterator result, T init,
    102  1.1  joerg                              BinaryOperation binary_op, UnaryOperation unary_op);  // C++17
    103  1.1  joerg 
    104  1.1  joerg template<class InputIterator, class OutputIterator,
    105  1.1  joerg          class BinaryOperation, class UnaryOperation>
    106  1.1  joerg     constexpr OutputIterator  // constexpr since C++20
    107  1.1  joerg     transform_inclusive_scan(InputIterator first, InputIterator last,
    108  1.1  joerg                              OutputIterator result,
    109  1.1  joerg                              BinaryOperation binary_op, UnaryOperation unary_op);  // C++17
    110  1.1  joerg 
    111  1.1  joerg template<class InputIterator, class OutputIterator,
    112  1.1  joerg          class BinaryOperation, class UnaryOperation, class T>
    113  1.1  joerg     constexpr OutputIterator  // constexpr since C++20
    114  1.1  joerg     transform_inclusive_scan(InputIterator first, InputIterator last,
    115  1.1  joerg                              OutputIterator result,
    116  1.1  joerg                              BinaryOperation binary_op, UnaryOperation unary_op,
    117  1.1  joerg                              T init);  // C++17
    118  1.1  joerg 
    119  1.1  joerg template <class InputIterator, class OutputIterator>
    120  1.1  joerg     constexpr OutputIterator  // constexpr since C++20
    121  1.1  joerg     adjacent_difference(InputIterator first, InputIterator last, OutputIterator result);
    122  1.1  joerg 
    123  1.1  joerg template <class InputIterator, class OutputIterator, class BinaryOperation>
    124  1.1  joerg     constexpr OutputIterator  // constexpr since C++20
    125  1.1  joerg     adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
    126  1.1  joerg 
    127  1.1  joerg template <class ForwardIterator, class T>
    128  1.1  joerg     constexpr void  // constexpr since C++20
    129  1.1  joerg     iota(ForwardIterator first, ForwardIterator last, T value);
    130  1.1  joerg 
    131  1.1  joerg template <class M, class N>
    132  1.1  joerg     constexpr common_type_t<M,N> gcd(M m, N n);    // C++17
    133  1.1  joerg 
    134  1.1  joerg template <class M, class N>
    135  1.1  joerg     constexpr common_type_t<M,N> lcm(M m, N n);    // C++17
    136  1.1  joerg 
    137  1.1  joerg template<class T>
    138  1.1  joerg     constexpr T midpoint(T a, T b) noexcept;  // C++20
    139  1.1  joerg 
    140  1.1  joerg template<class T>
    141  1.1  joerg     constexpr T* midpoint(T* a, T* b);        // C++20
    142  1.1  joerg 
    143  1.1  joerg }  // std
    144  1.1  joerg 
    145  1.1  joerg */
    146  1.1  joerg 
    147  1.1  joerg #include <__config>
    148  1.1  joerg #include <__debug>
    149  1.1  joerg #include <cmath> // for isnormal
    150  1.1  joerg #include <functional>
    151  1.1  joerg #include <iterator>
    152  1.1  joerg #include <limits> // for numeric_limits
    153  1.1  joerg #include <version>
    154  1.1  joerg 
    155  1.1  joerg #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
    156  1.1  joerg #pragma GCC system_header
    157  1.1  joerg #endif
    158  1.1  joerg 
    159  1.1  joerg _LIBCPP_PUSH_MACROS
    160  1.1  joerg #include <__undef_macros>
    161  1.1  joerg 
    162  1.1  joerg _LIBCPP_BEGIN_NAMESPACE_STD
    163  1.1  joerg 
    164  1.1  joerg template <class _InputIterator, class _Tp>
    165  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    166  1.1  joerg _Tp
    167  1.1  joerg accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
    168  1.1  joerg {
    169  1.1  joerg     for (; __first != __last; ++__first)
    170  1.1  joerg #if _LIBCPP_STD_VER > 17
    171  1.1  joerg         __init = _VSTD::move(__init) + *__first;
    172  1.1  joerg #else
    173  1.1  joerg         __init = __init + *__first;
    174  1.1  joerg #endif
    175  1.1  joerg     return __init;
    176  1.1  joerg }
    177  1.1  joerg 
    178  1.1  joerg template <class _InputIterator, class _Tp, class _BinaryOperation>
    179  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    180  1.1  joerg _Tp
    181  1.1  joerg accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
    182  1.1  joerg {
    183  1.1  joerg     for (; __first != __last; ++__first)
    184  1.1  joerg #if _LIBCPP_STD_VER > 17
    185  1.1  joerg         __init = __binary_op(_VSTD::move(__init), *__first);
    186  1.1  joerg #else
    187  1.1  joerg         __init = __binary_op(__init, *__first);
    188  1.1  joerg #endif
    189  1.1  joerg     return __init;
    190  1.1  joerg }
    191  1.1  joerg 
    192  1.1  joerg #if _LIBCPP_STD_VER > 14
    193  1.1  joerg template <class _InputIterator, class _Tp, class _BinaryOp>
    194  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    195  1.1  joerg _Tp
    196  1.1  joerg reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOp __b)
    197  1.1  joerg {
    198  1.1  joerg     for (; __first != __last; ++__first)
    199  1.1  joerg         __init = __b(__init, *__first);
    200  1.1  joerg     return __init;
    201  1.1  joerg }
    202  1.1  joerg 
    203  1.1  joerg template <class _InputIterator, class _Tp>
    204  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    205  1.1  joerg _Tp
    206  1.1  joerg reduce(_InputIterator __first, _InputIterator __last, _Tp __init)
    207  1.1  joerg {
    208  1.1  joerg     return _VSTD::reduce(__first, __last, __init, _VSTD::plus<>());
    209  1.1  joerg }
    210  1.1  joerg 
    211  1.1  joerg template <class _InputIterator>
    212  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    213  1.1  joerg typename iterator_traits<_InputIterator>::value_type
    214  1.1  joerg reduce(_InputIterator __first, _InputIterator __last)
    215  1.1  joerg {
    216  1.1  joerg     return _VSTD::reduce(__first, __last,
    217  1.1  joerg        typename iterator_traits<_InputIterator>::value_type{});
    218  1.1  joerg }
    219  1.1  joerg #endif
    220  1.1  joerg 
    221  1.1  joerg template <class _InputIterator1, class _InputIterator2, class _Tp>
    222  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    223  1.1  joerg _Tp
    224  1.1  joerg inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init)
    225  1.1  joerg {
    226  1.1  joerg     for (; __first1 != __last1; ++__first1, (void) ++__first2)
    227  1.1  joerg #if _LIBCPP_STD_VER > 17
    228  1.1  joerg         __init = _VSTD::move(__init) + *__first1 * *__first2;
    229  1.1  joerg #else
    230  1.1  joerg         __init = __init + *__first1 * *__first2;
    231  1.1  joerg #endif
    232  1.1  joerg     return __init;
    233  1.1  joerg }
    234  1.1  joerg 
    235  1.1  joerg template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
    236  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    237  1.1  joerg _Tp
    238  1.1  joerg inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
    239  1.1  joerg               _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2)
    240  1.1  joerg {
    241  1.1  joerg     for (; __first1 != __last1; ++__first1, (void) ++__first2)
    242  1.1  joerg #if _LIBCPP_STD_VER > 17
    243  1.1  joerg         __init = __binary_op1(_VSTD::move(__init), __binary_op2(*__first1, *__first2));
    244  1.1  joerg #else
    245  1.1  joerg         __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
    246  1.1  joerg #endif
    247  1.1  joerg     return __init;
    248  1.1  joerg }
    249  1.1  joerg 
    250  1.1  joerg #if _LIBCPP_STD_VER > 14
    251  1.1  joerg template <class _InputIterator, class _Tp, class _BinaryOp, class _UnaryOp>
    252  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    253  1.1  joerg _Tp
    254  1.1  joerg transform_reduce(_InputIterator __first, _InputIterator __last,
    255  1.1  joerg            _Tp __init,  _BinaryOp __b, _UnaryOp __u)
    256  1.1  joerg {
    257  1.1  joerg     for (; __first != __last; ++__first)
    258  1.1  joerg         __init = __b(__init, __u(*__first));
    259  1.1  joerg     return __init;
    260  1.1  joerg }
    261  1.1  joerg 
    262  1.1  joerg template <class _InputIterator1, class _InputIterator2,
    263  1.1  joerg           class _Tp, class _BinaryOp1, class _BinaryOp2>
    264  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    265  1.1  joerg _Tp
    266  1.1  joerg transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
    267  1.1  joerg                  _InputIterator2 __first2, _Tp __init,  _BinaryOp1 __b1, _BinaryOp2 __b2)
    268  1.1  joerg {
    269  1.1  joerg     for (; __first1 != __last1; ++__first1, (void) ++__first2)
    270  1.1  joerg         __init = __b1(__init, __b2(*__first1, *__first2));
    271  1.1  joerg     return __init;
    272  1.1  joerg }
    273  1.1  joerg 
    274  1.1  joerg template <class _InputIterator1, class _InputIterator2, class _Tp>
    275  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    276  1.1  joerg _Tp
    277  1.1  joerg transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
    278  1.1  joerg                  _InputIterator2 __first2, _Tp __init)
    279  1.1  joerg {
    280  1.1  joerg     return _VSTD::transform_reduce(__first1, __last1, __first2, _VSTD::move(__init),
    281  1.1  joerg                                    _VSTD::plus<>(), _VSTD::multiplies<>());
    282  1.1  joerg }
    283  1.1  joerg #endif
    284  1.1  joerg 
    285  1.1  joerg template <class _InputIterator, class _OutputIterator>
    286  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    287  1.1  joerg _OutputIterator
    288  1.1  joerg partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
    289  1.1  joerg {
    290  1.1  joerg     if (__first != __last)
    291  1.1  joerg     {
    292  1.1  joerg         typename iterator_traits<_InputIterator>::value_type __t(*__first);
    293  1.1  joerg         *__result = __t;
    294  1.1  joerg         for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
    295  1.1  joerg         {
    296  1.1  joerg #if _LIBCPP_STD_VER > 17
    297  1.1  joerg             __t = _VSTD::move(__t) + *__first;
    298  1.1  joerg #else
    299  1.1  joerg             __t = __t + *__first;
    300  1.1  joerg #endif
    301  1.1  joerg             *__result = __t;
    302  1.1  joerg         }
    303  1.1  joerg     }
    304  1.1  joerg     return __result;
    305  1.1  joerg }
    306  1.1  joerg 
    307  1.1  joerg template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
    308  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    309  1.1  joerg _OutputIterator
    310  1.1  joerg partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
    311  1.1  joerg               _BinaryOperation __binary_op)
    312  1.1  joerg {
    313  1.1  joerg     if (__first != __last)
    314  1.1  joerg     {
    315  1.1  joerg         typename iterator_traits<_InputIterator>::value_type __t(*__first);
    316  1.1  joerg         *__result = __t;
    317  1.1  joerg         for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
    318  1.1  joerg         {
    319  1.1  joerg #if _LIBCPP_STD_VER > 17
    320  1.1  joerg             __t = __binary_op(_VSTD::move(__t), *__first);
    321  1.1  joerg #else
    322  1.1  joerg             __t = __binary_op(__t, *__first);
    323  1.1  joerg #endif
    324  1.1  joerg             *__result = __t;
    325  1.1  joerg         }
    326  1.1  joerg     }
    327  1.1  joerg     return __result;
    328  1.1  joerg }
    329  1.1  joerg 
    330  1.1  joerg #if _LIBCPP_STD_VER > 14
    331  1.1  joerg template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
    332  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    333  1.1  joerg _OutputIterator
    334  1.1  joerg exclusive_scan(_InputIterator __first, _InputIterator __last,
    335  1.1  joerg                _OutputIterator __result, _Tp __init, _BinaryOp __b)
    336  1.1  joerg {
    337  1.1  joerg     if (__first != __last)
    338  1.1  joerg     {
    339  1.1  joerg         _Tp __tmp(__b(__init, *__first));
    340  1.1  joerg         while (true)
    341  1.1  joerg         {
    342  1.1  joerg             *__result = _VSTD::move(__init);
    343  1.1  joerg             ++__result;
    344  1.1  joerg             ++__first;
    345  1.1  joerg             if (__first == __last)
    346  1.1  joerg                 break;
    347  1.1  joerg             __init = _VSTD::move(__tmp);
    348  1.1  joerg             __tmp = __b(__init, *__first);
    349  1.1  joerg         }
    350  1.1  joerg     }
    351  1.1  joerg     return __result;
    352  1.1  joerg }
    353  1.1  joerg 
    354  1.1  joerg template <class _InputIterator, class _OutputIterator, class _Tp>
    355  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    356  1.1  joerg _OutputIterator
    357  1.1  joerg exclusive_scan(_InputIterator __first, _InputIterator __last,
    358  1.1  joerg                _OutputIterator __result, _Tp __init)
    359  1.1  joerg {
    360  1.1  joerg     return _VSTD::exclusive_scan(__first, __last, __result, __init, _VSTD::plus<>());
    361  1.1  joerg }
    362  1.1  joerg 
    363  1.1  joerg template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
    364  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    365  1.1  joerg _OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
    366  1.1  joerg                                _OutputIterator __result, _BinaryOp __b,  _Tp __init)
    367  1.1  joerg {
    368  1.1  joerg     for (; __first != __last; ++__first, (void) ++__result) {
    369  1.1  joerg         __init = __b(__init, *__first);
    370  1.1  joerg         *__result = __init;
    371  1.1  joerg     }
    372  1.1  joerg     return __result;
    373  1.1  joerg }
    374  1.1  joerg 
    375  1.1  joerg template <class _InputIterator, class _OutputIterator, class _BinaryOp>
    376  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    377  1.1  joerg _OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
    378  1.1  joerg                                _OutputIterator __result, _BinaryOp __b)
    379  1.1  joerg {
    380  1.1  joerg     if (__first != __last) {
    381  1.1  joerg         typename iterator_traits<_InputIterator>::value_type __init = *__first;
    382  1.1  joerg         *__result++ = __init;
    383  1.1  joerg         if (++__first != __last)
    384  1.1  joerg             return _VSTD::inclusive_scan(__first, __last, __result, __b, __init);
    385  1.1  joerg     }
    386  1.1  joerg 
    387  1.1  joerg     return __result;
    388  1.1  joerg }
    389  1.1  joerg 
    390  1.1  joerg template <class _InputIterator, class _OutputIterator>
    391  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    392  1.1  joerg _OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
    393  1.1  joerg                                _OutputIterator __result)
    394  1.1  joerg {
    395  1.1  joerg     return _VSTD::inclusive_scan(__first, __last, __result, _VSTD::plus<>());
    396  1.1  joerg }
    397  1.1  joerg 
    398  1.1  joerg template <class _InputIterator, class _OutputIterator, class _Tp,
    399  1.1  joerg           class _BinaryOp, class _UnaryOp>
    400  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    401  1.1  joerg _OutputIterator
    402  1.1  joerg transform_exclusive_scan(_InputIterator __first, _InputIterator __last,
    403  1.1  joerg                            _OutputIterator __result, _Tp __init,
    404  1.1  joerg                            _BinaryOp __b, _UnaryOp __u)
    405  1.1  joerg {
    406  1.1  joerg     if (__first != __last)
    407  1.1  joerg     {
    408  1.1  joerg         _Tp __saved = __init;
    409  1.1  joerg         do
    410  1.1  joerg         {
    411  1.1  joerg             __init = __b(__init, __u(*__first));
    412  1.1  joerg             *__result = __saved;
    413  1.1  joerg             __saved = __init;
    414  1.1  joerg             ++__result;
    415  1.1  joerg         } while (++__first != __last);
    416  1.1  joerg     }
    417  1.1  joerg     return __result;
    418  1.1  joerg }
    419  1.1  joerg 
    420  1.1  joerg template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp, class _UnaryOp>
    421  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    422  1.1  joerg _OutputIterator
    423  1.1  joerg transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
    424  1.1  joerg                            _OutputIterator __result, _BinaryOp __b, _UnaryOp __u, _Tp __init)
    425  1.1  joerg {
    426  1.1  joerg     for (; __first != __last; ++__first, (void) ++__result) {
    427  1.1  joerg         __init = __b(__init, __u(*__first));
    428  1.1  joerg         *__result = __init;
    429  1.1  joerg         }
    430  1.1  joerg 
    431  1.1  joerg     return __result;
    432  1.1  joerg }
    433  1.1  joerg 
    434  1.1  joerg template <class _InputIterator, class _OutputIterator, class _BinaryOp, class _UnaryOp>
    435  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    436  1.1  joerg _OutputIterator
    437  1.1  joerg transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
    438  1.1  joerg                                _OutputIterator __result, _BinaryOp __b, _UnaryOp __u)
    439  1.1  joerg {
    440  1.1  joerg     if (__first != __last) {
    441  1.1  joerg         typename iterator_traits<_InputIterator>::value_type __init = __u(*__first);
    442  1.1  joerg         *__result++ = __init;
    443  1.1  joerg         if (++__first != __last)
    444  1.1  joerg             return _VSTD::transform_inclusive_scan(__first, __last, __result, __b, __u, __init);
    445  1.1  joerg     }
    446  1.1  joerg 
    447  1.1  joerg     return __result;
    448  1.1  joerg }
    449  1.1  joerg #endif
    450  1.1  joerg 
    451  1.1  joerg template <class _InputIterator, class _OutputIterator>
    452  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    453  1.1  joerg _OutputIterator
    454  1.1  joerg adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
    455  1.1  joerg {
    456  1.1  joerg     if (__first != __last)
    457  1.1  joerg     {
    458  1.1  joerg         typename iterator_traits<_InputIterator>::value_type __acc(*__first);
    459  1.1  joerg         *__result = __acc;
    460  1.1  joerg         for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
    461  1.1  joerg         {
    462  1.1  joerg             typename iterator_traits<_InputIterator>::value_type __val(*__first);
    463  1.1  joerg #if _LIBCPP_STD_VER > 17
    464  1.1  joerg             *__result = __val - _VSTD::move(__acc);
    465  1.1  joerg #else
    466  1.1  joerg             *__result = __val - __acc;
    467  1.1  joerg #endif
    468  1.1  joerg             __acc = _VSTD::move(__val);
    469  1.1  joerg         }
    470  1.1  joerg     }
    471  1.1  joerg     return __result;
    472  1.1  joerg }
    473  1.1  joerg 
    474  1.1  joerg template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
    475  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    476  1.1  joerg _OutputIterator
    477  1.1  joerg adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
    478  1.1  joerg                       _BinaryOperation __binary_op)
    479  1.1  joerg {
    480  1.1  joerg     if (__first != __last)
    481  1.1  joerg     {
    482  1.1  joerg         typename iterator_traits<_InputIterator>::value_type __acc(*__first);
    483  1.1  joerg         *__result = __acc;
    484  1.1  joerg         for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
    485  1.1  joerg         {
    486  1.1  joerg             typename iterator_traits<_InputIterator>::value_type __val(*__first);
    487  1.1  joerg #if _LIBCPP_STD_VER > 17
    488  1.1  joerg             *__result = __binary_op(__val, _VSTD::move(__acc));
    489  1.1  joerg #else
    490  1.1  joerg             *__result = __binary_op(__val, __acc);
    491  1.1  joerg #endif
    492  1.1  joerg             __acc = _VSTD::move(__val);
    493  1.1  joerg         }
    494  1.1  joerg     }
    495  1.1  joerg     return __result;
    496  1.1  joerg }
    497  1.1  joerg 
    498  1.1  joerg template <class _ForwardIterator, class _Tp>
    499  1.1  joerg _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    500  1.1  joerg void
    501  1.1  joerg iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_)
    502  1.1  joerg {
    503  1.1  joerg     for (; __first != __last; ++__first, (void) ++__value_)
    504  1.1  joerg         *__first = __value_;
    505  1.1  joerg }
    506  1.1  joerg 
    507  1.1  joerg 
    508  1.1  joerg #if _LIBCPP_STD_VER > 14
    509  1.1  joerg template <typename _Result, typename _Source, bool _IsSigned = is_signed<_Source>::value> struct __ct_abs;
    510  1.1  joerg 
    511  1.1  joerg template <typename _Result, typename _Source>
    512  1.1  joerg struct __ct_abs<_Result, _Source, true> {
    513  1.1  joerg     _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
    514  1.1  joerg     _Result operator()(_Source __t) const noexcept
    515  1.1  joerg     {
    516  1.1  joerg         if (__t >= 0) return __t;
    517  1.1  joerg         if (__t == numeric_limits<_Source>::min()) return -static_cast<_Result>(__t);
    518  1.1  joerg         return -__t;
    519  1.1  joerg     }
    520  1.1  joerg };
    521  1.1  joerg 
    522  1.1  joerg template <typename _Result, typename _Source>
    523  1.1  joerg struct __ct_abs<_Result, _Source, false> {
    524  1.1  joerg     _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
    525  1.1  joerg     _Result operator()(_Source __t) const noexcept { return __t; }
    526  1.1  joerg };
    527  1.1  joerg 
    528  1.1  joerg 
    529  1.1  joerg template<class _Tp>
    530  1.1  joerg _LIBCPP_CONSTEXPR _LIBCPP_HIDDEN
    531  1.1  joerg _Tp __gcd(_Tp __m, _Tp __n)
    532  1.1  joerg {
    533  1.1  joerg     static_assert((!is_signed<_Tp>::value), "");
    534  1.1  joerg     return __n == 0 ? __m : _VSTD::__gcd<_Tp>(__n, __m % __n);
    535  1.1  joerg }
    536  1.1  joerg 
    537  1.1  joerg 
    538  1.1  joerg template<class _Tp, class _Up>
    539  1.1  joerg _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
    540  1.1  joerg common_type_t<_Tp,_Up>
    541  1.1  joerg gcd(_Tp __m, _Up __n)
    542  1.1  joerg {
    543  1.1  joerg     static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to gcd must be integer types");
    544  1.1  joerg     static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to gcd cannot be bool" );
    545  1.1  joerg     static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to gcd cannot be bool" );
    546  1.1  joerg     using _Rp = common_type_t<_Tp,_Up>;
    547  1.1  joerg     using _Wp = make_unsigned_t<_Rp>;
    548  1.1  joerg     return static_cast<_Rp>(_VSTD::__gcd(
    549  1.1  joerg         static_cast<_Wp>(__ct_abs<_Rp, _Tp>()(__m)),
    550  1.1  joerg         static_cast<_Wp>(__ct_abs<_Rp, _Up>()(__n))));
    551  1.1  joerg }
    552  1.1  joerg 
    553  1.1  joerg template<class _Tp, class _Up>
    554  1.1  joerg _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
    555  1.1  joerg common_type_t<_Tp,_Up>
    556  1.1  joerg lcm(_Tp __m, _Up __n)
    557  1.1  joerg {
    558  1.1  joerg     static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to lcm must be integer types");
    559  1.1  joerg     static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to lcm cannot be bool" );
    560  1.1  joerg     static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to lcm cannot be bool" );
    561  1.1  joerg     if (__m == 0 || __n == 0)
    562  1.1  joerg         return 0;
    563  1.1  joerg 
    564  1.1  joerg     using _Rp = common_type_t<_Tp,_Up>;
    565  1.1  joerg     _Rp __val1 = __ct_abs<_Rp, _Tp>()(__m) / _VSTD::gcd(__m, __n);
    566  1.1  joerg     _Rp __val2 = __ct_abs<_Rp, _Up>()(__n);
    567  1.1  joerg     _LIBCPP_ASSERT((numeric_limits<_Rp>::max() / __val1 > __val2), "Overflow in lcm");
    568  1.1  joerg     return __val1 * __val2;
    569  1.1  joerg }
    570  1.1  joerg 
    571  1.1  joerg #endif /* _LIBCPP_STD_VER > 14 */
    572  1.1  joerg 
    573  1.1  joerg #if _LIBCPP_STD_VER > 17
    574  1.1  joerg template <class _Tp>
    575  1.1  joerg _LIBCPP_INLINE_VISIBILITY constexpr
    576  1.1  joerg enable_if_t<is_integral_v<_Tp> && !is_same_v<bool, _Tp> && !is_null_pointer_v<_Tp>, _Tp>
    577  1.1  joerg midpoint(_Tp __a, _Tp __b) noexcept
    578  1.1  joerg _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
    579  1.1  joerg {
    580  1.1  joerg     using _Up = make_unsigned_t<_Tp>;
    581  1.1  joerg     constexpr _Up __bitshift = numeric_limits<_Up>::digits - 1;
    582  1.1  joerg 
    583  1.1  joerg     _Up __diff = _Up(__b) - _Up(__a);
    584  1.1  joerg     _Up __sign_bit = __b < __a;
    585  1.1  joerg 
    586  1.1  joerg     _Up __half_diff = (__diff / 2) + (__sign_bit << __bitshift) + (__sign_bit & __diff);
    587  1.1  joerg 
    588  1.1  joerg     return __a + __half_diff;
    589  1.1  joerg }
    590  1.1  joerg 
    591  1.1  joerg 
    592  1.1  joerg template <class _TPtr>
    593  1.1  joerg _LIBCPP_INLINE_VISIBILITY constexpr
    594  1.1  joerg enable_if_t<is_pointer_v<_TPtr>
    595  1.1  joerg              && is_object_v<remove_pointer_t<_TPtr>>
    596  1.1  joerg              && ! is_void_v<remove_pointer_t<_TPtr>>
    597  1.1  joerg              && (sizeof(remove_pointer_t<_TPtr>) > 0), _TPtr>
    598  1.1  joerg midpoint(_TPtr __a, _TPtr __b) noexcept
    599  1.1  joerg {
    600  1.1  joerg     return __a + _VSTD::midpoint(ptrdiff_t(0), __b - __a);
    601  1.1  joerg }
    602  1.1  joerg 
    603  1.1  joerg 
    604  1.1  joerg template <typename _Tp>
    605  1.1  joerg constexpr int __sign(_Tp __val) {
    606  1.1  joerg     return (_Tp(0) < __val) - (__val < _Tp(0));
    607  1.1  joerg }
    608  1.1  joerg 
    609  1.1  joerg template <typename _Fp>
    610  1.1  joerg constexpr _Fp __fp_abs(_Fp __f) { return __f >= 0 ? __f : -__f; }
    611  1.1  joerg 
    612  1.1  joerg template <class _Fp>
    613  1.1  joerg _LIBCPP_INLINE_VISIBILITY constexpr
    614  1.1  joerg enable_if_t<is_floating_point_v<_Fp>, _Fp>
    615  1.1  joerg midpoint(_Fp __a, _Fp __b) noexcept
    616  1.1  joerg {
    617  1.1  joerg     constexpr _Fp __lo = numeric_limits<_Fp>::min()*2;
    618  1.1  joerg     constexpr _Fp __hi = numeric_limits<_Fp>::max()/2;
    619  1.1  joerg     return __fp_abs(__a) <= __hi && __fp_abs(__b) <= __hi ?  // typical case: overflow is impossible
    620  1.1  joerg       (__a + __b)/2 :                                        // always correctly rounded
    621  1.1  joerg       __fp_abs(__a) < __lo ? __a + __b/2 :                   // not safe to halve a
    622  1.1  joerg       __fp_abs(__b) < __lo ? __a/2 + __b :                   // not safe to halve b
    623  1.1  joerg       __a/2 + __b/2;                                         // otherwise correctly rounded
    624  1.1  joerg }
    625  1.1  joerg 
    626  1.1  joerg #endif // _LIBCPP_STD_VER > 17
    627  1.1  joerg 
    628  1.1  joerg _LIBCPP_END_NAMESPACE_STD
    629  1.1  joerg 
    630  1.1  joerg _LIBCPP_POP_MACROS
    631  1.1  joerg 
    632  1.1  joerg #if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
    633  1.1  joerg #   include <__pstl_numeric>
    634  1.1  joerg #endif
    635  1.1  joerg 
    636  1.1  joerg #endif // _LIBCPP_NUMERIC
    637