2 Copyright (c) Marshall Clow 2017.
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)
9 /// \brief Combine the elements of a sequence into a single value
10 /// \author Marshall Clow
12 #ifndef BOOST_ALGORITHM_REDUCE_HPP
13 #define BOOST_ALGORITHM_REDUCE_HPP
15 #include <functional> // for std::plus
16 #include <iterator> // for std::iterator_traits
18 #include <boost/config.hpp>
19 #include <boost/range/begin.hpp>
20 #include <boost/range/end.hpp>
21 #include <boost/range/value_type.hpp>
23 namespace boost { namespace algorithm {
25 template<class InputIterator, class T, class BinaryOperation>
26 T reduce(InputIterator first, InputIterator last, T init, BinaryOperation bOp)
29 for (; first != last; ++first)
30 init = bOp(init, *first);
34 template<class InputIterator, class T>
35 T reduce(InputIterator first, InputIterator last, T init)
37 typedef typename std::iterator_traits<InputIterator>::value_type VT;
38 return boost::algorithm::reduce(first, last, init, std::plus<VT>());
41 template<class InputIterator>
42 typename std::iterator_traits<InputIterator>::value_type
43 reduce(InputIterator first, InputIterator last)
45 return boost::algorithm::reduce(first, last,
46 typename std::iterator_traits<InputIterator>::value_type());
50 typename boost::range_value<Range>::type
51 reduce(const Range &r)
53 return boost::algorithm::reduce(boost::begin(r), boost::end(r));
56 // Not sure that this won't be ambiguous (1)
57 template<class Range, class T>
58 T reduce(const Range &r, T init)
60 return boost::algorithm::reduce(boost::begin (r), boost::end (r), init);
64 // Not sure that this won't be ambiguous (2)
65 template<class Range, class T, class BinaryOperation>
66 T reduce(const Range &r, T init, BinaryOperation bOp)
68 return boost::algorithm::reduce(boost::begin(r), boost::end(r), init, bOp);
71 }} // namespace boost and algorithm
73 #endif // BOOST_ALGORITHM_REDUCE_HPP