forked from voutcn/megahit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
histgram.h
292 lines (224 loc) · 6.9 KB
/
histgram.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* @file histgram.h
* @brief Histgram Class.
* @author Yu Peng ([email protected])
* @version 1.0.0
* @date 2011-08-13
* @modified by Dinghua Li
*/
#ifndef __BASIC_HISTGRAM_H__
#define __BASIC_HISTGRAM_H__
#include <stdint.h>
#include <algorithm>
#include <cmath>
#include <deque>
#include <map>
/**
* @brief It is a class which contains a set of number for drawing histgram.
*
* @tparam T
*/
template <class T>
class Histgram {
public:
typedef T value_type;
Histgram(): size_(0) {
lock_ = 0;
}
Histgram(const Histgram<T> &hist)
: map_(hist.map_), size_(hist.size_) {
lock_ = 0;
}
~Histgram() { }
const Histgram<T> &operator =(const Histgram<T> &hist) {
map_ = hist.map_;
size_ = hist.size_;
return *this;
}
void insert(value_type value, size_t count = 1) {
while (__sync_lock_test_and_set(&lock_, 1)) while (lock_);
map_[value] += count;
size_ += count;
__sync_lock_release(&lock_);
}
size_t count(value_type value) const {
typename std::map<value_type, size_t>::const_iterator iter = map_.find(value);
if (iter == map_.end())
return 0;
else
return iter->second;
}
size_t count(value_type from, value_type to) const {
if (from >= to)
return 0;
size_t sum = 0;
for (typename std::map<value_type, size_t>::const_iterator iter = map_.lower_bound(from);
iter != map_.end() && iter->first < to; ++iter)
sum += iter->second;
return sum;
}
value_type minimum() const {
return empty() ? 0 : map_.begin()->first;
}
value_type maximum() const {
return empty() ? 0 : map_.rbegin()->first;
}
double mean() const {
if (size() == 0) {
return 0;
}
return sum() / size();
}
double sum() const {
double sum_ = 0;
for (typename std::map<value_type, size_t>::const_iterator iter = map_.begin(); iter != map_.end(); ++iter)
sum_ += 1.0 * iter->first * iter->second;
return sum_;
}
double variance() const {
double sum = 0;
double square_sum = 0;
size_t n = size();
for (typename std::map<value_type, size_t>::const_iterator iter = map_.begin(); iter != map_.end(); ++iter) {
sum += 1.0 * iter->first * iter->second;
square_sum += 1.0 * iter->first * iter->first * iter->second;
}
return square_sum / n - (sum / n) * (sum / n);
}
double sd() const {
return std::sqrt(variance());
}
value_type median() const {
size_t half = size() / 2;
size_t sum = 0;
for (typename std::map<value_type, size_t>::const_iterator iter = map_.begin(); iter != map_.end(); ++iter) {
sum += iter->second;
if (sum > half)
return iter->first;
}
return 0;
}
value_type percentile(double p) const {
size_t half = size() * p;
size_t sum = 0;
for (typename std::map<value_type, size_t>::const_iterator iter = map_.begin(); iter != map_.end(); ++iter) {
sum += iter->second;
if (sum > half)
return iter->first;
}
return 0;
}
double up_mean(double p) const {
size_t count = size() * p;
size_t sum = 0;
double total = 0;
for (typename std::map<value_type, size_t>::const_reverse_iterator iter = map_.rbegin(); iter != map_.rend(); ++iter) {
sum += iter->second;
total += iter->second * iter->first;
if (sum > count)
break;
}
return (sum != 0) ? total / sum : 0;
}
value_type Nx(double x) {
double total = 0;
for (typename std::map<value_type, size_t>::const_reverse_iterator iter = map_.rbegin(); iter != map_.rend(); ++iter) {
total += iter->second * iter->first;
if (total >= x)
return iter->first;
}
return 0;
}
/** Return the first local minimum or zero if a minimum is not
* found. */
value_type FirstLocalMinimum() const
{
const size_t kSmoothing = 4;
auto minimum = map_.begin();
size_t count = 0;
for (auto it = map_.begin(); it != map_.end(); ++it) {
if (it->second <= minimum->second) {
minimum = it;
count = 0;
} else if (++count >= kSmoothing)
break;
}
if (minimum->first == maximum())
return 0;
return minimum->first;
}
size_t Trim(double fraction) {
size_t trim_size = size_t(size() * fraction / 2 + 0.5);
std::deque<value_type> trim_values;
size_t sum = 0;
for (typename std::map<value_type, size_t>::iterator iter = map_.begin(); iter != map_.end(); ++iter) {
if (sum + iter->second <= trim_size) {
sum += iter->second;
trim_values.push_back(iter->first);
}
else
break;
}
size_ -= sum;
sum = 0;
for (typename std::map<value_type, size_t>::reverse_iterator iter = map_.rbegin(); iter != map_.rend(); ++iter) {
if (sum + iter->second <= trim_size) {
sum += iter->second;
trim_values.push_back(iter->first);
}
else
break;
}
size_ -= sum;
size_t trimmed = 0;
for (size_t i = 0; i < trim_values.size(); ++i) {
trimmed += map_[trim_values[i]];
map_.erase(trim_values[i]);
}
return trimmed;
}
size_t TrimLow(value_type threshold) {
std::deque<value_type> trim_values;
size_t sum = 0;
for (typename std::map<value_type, size_t>::iterator iter = map_.begin(); iter != map_.end(); ++iter) {
if (iter->first < threshold) {
sum += iter->second;
trim_values.push_back(iter->first);
}
else
break;
}
size_ -= sum;
size_t trimmed = 0;
for (size_t i = 0; i < trim_values.size(); ++i) {
trimmed += map_[trim_values[i]];
map_.erase(trim_values[i]);
}
return trimmed;
}
void swap(Histgram<value_type> &histgram) {
std::swap(size_, histgram.size_);
map_.swap(histgram.map_);
}
bool empty() const {
return map_.empty();
}
size_t size() const {
return size_;
}
void clear() {
map_.clear();
size_ = 0;
lock_ = 0;
}
private:
std::map<value_type, size_t> map_;
size_t size_;
volatile int lock_;
};
namespace std {
template <typename T> inline void swap(Histgram<T> &histgram1, Histgram<T> &histgram2) {
histgram1.swap(histgram2);
}
}
#endif