[PATCH] D34007: Implement inclusive_scan and transform_inclusive_scan

Bryce Adelstein Lelbach via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 8 15:45:35 PDT 2017


wash added a comment.

Here's `partial_sum`:

  template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
  inline _LIBCPP_INLINE_VISIBILITY
  _OutputIterator
  partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
              _BinaryOperation __binary_op)
  {
      if (__first != __last)
      {
          typename iterator_traits<_InputIterator>::value_type __t(*__first);
          *__result = __t;
          for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
          {
              __t = __binary_op(__t, *__first);
              *__result = __t;
          }
      }
      return __result;
  }

And here's the `inclusive_scan` that should be equivalent to that `partial_sum`:

  template <class _InputIterator, class _OutputIterator, class _BinaryOp>
  inline _LIBCPP_INLINE_VISIBILITY
  _OutputIterator
  inclusive_scan(_InputIterator __first, _InputIterator __last, 
                 _OutputIterator __result, _BinaryOp __b)
  {
      if (__first != __last) {
          typename iterator_traits<_InputIterator>::value_type __init = *__first++;
          return inclusive_scan(__first, __last, __result, __b, __init);
       }
      
      return __result;
  }

The `inclusive_scan` that it forwards to is:

  template <class _InputIterator, class _OutputIterator, class _Tp, class _BinaryOp>
  inline _LIBCPP_INLINE_VISIBILITY
  _OutputIterator 
  inclusive_scan(_InputIterator __first, _InputIterator __last, 
                 _OutputIterator __result, _BinaryOp __b,  _Tp __init)
  {
      *__result++ = __init;
      for (; __first != __last; ++__first) {
          __init = __b(__init, *__first);
          *__result++ = __init;
       }
  
      return __result;
  }

Inlining it, we get:

  template <class _InputIterator, class _OutputIterator, class _BinaryOp>
  inline _LIBCPP_INLINE_VISIBILITY
  _OutputIterator
  inclusive_scan(_InputIterator __first, _InputIterator __last, 
                 _OutputIterator __result, _BinaryOp __b)
  {
      if (__first != __last) {
          typename iterator_traits<_InputIterator>::value_type __init = *__first++;
          *__result++ = __init;
  
          for (; __first != __last; ++__first) {
              __init = __b(__init, *__first);
              *__result++ = __init;
          }
       }
      
      return __result;
  }

That looks equivalent to the `partial_sum` implementation above, so I think it is correct.


https://reviews.llvm.org/D34007





More information about the cfe-commits mailing list