/// <summary>
        /// Check if the values match and outputs a set of granular differences
        /// </summary>
        /// <param name="diffObject"></param>
        /// <param name="diffsForSelf"></param>
        /// <param name="diffsForArgument"></param>
        /// <returns></returns>
        public bool ValueMatches(IDiffObject diffObject,
                                 out IDiffObjectsCollection diffsForSelf,
                                 out IDiffObjectsCollection diffsForArgument,
                                 out IDiffObjectsCollection commonElements)
        {
            HeaderDiffObject otherObject = diffObject as HeaderDiffObject;

            diffsForSelf     = null;
            diffsForArgument = null;
            commonElements   = null;

            bool matches = false;

            if (otherObject != null)
            {
                matches = _headerLineHash == otherObject.HeaderLineHash;
                //if the values are different but the headers are the same
                if (!matches && String.Compare(_name, otherObject.Name, true) == 0)
                {
                    WordsDiffer valuesDiffer = new WordsDiffer();
                    valuesDiffer.AddTask(_values);
                    valuesDiffer.AddTask(otherObject.Values);
                    valuesDiffer.Properties.Sorted = true;
                    valuesDiffer.Properties.CaseInSensitiveSort = true;
                    double diffRatio = valuesDiffer.DoDiff(0, 1);

                    diffsForSelf     = valuesDiffer.GetResultingDifferences(0);
                    diffsForArgument = valuesDiffer.GetResultingDifferences(1);
                    commonElements   = valuesDiffer.GetResultingCommonElements(0);
                    matches          = diffRatio >= _valuesMinSimilarity;
                }
            }
            return(matches);
        }
        /// <summary>
        /// Checks if the values match, are similar
        /// </summary>
        /// <param name="diffObject"></param>
        /// <returns></returns>
        public bool ValueMatches(IDiffObject diffObject)
        {
            IDiffObjectsCollection diffsForSelf;
            IDiffObjectsCollection diffsForArg;
            IDiffObjectsCollection common;

            return(ValueMatches(diffObject, out diffsForSelf, out diffsForArg, out common));
        }
Example #3
0
 public void ImportDiff(IDiffObject diffElement)
 {
     if (diffElement != null)
     {
         _start  = (int)diffElement.Position;
         _length = diffElement.Length;
         _color  = DEFAULT_DIFF_COL;
         _type   = RtfHighlightType.Foreground;
     }
 }
Example #4
0
        public bool ValueEquals(IDiffObject diffObject)
        {
            bool res = false;
            BaseTextDiffObject other = diffObject as BaseTextDiffObject;

            if (other != null)
            {
                res = _valueHash == other.ValueHash;
            }
            return(res);
        }
        /// <summary>
        /// Verifies if the headers are exactly equal
        /// </summary>
        /// <param name="diffObject"></param>
        /// <returns></returns>
        public bool ValueEquals(IDiffObject diffObject)
        {
            bool             res         = false;
            HeaderDiffObject otherObject = diffObject as HeaderDiffObject;

            if (otherObject != null)
            {
                res = _headerLineHash == otherObject.HeaderLineHash;
            }
            return(res);
        }
        /// <summary>
        /// Compares the values
        /// </summary>
        /// <param name="diffObject"></param>
        /// <param name="ignoreCase"></param>
        /// <returns></returns>
        public CompareResult CompareValues(IDiffObject diffObject, bool ignoreCase)
        {
            HeaderDiffObject other = diffObject as HeaderDiffObject;
            CompareResult    res   = CompareResult.Greater;

            if (other != null)
            {
                string otherString = other.ToString();
                res = (CompareResult)String.Compare(_headerLine, otherString);
            }
            return(res);
        }
        public void Add(IDiffObject obj)
        {
            if (obj != null)
            {
                //clone the object so we don't modify objects that are being themselves diffed
                IDiffObject clone = obj.Clone();

                int n = Items.Count;
                if (!_allowMerges || n == 0 || !Items[n - 1].Merge(clone))
                {
                    //if the collection is empty or if we couldn't merge the last element to the new object
                    Items.Add(clone);
                }
            }
        }
Example #8
0
        public bool ValueMatches(IDiffObject diffObject)
        {
            BaseTextDiffObject otherObject = diffObject as BaseTextDiffObject;
            bool equals = false;

            if (otherObject != null)
            {
                equals = this.ValueHash == otherObject.ValueHash;

                if (!equals && SimilarityFactor < 1)
                {
                    equals = ASESimilarityAlgorithm.CalculateSimilarity(this.Value, otherObject.Value) >= this.SimilarityFactor;
                }
            }
            return(equals);
        }
Example #9
0
        /// <summary>
        /// Compares the current element against the given argument
        /// </summary>
        /// <param name="diffObject"></param>
        /// <param name="ignoreCase"></param>
        /// <returns></returns>
        public CompareResult CompareValues(IDiffObject diffObject, bool ignoreCase)
        {
            BaseTextDiffObject otherObject = diffObject as BaseTextDiffObject;
            CompareResult      result;

            if (diffObject != null)
            {
                result = (CompareResult)String.Compare(_value, otherObject.Value, ignoreCase);
            }
            else
            {
                throw new ArgumentException("Invalid comparison argument. Has to be of type TextDiffObject or not null");
            }

            return(result);
        }
Example #10
0
        public bool Merge(IDiffObject objectToMergeWith)
        {
            bool success = false;

            BaseTextDiffObject otherObject = objectToMergeWith as BaseTextDiffObject;

            if (otherObject != null)
            {
                if (_position + _length == objectToMergeWith.Position)
                {
                    _value += otherObject.Value;
                    _length = _value.Length;
                    success = true;
                }
            }

            return(success);
        }
        public void Push(IDiffObject obj)
        {
            if (obj != null)
            {
                //clone the object so we don't modify objects that are being themselves diffed
                IDiffObject clone = obj.Clone();

                if (_allowMerges && Items.Count > 0 && clone.Merge(Items[0]))
                {
                    Items[0] = clone;
                }
                else
                {
                    //there are no elements in the collection or we couldn't merge with the first
                    Items.Insert(0, clone);
                }
            }
        }
Example #12
0
        /// <summary>
        /// Checks is the value is equal. If not where applicable a collection of granular differences is outputed
        /// </summary>
        /// <param name="diffObject"></param>
        /// <param name="diffsForSelf"></param>
        /// <param name="diffsForArgument"></param>
        /// <param name="commonElements"></param>
        /// <returns></returns>
        public bool ValueMatches(IDiffObject diffObject, out IDiffObjectsCollection diffsForSelf, out IDiffObjectsCollection diffsForArgument, out IDiffObjectsCollection commonElements)
        {
            BaseTextDiffObject otherObject = diffObject as BaseTextDiffObject;

            diffsForSelf     = null;
            diffsForArgument = null;
            commonElements   = null;
            bool matches = false;

            if (otherObject != null)
            {
                matches = this.ValueHash == otherObject.ValueHash;

                if (!matches)
                {
                    double simFactor = ASESimilarityAlgorithm.CalculateSimilarity(this.Value, otherObject.Value);

                    LettersDiffer granularDiffer = InitGranularDiffer();

                    if (simFactor > 0 && granularDiffer != null)
                    {
                        //calculate granular differences
                        granularDiffer.AddTask(Value, Position);                         //imports the current word into a letters collection starting from the position of the current element
                        granularDiffer.AddTask(otherObject.Value, otherObject.Position);

                        double diffRatio = granularDiffer.DoDiff(0, 1);

                        diffsForSelf     = granularDiffer.GetResultingDifferences(0);
                        diffsForArgument = granularDiffer.GetResultingDifferences(1);
                        commonElements   = granularDiffer.GetResultingCommonElements(0);

                        matches = diffRatio >= this.SimilarityFactor;
                    }
                }
            }

            return(matches);
        }
Example #13
0
 /// <summary>
 /// Creates an object storing information about a highlighted portion of text from
 /// a diff elements
 /// </summary>
 /// <param name="diffElement"></param>
 public RtfHighlight(IDiffObject diffElement)
 {
     ImportDiff(diffElement);
 }
Example #14
0
        private double ExtractDiffs(
            ushort[, ] C, IDiffObjectsCollection first, IDiffObjectsCollection second, int start, int m, int n)
        {
            int i = m - 1, j = n - 1;

            double diffCount1 = 0;
            double diffCount2 = 0;

            IDiffObjectsCollection currFirstDiffs     = first.Differences;
            IDiffObjectsCollection currSecondDiffs    = second.Differences;
            IDiffObjectsCollection currCommonElements = first.CommonElements;

            second.CommonElements = currCommonElements;

            do
            {
                int         iIndex     = start + i - 1;
                int         jIndex     = start + j - 1;
                IDiffObject currFirst  = null;
                IDiffObject currSecond = null;


                IDiffObjectsCollection granularDiffsForFirst  = null;
                IDiffObjectsCollection granularDiffsForSecond = null;
                IDiffObjectsCollection granularCommonElements = null;


                if (iIndex >= 0 && iIndex < first.Count)
                {
                    currFirst = first[iIndex];
                }

                if (jIndex >= 0 && jIndex < second.Count)
                {
                    currSecond = second[jIndex];
                }


                if (currFirst != null &&
                    currFirst.ValueMatches(currSecond, out granularDiffsForFirst, out granularDiffsForSecond, out granularCommonElements))
                {
                    //ValuesMatch can return true if the objects are very similar
                    //In that case we need to capture granular differences
                    currFirstDiffs.PushRange(granularDiffsForFirst);
                    currSecondDiffs.PushRange(granularDiffsForSecond);


                    if (granularDiffsForFirst == null && granularDiffsForSecond == null)
                    {
                        currCommonElements.Add(currFirst);
                    }
                    else
                    {
                        currCommonElements.AddRange(granularCommonElements);
                    }

                    i--; j--;
                }
                else if (j > 0 && (i <= 0 || C[i, j - 1] >= C[i - 1, j]))
                {
                    currSecondDiffs.Push(currSecond);
                    diffCount2++;
                    j--;
                }
                else if (i > 0 && (j <= 0 || C[i, j - 1] < C[i - 1, j]))
                {
                    currFirstDiffs.Push(currFirst);
                    diffCount1++;
                    i--;
                }
            }while (i > 0 || j > 0);

            //calculate diff ratio, or similarity factor
            double ratio1 = 0;

            if (first.Count > 0)
            {
                ratio1 = 1 - diffCount1 / first.Count;
            }

            double ratio2 = 0;

            if (second.Count > 0)
            {
                ratio2 = 1 - diffCount2 / second.Count;
            }

            return(Math.Min(ratio1, ratio2));
        }
 /// <summary>
 /// Merge of headers is not supported this will always return false
 /// </summary>
 /// <param name="objectToMergeWith"></param>
 /// <returns></returns>
 public bool Merge(IDiffObject objectToMergeWith)
 {
     return(false);
 }