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

If the caller does not need to consume the items as a single span, SegmentedValueBuffer may be faster

Constructors

SegmentedValueBuffer<T>

SegmentedValueBuffer<T>()

YOU MUST PROVIDE AN INITIAL SCRATCH SPACE!

SegmentedValueBuffer<T>(Span<T> scratch, ArrayPool<T> pool = null)

Initializes an empty buffer, using the supplied scratch as initial space

  • scratch — Scratch used, until more space is required
  • pool — Pool used to rent additional segments (or Shared)

This instance MUST be disposed, otherwise any rented buffers will NOT be returned to the pool!

Properties

Capacity

int Capacity { get; }

Returns the current capacity of the buffer

Count

int Count { get; }

Returns the number of items that have been added to the buffer

IsSingleSegment

bool IsSingleSegment { get; }

Indicates if the buffer content does fit in a single consecutive span

Item

T Item { get; }

Methods

Add

void Add(T item)

Appends an item to the end of the buffer

AddRange

void AddRange(ReadOnlySpan<T> items)

Appends a range of items to the end of the buffer

void AddRange(T[] items)

Appends an array of items to the end of the buffer

void AddRange(IEnumerable<T> items)

Appends a sequence of items to the end of the buffer

Clear

void Clear()

Clears the content of the buffer, so that it can be reused immediately

CopyTo

int CopyTo(Span<T> destination)

Copies the content of the buffer into a destination span

void CopyTo(IBufferWriter<T> writer)

Copies the content of the buffer into a destination IBufferWriter

Dispose

void Dispose()

Release any rented buffer used by this instance

It is NOT same to continue using this buffer after Dispose has been called at once!

GetEnumerator

Enumerator GetEnumerator()

Enumerates the values inserted into to a SegmentedValueBuffer

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 it for immediate re-use

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

After this call, the buffer can be used again for the next batch of item

If the buffer is only used once, it is faster to call ToArray and the Dispose

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

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

After this call, the buffer can be used again for the next batch of item

If the buffer is only used once, it is faster to call ToList and the Dispose

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

TryGetSpan

bool TryGetSpan(out Span<T> span)

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