-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleApproximateMap.hpp
More file actions
41 lines (34 loc) · 990 Bytes
/
SimpleApproximateMap.hpp
File metadata and controls
41 lines (34 loc) · 990 Bytes
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
#pragma once
#include <vector>
template<typename V>
class SimpleApproximateMap {
using key_t = uint64_t;
using epoch_t = uint32_t;
static constexpr size_t SIZE = 1e9;
std::vector<std::tuple<key_t, epoch_t, V>> map;
size_t epoch = 1000;
public:
SimpleApproximateMap() {
map.resize(SIZE);
}
void insert(key_t key, V value) {
map[key % SIZE] = std::make_tuple(key, epoch, value);
}
struct Result {
V *value;
bool isSameEpoch;
};
Result get(key_t key) {
auto &entry = map[key % SIZE];
if (std::get<0>(entry) == key && std::get<1>(entry) > epoch - 1000 - 1) {
return { &std::get<2>(entry), std::get<1>(entry) == epoch };
}
return { nullptr };
}
void clear() {
epoch += 1000;
}
void nextEpoch() {
epoch += 1;
}
};