Created
September 11, 2012 15:09
-
-
Save egermano/3699524 to your computer and use it in GitHub Desktop.
Hash Tables
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
function HashTable(size) { | |
this.size = size; | |
this.buckets = new Array(size); | |
this.add = function(value) { | |
var index = this.hash(value); | |
this.buckets[index] = value; | |
}; | |
this.hash = function(value) { | |
var sum = 0; | |
for (var i = 0; i < value.length; ++i) { | |
sum += value[i].charCodeAt() - 97; | |
} | |
return sum % this.size; | |
}; | |
} |
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 hash = new HashTable(3); | |
hash.add('Germano'); | |
hash.add('Bruno'); | |
hash.add('Andrade'); | |
console.log(hash); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment