Пример #1
0
        private void OpRemove(int i, int j, int cost)
        {
            var scriptRemoveOpened = this._forestDist[i - 1, j]._editScript as EditScriptRemoveOpened;

            if (!(scriptRemoveOpened is EditScriptRemoveOpened))
            {
                scriptRemoveOpened = new EditScriptRemoveOpened(i, this._forestDist[i - 1, j]._editScript.GetClosedScript(i - 1, j));
            }
            this._forestDist[i, j]._editScript = scriptRemoveOpened;
            this._forestDist[i, j]._cost       = cost;
        }
Пример #2
0
        private void ComputeTreeDistance(int sourcePos, int targetPos)
        {
            var left1 = this._sourceNodes[sourcePos].Left;
            var left2 = this._targetNodes[targetPos].Left;
            var editScriptAddOpened = new EditScriptAddOpened(left2, EmptyEditScript);
            var scriptRemoveOpened  = new EditScriptRemoveOpened(left1, EmptyEditScript);

            this._forestDist[left1 - 1, left2 - 1]._cost       = 0;
            this._forestDist[left1 - 1, left2 - 1]._editScript = EmptyEditScript;
            for (var index = left1; index <= sourcePos; ++index)
            {
                this._forestDist[index, left2 - 1]._cost       = (index - left1 + 1) * MinimalTreeDistanceAlgo.OperationCost[2];
                this._forestDist[index, left2 - 1]._editScript = scriptRemoveOpened;
            }
            for (var index = left2; index <= targetPos; ++index)
            {
                this._forestDist[left1 - 1, index]._cost       = (index - left2 + 1) * MinimalTreeDistanceAlgo.OperationCost[1];
                this._forestDist[left1 - 1, index]._editScript = editScriptAddOpened;
            }
            for (var index1 = left1; index1 <= sourcePos; ++index1)
            {
                for (var index2 = left2; index2 <= targetPos; ++index2)
                {
                    var left3 = this._sourceNodes[index1].Left;
                    var left4 = this._targetNodes[index2].Left;
                    var cost1 = this._forestDist[index1 - 1, index2]._cost + MinimalTreeDistanceAlgo.OperationCost[2];
                    var cost2 = this._forestDist[index1, index2 - 1]._cost + MinimalTreeDistanceAlgo.OperationCost[1];
                    if (left3 == left1 && left4 == left2)
                    {
                        var diffOperation = this._sourceNodes[index1].GetDiffOperation(this._targetNodes[index2], this._xmlDiff);
                        if (diffOperation == XmlDiffOperation.Match)
                        {
                            this.OpNodesMatch(index1, index2);
                        }
                        else
                        {
                            var cost3 = this._forestDist[index1 - 1, index2 - 1]._cost + MinimalTreeDistanceAlgo.OperationCost[(int)diffOperation];
                            if (cost3 < cost2)
                            {
                                if (cost3 < cost1)
                                {
                                    this.OpChange(index1, index2, diffOperation, cost3);
                                }
                                else
                                {
                                    this.OpRemove(index1, index2, cost1);
                                }
                            }
                            else if (cost2 < cost1)
                            {
                                this.OpAdd(index1, index2, cost2);
                            }
                            else
                            {
                                this.OpRemove(index1, index2, cost1);
                            }
                        }
                        this._treeDist[index1, index2]._cost       = this._forestDist[index1, index2]._cost;
                        this._treeDist[index1, index2]._editScript = this._forestDist[index1, index2]._editScript.GetClosedScript(index1, index2);
                    }
                    else
                    {
                        var m = left3 - 1;
                        var n = left4 - 1;
                        if (m < left1 - 1)
                        {
                            m = left1 - 1;
                        }
                        if (n < left2 - 1)
                        {
                            n = left2 - 1;
                        }
                        var num = this._forestDist[m, n]._cost + this._treeDist[index1, index2]._cost;
                        if (num < cost2)
                        {
                            if (num < cost1)
                            {
                                if (this._treeDist[index1, index2]._editScript == MinimalTreeDistanceAlgo.EmptyEditScript)
                                {
                                    this.OpCopyScript(index1, index2, m, n);
                                }
                                else
                                {
                                    this.OpConcatScripts(index1, index2, m, n);
                                }
                            }
                            else
                            {
                                this.OpRemove(index1, index2, cost1);
                            }
                        }
                        else if (cost2 < cost1)
                        {
                            this.OpAdd(index1, index2, cost2);
                        }
                        else
                        {
                            this.OpRemove(index1, index2, cost1);
                        }
                    }
                }
            }
        }