Appender

Implements an output range that appends data to an array. This is recommended over array ~= data when appending many elements because it is more efficient. Appender maintains its own array metadata locally, so it can avoid global locking for each append where capacity is non-zero.

Constructors

this
this(A arr)

Constructs an Appender with a given array. Note that this does not copy the data. If the array has a larger capacity as determined by arr.capacity, it will be used by the appender. After initializing an appender on an array, appending to the original array will reallocate.

Members

Aliases

opOpAssign
alias opOpAssign(string op : "~") = put

Appends to the managed array.

Functions

clear
void clear()

Clears the managed array. This allows the elements of the array to be reused for appending.

put
void put(U item)

Appends item to the managed array. Performs encoding for char types if A is a differently typed char array.

put
void put(Range items)

Appends an entire range to the managed array. Performs encoding for char elements if A is a differently typed char array.

reserve
void reserve(size_t newCapacity)

Reserve at least newCapacity elements for appending. Note that more elements may be reserved than requested. If newCapacity <= capacity, then nothing is done.

shrinkTo
void shrinkTo(size_t newlength)

Shrinks the managed array to the given length.

toString
string toString()

Gives a string in the form of Appender!(A)(data).

Properties

capacity
size_t capacity [@property getter]
data
inout(ElementEncodingType!A)[] data [@property getter]

Use opSlice() from now on.

opSlice
inout(ElementEncodingType!A)[] opSlice [@property getter]

Templates

toString
template toString(Writer)

Gives a string in the form of Appender!(A)(data).

Examples

auto app = appender!string();
string b = "abcdefg";
foreach (char c; b)
    app.put(c);
assert(app[] == "abcdefg");

int[] a = [ 1, 2 ];
auto app2 = appender(a);
app2.put(3);
app2.put([ 4, 5, 6 ]);
assert(app2[] == [ 1, 2, 3, 4, 5, 6 ]);

See Also

Meta

Suggestion Box / Bug Report