Skip to content

Instantly share code, notes, and snippets.

@methodin
Created December 28, 2011 15:16
Show Gist options
  • Save methodin/1528293 to your computer and use it in GitHub Desktop.
Save methodin/1528293 to your computer and use it in GitHub Desktop.
Hash Table
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