Skip to content

Commit c5d9f20

Browse files
committed
add or check as you go, instead of putting hashes in an array
1 parent 52d759e commit c5d9f20

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

bloom.js

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,43 +37,30 @@ JSBloom.filter = function(items, target_prob) {
3737
};
3838

3939
return (hash >>> 0) % BUFFER_LEN;
40-
},
41-
getIndices: function(str) {
42-
43-
var hashes = [];
44-
45-
hashes.push(this.djb2(str));
46-
hashes.push(this.sdbm(str));
47-
48-
for (var round = 2; round <= HASH_ROUNDS; round++) {
49-
var new_hash = (hashes[0] + (round * hashes[1]) + (round^2)) % BUFFER_LEN;
50-
51-
hashes.push(new_hash);
52-
};
53-
54-
return hashes;
55-
5640
}
5741
}
5842

5943
addEntry = function(str) {
6044

61-
var indices = hashes.getIndices(str);
62-
63-
for (var i = indices.length - 1; i >= 0; i--) {
45+
var h1 = hashes.djb2(str)
46+
var h2 = hashes.sdbm(str)
47+
for (var round = 0; round <= HASH_ROUNDS; round++) {
48+
var new_hash = round == 0 ? h1
49+
: round == 1 ? h2
50+
: (h1 + (round * h2) + (round^2)) % BUFFER_LEN;
6451

65-
var extra_indices = indices[i] % 8,
66-
index = ((indices[i] - extra_indices) / 8);
52+
var extra_indices = new_hash % 8,
53+
index = ((new_hash - extra_indices) / 8);
6754

68-
if (extra_indices != 0 && (bVector[index] & (128 >> (extra_indices - 1))) == 0) {
69-
bVector[index] ^= (128 >> extra_indices - 1);
70-
} else if (extra_indices == 0 && (bVector[index] & 1) == 0) {
71-
bVector[index] ^= 1;
72-
}
55+
if (extra_indices != 0 && (bVector[index] & (128 >> (extra_indices - 1))) == 0) {
56+
bVector[index] ^= (128 >> extra_indices - 1);
57+
} else if (extra_indices == 0 && (bVector[index] & 1) == 0) {
58+
bVector[index] ^= 1;
59+
}
7360

74-
};
61+
};
7562

76-
return true;
63+
return true;
7764
}
7865

7966
addEntries = function(arr) {
@@ -87,12 +74,32 @@ JSBloom.filter = function(items, target_prob) {
8774
}
8875

8976
checkEntry = function(str) {
77+
var index, extra_indices
78+
var h1 = hashes.djb2(str)
79+
80+
extra_indices = h1 % 8;
81+
index = ((h1 - extra_indices) / 8);
9082

91-
var indices = hashes.getIndices(str);
83+
if (extra_indices != 0 && (bVector[index] & (128 >> (extra_indices - 1))) == 0) {
84+
return false;
85+
} else if (extra_indices == 0 && (bVector[index] & 1) == 0) {
86+
return false;
87+
}
88+
89+
var h2 = hashes.sdbm(str)
90+
extra_indices = h2 % 8;
91+
index = ((h2 - extra_indices) / 8);
9292

93-
for (var i = indices.length - 1; i >= 0; i--) {
94-
var extra_indices = indices[i] % 8,
95-
index = (indices[i] - extra_indices) / 8;
93+
if (extra_indices != 0 && (bVector[index] & (128 >> (extra_indices - 1))) == 0) {
94+
return false;
95+
} else if (extra_indices == 0 && (bVector[index] & 1) == 0) {
96+
return false;
97+
}
98+
99+
for (var round = 2; round <= HASH_ROUNDS; round++) {
100+
var new_hash = round==0?h1:round==1?h2:(h1 + (round * h2) + (round^2)) % BUFFER_LEN;
101+
var extra_indices = new_hash % 8,
102+
index = ((new_hash - extra_indices) / 8);
96103

97104
if (extra_indices != 0 && (bVector[index] & (128 >> (extra_indices - 1))) == 0) {
98105
return false;
@@ -148,4 +155,3 @@ JSBloom.filter = function(items, target_prob) {
148155
if (typeof exports !== "undefined") {
149156
exports.filter = JSBloom.filter;
150157
};
151-

0 commit comments

Comments
 (0)