HashLookup<TKey, TElement>

Namespace: SnowBank.Collections.Generic · class

Implements: IEnumerable<Grouping<TKey, TElement>>, IEnumerable, ILookup<TKey, TElement>, IEnumerable<IGrouping<TKey, TElement>>

Lookup table that groups elements by a common key

Constructors

HashLookup<TKey, TElement>

HashLookup<TKey, TElement>()

HashLookup<TKey, TElement>(IEqualityComparer<TKey> comparer)

HashLookup<TKey, TElement>(int capacity, IEqualityComparer<TKey> comparer)

HashLookup<TKey, TElement>(HashLookup<TKey, TElement> elements, IEqualityComparer<TKey> comparer)

HashLookup<TKey, TElement>(ILookup<TKey, TElement> elements, IEqualityComparer<TKey> comparer)

Properties

Comparer

IEqualityComparer<TKey> Comparer { get; }

Count

int Count { get; }

Item

IEnumerable<TElement> Item { get; }

Keys

ICollection<TKey> Keys { get; }

Values

ICollection<Grouping<TKey, TElement>> Values { get; }

Methods

Add

void Add(TKey key, TElement element)

AddOrUpdateGrouping

Grouping<TKey, TElement> AddOrUpdateGrouping(TKey key, TElement element, out bool created)

Grouping<TKey, TElement> AddOrUpdateGrouping(TKey key, IEnumerable<TElement> elements, out bool created)

AddRange

void AddRange(TKey key, ReadOnlySpan<TElement> elements)

Add multiple elements to the grouping with the specified key

  • key — Key of the grouping
  • elements — Sequence of elements that will be added to this grouping

void AddRange(TKey key, TElement[] elements)

Add multiple elements to the grouping with the specified key

  • key — Key of the grouping
  • elements — Sequence of elements that will be added to this grouping

void AddRange(TKey key, IEnumerable<TElement> elements)

Add multiple elements to the grouping with the specified key

  • key — Key of the grouping
  • elements — Sequence of elements that will be added to this grouping

Clear

void Clear()

Clears all groupings

Contains

bool Contains(TKey key)

ForEach

int ForEach(Action<TKey, TElement[], int> handler)

Executes an action on each non-empty grouping in this table.

  • handler — Called with each grouping with at least one element. The 3rd argument is the number of items in the array.

Returns: Number of grouping that were processed

int ForEach(Action<TKey, ReadOnlyMemory<TElement>> handler)

Executes an action on each non-empty grouping in this table.

  • handler — Called with each grouping with at least one element.

Returns: Number of grouping that were processed

GetEnumerator

Enumerator GetEnumerator()

GetGrouping

Grouping<TKey, TElement> GetGrouping(TKey key, bool createIfMissing)

GetOrCreateGrouping

Grouping<TKey, TElement> GetOrCreateGrouping(TKey key, out bool created)

GetValueOrDefault

Grouping<TKey, TElement> GetValueOrDefault(TKey key, Grouping<TKey, TElement> missing = null)

Returns the grouping for the given key, or the default value

  • key — Key of the grouping
  • missing — Value returned if there is no grouping with this key

Returns: Corresponding grouping, or the default value

Remove

bool Remove(TKey key)

Remove the grouping with the specified key from the lookup table

  • key — Key of the grouping to remove

bool Remove(TKey key, TElement element, bool cleanupIfEmpty = false)

Remove an element from the lookup table

  • key — Key of the grouping containing the element
  • element — Element to remove
  • cleanupIfEmpty — If true and this was the last element in the grouping, it will be removed from the table; otherwise, the empty grouping will be kept.

Returns: true if a matching element was removed; otherwise, false

TryGetValue

bool TryGetValue(TKey key, out Grouping<TKey, TElement> elements)

Returns the grouping for the specified key, if it exists

  • key — Key of the grouping
  • elements — Receives the corresponding grouping

Returns: true if the grouping was found; otherwise, false