ScrollingShooter.GameObjectManager.InsertBoundIntoAxis C# (CSharp) Method

InsertBoundIntoAxis() private method

Inserts a new bound into an axis list. The list is assumed to be sorted, so the method uses binary insertion for speed.
private InsertBoundIntoAxis ( List axis, Bound bound ) : int
axis List The axis list to insert the Bound into
bound Bound The Bound to insert
return int
        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;
        }