RangeSet<T> Class
Definition
Represents a sorted set of non-overlapping half-open ranges.
public sealed class RangeSet<T> : IReadOnlyCollection<Range<T>>, IEnumerable<Range<T>>, IEnumerable where T : IComparable<T>
Type Parameters
TThe comparable endpoint type.
- Inheritance
-
RangeSet<T>
- Implements
-
IEnumerable<Range<T>>
- Inherited Members
- Extension Methods
Examples
// Track disjoint blocks of allocated row IDs. Adjacent and overlapping inserts merge automatically.
var allocated = new RangeSet<int>();
allocated.Add( 0, 10); // [0, 10)
allocated.Add( 20, 30); // [20, 30)
allocated.Add( 5, 25); // merges all three into the single range [0, 30)
Console.WriteLine(allocated.Count); // 1
Console.WriteLine(allocated.Contains(15)); // true
allocated.Remove(10, 20); // splits into [0, 10) and [20, 30)
Remarks
Ranges are stored in two compact parallel arrays - one for the inclusive start of each range and one for the exclusive end. The arrays are kept sorted by start endpoint, and adjacent or overlapping ranges are merged on insertion.
Ranges use half-open semantics: [startInclusive, endExclusive).
This type is not thread-safe.
Constructors
RangeSet()
Initializes a new instance of the RangeSet<T> class using the default comparer.
public RangeSet()
RangeSet(IComparer<T>?)
Initializes a new instance of the RangeSet<T> class using the specified comparer.
public RangeSet(IComparer<T>? comparer)
Parameters
RangeSet(IEnumerable<Range<T>>, IComparer<T>?)
Initializes a new instance of the RangeSet<T> class containing the specified ranges.
public RangeSet(IEnumerable<Range<T>> ranges, IComparer<T>? comparer = null)
Parameters
rangesIEnumerable<Range<T>>The ranges to add. Must not be null.
comparerIComparer<T>
Exceptions
- ArgumentNullException
rangesis null.
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<T> Comparer { get; }
Property Value
- IComparer<T>
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 set.
this[int]
Gets the range at the specified sorted index.
public Range<T> this[int index] { get; }
Parameters
indexintThe zero-based range index.
Property Value
- Range<T>
The range at
index.
Exceptions
- ArgumentOutOfRangeException
indexis negative or greater than or equal to Count.
Methods
Add(Range<T>)
Adds the specified range to the set, merging any overlapping or adjacent ranges.
public bool Add(Range<T> range)
Parameters
rangeRange<T>The range to add.
Returns
Add(T, T)
Adds a half-open range to the set, merging any overlapping or adjacent ranges.
public bool Add(T startInclusive, T endExclusive)
Parameters
startInclusiveTThe inclusive start.
endExclusiveTThe exclusive end.
Returns
- bool
true if the set was changed; false if the range was already fully covered and the set was left unchanged.
Exceptions
- ArgumentNullException
startInclusiveorendExclusiveis null.- ArgumentException
startInclusiveis greater than or equal toendExclusive.
Clear()
Removes all ranges from the set.
public void Clear()
Contains(T)
Determines whether the specified value falls inside any stored range.
public bool Contains(T value)
Parameters
valueTThe value to test. Must not be null.
Returns
Exceptions
- ArgumentNullException
valueis null.
Contains(T, T)
Determines whether the specified range is fully contained in this set.
public bool Contains(T startInclusive, T endExclusive)
Parameters
startInclusiveTThe inclusive start.
endExclusiveTThe exclusive end.
Returns
Exceptions
- ArgumentNullException
startInclusiveorendExclusiveis null.- ArgumentException
startInclusiveis greater than or equal toendExclusive.
EnsureCapacity(int)
Ensures that the set 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.
Except(RangeSet<T>)
Returns a new set containing the ranges in this set except those covered by another set.
public RangeSet<T> Except(RangeSet<T> other)
Parameters
Returns
- RangeSet<T>
A new RangeSet<T> containing the difference.
Exceptions
- ArgumentNullException
otheris null.
GetEnumerator()
Returns an enumerator that iterates through the stored ranges in ascending order.
public RangeSet<T>.Enumerator GetEnumerator()
Returns
- RangeSet<T>.Enumerator
An RangeSet<T>.Enumerator over the stored ranges.
Intersect(RangeSet<T>)
Returns a new set containing the intersection of this set and another set.
public RangeSet<T> Intersect(RangeSet<T> other)
Parameters
Returns
- RangeSet<T>
A new RangeSet<T> containing the intersection.
Exceptions
- ArgumentNullException
otheris null.
Overlaps(T, T)
Determines whether the specified range overlaps any stored range.
public bool Overlaps(T startInclusive, T endExclusive)
Parameters
startInclusiveTThe inclusive start.
endExclusiveTThe exclusive end.
Returns
Exceptions
- ArgumentNullException
startInclusiveorendExclusiveis null.- ArgumentException
startInclusiveis greater than or equal toendExclusive.
Remove(Range<T>)
Removes the specified range from the set.
public bool Remove(Range<T> range)
Parameters
rangeRange<T>The range to remove.
Returns
Remove(T, T)
Removes the specified half-open range from the set, trimming or splitting overlapping ranges as needed.
public bool Remove(T startInclusive, T endExclusive)
Parameters
startInclusiveTThe inclusive start.
endExclusiveTThe exclusive end.
Returns
Exceptions
- ArgumentNullException
startInclusiveorendExclusiveis null.- ArgumentException
startInclusiveis greater than or equal toendExclusive.
ToArray()
Copies the stored ranges to a new array in ascending sorted order.
public Range<T>[] ToArray()
Returns
- Range<T>[]
A new array containing the stored ranges.
Union(RangeSet<T>)
Returns a new set containing the union of this set and another set.
public RangeSet<T> Union(RangeSet<T> other)
Parameters
Returns
- RangeSet<T>
A new RangeSet<T> containing the union.
Exceptions
- ArgumentNullException
otheris null.
Explicit Interface Implementations
IEnumerable<Range<T>>.GetEnumerator()
Returns an enumerator that iterates through the collection.
IEnumerator<Range<T>> IEnumerable<Range<T>>.GetEnumerator()
Returns
- IEnumerator<Range<T>>
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 |