Table of Contents

Class ValueSet<T>

Namespace
Badeend.ValueCollections
Assembly
Badeend.ValueCollections.dll

An immutable, thread-safe set with value semantics.

public sealed class ValueSet<T> : ISet<T>, ICollection<T>, IEquatable<ValueSet<T>>, IReadOnlySet<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable

Type Parameters

T

The type of items in the set.

Inheritance
ValueSet<T>
Implements
ISet<T>
Inherited Members
Extension Methods

Remarks

A set is a collection of unique elements (i.e. no duplicates) in no particular order.

Constructing new instances can be done using CreateBuilder<T>() or ToBuilder(). For creating ValueSets, ValueSet<T>.Builder is generally more efficient than HashSet<T>.

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

The order in which the elements are enumerated is undefined.

Properties

Count

Number of items in the set.

[Pure]
public int Count { get; }

Property Value

int

Empty

Get an empty set.

This does not allocate any memory.

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

Property Value

ValueSet<T>

IsEmpty

Shortcut for .Count == 0.

[Pure]
public bool IsEmpty { get; }

Property Value

bool

Methods

Contains(T)

Returns true when the set contains the specified item.

[Pure]
public bool Contains(T item)

Parameters

item T

Returns

bool

CopyTo(Span<T>)

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

public void CopyTo(Span<T> destination)

Parameters

destination Span<T>

Remarks

The order in which the elements are copied is undefined.

Exceptions

ArgumentException

destination is shorter than the source slice.

Equals(ValueSet<T>?)

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

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

Parameters

other ValueSet<T>

Returns

bool

GetEnumerator()

Returns an enumerator for this ValueSet<T>.

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

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

Returns

ValueSet<T>.Enumerator

GetHashCode()

Serves as the default hash function.

[Pure]
public override sealed int GetHashCode()

Returns

int

A hash code for the current object.

IsProperSubsetOf(ValueSet<T>)

Check whether this set is a proper subset of the provided collection.

public bool IsProperSubsetOf(ValueSet<T> other)

Parameters

other ValueSet<T>

Returns

bool

Remarks

This is an O(n) operation, where n is the number of elements in this.

An overload that takes any IEnumerable<T> exists as an extension method. Beware of the performance implications though.

IsProperSupersetOf(ValueSet<T>)

Check whether this set is a proper superset of the provided collection.

public bool IsProperSupersetOf(ValueSet<T> other)

Parameters

other ValueSet<T>

Returns

bool

Remarks

This is an O(n) operation, where n is the number of elements in this.

An overload that takes any IEnumerable<T> exists as an extension method. Beware of the performance implications though.

IsSubsetOf(ValueSet<T>)

Check whether this set is a subset of the provided collection.

public bool IsSubsetOf(ValueSet<T> other)

Parameters

other ValueSet<T>

Returns

bool

Remarks

This is an O(n) operation, where n is the number of elements in this.

An overload that takes any IEnumerable<T> exists as an extension method. Beware of the performance implications though.

IsSupersetOf(ValueSet<T>)

Check whether this set is a superset of the provided collection.

public bool IsSupersetOf(ValueSet<T> other)

Parameters

other ValueSet<T>

Returns

bool

Remarks

This is an O(n) operation, where n is the number of elements in other.

An overload that takes any IEnumerable<T> exists as an extension method.

IsSupersetOf(scoped ReadOnlySpan<T>)

Check whether this set is a superset of the provided sequence.

public bool IsSupersetOf(scoped ReadOnlySpan<T> other)

Parameters

other ReadOnlySpan<T>

Returns

bool

Remarks

This is an O(n) operation, where n is the number of elements in other.

Overlaps(ValueSet<T>)

Check whether this set and the provided collection share any common elements.

public bool Overlaps(ValueSet<T> other)

Parameters

other ValueSet<T>

Returns

bool

Remarks

This is an O(n) operation, where n is the number of elements in other.

An overload that takes any IEnumerable<T> exists as an extension method.

Overlaps(scoped ReadOnlySpan<T>)

Check whether this set and the provided sequence share any common elements.

public bool Overlaps(scoped ReadOnlySpan<T> other)

Parameters

other ReadOnlySpan<T>

Returns

bool

Remarks

This is an O(n) operation, where n is the number of elements in other.

SetEquals(ValueSet<T>)

Check whether this set and the provided collection contain the same elements, ignoring the order of the elements.

public bool SetEquals(ValueSet<T> other)

Parameters

other ValueSet<T>

Returns

bool

Remarks

This is an O(n) operation, where n is the number of elements in other.

An overload that takes any IEnumerable<T> exists as an extension method. Beware of the performance implications though.

ToBuilder()

Create a new ValueSet<T>.Builder with this set as its initial content. This builder can then be used to efficiently construct a new immutable ValueSet<T>.

[Pure]
public ValueSet<T>.Builder ToBuilder()

Returns

ValueSet<T>.Builder

Remarks

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

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

Operators

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

Check for equality.

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

Parameters

left ValueSet<T>
right ValueSet<T>

Returns

bool

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

Check for inequality.

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

Parameters

left ValueSet<T>
right ValueSet<T>

Returns

bool