15 #ifndef PAAL_ACCUMULATE_FUNCTORS_HPP
16 #define PAAL_ACCUMULATE_FUNCTORS_HPP
21 #include <boost/range/adaptor/transformed.hpp>
22 #include <boost/range/algorithm/max_element.hpp>
23 #include <boost/range/algorithm/min_element.hpp>
24 #include <boost/range/numeric.hpp>
38 BinaryOperation bin_op = BinaryOperation{}) {
39 return boost::accumulate(
40 rng | boost::adaptors::transformed(f),
46 template <
typename Range,
typename Functor>
53 template <
typename Range,
typename Functor>
55 auto unsafe_to_return_iterator = boost::max_element(
56 std::forward<Range>(range) |
58 return boost::make_transform_iterator(unsafe_to_return_iterator.base(), f);
62 template <
typename Range,
typename Functor>
64 auto unsafe_to_return_iterator = boost::min_element(
65 std::forward<Range>(range) |
67 return boost::make_transform_iterator(unsafe_to_return_iterator.base(), f);
71 template <
typename ValueType =
double,
typename CounterType = std::
size_t>
73 ValueType m_accumulated_value;
78 template<
class Archive>
79 void serialize(Archive & ar,
const unsigned int version) {
80 ar & m_accumulated_value;
91 CounterType cnt = CounterType{}) :
92 m_accumulated_value(value), m_cnt(cnt) {
97 return m_accumulated_value == other.m_accumulated_value &&
108 m_accumulated_value += value;
118 add_value(accumulator.m_accumulated_value, accumulator.m_cnt);
128 return m_accumulated_value;
154 template <
typename ReturnType =
double>
156 return static_cast<ReturnType
>(m_accumulated_value) / m_cnt;
166 template <
typename ReturnType =
double>
167 ReturnType
get_average(ReturnType default_value = ReturnType{})
const {
171 return default_value;
auto min_element_functor(Range &&range, Functor f)
combination of boost::min_element and boost::adaptors::transformed
ValueType get_accumulated_value() const
void serialize(Archive &ar, const unsigned int version)
serialize
ReturnType get_average(ReturnType default_value=ReturnType{}) const
auto max_element_functor(Range &&range, Functor f)
combination of boost::max_element and boost::adaptors::transformed
auto make_assignable_functor(Functor &f)
make function for assignable_functor
This file contains set of simple useful functors or functor adapters.
ReturnType get_average_unsafe() const
T accumulate_functor(const Range &rng, T init, Functor f, BinaryOperation bin_op=BinaryOperation{})
combination of boost::accumulate and boost::adaptors::transformed
void add_value(ValueType value, CounterType cnt=1)
average_accumulator & operator+=(const average_accumulator &accumulator)
average_accumulator(ValueType value=ValueType{}, CounterType cnt=CounterType{})
bool operator==(average_accumulator other) const
operator==
auto sum_functor(const Range &rng, Functor f)
sum of functor values over the range elements
CounterType get_count() const
typename std::decay< typename std::result_of< F >::type >::type pure_result_of_t
return pure type of function (decays const and reference)
helper class facilitating counting average