55 #ifndef _GLIBCXX_NUMERIC
56 #define _GLIBCXX_NUMERIC 1
58 #pragma GCC system_header
65 #ifdef _GLIBCXX_PARALLEL
77 #if __cplusplus >= 201402L
80 namespace std _GLIBCXX_VISIBILITY(default)
82 _GLIBCXX_BEGIN_NAMESPACE_VERSION
88 template<
typename _Up,
typename _Tp>
92 static_assert(is_unsigned<_Up>::value,
"result type must be unsigned");
93 static_assert(
sizeof(_Up) >=
sizeof(_Tp),
94 "result type must be at least as wide as the input type");
95 return __val < 0 ? -(_Up)__val : (_Up)__val;
98 template<
typename _Up>
void __absu(
bool) =
delete;
101 template<
typename _Tp>
103 __gcd(_Tp __m, _Tp __n)
105 static_assert(is_unsigned<_Tp>::value,
"type must be unsigned");
106 return __m == 0 ? __n
108 : __detail::__gcd(__n, _Tp(__m % __n));
112 template<
typename _Tp>
114 __lcm(_Tp __m, _Tp __n)
116 return (__m != 0 && __n != 0)
117 ? (__m / __detail::__gcd(__m, __n)) * __n
122 #if __cplusplus >= 201703L
124 #define __cpp_lib_gcd_lcm 201606
126 #define __cpp_lib_gcd 201606
127 #define __cpp_lib_lcm 201606
130 template<
typename _Mn,
typename _Nn>
131 constexpr common_type_t<_Mn, _Nn>
132 gcd(_Mn __m, _Nn __n) noexcept
134 static_assert(is_integral_v<_Mn>,
"std::gcd arguments must be integers");
135 static_assert(is_integral_v<_Nn>,
"std::gcd arguments must be integers");
136 static_assert(_Mn(2) != _Mn(1),
"std::gcd arguments must not be bool");
137 static_assert(_Nn(2) != _Nn(1),
"std::gcd arguments must not be bool");
138 using _Up = make_unsigned_t<common_type_t<_Mn, _Nn>>;
139 return __detail::__gcd(__detail::__absu<_Up>(__m),
140 __detail::__absu<_Up>(__n));
144 template<
typename _Mn,
typename _Nn>
145 constexpr common_type_t<_Mn, _Nn>
146 lcm(_Mn __m, _Nn __n) noexcept
148 static_assert(is_integral_v<_Mn>,
"std::lcm arguments must be integers");
149 static_assert(is_integral_v<_Nn>,
"std::lcm arguments must be integers");
150 static_assert(_Mn(2) == 2,
"std::lcm arguments must not be bool");
151 static_assert(_Nn(2) == 2,
"std::lcm arguments must not be bool");
152 using _Up = make_unsigned_t<common_type_t<_Mn, _Nn>>;
153 return __detail::__lcm(__detail::__absu<_Up>(__m),
154 __detail::__absu<_Up>(__n));
159 _GLIBCXX_END_NAMESPACE_VERSION
164 #if __cplusplus > 201703L
167 namespace std _GLIBCXX_VISIBILITY(default)
169 _GLIBCXX_BEGIN_NAMESPACE_VERSION
171 # define __cpp_lib_interpolate 201902L
173 template<
typename _Tp>
175 enable_if_t<__and_v<is_arithmetic<_Tp>, is_same<remove_cv_t<_Tp>, _Tp>,
176 __not_<is_same<_Tp, bool>>>,
178 midpoint(_Tp __a, _Tp __b) noexcept
180 if constexpr (is_integral_v<_Tp>)
182 using _Up = make_unsigned_t<_Tp>;
193 return __a + __k * _Tp(_Up(__M - __m) / 2);
199 const _Tp __abs_a = __a < 0 ? -__a : __a;
200 const _Tp __abs_b = __b < 0 ? -__b : __b;
201 if (__abs_a <= __hi && __abs_b <= __hi) [[likely]]
202 return (__a + __b) / 2;
207 return __a/2 + __b/2;
211 template<
typename _Tp>
212 constexpr enable_if_t<is_object_v<_Tp>, _Tp*>
213 midpoint(_Tp* __a, _Tp* __b) noexcept
215 static_assert(
sizeof(_Tp) != 0,
"type must be complete" );
216 return __a + (__b - __a) / 2;
218 _GLIBCXX_END_NAMESPACE_VERSION
223 #if __cplusplus > 201402L
226 namespace std _GLIBCXX_VISIBILITY(default)
228 _GLIBCXX_BEGIN_NAMESPACE_VERSION
230 #if __cplusplus > 201703L
231 #define __cpp_lib_constexpr_numeric 201911L
256 template<
typename _InputIterator,
typename _Tp,
typename _BinaryOperation>
259 reduce(_InputIterator __first, _InputIterator __last, _Tp __init,
260 _BinaryOperation __binary_op)
262 using __ref =
typename iterator_traits<_InputIterator>::reference;
263 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, _Tp&, __ref>);
264 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, __ref, _Tp&>);
265 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, _Tp&, _Tp&>);
266 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, __ref, __ref>);
267 if constexpr (__is_random_access_iter<_InputIterator>::value)
269 while ((__last - __first) >= 4)
271 _Tp __v1 = __binary_op(__first[0], __first[1]);
272 _Tp __v2 = __binary_op(__first[2], __first[3]);
273 _Tp __v3 = __binary_op(__v1, __v2);
274 __init = __binary_op(__init, __v3);
278 for (; __first != __last; ++__first)
279 __init = __binary_op(__init, *__first);
294 template<
typename _InputIterator,
typename _Tp>
297 reduce(_InputIterator __first, _InputIterator __last, _Tp __init)
298 {
return std::reduce(__first, __last,
std::move(__init), plus<>()); }
311 template<
typename _InputIterator>
313 inline typename iterator_traits<_InputIterator>::value_type
314 reduce(_InputIterator __first, _InputIterator __last)
316 using value_type =
typename iterator_traits<_InputIterator>::value_type;
317 return std::reduce(__first, __last, value_type{}, plus<>());
338 template<
typename _InputIterator1,
typename _InputIterator2,
typename _Tp,
339 typename _BinaryOperation1,
typename _BinaryOperation2>
342 transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
343 _InputIterator2 __first2, _Tp __init,
344 _BinaryOperation1 __binary_op1,
345 _BinaryOperation2 __binary_op2)
347 if constexpr (__and_v<__is_random_access_iter<_InputIterator1>,
348 __is_random_access_iter<_InputIterator2>>)
350 while ((__last1 - __first1) >= 4)
352 _Tp __v1 = __binary_op1(__binary_op2(__first1[0], __first2[0]),
353 __binary_op2(__first1[1], __first2[1]));
354 _Tp __v2 = __binary_op1(__binary_op2(__first1[2], __first2[2]),
355 __binary_op2(__first1[3], __first2[3]));
356 _Tp __v3 = __binary_op1(__v1, __v2);
357 __init = __binary_op1(__init, __v3);
362 for (; __first1 != __last1; ++__first1, (void) ++__first2)
363 __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
382 template<
typename _InputIterator1,
typename _InputIterator2,
typename _Tp>
385 transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
386 _InputIterator2 __first2, _Tp __init)
388 return std::transform_reduce(__first1, __last1, __first2,
390 plus<>(), multiplies<>());
407 template<
typename _InputIterator,
typename _Tp,
408 typename _BinaryOperation,
typename _UnaryOperation>
411 transform_reduce(_InputIterator __first, _InputIterator __last, _Tp __init,
412 _BinaryOperation __binary_op, _UnaryOperation __unary_op)
414 if constexpr (__is_random_access_iter<_InputIterator>::value)
416 while ((__last - __first) >= 4)
418 _Tp __v1 = __binary_op(__unary_op(__first[0]),
419 __unary_op(__first[1]));
420 _Tp __v2 = __binary_op(__unary_op(__first[2]),
421 __unary_op(__first[3]));
422 _Tp __v3 = __binary_op(__v1, __v2);
423 __init = __binary_op(__init, __v3);
427 for (; __first != __last; ++__first)
428 __init = __binary_op(__init, __unary_op(*__first));
450 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp,
451 typename _BinaryOperation>
454 exclusive_scan(_InputIterator __first, _InputIterator __last,
455 _OutputIterator __result, _Tp __init,
456 _BinaryOperation __binary_op)
458 while (__first != __last)
461 __init = __binary_op(__init, *__first);
485 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp>
487 inline _OutputIterator
488 exclusive_scan(_InputIterator __first, _InputIterator __last,
489 _OutputIterator __result, _Tp __init)
491 return std::exclusive_scan(__first, __last, __result,
std::move(__init),
513 template<
typename _InputIterator,
typename _OutputIterator,
514 typename _BinaryOperation,
typename _Tp>
517 inclusive_scan(_InputIterator __first, _InputIterator __last,
518 _OutputIterator __result, _BinaryOperation __binary_op,
521 for (; __first != __last; ++__first)
522 *__result++ = __init = __binary_op(__init, *__first);
542 template<
typename _InputIterator,
typename _OutputIterator,
543 typename _BinaryOperation>
546 inclusive_scan(_InputIterator __first, _InputIterator __last,
547 _OutputIterator __result, _BinaryOperation __binary_op)
549 if (__first != __last)
551 auto __init = *__first;
552 *__result++ = __init;
554 if (__first != __last)
555 __result = std::inclusive_scan(__first, __last, __result,
576 template<
typename _InputIterator,
typename _OutputIterator>
578 inline _OutputIterator
579 inclusive_scan(_InputIterator __first, _InputIterator __last,
580 _OutputIterator __result)
581 {
return std::inclusive_scan(__first, __last, __result, plus<>()); }
603 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp,
604 typename _BinaryOperation,
typename _UnaryOperation>
607 transform_exclusive_scan(_InputIterator __first, _InputIterator __last,
608 _OutputIterator __result, _Tp __init,
609 _BinaryOperation __binary_op,
610 _UnaryOperation __unary_op)
612 while (__first != __last)
615 __init = __binary_op(__init, __unary_op(*__first));
642 template<
typename _InputIterator,
typename _OutputIterator,
643 typename _BinaryOperation,
typename _UnaryOperation,
typename _Tp>
646 transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
647 _OutputIterator __result,
648 _BinaryOperation __binary_op,
649 _UnaryOperation __unary_op,
652 for (; __first != __last; ++__first)
653 *__result++ = __init = __binary_op(__init, __unary_op(*__first));
676 template<
typename _InputIterator,
typename _OutputIterator,
677 typename _BinaryOperation,
typename _UnaryOperation>
680 transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
681 _OutputIterator __result,
682 _BinaryOperation __binary_op,
683 _UnaryOperation __unary_op)
685 if (__first != __last)
687 auto __init = __unary_op(*__first);
688 *__result++ = __init;
690 if (__first != __last)
691 __result = std::transform_inclusive_scan(__first, __last, __result,
692 __binary_op, __unary_op,
700 _GLIBCXX_END_NAMESPACE_VERSION
704 # if _PSTL_EXECUTION_POLICIES_DEFINED
706 # include <pstl/glue_numeric_impl.h>
709 # include <pstl/glue_numeric_defs.h>
710 # define _PSTL_NUMERIC_FORWARD_DECLARED 1
714 # define __cpp_lib_parallel_algorithm 201603L
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
ISO C++ entities toplevel namespace is std.
constexpr common_type_t< _Mn, _Nn > lcm(_Mn __m, _Nn __n)
Least common multiple.
constexpr common_type_t< _Mn, _Nn > gcd(_Mn __m, _Nn __n) noexcept
Greatest common divisor.
static constexpr _Tp max() noexcept
static constexpr _Tp min() noexcept
Parallel STL function calls corresponding to stl_numeric.h. The functions defined here mainly do case...