rc-js-util
Version:
A collection of TS and C++ utilities to help writing performant and correct applications, achieved through strict typing and (removable) invariant checking.
65 lines • 1.78 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DirtyCheckedUniqueCollection = void 0;
/**
* @public
* {@inheritDoc IDirtyCheckedUniqueCollection}
*/
class DirtyCheckedUniqueCollection {
get size() {
return this.itemsSet.size;
}
static mapInitializeAdd(map, key, value) {
const collection = map.get(key);
if (collection == null) {
const collection = new DirtyCheckedUniqueCollection();
collection.add(value);
map.set(key, collection);
return true;
}
else {
return collection.reportingAdd(value);
}
}
constructor(itemsToCopy) {
this.isDirty = true;
this.itemsArray = [];
this.itemsSet = new Set(itemsToCopy);
}
add(item) {
this.itemsSet.add(item);
this.isDirty = true;
}
reportingAdd(item) {
if (this.itemsSet.has(item)) {
return false;
}
this.itemsSet.add(item);
this.isDirty = true;
return true;
}
delete(item) {
const deletedItem = this.itemsSet.delete(item);
this.isDirty || (this.isDirty = deletedItem);
return deletedItem;
}
has(item) {
return this.itemsSet.has(item);
}
clear() {
this.isDirty || (this.isDirty = this.itemsSet.size > 0);
this.itemsSet.clear();
}
getArray() {
if (this.isDirty) {
this.itemsArray = Array.from(this.itemsSet);
this.isDirty = false;
}
return this.itemsArray;
}
getSet() {
return this.itemsSet;
}
}
exports.DirtyCheckedUniqueCollection = DirtyCheckedUniqueCollection;
//# sourceMappingURL=dirty-checked-unique-collection.js.map