ValueBuffer<T>

Namespace: SnowBank.Buffers · struct

Implements: IDisposable

Buffer that will accumulate data in a contiguous span, starting from a stack allocated buffer, and switching to pooled buffers if required

Remarks

The final list of items will be available as a single contiguous Span

Consider using SegmentedValueBuffer if you do not need to consume the items as a single span, SegmentedValueBuffer may be faster

Constructors

ValueBuffer<T>

ValueBuffer<T>()

YOU MUST PROVIDE AN INITIAL CAPACITY OR SCRATCH SPACE!

ValueBuffer<T>(Span<T> initialBuffer)

Constructs a ValueBuffer using an already allocated scratch buffer

  • initialBuffer — Buffer used initially.

ValueBuffer<T>(int capacity)

Constructs a ValueBuffer with an initial capacity

  • capacity — Capacity of the initial buffer (allocated on a pool)

Properties

Capacity

int Capacity { get; }

Returns the current capacity of the buffer

Item

T Item { get; }

Span

Span<T> Span { get; }

Returns a span with all the items already written to this buffer

Methods

Add

void Add(T item)

Add an item to the end of the buffer

  • item — Item to add

AddRange

void AddRange(ReadOnlySpan<T> items)

Add a span of items at the end of the buffer

  • items — Items to add

void AddRange(IEnumerable<T> items)

Add a sequence of items at the end of the buffer

  • items — Items to add

Advance

void Advance(int count)

Notifies the ValueBuffer that count data items were written to the output Span.

Aggregate

TAggregate Aggregate<TAggregate>(TAggregate initialValue, Func<TAggregate, T, TAggregate> action)

Aggregates a value over all the elements in the buffer

Clear

void Clear()

Clears the content of the buffer

The buffer count is reset to zero, but the current backing store remains the same

CopyTo

int CopyTo(Span<T> destination)

Copies the content of the buffer into a destination span

Dispose

void Dispose()

FilterAndProjectTo

int FilterAndProjectTo<TOther>(Span<TOther> destination, Func<T, bool> predicate, Func<T, TOther> selector)

Projects any elements that match a predicate of a buffer into a new form.

int FilterAndProjectTo<TOther>(Span<TOther> destination, Func<T, int, bool> predicate, Func<T, int, TOther> selector)

Projects any elements that match a predicate of a buffer into a new form.

FilterTo

int FilterTo(Span<T> destination, Func<T, bool> predicate)

Filters a buffer of values based on a predicate.

int FilterTo(Span<T> destination, Func<T, int, bool> predicate)

Filters a buffer of values based on a predicate. Each element's index is used in the logic of the predicate function.

First

T First()

Returns the first element of a buffer.

FirstOrDefault

T FirstOrDefault()

Returns the first element of a buffer, or a default value if the buffer contains no elements.

ForEach

void ForEach(Action<T> action)

Executes an action on each element in the buffer

void ForEach<TState>(TState state, Action<TState, T> action)

Executes an action on each element in the buffer

void ForEach<TAggregate>(ref TAggregate aggregate, Aggregator<T, TAggregate> action)

GetEnumerator

Enumerator GetEnumerator()

Returns an enumerator that will list all the items added to the buffer so far.

GetSpan

Span<T> GetSpan(int sizeHint = 0)

Returns a Span to write to that is at least the requested size (specified by sizeHint).

Last

T Last()

Returns the last element of a buffer.

LastOrDefault

T LastOrDefault()

Returns the last element of a buffer, or a default value if the buffer contains no elements.

ProjectTo

int ProjectTo<TOther>(Span<TOther> destination, Func<T, TOther> selector)

Projects each element of a buffer into a new form.

int ProjectTo<TOther>(Span<TOther> destination, Func<T, int, TOther> selector)

Projects each element of a buffer into a new form by incorporating the element's index.

Single

T Single()

Returns the only element of a buffer, and throws an exception if there is not exactly one element in the buffer.

SingleOrDefault

T SingleOrDefault()

Returns the only element of a buffer, or a default value if the buffer is empty; this method throws an exception if there is more than one element in the buffer.

ToArray

T[] ToArray()

Returns the content of the buffer as an array

Returns: Array of size Count containing all the items in this buffer

ToArrayAndClear

T[] ToArrayAndClear()

Returns the content of the buffer as an array, and clears the buffer for reuse

Returns: Array of size Count containing all the items in this buffer

The buffer is cleared and any allocated buffer returned to the pool.

ToDictionary

Dictionary<TKey, T> ToDictionary<TKey>(Func<T, TKey> keySelector, IEqualityComparer<TKey> comparer = null)

Returns the content of the buffer as a dictionary

  • keySelector — Selector used to extract the keys for each item in the buffer.
  • comparer — Key comparer used by the dictionary.

Returns: Dictionary containing all the items in this buffer, using keySelector to generate the keys.

Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(Func<T, TKey> keySelector, Func<T, TValue> valueSelector, IEqualityComparer<TKey> comparer = null)

Returns the content of the buffer as a dictionary

  • keySelector — Selector used to extract the keys for each item in the buffer.
  • valueSelector — Selector used to extract the values for each item in the buffer.
  • comparer — Key comparer used by the dictionary.

Returns: Dictionary containing all the mapped items in this buffer, using keySelector for the keys, and valueSelector for the values.

ToDictionaryAndClear

Dictionary<TKey, T> ToDictionaryAndClear<TKey>(Func<T, TKey> keySelector, IEqualityComparer<TKey> comparer = null)

Returns the content of the buffer as a dictionary, and clears the buffer for reuse

  • keySelector — Selector used to extract the keys for each item in the buffer.
  • comparer — Key comparer used by the dictionary.

Returns: Dictionary containing all the items in this buffer, using keySelector to generate the keys.

The buffer is cleared and any allocated buffer returned to the pool.

Dictionary<TKey, TValue> ToDictionaryAndClear<TKey, TValue>(Func<T, TKey> keySelector, Func<T, TValue> valueSelector, IEqualityComparer<TKey> comparer = null)

Returns the content of the buffer as a dictionary, and clears the buffer for reuse

  • keySelector — Selector used to extract the keys for each item in the buffer.
  • valueSelector — Selector used to extract the values for each item in the buffer.
  • comparer — Key comparer used by the dictionary.

Returns: Dictionary containing all the mapped items in this buffer, using keySelector for the keys, and valueSelector for the values.

The buffer is cleared and any allocated buffer returned to the pool.

ToHashSet

HashSet<T> ToHashSet(IEqualityComparer<T> comparer = null)

Returns the content of the buffer as a set

Returns: Set containing all the items in this buffer

ToHashSetAndClear

HashSet<T> ToHashSetAndClear(IEqualityComparer<T> comparer = null)

Returns the content of the buffer as a set, and clears the buffer for reuse

Returns: Set containing all the items in this buffer

The buffer is cleared and any allocated buffer returned to the pool.

ToImmutableArray

ImmutableArray<T> ToImmutableArray()

Returns the content of the buffer as an immutable array

Returns: Array of size Count containing all the items in this buffer

ToList

List<T> ToList()

Returns the content of the buffer as a list

Returns: List of size Count containing all the items in this buffer

ToListAndClear

List<T> ToListAndClear()

Returns the content of the buffer as a list, and clears the buffer for reuse

Returns: List of size Count containing all the items in this buffer

The buffer is cleared and any allocated buffer returned to the pool.

ToString

string ToString()

TryCopyTo

bool TryCopyTo(Span<T> destination, out int written)

Copies the content of the buffer into a destination span, if it is large enough

Fields

Count

int Count

Number of items in the buffer