numeric 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #ifndef _LIBCPP_NUMERIC
  11. #define _LIBCPP_NUMERIC
  12. /*
  13. numeric synopsis
  14. namespace std
  15. {
  16. template <class InputIterator, class T>
  17. T
  18. accumulate(InputIterator first, InputIterator last, T init);
  19. template <class InputIterator, class T, class BinaryOperation>
  20. T
  21. accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);
  22. template <class InputIterator1, class InputIterator2, class T>
  23. T
  24. inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init);
  25. template <class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
  26. T
  27. inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
  28. T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);
  29. template <class InputIterator, class OutputIterator>
  30. OutputIterator
  31. partial_sum(InputIterator first, InputIterator last, OutputIterator result);
  32. template <class InputIterator, class OutputIterator, class BinaryOperation>
  33. OutputIterator
  34. partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
  35. template <class InputIterator, class OutputIterator>
  36. OutputIterator
  37. adjacent_difference(InputIterator first, InputIterator last, OutputIterator result);
  38. template <class InputIterator, class OutputIterator, class BinaryOperation>
  39. OutputIterator
  40. adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
  41. template <class ForwardIterator, class T>
  42. void iota(ForwardIterator first, ForwardIterator last, T value);
  43. } // std
  44. */
  45. #include <__config>
  46. #include <iterator>
  47. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  48. #pragma GCC system_header
  49. #endif
  50. _LIBCPP_BEGIN_NAMESPACE_STD
  51. template <class _InputIterator, class _Tp>
  52. inline _LIBCPP_INLINE_VISIBILITY
  53. _Tp
  54. accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
  55. {
  56. for (; __first != __last; ++__first)
  57. __init = __init + *__first;
  58. return __init;
  59. }
  60. template <class _InputIterator, class _Tp, class _BinaryOperation>
  61. inline _LIBCPP_INLINE_VISIBILITY
  62. _Tp
  63. accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
  64. {
  65. for (; __first != __last; ++__first)
  66. __init = __binary_op(__init, *__first);
  67. return __init;
  68. }
  69. template <class _InputIterator1, class _InputIterator2, class _Tp>
  70. inline _LIBCPP_INLINE_VISIBILITY
  71. _Tp
  72. inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init)
  73. {
  74. for (; __first1 != __last1; ++__first1, (void) ++__first2)
  75. __init = __init + *__first1 * *__first2;
  76. return __init;
  77. }
  78. template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
  79. inline _LIBCPP_INLINE_VISIBILITY
  80. _Tp
  81. inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
  82. _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2)
  83. {
  84. for (; __first1 != __last1; ++__first1, (void) ++__first2)
  85. __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
  86. return __init;
  87. }
  88. template <class _InputIterator, class _OutputIterator>
  89. inline _LIBCPP_INLINE_VISIBILITY
  90. _OutputIterator
  91. partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
  92. {
  93. if (__first != __last)
  94. {
  95. typename iterator_traits<_InputIterator>::value_type __t(*__first);
  96. *__result = __t;
  97. for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
  98. {
  99. __t = __t + *__first;
  100. *__result = __t;
  101. }
  102. }
  103. return __result;
  104. }
  105. template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
  106. inline _LIBCPP_INLINE_VISIBILITY
  107. _OutputIterator
  108. partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
  109. _BinaryOperation __binary_op)
  110. {
  111. if (__first != __last)
  112. {
  113. typename iterator_traits<_InputIterator>::value_type __t(*__first);
  114. *__result = __t;
  115. for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
  116. {
  117. __t = __binary_op(__t, *__first);
  118. *__result = __t;
  119. }
  120. }
  121. return __result;
  122. }
  123. template <class _InputIterator, class _OutputIterator>
  124. inline _LIBCPP_INLINE_VISIBILITY
  125. _OutputIterator
  126. adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
  127. {
  128. if (__first != __last)
  129. {
  130. typename iterator_traits<_InputIterator>::value_type __t1(*__first);
  131. *__result = __t1;
  132. for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
  133. {
  134. typename iterator_traits<_InputIterator>::value_type __t2(*__first);
  135. *__result = __t2 - __t1;
  136. __t1 = _VSTD::move(__t2);
  137. }
  138. }
  139. return __result;
  140. }
  141. template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
  142. inline _LIBCPP_INLINE_VISIBILITY
  143. _OutputIterator
  144. adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
  145. _BinaryOperation __binary_op)
  146. {
  147. if (__first != __last)
  148. {
  149. typename iterator_traits<_InputIterator>::value_type __t1(*__first);
  150. *__result = __t1;
  151. for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
  152. {
  153. typename iterator_traits<_InputIterator>::value_type __t2(*__first);
  154. *__result = __binary_op(__t2, __t1);
  155. __t1 = _VSTD::move(__t2);
  156. }
  157. }
  158. return __result;
  159. }
  160. template <class _ForwardIterator, class _Tp>
  161. inline _LIBCPP_INLINE_VISIBILITY
  162. void
  163. iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_)
  164. {
  165. for (; __first != __last; ++__first, (void) ++__value_)
  166. *__first = __value_;
  167. }
  168. _LIBCPP_END_NAMESPACE_STD
  169. #endif // _LIBCPP_NUMERIC