# HG changeset patch # User kent # Date 1198394115 -32400 # Node ID c59b0886061c7bf4c75f40b60953512fb0b5e4d0 # Parent 522c6bd9b11b1edaa6237b534d03af31219c65ca translater diff -r 522c6bd9b11b -r c59b0886061c src/sample/merge/TranslaterImp1.java --- a/src/sample/merge/TranslaterImp1.java Sun Dec 23 16:14:06 2007 +0900 +++ b/src/sample/merge/TranslaterImp1.java Sun Dec 23 16:15:15 2007 +0900 @@ -1,7 +1,6 @@ package sample.merge; import java.util.ArrayList; -import java.util.Collection; import java.util.LinkedList; import java.util.Stack; @@ -15,6 +14,7 @@ //private LinkedList unMergedCmds; private Stack unMergedCmds; public int eid; + private int seq; public TranslaterImp1(int _eid){ eid = _eid; @@ -28,8 +28,10 @@ * @param cmd * @return translated command. */ - public REPCommand[] transSendCmd(REPCommand cmd){ + synchronized public REPCommand[] transSendCmd(REPCommand cmd){ REPCommand[] cmds = new REPCommand[1]; + setCmdState(cmd); + cmds[0] = cmd; sentCmds.add(cmd); unMergedCmds.push(cmd); return cmds; @@ -38,28 +40,40 @@ * Dequeue command cmd that was returned. * @param cmd */ - public REPCommand[] catchOwnCommand(REPCommand cmd){ + synchronized public REPCommand[] catchOwnCommand(REPCommand cmd){ ArrayList returnCmds = new ArrayList(); ArrayList cmds = new ArrayList(); // ringである以上、戻ってきたコマンドは確実にキューsentCmdsの先頭にある事を期待している - //REPCommand cmd0 = sentCmds.poll(); - //assert cmd0.seq!=cmd.seq; + REPCommand tmp = sentCmds.poll(); + assert tmp.seq==cmd.seq; assert cmd.eid==eid; /* スタックから全部取り出してマージする まだマージできないやつはまたスタックへ戻す */ + /* スタックから全部取り出す */ while ( !unMergedCmds.isEmpty() ){ REPCommand cmd0 = unMergedCmds.pop(); returnCmds.add( createUndo(cmd0) ); cmds.add(cmd0); } + + /* 必要な分だけソートして返却用のリストに追加 */ //if (cmds.size()==0) return null; returnCmds.addAll( sortCmds(cmds) ); + + /* 残ったコマンドも再び実行させるが、まだマージされてないのでunMergedにも入れる */ + for(int i=0; i sortCmds(ArrayList cmds) { ArrayList sortedCmds = new ArrayList(); - REPCommand cmd0; + int top; int prevEid=0; int i,j; - /* EID,SEQの低い順に、各EID毎に1個だけ選択しcmdsに格納 */ - while ( null != (cmd0=getPrecedence(cmds, prevEid+1)) ){ - sortedCmds.add(cmd0); - cmds.remove(cmd0); - prevEid = cmd0.eid; + /* + * EID,SEQの低い順に、各EID毎に1個だけ選択しsortedCmdsに格納 + * cmdsにはまだデータが残る + */ + while ( -1 != (top=getPrecedence(cmds, prevEid+1)) ){ + REPCommand tmp = cmds.remove(top); + sortedCmds.add(tmp); + prevEid = tmp.eid; } /* lineno の大きい順にソート */ @@ -89,18 +106,18 @@ /* deleteとinsertの場合などはeidによらず、順序を強制する必要があるかも */ k=j; } - cmd0 = sortedCmds.get(i); + REPCommand tmp = sortedCmds.get(i); sortedCmds.set(i, sortedCmds.get(k)); - sortedCmds.set(k, cmd0); + sortedCmds.set(k, tmp); } return sortedCmds; } /* search cmd. ordering by min EID that is lower lowEid and min SEQ. */ - private REPCommand getPrecedence(ArrayList cmds, int lowEid) { + private int getPrecedence(ArrayList cmds, int lowEid) { int cEid, cSeq; cEid=cSeq=Integer.MAX_VALUE; - REPCommand ret=null; + int ret=-1; for (int i=0; icSeq ) continue; cSeq=tmp.seq; - ret = tmp; + ret = i; } else { /* tmp.eidcmds.get(i) && cmds.get(i)>lowEid ) } @@ -124,7 +141,7 @@ * @param cmd the command to be translated. * @return translated commannd. */ - public REPCommand[] transReceiveCmd(REPCommand cmd){ + synchronized public REPCommand[] transReceiveCmd(REPCommand cmd){ int i=0; REPCommand cmds[]; if (cmd.eid==eid){ @@ -138,7 +155,7 @@ if ( sentCmds.size()