js-slang
Version:
Javascript-based implementations of Source, written in Typescript
42 lines • 1.16 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
/**
* The heap stores all objects in each environment.
*/
class Heap {
constructor() {
this.storage = null;
}
add(...items) {
this.storage ?? (this.storage = new Set());
for (const item of items) {
this.storage.add(item);
}
}
/** Checks the existence of `item` in the heap. */
contains(item) {
return this.storage?.has(item) ?? false;
}
/** Gets the number of items in the heap. */
size() {
return this.storage?.size ?? 0;
}
/**
* Removes `item` from current heap and adds it to `otherHeap`.
* If the current heap does not contain `item`, nothing happens.
* @returns whether the item transfer is successful
*/
move(item, otherHeap) {
if (!this.contains(item))
return false;
this.storage.delete(item);
otherHeap.add(item);
return true;
}
/** Returns a copy of the heap's contents. */
getHeap() {
return new Set(this.storage);
}
}
exports.default = Heap;
//# sourceMappingURL=heap.js.map
;