transform_inclusive_scan.hpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. /// \fn transform_inclusive_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. Inclusivity means that the nth element is included in
  22. /// 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,
  34. class BinaryOperation, class UnaryOperation, class T>
  35. OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last,
  36. OutputIterator result,
  37. BinaryOperation bOp, UnaryOperation uOp,
  38. T init)
  39. {
  40. for (; first != last; ++first, (void) ++result) {
  41. init = bOp(init, uOp(*first));
  42. *result = init;
  43. }
  44. return result;
  45. }
  46. /// \fn transform_inclusive_scan ( InputIterator first, InputIterator last, OutputIterator result, BinaryOperation bOp, UnaryOperation uOp, T init )
  47. /// \brief Transforms elements from the input range with uOp and then combines
  48. /// those transformed elements with bOp such that the n-1th element and the nth
  49. /// element are combined. Inclusivity means that the nth element is included in
  50. /// the nth combination. The first value will be used as the init.
  51. /// \return The updated output iterator
  52. ///
  53. /// \param first The start of the input sequence
  54. /// \param last The end of the input sequence
  55. /// \param result The output iterator to write the results into
  56. /// \param bOp The operation for combining transformed input elements
  57. /// \param uOp The operation for transforming input elements
  58. ///
  59. /// \note This function is part of the C++17 standard library
  60. template<class InputIterator, class OutputIterator,
  61. class BinaryOperation, class UnaryOperation>
  62. OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last,
  63. OutputIterator result,
  64. BinaryOperation bOp, UnaryOperation uOp)
  65. {
  66. if (first != last) {
  67. typename std::iterator_traits<InputIterator>::value_type init = uOp(*first);
  68. *result++ = init;
  69. if (++first != last)
  70. return boost::algorithm::transform_inclusive_scan
  71. (first, last, result, bOp, uOp, init);
  72. }
  73. return result;
  74. }
  75. }} // namespace boost and algorithm
  76. #endif // BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP