blob: fc44efff761d9796271db002e038f339fb6913e2 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_NUMERIC
11#define _LIBCPP_NUMERIC
12
13/*
14 numeric synopsis
15
16namespace std
17{
18
19template <class InputIterator, class T>
Mark de Wever17c5af42020-11-28 14:50:53 +010020 constexpr T // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000021 accumulate(InputIterator first, InputIterator last, T init);
22
23template <class InputIterator, class T, class BinaryOperation>
Mark de Wever17c5af42020-11-28 14:50:53 +010024 constexpr T // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000025 accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);
26
Marshall Clow3b7cad22017-06-14 04:48:45 +000027template<class InputIterator>
Mark de Wever17c5af42020-11-28 14:50:53 +010028 constexpr typename iterator_traits<InputIterator>::value_type // constexpr since C++20
Marshall Clow3b7cad22017-06-14 04:48:45 +000029 reduce(InputIterator first, InputIterator last); // C++17
30
31template<class InputIterator, class T>
Mark de Wever17c5af42020-11-28 14:50:53 +010032 constexpr T // constexpr since C++20
Marshall Clow3b7cad22017-06-14 04:48:45 +000033 reduce(InputIterator first, InputIterator last, T init); // C++17
34
35template<class InputIterator, class T, class BinaryOperation>
Mark de Wever17c5af42020-11-28 14:50:53 +010036 constexpr T // constexpr since C++20
Marshall Clow3b7cad22017-06-14 04:48:45 +000037 reduce(InputIterator first, InputIterator last, T init, BinaryOperation binary_op); // C++17
38
Howard Hinnantc51e1022010-05-11 19:42:16 +000039template <class InputIterator1, class InputIterator2, class T>
Mark de Wever17c5af42020-11-28 14:50:53 +010040 constexpr T // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000041 inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init);
42
43template <class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
Mark de Wever17c5af42020-11-28 14:50:53 +010044 constexpr T // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000045 inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
46 T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);
47
Marshall Clow3b7cad22017-06-14 04:48:45 +000048
49template<class InputIterator1, class InputIterator2, class T>
Mark de Wever17c5af42020-11-28 14:50:53 +010050 constexpr T // constexpr since C++20
Marshall Clow3b7cad22017-06-14 04:48:45 +000051 transform_reduce(InputIterator1 first1, InputIterator1 last1,
52 InputIterator2 first2, T init); // C++17
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +000053
Marshall Clow3b7cad22017-06-14 04:48:45 +000054template<class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
Mark de Wever17c5af42020-11-28 14:50:53 +010055 constexpr T // constexpr since C++20
Marshall Clow3b7cad22017-06-14 04:48:45 +000056 transform_reduce(InputIterator1 first1, InputIterator1 last1,
57 InputIterator2 first2, T init,
58 BinaryOperation1 binary_op1, BinaryOperation2 binary_op2); // C++17
59
60template<class InputIterator, class T, class BinaryOperation, class UnaryOperation>
Mark de Wever17c5af42020-11-28 14:50:53 +010061 constexpr T // constexpr since C++20
Marshall Clow3b7cad22017-06-14 04:48:45 +000062 transform_reduce(InputIterator first, InputIterator last, T init,
63 BinaryOperation binary_op, UnaryOperation unary_op); // C++17
64
Howard Hinnantc51e1022010-05-11 19:42:16 +000065template <class InputIterator, class OutputIterator>
Mark de Wever17c5af42020-11-28 14:50:53 +010066 constexpr OutputIterator // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000067 partial_sum(InputIterator first, InputIterator last, OutputIterator result);
68
69template <class InputIterator, class OutputIterator, class BinaryOperation>
Mark de Wever17c5af42020-11-28 14:50:53 +010070 constexpr OutputIterator // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000071 partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
72
Marshall Clow0ce24992017-06-10 02:22:13 +000073template<class InputIterator, class OutputIterator, class T>
Mark de Wever17c5af42020-11-28 14:50:53 +010074 constexpr OutputIterator // constexpr since C++20
Marshall Clow0ce24992017-06-10 02:22:13 +000075 exclusive_scan(InputIterator first, InputIterator last,
76 OutputIterator result, T init); // C++17
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +000077
Marshall Clow0ce24992017-06-10 02:22:13 +000078template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
Mark de Wever17c5af42020-11-28 14:50:53 +010079 constexpr OutputIterator // constexpr since C++20
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +000080 exclusive_scan(InputIterator first, InputIterator last,
Marshall Clow0ce24992017-06-10 02:22:13 +000081 OutputIterator result, T init, BinaryOperation binary_op); // C++17
82
Marshall Clow4c561442017-06-23 05:12:42 +000083template<class InputIterator, class OutputIterator>
Mark de Wever17c5af42020-11-28 14:50:53 +010084 constexpr OutputIterator // constexpr since C++20
Marshall Clow4c561442017-06-23 05:12:42 +000085 inclusive_scan(InputIterator first, InputIterator last, OutputIterator result); // C++17
86
87template<class InputIterator, class OutputIterator, class BinaryOperation>
Mark de Wever17c5af42020-11-28 14:50:53 +010088 constexpr OutputIterator // constexpr since C++20
Marshall Clow4c561442017-06-23 05:12:42 +000089 inclusive_scan(InputIterator first, InputIterator last,
90 OutputIterator result, BinaryOperation binary_op); // C++17
91
92template<class InputIterator, class OutputIterator, class BinaryOperation, class T>
Mark de Wever17c5af42020-11-28 14:50:53 +010093 constexpr OutputIterator // constexpr since C++20
Marshall Clow4c561442017-06-23 05:12:42 +000094 inclusive_scan(InputIterator first, InputIterator last,
95 OutputIterator result, BinaryOperation binary_op, T init); // C++17
96
Marshall Clow0ce24992017-06-10 02:22:13 +000097template<class InputIterator, class OutputIterator, class T,
98 class BinaryOperation, class UnaryOperation>
Mark de Wever17c5af42020-11-28 14:50:53 +010099 constexpr OutputIterator // constexpr since C++20
Marshall Clow0ce24992017-06-10 02:22:13 +0000100 transform_exclusive_scan(InputIterator first, InputIterator last,
101 OutputIterator result, T init,
102 BinaryOperation binary_op, UnaryOperation unary_op); // C++17
103
Marshall Clow4c561442017-06-23 05:12:42 +0000104template<class InputIterator, class OutputIterator,
105 class BinaryOperation, class UnaryOperation>
Mark de Wever17c5af42020-11-28 14:50:53 +0100106 constexpr OutputIterator // constexpr since C++20
Louis Dionne44bcff92018-08-03 22:36:53 +0000107 transform_inclusive_scan(InputIterator first, InputIterator last,
Marshall Clow4c561442017-06-23 05:12:42 +0000108 OutputIterator result,
109 BinaryOperation binary_op, UnaryOperation unary_op); // C++17
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000110
Marshall Clow4c561442017-06-23 05:12:42 +0000111template<class InputIterator, class OutputIterator,
112 class BinaryOperation, class UnaryOperation, class T>
Mark de Wever17c5af42020-11-28 14:50:53 +0100113 constexpr OutputIterator // constexpr since C++20
Louis Dionne44bcff92018-08-03 22:36:53 +0000114 transform_inclusive_scan(InputIterator first, InputIterator last,
Marshall Clow4c561442017-06-23 05:12:42 +0000115 OutputIterator result,
116 BinaryOperation binary_op, UnaryOperation unary_op,
117 T init); // C++17
118
Howard Hinnantc51e1022010-05-11 19:42:16 +0000119template <class InputIterator, class OutputIterator>
Mark de Wever17c5af42020-11-28 14:50:53 +0100120 constexpr OutputIterator // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000121 adjacent_difference(InputIterator first, InputIterator last, OutputIterator result);
122
123template <class InputIterator, class OutputIterator, class BinaryOperation>
Mark de Wever17c5af42020-11-28 14:50:53 +0100124 constexpr OutputIterator // constexpr since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000125 adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
126
Howard Hinnantf45797f2010-05-26 18:53:44 +0000127template <class ForwardIterator, class T>
Mark de Wever17c5af42020-11-28 14:50:53 +0100128 constexpr void // constexpr since C++20
129 iota(ForwardIterator first, ForwardIterator last, T value);
Howard Hinnantf45797f2010-05-26 18:53:44 +0000130
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000131template <class M, class N>
132 constexpr common_type_t<M,N> gcd(M m, N n); // C++17
133
134template <class M, class N>
135 constexpr common_type_t<M,N> lcm(M m, N n); // C++17
136
Mark de Wever17c5af42020-11-28 14:50:53 +0100137template<class T>
138 constexpr T midpoint(T a, T b) noexcept; // C++20
139
140template<class T>
141 constexpr T* midpoint(T* a, T* b); // C++20
Marshall Clowa5f2c8a2019-03-14 16:25:55 +0000142
Howard Hinnantc51e1022010-05-11 19:42:16 +0000143} // std
144
145*/
146
147#include <__config>
Arthur O'Dwyer597cac42021-05-12 23:04:03 -0400148#include <__debug>
149#include <cmath> // for isnormal
150#include <functional>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000151#include <iterator>
Eric Fiseliere7af1d82017-02-08 00:14:13 +0000152#include <limits> // for numeric_limits
Marshall Clow0a1e7502018-09-12 19:41:40 +0000153#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000155#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000156#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000157#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000158
Eric Fiselierf4433a32017-05-31 22:07:49 +0000159_LIBCPP_PUSH_MACROS
160#include <__undef_macros>
161
Howard Hinnantc51e1022010-05-11 19:42:16 +0000162_LIBCPP_BEGIN_NAMESPACE_STD
163
164template <class _InputIterator, class _Tp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100165_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166_Tp
167accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
168{
169 for (; __first != __last; ++__first)
zoecarverfc86b0c2020-11-27 11:05:03 -0800170#if _LIBCPP_STD_VER > 17
171 __init = _VSTD::move(__init) + *__first;
172#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000173 __init = __init + *__first;
zoecarverfc86b0c2020-11-27 11:05:03 -0800174#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000175 return __init;
176}
177
178template <class _InputIterator, class _Tp, class _BinaryOperation>
Mark de Wever17c5af42020-11-28 14:50:53 +0100179_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180_Tp
181accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
182{
183 for (; __first != __last; ++__first)
zoecarverfc86b0c2020-11-27 11:05:03 -0800184#if _LIBCPP_STD_VER > 17
185 __init = __binary_op(_VSTD::move(__init), *__first);
186#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187 __init = __binary_op(__init, *__first);
zoecarverfc86b0c2020-11-27 11:05:03 -0800188#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000189 return __init;
190}
191
Marshall Clow3b7cad22017-06-14 04:48:45 +0000192#if _LIBCPP_STD_VER > 14
193template <class _InputIterator, class _Tp, class _BinaryOp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100194_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow3b7cad22017-06-14 04:48:45 +0000195_Tp
196reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOp __b)
197{
198 for (; __first != __last; ++__first)
199 __init = __b(__init, *__first);
200 return __init;
201}
202
203template <class _InputIterator, class _Tp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100204_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow3b7cad22017-06-14 04:48:45 +0000205_Tp
206reduce(_InputIterator __first, _InputIterator __last, _Tp __init)
207{
208 return _VSTD::reduce(__first, __last, __init, _VSTD::plus<>());
209}
210
211template <class _InputIterator>
Mark de Wever17c5af42020-11-28 14:50:53 +0100212_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow3b7cad22017-06-14 04:48:45 +0000213typename iterator_traits<_InputIterator>::value_type
214reduce(_InputIterator __first, _InputIterator __last)
215{
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000216 return _VSTD::reduce(__first, __last,
Marshall Clow3b7cad22017-06-14 04:48:45 +0000217 typename iterator_traits<_InputIterator>::value_type{});
218}
219#endif
220
Howard Hinnantc51e1022010-05-11 19:42:16 +0000221template <class _InputIterator1, class _InputIterator2, class _Tp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100222_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223_Tp
224inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init)
225{
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000226 for (; __first1 != __last1; ++__first1, (void) ++__first2)
zoecarverfc86b0c2020-11-27 11:05:03 -0800227#if _LIBCPP_STD_VER > 17
228 __init = _VSTD::move(__init) + *__first1 * *__first2;
229#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000230 __init = __init + *__first1 * *__first2;
zoecarverfc86b0c2020-11-27 11:05:03 -0800231#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000232 return __init;
233}
234
235template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
Mark de Wever17c5af42020-11-28 14:50:53 +0100236_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000237_Tp
238inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
239 _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2)
240{
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000241 for (; __first1 != __last1; ++__first1, (void) ++__first2)
zoecarverfc86b0c2020-11-27 11:05:03 -0800242#if _LIBCPP_STD_VER > 17
243 __init = __binary_op1(_VSTD::move(__init), __binary_op2(*__first1, *__first2));
244#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000245 __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
zoecarverfc86b0c2020-11-27 11:05:03 -0800246#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247 return __init;
248}
249
Marshall Clow3b7cad22017-06-14 04:48:45 +0000250#if _LIBCPP_STD_VER > 14
251template <class _InputIterator, class _Tp, class _BinaryOp, class _UnaryOp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100252_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow3b7cad22017-06-14 04:48:45 +0000253_Tp
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000254transform_reduce(_InputIterator __first, _InputIterator __last,
Marshall Clow3b7cad22017-06-14 04:48:45 +0000255 _Tp __init, _BinaryOp __b, _UnaryOp __u)
256{
257 for (; __first != __last; ++__first)
258 __init = __b(__init, __u(*__first));
259 return __init;
260}
261
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000262template <class _InputIterator1, class _InputIterator2,
Marshall Clow3b7cad22017-06-14 04:48:45 +0000263 class _Tp, class _BinaryOp1, class _BinaryOp2>
Mark de Wever17c5af42020-11-28 14:50:53 +0100264_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow3b7cad22017-06-14 04:48:45 +0000265_Tp
266transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
267 _InputIterator2 __first2, _Tp __init, _BinaryOp1 __b1, _BinaryOp2 __b2)
268{
269 for (; __first1 != __last1; ++__first1, (void) ++__first2)
270 __init = __b1(__init, __b2(*__first1, *__first2));
271 return __init;
272}
273
274template <class _InputIterator1, class _InputIterator2, class _Tp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100275_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow3b7cad22017-06-14 04:48:45 +0000276_Tp
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000277transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow3b7cad22017-06-14 04:48:45 +0000278 _InputIterator2 __first2, _Tp __init)
279{
Billy Robert O'Neal III497250a2018-01-05 01:31:57 +0000280 return _VSTD::transform_reduce(__first1, __last1, __first2, _VSTD::move(__init),
Marshall Clow3b7cad22017-06-14 04:48:45 +0000281 _VSTD::plus<>(), _VSTD::multiplies<>());
282}
283#endif
284
Howard Hinnantc51e1022010-05-11 19:42:16 +0000285template <class _InputIterator, class _OutputIterator>
Mark de Wever17c5af42020-11-28 14:50:53 +0100286_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000287_OutputIterator
288partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
289{
290 if (__first != __last)
291 {
292 typename iterator_traits<_InputIterator>::value_type __t(*__first);
293 *__result = __t;
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000294 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000295 {
zoecarverfc86b0c2020-11-27 11:05:03 -0800296#if _LIBCPP_STD_VER > 17
297 __t = _VSTD::move(__t) + *__first;
298#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000299 __t = __t + *__first;
zoecarverfc86b0c2020-11-27 11:05:03 -0800300#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000301 *__result = __t;
302 }
303 }
304 return __result;
305}
306
307template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
Mark de Wever17c5af42020-11-28 14:50:53 +0100308_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000309_OutputIterator
310partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
311 _BinaryOperation __binary_op)
312{
313 if (__first != __last)
314 {
315 typename iterator_traits<_InputIterator>::value_type __t(*__first);
316 *__result = __t;
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000317 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000318 {
zoecarverfc86b0c2020-11-27 11:05:03 -0800319#if _LIBCPP_STD_VER > 17
320 __t = __binary_op(_VSTD::move(__t), *__first);
321#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000322 __t = __binary_op(__t, *__first);
zoecarverfc86b0c2020-11-27 11:05:03 -0800323#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000324 *__result = __t;
325 }
326 }
327 return __result;
328}
329
Marshall Clow0ce24992017-06-10 02:22:13 +0000330#if _LIBCPP_STD_VER > 14
331template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100332_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0ce24992017-06-10 02:22:13 +0000333_OutputIterator
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000334exclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow0ce24992017-06-10 02:22:13 +0000335 _OutputIterator __result, _Tp __init, _BinaryOp __b)
336{
337 if (__first != __last)
338 {
Louis Dionne99e7df82020-11-24 12:29:08 -0500339 _Tp __tmp(__b(__init, *__first));
340 while (true)
Marshall Clow0ce24992017-06-10 02:22:13 +0000341 {
Louis Dionne99e7df82020-11-24 12:29:08 -0500342 *__result = _VSTD::move(__init);
Marshall Clow0ce24992017-06-10 02:22:13 +0000343 ++__result;
Louis Dionne99e7df82020-11-24 12:29:08 -0500344 ++__first;
345 if (__first == __last)
346 break;
347 __init = _VSTD::move(__tmp);
348 __tmp = __b(__init, *__first);
349 }
Marshall Clow0ce24992017-06-10 02:22:13 +0000350 }
351 return __result;
352}
353
354template <class _InputIterator, class _OutputIterator, class _Tp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100355_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0ce24992017-06-10 02:22:13 +0000356_OutputIterator
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000357exclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow0ce24992017-06-10 02:22:13 +0000358 _OutputIterator __result, _Tp __init)
359{
360 return _VSTD::exclusive_scan(__first, __last, __result, __init, _VSTD::plus<>());
361}
362
Marshall Clow4c561442017-06-23 05:12:42 +0000363template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100364_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000365_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow4c561442017-06-23 05:12:42 +0000366 _OutputIterator __result, _BinaryOp __b, _Tp __init)
367{
368 for (; __first != __last; ++__first, (void) ++__result) {
369 __init = __b(__init, *__first);
370 *__result = __init;
Louis Dionne5b20ae92020-11-24 12:26:05 -0500371 }
Marshall Clow4c561442017-06-23 05:12:42 +0000372 return __result;
373}
374
375template <class _InputIterator, class _OutputIterator, class _BinaryOp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100376_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000377_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow4c561442017-06-23 05:12:42 +0000378 _OutputIterator __result, _BinaryOp __b)
379{
380 if (__first != __last) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500381 typename iterator_traits<_InputIterator>::value_type __init = *__first;
Marshall Clow4c561442017-06-23 05:12:42 +0000382 *__result++ = __init;
383 if (++__first != __last)
384 return _VSTD::inclusive_scan(__first, __last, __result, __b, __init);
Louis Dionne5b20ae92020-11-24 12:26:05 -0500385 }
Marshall Clow4c561442017-06-23 05:12:42 +0000386
387 return __result;
388}
389
390template <class _InputIterator, class _OutputIterator>
Mark de Wever17c5af42020-11-28 14:50:53 +0100391_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000392_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow4c561442017-06-23 05:12:42 +0000393 _OutputIterator __result)
394{
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500395 return _VSTD::inclusive_scan(__first, __last, __result, _VSTD::plus<>());
Marshall Clow4c561442017-06-23 05:12:42 +0000396}
397
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000398template <class _InputIterator, class _OutputIterator, class _Tp,
Marshall Clow0ce24992017-06-10 02:22:13 +0000399 class _BinaryOp, class _UnaryOp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100400_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0ce24992017-06-10 02:22:13 +0000401_OutputIterator
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000402transform_exclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow0ce24992017-06-10 02:22:13 +0000403 _OutputIterator __result, _Tp __init,
404 _BinaryOp __b, _UnaryOp __u)
405{
406 if (__first != __last)
407 {
408 _Tp __saved = __init;
409 do
410 {
411 __init = __b(__init, __u(*__first));
412 *__result = __saved;
413 __saved = __init;
414 ++__result;
415 } while (++__first != __last);
416 }
417 return __result;
418}
Marshall Clow4c561442017-06-23 05:12:42 +0000419
420template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp, class _UnaryOp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100421_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
422_OutputIterator
423transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow4c561442017-06-23 05:12:42 +0000424 _OutputIterator __result, _BinaryOp __b, _UnaryOp __u, _Tp __init)
425{
426 for (; __first != __last; ++__first, (void) ++__result) {
427 __init = __b(__init, __u(*__first));
428 *__result = __init;
429 }
430
431 return __result;
432}
433
434template <class _InputIterator, class _OutputIterator, class _BinaryOp, class _UnaryOp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100435_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
436_OutputIterator
437transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow4c561442017-06-23 05:12:42 +0000438 _OutputIterator __result, _BinaryOp __b, _UnaryOp __u)
439{
440 if (__first != __last) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500441 typename iterator_traits<_InputIterator>::value_type __init = __u(*__first);
Marshall Clow4c561442017-06-23 05:12:42 +0000442 *__result++ = __init;
443 if (++__first != __last)
444 return _VSTD::transform_inclusive_scan(__first, __last, __result, __b, __u, __init);
Louis Dionne5b20ae92020-11-24 12:26:05 -0500445 }
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000446
Marshall Clow4c561442017-06-23 05:12:42 +0000447 return __result;
448}
Marshall Clow0ce24992017-06-10 02:22:13 +0000449#endif
450
Howard Hinnantc51e1022010-05-11 19:42:16 +0000451template <class _InputIterator, class _OutputIterator>
Mark de Wever17c5af42020-11-28 14:50:53 +0100452_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000453_OutputIterator
454adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
455{
456 if (__first != __last)
457 {
zoecarverfc86b0c2020-11-27 11:05:03 -0800458 typename iterator_traits<_InputIterator>::value_type __acc(*__first);
459 *__result = __acc;
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000460 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000461 {
zoecarverfc86b0c2020-11-27 11:05:03 -0800462 typename iterator_traits<_InputIterator>::value_type __val(*__first);
463#if _LIBCPP_STD_VER > 17
464 *__result = __val - _VSTD::move(__acc);
465#else
466 *__result = __val - __acc;
467#endif
468 __acc = _VSTD::move(__val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000469 }
470 }
471 return __result;
472}
473
474template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
Mark de Wever17c5af42020-11-28 14:50:53 +0100475_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000476_OutputIterator
477adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
478 _BinaryOperation __binary_op)
479{
480 if (__first != __last)
481 {
zoecarverfc86b0c2020-11-27 11:05:03 -0800482 typename iterator_traits<_InputIterator>::value_type __acc(*__first);
483 *__result = __acc;
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000484 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000485 {
zoecarverfc86b0c2020-11-27 11:05:03 -0800486 typename iterator_traits<_InputIterator>::value_type __val(*__first);
487#if _LIBCPP_STD_VER > 17
488 *__result = __binary_op(__val, _VSTD::move(__acc));
489#else
490 *__result = __binary_op(__val, __acc);
491#endif
492 __acc = _VSTD::move(__val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000493 }
494 }
495 return __result;
496}
497
Howard Hinnantf45797f2010-05-26 18:53:44 +0000498template <class _ForwardIterator, class _Tp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100499_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantf45797f2010-05-26 18:53:44 +0000500void
Howard Hinnantbf074022011-10-22 20:59:45 +0000501iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_)
Howard Hinnantf45797f2010-05-26 18:53:44 +0000502{
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000503 for (; __first != __last; ++__first, (void) ++__value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000504 *__first = __value_;
Howard Hinnantf45797f2010-05-26 18:53:44 +0000505}
506
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000507
508#if _LIBCPP_STD_VER > 14
Marshall Clowf41c2422019-06-18 18:13:54 +0000509template <typename _Result, typename _Source, bool _IsSigned = is_signed<_Source>::value> struct __ct_abs;
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000510
Marshall Clowf9316722017-02-10 20:49:08 +0000511template <typename _Result, typename _Source>
Marshall Clowf41c2422019-06-18 18:13:54 +0000512struct __ct_abs<_Result, _Source, true> {
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000513 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Marshall Clowf9316722017-02-10 20:49:08 +0000514 _Result operator()(_Source __t) const noexcept
515 {
Louis Dionne5b20ae92020-11-24 12:26:05 -0500516 if (__t >= 0) return __t;
517 if (__t == numeric_limits<_Source>::min()) return -static_cast<_Result>(__t);
518 return -__t;
Marshall Clowf9316722017-02-10 20:49:08 +0000519 }
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000520};
521
Marshall Clowf9316722017-02-10 20:49:08 +0000522template <typename _Result, typename _Source>
Marshall Clowf41c2422019-06-18 18:13:54 +0000523struct __ct_abs<_Result, _Source, false> {
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000524 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Marshall Clowf9316722017-02-10 20:49:08 +0000525 _Result operator()(_Source __t) const noexcept { return __t; }
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000526};
527
528
529template<class _Tp>
Eric Fiseliered86f1f2017-05-09 00:00:00 +0000530_LIBCPP_CONSTEXPR _LIBCPP_HIDDEN
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000531_Tp __gcd(_Tp __m, _Tp __n)
532{
Marshall Clowf9316722017-02-10 20:49:08 +0000533 static_assert((!is_signed<_Tp>::value), "");
Eric Fiseliered86f1f2017-05-09 00:00:00 +0000534 return __n == 0 ? __m : _VSTD::__gcd<_Tp>(__n, __m % __n);
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000535}
536
537
538template<class _Tp, class _Up>
539_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
540common_type_t<_Tp,_Up>
541gcd(_Tp __m, _Up __n)
542{
543 static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to gcd must be integer types");
Marshall Clow78dbe462016-11-14 18:22:19 +0000544 static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to gcd cannot be bool" );
545 static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to gcd cannot be bool" );
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000546 using _Rp = common_type_t<_Tp,_Up>;
547 using _Wp = make_unsigned_t<_Rp>;
Eric Fiseliered86f1f2017-05-09 00:00:00 +0000548 return static_cast<_Rp>(_VSTD::__gcd(
Marshall Clowf41c2422019-06-18 18:13:54 +0000549 static_cast<_Wp>(__ct_abs<_Rp, _Tp>()(__m)),
550 static_cast<_Wp>(__ct_abs<_Rp, _Up>()(__n))));
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000551}
552
553template<class _Tp, class _Up>
554_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
555common_type_t<_Tp,_Up>
556lcm(_Tp __m, _Up __n)
557{
558 static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to lcm must be integer types");
Marshall Clow78dbe462016-11-14 18:22:19 +0000559 static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to lcm cannot be bool" );
560 static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to lcm cannot be bool" );
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000561 if (__m == 0 || __n == 0)
562 return 0;
563
564 using _Rp = common_type_t<_Tp,_Up>;
Marshall Clowf41c2422019-06-18 18:13:54 +0000565 _Rp __val1 = __ct_abs<_Rp, _Tp>()(__m) / _VSTD::gcd(__m, __n);
566 _Rp __val2 = __ct_abs<_Rp, _Up>()(__n);
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000567 _LIBCPP_ASSERT((numeric_limits<_Rp>::max() / __val1 > __val2), "Overflow in lcm");
568 return __val1 * __val2;
569}
570
571#endif /* _LIBCPP_STD_VER > 14 */
572
Marshall Clowa5f2c8a2019-03-14 16:25:55 +0000573#if _LIBCPP_STD_VER > 17
574template <class _Tp>
575_LIBCPP_INLINE_VISIBILITY constexpr
Marshall Clowa6091132019-05-29 15:17:55 +0000576enable_if_t<is_integral_v<_Tp> && !is_same_v<bool, _Tp> && !is_null_pointer_v<_Tp>, _Tp>
Marshall Clowa5f2c8a2019-03-14 16:25:55 +0000577midpoint(_Tp __a, _Tp __b) noexcept
578_LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
579{
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500580 using _Up = make_unsigned_t<_Tp>;
581 constexpr _Up __bitshift = numeric_limits<_Up>::digits - 1;
Marshall Clowa5f2c8a2019-03-14 16:25:55 +0000582
Jorg Browna1d2dfe2019-11-04 19:00:23 -0800583 _Up __diff = _Up(__b) - _Up(__a);
584 _Up __sign_bit = __b < __a;
585
586 _Up __half_diff = (__diff / 2) + (__sign_bit << __bitshift) + (__sign_bit & __diff);
587
588 return __a + __half_diff;
Marshall Clowa5f2c8a2019-03-14 16:25:55 +0000589}
590
591
592template <class _TPtr>
593_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne173f29e2019-05-29 16:01:36 +0000594enable_if_t<is_pointer_v<_TPtr>
595 && is_object_v<remove_pointer_t<_TPtr>>
596 && ! is_void_v<remove_pointer_t<_TPtr>>
Marshall Clowa6091132019-05-29 15:17:55 +0000597 && (sizeof(remove_pointer_t<_TPtr>) > 0), _TPtr>
Marshall Clowa5f2c8a2019-03-14 16:25:55 +0000598midpoint(_TPtr __a, _TPtr __b) noexcept
599{
600 return __a + _VSTD::midpoint(ptrdiff_t(0), __b - __a);
601}
Marshall Clow174901e2019-04-25 12:11:43 +0000602
603
604template <typename _Tp>
Marshall Clow2dc0ef12019-05-07 16:07:24 +0000605constexpr int __sign(_Tp __val) {
Marshall Clow174901e2019-04-25 12:11:43 +0000606 return (_Tp(0) < __val) - (__val < _Tp(0));
607}
608
Marshall Clowf41c2422019-06-18 18:13:54 +0000609template <typename _Fp>
610constexpr _Fp __fp_abs(_Fp __f) { return __f >= 0 ? __f : -__f; }
611
Marshall Clow174901e2019-04-25 12:11:43 +0000612template <class _Fp>
613_LIBCPP_INLINE_VISIBILITY constexpr
614enable_if_t<is_floating_point_v<_Fp>, _Fp>
615midpoint(_Fp __a, _Fp __b) noexcept
616{
Arthur O'Dwyer2fc9b5d2021-04-17 17:03:20 -0400617 constexpr _Fp __lo = numeric_limits<_Fp>::min()*2;
618 constexpr _Fp __hi = numeric_limits<_Fp>::max()/2;
Marshall Clowf41c2422019-06-18 18:13:54 +0000619 return __fp_abs(__a) <= __hi && __fp_abs(__b) <= __hi ? // typical case: overflow is impossible
620 (__a + __b)/2 : // always correctly rounded
621 __fp_abs(__a) < __lo ? __a + __b/2 : // not safe to halve a
Ruslan Baratov1adc3502019-12-21 01:22:12 -0800622 __fp_abs(__b) < __lo ? __a/2 + __b : // not safe to halve b
Marshall Clowf41c2422019-06-18 18:13:54 +0000623 __a/2 + __b/2; // otherwise correctly rounded
Marshall Clow174901e2019-04-25 12:11:43 +0000624}
Marshall Clowf41c2422019-06-18 18:13:54 +0000625
Marshall Clow174901e2019-04-25 12:11:43 +0000626#endif // _LIBCPP_STD_VER > 17
Marshall Clowa5f2c8a2019-03-14 16:25:55 +0000627
Howard Hinnantc51e1022010-05-11 19:42:16 +0000628_LIBCPP_END_NAMESPACE_STD
629
Eric Fiselierf4433a32017-05-31 22:07:49 +0000630_LIBCPP_POP_MACROS
631
Louis Dionne59d0b3c2019-08-05 18:29:14 +0000632#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +0000633# include <__pstl_numeric>
Louis Dionne59d0b3c2019-08-05 18:29:14 +0000634#endif
635
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400636#endif // _LIBCPP_NUMERIC