Utilities
Utilities header only library
Statistics.h
1 /*
2  Copyright (C) Skeletex Research, s.r.o. - All Rights Reserved
3  Unauthorized copying of this file, via any medium is strictly prohibited
4  Proprietary and confidential
5 */
6 #ifndef UTILS_STATISTICS_H
7 #define UTILS_STATISTICS_H
8 #include <glm/detail/func_common.hpp>
9 #include <glm/detail/func_exponential.hpp>
10 #include <string>
11 #include <vector>
12 
14 namespace utils
15 {
16 
33  template<typename Type>
34  class Statistics
35  {
36  public:
37 
40  {
41  data_.reserve(100);
42  };
43 
45  Statistics(size_t initial_capacity)
46  {
47  data_.reserve(initial_capacity);
48  };
49 
51  Statistics(const std::vector<Type> &data_vector)
52  {
53  data_.reserve(data_vector.size());
54  for (const auto &value : data_vector)
55  {
56  RawAdd(value);
57  }
58  };
59 
61  bool IsEmpty() const
62  {
63  return data_.empty();
64  }
65 
67  void Clear()
68  {
69  data_.clear();
70  data_sum_ = Type(0);
71  data_min_ = std::numeric_limits<Type>::max();
72  data_max_ = std::numeric_limits<Type>::lowest();
73  }
74 
76  void Add(const Type value)
77  {
78  if (data_.capacity() == data_.size())
79  {
80  data_.reserve(2 * data_.capacity());
81  }
82  data_.push_back(value);
83  data_sum_ += value;
84  data_min_ = glm::min(value, data_min_);
85  data_max_ = glm::max(value, data_max_);
86  };
87 
89  size_t Count() const
90  {
91  return data_.size();
92  };
93 
95  Type Sum() const
96  {
97  return data_sum_;
98  };
99 
101  Type Average() const
102  {
103  if (data_.empty())
104  {
105  return Type(0);
106  }
107  return data_sum_ / Type(data_.size());
108  };
109 
111  Type Min() const
112  {
113  return data_min_;
114  }
115 
117  Type Max() const
118  {
119  return data_max_;
120  }
121 
123  Type StandardDeviation() const
124  {
125  const auto avg = Average();
126  Type sum = Type(0);
127  for (size_t i = 0; i < data_.size(); ++i)
128  {
129  const auto dev = avg - data_[i];
130  sum += dev * dev;
131  }
132  return glm::sqrt(sum / Type(data_.size()));
133  }
134 
136  std::string ToString() const
137  {
138  std::string text;
139  text += "Statistic data: ";
140  text += std::to_string(data_.size()) + " records, ";
141  text += "sum of elements " + std::to_string(data_sum_) + ", ";
142  text += "min " + std::to_string(data_min_) + ", ";
143  text += "max " + std::to_string(data_max_) + ", ";
144  text += "average " + std::to_string(Average()) + ".";
145  return text;
146  }
147 
148  private:
149  std::vector<Type> data_;
150  Type data_sum_ = Type(0);
151  Type data_min_ = std::numeric_limits<Type>::max();
152  Type data_max_ = std::numeric_limits<Type>::lowest();
153  };
154 
155 }
156 
157 #endif /* !UTILS_STATISTICS_H */
Type Min() const
Returns minimal value of the data set.
Definition: Statistics.h:111
Statistics()
Creates new instance with default capacity of the data set.
Definition: Statistics.h:39
bool IsEmpty() const
Checks if there are no data records.
Definition: Statistics.h:61
std::string ToString() const
Prints all statistical values to the string and returns it.
Definition: Statistics.h:136
void Add(const Type value)
Stores a data record in the data set.
Definition: Statistics.h:76
Type Average() const
Returns average value of the data set.
Definition: Statistics.h:101
void Clear()
Removes all stored data.
Definition: Statistics.h:67
Type StandardDeviation() const
Returns standard deviation of the data set.
Definition: Statistics.h:123
Type Sum() const
Returns a sum of all elements in the data set.
Definition: Statistics.h:95
Statistics(const std::vector< Type > &data_vector)
Creates new instance with initialized data set.
Definition: Statistics.h:51
General namespace for utilities.
Definition: BulletConversions.h:9
Statistics(size_t initial_capacity)
Creates new instance with specific capacity of the data set.
Definition: Statistics.h:45
Type Max() const
Returns maximal value of the data set.
Definition: Statistics.h:117
Calculates statistical evaluation on a data set.
Definition: Statistics.h:34
size_t Count() const
Returns number of stored data records.
Definition: Statistics.h:89