* Every ancestor of `ids` along `kinds` edges, mapped back to the ids it is an * ancestor of. A level-by-level upward walk over EVERY id at once — one query * per level rather than one per id — up to MAX_OVERRIDE_ANCESTOR_DEPTH.
( cg: CodeGraph, ids: readonly string[], kinds: EdgeKind[] )
| 916 | * per level rather than one per id — up to {@link MAX_OVERRIDE_ANCESTOR_DEPTH}. |
| 917 | */ |
| 918 | function ancestorsOf( |
| 919 | cg: CodeGraph, |
| 920 | ids: readonly string[], |
| 921 | kinds: EdgeKind[] |
| 922 | ): Map<string, Set<string>> { |
| 923 | const reach = new Map<string, Set<string>>(); |
| 924 | const seen = new Map(ids.map((id) => [id, new Set([id])])); |
| 925 | let frontier = ids.map((id) => ({ id, roots: new Set<string>([id]) })); |
| 926 | |
| 927 | for (let depth = 0; depth < MAX_OVERRIDE_ANCESTOR_DEPTH && frontier.length > 0; depth++) { |
| 928 | const rootsOf = new Map(frontier.map((item) => [item.id, item.roots])); |
| 929 | const edges = cg.getOutgoingEdgesFrom( |
| 930 | frontier.map((item) => item.id), |
| 931 | kinds |
| 932 | ); |
| 933 | const next = new Map<string, Set<string>>(); |
| 934 | for (const edge of edges) { |
| 935 | if (edge.target === edge.source) continue; |
| 936 | const roots = rootsOf.get(edge.source); |
| 937 | if (!roots) continue; |
| 938 | const merged = next.get(edge.target) ?? new Set<string>(); |
| 939 | const visited = seen.get(edge.target) ?? new Set<string>(); |
| 940 | const known = reach.get(edge.target) ?? new Set<string>(); |
| 941 | // An ancestor may already be a root. Only skip pairs we have propagated, |
| 942 | // so later descendants still reach all of its ancestors. |
| 943 | for (const root of roots) { |
| 944 | if (visited.has(root)) continue; |
| 945 | visited.add(root); |
| 946 | merged.add(root); |
| 947 | known.add(root); |
| 948 | } |
| 949 | seen.set(edge.target, visited); |
| 950 | if (merged.size > 0) next.set(edge.target, merged); |
| 951 | if (known.size > 0) reach.set(edge.target, known); |
| 952 | } |
| 953 | frontier = []; |
| 954 | for (const [id, roots] of next) { |
| 955 | frontier.push({ id, roots }); |
| 956 | } |
| 957 | } |
| 958 | return reach; |
| 959 | } |
| 960 | |
| 961 | /** |
| 962 | * Which of `nodes` carry a decorator: their own outgoing `decorates` edge, an |