-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbloom_filter_test.cpp
More file actions
80 lines (68 loc) · 2.15 KB
/
bloom_filter_test.cpp
File metadata and controls
80 lines (68 loc) · 2.15 KB
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
// clang++ -std=c++17 -o bloom_filter_test bloom_filter_test.cpp
//
// ./bloom_filter_test
#include <bitset>
#include <iomanip>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>
class RuthBloomFilter {
public:
// 128 bits total, managed as a single bitset
static constexpr size_t M = 128;
static constexpr size_t K = 3;
void add(std::string_view item) {
for (size_t i = 0; i < K; ++i) {
bits.set(generate_hash(item, i));
}
}
bool exists(std::string_view item) const {
for (size_t i = 0; i < K; ++i) {
if (!bits.test(generate_hash(item, i)))
return false;
}
return true;
}
private:
std::bitset<M> bits;
// Simple Jenkins-style hash with seed to create k-functions
size_t generate_hash(std::string_view item, size_t seed) const {
size_t h = seed + 0x9e3779b9;
for (char c : item) {
h ^= static_cast<size_t>(c) + 0x9e3779b9 + (h << 6) + (h >> 2);
}
return h % M;
}
};
int main() {
RuthBloomFilter harvest;
// Items and characters added to the harvest set
std::vector<std::string> additions = {
"Naomi", // The mother-in-law
"Boaz", // The kinsman redeemer
"Barley-Sheaves", // The crop being harvested
"Winnowing-Floor", // Where the grain is separated
"Ephah-of-Barley", // The measure Ruth gathered
"Ten-Elders" // Witnesses at the gate
};
std::cout << "--- Seeding the Ruth Bloom Filter (The Harvest) ---\n";
for (const auto &item : additions) {
harvest.add(item);
std::cout << "Gleaning: " << item << "\n";
}
std::cout << "\n--- Testing Membership in the Harvest ---\n";
std::vector<std::string> tests = {
"Boaz", // Should be present
"Naomi", // Should be present
"Orpah", // Absent (she stayed in Moab)
"Moab", // Absent (the land they left)
"Barley-Sheaves" // Should be present
};
for (const auto &test : tests) {
bool result = harvest.exists(test);
std::cout << std::left << std::setw(18) << test << " : "
<< (result ? "[MAYBE PRESENT]" : "[DEFINITELY ABSENT]") << "\n";
}
return 0;
}