55 #ifndef _GLIBCXX_NUMERIC
56 #define _GLIBCXX_NUMERIC 1
58 #pragma GCC system_header
64 #ifdef _GLIBCXX_PARALLEL
76 #if __cplusplus >= 201402L
79 namespace std _GLIBCXX_VISIBILITY(default)
81 _GLIBCXX_BEGIN_NAMESPACE_VERSION
86 template<
typename _Tp>
88 enable_if_t<__and_<is_integral<_Tp>, is_signed<_Tp>>::value, _Tp>
89 __abs_integral(_Tp __val)
90 {
return __val < 0 ? -__val : __val; }
92 template<
typename _Tp>
94 enable_if_t<__and_<is_integral<_Tp>, is_unsigned<_Tp>>::value, _Tp>
95 __abs_integral(_Tp __val)
98 void __abs_integral(
bool) =
delete;
100 template<
typename _Mn,
typename _Nn>
101 constexpr common_type_t<_Mn, _Nn>
102 __gcd(_Mn __m, _Nn __n)
104 return __m == 0 ? __detail::__abs_integral(__n)
105 : __n == 0 ? __detail::__abs_integral(__m)
106 : __detail::__gcd(__n, __m % __n);
110 template<
typename _Mn,
typename _Nn>
111 constexpr common_type_t<_Mn, _Nn>
114 return (__m != 0 && __n != 0)
115 ? (__detail::__abs_integral(__m) / __detail::__gcd(__m, __n))
116 * __detail::__abs_integral(__n)
121 #if __cplusplus >= 201703L
123 #define __cpp_lib_gcd_lcm 201606
125 #define __cpp_lib_gcd 201606
126 #define __cpp_lib_lcm 201606
129 template<
typename _Mn,
typename _Nn>
130 constexpr common_type_t<_Mn, _Nn>
131 gcd(_Mn __m, _Nn __n)
133 static_assert(is_integral_v<_Mn>,
"gcd arguments are integers");
134 static_assert(is_integral_v<_Nn>,
"gcd arguments are integers");
135 static_assert(!is_same_v<remove_cv_t<_Mn>,
bool>,
136 "gcd arguments are not bools");
137 static_assert(!is_same_v<remove_cv_t<_Nn>,
bool>,
138 "gcd arguments are not bools");
139 return __detail::__gcd(__m, __n);
143 template<
typename _Mn,
typename _Nn>
144 constexpr common_type_t<_Mn, _Nn>
145 lcm(_Mn __m, _Nn __n)
147 static_assert(is_integral_v<_Mn>,
"lcm arguments are integers");
148 static_assert(is_integral_v<_Nn>,
"lcm arguments are integers");
149 static_assert(!is_same_v<remove_cv_t<_Mn>,
bool>,
150 "lcm arguments are not bools");
151 static_assert(!is_same_v<remove_cv_t<_Nn>,
bool>,
152 "lcm arguments are not bools");
158 _GLIBCXX_END_NAMESPACE_VERSION
163 #if __cplusplus > 201703L
166 namespace std _GLIBCXX_VISIBILITY(default)
168 _GLIBCXX_BEGIN_NAMESPACE_VERSION
170 # define __cpp_lib_interpolate 201902L
172 template<
typename _Tp>
174 enable_if_t<__and_v<is_arithmetic<_Tp>, is_same<remove_cv_t<_Tp>, _Tp>,
175 __not_<is_same<_Tp, bool>>>,
179 if constexpr (is_integral_v<_Tp>)
181 using _Up = make_unsigned_t<_Tp>;
192 return __a + __k * _Tp(_Up(__M - __m) / 2);
198 const _Tp __abs_a = __a < 0 ? -__a : __a;
199 const _Tp __abs_b = __b < 0 ? -__b : __b;
200 if (__abs_a <= __hi && __abs_b <= __hi) [[likely]]
201 return (__a + __b) / 2;
206 return __a/2 + __b/2;
210 template<
typename _Tp>
212 enable_if_t<__and_v<is_object<_Tp>, bool_constant<sizeof(_Tp) != 0>>, _Tp*>
213 midpoint(_Tp* __a, _Tp* __b)
noexcept
215 return __a + (__b - __a) / 2;
217 _GLIBCXX_END_NAMESPACE_VERSION
222 #if __cplusplus > 201402L
225 namespace std _GLIBCXX_VISIBILITY(default)
227 _GLIBCXX_BEGIN_NAMESPACE_VERSION
233 template<
typename _It,
typename _Traits = iterator_traits<_It>,
234 typename _Cat =
typename _Traits::iterator_category>
235 using __is_random_access_iter
236 = is_base_of<random_access_iterator_tag, _Cat>;
258 template<
typename _InputIterator,
typename _Tp,
typename _BinaryOperation>
260 reduce(_InputIterator __first, _InputIterator __last, _Tp __init,
261 _BinaryOperation __binary_op)
263 using value_type =
typename iterator_traits<_InputIterator>::value_type;
264 static_assert(is_invocable_r_v<_Tp, _BinaryOperation&, _Tp&, _Tp&>);
265 static_assert(is_convertible_v<value_type, _Tp>);
266 if constexpr (__is_random_access_iter<_InputIterator>::value)
268 while ((__last - __first) >= 4)
270 _Tp __v1 = __binary_op(__first[0], __first[1]);
271 _Tp __v2 = __binary_op(__first[2], __first[3]);
272 _Tp __v3 = __binary_op(__v1, __v2);
273 __init = __binary_op(__init, __v3);
277 for (; __first != __last; ++__first)
278 __init = __binary_op(__init, *__first);
293 template<
typename _InputIterator,
typename _Tp>
295 reduce(_InputIterator __first, _InputIterator __last, _Tp __init)
296 {
return std::reduce(__first, __last, std::move(__init), plus<>()); }
309 template<
typename _InputIterator>
310 inline typename iterator_traits<_InputIterator>::value_type
311 reduce(_InputIterator __first, _InputIterator __last)
313 using value_type =
typename iterator_traits<_InputIterator>::value_type;
314 return std::reduce(__first, __last, value_type{}, plus<>());
335 template<
typename _InputIterator1,
typename _InputIterator2,
typename _Tp,
336 typename _BinaryOperation1,
typename _BinaryOperation2>
338 transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
339 _InputIterator2 __first2, _Tp __init,
340 _BinaryOperation1 __binary_op1,
341 _BinaryOperation2 __binary_op2)
343 if constexpr (__and_v<__is_random_access_iter<_InputIterator1>,
344 __is_random_access_iter<_InputIterator2>>)
346 while ((__last1 - __first1) >= 4)
348 _Tp __v1 = __binary_op1(__binary_op2(__first1[0], __first2[0]),
349 __binary_op2(__first1[1], __first2[1]));
350 _Tp __v2 = __binary_op1(__binary_op2(__first1[2], __first2[2]),
351 __binary_op2(__first1[3], __first2[3]));
352 _Tp __v3 = __binary_op1(__v1, __v2);
353 __init = __binary_op1(__init, __v3);
358 for (; __first1 != __last1; ++__first1, (void) ++__first2)
359 __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
378 template<
typename _InputIterator1,
typename _InputIterator2,
typename _Tp>
380 transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1,
381 _InputIterator2 __first2, _Tp __init)
383 return std::transform_reduce(__first1, __last1, __first2,
385 plus<>(), multiplies<>());
402 template<
typename _InputIterator,
typename _Tp,
403 typename _BinaryOperation,
typename _UnaryOperation>
405 transform_reduce(_InputIterator __first, _InputIterator __last, _Tp __init,
406 _BinaryOperation __binary_op, _UnaryOperation __unary_op)
408 if constexpr (__is_random_access_iter<_InputIterator>::value)
410 while ((__last - __first) >= 4)
412 _Tp __v1 = __binary_op(__unary_op(__first[0]),
413 __unary_op(__first[1]));
414 _Tp __v2 = __binary_op(__unary_op(__first[2]),
415 __unary_op(__first[3]));
416 _Tp __v3 = __binary_op(__v1, __v2);
417 __init = __binary_op(__init, __v3);
421 for (; __first != __last; ++__first)
422 __init = __binary_op(__init, __unary_op(*__first));
444 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp,
445 typename _BinaryOperation>
447 exclusive_scan(_InputIterator __first, _InputIterator __last,
448 _OutputIterator __result, _Tp __init,
449 _BinaryOperation __binary_op)
451 while (__first != __last)
454 __init = __binary_op(__init, *__first);
456 *__result++ = std::move(__v);
478 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp>
479 inline _OutputIterator
480 exclusive_scan(_InputIterator __first, _InputIterator __last,
481 _OutputIterator __result, _Tp __init)
483 return std::exclusive_scan(__first, __last, __result, std::move(__init),
505 template<
typename _InputIterator,
typename _OutputIterator,
506 typename _BinaryOperation,
typename _Tp>
508 inclusive_scan(_InputIterator __first, _InputIterator __last,
509 _OutputIterator __result, _BinaryOperation __binary_op,
512 for (; __first != __last; ++__first)
513 *__result++ = __init = __binary_op(__init, *__first);
533 template<
typename _InputIterator,
typename _OutputIterator,
534 typename _BinaryOperation>
536 inclusive_scan(_InputIterator __first, _InputIterator __last,
537 _OutputIterator __result, _BinaryOperation __binary_op)
539 if (__first != __last)
541 auto __init = *__first;
542 *__result++ = __init;
544 if (__first != __last)
545 __result = std::inclusive_scan(__first, __last, __result,
546 __binary_op, std::move(__init));
566 template<
typename _InputIterator,
typename _OutputIterator>
567 inline _OutputIterator
568 inclusive_scan(_InputIterator __first, _InputIterator __last,
569 _OutputIterator __result)
570 {
return std::inclusive_scan(__first, __last, __result, plus<>()); }
592 template<
typename _InputIterator,
typename _OutputIterator,
typename _Tp,
593 typename _BinaryOperation,
typename _UnaryOperation>
595 transform_exclusive_scan(_InputIterator __first, _InputIterator __last,
596 _OutputIterator __result, _Tp __init,
597 _BinaryOperation __binary_op,
598 _UnaryOperation __unary_op)
600 while (__first != __last)
603 __init = __binary_op(__init, __unary_op(*__first));
605 *__result++ = std::move(__v);
630 template<
typename _InputIterator,
typename _OutputIterator,
631 typename _BinaryOperation,
typename _UnaryOperation,
typename _Tp>
633 transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
634 _OutputIterator __result,
635 _BinaryOperation __binary_op,
636 _UnaryOperation __unary_op,
639 for (; __first != __last; ++__first)
640 *__result++ = __init = __binary_op(__init, __unary_op(*__first));
663 template<
typename _InputIterator,
typename _OutputIterator,
664 typename _BinaryOperation,
typename _UnaryOperation>
666 transform_inclusive_scan(_InputIterator __first, _InputIterator __last,
667 _OutputIterator __result,
668 _BinaryOperation __binary_op,
669 _UnaryOperation __unary_op)
671 if (__first != __last)
673 auto __init = __unary_op(*__first);
674 *__result++ = __init;
676 if (__first != __last)
677 __result = std::transform_inclusive_scan(__first, __last, __result,
678 __binary_op, __unary_op,
686 _GLIBCXX_END_NAMESPACE_VERSION
690 # if __PSTL_EXECUTION_POLICIES_DEFINED
692 # include <pstl/glue_numeric_impl.h>
695 # include <pstl/glue_numeric_defs.h>
696 # define __PSTL_NUMERIC_FORWARD_DECLARED 1
700 # define __cpp_lib_parallel_algorithm 201603L
Parallel STL function calls corresponding to stl_numeric.h. The functions defined here mainly do case...
static constexpr _Tp max() noexcept
static constexpr _Tp min() noexcept
enable_if< ::__array_traits< _Tp, _Nm >::_Is_swappable::value >::type noexcept(noexcept(__one.swap(__two)))
swap
constexpr common_type_t< _Mn, _Nn > __lcm(_Mn __m, _Nn __n)
Least common multiple.