@loken/hierarchies
Version:
Library for working with hierarchies of identifiers and identifiable objects.
83 lines (82 loc) • 3.92 kB
JavaScript
var __typeError = (msg) => {
throw TypeError(msg);
};
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj)), __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value), __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
var __privateWrapper = (obj, member, setter, getter) => ({
set _(value) {
__privateSet(obj, member, value, setter);
},
get _() {
return __privateGet(obj, member, getter);
}
});
import { LinearStack, LinearQueue, Stack } from "@loken/utilities";
var _visited, _isDepthFirst, _nodes, _depth, _branchCount, _depthCount, _count, _skipped;
class GraphSignal {
//endregion
//#region internal
constructor(options) {
//#region fields
__privateAdd(this, _visited);
__privateAdd(this, _isDepthFirst);
__privateAdd(this, _nodes);
__privateAdd(this, _depth, 0);
__privateAdd(this, _branchCount);
__privateAdd(this, _depthCount, 0);
__privateAdd(this, _count, 0);
__privateAdd(this, _skipped, !1);
(options.detectCycles ?? !1) && __privateSet(this, _visited, /* @__PURE__ */ new Set()), __privateSet(this, _isDepthFirst, options.type === "depth-first"), __privateSet(this, _nodes, __privateGet(this, _isDepthFirst) ? new LinearStack() : new LinearQueue()), __privateSet(this, _depthCount, __privateGet(this, _nodes).attach(options.roots)), __privateGet(this, _isDepthFirst) && (__privateSet(this, _branchCount, new Stack()), __privateGet(this, _branchCount).push(__privateGet(this, _depthCount)));
}
//#endregion
//#region IGraphSignal
get depth() {
return __privateGet(this, _depth);
}
get count() {
return __privateGet(this, _count);
}
next(nodes) {
const count = __privateGet(this, _nodes).attach(nodes);
__privateGet(this, _isDepthFirst) && count > 0 && __privateGet(this, _branchCount).push(count);
}
skip() {
__privateSet(this, _skipped, !0);
}
end() {
__privateGet(this, _nodes).clear();
}
shouldYield() {
return !__privateGet(this, _skipped);
}
cleanup() {
if (__privateGet(this, _isDepthFirst)) {
let res = __privateGet(this, _branchCount).tryPeek();
for (; res[1] && res[0] === 0; )
__privateGet(this, _branchCount).pop(), res = __privateGet(this, _branchCount).tryPeek();
}
__privateGet(this, _skipped) || __privateWrapper(this, _count)._++;
}
tryGetNext() {
if (__privateGet(this, _visited) !== void 0) {
let res = this.tryGetNextInternal();
for (; res[1]; ) {
if (!__privateGet(this, _visited).has(res[0]))
return __privateGet(this, _visited).add(res[0]), res;
res = this.tryGetNextInternal();
}
return res;
} else
return this.tryGetNextInternal();
}
tryGetNextInternal() {
const res = __privateGet(this, _nodes).tryDetach();
return res[1] && (__privateSet(this, _skipped, !1), __privateGet(this, _isDepthFirst) ? (__privateSet(this, _depth, __privateGet(this, _branchCount).count - 1), __privateGet(this, _branchCount).push(__privateGet(this, _branchCount).pop() - 1)) : __privateWrapper(this, _depthCount)._-- == 0 && (__privateWrapper(this, _depth)._++, __privateSet(this, _depthCount, __privateGet(this, _nodes).count))), res;
}
//#endregion
}
_visited = new WeakMap(), _isDepthFirst = new WeakMap(), _nodes = new WeakMap(), _depth = new WeakMap(), _branchCount = new WeakMap(), _depthCount = new WeakMap(), _count = new WeakMap(), _skipped = new WeakMap();
export {
GraphSignal
};
//# sourceMappingURL=graph-signal.js.map