Example #1
0
 private static IEnumerable <T> BinarySearch <T>(IList <T> index, BinarySearchDelegate <T> tester)
 {
     if (index.Count > 0)
     {
         return(BinarySearch(index, 0, index.Count - 1, tester));
     }
     else
     {
         return(new List <T>());
     }
 }
Example #2
0
        private static IEnumerable <T> ExtendMatchingRange <T>(IList <T> index, int knownMatchIndex, BinarySearchDelegate <T> tester, out int first, out int last)
        {
            if (index == null)
            {
                throw new ArgumentNullException("index");
            }
            if (tester == null)
            {
                throw new ArgumentNullException("tester");
            }
            if (knownMatchIndex < 0 || knownMatchIndex >= index.Count)
            {
                throw new ArgumentOutOfRangeException("knownMatchIndex");
            }

            first = knownMatchIndex;
            last  = knownMatchIndex;

            while (first > 0 && tester(index[first - 1]) == 0)
            {
                --first;
            }

            while (last + 1 < index.Count && tester(index[last + 1]) == 0)
            {
                ++last;
            }

            List <T> result = new List <T>();

            for (int i = first; i <= last; ++i)
            {
                result.Add(index[i]);
            }

            return(result);
        }
Example #3
0
        private static IEnumerable <T> ExtendMatchingRange <T>(IList <T> index, int knownMatchIndex, BinarySearchDelegate <T> tester)
        {
            int dummyFirst = 0;
            int dummyLast  = 0;

            return(ExtendMatchingRange(index, knownMatchIndex, tester, out dummyFirst, out dummyLast));
        }
Example #4
0
        /// <summary>
        /// Finds all items in the specified range of the specified sorted list for which the
        /// given tester returns 0.
        /// </summary>
        private static IEnumerable <T> BinarySearch <T>(IList <T> index, int first, int last, BinarySearchDelegate <T> tester)
        {
            if (index == null)
            {
                throw new ArgumentNullException("index");
            }
            if (tester == null)
            {
                throw new ArgumentNullException("tester");
            }
            if (first < 0)
            {
                throw new ArgumentOutOfRangeException("first");
            }
            if (last >= index.Count || last < first)
            {
                throw new ArgumentOutOfRangeException("last");
            }

            int middle     = (first + last) / 2;
            int middleTest = tester(index[middle]);

            if (middleTest > 0)
            {
                if (middle > first)
                {
                    return(BinarySearch(index, first, middle - 1, tester));
                }
                else
                {
                    return(new List <T>());
                }
            }
            else if (middleTest < 0)
            {
                if (last > middle)
                {
                    return(BinarySearch(index, middle + 1, last, tester));
                }
                else
                {
                    return(new List <T>());
                }
            }
            else
            {
                return(ExtendMatchingRange(index, middle, tester));
            }
        }