Last active
October 13, 2016 12:24
-
-
Save okram/7bb2512935f8955551f9e3f87623b488 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class IdentityRemovalStrategy extends AbstractTraversalStrategy<TraversalStrategy.OptimizationStrategy> implements TraversalStrategy.OptimizationStrategy { | |
private static final IdentityRemovalStrategy INSTANCE = new IdentityRemovalStrategy(); | |
private IdentityRemovalStrategy() { | |
} | |
@Override | |
public void apply(final Traversal.Admin<?, ?> traversal) { | |
if (traversal.getSteps().size() <= 1) | |
return; | |
for (final IdentityStep<?> identityStep : TraversalHelper.getStepsOfClass(IdentityStep.class, traversal)) { | |
if (identityStep.getLabels().isEmpty() || !(identityStep.getPreviousStep() instanceof EmptyStep)) { | |
TraversalHelper.copyLabels(identityStep, identityStep.getPreviousStep(), false); | |
traversal.removeStep(identityStep); | |
} | |
} | |
} | |
@Override | |
public void apply(final Bytecode bytecode) { | |
if (bytecode.getStepInstructions().size() <= 1) | |
return; | |
for (final Instruction identityInstruction : BytecodeHelper.getInstructionsOfOperator("identity", bytecode)) { | |
if (!identityInstruction.getNextInstruction().getOperator().equals("as") || !(identityInstruction.getPreviousInstruction() instanceof NoOp)) { | |
BytecodeHelper.removeInstruction(identityInstruction, bytecode); | |
} | |
} | |
} | |
public static IdentityRemovalStrategy instance() { | |
return INSTANCE; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IdentityRemoveStrategy(TraversalStrategy): | |
def __init__(self): | |
TraversalStrategy.__init__(self) | |
def apply(obj): | |
return __apply_traversal(obj) if isinstance(obj,traversal) else __apply_bytecode(obj) | |
def __apply_traversal(traversal): | |
return | |
def __apply_bytecode(bytecode): | |
if len(bytecode.step_instructions) <= 1: | |
return | |
for inst in BytecodeHelper.get_instructions_of_operator("identity", bytecode): | |
if inst.next_instruction().operator is not "as" or inst.prev_instruction() is not NoOp: | |
BytecodeHelper.remove_instruction(inst, bytecode) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment