transform_inclusive_scan.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 transform_reduce.hpp
  7. /// \brief Combine the (transformed) elements of a sequence (or two) into a single value.
  8. /// \author Marshall Clow
  9. #ifndef BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP
  10. #define BOOST_ALGORITHM_TRANSFORM_REDUCE_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,
  19. class BinaryOperation, class UnaryOperation, class T>
  20. OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last,
  21. OutputIterator result,
  22. BinaryOperation bOp, UnaryOperation uOp,
  23. T init)
  24. {
  25. for (; first != last; ++first, (void) ++result) {
  26. init = bOp(init, uOp(*first));
  27. *result = init;
  28. }
  29. return result;
  30. }
  31. template<class InputIterator, class OutputIterator,
  32. class BinaryOperation, class UnaryOperation>
  33. OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last,
  34. OutputIterator result,
  35. BinaryOperation bOp, UnaryOperation uOp)
  36. {
  37. if (first != last) {
  38. typename std::iterator_traits<InputIterator>::value_type init = uOp(*first);
  39. *result++ = init;
  40. if (++first != last)
  41. return boost::algorithm::transform_inclusive_scan
  42. (first, last, result, bOp, uOp, init);
  43. }
  44. return result;
  45. }
  46. }} // namespace boost and algorithm
  47. #endif // BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP