1/*
2 Copyright (c) Marshall Clow 2017.
3
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6*/
7
8/// \file transform_reduce.hpp
9/// \brief Combine the (transformed) elements of a sequence (or two) into a single value.
10/// \author Marshall Clow
11
12#ifndef BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP
13#define BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP
14
15#include <functional> // for std::plus
16#include <iterator> // for std::iterator_traits
17
18#include <boost/config.hpp>
19#include <boost/range/begin.hpp>
20#include <boost/range/end.hpp>
21#include <boost/range/value_type.hpp>
22
23namespace boost { namespace algorithm {
24
25/// \fn transform_inclusive_scan ( InputIterator first, InputIterator last, OutputIterator result, BinaryOperation bOp, UnaryOperation uOp, T init )
26/// \brief Transforms elements from the input range with uOp and then combines
27/// those transformed elements with bOp such that the n-1th element and the nth
28/// element are combined. Inclusivity means that the nth element is included in
29/// the nth combination.
30/// \return The updated output iterator
31///
32/// \param first The start of the input sequence
33/// \param last The end of the input sequence
34/// \param result The output iterator to write the results into
35/// \param bOp The operation for combining transformed input elements
36/// \param uOp The operation for transforming input elements
37/// \param init The initial value
38///
39/// \note This function is part of the C++17 standard library
40template<class InputIterator, class OutputIterator,
41 class BinaryOperation, class UnaryOperation, class T>
42OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last,
43 OutputIterator result,
44 BinaryOperation bOp, UnaryOperation uOp,
45 T init)
46{
47 for (; first != last; ++first, (void) ++result) {
48 init = bOp(init, uOp(*first));
49 *result = init;
50 }
51
52 return result;
53}
54
55/// \fn transform_inclusive_scan ( InputIterator first, InputIterator last, OutputIterator result, BinaryOperation bOp, UnaryOperation uOp, T init )
56/// \brief Transforms elements from the input range with uOp and then combines
57/// those transformed elements with bOp such that the n-1th element and the nth
58/// element are combined. Inclusivity means that the nth element is included in
59/// the nth combination. The first value will be used as the init.
60/// \return The updated output iterator
61///
62/// \param first The start of the input sequence
63/// \param last The end of the input sequence
64/// \param result The output iterator to write the results into
65/// \param bOp The operation for combining transformed input elements
66/// \param uOp The operation for transforming input elements
67///
68/// \note This function is part of the C++17 standard library
69template<class InputIterator, class OutputIterator,
70 class BinaryOperation, class UnaryOperation>
71OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last,
72 OutputIterator result,
73 BinaryOperation bOp, UnaryOperation uOp)
74{
75 if (first != last) {
76 typename std::iterator_traits<InputIterator>::value_type init = uOp(*first);
77 *result++ = init;
78 if (++first != last)
79 return boost::algorithm::transform_inclusive_scan
80 (first, last, result, bOp, uOp, init);
81 }
82
83 return result;
84}
85
86
87}} // namespace boost and algorithm
88
89#endif // BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP
90

source code of boost/libs/algorithm/include/boost/algorithm/cxx17/transform_inclusive_scan.hpp