Table of Contents

Struct IntRange

Namespace
PAC.DataStructures

A set of consecutive integers.

public readonly struct IntRange : IReadOnlyList<int>, IReadOnlyContains<int>, IReadOnlyCollection<int>, IEnumerable<int>, IEnumerable, IEquatable<IntRange>, ISetComparable<IntRange>
Implements
Inherited Members
Extension Methods

Remarks

The enumerator iterates throught the elements from startElement to endElement.

If startBoundary and endBoundary are equal but their being inclusive / exclusive do not match, we define the range to be empty. This definition is useful in situations such as

foreach (int index in IntRange.InclExcl(0, array.Length))
{
    Console.WriteLine($"Element {index}: {array[index]}");
}
so that it's equivalent to
for (int index = 0; index < array.Length; index++)
{
    Console.WriteLine($"Element {index}: {array[index]}");
}

Constructors

IntRange(int, int, bool, bool)

Creates a new range.

public IntRange(int startBoundary, int endBoundary, bool startBoundaryInclusive, bool endBoundaryInclusive)

Parameters

startBoundary int

The start of the range. Can be inclusive or exclusive.

endBoundary int

The end of the range. Can be inclusive or exclusive.

startBoundaryInclusive bool

Whether the start boundary is inclusive or exclusive.

endBoundaryInclusive bool

Whether the end boundary is inclusive or exclusive.

Remarks

If startBoundary and endBoundary are equal but their being inclusive / exclusive do not match, we define the range to be empty. See IntRange for more details.

Fields

All

The range from MinValue (inclusive) to MaxValue (inclusive).

public static readonly IntRange All

Field Value

IntRange

Empty

The range from 0 (exclusive) to 0 (exclusive), which contains no elements.

public static readonly IntRange Empty

Field Value

IntRange

endBoundary

The end of the range. Can be inclusive or exclusive.

public readonly int endBoundary

Field Value

int

Remarks

Will be different from endElement if this boundary is exclusive.

See Also

endBoundaryInclusive

Whether endBoundary is inclusive or exclusive.

public readonly bool endBoundaryInclusive

Field Value

bool
See Also

startBoundary

The start of the range. Can be inclusive or exclusive.

public readonly int startBoundary

Field Value

int

Remarks

Will be different from startElement if this boundary is exclusive.

See Also

startBoundaryInclusive

Whether startBoundary is inclusive or exclusive.

public readonly bool startBoundaryInclusive

Field Value

bool
See Also

Properties

Count

The number of elements in the range.

public int Count { get; }

Property Value

int

Remarks

May throw an OverflowException. For example, the Count of a range from MinValue to MaxValue cannot be expressed as an int. Using LongCount instead will never encounter this issue.

Exceptions

OverflowException

The number of elements is too large to represent as an int.

this[int]

Indexes the elements of the range from startElement to endElement.

public int this[int index] { get; }

Parameters

index int

Property Value

int

Remarks

Some ranges, such as a range from MinValue to MaxValue, are too long for every element to be indexed by an int. Using this[long] instead will never have this issue.

Exceptions

ArgumentOutOfRangeException

index is not a valid index in the range.

this[long]

Indexes the elements of the range from startElement to endElement.

public int this[long index] { get; }

Parameters

index long

Property Value

int

Remarks

Unlike this[int], every element in a range can be indexed with a long. For example, the range from MinValue to MaxValue is too long for every element to be indexed by an int, but every element can be indexed by a long.

Exceptions

ArgumentOutOfRangeException

index is not a valid index in the range.

LongCount

The same as Count but computed as a long.

public long LongCount { get; }

Property Value

long

Remarks

Unlike Count, this will never throw an OverflowException. For example, the largest range, MinValue to MaxValue, cannot be expressed as an int but can be expressed as a long.

asDecreasing

A range with the same elements, in decreasing order.

public IntRange asDecreasing { get; }

Property Value

IntRange

Either the range unchanged or reverse.

See Also

asExclExcl

A range with the same elements, in the same order, but expressed with startBoundary exclusive and endBoundary exclusive.

public IntRange asExclExcl { get; }

Property Value

IntRange
See Also

asExclIncl

A range with the same elements, in the same order, but expressed with startBoundary exclusive and endBoundary inclusive.

public IntRange asExclIncl { get; }

Property Value

IntRange
See Also

asInclExcl

