예제 #1
0
        private long SearchFirst <T>(T value, IIndexedObjectComparer <T> c, long low, long high)
        {
            if (low > high)
            {
                return(-1);
            }

            while (true)
            {
                // If low is the same as high, we are either at the first value or at
                // the position to insert the value,
                if ((high - low) <= 4)
                {
                    for (long i = low; i <= high; ++i)
                    {
                        file.Position = (i * 8);
                        long val = fileReader.ReadInt64();
                        int  res = c.Compare(val, value);

                        if (res == 0)
                        {
                            return(i);
                        }
                        if (res > 0)
                        {
                            return(-(i + 1));
                        }
                    }
                    return(-(high + 2));
                }

                // The index half way between the low and high point
                long mid = (low + high) >> 1;
                // Reaf the middle value from the data file,
                file.Position = (mid * 8);
                long midVal = fileReader.ReadInt64();

                // Compare it with the value
                int res1 = c.Compare(midVal, value);
                if (res1 < 0)
                {
                    low = mid + 1;
                }
                else if (res1 > 0)
                {
                    high = mid - 1;
                }
                else                      // if (res == 0)
                {
                    high = mid;
                }
            }
        }
예제 #2
0
        private void SearchFirstAndLast <T>(T value, IIndexedObjectComparer <T> c, out long first, out long last)
        {
            long low  = 0;
            long high = Count - 1;

            if (low > high)
            {
                first = -1;
                last  = -1;
                return;
            }

            while (true)
            {
                // If low is the same as high, we are either at the first value or at
                // the position to insert the value,
                if ((high - low) <= 4)
                {
                    first = SearchFirst(value, c, low, high);
                    last  = SearchLast(value, c, low, high);
                    return;
                }

                // The index half way between the low and high point
                long mid = (low + high) >> 1;
                // Reaf the middle value from the data file,
                file.Position = (mid * 8);
                long midVal = fileReader.ReadInt64();

                // Compare it with the value
                int res = c.Compare(midVal, value);
                if (res < 0)
                {
                    low = mid + 1;
                }
                else if (res > 0)
                {
                    high = mid - 1;
                }
                else                      // if (res == 0)
                {
                    first = SearchFirst(value, c, low, high);
                    last  = SearchLast(value, c, low, high);
                    return;
                }
            }
        }