Buffer<T>

Namespace: SnowBank.Buffers · class

Implements: IReadOnlyList<T>, IEnumerable<T>, IEnumerable, IReadOnlyCollection<T>, IBufferWriter<T>, IDisposable

Small buffer that keeps a list of chunks that are larger and larger

Constructors

Buffer<T>

Buffer<T>(int capacity = 0, ArrayPool<T> pool = null)

Properties

IsSingleSegment

bool IsSingleSegment { get; }

Flag that sp

Item

T Item { get; }

Methods

Add

void Add(T item)

AddRange

void AddRange(T[] items)

void AddRange(ReadOnlySpan<T> items)

void AddRange(IEnumerable<T> items)

Advance

void Advance(int count)

Aggregate

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

Aggregates a value over all the elements in the buffer

AggregateSegments

TAggregate AggregateSegments<TAggregate>(TAggregate initialValue, Func<TAggregate, ReadOnlyMemory<T>, TAggregate> action)

Aggregates a value over all the segments of the buffer

CopyTo

int CopyTo(Span<T> destination)

Copies the content of the buffer into a destination span

Dispose

void Dispose()

EnumerateSegments

IEnumerable<ReadOnlyMemory<T>> EnumerateSegments()

Enumerates all the segments in this buffer

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<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)

ForEachSegments

void ForEachSegments<TState>(TState state, Action<TState, ReadOnlyMemory<T>> action)

Executes an action on each segment of the buffer

void ForEachSegments<TAggregate>(ref TAggregate aggregate, SegmentAggregator<T, TAggregate> action)

GetEnumerator

IEnumerator<T> GetEnumerator()

GetMemory

Memory<T> GetMemory(int sizeHint = 0)

GetReference

T GetReference(int index)

GetSpan

Span<T> GetSpan(int sizeHint = 0)

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.

Select

IEnumerable<TOther> Select<TOther>(Func<T, TOther> selector)

Projects each element of a buffer into a new form.

IEnumerable<TOther> Select<TOther>(Func<T, int, TOther> selector)

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

SelectMany

IEnumerable<TOther> SelectMany<TOther>(Func<ReadOnlyMemory<T>, IEnumerable<TOther>> selector)

Projects each element of a buffer into a new form.

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 returns all allocations to the pool

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

The buffer instance can be reused after this call.

ToDictionary

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

Returns the content of the buffer as a dictionary

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

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

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

ToHashSet

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

Returns the content of the buffer as a set

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

ToHashSetAndClear

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

Returns the content of the buffer as a set, and release all allocations to the pool

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

The buffer instance can be reused after this call.

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

ToImmutableArrayAndClear

ImmutableArray<T> ToImmutableArrayAndClear()

Returns the content of the buffer as an immutable array, and returns all allocations to the pool

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

The buffer instance can be reused after this call.

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 release all allocations to the pool

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

The buffer instance can be reused after this call.

ToMemory

ReadOnlyMemory<T> ToMemory()

Returns the content of the buffer as an array

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

TryCopyTo

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

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

TryGetSpan

bool TryGetSpan(out Span<T> segment)

Returns the content of the buffer as a span, if it fits in a single chunk

  • segment — Receives a view of the content, or default if the buffer spans multiple chunks

Returns: true if the buffer is empty, or its content is stored in a single continuous chunk, available in segment; otherwise, false

Where

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

Filters a buffer of values based on a predicate.

IEnumerable<T> Where(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.

Fields

Count

int Count

Number of items in the buffer