blob: ed06fcc49d5b5d9ee8d589c3156873336d65b996 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- numeric ---------------------------------===//
3//
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>
148#include <iterator>
Eric Fiseliere7af1d82017-02-08 00:14:13 +0000149#include <limits> // for numeric_limits
Marshall Clow0ce24992017-06-10 02:22:13 +0000150#include <functional>
Marshall Clow174901e2019-04-25 12:11:43 +0000151#include <cmath> // for isnormal
Marshall Clow0a1e7502018-09-12 19:41:40 +0000152#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000153
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000154#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000155#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000156#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157
Eric Fiselierf4433a32017-05-31 22:07:49 +0000158_LIBCPP_PUSH_MACROS
159#include <__undef_macros>
160
Howard Hinnantc51e1022010-05-11 19:42:16 +0000161_LIBCPP_BEGIN_NAMESPACE_STD
162
163template <class _InputIterator, class _Tp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100164_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000165_Tp
166accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
167{
168 for (; __first != __last; ++__first)
zoecarverfc86b0c2020-11-27 11:05:03 -0800169#if _LIBCPP_STD_VER > 17
170 __init = _VSTD::move(__init) + *__first;
171#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000172 __init = __init + *__first;
zoecarverfc86b0c2020-11-27 11:05:03 -0800173#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000174 return __init;
175}
176
177template <class _InputIterator, class _Tp, class _BinaryOperation>
Mark de Wever17c5af42020-11-28 14:50:53 +0100178_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000179_Tp
180accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
181{
182 for (; __first != __last; ++__first)
zoecarverfc86b0c2020-11-27 11:05:03 -0800183#if _LIBCPP_STD_VER > 17
184 __init = __binary_op(_VSTD::move(__init), *__first);
185#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186 __init = __binary_op(__init, *__first);
zoecarverfc86b0c2020-11-27 11:05:03 -0800187#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000188 return __init;
189}
190
Marshall Clow3b7cad22017-06-14 04:48:45 +0000191#if _LIBCPP_STD_VER > 14
192template <class _InputIterator, class _Tp, class _BinaryOp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100193_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow3b7cad22017-06-14 04:48:45 +0000194_Tp
195reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOp __b)
196{
197 for (; __first != __last; ++__first)
198 __init = __b(__init, *__first);
199 return __init;
200}
201
202template <class _InputIterator, class _Tp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100203_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow3b7cad22017-06-14 04:48:45 +0000204_Tp
205reduce(_InputIterator __first, _InputIterator __last, _Tp __init)
206{
207 return _VSTD::reduce(__first, __last, __init, _VSTD::plus<>());
208}
209
210template <class _InputIterator>
Mark de Wever17c5af42020-11-28 14:50:53 +0100211_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow3b7cad22017-06-14 04:48:45 +0000212typename iterator_traits<_InputIterator>::value_type
213reduce(_InputIterator __first, _InputIterator __last)
214{
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000215 return _VSTD::reduce(__first, __last,
Marshall Clow3b7cad22017-06-14 04:48:45 +0000216 typename iterator_traits<_InputIterator>::value_type{});
217}
218#endif
219
Howard Hinnantc51e1022010-05-11 19:42:16 +0000220template <class _InputIterator1, class _InputIterator2, class _Tp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100221_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000222_Tp
223inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init)
224{
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000225 for (; __first1 != __last1; ++__first1, (void) ++__first2)
zoecarverfc86b0c2020-11-27 11:05:03 -0800226#if _LIBCPP_STD_VER > 17
227 __init = _VSTD::move(__init) + *__first1 * *__first2;
228#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000229 __init = __init + *__first1 * *__first2;
zoecarverfc86b0c2020-11-27 11:05:03 -0800230#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231 return __init;
232}
233
234template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
Mark de Wever17c5af42020-11-28 14:50:53 +0100235_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000236_Tp
237inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
238 _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2)
239{
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000240 for (; __first1 != __last1; ++__first1, (void) ++__first2)
zoecarverfc86b0c2020-11-27 11:05:03 -0800241#if _LIBCPP_STD_VER > 17
242 __init = __binary_op1(_VSTD::move(__init), __binary_op2(*__first1, *__first2));
243#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000244 __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
zoecarverfc86b0c2020-11-27 11:05:03 -0800245#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000246 return __init;
247}
248
Marshall Clow3b7cad22017-06-14 04:48:45 +0000249#if _LIBCPP_STD_VER > 14
250template <class _InputIterator, class _Tp, class _BinaryOp, class _UnaryOp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100251_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow3b7cad22017-06-14 04:48:45 +0000252_Tp
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000253transform_reduce(_InputIterator __first, _InputIterator __last,
Marshall Clow3b7cad22017-06-14 04:48:45 +0000254 _Tp __init, _BinaryOp __b, _UnaryOp __u)
255{
256 for (; __first != __last; ++__first)
257 __init = __b(__init, __u(*__first));
258 return __init;
259}
260
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000261template <class _InputIterator1, class _InputIterator2,
Marshall Clow3b7cad22017-06-14 04:48:45 +0000262 class _Tp, class _BinaryOp1, class _BinaryOp2>
Mark de Wever17c5af42020-11-28 14:50:53 +0100263_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow3b7cad22017-06-14 04:48:45 +0000264_Tp
265transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
266 _InputIterator2 __first2, _Tp __init, _BinaryOp1 __b1, _BinaryOp2 __b2)
267{
268 for (; __first1 != __last1; ++__first1, (void) ++__first2)
269 __init = __b1(__init, __b2(*__first1, *__first2));
270 return __init;
271}
272
273template <class _InputIterator1, class _InputIterator2, class _Tp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100274_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow3b7cad22017-06-14 04:48:45 +0000275_Tp
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000276transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow3b7cad22017-06-14 04:48:45 +0000277 _InputIterator2 __first2, _Tp __init)
278{
Billy Robert O'Neal III497250a2018-01-05 01:31:57 +0000279 return _VSTD::transform_reduce(__first1, __last1, __first2, _VSTD::move(__init),
Marshall Clow3b7cad22017-06-14 04:48:45 +0000280 _VSTD::plus<>(), _VSTD::multiplies<>());
281}
282#endif
283
Howard Hinnantc51e1022010-05-11 19:42:16 +0000284template <class _InputIterator, class _OutputIterator>
Mark de Wever17c5af42020-11-28 14:50:53 +0100285_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000286_OutputIterator
287partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
288{
289 if (__first != __last)
290 {
291 typename iterator_traits<_InputIterator>::value_type __t(*__first);
292 *__result = __t;
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000293 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000294 {
zoecarverfc86b0c2020-11-27 11:05:03 -0800295#if _LIBCPP_STD_VER > 17
296 __t = _VSTD::move(__t) + *__first;
297#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298 __t = __t + *__first;
zoecarverfc86b0c2020-11-27 11:05:03 -0800299#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300 *__result = __t;
301 }
302 }
303 return __result;
304}
305
306template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
Mark de Wever17c5af42020-11-28 14:50:53 +0100307_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000308_OutputIterator
309partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
310 _BinaryOperation __binary_op)
311{
312 if (__first != __last)
313 {
314 typename iterator_traits<_InputIterator>::value_type __t(*__first);
315 *__result = __t;
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000316 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000317 {
zoecarverfc86b0c2020-11-27 11:05:03 -0800318#if _LIBCPP_STD_VER > 17
319 __t = __binary_op(_VSTD::move(__t), *__first);
320#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000321 __t = __binary_op(__t, *__first);
zoecarverfc86b0c2020-11-27 11:05:03 -0800322#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000323 *__result = __t;
324 }
325 }
326 return __result;
327}
328
Marshall Clow0ce24992017-06-10 02:22:13 +0000329#if _LIBCPP_STD_VER > 14
330template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100331_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0ce24992017-06-10 02:22:13 +0000332_OutputIterator
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000333exclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow0ce24992017-06-10 02:22:13 +0000334 _OutputIterator __result, _Tp __init, _BinaryOp __b)
335{
336 if (__first != __last)
337 {
Louis Dionne99e7df82020-11-24 12:29:08 -0500338 _Tp __tmp(__b(__init, *__first));
339 while (true)
Marshall Clow0ce24992017-06-10 02:22:13 +0000340 {
Louis Dionne99e7df82020-11-24 12:29:08 -0500341 *__result = _VSTD::move(__init);
Marshall Clow0ce24992017-06-10 02:22:13 +0000342 ++__result;
Louis Dionne99e7df82020-11-24 12:29:08 -0500343 ++__first;
344 if (__first == __last)
345 break;
346 __init = _VSTD::move(__tmp);
347 __tmp = __b(__init, *__first);
348 }
Marshall Clow0ce24992017-06-10 02:22:13 +0000349 }
350 return __result;
351}
352
353template <class _InputIterator, class _OutputIterator, class _Tp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100354_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0ce24992017-06-10 02:22:13 +0000355_OutputIterator
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000356exclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow0ce24992017-06-10 02:22:13 +0000357 _OutputIterator __result, _Tp __init)
358{
359 return _VSTD::exclusive_scan(__first, __last, __result, __init, _VSTD::plus<>());
360}
361
Marshall Clow4c561442017-06-23 05:12:42 +0000362template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100363_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000364_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow4c561442017-06-23 05:12:42 +0000365 _OutputIterator __result, _BinaryOp __b, _Tp __init)
366{
367 for (; __first != __last; ++__first, (void) ++__result) {
368 __init = __b(__init, *__first);
369 *__result = __init;
Louis Dionne5b20ae92020-11-24 12:26:05 -0500370 }
Marshall Clow4c561442017-06-23 05:12:42 +0000371 return __result;
372}
373
374template <class _InputIterator, class _OutputIterator, class _BinaryOp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100375_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000376_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow4c561442017-06-23 05:12:42 +0000377 _OutputIterator __result, _BinaryOp __b)
378{
379 if (__first != __last) {
380 typename std::iterator_traits<_InputIterator>::value_type __init = *__first;
381 *__result++ = __init;
382 if (++__first != __last)
383 return _VSTD::inclusive_scan(__first, __last, __result, __b, __init);
Louis Dionne5b20ae92020-11-24 12:26:05 -0500384 }
Marshall Clow4c561442017-06-23 05:12:42 +0000385
386 return __result;
387}
388
389template <class _InputIterator, class _OutputIterator>
Mark de Wever17c5af42020-11-28 14:50:53 +0100390_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000391_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow4c561442017-06-23 05:12:42 +0000392 _OutputIterator __result)
393{
394 return _VSTD::inclusive_scan(__first, __last, __result, std::plus<>());
395}
396
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000397template <class _InputIterator, class _OutputIterator, class _Tp,
Marshall Clow0ce24992017-06-10 02:22:13 +0000398 class _BinaryOp, class _UnaryOp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100399_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0ce24992017-06-10 02:22:13 +0000400_OutputIterator
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000401transform_exclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow0ce24992017-06-10 02:22:13 +0000402 _OutputIterator __result, _Tp __init,
403 _BinaryOp __b, _UnaryOp __u)
404{
405 if (__first != __last)
406 {
407 _Tp __saved = __init;
408 do
409 {
410 __init = __b(__init, __u(*__first));
411 *__result = __saved;
412 __saved = __init;
413 ++__result;
414 } while (++__first != __last);
415 }
416 return __result;
417}
Marshall Clow4c561442017-06-23 05:12:42 +0000418
419template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp, class _UnaryOp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100420_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
421_OutputIterator
422transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow4c561442017-06-23 05:12:42 +0000423 _OutputIterator __result, _BinaryOp __b, _UnaryOp __u, _Tp __init)
424{
425 for (; __first != __last; ++__first, (void) ++__result) {
426 __init = __b(__init, __u(*__first));
427 *__result = __init;
428 }
429
430 return __result;
431}
432
433template <class _InputIterator, class _OutputIterator, class _BinaryOp, class _UnaryOp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100434_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
435_OutputIterator
436transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow4c561442017-06-23 05:12:42 +0000437 _OutputIterator __result, _BinaryOp __b, _UnaryOp __u)
438{
439 if (__first != __last) {
440 typename std::iterator_traits<_InputIterator>::value_type __init = __u(*__first);
441 *__result++ = __init;
442 if (++__first != __last)
443 return _VSTD::transform_inclusive_scan(__first, __last, __result, __b, __u, __init);
Louis Dionne5b20ae92020-11-24 12:26:05 -0500444 }
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000445
Marshall Clow4c561442017-06-23 05:12:42 +0000446 return __result;
447}
Marshall Clow0ce24992017-06-10 02:22:13 +0000448#endif
449
Howard Hinnantc51e1022010-05-11 19:42:16 +0000450template <class _InputIterator, class _OutputIterator>
Mark de Wever17c5af42020-11-28 14:50:53 +0100451_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000452_OutputIterator
453adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
454{
455 if (__first != __last)
456 {
zoecarverfc86b0c2020-11-27 11:05:03 -0800457 typename iterator_traits<_InputIterator>::value_type __acc(*__first);
458 *__result = __acc;
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000459 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000460 {
zoecarverfc86b0c2020-11-27 11:05:03 -0800461 typename iterator_traits<_InputIterator>::value_type __val(*__first);
462#if _LIBCPP_STD_VER > 17
463 *__result = __val - _VSTD::move(__acc);
464#else
465 *__result = __val - __acc;
466#endif
467 __acc = _VSTD::move(__val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000468 }
469 }
470 return __result;
471}
472
473template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
Mark de Wever17c5af42020-11-28 14:50:53 +0100474_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000475_OutputIterator
476adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
477 _BinaryOperation __binary_op)
478{
479 if (__first != __last)
480 {
zoecarverfc86b0c2020-11-27 11:05:03 -0800481 typename iterator_traits<_InputIterator>::value_type __acc(*__first);
482 *__result = __acc;
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000483 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000484 {
zoecarverfc86b0c2020-11-27 11:05:03 -0800485 typename iterator_traits<_InputIterator>::value_type __val(*__first);
486#if _LIBCPP_STD_VER > 17
487 *__result = __binary_op(__val, _VSTD::move(__acc));
488#else
489 *__result = __binary_op(__val, __acc);
490#endif
491 __acc = _VSTD::move(__val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000492 }
493 }
494 return __result;
495}
496
Howard Hinnantf45797f2010-05-26 18:53:44 +0000497template <class _ForwardIterator, class _Tp>
Mark de Wever17c5af42020-11-28 14:50:53 +0100498_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantf45797f2010-05-26 18:53:44 +0000499void
Howard Hinnantbf074022011-10-22 20:59:45 +0000500iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_)
Howard Hinnantf45797f2010-05-26 18:53:44 +0000501{
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000502 for (; __first != __last; ++__first, (void) ++__value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000503 *__first = __value_;
Howard Hinnantf45797f2010-05-26 18:53:44 +0000504}
505
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000506
507#if _LIBCPP_STD_VER > 14
Marshall Clowf41c2422019-06-18 18:13:54 +0000508template <typename _Result, typename _Source, bool _IsSigned = is_signed<_Source>::value> struct __ct_abs;
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000509
Marshall Clowf9316722017-02-10 20:49:08 +0000510template <typename _Result, typename _Source>
Marshall Clowf41c2422019-06-18 18:13:54 +0000511struct __ct_abs<_Result, _Source, true> {
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000512 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Marshall Clowf9316722017-02-10 20:49:08 +0000513 _Result operator()(_Source __t) const noexcept
514 {
Louis Dionne5b20ae92020-11-24 12:26:05 -0500515 if (__t >= 0) return __t;
516 if (__t == numeric_limits<_Source>::min()) return -static_cast<_Result>(__t);
517 return -__t;
Marshall Clowf9316722017-02-10 20:49:08 +0000518 }
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000519};
520
Marshall Clowf9316722017-02-10 20:49:08 +0000521template <typename _Result, typename _Source>
Marshall Clowf41c2422019-06-18 18:13:54 +0000522struct __ct_abs<_Result, _Source, false> {
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000523 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Marshall Clowf9316722017-02-10 20:49:08 +0000524 _Result operator()(_Source __t) const noexcept { return __t; }
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000525};
526
527
528template<class _Tp>
Eric Fiseliered86f1f2017-05-09 00:00:00 +0000529_LIBCPP_CONSTEXPR _LIBCPP_HIDDEN
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000530_Tp __gcd(_Tp __m, _Tp __n)
531{
Marshall Clowf9316722017-02-10 20:49:08 +0000532 static_assert((!is_signed<_Tp>::value), "");
Eric Fiseliered86f1f2017-05-09 00:00:00 +0000533 return __n == 0 ? __m : _VSTD::__gcd<_Tp>(__n, __m % __n);
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000534}
535
536
537template<class _Tp, class _Up>
538_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
539common_type_t<_Tp,_Up>
540gcd(_Tp __m, _Up __n)
541{
542 static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to gcd must be integer types");
Marshall Clow78dbe462016-11-14 18:22:19 +0000543 static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to gcd cannot be bool" );
544 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 +0000545 using _Rp = common_type_t<_Tp,_Up>;
546 using _Wp = make_unsigned_t<_Rp>;
Eric Fiseliered86f1f2017-05-09 00:00:00 +0000547 return static_cast<_Rp>(_VSTD::__gcd(
Marshall Clowf41c2422019-06-18 18:13:54 +0000548 static_cast<_Wp>(__ct_abs<_Rp, _Tp>()(__m)),
549 static_cast<_Wp>(__ct_abs<_Rp, _Up>()(__n))));
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000550}
551
552template<class _Tp, class _Up>
553_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
554common_type_t<_Tp,_Up>
555lcm(_Tp __m, _Up __n)
556{
557 static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to lcm must be integer types");
Marshall Clow78dbe462016-11-14 18:22:19 +0000558 static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to lcm cannot be bool" );
559 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 +0000560 if (__m == 0 || __n == 0)
561 return 0;
562
563 using _Rp = common_type_t<_Tp,_Up>;
Marshall Clowf41c2422019-06-18 18:13:54 +0000564 _Rp __val1 = __ct_abs<_Rp, _Tp>()(__m) / _VSTD::gcd(__m, __n);
565 _Rp __val2 = __ct_abs<_Rp, _Up>()(__n);
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000566 _LIBCPP_ASSERT((numeric_limits<_Rp>::max() / __val1 > __val2), "Overflow in lcm");
567 return __val1 * __val2;
568}
569
570#endif /* _LIBCPP_STD_VER > 14 */
571
Marshall Clowa5f2c8a2019-03-14 16:25:55 +0000572#if _LIBCPP_STD_VER > 17
573template <class _Tp>
574_LIBCPP_INLINE_VISIBILITY constexpr
Marshall Clowa6091132019-05-29 15:17:55 +0000575enable_if_t<is_integral_v<_Tp> && !is_same_v<bool, _Tp> && !is_null_pointer_v<_Tp>, _Tp>
Marshall Clowa5f2c8a2019-03-14 16:25:55 +0000576midpoint(_Tp __a, _Tp __b) noexcept
577_LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
578{
579 using _Up = std::make_unsigned_t<_Tp>;
Jorg Browna1d2dfe2019-11-04 19:00:23 -0800580 constexpr _Up __bitshift = std::numeric_limits<_Up>::digits - 1;
Marshall Clowa5f2c8a2019-03-14 16:25:55 +0000581
Jorg Browna1d2dfe2019-11-04 19:00:23 -0800582 _Up __diff = _Up(__b) - _Up(__a);
583 _Up __sign_bit = __b < __a;
584
585 _Up __half_diff = (__diff / 2) + (__sign_bit << __bitshift) + (__sign_bit & __diff);
586
587 return __a + __half_diff;
Marshall Clowa5f2c8a2019-03-14 16:25:55 +0000588}
589
590
591template <class _TPtr>
592_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne173f29e2019-05-29 16:01:36 +0000593enable_if_t<is_pointer_v<_TPtr>
594 && is_object_v<remove_pointer_t<_TPtr>>
595 && ! is_void_v<remove_pointer_t<_TPtr>>
Marshall Clowa6091132019-05-29 15:17:55 +0000596 && (sizeof(remove_pointer_t<_TPtr>) > 0), _TPtr>
Marshall Clowa5f2c8a2019-03-14 16:25:55 +0000597midpoint(_TPtr __a, _TPtr __b) noexcept
598{
599 return __a + _VSTD::midpoint(ptrdiff_t(0), __b - __a);
600}
Marshall Clow174901e2019-04-25 12:11:43 +0000601
602
603template <typename _Tp>
Marshall Clow2dc0ef12019-05-07 16:07:24 +0000604constexpr int __sign(_Tp __val) {
Marshall Clow174901e2019-04-25 12:11:43 +0000605 return (_Tp(0) < __val) - (__val < _Tp(0));
606}
607
Marshall Clowf41c2422019-06-18 18:13:54 +0000608template <typename _Fp>
609constexpr _Fp __fp_abs(_Fp __f) { return __f >= 0 ? __f : -__f; }
610
Marshall Clow174901e2019-04-25 12:11:43 +0000611template <class _Fp>
612_LIBCPP_INLINE_VISIBILITY constexpr
613enable_if_t<is_floating_point_v<_Fp>, _Fp>
614midpoint(_Fp __a, _Fp __b) noexcept
615{
Marshall Clowf41c2422019-06-18 18:13:54 +0000616 constexpr _Fp __lo = numeric_limits<_Fp>::min()*2;
617 constexpr _Fp __hi = numeric_limits<_Fp>::max()/2;
618 return __fp_abs(__a) <= __hi && __fp_abs(__b) <= __hi ? // typical case: overflow is impossible
619 (__a + __b)/2 : // always correctly rounded
620 __fp_abs(__a) < __lo ? __a + __b/2 : // not safe to halve a
Ruslan Baratov1adc3502019-12-21 01:22:12 -0800621 __fp_abs(__b) < __lo ? __a/2 + __b : // not safe to halve b
Marshall Clowf41c2422019-06-18 18:13:54 +0000622 __a/2 + __b/2; // otherwise correctly rounded
Marshall Clow174901e2019-04-25 12:11:43 +0000623}
Marshall Clowf41c2422019-06-18 18:13:54 +0000624
Marshall Clow174901e2019-04-25 12:11:43 +0000625#endif // _LIBCPP_STD_VER > 17
Marshall Clowa5f2c8a2019-03-14 16:25:55 +0000626
Howard Hinnantc51e1022010-05-11 19:42:16 +0000627_LIBCPP_END_NAMESPACE_STD
628
Eric Fiselierf4433a32017-05-31 22:07:49 +0000629_LIBCPP_POP_MACROS
630
Louis Dionne59d0b3c2019-08-05 18:29:14 +0000631#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +0000632# include <__pstl_numeric>
Louis Dionne59d0b3c2019-08-05 18:29:14 +0000633#endif
634
Howard Hinnantc51e1022010-05-11 19:42:16 +0000635#endif // _LIBCPP_NUMERIC