RangeDictionary<TKey, TValue> Class
Definition
Represents a sorted dictionary that maps non-overlapping half-open ranges to values.
public sealed class RangeDictionary<TKey, TValue> : IReadOnlyCollection<ValueRange<TKey, TValue>>, IEnumerable<ValueRange<TKey, TValue>>, IEnumerable where TKey : IComparable<TKey>
Type Parameters
TKeyThe comparable endpoint type.
TValueThe value type.
- Inheritance
-
RangeDictionary<TKey, TValue>
- Implements
-
IReadOnlyCollection<ValueRange<TKey, TValue>>IEnumerable<ValueRange<TKey, TValue>>
- Inherited Members
- Extension Methods
Examples
// Tax-bracket lookup: ranges are half-open and must not overlap.
var brackets = new RangeDictionary<decimal, decimal>
{
{ 0m, 18_200m, 0.00m },
{ 18_200m, 45_000m, 0.19m },
{ 45_000m, 135_000m, 0.30m },
};
if (brackets.TryGetValue(50_000m, out decimal rate))
Console.WriteLine($"Rate: {rate:P0}"); // Rate: 30%
// Adjacent ranges are allowed; an overlapping insertion throws ArgumentException.
brackets.Add(135_000m, 190_000m, 0.37m);
Remarks
Entries are stored in three parallel arrays - one for the inclusive start of each range, one for the exclusive end, and one for the associated value. Lookups use binary search across the start endpoints, followed by a single end-boundary check. Insertions and removals shift the affected suffix of each array.
Ranges use half-open semantics: [startInclusive, endExclusive). Adjacent ranges are allowed; overlapping
ranges are rejected with ArgumentException.
This type is not thread-safe.
Constructors
RangeDictionary()
Initializes a new instance of the RangeDictionary<TKey, TValue> class using the default endpoint comparer.
public RangeDictionary()
RangeDictionary(IComparer<TKey>?)
Initializes a new instance of the RangeDictionary<TKey, TValue> class using the specified comparer.
public RangeDictionary(IComparer<TKey>? comparer)
Parameters
Properties
Capacity
Gets the allocated range capacity.
public int Capacity { get; }
Property Value
- int
The current allocated capacity of the underlying storage.
Comparer
Gets the comparer used to order range endpoints.
public IComparer<TKey> Comparer { get; }
Property Value
- IComparer<TKey>
The active endpoint comparer.
Count
Gets the number of stored ranges.
public int Count { get; }
Property Value
- int
The number of ranges currently stored in the dictionary.
this[TKey]
Gets the value associated with the range containing the specified key.
public TValue this[TKey key] { get; }
Parameters
keyTKeyThe key to locate. Must not be null.
Property Value
- TValue
The value associated with the containing range.
Exceptions
- ArgumentNullException
keyis null.- KeyNotFoundException
No range contains
key.
Methods
Add(ValueRange<TKey, TValue>)
Adds the specified range entry.
public void Add(ValueRange<TKey, TValue> entry)
Parameters
entryValueRange<TKey, TValue>The entry to add.
Exceptions
- ArgumentException
The entry overlaps an existing range.
Add(TKey, TKey, TValue)
Adds a non-overlapping half-open range and its value.
public void Add(TKey startInclusive, TKey endExclusive, TValue value)
Parameters
startInclusiveTKeyThe inclusive start.
endExclusiveTKeyThe exclusive end.
valueTValueThe value to associate with the range.
Exceptions
- ArgumentNullException
startInclusiveorendExclusiveis null.- ArgumentException
startInclusiveis greater than or equal toendExclusive, or the range overlaps an existing range.
Clear()
Removes all ranges from the dictionary.
public void Clear()
ContainsKey(TKey)
Determines whether any stored range contains the specified key.
public bool ContainsKey(TKey key)
Parameters
keyTKeyThe key to locate. Must not be null.
Returns
Exceptions
- ArgumentNullException
keyis null.
EnsureCapacity(int)
Ensures that the dictionary can hold at least the specified number of ranges without reallocating.
public int EnsureCapacity(int capacity)
Parameters
capacityintThe desired capacity.
Returns
- int
The current capacity.
Exceptions
- ArgumentOutOfRangeException
capacityis negative.
GetEntryAt(int)
Gets the range entry at the specified sorted index.
public ValueRange<TKey, TValue> GetEntryAt(int index)
Parameters
indexintThe zero-based range index.
Returns
- ValueRange<TKey, TValue>
The range entry at
index.
Exceptions
- ArgumentOutOfRangeException
indexis negative or greater than or equal to Count.
GetEnumerator()
Returns an enumerator that iterates through the stored range entries in ascending order.
public RangeDictionary<TKey, TValue>.Enumerator GetEnumerator()
Returns
- RangeDictionary<TKey, TValue>.Enumerator
An RangeDictionary<TKey, TValue>.Enumerator over the dictionary entries.
Overlaps(TKey, TKey)
Determines whether the specified range overlaps any existing range.
public bool Overlaps(TKey startInclusive, TKey endExclusive)
Parameters
startInclusiveTKeyThe inclusive start.
endExclusiveTKeyThe exclusive end.
Returns
Exceptions
- ArgumentNullException
startInclusiveorendExclusiveis null.- ArgumentException
startInclusiveis greater than or equal toendExclusive.
Remove(TKey, TKey)
Removes an exact range from the dictionary.
public bool Remove(TKey startInclusive, TKey endExclusive)
Parameters
startInclusiveTKeyThe inclusive start of the range.
endExclusiveTKeyThe exclusive end of the range.
Returns
Exceptions
- ArgumentNullException
startInclusiveorendExclusiveis null.- ArgumentException
startInclusiveis greater than or equal toendExclusive.
ToArray()
Copies the stored range entries to a new array in ascending sorted order.
public ValueRange<TKey, TValue>[] ToArray()
Returns
- ValueRange<TKey, TValue>[]
A new array containing the stored entries.
TryGetEntry(TKey, out ValueRange<TKey, TValue>)
Attempts to get the range entry containing the specified key.
public bool TryGetEntry(TKey key, out ValueRange<TKey, TValue> entry)
Parameters
keyTKeyThe key to locate. Must not be null.
entryValueRange<TKey, TValue>The containing range entry, if found.
Returns
Exceptions
- ArgumentNullException
keyis null.
TryGetValue(TKey, out TValue)
Attempts to get the value associated with the range containing the specified key.
public bool TryGetValue(TKey key, out TValue value)
Parameters
keyTKeyThe key to locate. Must not be null.
valueTValueThe value associated with the containing range, if found.
Returns
Exceptions
- ArgumentNullException
keyis null.
Explicit Interface Implementations
IEnumerable<ValueRange<TKey, TValue>>.GetEnumerator()
Returns an enumerator that iterates through the collection.
IEnumerator<ValueRange<TKey, TValue>> IEnumerable<ValueRange<TKey, TValue>>.GetEnumerator()
Returns
- IEnumerator<ValueRange<TKey, TValue>>
An enumerator that can be used to iterate through the collection.
IEnumerable.GetEnumerator()
Returns an enumerator that iterates through a collection.
IEnumerator IEnumerable.GetEnumerator()
Returns
- IEnumerator
An IEnumerator object that can be used to iterate through the collection.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |