User:Brooke Vibber/Gadget-ParserPlayground-HashMap.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Brooke Vibber/Gadget-ParserPlayground-HashMap. |
/**
* Very primitive hashmap class that allows using objects as keys;
* JSON flattening of the key object is used as a hash code, so only
* suitable for objects that will be immutable for now.
*
* Actual final comparison is done using object identity, but the
* bucket match is from the JSON, so don't mess around!
*
* Used to map parse tree nodes to output nodes for the inspector mode.
*/
function HashMap() {
this.keyBuckets = {};
this.valBuckets = {};
}
/**
* @param {object} keyObj
* @return {object} original object, or null if no match found.
*/
HashMap.prototype.get = function(keyObj) {
var key = this.hash(keyObj);
if (typeof this.keyBuckets[key] !== 'undefined') {
var keys = this.keyBuckets[key],
max = keys.length;
for (var i = 0; i < max; i++) {
if (keyObj === keys[i]) {
return this.valBuckets[key][i];
}
}
}
return null;
};
/**
* @param {object} keyObj
* @param {object} val
*/
HashMap.prototype.put = function(keyObj, val) {
var key = this.hash(keyObj);
if (typeof this.keyBuckets[key] === 'undefined') {
this.keyBuckets[key] = [];
this.valBuckets[key] = [];
}
this.keyBuckets[key].push(keyObj);
this.valBuckets[key].push(val);
};
/**
* This will do for us for now. :)
*/
HashMap.prototype.hash = function(keyObj) {
return JSON.stringify(keyObj).substr(0, 40);
};