forked from bbondy/bloom-filter-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BloomFilter.cpp
109 lines (95 loc) · 3.31 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* Copyright (c) 2015 Brian R. Bondy. Distributed under the MPL2 license.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <string.h>
#include "BloomFilter.h"
BloomFilter::BloomFilter(unsigned int bitsPerElement,
unsigned int estimatedNumElements, HashFn *hashFns, int numHashFns) :
hashFns(nullptr), numHashFns(0), byteBufferSize(0), buffer(nullptr) {
this->hashFns = hashFns;
this->numHashFns = numHashFns;
lastHashes = new uint64_t[numHashFns];
byteBufferSize = bitsPerElement * estimatedNumElements / 8 + 1;
bitBufferSize = byteBufferSize * 8;
buffer = new char[byteBufferSize];
memset(buffer, 0, byteBufferSize);
}
// Constructs a BloomFilter by copying the specified buffer and number of bytes
BloomFilter::BloomFilter(const char *buffer, int byteBufferSize,
HashFn *hashFns, int numHashFns) :
hashFns(nullptr), numHashFns(0), byteBufferSize(0), buffer(nullptr) {
this->hashFns = hashFns;
this->numHashFns = numHashFns;
lastHashes = new uint64_t[numHashFns];
this->byteBufferSize = byteBufferSize;
bitBufferSize = byteBufferSize * 8;
this->buffer = new char[byteBufferSize];
memcpy(this->buffer, buffer, byteBufferSize);
}
BloomFilter::~BloomFilter() {
if (buffer) {
delete[] buffer;
}
if (lastHashes) {
delete[] lastHashes;
}
}
void BloomFilter::setBit(unsigned int bitLocation) {
buffer[bitLocation / 8] |= 1 << bitLocation % 8;
}
bool BloomFilter::isBitSet(unsigned int bitLocation) {
return !!(buffer[bitLocation / 8] & 1 << bitLocation % 8);
}
void BloomFilter::add(const char *input, int len) {
for (int j = 0; j < numHashFns; j++) {
setBit(hashFns[j](input, len) % bitBufferSize);
}
}
void BloomFilter::add(const char *sz) {
add(sz, static_cast<int>(strlen(sz)));
}
bool BloomFilter::exists(const char *input, int len) {
bool allSet = true;
for (int j = 0; j < numHashFns; j++) {
allSet = allSet && isBitSet(hashFns[j](input, len) % bitBufferSize);
}
return allSet;
}
bool BloomFilter::exists(const char *sz) {
return exists(sz, static_cast<int>(strlen(sz)));
}
void BloomFilter::getHashesForCharCodes(const char *input, int inputLen,
uint64_t *lastHashes, uint64_t *newHashes, unsigned char lastCharCode) {
for (int i = 0; i < numHashFns; i++) {
if (lastHashes) {
*(newHashes + i) = hashFns[i](input, inputLen,
lastCharCode, *(lastHashes+i));
} else {
*(newHashes + i) = hashFns[i](input, inputLen);
}
}
}
bool BloomFilter::substringExists(const char *data, int dataLen,
int substringLength) {
unsigned char lastCharCode = 0;
for (int i = 0; i < dataLen - substringLength + 1; i++) {
getHashesForCharCodes(data + i, substringLength, i == 0
? nullptr : lastHashes, lastHashes, lastCharCode);
bool allSet = true;
for (int j = 0; j < numHashFns; j++) {
allSet = allSet && isBitSet(lastHashes[j] % bitBufferSize);
}
if (allSet) {
return true;
}
lastCharCode = data[i];
}
return false;
}
bool BloomFilter::substringExists(const char *data, int substringLength) {
return substringExists(data, static_cast<int>(strlen(data)), substringLength);
}
void BloomFilter::clear() {
memset(buffer, 0, byteBufferSize);
}