Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:23

0001 /*
0002    Copyright (c) Marshall Clow 2017.
0003 
0004    Distributed under the Boost Software License, Version 1.0. (See accompanying
0005    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 */
0007 
0008 /// \file  reduce.hpp
0009 /// \brief Combine the elements of a sequence into a single value
0010 /// \author Marshall Clow
0011 
0012 #ifndef BOOST_ALGORITHM_REDUCE_HPP
0013 #define BOOST_ALGORITHM_REDUCE_HPP
0014 
0015 #include <functional>     // for std::plus
0016 #include <iterator>       // for std::iterator_traits
0017 
0018 #include <boost/config.hpp>
0019 #include <boost/range/begin.hpp>
0020 #include <boost/range/end.hpp>
0021 #include <boost/range/value_type.hpp>
0022 
0023 namespace boost { namespace algorithm {
0024 
0025 template<class InputIterator, class T, class BinaryOperation>
0026 T reduce(InputIterator first, InputIterator last, T init, BinaryOperation bOp)
0027 {
0028     ;
0029     for (; first != last; ++first)
0030         init = bOp(init, *first);
0031     return init;
0032 }
0033 
0034 template<class InputIterator, class T>
0035 T reduce(InputIterator first, InputIterator last, T init)
0036 {
0037     typedef typename std::iterator_traits<InputIterator>::value_type VT;
0038     return boost::algorithm::reduce(first, last, init, std::plus<VT>());
0039 }
0040 
0041 template<class InputIterator>
0042 typename std::iterator_traits<InputIterator>::value_type
0043 reduce(InputIterator first, InputIterator last)
0044 {
0045     return boost::algorithm::reduce(first, last,
0046        typename std::iterator_traits<InputIterator>::value_type());
0047 }
0048 
0049 template<class Range>
0050 typename boost::range_value<Range>::type
0051 reduce(const Range &r)
0052 {
0053     return boost::algorithm::reduce(boost::begin(r), boost::end(r));
0054 }
0055 
0056 //  Not sure that this won't be ambiguous (1)
0057 template<class Range, class T>
0058 T reduce(const Range &r, T init)
0059 {
0060     return boost::algorithm::reduce(boost::begin (r), boost::end (r), init);
0061 }
0062 
0063 
0064 //  Not sure that this won't be ambiguous (2)
0065 template<class Range, class T, class BinaryOperation>
0066 T reduce(const Range &r, T init, BinaryOperation bOp)
0067 {
0068     return boost::algorithm::reduce(boost::begin(r), boost::end(r), init, bOp);
0069 }
0070 
0071 }} // namespace boost and algorithm
0072 
0073 #endif // BOOST_ALGORITHM_REDUCE_HPP