A range with the same elements, in the same order, but expressed with startBoundary inclusive and endBoundary exclusive.

public IntRange asInclExcl { get; }

Property Value

IntRange
See Also

asInclIncl

A range with the same elements, in the same order, but expressed with startBoundary inclusive and endBoundary inclusive.

public IntRange asInclIncl { get; }

Property Value

IntRange

Exceptions

InvalidOperationException

The range is empty.

See Also

asIncreasing

A range with the same elements, in increasing order.

public IntRange asIncreasing { get; }

Property Value

IntRange

Either the range unchanged or reverse.

See Also

endElement

The last element when iterating through the range (from start to end).

public int endElement { get; }

Property Value

int

Remarks

Will be different from endBoundary if that boundary is exclusive.

See Also

isEmpty

Whether the range has no elements.

public bool isEmpty { get; }

Property Value

bool

isExclExcl

Whether startBoundary is exclusive and endBoundary is exclusive.

public bool isExclExcl { get; }

Property Value

bool

Remarks

See Also

isExclIncl

Whether startBoundary is exclusive and endBoundary is inclusive.

public bool isExclIncl { get; }

Property Value

bool

Remarks

See Also

isInclExcl

Whether startBoundary is inclusive and endBoundary is exclusive.

public bool isInclExcl { get; }

Property Value

bool

Remarks

See Also

isInclIncl

Whether startBoundary is inclusive and endBoundary is inclusive.

public bool isInclIncl { get; }

Property Value

bool

Remarks

See Also

maxBoundary

The higher of startBoundary and endBoundary.

public int maxBoundary { get; }

Property Value

int

Remarks

Will be different from maxElement if this boundary is exclusive.

See Also

maxBoundaryInclusive

Whether maxBoundary is inclusive or exclusive.

public bool maxBoundaryInclusive { get; }

Property Value

bool

Remarks

If startBoundary and endBoundary are equal but their being inclusive / exclusive do not match, we define this to be exclusive. See IntRange for more details.

See Also

maxElement

The highest element in the range.

public int maxElement { get; }

Property Value

int

Remarks

Will be different from maxBoundary if that boundary is exclusive.

See Also

minBoundary

The lower of startBoundary and endBoundary.

public int minBoundary { get; }

Property Value

int

Remarks

Will be different from minElement if this boundary is exclusive.

See Also

minBoundaryInclusive

Whether minBoundary is inclusive or exclusive.

public bool minBoundaryInclusive { get; }

Property Value

bool

Remarks

If startBoundary and endBoundary are equal but their being inclusive / exclusive do not match, we define this to be exclusive. See IntRange for more details.

See Also

minElement

The lowest element in the range.

public int minElement { get; }

Property Value

int

Remarks

Will be different from minBoundary if that boundary is exclusive.

See Also

reverse

The range with startBoundary and endBoundary swapped, and startBoundaryInclusive and endBoundaryInclusive swapped.

public IntRange reverse { get; }

Property Value

IntRange

startElement

The first element when iterating through the range (from start to end).

public int startElement { get; }

Property Value

int

Remarks

Will be different from startBoundary if that boundary is exclusive.

See Also

Methods

AtLeast(int)

Creates a range from x (inclusive) to MaxValue (inclusive).

public static IntRange AtLeast(int x)

Parameters

x int

Returns

IntRange
See Also

AtMost(int)

Creates a range from x (inclusive) to MinValue (inclusive).

public static IntRange AtMost(int x)

Parameters

x int

Returns

IntRange
See Also

BoundingRange(IEnumerable<int>)

Returns the smallest range containing all the given values.

public static IntRange BoundingRange(IEnumerable<int> integers)

Parameters

integers IEnumerable<int>

Returns

IntRange

The smallest range containing all the given values. It will be expressed with both boundaries inclusive, unless the sequence of values is empty, in which case it will be expressed with both boundaries exclusive.

Exceptions

ArgumentNullException

integers is null.

Clamp(int)

Clamps the given integer to be within the IntRange.

public int Clamp(int n)

Parameters

n int

The value to clamp.

Returns

int

Exceptions

InvalidOperationException

The range is empty.

See Also

Contains(int)

Whether the given integer is in the range.

public bool Contains(int x)

Parameters

x int

Returns

bool

Equals(IntRange)

public bool Equals(IntRange other)

Parameters

other IntRange

Returns

bool
See Also

Equals(object)

The same as Equals(IntRange).

public override bool Equals(object obj)

Parameters

obj object

Returns

bool
See Also

ExclExcl(int, int)

Creates a range from startBoundary (exclusive) to endBoundary (exclusive).

public static IntRange ExclExcl(int startBoundary, int endBoundary)

Parameters

startBoundary int
endBoundary int

Returns

IntRange
See Also

ExclIncl(int, int)

Creates a range from startBoundary (exclusive) to endBoundary (inclusive).

public static IntRange ExclIncl(int startBoundary, int endBoundary)

Parameters

startBoundary int
endBoundary int

Returns

IntRange

Remarks

If startBoundary and endBoundary are equal, we define the range to be empty. See IntRange for more details.

See Also

Extend(int, int)

Adds startBoundaryOffset to startBoundary and adds endBoundaryOffset to endBoundary.

public IntRange Extend(int startBoundaryOffset, int endBoundaryOffset)

Parameters

startBoundaryOffset int
endBoundaryOffset int

Returns

IntRange

Remarks

GetEnumerator()

Iterates throught the elements from startElement to endElement.

public IEnumerator<int> GetEnumerator()

Returns

IEnumerator<int>

GetHashCode()

Returns the hash code for this instance.

public override int GetHashCode()

Returns

int

A 32-bit signed integer that is the hash code for this instance.

InclExcl(int, int)

Creates a range from startBoundary (inclusive) to endBoundary (exclusive).

public static IntRange InclExcl(int startBoundary, int endBoundary)

Parameters

startBoundary int
endBoundary int

Returns

IntRange

Remarks

If startBoundary and endBoundary are equal, we define the range to be empty. See IntRange for more details.

See Also

InclIncl(int, int)

Creates a range from startBoundary (inclusive) to endBoundary (inclusive).

public static IntRange InclIncl(int startBoundary, int endBoundary)

Parameters

startBoundary int
endBoundary int

Returns

IntRange
See Also

Intersect(IntRange, IntRange)

Whether the two ranges have any elements in common.

public static bool Intersect(IntRange a, IntRange b)

Parameters

a IntRange
b IntRange

Returns

bool
See Also

Intersection(IntRange)

Returns the elements the two ranges have in common.

public IntRange Intersection(IntRange other)

Parameters

other IntRange

Returns

IntRange

The intersection of the two ranges, expressed with both boundaries inclusive, unless the intersection is empty, in which case it will be expressed with both boundaries exclusive.

See Also

Intersection(IntRange, IntRange)

Returns the elements the two ranges have in common.

public static IntRange Intersection(IntRange a, IntRange b)

Parameters

a IntRange
b IntRange

Returns

IntRange

The intersection of the two ranges. It will be expressed with both boundaries inclusive, unless the intersection is empty, in which case it will be expressed with both boundaries exclusive.

See Also

Intersects(IntRange)

Whether the two ranges have any elements in common.

public bool Intersects(IntRange other)

Parameters

other IntRange

Returns

bool
See Also

IsProperSubsequenceOf(IntRange)

public bool IsProperSubsequenceOf(IntRange other)

Parameters

other IntRange

Returns

bool

IsProperSupersequenceOf(IntRange)

public bool IsProperSupersequenceOf(IntRange other)

Parameters

other IntRange

Returns

bool

IsSubsequenceOf(IntRange)

public bool IsSubsequenceOf(IntRange other)

Parameters

other IntRange

Returns

bool

IsSubsetOf(IntRange)

Whether this as a set is a subset of other as a set.

public bool IsSubsetOf(IntRange other)

Parameters

other IntRange

Returns

bool

Remarks

Recall that 'this is subset of other' means that every element of this is an element of other, ignoring order and duplicate elements.

If T1 implements ISetComparable<T2> and T2 implements ISetComparable<T1>, then T1.IsSubsetOf(T2) should be logically equivalent to T2.IsSupersetOf(T1).

See Also

IsSupersequenceOf(IntRange)

public bool IsSupersequenceOf(IntRange other)

Parameters

other IntRange

Returns

bool

IsSupersetOf(IntRange)

Whether this as a set is a superset of other as a set.

public bool IsSupersetOf(IntRange other)

Parameters

other IntRange

Returns

bool

Remarks

Recall that 'this is superset of other' means that every element of other is an element of this, ignoring order and duplicate elements.

If T1 implements ISetComparable<T2> and T2 implements ISetComparable<T1>, then T1.IsSupersetOf(T2) should be logically equivalent to T2.IsSubsetOf(T1).

