findInstructionPlan

function findInstructionPlan(
    instructionPlan,
    predicate,
): InstructionPlan | undefined;

Finds the first instruction plan in the tree that matches the given predicate.

This function performs a depth-first search through the instruction plan tree, returning the first plan that satisfies the predicate. It checks the root plan first, then recursively searches through nested plans.

Parameters

ParameterTypeDescription
instructionPlanInstructionPlanThe instruction plan tree to search.
predicate(plan) => booleanA function that returns true for the plan to find.

Returns

InstructionPlan | undefined

The first matching instruction plan, or undefined if no match is found.

Examples

Finding a non-divisible sequential plan.

const plan = parallelInstructionPlan([
  sequentialInstructionPlan([instructionA, instructionB]),
  nonDivisibleSequentialInstructionPlan([instructionC, instructionD]),
]);
 
const nonDivisible = findInstructionPlan(
  plan,
  (p) => p.kind === 'sequential' && !p.divisible,
);
// Returns the non-divisible sequential plan containing instructionC and instructionD.

Finding a specific single instruction plan.

const plan = sequentialInstructionPlan([instructionA, instructionB, instructionC]);
 
const found = findInstructionPlan(
  plan,
  (p) => p.kind === 'single' && p.instruction === instructionB,
);
// Returns the SingleInstructionPlan wrapping instructionB.

See

On this page