Table of Contents

Class ValueList<T>

Namespace
Badeend.ValueCollections
Assembly
Badeend.ValueCollections.dll

An immutable, thread-safe list with value semantics.

public sealed class ValueList<T> : IReadOnlyList<T>, IReadOnlyCollection<T>, IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IEquatable<ValueList<T>>

Type Parameters

T

The type of items in the list.

Inheritance
ValueList<T>
Implements
Inherited Members
Extension Methods

Remarks

Constructing new instances can be done using Builder<T>() or ToBuilder(). For creating ValueLists, ValueListBuilder<T> is generally more efficient than List<T>.

ValueLists have "structural equality". This means that two lists are considered equal only when their contents are equal. As long as a value is present in a ValueList, its hash code may not change.

Taking a subslice with Slice(int) and Slice(int, int) is very cheap as it reuses the same allocation.

Properties

Count

Length of the list.

[Pure]
public int Count { get; }

Property Value

int

Empty

Get an empty list.

This does not allocate any memory.

[Pure]
public static ValueList<T> Empty { get; }

Property Value

ValueList<T>

IsEmpty

Shortcut for .Count == 0.

[Pure]
public bool IsEmpty { get; }

Property Value

bool

this[int]

Get an item from the list at the specified index.

public ref readonly T this[int index] { get; }

Parameters

index int

Property Value

T

Methods

AsMemory()

Access the list's contents using a ReadOnlyMemory<T>.

[Pure]
public ReadOnlyMemory<T> AsMemory()

Returns

ReadOnlyMemory<T>

AsSpan()

Access the list's contents using a ReadOnlySpan<T>.

[Pure]
public ReadOnlySpan<T> AsSpan()

Returns

ReadOnlySpan<T>

AsValueSlice()

Access the list's contents using a ValueSlice<T>.

[Pure]
public ValueSlice<T> AsValueSlice()

Returns

ValueSlice<T>

BinarySearch(T)

Perform a binary search for item within the list. The list is assumed to already be sorted. This uses the Default comparer and throws if T is not comparable. If the item is found, its index is returned. Otherwise a negative value is returned representing the bitwise complement of the index where the item should be inserted.

[Pure]
public int BinarySearch(T item)

Parameters

item T

Returns

int

Contains(T)

Returns true when the list contains the specified item.

[Pure]
public bool Contains(T item)

Parameters

item T

Returns

bool

CopyTo(Span<T>)

Copy the contents of the list into an existing Span<T>.

public void CopyTo(Span<T> destination)

Parameters

destination Span<T>

Exceptions

ArgumentException

destination is shorter than the source slice.

Equals(ValueList<T>?)

Returns true when the two lists have identical length and content.

[Pure]
public bool Equals(ValueList<T>? other)

Parameters

other ValueList<T>

Returns

bool

GetEnumerator()

Returns an enumerator for this ValueList<T>.

Typically, you don't need to manually call this method, but instead use the built-in foreach syntax.

[Pure]
public ValueList<T>.Enumerator GetEnumerator()

Returns

ValueList<T>.Enumerator

GetHashCode()

Serves as the default hash function.

[Pure]
public override sealed int GetHashCode()

Returns

int

A hash code for the current object.

IndexOf(T)

Return the index of the first occurrence of item in the list, or -1 if not found.

[Pure]
public int IndexOf(T item)

Parameters

item T

Returns

int

LastIndexOf(T)

Return the index of the last occurrence of item in the list, or -1 if not found.

[Pure]
public int LastIndexOf(T item)

Parameters

item T

Returns

int

Slice(int)

Create a subslice, starting at offset.

This does not allocate any memory.

[Pure]
public ValueSlice<T> Slice(int offset)

Parameters

offset int

The index at which to begin the subslice.

Returns

ValueSlice<T>

Exceptions

ArgumentOutOfRangeException

offset is below 0 or greater than the current list's length.

Slice(int, int)

Create a subslice with a specified length, starting at offset.

This does not allocate any memory.

[Pure]
public ValueSlice<T> Slice(int offset, int length)

Parameters

offset int

The index at which to begin the subslice.

length int

The length of the new subslice.

Returns

ValueSlice<T>

Exceptions

ArgumentOutOfRangeException

offset is below 0 or greater than the current list's length.

ArgumentOutOfRangeException

length is below 0 or would extend beyond the current list's length.

ToArray()

Copy the contents of the list into a new array.

[Pure]
public T[] ToArray()

Returns

T[]

ToBuilder()

Create a new ValueListBuilder<T> with this list as its initial content. This builder can then be used to efficiently construct a new immutable ValueList<T>.

[Pure]
public ValueListBuilder<T> ToBuilder()

Returns

ValueListBuilder<T>

Remarks

The capacity of the returned builder may be larger than the size of this list. How much larger exactly is undefined.

ToBuilder(int)

Create a new ValueListBuilder<T> with a capacity of at least minimumCapacity and with this list as its initial content. This builder can then be used to efficiently construct a new immutable ValueList<T>.

[Pure]
public ValueListBuilder<T> ToBuilder(int minimumCapacity)

Parameters

minimumCapacity int

Returns

ValueListBuilder<T>

Remarks

This is functionally equivalent to:

list.ToBuilder().EnsureCapacity(minimumCapacity)

but without unnecessary intermediate copies.

Exceptions

ArgumentOutOfRangeException

minimumCapacity is less than 0.

ToString()

Get a string representation of the collection for debugging purposes. The format is not stable and may change without prior notice.

[Pure]
public override string ToString()

Returns

string

TryCopyTo(Span<T>)

Attempt to copy the contents of the list into an existing Span<T>. If the destination is too short, no items are copied and the method returns false.

public bool TryCopyTo(Span<T> destination)

Parameters

destination Span<T>

Returns

bool

Operators

operator ==(ValueList<T>?, ValueList<T>?)

Check for equality.

[Pure]
public static bool operator ==(ValueList<T>? left, ValueList<T>? right)

Parameters

left ValueList<T>
right ValueList<T>

Returns

bool

implicit operator ValueSlice<T>(ValueList<T>)

Convert list to slice.

[Pure]
public static implicit operator ValueSlice<T>(ValueList<T> list)

Parameters

list ValueList<T>

Returns

ValueSlice<T>

operator !=(ValueList<T>?, ValueList<T>?)

Check for inequality.

[Pure]
public static bool operator !=(ValueList<T>? left, ValueList<T>? right)

Parameters

left ValueList<T>
right ValueList<T>

Returns

bool