transform_exclusive_scan.hpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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_exclusive_scan.hpp
  7. /// \brief ????
  8. /// \author Marshall Clow
  9. #ifndef BOOST_ALGORITHM_TRANSFORM_EXCLUSIVE_SCAN_HPP
  10. #define BOOST_ALGORITHM_TRANSFORM_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. /// \fn transform_exclusive_scan ( InputIterator first, InputIterator last, OutputIterator result, BinaryOperation bOp, UnaryOperation uOp, T init )
  19. /// \brief Transforms elements from the input range with uOp and then combines
  20. /// those transformed elements with bOp such that the n-1th element and the nth
  21. /// element are combined. Exclusivity means that the nth element is not
  22. /// included in the nth combination.
  23. /// \return The updated output iterator
  24. ///
  25. /// \param first The start of the input sequence
  26. /// \param last The end of the input sequence
  27. /// \param result The output iterator to write the results into
  28. /// \param bOp The operation for combining transformed input elements
  29. /// \param uOp The operation for transforming input elements
  30. /// \param init The initial value
  31. ///
  32. /// \note This function is part of the C++17 standard library
  33. template<class InputIterator, class OutputIterator, class T,
  34. class BinaryOperation, class UnaryOperation>
  35. OutputIterator transform_exclusive_scan(InputIterator first, InputIterator last,
  36. OutputIterator result, T init,
  37. BinaryOperation bOp, UnaryOperation uOp)
  38. {
  39. if (first != last)
  40. {
  41. T saved = init;
  42. do
  43. {
  44. init = bOp(init, uOp(*first));
  45. *result = saved;
  46. saved = init;
  47. ++result;
  48. } while (++first != last);
  49. }
  50. return result;
  51. }
  52. }} // namespace boost and algorithm
  53. #endif // BOOST_ALGORITHM_TRANSFORM_EXCLUSIVE_SCAN_HPP