Skip to content

Instantly share code, notes, and snippets.

@dydx
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dydx/e7fdec61c7e1ec0f49b2 to your computer and use it in GitHub Desktop.
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.
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;
}
}
};
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