Exemplo n.º 1
0
        /// <summary>結果出力</summary>
        private static IList <DiffResult> PresentDiffSwap(CommonSubsequence cs, string dataA, string dataB)
        {
            List <DiffResult> list = new List <DiffResult>();
            int originalStart = 0, modifiedStart = 0;

            while (true)
            {
                if (originalStart < cs.StartB ||
                    modifiedStart < cs.StartA)
                {
                    DiffResult d = new DiffResult(
                        originalStart, cs.StartB - originalStart,
                        dataA.Substring(modifiedStart, cs.StartA - modifiedStart));
                    list.Add(d);
                }

                // 末尾検出
                if (cs.Length == 0)
                {
                    break;
                }

                originalStart = cs.StartB;
                modifiedStart = cs.StartA;

                originalStart += cs.Length;
                modifiedStart += cs.Length;

                cs = cs.Next;
            }
            return(CompressDiff(list, dataB, dataA));
        }
Exemplo n.º 2
0
 /// <summary>
 /// ☆山内:引数付きコンストラクタ
 /// </summary>
 /// <param name="startA"></param>
 /// <param name="startB"></param>
 /// <param name="length"></param>
 /// <param name="next"></param>
 public CommonSubsequence(int startA, int startB, int length, CommonSubsequence next)
 {
     this.startA_ = startA;
     this.startB_ = startB;
     this.length_ = length;
     this.Next    = next;
 }
Exemplo n.º 3
0
        private void SearchSnake(int k)
        {
            int kk = this.dataA.Length + 1 + k;
            CommonSubsequence previousCS = null;
            int posA = 0, posB = 0;

            int lk = kk - 1;
            int rk = kk + 1;

            // 論文のfp[n]は-1始まりだが、0始まりのほうが初期化の都合がよいため、
            // +1のゲタを履かせる。fpから読む際は-1し、書く際は+1する。
            int lb = this.fp[lk].posB;
            int rb = this.fp[rk].posB - 1;

            //Debug.Write( "fp[" + string.Format( "{0,2}", k ) + "]=Snake( " + string.Format( "{0,2}", k )
            //    + ", max( fp[" + string.Format( "{0,2}", ( k - 1 ) ) + "]+1= " + string.Format( "{0,2}", lb )
            //    + ", fp[" + string.Format( "{0,2}", ( k + 1 ) ) + "]= " + string.Format( "{0,2}", rb ) + " ))," );

            if (lb > rb)
            {
                posB       = lb;
                previousCS = this.fp[lk].CS;
            }
            else
            {
                posB       = rb;
                previousCS = this.fp[rk].CS;
            }
            posA = posB - k;

            int startA = posA;
            int startB = posB;

            //Debug.Write( "(x: " + string.Format( "{0,2}", startA ) + ", y: " + string.Format( "{0,2}", startB ) + " )" );

            while ((posA < this.dataA.Length) &&
                   (posB < this.dataB.Length) &&
                   this.isSame(posA, posB))
            {
                posA++;
                posB++;
            }

            if (startA != posA)
            {
                this.fp[kk].CS = new CommonSubsequence(startA, startB, posA - startA, previousCS);
            }
            else
            {
                this.fp[kk].CS = previousCS;
            }
            this.fp[kk].posB = posB + 1; // fpへ+1して書く。論文のfpに+1のゲタを履かせる。

            //Debug.WriteLine( "= " + string.Format( "{0,2}", posB ) );
        }
Exemplo n.º 4
0
            /// <summary>リンクリスト反転</summary>
            public static CommonSubsequence Reverse(CommonSubsequence old)
            {
                CommonSubsequence newTop = null;

                while (old != null)
                {
                    CommonSubsequence next = old.Next;
                    old.Next = newTop;
                    newTop   = old;
                    old      = next;
                }
                return(newTop);
            }
Exemplo n.º 5
0
        /// <summary>結果出力</summary>
        private static DiffResult[] PresentDiffSwap(CommonSubsequence cs, bool wantCommon)
        {
            List <DiffResult> list = new List <DiffResult>();
            int originalStart = 0, modifiedStart = 0;

            while (true)
            {
                if (originalStart < cs.StartB ||
                    modifiedStart < cs.StartA)
                {
                    DiffResult d = new DiffResult(
                        true,
                        originalStart, cs.StartB - originalStart,
                        modifiedStart, cs.StartA - modifiedStart);
                    list.Add(d);
                }

                // 末尾検出
                if (cs.Length == 0)
                {
                    break;
                }

                originalStart = cs.StartB;
                modifiedStart = cs.StartA;

                if (wantCommon)
                {
                    DiffResult d = new DiffResult(
                        false,
                        originalStart, cs.Length,
                        modifiedStart, cs.Length);
                    list.Add(d);
                }
                originalStart += cs.Length;
                modifiedStart += cs.Length;

                cs = cs.Next;
            }
            return(list.ToArray());
        }
