2 Copyright (c) Marshall Clow 2017.
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE10.txt or copy at http://www.boost.org/LICENSE10.txt)
8 /// \file transform_reduce.hpp
9 /// \brief Combine the (transformed) elements of a sequence (or two) into a single value.
10 /// \author Marshall Clow
12 #ifndef BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP
13 #define BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP
15 #include <functional> // for std::plus
16 #include <iterator> // for std::iterator_traits
18 #include <boost/range/begin.hpp>
19 #include <boost/range/end.hpp>
20 #include <boost/range/value_type.hpp>
22 namespace boost { namespace algorithm {
24 template<class InputIterator1, class InputIterator2, class T,
25 class BinaryOperation1, class BinaryOperation2>
26 T transform_reduce(InputIterator1 first1, InputIterator1 last1,
27 InputIterator2 first2, T init,
28 BinaryOperation1 bOp1, BinaryOperation2 bOp2)
30 for (; first1 != last1; ++first1, (void) ++first2)
31 init = bOp1(init, bOp2(*first1, *first2));
35 template<class InputIterator, class T,
36 class BinaryOperation, class UnaryOperation>
37 T transform_reduce(InputIterator first, InputIterator last,
38 T init, BinaryOperation bOp, UnaryOperation uOp)
40 for (; first != last; ++first)
41 init = bOp(init, uOp(*first));
45 template<class InputIterator1, class InputIterator2, class T>
46 T transform_reduce(InputIterator1 first1, InputIterator1 last1,
47 InputIterator2 first2, T init)
49 return transform_reduce(first1, last1, first2, init,
50 std::plus<T>(), std::multiplies<T>());
53 }} // namespace boost and algorithm
55 #endif // BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP