Skip to content

Instantly share code, notes, and snippets.

@webjay
Created January 17, 2015 11:45
Show Gist options
  • Save webjay/8430c0fdedd9be0a7946 to your computer and use it in GitHub Desktop.
Save webjay/8430c0fdedd9be0a7946 to your computer and use it in GitHub Desktop.
'use strict';
module.exports = HashTable;
function HashTable () {
this.index = [];
this.table = [];
}
HashTable.prototype = {
constructor: HashTable,
set: function (id, key, value) {
var index = this.index.indexOf(id);
if (index === -1) {
index = this.index.length;
this.index.push(id);
this.table[index] = {};
}
this.table[index][key] = value;
},
get: function (id, key) {
var index = this.index.indexOf(id);
if (index === -1) {
return undefined;
}
return this.table[index][key];
},
getIdsWhere: function (key, value) {
var self = this;
return this.index.filter(function (id, index) {
return (key in self.table[index] && self.table[index][key] === value);
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment