27#include <initializer_list>
42 std::vector<std::size_t>
data;
47 explicit Bitset(std::size_t);
49 void add(std::size_t);
73 if (blockIndex >=
data.size()) {
74 data.resize(blockIndex + 1);
88 if (blockIndex >=
data.size()) {
102 std::vector<std::function<std::size_t(T)>>
107 std::initializer_list<std::function<std::size_t(T)>>);
123 std::initializer_list<std::function<std::size_t(T)>> funks)
124 : set(size), hashFunks(funks) {}
135 for (std::size_t i = 0; i < hashFunks.size(); i++) {
136 set.
add(hashFunks[i](x) % (
sizeof(std::size_t) * set.
size()));
150 for (std::size_t i = 0; i < hashFunks.size(); i++) {
152 (
sizeof(std::size_t) * set.
size())) ==
false) {
167 std::size_t hash = 5381;
169 hash = ((hash << 5) + hash) + c;
182static std::size_t
hashStr(std::string
const& s) {
183 std::size_t hash = 37;
184 std::size_t primeNum1 = 54059;
185 std::size_t primeNum2 = 76963;
187 hash = (hash * primeNum1) ^ (c * primeNum2);
200 x = ((x >> 16) ^ x) * 0x45d9f3b;
201 x = ((x >> 16) ^ x) * 0x45d9f3b;
214 auto y =
static_cast<std::size_t
>(x);
215 y = (y ^ (y >> 30)) *
static_cast<std::size_t
>(0xbf58476d1ce4e5b9);
216 y = (y ^ (y >> 27)) *
static_cast<std::size_t
>(0x94d049bb133111eb);
229 std::vector<std::string> toCheck{
"hello",
"world",
"!"};
230 std::vector<std::string> toFalse{
"false",
"world2",
"!!!"};
231 for (
const auto& x : toCheck) {
234 for (
const auto& x : toFalse) {
235 assert(filter.contains(x) ==
false);
237 for (
const auto& x : toCheck) {
238 assert(filter.contains(x));
249 std::vector<int> toCheck{100, 200, 300, 50};
250 std::vector<int> toFalse{1, 2, 3, 4, 5, 6, 7, 8};
251 for (
int x : toCheck) {
254 for (
int x : toFalse) {
255 assert(filter.contains(x) ==
false);
257 for (
int x : toCheck) {
258 assert(filter.contains(x));
269 std::vector<std::size_t> toCheck{0, 1, 5, 8, 63, 64, 67, 127};
270 for (
auto x : toCheck) {
289 std::cout <<
"All tests have successfully passed!\n";
static void test_bloom_filter_int()
Test for bloom filter with int as generic type.
static void test_bitset()
Test for bitset.
static void test_bloom_filter_string()
Test for bloom filter with string as generic type.
Simple bitset implementation for bloom filter.
Bitset(std::size_t)
BitSet class constructor.
void add(std::size_t)
Turn bit on position x to 1s.
std::size_t size()
Utility function to return the size of the inner array.
bool contains(std::size_t)
Doest bitset contains element x.
static const std::size_t blockSize
std::vector< std::size_t > data
short info of this variable
Bloom filter template class.
bool contains(T)
Check element function for Bloom filter.
std::vector< std::function< std::size_t(T)> > hashFunks
hash functions for T type
void add(T)
Add function for Bloom filter.
BloomFilter(std::size_t, std::initializer_list< std::function< std::size_t(T)> >)
Constructor for Bloom filter.
Bitset set
inner bitset for elements
static std::size_t hashDJB2(std::string const &s)
Function djb2 to get hash for the given string.
static std::size_t hashStr(std::string const &s)
Hash function, to get hash for the given string.
std::size_t hashInt_2(int x)
Hash function for test
std::size_t hashInt_1(int x)
Hash function for test