private void handleStepOutOfLoop( @NotNull final Instruction prevInstruction, @NotNull Instruction nextInstruction, @NotNull final int[] loopNumber, @NotNull MultiMap<BranchingInstruction, DfaMemoryState> processedStates, @NotNull MultiMap<BranchingInstruction, DfaMemoryState> incomingStates, @NotNull List<DfaInstructionState> inFlightStates, @NotNull DfaInstructionState[] afterStates, @NotNull StateQueue queue) { if (loopNumber[prevInstruction.getIndex()] == 0 || inSameLoop(prevInstruction, nextInstruction, loopNumber)) { return; } // stepped out of loop. destroy all memory states from the loop, we don't need them anymore // but do not touch yet states being handled right now for (DfaInstructionState state : inFlightStates) { Instruction instruction = state.getInstruction(); if (inSameLoop(prevInstruction, instruction, loopNumber)) { return; } } for (DfaInstructionState state : afterStates) { Instruction instruction = state.getInstruction(); if (inSameLoop(prevInstruction, instruction, loopNumber)) { return; } } // and still in queue if (!queue.processAll( new Processor<DfaInstructionState>() { @Override public boolean process(DfaInstructionState state) { Instruction instruction = state.getInstruction(); return !inSameLoop(prevInstruction, instruction, loopNumber); } })) return; // now remove obsolete memory states final Set<BranchingInstruction> mayRemoveStatesFor = new THashSet<BranchingInstruction>(); for (Instruction instruction : myInstructions) { if (inSameLoop(prevInstruction, instruction, loopNumber) && instruction instanceof BranchingInstruction) { mayRemoveStatesFor.add((BranchingInstruction) instruction); } } for (Instruction instruction : mayRemoveStatesFor) { processedStates.remove((BranchingInstruction) instruction); incomingStates.remove((BranchingInstruction) instruction); } }