Skip to content

Instantly share code, notes, and snippets.

@okram
Last active October 13, 2016 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okram/7bb2512935f8955551f9e3f87623b488 to your computer and use it in GitHub Desktop.
Save okram/7bb2512935f8955551f9e3f87623b488 to your computer and use it in GitHub Desktop.
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;
}
}
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