pddl-workspace
Version:
77 lines • 2.2 kB
JavaScript
"use strict";
/* --------------------------------------------------------------------------------------------
* Copyright (c) Jan Dolejsi 2020. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
Object.defineProperty(exports, "__esModule", { value: true });
exports.StringifyingMap = void 0;
/**
* Map that stringifies the key objects in order to leverage
* the javascript native Map and preserve key uniqueness.
*/
class StringifyingMap {
constructor() {
this.map = new Map();
this.keyMap = new Map();
}
has(key) {
const keyString = this.stringifyKey(key);
return this.map.has(keyString);
}
get(key) {
const keyString = this.stringifyKey(key);
return this.map.get(keyString);
}
set(key, value) {
const keyString = this.stringifyKey(key);
this.map.set(keyString, value);
this.keyMap.set(keyString, key);
return this;
}
/**
* Puts new key/value if key is absent.
* @param key key
* @param defaultValue default value factory
*/
putIfAbsent(key, defaultValue) {
if (!this.has(key)) {
const value = defaultValue();
this.set(key, value);
return true;
}
return false;
}
keys() {
return this.keyMap.values();
}
keyList() {
return [...this.keys()];
}
values() {
return this.map.values();
}
valueList() {
return [...this.values()];
}
delete(key) {
const keyString = this.stringifyKey(key);
const flag = this.map.delete(keyString);
this.keyMap.delete(keyString);
return flag;
}
clear() {
this.map.clear();
this.keyMap.clear();
}
size() {
return this.map.size;
}
get length() {
return this.size();
}
forEach(callbackfn, thisArg) {
this.map.forEach(callbackfn, thisArg);
}
}
exports.StringifyingMap = StringifyingMap;
//# sourceMappingURL=StringifyingMap.js.map