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/config.hpp>
  14. #include <boost/range/begin.hpp>
  15. #include <boost/range/end.hpp>
  16. #include <boost/range/value_type.hpp>
  17. namespace boost { namespace algorithm {
  18. template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
  19. OutputIterator exclusive_scan(InputIterator first, InputIterator last,
  20. OutputIterator result, T init, BinaryOperation bOp)
  21. {
  22. if (first != last)
  23. {
  24. T saved = init;
  25. do
  26. {
  27. init = bOp(init, *first);
  28. *result = saved;
  29. saved = init;
  30. ++result;
  31. } while (++first != last);
  32. }
  33. return result;
  34. }
  35. template<class InputIterator, class OutputIterator, class T>
  36. OutputIterator exclusive_scan(InputIterator first, InputIterator last,
  37. OutputIterator result, T init)
  38. {
  39. typedef typename std::iterator_traits<InputIterator>::value_type VT;
  40. return boost::algorithm::exclusive_scan(first, last, result, init, std::plus<VT>());
  41. }
  42. }} // namespace boost and algorithm
  43. #endif // BOOST_ALGORITHM_EXCLUSIVE_SCAN_HPP