Created
December 28, 2011 15:16
-
-
Save methodin/1528293 to your computer and use it in GitHub Desktop.
Hash Table
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 = function() { | |
this.tableSize = 16; | |
this.table = new Array(this.tableSize); | |
this.convert = function(key) { | |
var hash = 0; | |
for (var i=0;i<key.length;i++) hash += key[i].charCodeAt() * (i+1); | |
return Math.abs(hash) % this.tableSize; | |
}; | |
this.set = function(key, data) { | |
var hash = this.convert(key); | |
this.table[hash] = data; | |
}; | |
this.get = function(key) { | |
var hash = this.convert(key); | |
if(this.table[hash] !== undefined) return this.table[hash]; | |
return null; | |
}; | |
}; | |
var key = 'test'; | |
var value = 'result'; | |
var map = new Hash(); | |
map.set(key, value); | |
var result = map.get(key); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment