Skip to content

Instantly share code, notes, and snippets.

@asiletto
Created June 17, 2015 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asiletto/0ac9ea2ce51f542401fe to your computer and use it in GitHub Desktop.
Save asiletto/0ac9ea2ce51f542401fe to your computer and use it in GitHub Desktop.
define(function() {
HashMap = function() {
this._dict = [];
}
HashMap.prototype._get = function(key) {
for (var i = 0, couplet; couplet = this._dict[i]; i++) {
if (couplet[0] === key) {
return couplet;
}
}
}
HashMap.prototype.put = function(key, value) {
var couplet = this._get(key);
if (couplet) {
couplet[1] = value;
} else {
this._dict.push([ key, value ]);
}
return this; // for chaining
}
HashMap.prototype.get = function(key) {
var couplet = this._get(key);
if (couplet) {
return couplet[1];
}
}
HashMap.prototype.getKeys = function(){
var ret = [];
for (var i = 0; couplet = this._dict[i]; i++) {
ret.push(this._dict[i][0]);
}
return ret;
}
return HashMap;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment