Xnlab.SQLMon.Diff.DiffStateList.GetByIndex C# (CSharp) Method

GetByIndex() public method

public GetByIndex ( int index ) : DiffState
index int
return DiffState
        public DiffState GetByIndex(int index)
        {
            #if USE_HASH_TABLE
            DiffState retval = (DiffState)_table[index];
            if (retval == null)
            {
                retval = new DiffState();
                _table.Add(index,retval);
            }
            #else
            var retval = _array[index];
            if (retval == null)
            {
                retval = new DiffState();
                _array[index] = retval;
            }
            #endif
            return retval;
        }

Usage Example

Example #1
0
        private void ProcessRange(int destStart, int destEnd, int sourceStart, int sourceEnd)
        {
            var       curBestIndex          = -1;
            var       curBestLength         = -1;
            var       maxPossibleDestLength = 0;
            DiffState curItem  = null;
            DiffState bestItem = null;

            for (var destIndex = destStart; destIndex <= destEnd; destIndex++)
            {
                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 == DiffStatus.Matched)
                {
                    switch (_level)
                    {
                    case DiffEngineLevel.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 DiffEngineLevel.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
            {
                var sourceIndex = bestItem.StartIndex;
                _matchList.Add(DiffResultSpan.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);
                    }
                }
                var upperDestStart   = curBestIndex + curBestLength;
                var 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);
                    }
                }
            }
        }