-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBloomFilter.cpp
84 lines (75 loc) · 3.02 KB
/
BloomFilter.cpp
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
/*
* File: BloomFilter.cpp
* Author: Jason
*
* Created on May 31, 2021, 6:43 PM
*/
#include "BloomFilter.h"
/******************************************************************************\
bfSearch
This function will search the bit vector for the event of a positive flag set
\******************************************************************************/
bool BloomFilter::bfSearch(string s)
{
return (bitarray[ELFHash(s) % BFarrSize] && bitarray[APHash(s) % BFarrSize]);
}
/******************************************************************************\
bfPush
This function will push the hashed name to the bitvector setting two flags
\******************************************************************************/
void BloomFilter::bfPush(string s)
{
bitarray[ELFHash(s) % BFarrSize] = true;
bitarray[APHash(s) % BFarrSize] = true;
}
/******************************************************************************\
ELFHash
\******************************************************************************/
unsigned int BloomFilter::ELFHash(const std::string& str)
{
unsigned int hash = 0;
unsigned int x = 0;
for (std::size_t i = 0; i < str.length(); i++)
{
hash = (hash << 4) + str[i];
if ((x = hash & 0xF0000000L) != 0)
{
hash ^= (x >> 24);
}
hash &= ~x;
}
return hash;
}
/******************************************************************************\
APHash
\******************************************************************************/
unsigned int BloomFilter::APHash(const std::string& str)
{
unsigned int hash = 0xAAAAAAAA;
for (std::size_t i = 0; i < str.length(); i++)
{
hash ^= ((i & 1) == 0) ? ((hash << 7) ^ str[i] * (hash >> 3)) :
(~((hash << 11) + (str[i] ^ (hash >> 5))));
}
return hash;
}
/******************************************************************************\
getBloomData
This function populate the bitvector from the binary file save data.
\******************************************************************************/
void BloomFilter::getBloomData()
{
ifstream file ("bloomFilter.bin", ios::binary|ios::in);
file.read(reinterpret_cast<char *>(bitarray), sizeof(bool) * BFarrSize);
file.close();
}
/******************************************************************************\
bfPush
This function will save the bloom filter bitvector to a binary file
\******************************************************************************/
void BloomFilter::pushBloomData()
{
ofstream file ("bloomFilter.bin", ios::binary|ios::out);
file.write(reinterpret_cast<char *>(bitarray), sizeof(bool) * BFarrSize);
file.close();
}