1// -*- C++ -*- 2//===---------------------------- numeric ---------------------------------===// 3// 4// The LLVM Compiler Infrastructure 5// 6// This file is dual licensed under the MIT and the University of Illinois Open 7// Source Licenses. See LICENSE.TXT for details. 8// 9//===----------------------------------------------------------------------===// 10 11#ifndef _LIBCPP_NUMERIC 12#define _LIBCPP_NUMERIC 13 14/* 15 numeric synopsis 16 17namespace std 18{ 19 20template <class InputIterator, class T> 21 T 22 accumulate(InputIterator first, InputIterator last, T init); 23 24template <class InputIterator, class T, class BinaryOperation> 25 T 26 accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op); 27 28template <class InputIterator1, class InputIterator2, class T> 29 T 30 inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init); 31 32template <class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2> 33 T 34 inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, 35 T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2); 36 37template <class InputIterator, class OutputIterator> 38 OutputIterator 39 partial_sum(InputIterator first, InputIterator last, OutputIterator result); 40 41template <class InputIterator, class OutputIterator, class BinaryOperation> 42 OutputIterator 43 partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op); 44 45template <class InputIterator, class OutputIterator> 46 OutputIterator 47 adjacent_difference(InputIterator first, InputIterator last, OutputIterator result); 48 49template <class InputIterator, class OutputIterator, class BinaryOperation> 50 OutputIterator 51 adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op); 52 53template <class ForwardIterator, class T> 54 void iota(ForwardIterator first, ForwardIterator last, T value); 55 56} // std 57 58*/ 59 60#include <__config> 61#include <iterator> 62 63#pragma GCC system_header 64 65_LIBCPP_BEGIN_NAMESPACE_STD 66 67template <class _InputIterator, class _Tp> 68inline _LIBCPP_INLINE_VISIBILITY 69_Tp 70accumulate(_InputIterator __first, _InputIterator __last, _Tp __init) 71{ 72 for (; __first != __last; ++__first) 73 __init = __init + *__first; 74 return __init; 75} 76 77template <class _InputIterator, class _Tp, class _BinaryOperation> 78inline _LIBCPP_INLINE_VISIBILITY 79_Tp 80accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op) 81{ 82 for (; __first != __last; ++__first) 83 __init = __binary_op(__init, *__first); 84 return __init; 85} 86 87template <class _InputIterator1, class _InputIterator2, class _Tp> 88inline _LIBCPP_INLINE_VISIBILITY 89_Tp 90inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init) 91{ 92 for (; __first1 != __last1; ++__first1, ++__first2) 93 __init = __init + *__first1 * *__first2; 94 return __init; 95} 96 97template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2> 98inline _LIBCPP_INLINE_VISIBILITY 99_Tp 100inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, 101 _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2) 102{ 103 for (; __first1 != __last1; ++__first1, ++__first2) 104 __init = __binary_op1(__init, __binary_op2(*__first1, *__first2)); 105 return __init; 106} 107 108template <class _InputIterator, class _OutputIterator> 109inline _LIBCPP_INLINE_VISIBILITY 110_OutputIterator 111partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result) 112{ 113 if (__first != __last) 114 { 115 typename iterator_traits<_InputIterator>::value_type __t(*__first); 116 *__result = __t; 117 for (++__first, ++__result; __first != __last; ++__first, ++__result) 118 { 119 __t = __t + *__first; 120 *__result = __t; 121 } 122 } 123 return __result; 124} 125 126template <class _InputIterator, class _OutputIterator, class _BinaryOperation> 127inline _LIBCPP_INLINE_VISIBILITY 128_OutputIterator 129partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result, 130 _BinaryOperation __binary_op) 131{ 132 if (__first != __last) 133 { 134 typename iterator_traits<_InputIterator>::value_type __t(*__first); 135 *__result = __t; 136 for (++__first, ++__result; __first != __last; ++__first, ++__result) 137 { 138 __t = __binary_op(__t, *__first); 139 *__result = __t; 140 } 141 } 142 return __result; 143} 144 145template <class _InputIterator, class _OutputIterator> 146inline _LIBCPP_INLINE_VISIBILITY 147_OutputIterator 148adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result) 149{ 150 if (__first != __last) 151 { 152 typename iterator_traits<_InputIterator>::value_type __t1(*__first); 153 *__result = __t1; 154 for (++__first, ++__result; __first != __last; ++__first, ++__result) 155 { 156 typename iterator_traits<_InputIterator>::value_type __t2(*__first); 157 *__result = __t2 - __t1; 158 __t1 = __t2; 159 } 160 } 161 return __result; 162} 163 164template <class _InputIterator, class _OutputIterator, class _BinaryOperation> 165inline _LIBCPP_INLINE_VISIBILITY 166_OutputIterator 167adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result, 168 _BinaryOperation __binary_op) 169{ 170 if (__first != __last) 171 { 172 typename iterator_traits<_InputIterator>::value_type __t1(*__first); 173 *__result = __t1; 174 for (++__first, ++__result; __first != __last; ++__first, ++__result) 175 { 176 typename iterator_traits<_InputIterator>::value_type __t2(*__first); 177 *__result = __binary_op(__t2, __t1); 178 __t1 = __t2; 179 } 180 } 181 return __result; 182} 183 184template <class _ForwardIterator, class _Tp> 185inline _LIBCPP_INLINE_VISIBILITY 186void 187iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value) 188{ 189 for (; __first != __last; ++__first, ++__value) 190 *__first = __value; 191} 192 193_LIBCPP_END_NAMESPACE_STD 194 195#endif // _LIBCPP_NUMERIC 196