Table of Contents

Class Hashset<T>

Namespace
NumSharp.Utilities
Assembly
NumSharp.dll
[SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix", Justification = "By design")]
public class Hashset<T> : ISet<T>, ICollection<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable

Type Parameters

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

Constructors

Hashset()

public Hashset()

Hashset(IEnumerable<T>)

public Hashset(IEnumerable<T> collection)

Parameters

collection IEnumerable<T>

Hashset(IEnumerable<T>, IEqualityComparer<T>)

Implementation Notes: Since resizes are relatively expensive (require rehashing), this attempts to minimize the need to resize by setting the initial capacity based on size of collection.

public Hashset(IEnumerable<T> collection, IEqualityComparer<T> comparer)

Parameters

collection IEnumerable<T>
comparer IEqualityComparer<T>

Hashset(IEqualityComparer<T>)

public Hashset(IEqualityComparer<T> comparer)

Parameters

comparer IEqualityComparer<T>

Hashset(int)

public Hashset(int capacity)

Parameters

capacity int

Hashset(int, IEqualityComparer<T>)

public Hashset(int capacity, IEqualityComparer<T> comparer)

Parameters

capacity int
comparer IEqualityComparer<T>

Properties

Comparer

Gets the IEqualityComparer that is used to determine equality of keys for the HashSet.

public IEqualityComparer<T> Comparer { get; }

Property Value

IEqualityComparer<T>

Count

Number of elements in this hashset

public int Count { get; }

Property Value

int

Methods

Add(T)

Add item to this HashSet. Returns bool indicating whether item was added (won't be added if already present)

public bool Add(T item)

Parameters

item T

Returns

bool

true if added, false if already present

Clear()

Remove all items from this set. This clears the elements but not the underlying buckets and slots array. Follow this call by TrimExcess to release these.

public void Clear()

Contains(T)

Checks if this hashset contains the item

public bool Contains(T item)

Parameters

item T

item to check for containment

Returns

bool

true if item contained; false if not

CopyTo(T[])

public void CopyTo(T[] array)

Parameters

array T[]

CopyTo(T[], int)

Copy items in this hashset to array, starting at arrayIndex

public void CopyTo(T[] array, int arrayIndex)

Parameters

array T[]

array to add items to

arrayIndex int

index to start at

CopyTo(T[], int, int)

public void CopyTo(T[] array, int arrayIndex, int count)

Parameters

array T[]
arrayIndex int
count int

CopyTo<T>(Hashset<T>, ArraySlice<T>)

public static void CopyTo<T>(Hashset<T> src, ArraySlice<T> array) where T : unmanaged

Parameters

src Hashset<T>
array ArraySlice<T>

Type Parameters

T

CopyTo<T>(Hashset<T>, ArraySlice<T>, int, int)

public static void CopyTo<T>(Hashset<T> src, ArraySlice<T> array, int arrayIndex, int count) where T : unmanaged

Parameters

src Hashset<T>
array ArraySlice<T>
arrayIndex int
count int

Type Parameters

T

CreateSetComparer()

Used for deep equality of HashSet testing

public static IEqualityComparer<Hashset<T>> CreateSetComparer()

Returns

IEqualityComparer<Hashset<T>>

ExceptWith(IEnumerable<T>)

Remove items in other from this set. Modifies this set.

public void ExceptWith(IEnumerable<T> other)

Parameters

other IEnumerable<T>

enumerable with items to remove

GetEnumerator()

public Hashset<T>.Enumerator GetEnumerator()

Returns

Hashset<T>.Enumerator

IntersectWith(IEnumerable<T>)

Takes the intersection of this set with other. Modifies this set.

Implementation Notes: We get better perf if other is a hashset using same equality comparer, because we get constant contains check in other. Resulting cost is O(n1) to iterate over this.

If we can't go above route, iterate over the other and mark intersection by checking contains in this. Then loop over and delete any unmarked elements. Total cost is n2+n1.

Attempts to return early based on counts alone, using the property that the intersection of anything with the empty set is the empty set.

public void IntersectWith(IEnumerable<T> other)

Parameters

other IEnumerable<T>

enumerable with items to add

IsProperSubsetOf(IEnumerable<T>)

Checks if this is a proper subset of other (i.e. strictly contained in)

Implementation Notes: The following properties are used up-front to avoid element-wise checks:

  1. If this is the empty set, then it's a proper subset of a set that contains at least one element, but it's not a proper subset of the empty set.
  2. If other has unique elements according to this equality comparer, and this has >= the number of elements in other, then this can't be a proper subset.

Furthermore, if other is a hashset using the same equality comparer, we can use a faster element-wise check.

public bool IsProperSubsetOf(IEnumerable<T> other)

Parameters

other IEnumerable<T>

Returns

bool

true if this is a proper subset of other; false if not

IsProperSupersetOf(IEnumerable<T>)

Checks if this is a proper superset of other (i.e. other strictly contained in this)

Implementation Notes: This is slightly more complicated than above because we have to keep track if there was at least one element not contained in other.

The following properties are used up-front to avoid element-wise checks:

  1. If this is the empty set, then it can't be a proper superset of any set, even if other is the empty set.
  2. If other is an empty set and this contains at least 1 element, then this is a proper superset.
  3. If other has unique elements according to this equality comparer, and other's count is greater than or equal to this count, then this can't be a proper superset

Furthermore, if other has unique elements according to this equality comparer, we can use a faster element-wise check.

public bool IsProperSupersetOf(IEnumerable<T> other)

Parameters

other IEnumerable<T>

Returns

bool

true if this is a proper superset of other; false if not

IsSubsetOf(IEnumerable<T>)

Checks if this is a subset of other.

Implementation Notes: The following properties are used up-front to avoid element-wise checks:

  1. If this is the empty set, then it's a subset of anything, including the empty set
  2. If other has unique elements according to this equality comparer, and this has more elements than other, then it can't be a subset.

Furthermore, if other is a hashset using the same equality comparer, we can use a faster element-wise check.

public bool IsSubsetOf(IEnumerable<T> other)

Parameters

other IEnumerable<T>

Returns

bool

true if this is a subset of other; false if not

IsSupersetOf(IEnumerable<T>)

Checks if this is a superset of other

Implementation Notes: The following properties are used up-front to avoid element-wise checks:

  1. If other has no elements (it's the empty set), then this is a superset, even if this is also the empty set.
  2. If other has unique elements according to this equality comparer, and this has less than the number of elements in other, then this can't be a superset
public bool IsSupersetOf(IEnumerable<T> other)

Parameters

other IEnumerable<T>

Returns

bool

true if this is a superset of other; false if not

Overlaps(IEnumerable<T>)

Checks if this set overlaps other (i.e. they share at least one item)

public bool Overlaps(IEnumerable<T> other)

Parameters

other IEnumerable<T>

Returns

bool

true if these have at least one common element; false if disjoint

Remove(T)

Remove item from this hashset

public bool Remove(T item)

Parameters

item T

item to remove

Returns

bool

true if removed; false if not (i.e. if the item wasn't in the HashSet)

RemoveWhere(Predicate<T>)

Remove elements that match specified predicate. Returns the number of elements removed

public int RemoveWhere(Predicate<T> match)

Parameters

match Predicate<T>

Returns

int

SetEquals(IEnumerable<T>)

Checks if this and other contain the same elements. This is set equality: duplicates and order are ignored

public bool SetEquals(IEnumerable<T> other)

Parameters

other IEnumerable<T>

Returns

bool

SymmetricExceptWith(IEnumerable<T>)

Takes symmetric difference (XOR) with other and this set. Modifies this set.

public void SymmetricExceptWith(IEnumerable<T> other)

Parameters

other IEnumerable<T>

enumerable with items to XOR

TrimExcess()

Sets the capacity of this list to the size of the list (rounded up to nearest prime), unless count is 0, in which case we release references.

This method can be used to minimize a list's memory overhead once it is known that no new elements will be added to the list. To completely clear a list and release all memory referenced by the list, execute the following statements:

list.Clear(); list.TrimExcess();

public void TrimExcess()

TryGetValue(T, out T)

Searches the set for a given value and returns the equal value it finds, if any.

public bool TryGetValue(T equalValue, out T actualValue)

Parameters

equalValue T

The value to search for.

actualValue T

The value from the set that the search found, or the default value of T when the search yielded no match.

Returns

bool

A value indicating whether the search was successful.

Remarks

This can be useful when you want to reuse a previously stored reference instead of a newly constructed one (so that more sharing of references can occur) or to look up a value that has more complete data than the value you currently have, although their comparer functions indicate they are equal.

UnionWith(IEnumerable<T>)

Take the union of this HashSet with other. Modifies this set.

Implementation note: GetSuggestedCapacity (to increase capacity in advance avoiding multiple resizes ended up not being useful in practice; quickly gets to the point where it's a wasteful check.

public void UnionWith(IEnumerable<T> other)

Parameters

other IEnumerable<T>

enumerable with items to add