std.array
Functions and types that manipulate built-in arrays and associative arrays.
This module provides all kinds of functions to create, manipulate or convert arrays:
Copyright
Copyright Andrei Alexandrescu 2008- and Jonathan M Davis 2011-.
Types 2
structAppender(A) if (isDynamicArray!A)
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 the spec/arrays for each append.
Parameters
A | the array type to simulate. |
See Also
Fields
private Data * _dataMethods
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.size_t capacity() @property constReturns: the capacity of the array (the maximum number of elements the managed array can accommodate before triggering a reallocation). If any appending will reallocate, `0` will be returned.private
void ensureAddable(size_t nelems)void put(U)(U item) if (canPutItem!U)Appends `item` to the managed array. Performs encoding for `char` types if `A` is a differently typed `char` array.Constructors
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 ini...structRefAppender(A) if (isDynamicArray!A)
A version of Appender that can update an array in-place. It forwards all calls to an underlying appender implementation. Any calls made to the appender also update the pointer to the original array passed in.
Tip: Use the arrayPtr overload of appender for construction with type-inference.
Parameters
A | The array type to simulate |
Methods
void opDispatch(string fn, Args...)(Args args) if (__traits(compiles, (Appender!A a) => mixin("a." ~ fn ~ "(args)")))Wraps remaining `Appender` methods such as put. Params: fn = Method name to call. args = Arguments to pass to the method.void opOpAssign(string op : "~", U)(U rhs) if (__traits(compiles, (Appender!A a) { a.put(rhs); }))Appends `rhs` to the managed array. Params: rhs = Element or range.size_t capacity() @property constReturns the capacity of the array (the maximum number of elements the managed array can accommodate before triggering a reallocation). If any appending will reallocate, `capacity` returns `0`.Constructors
this(A * arr)Constructs a `RefAppender` with a given array reference. 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.Functions 45
fn
ForeachType!Range[] array(Range)(Range r) if (isIterable!Range && !isAutodecodableString!Range && !isInfinite!Range)Allocates an array and initializes it with copies of the elements of range `r`.fn
ForeachType!(typeof((* Range).init))[] array(Range)(Range r) if (is(Range == U *, U) && isIterable!U && !isAutodecodableString!Range && !isInfinite!Range)dittofn
CopyTypeQualifiers!(ElementType!String, dchar)[] array(String)(scope String str) if (isAutodecodableString!String)Convert a narrow autodecoding string to an array type that fully supports random access. This is handled as a special case and always returns an array of `dchar`fn
auto assocArray(Range)(Range r) if (isInputRange!Range)Returns a newly allocated associative array from a range of key/value tuples or from a range of keys and a range of values.fn
auto assocArray(Keys, Values)(Keys keys, Values values) if (isInputRange!Values && isInputRange!Keys)dittofn
auto byPair(AA)(AA aa) if (isAssociativeArray!AA)Construct a range iterating over an associative array by key/value tuples.fn
auto uninitializedArray(T, I...)(I sizes) if (isDynamicArray!T && allSatisfy!(isIntegral, I) && hasIndirections!(ElementEncodingType!T)) nothrow @systemReturns a new array of type `T` allocated on the garbage collected heap without initializing its elements. This can be a useful optimization if every element will be immediately initialized. `T` ma...fn
auto uninitializedArray(T, I...)(I sizes) if (isDynamicArray!T && allSatisfy!(isIntegral, I) && !hasIndirections!(ElementEncodingType!T)) nothrow @trusteddittofn
auto minimallyInitializedArray(T, I...)(I sizes) if (isDynamicArray!T && allSatisfy!(isIntegral, I)) nothrow @trustedReturns a new array of type `T` allocated on the garbage collected heap.fn
CommonType!(T[], U[]) overlap(T, U)(T[] a, U[] b) if (is(typeof(a.ptr < b.ptr) == bool)) @trustedReturns the overlapping portion, if any, of two arrays. Unlike `equal`, `overlap` only compares the pointers and lengths in the ranges, not the values referred by them. If `r1` and `r2` have an ove...fn
void insertInPlace(T, U...)(ref T[] array, size_t pos, U stuff) if (!isSomeString!(T[])
&& allSatisfy!(isInputRangeOrConvertible!T, U) && U.length > 0)Inserts `stuff` (which must be an input range or any number of implicitly convertible items) in `array` at position `pos`.fn
void insertInPlace(T, U...)(ref T[] array, size_t pos, U stuff) if (isSomeString!(T[]) && allSatisfy!(isCharOrStringOrDcharRange, U))Dittofn
bool sameHead(T)(in T[] lhs, in T[] rhs) @safe pure nothrow @nogcReturns whether the `front`s of `lhs` and `rhs` both refer to the same place in memory, making one of the arrays a slice of the other which starts at index `0`.fn
bool sameTail(T)(in T[] lhs, in T[] rhs) @trusted pure nothrow @nogcReturns whether the `back`s of `lhs` and `rhs` both refer to the same place in memory, making one of the arrays a slice of the other which end at index `$`.fn
ElementEncodingType!S[] replicate(S)(S s, size_t n) if (isDynamicArray!S)Params: s = an input range or a dynamic array n = number of times to repeat `s`fn
S[] split(S)(S s) if (isSomeString!S) @safe pureEagerly splits `range` into an array, using `sep` as the delimiter.fn
auto split(Range, Separator)(Range range, Separator sep) if (isForwardRange!Range && (
is(typeof(ElementType!Range.init == Separator.init)) ||
is(typeof(ElementType!Range.init == ElementType!Separator.init)) && isForwardRange!Separator
))dittofn
auto split(alias isTerminator, Range)(Range range) if (isForwardRange!Range && is(typeof(unaryFun!isTerminator(range.front))))dittofn
ElementEncodingType!(ElementType!RoR)[] join(RoR, R)(RoR ror, R sep) if (isInputRange!RoR &&
isInputRange!(Unqual!(ElementType!RoR)) &&
isInputRange!R &&
(is(immutable ElementType!(ElementType!RoR) == immutable ElementType!R) ||
(isSomeChar!(ElementType!(ElementType!RoR)) && isSomeChar!(ElementType!R))
))Eagerly concatenates all of the ranges in `ror` together (with the GC) into one array using `sep` as the separator if present.fn
ElementEncodingType!(ElementType!RoR)[] join(RoR, E)(RoR ror, scope E sep) if (isInputRange!RoR &&
isInputRange!(Unqual!(ElementType!RoR)) &&
((is(E : ElementType!(ElementType!RoR))) ||
(!autodecodeStrings && isSomeChar!(ElementType!(ElementType!RoR)) &&
isSomeChar!E)))Dittofn
ElementEncodingType!(ElementType!RoR)[] join(RoR)(RoR ror) if (isInputRange!RoR &&
isInputRange!(Unqual!(ElementType!RoR)))Dittofn
E[] replace(E, R1, R2)(E[] subject, R1 from, R2 to) if ((isForwardRange!R1 && isForwardRange!R2 && (hasLength!R2 || isSomeString!R2)) ||
is(Unqual!E : Unqual!R1))Replace occurrences of `from` with `to` in `subject` in a new array.fn
E[] replace(E, R1, R2)(E[] subject, R1 from, R2 to, ref size_t changed) if ((isForwardRange!R1 && isForwardRange!R2 && (hasLength!R2 || isSomeString!R2)) ||
is(Unqual!E : Unqual!R1))Replace occurrences of `from` with `to` in `subject` in a new array. `changed` counts how many replacements took place.fn
void replaceInto(E, Sink, R1, R2)(Sink sink, E[] subject, R1 from, R2 to) if (isOutputRange!(Sink, E) &&
((isForwardRange!R1 && isForwardRange!R2 && (hasLength!R2 || isSomeString!R2)) ||
is(Unqual!E : Unqual!R1)))Replace occurrences of `from` with `to` in `subject` and output the result into `sink`.fn
void replaceInto(E, Sink, R1, R2)(Sink sink, E[] subject, R1 from, R2 to, ref size_t changed) if (isOutputRange!(Sink, E) &&
((isForwardRange!R1 && isForwardRange!R2 && (hasLength!R2 || isSomeString!R2)) ||
is(Unqual!E : Unqual!R1)))Replace occurrences of `from` with `to` in `subject` and output the result into `sink`. `changed` counts how many replacements took place.fn
T[] replace(T, Range)(T[] subject, size_t from, size_t to, Range stuff) if (isInputRange!Range &&
(is(ElementType!Range : T) ||
isSomeString!(T[]) && is(ElementType!Range : dchar)))Replaces elements from `array` with indices ranging from `from` (inclusive) to `to` (exclusive) with the range `stuff`.fn
void replaceInPlace(T, Range)(ref T[] array, size_t from, size_t to, Range stuff) if (is(typeof(replace(array, from, to, stuff))))Replaces elements from `array` with indices ranging from `from` (inclusive) to `to` (exclusive) with the range `stuff`. Expands or shrinks the array as needed.fn
E[] replaceFirst(E, R1, R2)(E[] subject, R1 from, R2 to) if (isDynamicArray!(E[]) &&
isForwardRange!R1 && is(typeof(appender!(E[])().put(from[0 .. 1]))) &&
isForwardRange!R2 && is(typeof(appender!(E[])().put(to[0 .. 1]))))Replaces the first occurrence of `from` with `to` in `subject`.fn
E[] replaceLast(E, R1, R2)(E[] subject, R1 from , R2 to) if (isDynamicArray!(E[]) &&
isForwardRange!R1 && is(typeof(appender!(E[])().put(from[0 .. 1]))) &&
isForwardRange!R2 && is(typeof(appender!(E[])().put(to[0 .. 1]))))Replaces the last occurrence of `from` with `to` in `subject`.fn
inout(T)[] replaceSlice(T)(inout(T)[] s, in T[] slice, in T[] replacement)Creates a new array such that the items in `slice` are replaced with the items in `replacement`. `slice` and `replacement` do not need to be the same length. The result will grow or shrink based on...fn
Appender!A appender(A)() if (isDynamicArray!A)Convenience function that returns an Appender instance, optionally initialized with `array`.fn
RefAppender!(E[]) appender(P : E[] *, E)(P arrayPtr)Convenience function that returns a RefAppender instance initialized with `arrayPtr`. Don't use null for the array pointer, use the other version of `appender` instead.fn
T[n] staticArray(T, size_t n)(auto ref T[n] a)Constructs a static array from a dynamic array whose length is known at compile-time. The element type can be inferred or specified explicitly:fn
auto staticArray(size_t n, T)(scope T a) if (isInputRange!T)Constructs a static array from a range. When `a.length` is not known at compile time, the number of elements must be given as a template argument (e.g. `myrange.staticArray!2`). Size and type can b...fn
auto staticArray(Un : U[n], U, size_t n, T)(scope T a) if (isInputRange!T && is(ElementType!T : U))dittofn
auto staticArray(Un : U[n], U, size_t n, T)(scope T a, out size_t rangeLength) if (isInputRange!T && is(ElementType!T : U))dittoVariables 1
private enumvar
hasCheapIteration = isArray!RTemplates 5
tmplblockAttribute(T)
tmplnDimensions(T)
tmplisInputRangeWithLengthOrConvertible(E)
tmplisCharOrStringOrDcharRange(T)
tmplisInputRangeOrConvertible(E)