Horizon
min_max_accumulator.hpp
1 #pragma once
2 #include <algorithm>
3 
4 namespace horizon {
5 template <typename T> class MinMaxAccumulator {
6 public:
7  MinMaxAccumulator() : mi(), ma()
8  {
9  }
10  void accumulate(const T &v)
11  {
12  if (first) {
13  mi = v;
14  ma = v;
15  first = false;
16  }
17  else {
18  mi = std::min(mi, v);
19  ma = std::max(ma, v);
20  }
21  }
22  T get_max() const
23  {
24  return ma;
25  }
26  T get_min() const
27  {
28  return mi;
29  }
30 
31 private:
32  T mi;
33  T ma;
34  bool first = true;
35 };
36 } // namespace horizon