@loken/hierarchies
Version:
Library for working with hierarchies of identifiers and identifiable objects.
34 lines (33 loc) • 1.27 kB
JavaScript
import { LinearStack, LinearQueue } from "@loken/utilities";
const searchGraph = (options) => {
const visited = options.detectCycles ? /* @__PURE__ */ new Set() : void 0, store = options.type === "depth-first" ? new LinearStack() : new LinearQueue();
store.attach(options.roots);
const nextFn = options.next;
for (; store.count > 0; ) {
const node = store.detach();
if (visited != null && visited.has(node))
continue;
if (visited == null || visited.add(node), options.search(node))
return node;
const children = nextFn(node);
children && store.attach(children);
}
}, searchGraphMany = (options) => {
const result = [], visited = options.detectCycles ? /* @__PURE__ */ new Set() : void 0, store = options.type === "depth-first" ? new LinearStack() : new LinearQueue();
store.attach(options.roots);
const nextFn = options.next;
for (; store.count > 0; ) {
const node = store.detach();
if (visited != null && visited.has(node))
continue;
visited == null || visited.add(node), options.search(node) && result.push(node);
const children = nextFn(node);
children && store.attach(children);
}
return result;
};
export {
searchGraph,
searchGraphMany
};
//# sourceMappingURL=graph-search.js.map