示例#1
0
        public static int BinarySearch(this IList <IIndexedDict> items, int[] keysNdxs, object[] keysValues)
        {
            int n = items.Count;
            int ia = 0, ib = n - 1;

            while (ia <= ib)
            {
                int i = (ia + ib) / 2;
                int r = Cmp.CompareKeys(items[i].ValuesList, keysNdxs, keysValues);
                if (r > 0)
                {
                    ib = i - 1;
                    if (ib < ia)
                    {
                        return(~i);
                    }
                }
                else if (r < 0)
                {
                    ia = i + 1;
                    if (ib < ia)
                    {
                        return(~ia);
                    }
                }
                else
                {
                    return(i);
                }
            }
            return(~0);
        }
示例#2
0
        public static IEnumerable <int> BinarySearchAll(this IList <IIndexedDict> items, int[] keysNdxs, object[] keysValues)
        {
            if (items == null)
            {
                yield break;
            }
            int n = items.Count;

            if (n == 0)
            {
                yield break;
            }
            int i = BinarySearch(items, keysNdxs, keysValues);

            if (i < 0)
            {
                i = 0; yield break;
            }
            var Imax = i;

            while (i > 0 && Cmp.CompareKeys(items[i - 1].ValuesList, keysNdxs, keysValues) == 0)
            {
                i--;
            }
            while (i <= Imax)
            {
#if CHECK_DATA_DUPS
                if (i < Imax && Cmp.CompareTimed(items[i].ValuesList, items[i + 1].ValuesList) == 0)
                {
                    DupDataError(items[i]);
                    i++; continue;
                }
#endif
                yield return(i++);
            }
            while (i < n)
            {
                if (Cmp.CompareKeys(items[i].ValuesList, keysNdxs, keysValues) == 0)
                {
#if CHECK_DATA_DUPS
                    if (Cmp.CompareTimed(items[i - 1].ValuesList, items[i].ValuesList) == 0)
                    {
                        DupDataError(items[i]);
                        i++; continue;
                    }
#endif
                    yield return(i++);
                }
                else
                {
                    yield break;
                }
            }
        }