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 Builder<T>() or ToBuilder(). For creating ValueSets, ValueSetBuilder<T> 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

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(IEnumerable<T>)

Determines whether the current set is a proper (strict) subset of a specified collection.

public bool IsProperSubsetOf(IEnumerable<T> other)

Parameters

other IEnumerable<T>

The collection to compare to the current set.

Returns

bool

true if the current set is a proper subset of other; otherwise, false.

Exceptions

ArgumentNullException

other is null.

IsProperSupersetOf(IEnumerable<T>)

Determines whether the current set is a proper (strict) superset of a specified collection.

public bool IsProperSupersetOf(IEnumerable<T> other)

Parameters

other IEnumerable<T>

The collection to compare to the current set.

Returns

bool

true if the current set is a proper superset of other; otherwise, false.

Exceptions

ArgumentNullException

other is null.

IsSubsetOf(IEnumerable<T>)

Determines whether a set is a subset of a specified collection.

public bool IsSubsetOf(IEnumerable<T> other)

Parameters

other IEnumerable<T>

The collection to compare to the current set.

Returns

bool

true if the current set is a subset of other; otherwise, false.

Exceptions

ArgumentNullException

other is null.

IsSupersetOf(IEnumerable<T>)

Determines whether the current set is a superset of a specified collection.

public bool IsSupersetOf(IEnumerable<T> other)

Parameters

other IEnumerable<T>

The collection to compare to the current set.

Returns

bool

true if the current set is a superset of other; otherwise, false.

Exceptions

ArgumentNullException

other is null.

Overlaps(IEnumerable<T>)

Determines whether the current set overlaps with the specified collection.

public bool Overlaps(IEnumerable<T> other)

Parameters

other IEnumerable<T>

The collection to compare to the current set.

Returns

bool

true if the current set and other share at least one common element; otherwise, false.

Exceptions

ArgumentNullException

other is null.

SetEquals(IEnumerable<T>)

Determines whether the current set and the specified collection contain the same elements.

public bool SetEquals(IEnumerable<T> other)

Parameters

other IEnumerable<T>

The collection to compare to the current set.

Returns

bool

true if the current set is equal to other; otherwise, false.

Exceptions

ArgumentNullException

other is null.

ToBuilder()

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

[Pure]
public ValueSetBuilder<T> ToBuilder()

Returns

ValueSetBuilder<T>

Remarks

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

ToBuilder(int)

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

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

Parameters

minimumCapacity int

Returns

ValueSetBuilder<T>

Remarks

This is functionally equivalent to:

set.ToBuilder().EnsureCapacity(minimumCapacity)

but without unnecessary intermediate copies.

Available on .NET Standard 2.1 and .NET Core 2.1 and higher.

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

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