forked from 4lex4/scantailor-advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeviationProvider.h
146 lines (108 loc) · 3.57 KB
/
DeviationProvider.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifndef SCANTAILOR_DEVIATION_H
#define SCANTAILOR_DEVIATION_H
#include <foundation/NonCopyable.h>
#include <cmath>
#include <functional>
#include <unordered_map>
template <typename K, typename Hash = std::hash<K>>
class DeviationProvider {
DECLARE_NON_COPYABLE(DeviationProvider)
public:
DeviationProvider() = default;
explicit DeviationProvider(const std::function<double(const K&)>& computeValueByKey);
bool isDeviant(const K& key, double coefficient = 1.0, double threshold = 0.0) const;
double getDeviationValue(const K& key) const;
void addOrUpdate(const K& key);
void addOrUpdate(const K& key, double value);
void remove(const K& key);
void clear();
void setComputeValueByKey(const std::function<double(const K&)>& computeValueByKey);
protected:
void update() const;
private:
std::function<double(const K&)> m_computeValueByKey;
std::unordered_map<K, double, Hash> m_keyValueMap;
// Cached values.
mutable bool m_needUpdate = false;
mutable double m_meanValue = 0.0;
mutable double m_standardDeviation = 0.0;
};
template <typename K, typename Hash>
DeviationProvider<K, Hash>::DeviationProvider(const std::function<double(const K&)>& computeValueByKey)
: m_computeValueByKey(computeValueByKey) {}
template <typename K, typename Hash>
bool DeviationProvider<K, Hash>::isDeviant(const K& key, const double coefficient, const double threshold) const {
if (m_keyValueMap.find(key) == m_keyValueMap.end()) {
return false;
}
if (m_keyValueMap.size() < 3) {
return false;
}
update();
return (std::abs(m_keyValueMap.at(key) - m_meanValue) > std::max((coefficient * m_standardDeviation), threshold));
}
template <typename K, typename Hash>
double DeviationProvider<K, Hash>::getDeviationValue(const K& key) const {
if (m_keyValueMap.find(key) == m_keyValueMap.end()) {
return .0;
}
if (m_keyValueMap.size() < 2) {
return .0;
}
update();
return std::abs(m_keyValueMap.at(key) - m_meanValue);
}
template <typename K, typename Hash>
void DeviationProvider<K, Hash>::addOrUpdate(const K& key) {
m_needUpdate = true;
m_keyValueMap[key] = m_computeValueByKey(key);
}
template <typename K, typename Hash>
void DeviationProvider<K, Hash>::addOrUpdate(const K& key, const double value) {
m_needUpdate = true;
m_keyValueMap[key] = value;
}
template <typename K, typename Hash>
void DeviationProvider<K, Hash>::remove(const K& key) {
m_needUpdate = true;
if (m_keyValueMap.find(key) == m_keyValueMap.end()) {
return;
}
m_keyValueMap.erase(key);
}
template <typename K, typename Hash>
void DeviationProvider<K, Hash>::update() const {
if (!m_needUpdate) {
return;
}
if (m_keyValueMap.size() < 2) {
return;
}
{
double sum = .0;
for (const std::pair<K, double>& keyAndValue : m_keyValueMap) {
sum += keyAndValue.second;
}
m_meanValue = sum / m_keyValueMap.size();
}
{
double differencesSum = .0;
for (const std::pair<K, double>& keyAndValue : m_keyValueMap) {
differencesSum += std::pow(keyAndValue.second - m_meanValue, 2);
}
m_standardDeviation = std::sqrt(differencesSum / (m_keyValueMap.size() - 1));
}
m_needUpdate = false;
}
template <typename K, typename Hash>
void DeviationProvider<K, Hash>::setComputeValueByKey(const std::function<double(const K&)>& computeValueByKey) {
this->m_computeValueByKey = std::move(computeValueByKey);
}
template <typename K, typename Hash>
void DeviationProvider<K, Hash>::clear() {
m_keyValueMap.clear();
m_needUpdate = false;
m_meanValue = 0.0;
m_standardDeviation = 0.0;
}
#endif // SCANTAILOR_DEVIATION_H