WikiFunctions.Diff._replaceNextLargerWith C# (CSharp) Method

_replaceNextLargerWith() private method

private _replaceNextLargerWith ( System.Collections.Generic.List array, int value, int high ) : int
array System.Collections.Generic.List
value int
high int
return int
        private int _replaceNextLargerWith(IntList array, int value, int high)
        {
            if (high <= 0)
            {
                high = array.Count - 1;
            }

            // off the end?
            if (high == -1 || value > array[array.Count - 1])
            {
                array.Add(value);
                return array.Count - 1;
            }

            // binary search for insertion point...
            int low = 0;
            while (low <= high)
            {
                int index = (high + low)/2;

                int found = array[index];

                if (value == found)
                {
                    return -1;
                }
                if (value > found)
                    low = index + 1;
                else
                    high = index - 1;
            }

            // # now insertion point is in $low.
            array[low] = value; // overwrite next larger
            return low;
        }