Created
January 17, 2015 11:45
-
-
Save webjay/8430c0fdedd9be0a7946 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
'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