158
|
1 package pathfinder.mergetest.test;
|
|
2
|
|
3 import java.util.LinkedList;
|
|
4
|
|
5 import pathfinder.mergetest.Text;
|
|
6
|
|
7
|
|
8 import remoteeditor.command.REPCommand;
|
|
9 import remoteeditor.network.REP;
|
|
10
|
|
11
|
|
12
|
|
13 public class RepCommandOptimizeTest {
|
|
14 //テストコマンド (command,string,lineno,id)
|
|
15 static String[] test1 = {
|
|
16 Integer.toString(REP.REPCMD_INSERT),"TEST0","3","1",
|
|
17 Integer.toString(REP.REPCMD_INSERT),"TEST1","3","2",
|
|
18 Integer.toString(REP.REPCMD_INSERT),"TEST2","3","3"
|
|
19
|
|
20 };
|
|
21
|
159
|
22 //String[] test2 = new String[test1.length];
|
158
|
23
|
|
24 static LinkedList<REPCommand> cmdlist = new LinkedList<REPCommand>();
|
|
25 Text text = new Text();
|
|
26
|
|
27 //REPコマンドの生成
|
|
28 void makeCommand(String[] str){
|
|
29 int seq = 0;
|
|
30 try{
|
|
31 for( int i = 0;i < str.length; i+=4){
|
|
32 int cmd = Integer.parseInt(str[i]);
|
|
33 int lineno = Integer.parseInt(str[i+2]);
|
|
34
|
|
35 int sid = Integer.parseInt(str[i+3]);
|
|
36 int eid = sid;
|
|
37 cmdlist.add(new REPCommand(cmd, sid, eid, seq++, lineno, str[i+1].length(), str[i+1]));
|
|
38 }
|
|
39 }catch(Exception e){
|
|
40 e.printStackTrace();
|
|
41 }
|
|
42 }
|
|
43 //生成したコマンドのリストを表示
|
|
44 void printCmdList(){
|
|
45 System.out.println("---------- CmdList ----------");
|
|
46 for(REPCommand r: cmdlist){
|
|
47 System.out.println(r.toString());
|
|
48 }
|
|
49 }
|
|
50
|
|
51 void edit(){
|
|
52 for(REPCommand r : cmdlist){
|
|
53 text.edit(r);
|
|
54 }
|
|
55 }
|
|
56
|
|
57 void printText(){
|
|
58 System.out.println("------------ Text -----------");
|
|
59 text.printAllText();
|
|
60 }
|
159
|
61 LinkedList<REPCommand> optimize(LinkedList<REPCommand> input){
|
|
62 LinkedList<REPCommand> output = new LinkedList<REPCommand>();
|
|
63 for(REPCommand r : input){
|
|
64 optimizedAdd(output,r);
|
|
65 }
|
|
66
|
|
67 return reverse(output);
|
|
68
|
|
69 }
|
|
70 private LinkedList<REPCommand> reverse(LinkedList<REPCommand> outp) {
|
|
71 LinkedList<REPCommand> reverse = new LinkedList<REPCommand>();
|
|
72 for(REPCommand r : outp){
|
|
73 reverse.addFirst(r);
|
|
74 }
|
|
75 return reverse;
|
|
76 }
|
158
|
77
|
159
|
78 private void optimizedAdd(LinkedList<REPCommand> output, REPCommand r) {
|
|
79 switch(r.cmd){
|
|
80 case REP.REPCMD_INSERT:
|
|
81 optimizedAddInsert(output,r);break;
|
|
82 case REP.REPCMD_DELETE:
|
|
83 optimizedAddDelete(output,r);break;
|
|
84 }
|
|
85
|
|
86 }
|
|
87 private void optimizedAddDelete(LinkedList<REPCommand> output, REPCommand r) {
|
|
88 for(REPCommand s : output){
|
|
89 switch(r.cmd){
|
|
90 case REP.REPCMD_INSERT:
|
|
91 break;
|
|
92 case REP.REPCMD_DELETE:
|
|
93 break;
|
|
94 }
|
|
95 }
|
|
96
|
|
97 }
|
|
98 private void optimizedAddInsert(LinkedList<REPCommand> output, REPCommand r) {
|
|
99 int lineno = r.lineno;
|
|
100 for(int i = 0; i < output.size(); i++){
|
|
101 REPCommand s = output.get(i);
|
|
102 switch(s.cmd){
|
|
103 case REP.REPCMD_INSERT:
|
|
104 break;
|
|
105 case REP.REPCMD_DELETE:
|
|
106 if(s.lineno == lineno){
|
|
107 output.remove(s);
|
|
108 }
|
|
109 break;
|
|
110 }
|
|
111 }
|
|
112 }
|
158
|
113 public static void main(String[] s){
|
|
114
|
|
115 RepCommandOptimizeTest rco = new RepCommandOptimizeTest();
|
|
116 rco.makeCommand(test1);
|
|
117 java.util.Collections.shuffle(cmdlist);
|
|
118 rco.printCmdList();
|
|
119 rco.edit();
|
|
120 rco.printText();
|
|
121 //java.util.Collections.shuffle(cmdlist);
|
|
122 for ( int i = 0; i < 10; ++i ) {
|
|
123 System.out.println(cmdlist.get(i));
|
|
124 }
|
|
125 }
|
|
126 }
|