Example #1
0
        public DifferenceState GetByIndex(int index)
        {
            DifferenceState retval = _array[index];

            if (retval == null)
            {
                retval        = new DifferenceState();
                _array[index] = retval;
            }

            return(retval);
        }
Example #2
0
        private void GetLongestSourceMatch(DifferenceState curItem, int destIndex, int destEnd, int sourceStart, int sourceEnd)
        {
            int maxDestLength     = (destEnd - destIndex) + 1;
            int currentBestLength = 0;
            int currentBestIndex  = -1;

            for (int sourceIndex = sourceStart; sourceIndex <= sourceEnd; sourceIndex++)
            {
                int maxLength = Math.Min(maxDestLength, (sourceEnd - sourceIndex) + 1);
                if (maxLength <= currentBestLength)
                {
                    // No chance to find a longer one any more
                    break;
                }

                int currentLength = GetSourceMatchLength(destIndex, sourceIndex, maxLength);
                if (currentLength > currentBestLength)
                {
                    // This is the best match so far
                    currentBestIndex  = sourceIndex;
                    currentBestLength = currentLength;
                }

                // Jump over the match
                sourceIndex += currentBestLength;
            }

            // DifferenceState current = _stateList.GetByIndex(destIndex);
            if (currentBestIndex == -1)
            {
                curItem.SetNoMatch();
            }
            else
            {
                curItem.SetMatch(currentBestIndex, currentBestLength);
            }
        }
Example #3
0
        public DifferenceState GetByIndex(int index)
        {
            DifferenceState retval = _array[index];
            if (retval == null)
            {
                retval = new DifferenceState();
                _array[index] = retval;
            }

            return retval;
        }
Example #4
0
        private void ProcessRange(int destStart, int destEnd, int sourceStart, int sourceEnd)
        {
            int             curBestIndex  = -1;
            int             curBestLength = -1;
            DifferenceState curItem;
            DifferenceState bestItem = null;

            for (int destIndex = destStart; destIndex <= destEnd; destIndex++)
            {
                int maxPossibleDestLength = (destEnd - destIndex) + 1;
                if (maxPossibleDestLength <= curBestLength)
                {
                    // we won't find a longer one even if we looked
                    break;
                }

                curItem = _stateList.GetByIndex(destIndex);

                if (!curItem.HasValidLength(sourceStart, sourceEnd, maxPossibleDestLength))
                {
                    // recalc new best length since it isn't valid or has never been done.
                    GetLongestSourceMatch(curItem, destIndex, destEnd, sourceStart, sourceEnd);
                }

                if (curItem.Status == DifferenceStatus.Matched)
                {
                    switch (_level)
                    {
                    case DifferenceEngineLevel.FastImperfect:
                        if (curItem.Length > curBestLength)
                        {
                            // this is longest match so far
                            curBestIndex  = destIndex;
                            curBestLength = curItem.Length;
                            bestItem      = curItem;
                        }

                        // Jump over the match
                        destIndex += curItem.Length - 1;
                        break;

                    case DifferenceEngineLevel.Medium:
                        if (curItem.Length > curBestLength)
                        {
                            // this is longest match so far
                            curBestIndex  = destIndex;
                            curBestLength = curItem.Length;
                            bestItem      = curItem;

                            // Jump over the match
                            destIndex += curItem.Length - 1;
                        }

                        break;

                    default:
                        if (curItem.Length > curBestLength)
                        {
                            // this is longest match so far
                            curBestIndex  = destIndex;
                            curBestLength = curItem.Length;
                            bestItem      = curItem;
                        }

                        break;
                    }
                }
            }

            if (curBestIndex < 0)
            {
                // we are done - there are no matches in this span
            }
            else
            {
                if (bestItem != null)
                {
                    int sourceIndex = bestItem.StartIndex;
                    _matchList.Add(DifferenceResultSpan.CreateNoChange(curBestIndex, sourceIndex, curBestLength));
                    if (destStart < curBestIndex)
                    {
                        // Still have more lower destination data
                        if (sourceStart < sourceIndex)
                        {
                            // Still have more lower source data
                            // Recursive call to process lower indexes
                            ProcessRange(destStart, curBestIndex - 1, sourceStart, sourceIndex - 1);
                        }
                    }

                    int upperDestStart   = curBestIndex + curBestLength;
                    int upperSourceStart = sourceIndex + curBestLength;
                    if (destEnd > upperDestStart)
                    {
                        // we still have more upper dest data
                        if (sourceEnd > upperSourceStart)
                        {
                            // set still have more upper source data
                            // Recursive call to process upper indexes
                            ProcessRange(upperDestStart, destEnd, upperSourceStart, sourceEnd);
                        }
                    }
                }
            }
        }
Example #5
0
        private void GetLongestSourceMatch(DifferenceState curItem, int destIndex, int destEnd, int sourceStart, int sourceEnd)
        {
            int maxDestLength = (destEnd - destIndex) + 1;
            int currentBestLength = 0;
            int currentBestIndex = -1;
            for (int sourceIndex = sourceStart; sourceIndex <= sourceEnd; sourceIndex++)
            {
                int maxLength = Math.Min(maxDestLength, (sourceEnd - sourceIndex) + 1);
                if (maxLength <= currentBestLength)
                {
                    // No chance to find a longer one any more
                    break;
                }

                int currentLength = GetSourceMatchLength(destIndex, sourceIndex, maxLength);
                if (currentLength > currentBestLength)
                {
                    // This is the best match so far
                    currentBestIndex = sourceIndex;
                    currentBestLength = currentLength;
                }

                // Jump over the match
                sourceIndex += currentBestLength;
            }

            // DifferenceState current = _stateList.GetByIndex(destIndex);
            if (currentBestIndex == -1)
            {
                curItem.SetNoMatch();
            }
            else
            {
                curItem.SetMatch(currentBestIndex, currentBestLength);
            }
        }