blob: ce4fc5f55347c768624faa31613c383a1feafeec [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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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 Weverab4eec72020-11-24 14:55:55 +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)
169 __init = __init + *__first;
170 return __init;
171}
172
173template <class _InputIterator, class _Tp, class _BinaryOperation>
Mark de Weverab4eec72020-11-24 14:55:55 +0100174_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000175_Tp
176accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
177{
178 for (; __first != __last; ++__first)
179 __init = __binary_op(__init, *__first);
180 return __init;
181}
182
Marshall Clow3b7cad22017-06-14 04:48:45 +0000183#if _LIBCPP_STD_VER > 14
184template <class _InputIterator, class _Tp, class _BinaryOp>
Mark de Weverab4eec72020-11-24 14:55:55 +0100185_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow3b7cad22017-06-14 04:48:45 +0000186_Tp
187reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOp __b)
188{
189 for (; __first != __last; ++__first)
190 __init = __b(__init, *__first);
191 return __init;
192}
193
194template <class _InputIterator, class _Tp>
Mark de Weverab4eec72020-11-24 14:55:55 +0100195_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow3b7cad22017-06-14 04:48:45 +0000196_Tp
197reduce(_InputIterator __first, _InputIterator __last, _Tp __init)
198{
199 return _VSTD::reduce(__first, __last, __init, _VSTD::plus<>());
200}
201
202template <class _InputIterator>
Mark de Weverab4eec72020-11-24 14:55:55 +0100203_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow3b7cad22017-06-14 04:48:45 +0000204typename iterator_traits<_InputIterator>::value_type
205reduce(_InputIterator __first, _InputIterator __last)
206{
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000207 return _VSTD::reduce(__first, __last,
Marshall Clow3b7cad22017-06-14 04:48:45 +0000208 typename iterator_traits<_InputIterator>::value_type{});
209}
210#endif
211
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212template <class _InputIterator1, class _InputIterator2, class _Tp>
Mark de Weverab4eec72020-11-24 14:55:55 +0100213_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000214_Tp
215inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init)
216{
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000217 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000218 __init = __init + *__first1 * *__first2;
219 return __init;
220}
221
222template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
Mark de Weverab4eec72020-11-24 14:55:55 +0100223_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000224_Tp
225inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
226 _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2)
227{
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000228 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000229 __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
230 return __init;
231}
232
Marshall Clow3b7cad22017-06-14 04:48:45 +0000233#if _LIBCPP_STD_VER > 14
234template <class _InputIterator, class _Tp, class _BinaryOp, class _UnaryOp>
Mark de Weverab4eec72020-11-24 14:55:55 +0100235_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow3b7cad22017-06-14 04:48:45 +0000236_Tp
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000237transform_reduce(_InputIterator __first, _InputIterator __last,
Marshall Clow3b7cad22017-06-14 04:48:45 +0000238 _Tp __init, _BinaryOp __b, _UnaryOp __u)
239{
240 for (; __first != __last; ++__first)
241 __init = __b(__init, __u(*__first));
242 return __init;
243}
244
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000245template <class _InputIterator1, class _InputIterator2,
Marshall Clow3b7cad22017-06-14 04:48:45 +0000246 class _Tp, class _BinaryOp1, class _BinaryOp2>
Mark de Weverab4eec72020-11-24 14:55:55 +0100247_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow3b7cad22017-06-14 04:48:45 +0000248_Tp
249transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
250 _InputIterator2 __first2, _Tp __init, _BinaryOp1 __b1, _BinaryOp2 __b2)
251{
252 for (; __first1 != __last1; ++__first1, (void) ++__first2)
253 __init = __b1(__init, __b2(*__first1, *__first2));
254 return __init;
255}
256
257template <class _InputIterator1, class _InputIterator2, class _Tp>
Mark de Weverab4eec72020-11-24 14:55:55 +0100258_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow3b7cad22017-06-14 04:48:45 +0000259_Tp
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000260transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow3b7cad22017-06-14 04:48:45 +0000261 _InputIterator2 __first2, _Tp __init)
262{
Billy Robert O'Neal III497250a2018-01-05 01:31:57 +0000263 return _VSTD::transform_reduce(__first1, __last1, __first2, _VSTD::move(__init),
Marshall Clow3b7cad22017-06-14 04:48:45 +0000264 _VSTD::plus<>(), _VSTD::multiplies<>());
265}
266#endif
267
Howard Hinnantc51e1022010-05-11 19:42:16 +0000268template <class _InputIterator, class _OutputIterator>
Mark de Weverab4eec72020-11-24 14:55:55 +0100269_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270_OutputIterator
271partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
272{
273 if (__first != __last)
274 {
275 typename iterator_traits<_InputIterator>::value_type __t(*__first);
276 *__result = __t;
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000277 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000278 {
279 __t = __t + *__first;
280 *__result = __t;
281 }
282 }
283 return __result;
284}
285
286template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
Mark de Weverab4eec72020-11-24 14:55:55 +0100287_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000288_OutputIterator
289partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
290 _BinaryOperation __binary_op)
291{
292 if (__first != __last)
293 {
294 typename iterator_traits<_InputIterator>::value_type __t(*__first);
295 *__result = __t;
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000296 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000297 {
298 __t = __binary_op(__t, *__first);
299 *__result = __t;
300 }
301 }
302 return __result;
303}
304
Marshall Clow0ce24992017-06-10 02:22:13 +0000305#if _LIBCPP_STD_VER > 14
306template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
Mark de Weverab4eec72020-11-24 14:55:55 +0100307_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0ce24992017-06-10 02:22:13 +0000308_OutputIterator
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000309exclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow0ce24992017-06-10 02:22:13 +0000310 _OutputIterator __result, _Tp __init, _BinaryOp __b)
311{
312 if (__first != __last)
313 {
314 _Tp __saved = __init;
315 do
316 {
317 __init = __b(__init, *__first);
318 *__result = __saved;
319 __saved = __init;
320 ++__result;
321 } while (++__first != __last);
322 }
323 return __result;
324}
325
326template <class _InputIterator, class _OutputIterator, class _Tp>
Mark de Weverab4eec72020-11-24 14:55:55 +0100327_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0ce24992017-06-10 02:22:13 +0000328_OutputIterator
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000329exclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow0ce24992017-06-10 02:22:13 +0000330 _OutputIterator __result, _Tp __init)
331{
332 return _VSTD::exclusive_scan(__first, __last, __result, __init, _VSTD::plus<>());
333}
334
Marshall Clow4c561442017-06-23 05:12:42 +0000335template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
Mark de Weverab4eec72020-11-24 14:55:55 +0100336_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000337_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow4c561442017-06-23 05:12:42 +0000338 _OutputIterator __result, _BinaryOp __b, _Tp __init)
339{
340 for (; __first != __last; ++__first, (void) ++__result) {
341 __init = __b(__init, *__first);
342 *__result = __init;
Louis Dionne5b20ae92020-11-24 12:26:05 -0500343 }
Marshall Clow4c561442017-06-23 05:12:42 +0000344 return __result;
345}
346
347template <class _InputIterator, class _OutputIterator, class _BinaryOp>
Mark de Weverab4eec72020-11-24 14:55:55 +0100348_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000349_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow4c561442017-06-23 05:12:42 +0000350 _OutputIterator __result, _BinaryOp __b)
351{
352 if (__first != __last) {
353 typename std::iterator_traits<_InputIterator>::value_type __init = *__first;
354 *__result++ = __init;
355 if (++__first != __last)
356 return _VSTD::inclusive_scan(__first, __last, __result, __b, __init);
Louis Dionne5b20ae92020-11-24 12:26:05 -0500357 }
Marshall Clow4c561442017-06-23 05:12:42 +0000358
359 return __result;
360}
361
362template <class _InputIterator, class _OutputIterator>
Mark de Weverab4eec72020-11-24 14:55:55 +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)
366{
367 return _VSTD::inclusive_scan(__first, __last, __result, std::plus<>());
368}
369
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000370template <class _InputIterator, class _OutputIterator, class _Tp,
Marshall Clow0ce24992017-06-10 02:22:13 +0000371 class _BinaryOp, class _UnaryOp>
Mark de Weverab4eec72020-11-24 14:55:55 +0100372_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0ce24992017-06-10 02:22:13 +0000373_OutputIterator
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000374transform_exclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow0ce24992017-06-10 02:22:13 +0000375 _OutputIterator __result, _Tp __init,
376 _BinaryOp __b, _UnaryOp __u)
377{
378 if (__first != __last)
379 {
380 _Tp __saved = __init;
381 do
382 {
383 __init = __b(__init, __u(*__first));
384 *__result = __saved;
385 __saved = __init;
386 ++__result;
387 } while (++__first != __last);
388 }
389 return __result;
390}
Marshall Clow4c561442017-06-23 05:12:42 +0000391
392template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp, class _UnaryOp>
Mark de Weverab4eec72020-11-24 14:55:55 +0100393_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
394_OutputIterator
395transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow4c561442017-06-23 05:12:42 +0000396 _OutputIterator __result, _BinaryOp __b, _UnaryOp __u, _Tp __init)
397{
398 for (; __first != __last; ++__first, (void) ++__result) {
399 __init = __b(__init, __u(*__first));
400 *__result = __init;
401 }
402
403 return __result;
404}
405
406template <class _InputIterator, class _OutputIterator, class _BinaryOp, class _UnaryOp>
Mark de Weverab4eec72020-11-24 14:55:55 +0100407_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
408_OutputIterator
409transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
Marshall Clow4c561442017-06-23 05:12:42 +0000410 _OutputIterator __result, _BinaryOp __b, _UnaryOp __u)
411{
412 if (__first != __last) {
413 typename std::iterator_traits<_InputIterator>::value_type __init = __u(*__first);
414 *__result++ = __init;
415 if (++__first != __last)
416 return _VSTD::transform_inclusive_scan(__first, __last, __result, __b, __u, __init);
Louis Dionne5b20ae92020-11-24 12:26:05 -0500417 }
Billy Robert O'Neal III876ecf92018-01-05 01:31:55 +0000418
Marshall Clow4c561442017-06-23 05:12:42 +0000419 return __result;
420}
Marshall Clow0ce24992017-06-10 02:22:13 +0000421#endif
422
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423template <class _InputIterator, class _OutputIterator>
Mark de Weverab4eec72020-11-24 14:55:55 +0100424_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000425_OutputIterator
426adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
427{
428 if (__first != __last)
429 {
430 typename iterator_traits<_InputIterator>::value_type __t1(*__first);
431 *__result = __t1;
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000432 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000433 {
434 typename iterator_traits<_InputIterator>::value_type __t2(*__first);
435 *__result = __t2 - __t1;
Howard Hinnant528ceb72013-08-22 18:02:34 +0000436 __t1 = _VSTD::move(__t2);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000437 }
438 }
439 return __result;
440}
441
442template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
Mark de Weverab4eec72020-11-24 14:55:55 +0100443_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000444_OutputIterator
445adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
446 _BinaryOperation __binary_op)
447{
448 if (__first != __last)
449 {
450 typename iterator_traits<_InputIterator>::value_type __t1(*__first);
451 *__result = __t1;
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000452 for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000453 {
454 typename iterator_traits<_InputIterator>::value_type __t2(*__first);
455 *__result = __binary_op(__t2, __t1);
Howard Hinnant528ceb72013-08-22 18:02:34 +0000456 __t1 = _VSTD::move(__t2);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000457 }
458 }
459 return __result;
460}
461
Howard Hinnantf45797f2010-05-26 18:53:44 +0000462template <class _ForwardIterator, class _Tp>
Mark de Weverab4eec72020-11-24 14:55:55 +0100463_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantf45797f2010-05-26 18:53:44 +0000464void
Howard Hinnantbf074022011-10-22 20:59:45 +0000465iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_)
Howard Hinnantf45797f2010-05-26 18:53:44 +0000466{
Eric Fiseliera09a3b42014-10-27 19:28:20 +0000467 for (; __first != __last; ++__first, (void) ++__value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000468 *__first = __value_;
Howard Hinnantf45797f2010-05-26 18:53:44 +0000469}
470
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000471
472#if _LIBCPP_STD_VER > 14
Marshall Clowf41c2422019-06-18 18:13:54 +0000473template <typename _Result, typename _Source, bool _IsSigned = is_signed<_Source>::value> struct __ct_abs;
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000474
Marshall Clowf9316722017-02-10 20:49:08 +0000475template <typename _Result, typename _Source>
Marshall Clowf41c2422019-06-18 18:13:54 +0000476struct __ct_abs<_Result, _Source, true> {
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000477 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Marshall Clowf9316722017-02-10 20:49:08 +0000478 _Result operator()(_Source __t) const noexcept
479 {
Louis Dionne5b20ae92020-11-24 12:26:05 -0500480 if (__t >= 0) return __t;
481 if (__t == numeric_limits<_Source>::min()) return -static_cast<_Result>(__t);
482 return -__t;
Marshall Clowf9316722017-02-10 20:49:08 +0000483 }
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000484};
485
Marshall Clowf9316722017-02-10 20:49:08 +0000486template <typename _Result, typename _Source>
Marshall Clowf41c2422019-06-18 18:13:54 +0000487struct __ct_abs<_Result, _Source, false> {
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000488 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Marshall Clowf9316722017-02-10 20:49:08 +0000489 _Result operator()(_Source __t) const noexcept { return __t; }
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000490};
491
492
493template<class _Tp>
Eric Fiseliered86f1f2017-05-09 00:00:00 +0000494_LIBCPP_CONSTEXPR _LIBCPP_HIDDEN
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000495_Tp __gcd(_Tp __m, _Tp __n)
496{
Marshall Clowf9316722017-02-10 20:49:08 +0000497 static_assert((!is_signed<_Tp>::value), "");
Eric Fiseliered86f1f2017-05-09 00:00:00 +0000498 return __n == 0 ? __m : _VSTD::__gcd<_Tp>(__n, __m % __n);
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000499}
500
501
502template<class _Tp, class _Up>
503_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
504common_type_t<_Tp,_Up>
505gcd(_Tp __m, _Up __n)
506{
507 static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to gcd must be integer types");
Marshall Clow78dbe462016-11-14 18:22:19 +0000508 static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to gcd cannot be bool" );
509 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 +0000510 using _Rp = common_type_t<_Tp,_Up>;
511 using _Wp = make_unsigned_t<_Rp>;
Eric Fiseliered86f1f2017-05-09 00:00:00 +0000512 return static_cast<_Rp>(_VSTD::__gcd(
Marshall Clowf41c2422019-06-18 18:13:54 +0000513 static_cast<_Wp>(__ct_abs<_Rp, _Tp>()(__m)),
514 static_cast<_Wp>(__ct_abs<_Rp, _Up>()(__n))));
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000515}
516
517template<class _Tp, class _Up>
518_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
519common_type_t<_Tp,_Up>
520lcm(_Tp __m, _Up __n)
521{
522 static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to lcm must be integer types");
Marshall Clow78dbe462016-11-14 18:22:19 +0000523 static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to lcm cannot be bool" );
524 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 +0000525 if (__m == 0 || __n == 0)
526 return 0;
527
528 using _Rp = common_type_t<_Tp,_Up>;
Marshall Clowf41c2422019-06-18 18:13:54 +0000529 _Rp __val1 = __ct_abs<_Rp, _Tp>()(__m) / _VSTD::gcd(__m, __n);
530 _Rp __val2 = __ct_abs<_Rp, _Up>()(__n);
Marshall Clowb8bfc2c2016-07-26 14:29:45 +0000531 _LIBCPP_ASSERT((numeric_limits<_Rp>::max() / __val1 > __val2), "Overflow in lcm");
532 return __val1 * __val2;
533}
534
535#endif /* _LIBCPP_STD_VER > 14 */
536
Marshall Clowa5f2c8a2019-03-14 16:25:55 +0000537#if _LIBCPP_STD_VER > 17
538template <class _Tp>
539_LIBCPP_INLINE_VISIBILITY constexpr
Marshall Clowa6091132019-05-29 15:17:55 +0000540enable_if_t<is_integral_v<_Tp> && !is_same_v<bool, _Tp> && !is_null_pointer_v<_Tp>, _Tp>
Marshall Clowa5f2c8a2019-03-14 16:25:55 +0000541midpoint(_Tp __a, _Tp __b) noexcept
542_LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
543{
544 using _Up = std::make_unsigned_t<_Tp>;
Jorg Browna1d2dfe2019-11-04 19:00:23 -0800545 constexpr _Up __bitshift = std::numeric_limits<_Up>::digits - 1;
Marshall Clowa5f2c8a2019-03-14 16:25:55 +0000546
Jorg Browna1d2dfe2019-11-04 19:00:23 -0800547 _Up __diff = _Up(__b) - _Up(__a);
548 _Up __sign_bit = __b < __a;
549
550 _Up __half_diff = (__diff / 2) + (__sign_bit << __bitshift) + (__sign_bit & __diff);
551
552 return __a + __half_diff;
Marshall Clowa5f2c8a2019-03-14 16:25:55 +0000553}
554
555
556template <class _TPtr>
557_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne173f29e2019-05-29 16:01:36 +0000558enable_if_t<is_pointer_v<_TPtr>
559 && is_object_v<remove_pointer_t<_TPtr>>
560 && ! is_void_v<remove_pointer_t<_TPtr>>
Marshall Clowa6091132019-05-29 15:17:55 +0000561 && (sizeof(remove_pointer_t<_TPtr>) > 0), _TPtr>
Marshall Clowa5f2c8a2019-03-14 16:25:55 +0000562midpoint(_TPtr __a, _TPtr __b) noexcept
563{
564 return __a + _VSTD::midpoint(ptrdiff_t(0), __b - __a);
565}
Marshall Clow174901e2019-04-25 12:11:43 +0000566
567
568template <typename _Tp>
Marshall Clow2dc0ef12019-05-07 16:07:24 +0000569constexpr int __sign(_Tp __val) {
Marshall Clow174901e2019-04-25 12:11:43 +0000570 return (_Tp(0) < __val) - (__val < _Tp(0));
571}
572
Marshall Clowf41c2422019-06-18 18:13:54 +0000573template <typename _Fp>
574constexpr _Fp __fp_abs(_Fp __f) { return __f >= 0 ? __f : -__f; }
575
Marshall Clow174901e2019-04-25 12:11:43 +0000576template <class _Fp>
577_LIBCPP_INLINE_VISIBILITY constexpr
578enable_if_t<is_floating_point_v<_Fp>, _Fp>
579midpoint(_Fp __a, _Fp __b) noexcept
580{
Marshall Clowf41c2422019-06-18 18:13:54 +0000581 constexpr _Fp __lo = numeric_limits<_Fp>::min()*2;
582 constexpr _Fp __hi = numeric_limits<_Fp>::max()/2;
583 return __fp_abs(__a) <= __hi && __fp_abs(__b) <= __hi ? // typical case: overflow is impossible
584 (__a + __b)/2 : // always correctly rounded
585 __fp_abs(__a) < __lo ? __a + __b/2 : // not safe to halve a
Ruslan Baratov1adc3502019-12-21 01:22:12 -0800586 __fp_abs(__b) < __lo ? __a/2 + __b : // not safe to halve b
Marshall Clowf41c2422019-06-18 18:13:54 +0000587 __a/2 + __b/2; // otherwise correctly rounded
Marshall Clow174901e2019-04-25 12:11:43 +0000588}
Marshall Clowf41c2422019-06-18 18:13:54 +0000589
Marshall Clow174901e2019-04-25 12:11:43 +0000590#endif // _LIBCPP_STD_VER > 17
Marshall Clowa5f2c8a2019-03-14 16:25:55 +0000591
Howard Hinnantc51e1022010-05-11 19:42:16 +0000592_LIBCPP_END_NAMESPACE_STD
593
Eric Fiselierf4433a32017-05-31 22:07:49 +0000594_LIBCPP_POP_MACROS
595
Louis Dionne59d0b3c2019-08-05 18:29:14 +0000596#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionned53d8c02019-08-06 21:11:24 +0000597# include <__pstl_numeric>
Louis Dionne59d0b3c2019-08-05 18:29:14 +0000598#endif
599
Howard Hinnantc51e1022010-05-11 19:42:16 +0000600#endif // _LIBCPP_NUMERIC