See Also

LessThan(int)

Creates a range from x (exclusive) to MinValue (inclusive).

public static IntRange LessThan(int x)

Parameters

x int

Returns

IntRange
See Also

MoreThan(int)

Creates a range from x (exclusive) to MaxValue (inclusive).

public static IntRange MoreThan(int x)

Parameters

x int

Returns

IntRange
See Also

SequenceEqual(IntRange, IntRange)

Whether the two ranges have exactly the same elements in the same order.

public static bool SequenceEqual(IntRange a, IntRange b)

Parameters

a IntRange
b IntRange

Returns

bool

Remarks

Note that ranges with different boundaries can give the same sequence (and therefore be considered sequence-equal), due to the boundaries being inclusive or exclusive.

See Also

SequenceEquals(IntRange)

Whether the two ranges have exactly the same elements in the same order.

public bool SequenceEquals(IntRange other)

Parameters

other IntRange

Returns

bool

Remarks

Note that ranges with different boundaries can give the same sequence (and therefore be considered sequence-equal), due to the boundaries being inclusive or exclusive.

See Also

SetEqual(IntRange, IntRange)

Whether the two ranges have exactly the same elements, ignoring order.

public static bool SetEqual(IntRange a, IntRange b)

Parameters

a IntRange
b IntRange

Returns

bool

Remarks

Note that ranges with different boundaries can have the same elements (and therefore be considered set-equal), due to the boundaries being inclusive or exclusive.

See Also

SetEquals(IntRange)

Whether the two ranges have exactly the same elements, ignoring order.

public bool SetEquals(IntRange other)

Parameters

other IntRange

Returns

bool

Remarks

Note that ranges with different boundaries can have the same elements (and therefore be considered set-equal), due to the boundaries being inclusive or exclusive.

See Also

Singleton(int)

Creates a range from x (inclusive) to x (inclusive).

public static IntRange Singleton(int x)

Parameters

x int

Returns

IntRange

ToString()

Represents the range as a string using the mathematical notation for intervals - i.e. square brackets denote an inclusive boundary and round brackets denote an exclusive boundary.

For example, the range from -1 (inclusive) to 5 (exclusive) will be respresented as "[-1, 5)".

public override string ToString()

Returns

string

Operators

operator +(IntRange, int)

Translates the range by the given integer.

public static IntRange operator +(IntRange range, int integer)

Parameters

range IntRange
integer int

Returns

IntRange

Exceptions

OverflowException

If any translated elements cannot be expressed as an int.

operator +(int, IntRange)

Translates the range by the given integer.

public static IntRange operator +(int integer, IntRange range)

Parameters

integer int
range IntRange

Returns

IntRange

Exceptions

OverflowException

If any translated elements cannot be expressed as an int.

operator ==(IntRange, IntRange)

Whether the two ranges have the same start boundary and end boundary and the same inclusivity at each boundary - i.e. compares the structs' fields.

public static bool operator ==(IntRange a, IntRange b)

Parameters

a IntRange
b IntRange

Returns

bool

Remarks

Note that having the same elements does not necessarily mean they are ==, due to the boundaries being inclusive or exclusive.

See Also

operator !=(IntRange, IntRange)

Whether the two ranges have the same start boundary and end boundary and the same inclusivity at each boundary - i.e. compares the structs' fields.

public static bool operator !=(IntRange a, IntRange b)

Parameters

a IntRange
b IntRange

Returns

bool

Remarks

Note that being != does not necessarily mean they have different elements, due to the boundaries being inclusive or exclusive.

See Also

operator -(IntRange, int)

Translates the range by the given integer.

public static IntRange operator -(IntRange range, int integer)

Parameters

range IntRange
integer int

Returns

IntRange

Exceptions

OverflowException

If any translated elements cannot be expressed as an int.

operator -(int, IntRange)

Subtracts each element of the range from the given integer.

public static IntRange operator -(int integer, IntRange range)

Parameters

integer int
range IntRange

Returns

IntRange

Exceptions

OverflowException

If any translated elements cannot be expressed as an int.

operator -(IntRange)

Negates each element of the range.

public static IntRange operator -(IntRange range)

Parameters

range IntRange

Returns

IntRange

Exceptions

OverflowException

If any negated elements cannot be expressed as an int. This only occurs if the range contains MinValue.