Skip to content

Commit d13cb2e

Browse files
author
Carey Li
authored
Merge pull request cry#3 from dominictarr/if-added
feature: addEntry returns whether the item was added
2 parents e5fb8e5 + 50231ff commit d13cb2e

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

bloom.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ JSBloom.filter = function(items, target_prob) {
4444

4545
var h1 = hashes.djb2(str)
4646
var h2 = hashes.sdbm(str)
47+
var added = false
4748
for (var round = 0; round <= HASH_ROUNDS; round++) {
4849
var new_hash = round == 0 ? h1
4950
: round == 1 ? h2
@@ -54,13 +55,15 @@ JSBloom.filter = function(items, target_prob) {
5455

5556
if (extra_indices != 0 && (bVector[index] & (128 >> (extra_indices - 1))) == 0) {
5657
bVector[index] ^= (128 >> extra_indices - 1);
58+
added = true;
5759
} else if (extra_indices == 0 && (bVector[index] & 1) == 0) {
5860
bVector[index] ^= 1;
61+
added = true;
5962
}
6063

6164
};
6265

63-
return true;
66+
return added;
6467
}
6568

6669
addEntries = function(arr) {

test/test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ describe('JSBloom - Bloom Filter', function() {
6363

6464
});
6565

66+
describe('Duplicate Insertion', function() {
67+
it('should return true for 2000 added elements', function() {
68+
for (var i = test_entries.length - 1; i >= 0; i--) {
69+
assert.equal(filter.addEntry(test_entries[i]), false);
70+
};
71+
});
72+
73+
});
74+
75+
6676
describe('Non-Existence', function() {
6777
it('should return false for 1000 non elements', function() {
6878
var positive = 0;

0 commit comments

Comments
 (0)