Home | History | Annotate | Download | only in include
      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 
     17 namespace std
     18 {
     19 
     20 template <class InputIterator, class T>
     21     T
     22     accumulate(InputIterator first, InputIterator last, T init);
     23 
     24 template <class InputIterator, class T, class BinaryOperation>
     25     T
     26     accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);
     27 
     28 template <class InputIterator1, class InputIterator2, class T>
     29     T
     30     inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init);
     31 
     32 template <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 
     37 template <class InputIterator, class OutputIterator>
     38     OutputIterator
     39     partial_sum(InputIterator first, InputIterator last, OutputIterator result);
     40 
     41 template <class InputIterator, class OutputIterator, class BinaryOperation>
     42     OutputIterator
     43     partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
     44 
     45 template <class InputIterator, class OutputIterator>
     46     OutputIterator
     47     adjacent_difference(InputIterator first, InputIterator last, OutputIterator result);
     48 
     49 template <class InputIterator, class OutputIterator, class BinaryOperation>
     50     OutputIterator
     51     adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
     52 
     53 template <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 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
     64 #pragma GCC system_header
     65 #endif
     66 
     67 _LIBCPP_BEGIN_NAMESPACE_STD
     68 
     69 template <class _InputIterator, class _Tp>
     70 inline _LIBCPP_INLINE_VISIBILITY
     71 _Tp
     72 accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
     73 {
     74     for (; __first != __last; ++__first)
     75         __init = __init + *__first;
     76     return __init;
     77 }
     78 
     79 template <class _InputIterator, class _Tp, class _BinaryOperation>
     80 inline _LIBCPP_INLINE_VISIBILITY
     81 _Tp
     82 accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
     83 {
     84     for (; __first != __last; ++__first)
     85         __init = __binary_op(__init, *__first);
     86     return __init;
     87 }
     88 
     89 template <class _InputIterator1, class _InputIterator2, class _Tp>
     90 inline _LIBCPP_INLINE_VISIBILITY
     91 _Tp
     92 inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init)
     93 {
     94     for (; __first1 != __last1; ++__first1, (void) ++__first2)
     95         __init = __init + *__first1 * *__first2;
     96     return __init;
     97 }
     98 
     99 template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
    100 inline _LIBCPP_INLINE_VISIBILITY
    101 _Tp
    102 inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
    103               _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2)
    104 {
    105     for (; __first1 != __last1; ++__first1, (void) ++__first2)
    106         __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
    107     return __init;
    108 }
    109 
    110 template <class _InputIterator, class _OutputIterator>
    111 inline _LIBCPP_INLINE_VISIBILITY
    112 _OutputIterator
    113 partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
    114 {
    115     if (__first != __last)
    116     {
    117         typename iterator_traits<_InputIterator>::value_type __t(*__first);
    118         *__result = __t;
    119         for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
    120         {
    121             __t = __t + *__first;
    122             *__result = __t;
    123         }
    124     }
    125     return __result;
    126 }
    127 
    128 template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
    129 inline _LIBCPP_INLINE_VISIBILITY
    130 _OutputIterator
    131 partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
    132               _BinaryOperation __binary_op)
    133 {
    134     if (__first != __last)
    135     {
    136         typename iterator_traits<_InputIterator>::value_type __t(*__first);
    137         *__result = __t;
    138         for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
    139         {
    140             __t = __binary_op(__t, *__first);
    141             *__result = __t;
    142         }
    143     }
    144     return __result;
    145 }
    146 
    147 template <class _InputIterator, class _OutputIterator>
    148 inline _LIBCPP_INLINE_VISIBILITY
    149 _OutputIterator
    150 adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
    151 {
    152     if (__first != __last)
    153     {
    154         typename iterator_traits<_InputIterator>::value_type __t1(*__first);
    155         *__result = __t1;
    156         for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
    157         {
    158             typename iterator_traits<_InputIterator>::value_type __t2(*__first);
    159             *__result = __t2 - __t1;
    160             __t1 = _VSTD::move(__t2);
    161         }
    162     }
    163     return __result;
    164 }
    165 
    166 template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
    167 inline _LIBCPP_INLINE_VISIBILITY
    168 _OutputIterator
    169 adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
    170                       _BinaryOperation __binary_op)
    171 {
    172     if (__first != __last)
    173     {
    174         typename iterator_traits<_InputIterator>::value_type __t1(*__first);
    175         *__result = __t1;
    176         for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
    177         {
    178             typename iterator_traits<_InputIterator>::value_type __t2(*__first);
    179             *__result = __binary_op(__t2, __t1);
    180             __t1 = _VSTD::move(__t2);
    181         }
    182     }
    183     return __result;
    184 }
    185 
    186 template <class _ForwardIterator, class _Tp>
    187 inline _LIBCPP_INLINE_VISIBILITY
    188 void
    189 iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_)
    190 {
    191     for (; __first != __last; ++__first, (void) ++__value_)
    192         *__first = __value_;
    193 }
    194 
    195 _LIBCPP_END_NAMESPACE_STD
    196 
    197 #endif  // _LIBCPP_NUMERIC
    198