Mercurial > hg > RemoteEditor > REPSessionManager
view rep/optimizers/DeleteInsertOptimizer.java @ 368:335f685ac658
*** empty log message ***
author | tkaito |
---|---|
date | Tue, 21 Oct 2008 20:11:09 +0900 |
parents | 6ea3aa6c795f |
children |
line wrap: on
line source
package rep.optimizers; import java.util.LinkedList; import java.util.List; import rep.REP; import rep.REPCommand; /** * * @author Tagano * */ public class DeleteInsertOptimizer implements REPCommandOptimizer { public List<REPCommand> optimize(List<REPCommand> cmdlist){ List<REPCommand> output = new LinkedList<REPCommand>(); for (REPCommand cmd: cmdlist){ if (cmd.cmd==REP.REPCMD_NOP) continue; output.add(0,new REPCommand(cmd)); } for(int i = 0; i < output.size(); i++){ REPCommand r = output.get(i); switch(r.cmd){ case REPCMD_INSERT: break; case REPCMD_DELETE: optimizedAdd(output,r,i); break; case REPCMD_NOP: output.remove(r); i--; break; } } java.util.Collections.reverse(output); return output; } private void optimizedAdd(List<REPCommand> output, REPCommand r, int ln) { int lineno = r.lineno; for(int i = ln+1; i < output.size(); i++){ REPCommand s = output.get(i); if(s.cmd==REP.REPCMD_INSERT) { if(s.lineno <= lineno){ lineno --; }else if(s.lineno == lineno){ lineNumberCorrection(output,s.lineno,i,ln); output.remove(r); output.remove(s); ln--; break; } }else if(s.cmd==REP.REPCMD_DELETE){ if(s.lineno <= lineno){ lineno ++; } }else{ System.out.println("There are no such commands."); } } } private void lineNumberCorrection(List<REPCommand> opt, int ln, int count, int r){ for(int i = r+1; i < count; i++){ REPCommand o = opt.get(i); if(o.lineno == 0){ }else if(ln <= o.lineno){ o.lineno -= 1; } } } }