All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
accumulate_functors.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2014 Andrzej Pacuk
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
15 #ifndef PAAL_ACCUMULATE_FUNCTORS_HPP
16 #define PAAL_ACCUMULATE_FUNCTORS_HPP
17 
18 #include "paal/utils/functors.hpp"
20 
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>
25 
26 namespace paal {
27 
29 template <
30  typename Range,
31  typename T,
32  typename Functor,
33  typename BinaryOperation = utils::plus>
35  const Range& rng,
36  T init,
37  Functor f,
38  BinaryOperation bin_op = BinaryOperation{}) {
39  return boost::accumulate(
40  rng | boost::adaptors::transformed(f),
41  init,
42  bin_op);
43 }
44 
46 template <typename Range, typename Functor>
47 auto sum_functor(const Range& rng, Functor f) {
49  return accumulate_functor(rng, T{}, f);
50 }
51 
53 template <typename Range, typename Functor>
54 auto max_element_functor(Range&& range, Functor f) {
55  auto unsafe_to_return_iterator = boost::max_element(
56  std::forward<Range>(range) |
57  boost::adaptors::transformed(utils::make_assignable_functor(f)));
58  return boost::make_transform_iterator(unsafe_to_return_iterator.base(), f);
59 }
60 
62 template <typename Range, typename Functor>
63 auto min_element_functor(Range&& range, Functor f) {
64  auto unsafe_to_return_iterator = boost::min_element(
65  std::forward<Range>(range) |
66  boost::adaptors::transformed(utils::make_assignable_functor(f)));
67  return boost::make_transform_iterator(unsafe_to_return_iterator.base(), f);
68 }
69 
71 template <typename ValueType = double, typename CounterType = std::size_t>
73  ValueType m_accumulated_value;
74  CounterType m_cnt;
75 
76 public:
78  template<class Archive>
79  void serialize(Archive & ar, const unsigned int version) {
80  ar & m_accumulated_value;
81  ar & m_cnt;
82  }
83 
90  average_accumulator(ValueType value = ValueType{},
91  CounterType cnt = CounterType{}) :
92  m_accumulated_value(value), m_cnt(cnt) {
93  }
94 
96  bool operator==(average_accumulator other) const {
97  return m_accumulated_value == other.m_accumulated_value &&
98  m_cnt == other.m_cnt;
99  }
100 
107  void add_value(ValueType value, CounterType cnt = 1) {
108  m_accumulated_value += value;
109  m_cnt += cnt;
110  }
111 
118  add_value(accumulator.m_accumulated_value, accumulator.m_cnt);
119  return *this;
120  }
121 
127  ValueType get_accumulated_value() const {
128  return m_accumulated_value;
129  }
130 
136  CounterType get_count() const {
137  return m_cnt;
138  }
139 
145  bool empty() const {
146  return m_cnt == 0;
147  }
148 
154  template <typename ReturnType = double>
155  ReturnType get_average_unsafe() const {
156  return static_cast<ReturnType>(m_accumulated_value) / m_cnt;
157  }
158 
166  template <typename ReturnType = double>
167  ReturnType get_average(ReturnType default_value = ReturnType{}) const {
168  if (m_cnt) {
169  return get_average_unsafe();
170  } else {
171  return default_value;
172  }
173  }
174 
175 };
176 
177 }
178 #endif /* PAAL_ACCUMULATE_FUNCTORS_HPP */
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
Definition: functors.hpp:421
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