Thrill  0.1
aggregate_min_max.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/math/aggregate_min_max.hpp
3  *
4  * Part of tlx - http://panthema.net/tlx
5  *
6  * Copyright (C) 2015-2019 Timo Bingmann <[email protected]>
7  *
9  ******************************************************************************/
10
13
14 #include <algorithm>
15 #include <limits>
16
17 namespace tlx {
18
19 //! \addtogroup tlx_math
20 //! \{
21
22 /*!
23  * Calculate running aggregate statistics: feed it with values, and it will keep
24  * the minimum and the maximum values.
25  */
26 template <typename Type_>
28 {
29 public:
30  using Type = Type_;
31
32  //! default constructor
33  AggregateMinMax() = default;
34
35  //! initializing constructor
36  AggregateMinMax(const Type& min, const Type& max) noexcept
37  : min_(min), max_(max) { }
38
39  //! add a value to the running aggregation
40  AggregateMinMax& add(const Type& value) noexcept {
41  min_ = std::min(min_, value);
42  max_ = std::max(max_, value);
43  return *this;
44  }
45
46  //! return minimum over all values aggregated
47  const Type& min() const noexcept { return min_; }
48
49  //! return maximum over all values aggregated
50  const Type& max() const noexcept { return max_; }
51
52  //! return maximum - minimum over all values aggregated
53  Type span() const noexcept { return max_ - min_; }
54
55  //! change currently aggregated minimum
56  void set_min(const Type& v) noexcept { min_ = v; }
57
58  //! change currently aggregated minimum
59  void set_max(const Type& v) noexcept { max_ = v; }
60
61  //! operator + to combine two AggregateMinMax<>
62  AggregateMinMax operator + (const AggregateMinMax& a) const noexcept {
63  return AggregateMinMax(
64  // min, max
65  std::min(min_, a.min_), std::max(max_, a.max_));
66  }
67
68  //! operator += to combine two AggregateMinMax<>
70  min_ = std::min(min_, a.min_);
71  max_ = std::max(max_, a.max_);
72  return *this;
73  }
74
75  //! serialization method for cereal.
76  template <typename Archive>
77  void serialize(Archive& archive) {
78  archive(min_, max_);
79  }
80
81 private:
82  //! minimum value
84
85  //! maximum value
86  Type max_ = std::numeric_limits<Type>::lowest();
87 };
88
89 //! \}
90
91 } // namespace tlx
92
93 #endif // !TLX_MATH_AGGREGATE_MIN_MAX_HEADER
94
95 /******************************************************************************/
void serialize(Archive &archive)
serialization method for cereal.
static uint_pair max()
return an uint_pair instance containing the largest value possible
Definition: uint_types.hpp:226
Type min_
minimum value
const Type & max() const noexcept
return maximum over all values aggregated
const Type & min() const noexcept
return minimum over all values aggregated
Type max_
maximum value
Type span() const noexcept
return maximum - minimum over all values aggregated
AggregateMinMax operator+(const AggregateMinMax &a) const noexcept
operator + to combine two AggregateMinMax<>
AggregateMinMax()=default
default constructor
AggregateMinMax(const Type &min, const Type &max) noexcept
initializing constructor
int value
Definition: gen_data.py:41
static uint_pair min()
return an uint_pair instance containing the smallest value possible
Definition: uint_types.hpp:217
void set_min(const Type &v) noexcept
change currently aggregated minimum
void set_max(const Type &v) noexcept
change currently aggregated minimum
AggregateMinMax & operator+=(const AggregateMinMax &a) noexcept
operator += to combine two AggregateMinMax<>
Calculate running aggregate statistics: feed it with values, and it will keep the minimum and the max...
AggregateMinMax & add(const Type &value) noexcept
add a value to the running aggregation