Skip to content

Commit

Permalink
feat(dgraph): add leaves() & roots() iterators, update sort()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed May 9, 2018
1 parent 91a2b74 commit 68ca46d
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/dgraph/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ export class DGraph<T> implements
);
}

leaves(): IterableIterator<T> {
return filter((node: T) => this.isLeaf(node), this.nodes());
}

roots(): IterableIterator<T> {
return filter((node: T) => this.isRoot(node), this.nodes());
}

transitiveDependencies(x: T) {
return transitive(this.dependencies, x);
}
Expand All @@ -108,14 +116,14 @@ export class DGraph<T> implements
sort() {
const sorted: T[] = [];
const g = this.copy();
let queue = new LLSet(filter((node: T) => g.isLeaf(node), g.nodes()));
let queue = new LLSet(g.leaves());
while (true) {
if (!queue.size) {
return sorted.reverse();
}
const node = queue.first();
queue.delete(node);
for (let d of (<LLSet<T>>g.immediateDependencies(node)).copy()) {
for (let d of [...g.immediateDependencies(node)]) {
g.removeEdge(node, d);
if (g.isLeaf(d)) {
queue.add(d);
Expand Down

0 comments on commit 68ca46d

Please sign in to comment.