Skip to content

Instantly share code, notes, and snippets.

@danshearmur
Last active December 15, 2015 10:39
Show Gist options
  • Save danshearmur/5247158 to your computer and use it in GitHub Desktop.
Save danshearmur/5247158 to your computer and use it in GitHub Desktop.
function HashMap() { this.data = []; }
HashMap.prototype.hash = function (val) {
var i = 0;
var d = ('' + val).split('');
var l = d.length;
var t = 0;
while(i < l) {
t += d[i].charCodeAt();
i++;
}
return t;
}
HashMap.prototype.add = function (key, val) {
if (this.data[this.hash(key)] == null) this.data[this.hash(key)] = [key, val];
else this.data[this.hash(key)].push(key, val);
}
HashMap.prototype.get = function (key) {
var hash = this.hash(key);
if (this.data[hash] == null) return null;
var list = this.data[hash];
var i = 0;
var l = list.length;
while (i < l) {
if (list[i] === key) {
return list[i + 1];
}
i += 2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment