Mercurial > hg > RemoteEditor > REPSessionManager
comparison 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 |
comparison
equal
deleted
inserted
replaced
222:18d6a7140fa3 | 223:3680d8357429 |
---|---|
1 package rep.optimizers; | |
2 | |
3 import java.util.LinkedList; | |
4 import java.util.List; | |
5 | |
6 import rep.REP; | |
7 import rep.REPCommand; | |
8 | |
9 /** | |
10 * | |
11 * @author Takano | |
12 * | |
13 */ | |
14 public class DeleteInsertOptimizer extends REPCommandOptimizer { | |
15 | |
16 // optimize メソッドから呼ばれます | |
17 protected List<REPCommand> optimize0(List<REPCommand> inp){ | |
18 List<REPCommand> output; | |
19 output = reverse(inp); | |
20 | |
21 for(int i = 0; i < output.size(); i++){ | |
22 REPCommand r = output.get(i); | |
23 switch(r.cmd){ | |
24 case REP.REPCMD_INSERT: | |
25 break; | |
26 case REP.REPCMD_DELETE: | |
27 optimizedAdd(output,r,i); | |
28 break; | |
29 } | |
30 } | |
31 return reverse(output); | |
32 | |
33 } | |
34 | |
35 private List<REPCommand> reverse(List<REPCommand> outp) { | |
36 LinkedList<REPCommand> reverse = new LinkedList<REPCommand>(); | |
37 for(REPCommand r : outp){ | |
38 reverse.addFirst(r); | |
39 } | |
40 return reverse; | |
41 } | |
42 private void optimizedAdd(List<REPCommand> output, REPCommand r, int ln) { | |
43 int lineno = r.lineno; | |
44 int minln = output.size(); | |
45 for(int i = ln; i < output.size(); i++){ | |
46 REPCommand s = output.get(i); | |
47 if(s.cmd==REP.REPCMD_INSERT) { | |
48 if(s.lineno < lineno){ | |
49 lineno --; | |
50 }else if(s.lineno == lineno){ | |
51 if(s.lineno < minln){ | |
52 minln = s.lineno; | |
53 } | |
54 lineNumberCorrection(output,minln,i,ln); | |
55 output.remove(r); | |
56 output.remove(s); | |
57 ln--; | |
58 break; | |
59 } | |
60 }else if(s.cmd==REP.REPCMD_DELETE){ | |
61 if(s.lineno < lineno){ | |
62 lineno ++; | |
63 } | |
64 }else{ | |
65 System.out.println("There are no such commands."); | |
66 } | |
67 } | |
68 } | |
69 | |
70 private void lineNumberCorrection(List<REPCommand> opt, int ln, int count, int r){ | |
71 for(int i = r; i < count; i++){ | |
72 REPCommand o = opt.get(i); | |
73 if(ln < o.lineno) o.lineno -= 1; | |
74 } | |
75 } | |
76 | |
77 | |
78 } |