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 28f4ea23d3SMarshall Clowtemplate<class InputIterator> 29f4ea23d3SMarshall Clow typename iterator_traits<InputIterator>::value_type 30f4ea23d3SMarshall Clow reduce(InputIterator first, InputIterator last); // C++17 31f4ea23d3SMarshall Clow 32f4ea23d3SMarshall Clowtemplate<class InputIterator, class T> 33f4ea23d3SMarshall Clow T 34f4ea23d3SMarshall Clow reduce(InputIterator first, InputIterator last, T init); // C++17 35f4ea23d3SMarshall Clow 36f4ea23d3SMarshall Clowtemplate<class InputIterator, class T, class BinaryOperation> 37f4ea23d3SMarshall Clow T 38f4ea23d3SMarshall Clow reduce(InputIterator first, InputIterator last, T init, BinaryOperation binary_op); // C++17 39f4ea23d3SMarshall 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 49f4ea23d3SMarshall Clow 50f4ea23d3SMarshall Clowtemplate<class InputIterator1, class InputIterator2, class T> 51f4ea23d3SMarshall Clow T 52f4ea23d3SMarshall Clow transform_reduce(InputIterator1 first1, InputIterator1 last1, 53f4ea23d3SMarshall Clow InputIterator2 first2, T init); // C++17 54f4ea23d3SMarshall Clow 55f4ea23d3SMarshall Clowtemplate<class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2> 56f4ea23d3SMarshall Clow T 57f4ea23d3SMarshall Clow transform_reduce(InputIterator1 first1, InputIterator1 last1, 58f4ea23d3SMarshall Clow InputIterator2 first2, T init, 59f4ea23d3SMarshall Clow BinaryOperation1 binary_op1, BinaryOperation2 binary_op2); // C++17 60f4ea23d3SMarshall Clow 61f4ea23d3SMarshall Clowtemplate<class InputIterator, class T, class BinaryOperation, class UnaryOperation> 62f4ea23d3SMarshall Clow T 63f4ea23d3SMarshall Clow transform_reduce(InputIterator first, InputIterator last, T init, 64f4ea23d3SMarshall Clow BinaryOperation binary_op, UnaryOperation unary_op); // C++17 65f4ea23d3SMarshall 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 84*ac8ea7c6SMarshall Clowtemplate<class InputIterator, class OutputIterator> 85*ac8ea7c6SMarshall Clow OutputIterator 86*ac8ea7c6SMarshall Clow inclusive_scan(InputIterator first, InputIterator last, OutputIterator result); // C++17 87*ac8ea7c6SMarshall Clow 88*ac8ea7c6SMarshall Clowtemplate<class InputIterator, class OutputIterator, class BinaryOperation> 89*ac8ea7c6SMarshall Clow OutputIterator 90*ac8ea7c6SMarshall Clow inclusive_scan(InputIterator first, InputIterator last, 91*ac8ea7c6SMarshall Clow OutputIterator result, BinaryOperation binary_op); // C++17 92*ac8ea7c6SMarshall Clow 93*ac8ea7c6SMarshall Clowtemplate<class InputIterator, class OutputIterator, class BinaryOperation, class T> 94*ac8ea7c6SMarshall Clow OutputIterator 95*ac8ea7c6SMarshall Clow inclusive_scan(InputIterator first, InputIterator last, 96*ac8ea7c6SMarshall Clow OutputIterator result, BinaryOperation binary_op, T init); // C++17 97*ac8ea7c6SMarshall Clow 98e948ba1cSMarshall Clowtemplate<class InputIterator, class OutputIterator, class T, 99e948ba1cSMarshall Clow class BinaryOperation, class UnaryOperation> 100e948ba1cSMarshall Clow OutputIterator 101e948ba1cSMarshall Clow transform_exclusive_scan(InputIterator first, InputIterator last, 102e948ba1cSMarshall Clow OutputIterator result, T init, 103e948ba1cSMarshall Clow BinaryOperation binary_op, UnaryOperation unary_op); // C++17 104e948ba1cSMarshall Clow 105*ac8ea7c6SMarshall Clowtemplate<class InputIterator, class OutputIterator, 106*ac8ea7c6SMarshall Clow class BinaryOperation, class UnaryOperation> 107*ac8ea7c6SMarshall Clow OutputIterator 108*ac8ea7c6SMarshall Clow transform_inclusive_scan(InputIterator first, InputIterator last, 109*ac8ea7c6SMarshall Clow OutputIterator result, 110*ac8ea7c6SMarshall Clow BinaryOperation binary_op, UnaryOperation unary_op); // C++17 111*ac8ea7c6SMarshall Clow 112*ac8ea7c6SMarshall Clowtemplate<class InputIterator, class OutputIterator, 113*ac8ea7c6SMarshall Clow class BinaryOperation, class UnaryOperation, class T> 114*ac8ea7c6SMarshall Clow OutputIterator 115*ac8ea7c6SMarshall Clow transform_inclusive_scan(InputIterator first, InputIterator last, 116*ac8ea7c6SMarshall Clow OutputIterator result, 117*ac8ea7c6SMarshall Clow BinaryOperation binary_op, UnaryOperation unary_op, 118*ac8ea7c6SMarshall Clow T init); // C++17 119*ac8ea7c6SMarshall Clow 1203e519524SHoward Hinnanttemplate <class InputIterator, class OutputIterator> 1213e519524SHoward Hinnant OutputIterator 1223e519524SHoward Hinnant adjacent_difference(InputIterator first, InputIterator last, OutputIterator result); 1233e519524SHoward Hinnant 1243e519524SHoward Hinnanttemplate <class InputIterator, class OutputIterator, class BinaryOperation> 1253e519524SHoward Hinnant OutputIterator 1263e519524SHoward Hinnant adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op); 1273e519524SHoward Hinnant 12840c7ef90SHoward Hinnanttemplate <class ForwardIterator, class T> 12940c7ef90SHoward Hinnant void iota(ForwardIterator first, ForwardIterator last, T value); 13040c7ef90SHoward Hinnant 13119b4035fSMarshall Clowtemplate <class M, class N> 13219b4035fSMarshall Clow constexpr common_type_t<M,N> gcd(M m, N n); // C++17 13319b4035fSMarshall Clow 13419b4035fSMarshall Clowtemplate <class M, class N> 13519b4035fSMarshall Clow constexpr common_type_t<M,N> lcm(M m, N n); // C++17 13619b4035fSMarshall Clow 1373e519524SHoward Hinnant} // std 1383e519524SHoward Hinnant 1393e519524SHoward Hinnant*/ 1403e519524SHoward Hinnant 1413e519524SHoward Hinnant#include <__config> 1423e519524SHoward Hinnant#include <iterator> 143d35528b3SEric Fiselier#include <limits> // for numeric_limits 144e948ba1cSMarshall Clow#include <functional> 1453e519524SHoward Hinnant 146073458b1SHoward Hinnant#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 1473e519524SHoward Hinnant#pragma GCC system_header 148073458b1SHoward Hinnant#endif 1493e519524SHoward Hinnant 150a016efb1SEric Fiselier_LIBCPP_PUSH_MACROS 151a016efb1SEric Fiselier#include <__undef_macros> 152a016efb1SEric Fiselier 1533e519524SHoward Hinnant_LIBCPP_BEGIN_NAMESPACE_STD 1543e519524SHoward Hinnant 1553e519524SHoward Hinnanttemplate <class _InputIterator, class _Tp> 1563e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 1573e519524SHoward Hinnant_Tp 1583e519524SHoward Hinnantaccumulate(_InputIterator __first, _InputIterator __last, _Tp __init) 1593e519524SHoward Hinnant{ 1603e519524SHoward Hinnant for (; __first != __last; ++__first) 1613e519524SHoward Hinnant __init = __init + *__first; 1623e519524SHoward Hinnant return __init; 1633e519524SHoward Hinnant} 1643e519524SHoward Hinnant 1653e519524SHoward Hinnanttemplate <class _InputIterator, class _Tp, class _BinaryOperation> 1663e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 1673e519524SHoward Hinnant_Tp 1683e519524SHoward Hinnantaccumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op) 1693e519524SHoward Hinnant{ 1703e519524SHoward Hinnant for (; __first != __last; ++__first) 1713e519524SHoward Hinnant __init = __binary_op(__init, *__first); 1723e519524SHoward Hinnant return __init; 1733e519524SHoward Hinnant} 1743e519524SHoward Hinnant 175f4ea23d3SMarshall Clow#if _LIBCPP_STD_VER > 14 176f4ea23d3SMarshall Clowtemplate <class _InputIterator, class _Tp, class _BinaryOp> 177f4ea23d3SMarshall Clowinline _LIBCPP_INLINE_VISIBILITY 178f4ea23d3SMarshall Clow_Tp 179f4ea23d3SMarshall Clowreduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOp __b) 180f4ea23d3SMarshall Clow{ 181f4ea23d3SMarshall Clow for (; __first != __last; ++__first) 182f4ea23d3SMarshall Clow __init = __b(__init, *__first); 183f4ea23d3SMarshall Clow return __init; 184f4ea23d3SMarshall Clow} 185f4ea23d3SMarshall Clow 186f4ea23d3SMarshall Clowtemplate <class _InputIterator, class _Tp> 187f4ea23d3SMarshall Clowinline _LIBCPP_INLINE_VISIBILITY 188f4ea23d3SMarshall Clow_Tp 189f4ea23d3SMarshall Clowreduce(_InputIterator __first, _InputIterator __last, _Tp __init) 190f4ea23d3SMarshall Clow{ 191f4ea23d3SMarshall Clow return _VSTD::reduce(__first, __last, __init, _VSTD::plus<>()); 192f4ea23d3SMarshall Clow} 193f4ea23d3SMarshall Clow 194f4ea23d3SMarshall Clowtemplate <class _InputIterator> 195f4ea23d3SMarshall Clowinline _LIBCPP_INLINE_VISIBILITY 196f4ea23d3SMarshall Clowtypename iterator_traits<_InputIterator>::value_type 197f4ea23d3SMarshall Clowreduce(_InputIterator __first, _InputIterator __last) 198f4ea23d3SMarshall Clow{ 199f4ea23d3SMarshall Clow return _VSTD::reduce(__first, __last, 200f4ea23d3SMarshall Clow typename iterator_traits<_InputIterator>::value_type{}); 201f4ea23d3SMarshall Clow} 202f4ea23d3SMarshall Clow#endif 203f4ea23d3SMarshall Clow 2043e519524SHoward Hinnanttemplate <class _InputIterator1, class _InputIterator2, class _Tp> 2053e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 2063e519524SHoward Hinnant_Tp 2073e519524SHoward Hinnantinner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init) 2083e519524SHoward Hinnant{ 209910285b2SEric Fiselier for (; __first1 != __last1; ++__first1, (void) ++__first2) 2103e519524SHoward Hinnant __init = __init + *__first1 * *__first2; 2113e519524SHoward Hinnant return __init; 2123e519524SHoward Hinnant} 2133e519524SHoward Hinnant 2143e519524SHoward Hinnanttemplate <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2> 2153e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 2163e519524SHoward Hinnant_Tp 2173e519524SHoward Hinnantinner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, 2183e519524SHoward Hinnant _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2) 2193e519524SHoward Hinnant{ 220910285b2SEric Fiselier for (; __first1 != __last1; ++__first1, (void) ++__first2) 2213e519524SHoward Hinnant __init = __binary_op1(__init, __binary_op2(*__first1, *__first2)); 2223e519524SHoward Hinnant return __init; 2233e519524SHoward Hinnant} 2243e519524SHoward Hinnant 225f4ea23d3SMarshall Clow#if _LIBCPP_STD_VER > 14 226f4ea23d3SMarshall Clowtemplate <class _InputIterator, class _Tp, class _BinaryOp, class _UnaryOp> 227f4ea23d3SMarshall Clowinline _LIBCPP_INLINE_VISIBILITY 228f4ea23d3SMarshall Clow_Tp 229f4ea23d3SMarshall Clowtransform_reduce(_InputIterator __first, _InputIterator __last, 230f4ea23d3SMarshall Clow _Tp __init, _BinaryOp __b, _UnaryOp __u) 231f4ea23d3SMarshall Clow{ 232f4ea23d3SMarshall Clow for (; __first != __last; ++__first) 233f4ea23d3SMarshall Clow __init = __b(__init, __u(*__first)); 234f4ea23d3SMarshall Clow return __init; 235f4ea23d3SMarshall Clow} 236f4ea23d3SMarshall Clow 237f4ea23d3SMarshall Clowtemplate <class _InputIterator1, class _InputIterator2, 238f4ea23d3SMarshall Clow class _Tp, class _BinaryOp1, class _BinaryOp2> 239f4ea23d3SMarshall Clowinline _LIBCPP_INLINE_VISIBILITY 240f4ea23d3SMarshall Clow_Tp 241f4ea23d3SMarshall Clowtransform_reduce(_InputIterator1 __first1, _InputIterator1 __last1, 242f4ea23d3SMarshall Clow _InputIterator2 __first2, _Tp __init, _BinaryOp1 __b1, _BinaryOp2 __b2) 243f4ea23d3SMarshall Clow{ 244f4ea23d3SMarshall Clow for (; __first1 != __last1; ++__first1, (void) ++__first2) 245f4ea23d3SMarshall Clow __init = __b1(__init, __b2(*__first1, *__first2)); 246f4ea23d3SMarshall Clow return __init; 247f4ea23d3SMarshall Clow} 248f4ea23d3SMarshall Clow 249f4ea23d3SMarshall Clowtemplate <class _InputIterator1, class _InputIterator2, class _Tp> 250f4ea23d3SMarshall Clowinline _LIBCPP_INLINE_VISIBILITY 251f4ea23d3SMarshall Clow_Tp 252f4ea23d3SMarshall Clowtransform_reduce(_InputIterator1 __first1, _InputIterator1 __last1, 253f4ea23d3SMarshall Clow _InputIterator2 __first2, _Tp __init) 254f4ea23d3SMarshall Clow{ 255f4ea23d3SMarshall Clow return _VSTD::transform_reduce(__first1, __last1, __first2, __init, 256f4ea23d3SMarshall Clow _VSTD::plus<>(), _VSTD::multiplies<>()); 257f4ea23d3SMarshall Clow} 258f4ea23d3SMarshall Clow#endif 259f4ea23d3SMarshall Clow 2603e519524SHoward Hinnanttemplate <class _InputIterator, class _OutputIterator> 2613e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 2623e519524SHoward Hinnant_OutputIterator 2633e519524SHoward Hinnantpartial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result) 2643e519524SHoward Hinnant{ 2653e519524SHoward Hinnant if (__first != __last) 2663e519524SHoward Hinnant { 2673e519524SHoward Hinnant typename iterator_traits<_InputIterator>::value_type __t(*__first); 2683e519524SHoward Hinnant *__result = __t; 269910285b2SEric Fiselier for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result) 2703e519524SHoward Hinnant { 2713e519524SHoward Hinnant __t = __t + *__first; 2723e519524SHoward Hinnant *__result = __t; 2733e519524SHoward Hinnant } 2743e519524SHoward Hinnant } 2753e519524SHoward Hinnant return __result; 2763e519524SHoward Hinnant} 2773e519524SHoward Hinnant 2783e519524SHoward Hinnanttemplate <class _InputIterator, class _OutputIterator, class _BinaryOperation> 2793e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 2803e519524SHoward Hinnant_OutputIterator 2813e519524SHoward Hinnantpartial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result, 2823e519524SHoward Hinnant _BinaryOperation __binary_op) 2833e519524SHoward Hinnant{ 2843e519524SHoward Hinnant if (__first != __last) 2853e519524SHoward Hinnant { 2863e519524SHoward Hinnant typename iterator_traits<_InputIterator>::value_type __t(*__first); 2873e519524SHoward Hinnant *__result = __t; 288910285b2SEric Fiselier for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result) 2893e519524SHoward Hinnant { 2903e519524SHoward Hinnant __t = __binary_op(__t, *__first); 2913e519524SHoward Hinnant *__result = __t; 2923e519524SHoward Hinnant } 2933e519524SHoward Hinnant } 2943e519524SHoward Hinnant return __result; 2953e519524SHoward Hinnant} 2963e519524SHoward Hinnant 297e948ba1cSMarshall Clow#if _LIBCPP_STD_VER > 14 298e948ba1cSMarshall Clowtemplate <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp> 299e948ba1cSMarshall Clowinline _LIBCPP_INLINE_VISIBILITY 300e948ba1cSMarshall Clow_OutputIterator 301e948ba1cSMarshall Clowexclusive_scan(_InputIterator __first, _InputIterator __last, 302e948ba1cSMarshall Clow _OutputIterator __result, _Tp __init, _BinaryOp __b) 303e948ba1cSMarshall Clow{ 304e948ba1cSMarshall Clow if (__first != __last) 305e948ba1cSMarshall Clow { 306e948ba1cSMarshall Clow _Tp __saved = __init; 307e948ba1cSMarshall Clow do 308e948ba1cSMarshall Clow { 309e948ba1cSMarshall Clow __init = __b(__init, *__first); 310e948ba1cSMarshall Clow *__result = __saved; 311e948ba1cSMarshall Clow __saved = __init; 312e948ba1cSMarshall Clow ++__result; 313e948ba1cSMarshall Clow } while (++__first != __last); 314e948ba1cSMarshall Clow } 315e948ba1cSMarshall Clow return __result; 316e948ba1cSMarshall Clow} 317e948ba1cSMarshall Clow 318e948ba1cSMarshall Clowtemplate <class _InputIterator, class _OutputIterator, class _Tp> 319e948ba1cSMarshall Clowinline _LIBCPP_INLINE_VISIBILITY 320e948ba1cSMarshall Clow_OutputIterator 321e948ba1cSMarshall Clowexclusive_scan(_InputIterator __first, _InputIterator __last, 322e948ba1cSMarshall Clow _OutputIterator __result, _Tp __init) 323e948ba1cSMarshall Clow{ 324e948ba1cSMarshall Clow return _VSTD::exclusive_scan(__first, __last, __result, __init, _VSTD::plus<>()); 325e948ba1cSMarshall Clow} 326e948ba1cSMarshall Clow 327*ac8ea7c6SMarshall Clowtemplate <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp> 328*ac8ea7c6SMarshall Clow_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last, 329*ac8ea7c6SMarshall Clow _OutputIterator __result, _BinaryOp __b, _Tp __init) 330*ac8ea7c6SMarshall Clow{ 331*ac8ea7c6SMarshall Clow for (; __first != __last; ++__first, (void) ++__result) { 332*ac8ea7c6SMarshall Clow __init = __b(__init, *__first); 333*ac8ea7c6SMarshall Clow *__result = __init; 334*ac8ea7c6SMarshall Clow } 335*ac8ea7c6SMarshall Clow return __result; 336*ac8ea7c6SMarshall Clow} 337*ac8ea7c6SMarshall Clow 338*ac8ea7c6SMarshall Clowtemplate <class _InputIterator, class _OutputIterator, class _BinaryOp> 339*ac8ea7c6SMarshall Clow_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last, 340*ac8ea7c6SMarshall Clow _OutputIterator __result, _BinaryOp __b) 341*ac8ea7c6SMarshall Clow{ 342*ac8ea7c6SMarshall Clow if (__first != __last) { 343*ac8ea7c6SMarshall Clow typename std::iterator_traits<_InputIterator>::value_type __init = *__first; 344*ac8ea7c6SMarshall Clow *__result++ = __init; 345*ac8ea7c6SMarshall Clow if (++__first != __last) 346*ac8ea7c6SMarshall Clow return _VSTD::inclusive_scan(__first, __last, __result, __b, __init); 347*ac8ea7c6SMarshall Clow } 348*ac8ea7c6SMarshall Clow 349*ac8ea7c6SMarshall Clow return __result; 350*ac8ea7c6SMarshall Clow} 351*ac8ea7c6SMarshall Clow 352*ac8ea7c6SMarshall Clowtemplate <class _InputIterator, class _OutputIterator> 353*ac8ea7c6SMarshall Clow_OutputIterator inclusive_scan(_InputIterator __first, _InputIterator __last, 354*ac8ea7c6SMarshall Clow _OutputIterator __result) 355*ac8ea7c6SMarshall Clow{ 356*ac8ea7c6SMarshall Clow return _VSTD::inclusive_scan(__first, __last, __result, std::plus<>()); 357*ac8ea7c6SMarshall Clow} 358*ac8ea7c6SMarshall Clow 359e948ba1cSMarshall Clowtemplate <class _InputIterator, class _OutputIterator, class _Tp, 360e948ba1cSMarshall Clow class _BinaryOp, class _UnaryOp> 361e948ba1cSMarshall Clowinline _LIBCPP_INLINE_VISIBILITY 362e948ba1cSMarshall Clow_OutputIterator 363e948ba1cSMarshall Clowtransform_exclusive_scan(_InputIterator __first, _InputIterator __last, 364e948ba1cSMarshall Clow _OutputIterator __result, _Tp __init, 365e948ba1cSMarshall Clow _BinaryOp __b, _UnaryOp __u) 366e948ba1cSMarshall Clow{ 367e948ba1cSMarshall Clow if (__first != __last) 368e948ba1cSMarshall Clow { 369e948ba1cSMarshall Clow _Tp __saved = __init; 370e948ba1cSMarshall Clow do 371e948ba1cSMarshall Clow { 372e948ba1cSMarshall Clow __init = __b(__init, __u(*__first)); 373e948ba1cSMarshall Clow *__result = __saved; 374e948ba1cSMarshall Clow __saved = __init; 375e948ba1cSMarshall Clow ++__result; 376e948ba1cSMarshall Clow } while (++__first != __last); 377e948ba1cSMarshall Clow } 378e948ba1cSMarshall Clow return __result; 379e948ba1cSMarshall Clow} 380*ac8ea7c6SMarshall Clow 381*ac8ea7c6SMarshall Clowtemplate <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp, class _UnaryOp> 382*ac8ea7c6SMarshall Clow_OutputIterator transform_inclusive_scan(_InputIterator __first, _InputIterator __last, 383*ac8ea7c6SMarshall Clow _OutputIterator __result, _BinaryOp __b, _UnaryOp __u, _Tp __init) 384*ac8ea7c6SMarshall Clow{ 385*ac8ea7c6SMarshall Clow for (; __first != __last; ++__first, (void) ++__result) { 386*ac8ea7c6SMarshall Clow __init = __b(__init, __u(*__first)); 387*ac8ea7c6SMarshall Clow *__result = __init; 388*ac8ea7c6SMarshall Clow } 389*ac8ea7c6SMarshall Clow 390*ac8ea7c6SMarshall Clow return __result; 391*ac8ea7c6SMarshall Clow} 392*ac8ea7c6SMarshall Clow 393*ac8ea7c6SMarshall Clowtemplate <class _InputIterator, class _OutputIterator, class _BinaryOp, class _UnaryOp> 394*ac8ea7c6SMarshall Clow_OutputIterator transform_inclusive_scan(_InputIterator __first, _InputIterator __last, 395*ac8ea7c6SMarshall Clow _OutputIterator __result, _BinaryOp __b, _UnaryOp __u) 396*ac8ea7c6SMarshall Clow{ 397*ac8ea7c6SMarshall Clow if (__first != __last) { 398*ac8ea7c6SMarshall Clow typename std::iterator_traits<_InputIterator>::value_type __init = __u(*__first); 399*ac8ea7c6SMarshall Clow *__result++ = __init; 400*ac8ea7c6SMarshall Clow if (++__first != __last) 401*ac8ea7c6SMarshall Clow return _VSTD::transform_inclusive_scan(__first, __last, __result, __b, __u, __init); 402*ac8ea7c6SMarshall Clow } 403*ac8ea7c6SMarshall Clow 404*ac8ea7c6SMarshall Clow return __result; 405*ac8ea7c6SMarshall Clow} 406e948ba1cSMarshall Clow#endif 407e948ba1cSMarshall Clow 4083e519524SHoward Hinnanttemplate <class _InputIterator, class _OutputIterator> 4093e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 4103e519524SHoward Hinnant_OutputIterator 4113e519524SHoward Hinnantadjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result) 4123e519524SHoward Hinnant{ 4133e519524SHoward Hinnant if (__first != __last) 4143e519524SHoward Hinnant { 4153e519524SHoward Hinnant typename iterator_traits<_InputIterator>::value_type __t1(*__first); 4163e519524SHoward Hinnant *__result = __t1; 417910285b2SEric Fiselier for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result) 4183e519524SHoward Hinnant { 4193e519524SHoward Hinnant typename iterator_traits<_InputIterator>::value_type __t2(*__first); 4203e519524SHoward Hinnant *__result = __t2 - __t1; 4219ff3203fSHoward Hinnant __t1 = _VSTD::move(__t2); 4223e519524SHoward Hinnant } 4233e519524SHoward Hinnant } 4243e519524SHoward Hinnant return __result; 4253e519524SHoward Hinnant} 4263e519524SHoward Hinnant 4273e519524SHoward Hinnanttemplate <class _InputIterator, class _OutputIterator, class _BinaryOperation> 4283e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 4293e519524SHoward Hinnant_OutputIterator 4303e519524SHoward Hinnantadjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result, 4313e519524SHoward Hinnant _BinaryOperation __binary_op) 4323e519524SHoward Hinnant{ 4333e519524SHoward Hinnant if (__first != __last) 4343e519524SHoward Hinnant { 4353e519524SHoward Hinnant typename iterator_traits<_InputIterator>::value_type __t1(*__first); 4363e519524SHoward Hinnant *__result = __t1; 437910285b2SEric Fiselier for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result) 4383e519524SHoward Hinnant { 4393e519524SHoward Hinnant typename iterator_traits<_InputIterator>::value_type __t2(*__first); 4403e519524SHoward Hinnant *__result = __binary_op(__t2, __t1); 4419ff3203fSHoward Hinnant __t1 = _VSTD::move(__t2); 4423e519524SHoward Hinnant } 4433e519524SHoward Hinnant } 4443e519524SHoward Hinnant return __result; 4453e519524SHoward Hinnant} 4463e519524SHoward Hinnant 44740c7ef90SHoward Hinnanttemplate <class _ForwardIterator, class _Tp> 44840c7ef90SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY 44940c7ef90SHoward Hinnantvoid 450e4383379SHoward Hinnantiota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_) 45140c7ef90SHoward Hinnant{ 452910285b2SEric Fiselier for (; __first != __last; ++__first, (void) ++__value_) 453e4383379SHoward Hinnant *__first = __value_; 45440c7ef90SHoward Hinnant} 45540c7ef90SHoward Hinnant 45619b4035fSMarshall Clow 45719b4035fSMarshall Clow#if _LIBCPP_STD_VER > 14 45858fc1b50SMarshall Clowtemplate <typename _Result, typename _Source, bool _IsSigned = is_signed<_Source>::value> struct __abs; 45919b4035fSMarshall Clow 46058fc1b50SMarshall Clowtemplate <typename _Result, typename _Source> 46158fc1b50SMarshall Clowstruct __abs<_Result, _Source, true> { 46219b4035fSMarshall Clow _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY 46358fc1b50SMarshall Clow _Result operator()(_Source __t) const noexcept 46458fc1b50SMarshall Clow { 46558fc1b50SMarshall Clow if (__t >= 0) return __t; 46658fc1b50SMarshall Clow if (__t == numeric_limits<_Source>::min()) return -static_cast<_Result>(__t); 46758fc1b50SMarshall Clow return -__t; 46858fc1b50SMarshall Clow } 46919b4035fSMarshall Clow}; 47019b4035fSMarshall Clow 47158fc1b50SMarshall Clowtemplate <typename _Result, typename _Source> 47258fc1b50SMarshall Clowstruct __abs<_Result, _Source, false> { 47319b4035fSMarshall Clow _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY 47458fc1b50SMarshall Clow _Result operator()(_Source __t) const noexcept { return __t; } 47519b4035fSMarshall Clow}; 47619b4035fSMarshall Clow 47719b4035fSMarshall Clow 47819b4035fSMarshall Clowtemplate<class _Tp> 47950d61da2SEric Fiselier_LIBCPP_CONSTEXPR _LIBCPP_HIDDEN 48019b4035fSMarshall Clow_Tp __gcd(_Tp __m, _Tp __n) 48119b4035fSMarshall Clow{ 48219b4035fSMarshall Clow static_assert((!is_signed<_Tp>::value), ""); 48350d61da2SEric Fiselier return __n == 0 ? __m : _VSTD::__gcd<_Tp>(__n, __m % __n); 48419b4035fSMarshall Clow} 48519b4035fSMarshall Clow 48619b4035fSMarshall Clow 48719b4035fSMarshall Clowtemplate<class _Tp, class _Up> 48819b4035fSMarshall Clow_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY 48919b4035fSMarshall Clowcommon_type_t<_Tp,_Up> 49019b4035fSMarshall Clowgcd(_Tp __m, _Up __n) 49119b4035fSMarshall Clow{ 49219b4035fSMarshall Clow static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to gcd must be integer types"); 4931c7fe126SMarshall Clow static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to gcd cannot be bool" ); 4941c7fe126SMarshall Clow static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to gcd cannot be bool" ); 49519b4035fSMarshall Clow using _Rp = common_type_t<_Tp,_Up>; 49619b4035fSMarshall Clow using _Wp = make_unsigned_t<_Rp>; 49750d61da2SEric Fiselier return static_cast<_Rp>(_VSTD::__gcd( 49850d61da2SEric Fiselier static_cast<_Wp>(__abs<_Rp, _Tp>()(__m)), 49958fc1b50SMarshall Clow static_cast<_Wp>(__abs<_Rp, _Up>()(__n)))); 50019b4035fSMarshall Clow} 50119b4035fSMarshall Clow 50219b4035fSMarshall Clowtemplate<class _Tp, class _Up> 50319b4035fSMarshall Clow_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY 50419b4035fSMarshall Clowcommon_type_t<_Tp,_Up> 50519b4035fSMarshall Clowlcm(_Tp __m, _Up __n) 50619b4035fSMarshall Clow{ 50719b4035fSMarshall Clow static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to lcm must be integer types"); 5081c7fe126SMarshall Clow static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to lcm cannot be bool" ); 5091c7fe126SMarshall Clow static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to lcm cannot be bool" ); 51019b4035fSMarshall Clow if (__m == 0 || __n == 0) 51119b4035fSMarshall Clow return 0; 51219b4035fSMarshall Clow 51319b4035fSMarshall Clow using _Rp = common_type_t<_Tp,_Up>; 51450d61da2SEric Fiselier _Rp __val1 = __abs<_Rp, _Tp>()(__m) / _VSTD::gcd(__m, __n); 51558fc1b50SMarshall Clow _Rp __val2 = __abs<_Rp, _Up>()(__n); 51619b4035fSMarshall Clow _LIBCPP_ASSERT((numeric_limits<_Rp>::max() / __val1 > __val2), "Overflow in lcm"); 51719b4035fSMarshall Clow return __val1 * __val2; 51819b4035fSMarshall Clow} 51919b4035fSMarshall Clow 52019b4035fSMarshall Clow#endif /* _LIBCPP_STD_VER > 14 */ 52119b4035fSMarshall Clow 5223e519524SHoward Hinnant_LIBCPP_END_NAMESPACE_STD 5233e519524SHoward Hinnant 524a016efb1SEric Fiselier_LIBCPP_POP_MACROS 525a016efb1SEric Fiselier 5263e519524SHoward Hinnant#endif // _LIBCPP_NUMERIC 527