コード例 #1
0
 /// <summary>
 /// Determines if another range is inside the bounds of this range
 /// </summary>
 /// <param name="range">The range to test</param>
 /// <param name="value">The value to test</param>
 /// <returns>True if range is inside, else false</returns>
 public static bool Contains <T>(this IRange <T> range, IRange <T> value)
     where T : IComparable <T>
 {
     return(range.Contains(value.Minimum) || // For when A contains B
            range.Contains(value.Maximum) ||
            value.Contains(range.Minimum) || // For when B contains A
            value.Contains(range.Maximum));
 }
コード例 #2
0
        /// <summary>
        /// Find the index of the range containing the given element currently in the list, if any.
        /// </summary>
        /// <param name="element">The element we're looking for.</param>
        /// <returns>The index of the containing range, or the appropriate insertion point (a negative number) if there isn't any.</returns>
        private int FindIndexOfRangeFor(int element)
        {
            if (rangeList.Count == 0)
            {
                return(-1);
            }

            int low  = 0;
            int high = rangeList.Count - 1;

            while (low <= high)
            {
                int    middle   = (low == high) ? low : ((low + high) / 2);
                IRange curRange = (IRange)rangeList[middle];

                if (curRange.Contains(element))
                {
                    return(middle);
                }
                else if (curRange.Low > element)
                {
                    high = middle - 1;
                }
                else if (curRange.High < element)
                {
                    low = middle + 1;
                }
                else
                {
                    return(-middle);
                }
            }

            // Standard data structures hack to indicate the appropriate insertion point as a part of this call.
            return(-(low + 1));
        }