private int InsertBoundIntoAxis(List<Bound> axis, Bound bound)
{
// Use binary search for fast O(log(n)) indentification
// of appropriate index for our bound
int index = axis.BinarySearch(bound);
if (index < 0)
{
// If the index returned by binary search is negative,
// then our current bound value does not exist within the
// axis (most common case). The bitwise compliement (~) of
// that index value indicates the index of the first element
// in the axis list *larger* than our bound, and therefore
// the appropriate place for our item
index = ~index;
axis.Insert(index, bound);
}
else
{
// If the index returned by binary search is positive, then
// we have another bound with the *exact same value*. We'll
// go ahead and insert at that position.
axis.Insert(index, bound);
}
return index;
}