Exemplo n.º 6
0
        private DiffResult[] DetectDiff()
        {
            Debug.Assert(this.dataA.Length <= this.dataB.Length);

            this.fp = new Snake[this.dataA.Length + this.dataB.Length + 3];
            int d = this.dataB.Length - this.dataA.Length;
            int p = 0;

            do
            {
                //Debug.Unindent();
                //Debug.WriteLine( "p:" + p );
                //Debug.Indent();

                for (int k = -p; k < d; k++)
                {
                    SearchSnake(k);
                }

                for (int k = d + p; k >= d; k--)
                {
                    SearchSnake(k);
                }

                p++;
            }while (this.fp[this.dataB.Length + 1].posB != (this.dataB.Length + 1));

            // 末尾検出用のCommonSubsequence
            CommonSubsequence endCS  = new CommonSubsequence(this.dataA.Length, this.dataB.Length, 0, this.fp[this.dataB.Length + 1].CS);
            CommonSubsequence result = CommonSubsequence.Reverse(endCS);

            if (this.isSwap)
            {
                return(PresentDiffSwap(result, true));
            }
            else
            {
                return(PresentDiff(result, true));
            }
        }
Exemplo n.º 7
0
 public static CommonSubsequence Reverse(CommonSubsequence old)
 {
     CommonSubsequence newTop = null;
     while (old != null) {
         CommonSubsequence next = old.Next;
         old.Next = newTop;
         newTop = old;
         old = next;
     }
     return newTop;
 }
Exemplo n.º 8
0
 public CommonSubsequence(int startA, int startB, int length, CommonSubsequence next)
 {
     this.startA_ = startA;
     this.startB_ = startB;
     this.length_ = length;
     this.Next = next;
 }
Exemplo n.º 9
0
        private static DiffResult[] PresentDiffSwap(CommonSubsequence cs, bool wantCommon)
        {
            List<DiffResult> list = new List<DiffResult>();
            int originalStart = 0, modifiedStart = 0;

            while (true) {
                if (originalStart < cs.StartB
                    || modifiedStart < cs.StartA) {
                    DiffResult d = new DiffResult(
                        true,
                        originalStart, cs.StartB - originalStart,
                        modifiedStart, cs.StartA - modifiedStart);
                    list.Add(d);
                }

                // 末尾検出
                if (cs.Length == 0) break;

                originalStart = cs.StartB;
                modifiedStart = cs.StartA;

                if (wantCommon) {
                    DiffResult d = new DiffResult(
                        false,
                        originalStart, cs.Length,
                        modifiedStart, cs.Length);
                    list.Add(d);
                }
                originalStart += cs.Length;
                modifiedStart += cs.Length;

                cs = cs.Next;
            }
            return list.ToArray();
        }
Exemplo n.º 10
0
        private DiffResult[] DetectDiff()
        {
            Debug.Assert(this.dataA.Length <= this.dataB.Length);

            this.fp = new Snake[this.dataA.Length + this.dataB.Length + 3];
            int d = this.dataB.Length - this.dataA.Length;
            int p = 0;
            do {
                //Debug.Unindent();
                //Debug.WriteLine( "p:" + p );
                //Debug.Indent();

                for (int k = -p; k < d; k++)
                    SearchSnake(k);

                for (int k = d + p; k >= d; k--)
                    SearchSnake(k);

                p++;
            }
            while (this.fp[this.dataB.Length + 1].posB != (this.dataB.Length + 1));

            // 末尾検出用のCS
            CommonSubsequence endCS = new CommonSubsequence(this.dataA.Length, this.dataB.Length, 0, this.fp[this.dataB.Length + 1].CS);
            CommonSubsequence result = CommonSubsequence.Reverse(endCS);

            if (this.isSwap)
                return PresentDiffSwap(result, true);
            else
                return PresentDiff(result, true);
        }