Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / algorithm / cxx17 / transform_reduce.hpp
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
23 namespace boost { namespace algorithm {
24
25 template<class InputIterator1, class InputIterator2, class T,
26          class BinaryOperation1, class BinaryOperation2>
27 T transform_reduce(InputIterator1 first1, InputIterator1 last1,
28                    InputIterator2 first2, T init,
29                  BinaryOperation1 bOp1, BinaryOperation2 bOp2)
30 {
31     for (; first1 != last1; ++first1, (void) ++first2)
32         init = bOp1(init, bOp2(*first1, *first2));
33     return init;
34 }
35
36 template<class InputIterator, class T,
37          class BinaryOperation, class UnaryOperation>
38 T transform_reduce(InputIterator first, InputIterator last,
39                    T init, BinaryOperation bOp, UnaryOperation uOp)
40 {
41     for (; first != last; ++first)
42         init = bOp(init, uOp(*first));
43     return init;
44 }
45
46 template<class InputIterator1, class InputIterator2, class T>
47 T transform_reduce(InputIterator1 first1, InputIterator1 last1,
48                    InputIterator2 first2, T init)
49 {
50     return boost::algorithm::transform_reduce(first1, last1, first2, init,
51                             std::plus<T>(), std::multiplies<T>());
52 }
53
54 }} // namespace boost and algorithm
55
56 #endif // BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP