Last active
August 29, 2015 14:15
-
-
Save dydx/e7fdec61c7e1ec0f49b2 to your computer and use it in GitHub Desktop.
dumb bloom filter based on a single hash function that adds up the ASCII of a given key string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
bitArray: Array.apply(null, new Array(100)).map(Number.prototype.valueOf, 0), | |
hash: function(key) { | |
return key.split('') | |
.map(function(element) { | |
return element.charCodeAt(0); | |
}) | |
.reduce(function(a, b) { | |
return a + b; | |
}); | |
}, | |
insert: function(key) { | |
// compute the index of the hash of the key, | |
// modulo the size of the array | |
this.bitArray[(this.hash(key) % this.bitArray.length) - 1] = 1; | |
return true; | |
}, | |
search: function(key) { | |
var index = (this.hash(key) % this.bitArray.length) - 1; | |
if(this.bitArray[index] === 1) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var bloom = require('./bloom.js'); | |
var corpus = "the quick brown fox jumps over the lazy dog"; | |
corpus.split(' ') | |
.map(function(element) { | |
bloom.insert(element); | |
}); | |
console.log(bloom.bitArray); | |
console.log(bloom.search('brown')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment