forked from dmlc/parameter_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountmin.h
50 lines (45 loc) · 1.15 KB
/
countmin.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
#pragma once
#include "util/sketch.h"
#include <math.h>
#include "util/shared_array_inl.h"
namespace PS {
template <typename K, typename V>
class CountMin : public Sketch {
public:
// TODO prefetch to accelerate the memory access
bool empty() { return n_ == 0; }
void clear() { data_.clear(); n_ = 0; }
void resize(int n, int k, V v_max) {
n_ = std::max(n, 64);
data_.resize(n_);
data_.setZero();
k_ = std::min(30, std::max(1, k));
v_max_ = v_max;
}
void insert(const K& key, const V& count) {
uint32 h = hash(key);
const uint32 delta = (h >> 17) | (h << 15); // Rotate right 17 bits
for (int j = 0; j < k_; ++j) {
V v = data_[h % n_];
// to avoid overflow
data_[h % n_] = count > v_max_ - v ? v_max_ : v + count;
h += delta;
}
}
V query(const K& key) const {
V res = v_max_;
uint32 h = hash(key);
const uint32 delta = (h >> 17) | (h << 15); // Rotate right 17 bits
for (int j = 0; j < k_; ++j) {
res = std::min(res, data_[h % n_]);
h += delta;
}
return res;
}
private:
SArray<V> data_;
int n_ = 0;
int k_ = 1;
V v_max_ = 0;
};
} // namespace PS