Example #1
0
        public bool TryGetValue(TKey key, out TValue value)
        {
            var index = RangeOperations <TKey> .BinarySearch(this.Keys, key);

            if (index >= 0)
            {
                value = this.items[index].Value;
                return(true);
            }
            value = default;
            return(false);
        }
Example #2
0
        /// <summary>Adds a new value at the specified key range.</summary>
        /// <exception cref="ArgumentOutOfRangeException">Thrown when the range overlaps an existing range of the dictionary.</exception>
        /// <param name="range">The key range.</param>
        /// <param name="value">The value.</param>
        /// <remarks>This operation takes O(log n) time, where n is the number of range key and value pairs)</remarks>
        public void Add(Range <TKey> range, TValue value)
        {
            var left = ~RangeOperations <TKey> .BinarySearch(this.Keys, range.From);

            var right = ~RangeOperations <TKey> .BinarySearch(this.Keys, range.To);

            if ((left < 0) || (right < 0) || (left != right))
            {
                throw new ArgumentOutOfRangeException(nameof(range), "The range overlaps an existing range");
            }
            this.items.Insert(left, new KeyValuePair <Range <TKey>, TValue>(range, value));
            this.MergeIfAdjacent(left, left + 1);
            this.MergeIfAdjacent(left - 1, left);
        }
Example #3
0
        public IEnumerable <KeyValuePair <Range <TKey>, TValue> > Slice(Range <TKey> range)
        {
            switch (this.items.Count)
            {
            case 0:
                break;

            case 1:
                var single     = this.items[0];
                var singleFrom = Incrementor <TKey> .Max(single.Key.From, range.From);

                var singleTo = Incrementor <TKey> .Min(single.Key.To, range.To);

                if (singleFrom.CompareTo(singleTo) <= 0)
                {
                    yield return(new KeyValuePair <Range <TKey>, TValue>(new Range <TKey>(singleFrom, singleTo), single.Value));
                }
                break;

            default:
                var left = RangeOperations <TKey> .BinarySearch(this.Keys, range.From);

                if (left < 0)
                {
                    left = ~left;
                }
                var right = RangeOperations <TKey> .BinarySearch(this.Keys, range.From);

                if (right < 0)
                {
                    right = ~right - 1;
                }
                for (var i = left; i <= right; i++)
                {
                    var current = this.items[i];
                    var from    = current.Key.From;
                    var to      = current.Key.To;
                    yield return(new KeyValuePair <Range <TKey>, TValue>(
                                     new Range <TKey>((i == left) && (@from.CompareTo(range.From) < 0) ? range.From : from, (i == right) && (to.CompareTo(range.To) > 0) ? range.To : to),
                                     current.Value));
                }
                break;
            }
        }
Example #4
0
 public bool ContainsKey(TKey key)
 {
     return(RangeOperations <TKey> .BinarySearch(this.owner.Keys, key) >= 0);
 }