Repository navigation
Expand file tree
/
Copy pathparent-forest.ts
More file actions
57 lines (51 loc) · 2.06 KB
/
Copy pathparent-forest.ts
File metadata and controls
57 lines (51 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
export type OrderedParentIssue =
| { kind: "unknown_child"; childId: string; parentId: string }
| { kind: "unknown_parent"; childId: string; parentId: string }
| { kind: "parent_order"; childId: string; parentId: string };
export function analyzeOrderedParentForest(
orderedIds: readonly string[],
parentIds: Readonly<Record<string, string>>,
): { depths: Map<string, number>; issues: OrderedParentIssue[] } {
const positions = new Map(orderedIds.map((id, index) => [id, index]));
const issues: OrderedParentIssue[] = [];
for (const [childId, parentId] of Object.entries(parentIds)) {
const childPosition = positions.get(childId);
const parentPosition = positions.get(parentId);
if (childPosition === undefined) {
issues.push({ kind: "unknown_child", childId, parentId });
}
if (parentPosition === undefined) {
issues.push({ kind: "unknown_parent", childId, parentId });
} else if (childPosition !== undefined && parentPosition >= childPosition) {
issues.push({ kind: "parent_order", childId, parentId });
}
}
const depths = new Map<string, number>();
for (const id of orderedIds) {
const parentId = parentIds[id];
depths.set(id, parentId && depths.has(parentId) ? depths.get(parentId)! + 1 : 0);
}
return { depths, issues };
}
export function findParentCycles(parentByChild: ReadonlyMap<string, string>): string[][] {
const complete = new Set<string>();
const cycles: string[][] = [];
for (const startId of parentByChild.keys()) {
if (complete.has(startId)) continue;
const path: string[] = [];
const positions = new Map<string, number>();
let currentId: string | undefined = startId;
while (currentId && !complete.has(currentId)) {
const cycleStart = positions.get(currentId);
if (cycleStart !== undefined) {
cycles.push([...path.slice(cycleStart), currentId]);
break;
}
positions.set(currentId, path.length);
path.push(currentId);
currentId = parentByChild.get(currentId);
}
path.forEach((id) => complete.add(id));
}
return cycles;
}