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;
}