Mercurial > hg > RemoteEditor > REPSessionManager
diff rep/optimizers/DeleteInsertOptimizer.java @ 223:3680d8357429
Optimizer of REPCommand
originated from tkaito
created by kent
author | kent |
---|---|
date | Sun, 31 Aug 2008 13:06:36 +0900 |
parents | |
children | 5b7abc22e61a |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/optimizers/DeleteInsertOptimizer.java Sun Aug 31 13:06:36 2008 +0900 @@ -0,0 +1,78 @@ +package rep.optimizers; + +import java.util.LinkedList; +import java.util.List; + +import rep.REP; +import rep.REPCommand; + +/** + * + * @author Takano + * + */ +public class DeleteInsertOptimizer extends REPCommandOptimizer { + + // optimize メソッドから呼ばれます + protected List<REPCommand> optimize0(List<REPCommand> inp){ + List<REPCommand> output; + output = reverse(inp); + + for(int i = 0; i < output.size(); i++){ + REPCommand r = output.get(i); + switch(r.cmd){ + case REP.REPCMD_INSERT: + break; + case REP.REPCMD_DELETE: + optimizedAdd(output,r,i); + break; + } + } + return reverse(output); + + } + + private List<REPCommand> reverse(List<REPCommand> outp) { + LinkedList<REPCommand> reverse = new LinkedList<REPCommand>(); + for(REPCommand r : outp){ + reverse.addFirst(r); + } + return reverse; + } + private void optimizedAdd(List<REPCommand> output, REPCommand r, int ln) { + int lineno = r.lineno; + int minln = output.size(); + for(int i = ln; 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){ + if(s.lineno < minln){ + minln = s.lineno; + } + lineNumberCorrection(output,minln,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; i < count; i++){ + REPCommand o = opt.get(i); + if(ln < o.lineno) o.lineno -= 1; + } + } + + +}