package com.volmit.host; import com.volmit.resource.ResourceId; import com.volmit.stage.Pipeline; import com.volmit.stage.StageKind; import java.util.ArrayList; import java.util.BitSet; import java.util.HashMap; import java.util.List; import java.util.Objects; /** Immutable, validated order of one pipeline, resolved once per pack load. Adjacent block * stages form one fused block pass; any invocation stage between them splits the pass, even * when that stage is inactive for a particular invocation. Execution never sorts or hashes. */ public final class ResolvedGraph { private final Pipeline pipeline; private final List stages; private final List executables; private final List> settingsTypes; private final List segments; ResolvedGraph( Pipeline pipeline, List stages, List executables, List> settingsTypes) { this.pipeline = Objects.requireNonNull(pipeline, "pipeline"); this.stages = List.copyOf(stages); this.executables = List.copyOf(executables); this.settingsTypes = List.copyOf(settingsTypes); segments = segments(this.executables); } public Pipeline pipeline() { return pipeline; } /** Stages in execution order. */ public List stages() { return stages; } /** Each fused block pass as stage ids in execution order. */ public List> blockPasses() { return segments.stream() .filter(segment -> segment.kind() == StageKind.BLOCK) .map(segment -> stages.subList(segment.start(), segment.end()).stream() .map(stage -> stage.stage().id()) .toList()) .toList(); } /** Pairs with no path between them in either direction, first stage earlier in the order. * Their relative order comes only from the deterministic tie-break, so a pack author should * check that they do not conflict. */ public List unorderedPairs() { var reachable = reachability(); var pairs = new ArrayList(); for (int first = 0; first < stages.size(); first++) { for (int second = first + 1; second < stages.size(); second++) { if (!reachable[first].get(second)) { pairs.add(new UnorderedPair(id(first), id(second))); } } } return pairs; } /** The most the pipeline's stages write beyond their footprint, overall and per mantle layer. * * @throws IllegalStateException when a mantle stage works out its reach when the pack loads: the host * runs each mantle stage as its own pass and asks it with {@link ResolvedStage#reachBlocks} */ public PipelineReach reach() { return PipelineReach.of(stages); } /** Human-readable order, owners, edges, block passes and unordered pairs. */ public String describe() { return GraphDescription.describe(this); } int size() { return executables.size(); } ExecutableStage executable(int position) { return executables.get(position); } List segments() { return segments; } List> settingsTypes() { return settingsTypes; } private ResourceId id(int position) { return stages.get(position).stage().id(); } /** Edges always point forward in the order, so a reverse sweep completes each stage's * reachable set before any predecessor merges it. */ private BitSet[] reachability() { var positions = new HashMap(); var reachable = new BitSet[stages.size()]; for (int position = 0; position < stages.size(); position++) { positions.put(id(position), position); reachable[position] = new BitSet(); } for (int position = stages.size() - 1; position >= 0; position--) { for (var edge : stages.get(position).incoming()) { int predecessor = positions.get(edge.from()); reachable[predecessor].set(position); reachable[predecessor].or(reachable[position]); } } return reachable; } private static List segments(List executables) { var segments = new ArrayList(); int position = 0; while (position < executables.size()) { int end = segmentEnd(executables, position); segments.add(new Segment(executables.get(position).stage().kind(), position, end)); position = end; } return List.copyOf(segments); } private static int segmentEnd(List executables, int start) { if (executables.get(start).stage().kind() != StageKind.BLOCK) { return start + 1; } int end = start + 1; while (end < executables.size() && executables.get(end).stage().kind() == StageKind.BLOCK) { end++; } return end; } public record UnorderedPair(ResourceId first, ResourceId second) { public UnorderedPair { Objects.requireNonNull(first, "first"); Objects.requireNonNull(second, "second"); } } }