Table of Contents

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

int

Remarks

Time: O(1).

Empty

Gets an empty view, equal to default(SortedIntView).

public static SortedIntView Empty { get; }

Property Value

SortedIntView

IsEmpty

Gets whether the view has no values. Equivalent to Count == 0.

public bool IsEmpty { get; }

Property Value

bool

Remarks

Time: O(1).

this[int]

Gets the value at position index.

public int this[int index] { get; }

Parameters

index int

Position in 0..Count-1.

Property Value

int

Remarks

Time: O(1).

Exceptions

ArgumentOutOfRangeException

index is 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

value int

The value to search for.

Returns

bool

true if the view contains the value.

Remarks

Time: O(log Count).

CopyTo(Span<int>)

Copies the values into destination from position 0 on.

public void CopyTo(Span<int> destination)

Parameters

destination Span<int>

The span to fill; at least Count long.

Remarks

A part of the view copies with Slice(start, length).CopyTo(...).

Time: O(Count).

Exceptions

ArgumentException

destination is 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

other SortedIntView

The view to compare with.

Returns

bool

true if both hold the same values.

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

obj object

Returns

bool

true if both hold the same values.

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

value int

The 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

value int

The 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

start int

The first position.

length int

The number of values.

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

other SortedIntView

The 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.MaxLength values, 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

array int[]

The array to view.

fromIndex int

The first position of the range.

toIndex int

The 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

fromIndex is negative, toIndex is greater than array.Length, or fromIndex > 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

left SortedIntView

The first view.

right SortedIntView

The second view.

Returns

bool

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

view SortedIntView

The view.

Returns

ReadOnlySpan<int>

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

left SortedIntView

The first view.

right SortedIntView

The second view.

Returns

bool

Remarks

Time: O(Count).