Grouping<TKey, TElement>

Namespace: SnowBank.Collections.Generic · class

Implements: IGrouping<TKey, TElement>, IEnumerable<TElement>, IEnumerable, IList<TElement>, ICollection<TElement>

Container for a set of elements that share the same key

Constructors

Grouping<TKey, TElement>

Grouping<TKey, TElement>()

Properties

Count

int Count { get; }

Get the number of elements contained in the Grouping

First

TElement First { get; }

Get the first element of the grouping (or default(T) if empty)

Item

TElement Item { get; set; }

Key

TKey Key { get; }

Get the key of the Grouping

Last

TElement Last { get; }

Get the last element of the grouping (or default(T) if empty)

Memory

ReadOnlyMemory<TElement> Memory { get; }

Span

ReadOnlySpan<TElement> Span { get; }

Methods

Add

void Add(TElement element)

Add an item to this grouping

AddRange

void AddRange(ReadOnlySpan<TElement> elements)

Adds a batch of elements to this grouping

  • elements — Span of elements (can be empty)

void AddRange(TElement[] elements)

Adds a batch of elements to this grouping

  • elements — Array of elements (can be null or empty)

void AddRange(IEnumerable<TElement> elements)

Adds a batch of elements to this grouping

  • elements — Sequence of elements (can be empty)

void AddRange(Grouping<TKey, TElement> grouping)

Adds all the elements of another grouping to this grouping

  • grouping — Grouping that must be merged with this one

No attempt will be made to de-dup the items. If the both grouping contain the same item, it will be duplicated!

All

bool All(Func<TElement, bool> predicate)

Any

bool Any(Func<TElement, bool> predicate)

Clear

void Clear()

Clears all elements from this grouping

CopyTo

void CopyTo(TElement[] array, int arrayIndex)

Copy the elements of this grouping into an array

The destinatino array must be large enough to receive all elements.

ForEach

void ForEach(Action<TElement> action)

Runs an action on each element in this grouping

  • action — Lambda that will be called with each element, one by one

void ForEach(Action<TKey, TElement> action)

Runs an action on each element in this grouping

  • action — Lambda that will be called with each element, one by one

GetEnumerator

Enumerator GetEnumerator()

Returns an enumerator that iterates through the grouping.

IndexOf

int IndexOf(Func<TElement, bool> match)

Map

IEnumerable<KeyValuePair<TKey, TElement>> Map(Func<TElement, bool> selector = null)

Converts the grouping into a sequence of key/value pairs

  • selector — Optional filter (return true for elements to keep, and false for elements to discard)

Returns: Sequence of KeyValuePair where Key is the grouping's key, and Value is an element of this grouping

Pop

TElement Pop()

Removes and return the last element of the grouping

Returns: Last element

Remove

bool Remove(TElement element)

Removes an element from this grouping

  • element — Element to remove

Returns: true if the element was present and has been removed; otherwise, false

Uses EqualityComparer.Default to compare the elements. Warning: will only remove the first occurrence found, in case of duplicates!

bool Remove(TElement element, IEqualityComparer<TElement> comparer)

Removes an element from this grouping

  • element — Element to remove
  • comparer — Comparer used to find the element

Returns: true if the element was present and has been removed; otherwise, false

Warning: will only remove the first occurrence found, in case of duplicates!

RemoveAll

int RemoveAll(Func<TElement, bool> match)

Remove all elements that match the specified predicate

  • match — Predicate that returns true for each element to remove, and false for elements to keep.

Number of removed elements (0 if grouping was empty, or no match found)

RemoveAt

void RemoveAt(int index)

Removes the element at the specified position

ToArray

TElement[] ToArray()

ToList

List<TElement> ToList()

UpdateLast

void UpdateLast(TElement newValue)

Updates the last element, or add it if the grouping was empty

  • newValue — New value for the last element

Where

IEnumerable<TElement> Where(Func<TElement, bool> predicate)