Skip to content

Commit e1fedab

Browse files
committed
BFS Dependency solver
1 parent c9d5269 commit e1fedab

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

scripts/lib/util.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,18 @@ export function deepDependencies<T>(node: T, links: Links<T>): Array<T> {
5252
if (is.empty(dependencies)) return []; //base:no-dependency
5353
else return flatten(dependencies.map(eDep => deepDependencies(eDep, links).concat(eDep)));
5454
}
55+
export function flatDependencies<T>(root: T, links: Links<T>): Array<T> {
56+
let bfsQueue: Array<T> = [...links(root)];
57+
let dependencySet: Set<T> = new Set();
58+
while (is.notEmpty(bfsQueue)) {
59+
let someNode = bfsQueue.shift();
60+
if (dependencySet.has(someNode)) {
61+
continue; // skip cyclic deps like a-b
62+
} else {
63+
dependencySet.add(someNode);
64+
let itsDepencencies = links(someNode);
65+
bfsQueue.push(...itsDepencencies);
66+
}
67+
}
68+
return [...dependencySet.values()];
69+
}

scripts/literate_kotlin.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { nextSiblings, treeInsert, schedule, has } from './lib/dom'
22
import { element, configured, withDefaults, withClasses, withAttributes, withText, withInnerHTML } from './lib/dom'
33

4-
import { iterator, preetyShowList, showIfSomeLength, deepDependencies } from './lib/util'
4+
import { iterator, preetyShowList, showIfSomeLength, deepDependencies, flatDependencies, Links } from './lib/util'
55
import { Predicate, negate, or } from './lib/util'
66
import { Peek, peekWhile } from './lib/read'
77
import is from './lib/is_test'
@@ -25,7 +25,8 @@ export const literateKtConfig = {
2525
dependsOn: (deps: Array<string>) => ` depends on ${preetyShowList(deps.map(t => t.bold().italics()))}`,
2626
expectingFor: (what:any, that:any) => `Expecting ${what} for ${that}`,
2727
adjNounDesc: (adj:string, noun:string, desc:string) => `${adj} ${noun}${desc}`
28-
}
28+
},
29+
dependencyOrdered: false
2930
};
3031

3132
////
@@ -116,5 +117,10 @@ function solveDependencies(e_root: Element): Array<Element> {
116117

117118
const linkIds = (e:Element) => e.getAttribute(depend)?.split(dependSeprator) ?? [];
118119
const links = (e:Element) => linkIds(e).map(id => document.getElementById(id));
119-
return deepDependencies(e_root, links);
120+
return dependencySolver<Element>()(e_root, links);
121+
}
122+
function dependencySolver<T>(): (root:T, link:Links<T>) => Array<T> {
123+
const { dependencyOrdered } = literateKtConfig;
124+
125+
return dependencyOrdered? deepDependencies : flatDependencies;
120126
}

0 commit comments

Comments
 (0)