13e519524SHoward Hinnant// -*- C++ -*- 23e519524SHoward Hinnant//===---------------------------- numeric ---------------------------------===// 33e519524SHoward Hinnant// 45b08a8a4SHoward Hinnant// The LLVM Compiler Infrastructure 53e519524SHoward Hinnant// 6412dbebeSHoward Hinnant// This file is dual licensed under the MIT and the University of Illinois Open 7412dbebeSHoward Hinnant// Source Licenses. See LICENSE.TXT for details. 83e519524SHoward Hinnant// 93e519524SHoward Hinnant//===----------------------------------------------------------------------===// 103e519524SHoward Hinnant 113e519524SHoward Hinnant#ifndef _LIBCPP_NUMERIC 123e519524SHoward Hinnant#define _LIBCPP_NUMERIC 133e519524SHoward Hinnant 143e519524SHoward Hinnant/* 153e519524SHoward Hinnant numeric synopsis 163e519524SHoward Hinnant 173e519524SHoward Hinnantnamespace std 183e519524SHoward Hinnant{ 193e519524SHoward Hinnant 203e519524SHoward Hinnanttemplate <class InputIterator, class T> 213e519524SHoward Hinnant T 223e519524SHoward Hinnant accumulate(InputIterator first, InputIterator last, T init); 233e519524SHoward Hinnant 243e519524SHoward Hinnanttemplate <class InputIterator, class T, class BinaryOperation> 253e519524SHoward Hinnant T 263e519524SHoward Hinnant accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op); 273e519524SHoward Hinnant 28*f4ea23d3SMarshall Clowtemplate<class InputIterator> 29*f4ea23d3SMarshall Clow typename iterator_traits<InputIterator>::value_type 30*f4ea23d3SMarshall Clow reduce(InputIterator first, InputIterator last); // C++17 31*f4ea23d3SMarshall Clow 32*f4ea23d3SMarshall Clowtemplate<class InputIterator, class T> 33*f4ea23d3SMarshall Clow T 34*f4ea23d3SMarshall Clow reduce(InputIterator first, InputIterator last, T init); // C++17 35*f4ea23d3SMarshall Clow 36*f4ea23d3SMarshall Clowtemplate<class InputIterator, class T, class BinaryOperation> 37*f4ea23d3SMarshall Clow T 38*f4ea23d3SMarshall Clow reduce(InputIterator first, InputIterator last, T init, BinaryOperation binary_op); // C++17 39*f4ea23d3SMarshall Clow 403e519524SHoward Hinnanttemplate <class InputIterator1, class InputIterator2, class T> 413e519524SHoward Hinnant T 423e519524SHoward Hinnant inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init); 433e519524SHoward Hinnant 443e519524SHoward Hinnanttemplate <class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2> 453e519524SHoward Hinnant T 463e519524SHoward Hinnant inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, 473e519524SHoward Hinnant T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2); 483e519524SHoward Hinnant 49*f4ea23d3SMarshall Clow 50*f4ea23d3SMarshall Clowtemplate<class InputIterator1, class InputIterator2, class T> 51*f4ea23d3SMarshall Clow T 52*f4ea23d3SMarshall Clow transform_reduce(InputIterator1 first1, InputIterator1 last1, 53*f4ea23d3SMarshall Clow InputIterator2 first2, T init); // C++17 54*f4ea23d3SMarshall Clow 55*f4ea23d3SMarshall Clowtemplate<class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2> 56*f4ea23d3SMarshall Clow T 57*f4ea23d3SMarshall Clow transform_reduce(InputIterator1 first1, InputIterator1 last1, 58*f4ea23d3SMarshall Clow InputIterator2 first2, T init, 59*f4ea23d3SMarshall Clow BinaryOperation1 binary_op1, BinaryOperation2 binary_op2); // C++17 60*f4ea23d3SMarshall Clow 61*f4ea23d3SMarshall Clowtemplate<class InputIterator, class T, class BinaryOperation, class UnaryOperation> 62*f4ea23d3SMarshall Clow T 63*f4ea23d3SMarshall Clow transform_reduce(InputIterator first, InputIterator last, T init, 64*f4ea23d3SMarshall Clow BinaryOperation binary_op, UnaryOperation unary_op); // C++17 65*f4ea23d3SMarshall Clow 663e519524SHoward Hinnanttemplate <class InputIterator, class OutputIterator> 673e519524SHoward Hinnant OutputIterator 683e519524SHoward Hinnant partial_sum(InputIterator first, InputIterator last, OutputIterator result); 693e519524SHoward Hinnant 703e519524SHoward Hinnanttemplate <class InputIterator, class OutputIterator, class BinaryOperation> 713e519524SHoward Hinnant OutputIterator 723e519524SHoward Hinnant partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op); 733e519524SHoward Hinnant 74e948ba1cSMarshall Clowtemplate<class InputIterator, class OutputIterator, class T> 75e948ba1cSMarshall Clow OutputIterator 76e948ba1cSMarshall Clow exclusive_scan(InputIterator first, InputIterator last, 77e948ba1cSMarshall Clow OutputIterator result, T init); // C++17 78e948ba1cSMarshall Clow 79e948ba1cSMarshall Clowtemplate<class InputIterator, class OutputIterator, class T, class BinaryOperation> 80e948ba1cSMarshall Clow OutputIterator 81e948ba1cSMarshall Clow exclusive_scan(InputIterator first, InputIterator last, 82e948ba1cSMarshall Clow OutputIterator result, T init, BinaryOperation binary_op); // C++17 83e948ba1cSMarshall Clow 84e948ba1cSMarshall Clowtemplate<class InputIterator, class OutputIterator, class T, 85e948ba1cSMarshall Clow class BinaryOperation, class UnaryOperation> 86e948ba1cSMarshall Clow OutputIterator 87e948ba1cSMarshall Clow transform_exclusive_scan(InputIterator first, InputIterator last, 88e948ba1cSMarshall Clow OutputIterator result, T init, 89e948ba1cSMarshall Clow BinaryOperation binary_op, UnaryOperation unary_op); // C++17 90e948ba1cSMarshall Clow 913e519524SHoward Hinnanttemplate <class InputIterator, class OutputIterator> 923e519524SHoward Hinnant OutputIterator 933e519524SHoward Hinnant adjacent_difference(InputIterator first, InputIterator last, OutputIterator result); 943e519524SHoward Hinnant 953e519524SHoward Hinnanttemplate <class InputIterator, class OutputIterator, class BinaryOperation> 963e519524SHoward Hinnant OutputIterator 973e519524SHoward Hinnant adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op); 983e519524SHoward Hinnant 9940c7ef90SHoward Hinnanttemplate <class ForwardIterator, class T> 10040c7ef90SHoward Hinnant void iota(ForwardIterator first, ForwardIterator last, T value); 10140c7ef90SHoward Hinnant 10219b4035fSMarshall Clowtemplate <class M, class N> 10319b4035fSMarshall Clow constexpr common_type_t<M,N> gcd(M m, N n); // C++17 10419b4035fSMarshall Clow 10519b4035fSMarshall Clowtemplate <class M, class N> 10619b4035fSMarshall Clow constexpr common_type_t<M,N> lcm(M m, N n); // C++17 10719b4035fSMarshall Clow 1083e519524SHoward Hinnant} // std 1093e519524SHoward Hinnant 1103e519524SHoward Hinnant*/ 1113e519524SHoward Hinnant 1123e519524SHoward Hinnant#include <__config> 1133e519524SHoward Hinnant#include <iterator> 114d35528b3SEric Fiselier#include <limits> // for numeric_limits 115e948ba1cSMarshall Clow#include <functional> 1163e519524SHoward Hinnant 117073458b1SHoward Hinnant#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 1183e519524SHoward Hinnant#pragma GCC system_header 119073458b1SHoward Hinnant#endif 1203e519524SHoward Hinnant 121a016efb1SEric Fiselier_LIBCPP_PUSH_MACROS 122a016efb1SEric Fiselier#include <__undef_macros> 123a016efb1SEric Fiselier 1243e519524SHoward Hinnant_LIBCPP_BEGIN_NAMESPACE_STD 1253e519524SHoward Hinnant 1263e519524SHoward Hinnanttemplate <class _InputIterator, class _Tp> 1273e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 1283e519524SHoward Hinnant_Tp 1293e519524SHoward Hinnantaccumulate(_InputIterator __first, _InputIterator __last, _Tp __init) 1303e519524SHoward Hinnant{ 1313e519524SHoward Hinnant for (; __first != __last; ++__first) 1323e519524SHoward Hinnant __init = __init + *__first; 1333e519524SHoward Hinnant return __init; 1343e519524SHoward Hinnant} 1353e519524SHoward Hinnant 1363e519524SHoward Hinnanttemplate <class _InputIterator, class _Tp, class _BinaryOperation> 1373e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 1383e519524SHoward Hinnant_Tp 1393e519524SHoward Hinnantaccumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op) 1403e519524SHoward Hinnant{ 1413e519524SHoward Hinnant for (; __first != __last; ++__first) 1423e519524SHoward Hinnant __init = __binary_op(__init, *__first); 1433e519524SHoward Hinnant return __init; 1443e519524SHoward Hinnant} 1453e519524SHoward Hinnant 146*f4ea23d3SMarshall Clow#if _LIBCPP_STD_VER > 14 147*f4ea23d3SMarshall Clowtemplate <class _InputIterator, class _Tp, class _BinaryOp> 148*f4ea23d3SMarshall Clowinline _LIBCPP_INLINE_VISIBILITY 149*f4ea23d3SMarshall Clow_Tp 150*f4ea23d3SMarshall Clowreduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOp __b) 151*f4ea23d3SMarshall Clow{ 152*f4ea23d3SMarshall Clow for (; __first != __last; ++__first) 153*f4ea23d3SMarshall Clow __init = __b(__init, *__first); 154*f4ea23d3SMarshall Clow return __init; 155*f4ea23d3SMarshall Clow} 156*f4ea23d3SMarshall Clow 157*f4ea23d3SMarshall Clowtemplate <class _InputIterator, class _Tp> 158*f4ea23d3SMarshall Clowinline _LIBCPP_INLINE_VISIBILITY 159*f4ea23d3SMarshall Clow_Tp 160*f4ea23d3SMarshall Clowreduce(_InputIterator __first, _InputIterator __last, _Tp __init) 161*f4ea23d3SMarshall Clow{ 162*f4ea23d3SMarshall Clow return _VSTD::reduce(__first, __last, __init, _VSTD::plus<>()); 163*f4ea23d3SMarshall Clow} 164*f4ea23d3SMarshall Clow 165*f4ea23d3SMarshall Clowtemplate <class _InputIterator> 166*f4ea23d3SMarshall Clowinline _LIBCPP_INLINE_VISIBILITY 167*f4ea23d3SMarshall Clowtypename iterator_traits<_InputIterator>::value_type 168*f4ea23d3SMarshall Clowreduce(_InputIterator __first, _InputIterator __last) 169*f4ea23d3SMarshall Clow{ 170*f4ea23d3SMarshall Clow return _VSTD::reduce(__first, __last, 171*f4ea23d3SMarshall Clow typename iterator_traits<_InputIterator>::value_type{}); 172*f4ea23d3SMarshall Clow} 173*f4ea23d3SMarshall Clow#endif 174*f4ea23d3SMarshall Clow 1753e519524SHoward Hinnanttemplate <class _InputIterator1, class _InputIterator2, class _Tp> 1763e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 1773e519524SHoward Hinnant_Tp 1783e519524SHoward Hinnantinner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init) 1793e519524SHoward Hinnant{ 180910285b2SEric Fiselier for (; __first1 != __last1; ++__first1, (void) ++__first2) 1813e519524SHoward Hinnant __init = __init + *__first1 * *__first2; 1823e519524SHoward Hinnant return __init; 1833e519524SHoward Hinnant} 1843e519524SHoward Hinnant 1853e519524SHoward Hinnanttemplate <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2> 1863e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 1873e519524SHoward Hinnant_Tp 1883e519524SHoward Hinnantinner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, 1893e519524SHoward Hinnant _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2) 1903e519524SHoward Hinnant{ 191910285b2SEric Fiselier for (; __first1 != __last1; ++__first1, (void) ++__first2) 1923e519524SHoward Hinnant __init = __binary_op1(__init, __binary_op2(*__first1, *__first2)); 1933e519524SHoward Hinnant return __init; 1943e519524SHoward Hinnant} 1953e519524SHoward Hinnant 196*f4ea23d3SMarshall Clow#if _LIBCPP_STD_VER > 14 197*f4ea23d3SMarshall Clowtemplate <class _InputIterator, class _Tp, class _BinaryOp, class _UnaryOp> 198*f4ea23d3SMarshall Clowinline _LIBCPP_INLINE_VISIBILITY 199*f4ea23d3SMarshall Clow_Tp 200*f4ea23d3SMarshall Clowtransform_reduce(_InputIterator __first, _InputIterator __last, 201*f4ea23d3SMarshall Clow _Tp __init, _BinaryOp __b, _UnaryOp __u) 202*f4ea23d3SMarshall Clow{ 203*f4ea23d3SMarshall Clow for (; __first != __last; ++__first) 204*f4ea23d3SMarshall Clow __init = __b(__init, __u(*__first)); 205*f4ea23d3SMarshall Clow return __init; 206*f4ea23d3SMarshall Clow} 207*f4ea23d3SMarshall Clow 208*f4ea23d3SMarshall Clowtemplate <class _InputIterator1, class _InputIterator2, 209*f4ea23d3SMarshall Clow class _Tp, class _BinaryOp1, class _BinaryOp2> 210*f4ea23d3SMarshall Clowinline _LIBCPP_INLINE_VISIBILITY 211*f4ea23d3SMarshall Clow_Tp 212*f4ea23d3SMarshall Clowtransform_reduce(_InputIterator1 __first1, _InputIterator1 __last1, 213*f4ea23d3SMarshall Clow _InputIterator2 __first2, _Tp __init, _BinaryOp1 __b1, _BinaryOp2 __b2) 214*f4ea23d3SMarshall Clow{ 215*f4ea23d3SMarshall Clow for (; __first1 != __last1; ++__first1, (void) ++__first2) 216*f4ea23d3SMarshall Clow __init = __b1(__init, __b2(*__first1, *__first2)); 217*f4ea23d3SMarshall Clow return __init; 218*f4ea23d3SMarshall Clow} 219*f4ea23d3SMarshall Clow 220*f4ea23d3SMarshall Clowtemplate <class _InputIterator1, class _InputIterator2, class _Tp> 221*f4ea23d3SMarshall Clowinline _LIBCPP_INLINE_VISIBILITY 222*f4ea23d3SMarshall Clow_Tp 223*f4ea23d3SMarshall Clowtransform_reduce(_InputIterator1 __first1, _InputIterator1 __last1, 224*f4ea23d3SMarshall Clow _InputIterator2 __first2, _Tp __init) 225*f4ea23d3SMarshall Clow{ 226*f4ea23d3SMarshall Clow return _VSTD::transform_reduce(__first1, __last1, __first2, __init, 227*f4ea23d3SMarshall Clow _VSTD::plus<>(), _VSTD::multiplies<>()); 228*f4ea23d3SMarshall Clow} 229*f4ea23d3SMarshall Clow#endif 230*f4ea23d3SMarshall Clow 2313e519524SHoward Hinnanttemplate <class _InputIterator, class _OutputIterator> 2323e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 2333e519524SHoward Hinnant_OutputIterator 2343e519524SHoward Hinnantpartial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result) 2353e519524SHoward Hinnant{ 2363e519524SHoward Hinnant if (__first != __last) 2373e519524SHoward Hinnant { 2383e519524SHoward Hinnant typename iterator_traits<_InputIterator>::value_type __t(*__first); 2393e519524SHoward Hinnant *__result = __t; 240910285b2SEric Fiselier for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result) 2413e519524SHoward Hinnant { 2423e519524SHoward Hinnant __t = __t + *__first; 2433e519524SHoward Hinnant *__result = __t; 2443e519524SHoward Hinnant } 2453e519524SHoward Hinnant } 2463e519524SHoward Hinnant return __result; 2473e519524SHoward Hinnant} 2483e519524SHoward Hinnant 2493e519524SHoward Hinnanttemplate <class _InputIterator, class _OutputIterator, class _BinaryOperation> 2503e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 2513e519524SHoward Hinnant_OutputIterator 2523e519524SHoward Hinnantpartial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result, 2533e519524SHoward Hinnant _BinaryOperation __binary_op) 2543e519524SHoward Hinnant{ 2553e519524SHoward Hinnant if (__first != __last) 2563e519524SHoward Hinnant { 2573e519524SHoward Hinnant typename iterator_traits<_InputIterator>::value_type __t(*__first); 2583e519524SHoward Hinnant *__result = __t; 259910285b2SEric Fiselier for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result) 2603e519524SHoward Hinnant { 2613e519524SHoward Hinnant __t = __binary_op(__t, *__first); 2623e519524SHoward Hinnant *__result = __t; 2633e519524SHoward Hinnant } 2643e519524SHoward Hinnant } 2653e519524SHoward Hinnant return __result; 2663e519524SHoward Hinnant} 2673e519524SHoward Hinnant 268e948ba1cSMarshall Clow#if _LIBCPP_STD_VER > 14 269e948ba1cSMarshall Clowtemplate <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp> 270e948ba1cSMarshall Clowinline _LIBCPP_INLINE_VISIBILITY 271e948ba1cSMarshall Clow_OutputIterator 272e948ba1cSMarshall Clowexclusive_scan(_InputIterator __first, _InputIterator __last, 273e948ba1cSMarshall Clow _OutputIterator __result, _Tp __init, _BinaryOp __b) 274e948ba1cSMarshall Clow{ 275e948ba1cSMarshall Clow if (__first != __last) 276e948ba1cSMarshall Clow { 277e948ba1cSMarshall Clow _Tp __saved = __init; 278e948ba1cSMarshall Clow do 279e948ba1cSMarshall Clow { 280e948ba1cSMarshall Clow __init = __b(__init, *__first); 281e948ba1cSMarshall Clow *__result = __saved; 282e948ba1cSMarshall Clow __saved = __init; 283e948ba1cSMarshall Clow ++__result; 284e948ba1cSMarshall Clow } while (++__first != __last); 285e948ba1cSMarshall Clow } 286e948ba1cSMarshall Clow return __result; 287e948ba1cSMarshall Clow} 288e948ba1cSMarshall Clow 289e948ba1cSMarshall Clowtemplate <class _InputIterator, class _OutputIterator, class _Tp> 290e948ba1cSMarshall Clowinline _LIBCPP_INLINE_VISIBILITY 291e948ba1cSMarshall Clow_OutputIterator 292e948ba1cSMarshall Clowexclusive_scan(_InputIterator __first, _InputIterator __last, 293e948ba1cSMarshall Clow _OutputIterator __result, _Tp __init) 294e948ba1cSMarshall Clow{ 295e948ba1cSMarshall Clow return _VSTD::exclusive_scan(__first, __last, __result, __init, _VSTD::plus<>()); 296e948ba1cSMarshall Clow} 297e948ba1cSMarshall Clow 298e948ba1cSMarshall Clowtemplate <class _InputIterator, class _OutputIterator, class _Tp, 299e948ba1cSMarshall Clow class _BinaryOp, class _UnaryOp> 300e948ba1cSMarshall Clowinline _LIBCPP_INLINE_VISIBILITY 301e948ba1cSMarshall Clow_OutputIterator 302e948ba1cSMarshall Clowtransform_exclusive_scan(_InputIterator __first, _InputIterator __last, 303e948ba1cSMarshall Clow _OutputIterator __result, _Tp __init, 304e948ba1cSMarshall Clow _BinaryOp __b, _UnaryOp __u) 305e948ba1cSMarshall Clow{ 306e948ba1cSMarshall Clow if (__first != __last) 307e948ba1cSMarshall Clow { 308e948ba1cSMarshall Clow _Tp __saved = __init; 309e948ba1cSMarshall Clow do 310e948ba1cSMarshall Clow { 311e948ba1cSMarshall Clow __init = __b(__init, __u(*__first)); 312e948ba1cSMarshall Clow *__result = __saved; 313e948ba1cSMarshall Clow __saved = __init; 314e948ba1cSMarshall Clow ++__result; 315e948ba1cSMarshall Clow } while (++__first != __last); 316e948ba1cSMarshall Clow } 317e948ba1cSMarshall Clow return __result; 318e948ba1cSMarshall Clow} 319e948ba1cSMarshall Clow#endif 320e948ba1cSMarshall Clow 3213e519524SHoward Hinnanttemplate <class _InputIterator, class _OutputIterator> 3223e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 3233e519524SHoward Hinnant_OutputIterator 3243e519524SHoward Hinnantadjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result) 3253e519524SHoward Hinnant{ 3263e519524SHoward Hinnant if (__first != __last) 3273e519524SHoward Hinnant { 3283e519524SHoward Hinnant typename iterator_traits<_InputIterator>::value_type __t1(*__first); 3293e519524SHoward Hinnant *__result = __t1; 330910285b2SEric Fiselier for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result) 3313e519524SHoward Hinnant { 3323e519524SHoward Hinnant typename iterator_traits<_InputIterator>::value_type __t2(*__first); 3333e519524SHoward Hinnant *__result = __t2 - __t1; 3349ff3203fSHoward Hinnant __t1 = _VSTD::move(__t2); 3353e519524SHoward Hinnant } 3363e519524SHoward Hinnant } 3373e519524SHoward Hinnant return __result; 3383e519524SHoward Hinnant} 3393e519524SHoward Hinnant 3403e519524SHoward Hinnanttemplate <class _InputIterator, class _OutputIterator, class _BinaryOperation> 3413e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 3423e519524SHoward Hinnant_OutputIterator 3433e519524SHoward Hinnantadjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result, 3443e519524SHoward Hinnant _BinaryOperation __binary_op) 3453e519524SHoward Hinnant{ 3463e519524SHoward Hinnant if (__first != __last) 3473e519524SHoward Hinnant { 3483e519524SHoward Hinnant typename iterator_traits<_InputIterator>::value_type __t1(*__first); 3493e519524SHoward Hinnant *__result = __t1; 350910285b2SEric Fiselier for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result) 3513e519524SHoward Hinnant { 3523e519524SHoward Hinnant typename iterator_traits<_InputIterator>::value_type __t2(*__first); 3533e519524SHoward Hinnant *__result = __binary_op(__t2, __t1); 3549ff3203fSHoward Hinnant __t1 = _VSTD::move(__t2); 3553e519524SHoward Hinnant } 3563e519524SHoward Hinnant } 3573e519524SHoward Hinnant return __result; 3583e519524SHoward Hinnant} 3593e519524SHoward Hinnant 36040c7ef90SHoward Hinnanttemplate <class _ForwardIterator, class _Tp> 36140c7ef90SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 36240c7ef90SHoward Hinnantvoid 363e4383379SHoward Hinnantiota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_) 36440c7ef90SHoward Hinnant{ 365910285b2SEric Fiselier for (; __first != __last; ++__first, (void) ++__value_) 366e4383379SHoward Hinnant *__first = __value_; 36740c7ef90SHoward Hinnant} 36840c7ef90SHoward Hinnant 36919b4035fSMarshall Clow 37019b4035fSMarshall Clow#if _LIBCPP_STD_VER > 14 37158fc1b50SMarshall Clowtemplate <typename _Result, typename _Source, bool _IsSigned = is_signed<_Source>::value> struct __abs; 37219b4035fSMarshall Clow 37358fc1b50SMarshall Clowtemplate <typename _Result, typename _Source> 37458fc1b50SMarshall Clowstruct __abs<_Result, _Source, true> { 37519b4035fSMarshall Clow _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY 37658fc1b50SMarshall Clow _Result operator()(_Source __t) const noexcept 37758fc1b50SMarshall Clow { 37858fc1b50SMarshall Clow if (__t >= 0) return __t; 37958fc1b50SMarshall Clow if (__t == numeric_limits<_Source>::min()) return -static_cast<_Result>(__t); 38058fc1b50SMarshall Clow return -__t; 38158fc1b50SMarshall Clow } 38219b4035fSMarshall Clow}; 38319b4035fSMarshall Clow 38458fc1b50SMarshall Clowtemplate <typename _Result, typename _Source> 38558fc1b50SMarshall Clowstruct __abs<_Result, _Source, false> { 38619b4035fSMarshall Clow _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY 38758fc1b50SMarshall Clow _Result operator()(_Source __t) const noexcept { return __t; } 38819b4035fSMarshall Clow}; 38919b4035fSMarshall Clow 39019b4035fSMarshall Clow 39119b4035fSMarshall Clowtemplate<class _Tp> 39250d61da2SEric Fiselier_LIBCPP_CONSTEXPR _LIBCPP_HIDDEN 39319b4035fSMarshall Clow_Tp __gcd(_Tp __m, _Tp __n) 39419b4035fSMarshall Clow{ 39519b4035fSMarshall Clow static_assert((!is_signed<_Tp>::value), ""); 39650d61da2SEric Fiselier return __n == 0 ? __m : _VSTD::__gcd<_Tp>(__n, __m % __n); 39719b4035fSMarshall Clow} 39819b4035fSMarshall Clow 39919b4035fSMarshall Clow 40019b4035fSMarshall Clowtemplate<class _Tp, class _Up> 40119b4035fSMarshall Clow_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY 40219b4035fSMarshall Clowcommon_type_t<_Tp,_Up> 40319b4035fSMarshall Clowgcd(_Tp __m, _Up __n) 40419b4035fSMarshall Clow{ 40519b4035fSMarshall Clow static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to gcd must be integer types"); 4061c7fe126SMarshall Clow static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to gcd cannot be bool" ); 4071c7fe126SMarshall Clow static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to gcd cannot be bool" ); 40819b4035fSMarshall Clow using _Rp = common_type_t<_Tp,_Up>; 40919b4035fSMarshall Clow using _Wp = make_unsigned_t<_Rp>; 41050d61da2SEric Fiselier return static_cast<_Rp>(_VSTD::__gcd( 41150d61da2SEric Fiselier static_cast<_Wp>(__abs<_Rp, _Tp>()(__m)), 41258fc1b50SMarshall Clow static_cast<_Wp>(__abs<_Rp, _Up>()(__n)))); 41319b4035fSMarshall Clow} 41419b4035fSMarshall Clow 41519b4035fSMarshall Clowtemplate<class _Tp, class _Up> 41619b4035fSMarshall Clow_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY 41719b4035fSMarshall Clowcommon_type_t<_Tp,_Up> 41819b4035fSMarshall Clowlcm(_Tp __m, _Up __n) 41919b4035fSMarshall Clow{ 42019b4035fSMarshall Clow static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to lcm must be integer types"); 4211c7fe126SMarshall Clow static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to lcm cannot be bool" ); 4221c7fe126SMarshall Clow static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to lcm cannot be bool" ); 42319b4035fSMarshall Clow if (__m == 0 || __n == 0) 42419b4035fSMarshall Clow return 0; 42519b4035fSMarshall Clow 42619b4035fSMarshall Clow using _Rp = common_type_t<_Tp,_Up>; 42750d61da2SEric Fiselier _Rp __val1 = __abs<_Rp, _Tp>()(__m) / _VSTD::gcd(__m, __n); 42858fc1b50SMarshall Clow _Rp __val2 = __abs<_Rp, _Up>()(__n); 42919b4035fSMarshall Clow _LIBCPP_ASSERT((numeric_limits<_Rp>::max() / __val1 > __val2), "Overflow in lcm"); 43019b4035fSMarshall Clow return __val1 * __val2; 43119b4035fSMarshall Clow} 43219b4035fSMarshall Clow 43319b4035fSMarshall Clow#endif /* _LIBCPP_STD_VER > 14 */ 43419b4035fSMarshall Clow 4353e519524SHoward Hinnant_LIBCPP_END_NAMESPACE_STD 4363e519524SHoward Hinnant 437a016efb1SEric Fiselier_LIBCPP_POP_MACROS 438a016efb1SEric Fiselier 4393e519524SHoward Hinnant#endif // _LIBCPP_NUMERIC 440