Skip to content

Instantly share code, notes, and snippets.

@Joopmicroop
Created April 8, 2018 17:56
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 Joopmicroop/e38f7204acc481c03062b73abeab517e to your computer and use it in GitHub Desktop.
Save Joopmicroop/e38f7204acc481c03062b73abeab517e to your computer and use it in GitHub Desktop.
// writen but untested
function HashTable(obj){
this.length = 0;
this.items = { };
for(var p in obj){
if(obj.hasOwnProperty(p)){
this.items[p] = obj[p];
this.length++;
}
}
return {
setItem: function(k,v){
if(this.items.hasOwnProperty(k)) return this.items[k];
this.items[k] = v;
},
getItem: function(k){
if(this.items.hasOwnProperty(k)) return this.items[k];
},
hasItem: function(k){
return this.items.hasOwnProperty(k);
},
removeItem: function(k){
if(this.items.hasOwnProperty(k)){
var prev = this.items[k];
delete this.items[k];
this.length--;
return prev;
}
},
keys: function(){
return Object.keys(this.items);
},
values: function(){
var vals = [];
for(var i in Object.keys(this.items)) vals.push(this.items[i]);
return vals;
},
clear: function(){
this.items = { };
this.length = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment