Пример #1
0
        /// <summary>
        /// Returns the count within a range.
        /// </summary>
        /// <returns></returns>
        public int CountInRange(
            Bound <TK> start,
            Bound <TK> end)
        {
            var tailIndex = GetTailIndex(end);

            if (tailIndex != -1)
            {
                if (tailIndex >= _itemList.Count)
                {
                    tailIndex = _itemList.Count - 1;
                }

                while (tailIndex >= 0 && !BoundExtensions.IsLessThan(end, _itemList[tailIndex].Key, _itemComparer.KeyComparer))
                {
                    tailIndex--;
                }

                if (tailIndex == -1)
                {
                    return(0);
                }

                var headIndex = AdvanceIntoRange(GetHeadIndex(start), start);
                return(Math.Max(tailIndex - headIndex + 1, 0));
            }

            return(0);
        }
Пример #2
0
        public IEnumerable <KeyValuePair <TK, TV> > EnumerateRange(
            Bound <TK> start,
            Bound <TK> end)
        {
            var headIndex = AdvanceIntoRange(GetHeadIndex(start), start);

            for (var ii = headIndex; ii < _itemList.Count && BoundExtensions.IsLessThan(end, _itemList[ii].Key, _itemComparer.KeyComparer); ii++)
            {
                yield return(_itemList[ii]);
            }
        }
Пример #3
0
        /// <summary>
        /// Advances a head index until it reaches a valid point in the range.
        /// </summary>
        /// <param name="headIndex"></param>
        /// <param name="start"></param>
        /// <returns></returns>
        private int AdvanceIntoRange(
            int headIndex,
            Bound <TK> start)
        {
            if (headIndex == -1)
            {
                headIndex = 0;
            }

            while (headIndex < _itemList.Count && !BoundExtensions.IsGreaterThan(start, _itemList[headIndex].Key, _itemComparer.KeyComparer))
            {
                headIndex++;
            }

            return(headIndex);
        }