@eagleoutice/flowr
Version:
Static Dataflow Analyzer and Program Slicer for the R Programming Language
65 lines • 2.01 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultMap = void 0;
/**
* A default map allows for a generator producing values automatically if you want to add something to a map that does not have a value associated with a given key.
* This does not implement the default map interface as return types (and some future methods may) change
*/
class DefaultMap {
/** the internal map the default map wraps around */
internal;
/** generator function to produce a default value for a given key */
generator;
/**
* @param generator - the generator to produce a default value for a given key
* @param map - the initial map to start with
*/
constructor(generator, map = new Map()) {
this.generator = generator;
this.internal = map;
}
/**
* Sets a value for a given key.
* As you provide value, this does not invoke the generator!
*/
set(k, v) {
this.internal.set(k, v);
return this;
}
/**
* Return a value for the given key, if the key does not exist within the default map,
* this will invoke the generator and assign the produced value.
*/
get(k) {
const potential = this.internal.get(k);
if (potential !== undefined) {
return potential;
}
else {
const defaultValue = this.generator(k);
this.internal.set(k, defaultValue);
return defaultValue;
}
}
/**
* Iterates over all entries that have been set (explicitly or by the generator)
*/
entries() {
return this.internal.entries();
}
/** returns only the keys really stored in the map */
keys() {
return this.internal.keys();
}
values() {
return this.internal.values();
}
delete(k) {
return this.internal.delete(k);
}
size() {
return this.internal.size;
}
}
exports.DefaultMap = DefaultMap;
//# sourceMappingURL=defaultmap.js.map