exclusive_scan.hpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. Copyright (c) Marshall Clow 2017.
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. */
  6. /// \file exclusive_scan.hpp
  7. /// \brief ???
  8. /// \author Marshall Clow
  9. #ifndef BOOST_ALGORITHM_EXCLUSIVE_SCAN_HPP
  10. #define BOOST_ALGORITHM_EXCLUSIVE_SCAN_HPP
  11. #include <functional> // for std::plus
  12. #include <iterator> // for std::iterator_traits
  13. #include <boost/range/begin.hpp>
  14. #include <boost/range/end.hpp>
  15. #include <boost/range/value_type.hpp>
  16. namespace boost { namespace algorithm {
  17. template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
  18. OutputIterator exclusive_scan(InputIterator first, InputIterator last,
  19. OutputIterator result, T init, BinaryOperation bOp)
  20. {
  21. if (first != last)
  22. {
  23. T saved = init;
  24. do
  25. {
  26. init = bOp(init, *first);
  27. *result = saved;
  28. saved = init;
  29. ++result;
  30. } while (++first != last);
  31. }
  32. return result;
  33. }
  34. template<class InputIterator, class OutputIterator, class T>
  35. OutputIterator exclusive_scan(InputIterator first, InputIterator last,
  36. OutputIterator result, T init)
  37. {
  38. typedef typename std::iterator_traits<InputIterator>::value_type VT;
  39. return boost::algorithm::exclusive_scan(first, last, result, init, std::plus<VT>());
  40. }
  41. }} // namespace boost and algorithm
  42. #endif // BOOST_ALGORITHM_EXCLUSIVE_SCAN_HPP