Struct SortedIntView
- Namespace
- Graphity
- Assembly
- Graphity.Core.dll
A list of int values in ascending order and without duplicates, as a view of a range of
an array.
public readonly struct SortedIntView : IReadOnlyList<int>, IReadOnlyCollection<int>, IEnumerable<int>, IEnumerable, IEquatable<SortedIntView>
- Implements
- Inherited Members
Remarks
Adjacency lookups such as OutNodes(int) return it, as do other methods of this library that return nodes or edge indices in ascending order, such as ReachableFrom(Graph, int). It is a read-only list (IReadOnlyList<T>) without boxing. Since its values are sorted and distinct, IndexOf(int) and Contains(int) search in O(log Count), and First() and Last() are the smallest and the largest value.
It is a readonly struct of three fields, so returning one allocates nothing. It converts
implicitly to ReadOnlySpan<T>, so a view feeds every method that takes a span,
such as builder.AddOutEdges(source, graph.OutNodes(node)). Unlike a span, a view can be
stored in fields and collections. foreach runs over it in any context, even in iterators
and in async methods that await inside the loop, with a struct enumerator that allocates
nothing. foreach over AsSpan() is the fastest loop. Slice(int, int) and
the range syntax view[a..b] return a part of it.
A view shares its array with the object it comes from instead of copying it; a method that
returns a copy, such as Union(SortedIntView), says so. Instances are immutable and safe to share
between threads, as the arrays behind them never change. A view that ViewOf(int[], int, int) makes
of an array of the caller stays sorted and unchanged only as long as nobody modifies that range
of the array. default(SortedIntView) is the empty view.
Memory: constant; the values stay in the shared array.
Properties
Count
Gets the number of values.
public int Count { get; }
Property Value
Remarks
Time: O(1).
Empty
Gets an empty view, equal to default(SortedIntView).
public static SortedIntView Empty { get; }
Property Value
IsEmpty
Gets whether the view has no values. Equivalent to Count == 0.
public bool IsEmpty { get; }
Property Value
Remarks
Time: O(1).
this[int]
Gets the value at position index.
public int this[int index] { get; }
Parameters
indexintPosition in
0..Count-1.
Property Value
Remarks
Time: O(1).
Exceptions
- ArgumentOutOfRangeException
indexis out of range.
Methods
AsSpan()
Returns the values as a read-only span over the shared array, not a copy.
public ReadOnlySpan<int> AsSpan()
Returns
- ReadOnlySpan<int>
The values.
Remarks
Time: O(1).
Contains(int)
Returns whether the view contains value.
public bool Contains(int value)
Parameters
valueintThe value to search for.
Returns
Remarks
Time: O(log Count).
CopyTo(Span<int>)
Copies the values into destination from position 0 on.
public void CopyTo(Span<int> destination)
Parameters
Remarks
A part of the view copies with Slice(start, length).CopyTo(...).
Time: O(Count).
Exceptions
- ArgumentException
destinationis shorter than the view.
Equals(SortedIntView)
Returns whether other is a view with the same values in the same order.
public bool Equals(SortedIntView other)
Parameters
otherSortedIntViewThe view to compare with.
Returns
Remarks
Equality is defined between views; to compare with another sequence, use
AsSpan().SequenceEqual(...).
Time: O(Count).
Equals(object?)
Returns whether other is a view with the same values in the same order.
public override bool Equals(object? obj)
Parameters
objobject
Returns
Remarks
Equality is defined between views; to compare with another sequence, use
AsSpan().SequenceEqual(...).
Time: O(Count).
First()
Returns the first value, which is the smallest.
public int First()
Returns
- int
The smallest value.
Remarks
Time: O(1).
Exceptions
- InvalidOperationException
The view is empty.
GetEnumerator()
Returns an enumerator over the values, in ascending order.
public SortedIntView.Enumerator GetEnumerator()
Returns
- SortedIntView.Enumerator
The enumerator.
Remarks
A struct enumerator: foreach over a view allocates nothing and works in any context,
even in iterators and in async methods that await inside the loop. A loop that has to be as
fast as possible runs over the span instead: foreach (int v in view.AsSpan()).
Time: O(1) to create the enumerator.
GetHashCode()
Returns a hash code over the values. It is the list hash code that the Java library computes as well.
public override int GetHashCode()
Returns
- int
The hash code.
Remarks
Time: O(Count).
IndexOf(int)
Returns the position of value, or -1 if the view does not contain it.
public int IndexOf(int value)
Parameters
valueintThe value to search for.
Returns
- int
The position in
0..Count-1, or -1.
Remarks
Time: O(log Count).
Last()
Returns the last value, which is the largest.
public int Last()
Returns
- int
The largest value.
Remarks
Time: O(1).
Exceptions
- InvalidOperationException
The view is empty.
LastIndexOf(int)
Returns the position of value, or -1 if the view does not contain it.
Equivalent to IndexOf(int), since no value occurs twice.
public int LastIndexOf(int value)
Parameters
valueintThe value to search for.
Returns
- int
The position in
0..Count-1, or -1.
Remarks
Time: O(log Count).
Slice(int, int)
Returns the length values from position start on as a
view of the same array, not a copy.
public SortedIntView Slice(int start, int length)
Parameters
Returns
- SortedIntView
A view of the range.
Remarks
The range syntax view[a..b] calls it with Slice(a, b - a).
Time: O(1).
Exceptions
- ArgumentOutOfRangeException
The range is not within the view.
ToArray()
Returns the values as a new array.
public int[] ToArray()
Returns
- int[]
A copy of the values.
Remarks
Time: O(Count).
Memory: 4 x Count bytes for the result.
ToString()
Returns the values in brackets, such as [1, 3, 5].
public override string ToString()
Returns
- string
The text.
Remarks
Time: O(Count).
Union(SortedIntView)
Returns the values of this view and other together, in ascending order and
without duplicates.
public SortedIntView Union(SortedIntView other)
Parameters
otherSortedIntViewThe view to unite with this one.
Returns
- SortedIntView
The union.
Remarks
The result is a copy, merged on every call, unless one of the two views is empty: then it is the other one.
Time: O(Count + other.Count).
Memory: up to 4 x (Count + other.Count) bytes for the result; while it runs, up to as
much again if the two views share values.
Exceptions
- ArgumentException
The union has more than
Array.MaxLengthvalues, the most an array can hold.
ViewOf(int[], int, int)
Returns a view of array[fromIndex..toIndex-1], which must be strictly ascending.
public static SortedIntView ViewOf(int[] array, int fromIndex, int toIndex)
Parameters
arrayint[]The array to view.
fromIndexintThe first position of the range.
toIndexintThe end position of the range, exclusive.
Returns
- SortedIntView
A view of the range.
Remarks
The range is checked, not copied: the view shares array, and stays sorted
and unchanged only as long as nobody modifies that range of it.
Time: O(toIndex - fromIndex).
Exceptions
- ArgumentOutOfRangeException
fromIndexis negative,toIndexis greater thanarray.Length, orfromIndex > toIndex.- ArgumentException
The range is not strictly ascending.
Operators
operator ==(SortedIntView, SortedIntView)
Returns whether both views hold the same values in the same order.
public static bool operator ==(SortedIntView left, SortedIntView right)
Parameters
leftSortedIntViewThe first view.
rightSortedIntViewThe second view.
Returns
Remarks
Time: O(Count).
implicit operator ReadOnlySpan<int>(SortedIntView)
Converts the view into a read-only span over the shared array, not a copy.
public static implicit operator ReadOnlySpan<int>(SortedIntView view)
Parameters
viewSortedIntViewThe view.
Returns
Remarks
Time: O(1).
operator !=(SortedIntView, SortedIntView)
Returns whether the views differ in a value or in length.
public static bool operator !=(SortedIntView left, SortedIntView right)
Parameters
leftSortedIntViewThe first view.
rightSortedIntViewThe second view.
Returns
Remarks
Time: O(Count).