parallel.es
Version:
Simple parallelization for EcmaScript
49 lines (48 loc) • 1.6 kB
JavaScript
/**
* A very simple implementation of a map. Do not use with complex objects as Key.
* @param K type of the key
* @param V type of the value
*/
var SimpleMap = (function () {
function SimpleMap() {
this.data = {};
}
/**
* Gets the value for the given key if available
* @param key the key to look up
* @returns the looked up value or undefined if the map does not contain any value associated with the given key
*/
SimpleMap.prototype.get = function (key) {
var internalKey = this.toInternalKey(key);
return this.has(key) ? this.data[internalKey] : undefined;
};
/**
* Tests if the map contains value stored by the given key
* @param key the key
* @returns true if the map contains a value by the given key, false otherwise
*/
SimpleMap.prototype.has = function (key) {
return this.hasOwnProperty.call(this.data, this.toInternalKey(key));
};
/**
* Sets the value for the given key. If the map already contains a value stored by the given key, then this value is
* overridden
* @param key the key
* @param value the value to associate with the given key
*/
SimpleMap.prototype.set = function (key, value) {
this.data[this.toInternalKey(key)] = value;
};
/**
* Clears all values from the map
*/
SimpleMap.prototype.clear = function () {
this.data = {};
};
SimpleMap.prototype.toInternalKey = function (key) {
return "@" + key;
};
return SimpleMap;
}());
exports.SimpleMap = SimpleMap;
;