Last active
December 15, 2015 10:39
-
-
Save danshearmur/5247158 to your computer and use it in GitHub Desktop.
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 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