SPH
ArrayStats.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "io/Logger.h"
7 #include <algorithm>
8 
10 
11 template <typename Type>
12 class ArrayStats {
13 private:
14  Type minValue = std::numeric_limits<Type>::max();
15  Type maxValue = std::numeric_limits<Type>::min();
16  Type medianValue = Type(0);
17  double averageValue = 0.;
18 
19 public:
22  for (const Type& v : values) {
23  minValue = Sph::min(minValue, v);
24  maxValue = Sph::max(maxValue, v);
25  averageValue += v;
26  }
27  if (values.empty()) {
28  return;
29  }
30  averageValue /= values.size();
31  const Size mid = values.size() >> 1;
32  Array<Type> cloned;
33  cloned.pushAll(values.begin(), values.end());
34  std::nth_element(cloned.begin(), cloned.begin() + mid, cloned.end());
35  medianValue = cloned[mid];
36  }
37 
38  INLINE Type min() const {
39  return minValue;
40  }
41 
42  INLINE Type max() const {
43  return maxValue;
44  }
45 
46  INLINE Interval range() const {
47  return Interval(Float(minValue), Float(maxValue));
48  }
49 
50  INLINE Type average() const {
51  return averageValue;
52  }
53 
54  INLINE Type median() const {
55  return medianValue;
56  }
57 
58  void write(ILogger& logger) {
59  logger.write("Min = " + std::to_string(min()) + "; Max = " + std::to_string(max()) +
60  "; Mean = " + std::to_string(average()) + "; Median = " + std::to_string(median()));
61  }
62 };
63 
64 
Simple non-owning view of a container.
Generic dynamically allocated resizable storage.
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
Object representing interval of real values.
Logging routines of the run.
constexpr INLINE T max(const T &f1, const T &f2)
Definition: MathBasic.h:20
NAMESPACE_SPH_BEGIN constexpr INLINE T min(const T &f1, const T &f2)
Minimum & Maximum value.
Definition: MathBasic.h:15
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
INLINE Type max() const
Definition: ArrayStats.h:42
INLINE Type min() const
Definition: ArrayStats.h:38
ArrayStats(ArrayView< const Type > values)
Definition: ArrayStats.h:21
INLINE Type average() const
Definition: ArrayStats.h:50
INLINE Type median() const
Definition: ArrayStats.h:54
void write(ILogger &logger)
Definition: ArrayStats.h:58
INLINE Interval range() const
Definition: ArrayStats.h:46
INLINE bool empty() const
Definition: ArrayView.h:105
INLINE TCounter size() const
Definition: ArrayView.h:101
INLINE Iterator< StorageType > begin()
Definition: ArrayView.h:55
INLINE Iterator< StorageType > end()
Definition: ArrayView.h:63
INLINE Iterator< StorageType > end() noexcept
Definition: Array.h:462
INLINE Iterator< StorageType > begin() noexcept
Definition: Array.h:450
void pushAll(const TIter first, const TIter last)
Definition: Array.h:312
Interface providing generic (text, human readable) output of the program.
Definition: Logger.h:22
void write(TArgs &&... args)
Creates and logs a message by concatenating arguments.
Definition: Logger.h:37
Object representing a 1D interval of real numbers.
Definition: Interval.h:17