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 InputIterator, class OutputIterator, class T, class BinaryOperation>
26 OutputIterator inclusive_scan(InputIterator first, InputIterator last,
27                               OutputIterator result, BinaryOperation bOp, T init)
28 {
29     for (; first != last; ++first, (void) ++result) {
30         init = bOp(init, *first);
31         *result = init;
32         }
33     return result;
34 }
35 
36 
37 template<class InputIterator, class OutputIterator, class BinaryOperation>
38 OutputIterator inclusive_scan(InputIterator first, InputIterator last,
39                               OutputIterator result, BinaryOperation bOp)
40 {
41     if (first != last) {
42         typename std::iterator_traits<InputIterator>::value_type init = *first;
43         *result++ = init;
44         if (++first != last)
45             return boost::algorithm::inclusive_scan(first, last, result, bOp, init);
46         }
47 
48     return result;
49 }
50 
51 template<class InputIterator, class OutputIterator>
inclusive_scan(InputIterator first,InputIterator last,OutputIterator result)52 OutputIterator inclusive_scan(InputIterator first, InputIterator last,
53                    OutputIterator result)
54 {
55     typedef typename std::iterator_traits<InputIterator>::value_type VT;
56     return boost::algorithm::inclusive_scan(first, last, result, std::plus<VT>());
57 }
58 
59 }} // namespace boost and algorithm
60 
61 #endif // BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP
62