ConcatBuffer

Concat buffer class template.

Constructors

this
this(size_t len)

Constructor.

Members

Functions

add
T[] add(const(T)[] data)

Appends a new piece of data to the end of the buffer.

add
T[] add(size_t length)

Reserves a new piece of data at the end of the buffer.

clear
void clear()

Empties the buffer.

dimension
size_t dimension()

Parameters

T

element type of buffer

This template is useful for situations where you want to be able to repeatedly fill and empty a buffer of type T[] without recurring memory allocation. The buffer will grow over time to the required maximum size, and then will no longer allocate memory.

ConcatBuffer classes also avoid modifying the .length property of the internal buffer, which has been observed to be costly, even when extending an array's length to <= its previous length.

Internally the class stores a single buffer. If an item is added which does not fit in the currently allocated buffer, then a new expanded buffer is newed and replaces the old buffer. This means that the old buffer still exists in memory, and will not be garbage collected until there are no more references to it. As a result of this behaviour, any slices remaining to the previous buffer may still safely be used. Only at the point where all these slices no longer reference the old buffer will it be garbage collected.

Usage example:

import ocean.util.container.ConcatBuffer;

// Create a concat buffer
auto buff = new ConcatBuffer!(char);

// Repeatedly...
while ( application_running )
{
    // Empty the buffer
    buff.clear();

    // Add stuff to the buffer
    buff.add("hello");
    buff.add("world");
}

Meta