Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
Page wiki
View or edit the community-maintained wiki page associated with this page.
std.array
Functions and types that manipulate built-in arrays. License:Boost License 1.0. Authors:
Andrei Alexandrescu and Jonathan M Davis Source:
std/array.d
- ForeachType!(Range)[] array(Range)(Range r);
- Returns a newly-allocated dynamic array consisting of a copy of the
input range, static array, dynamic array, or class or struct with an
opApply function r. Note that narrow strings are handled as
a special case in an overload.
Example:
auto a = array([1, 2, 3, 4, 5][]); assert(a == [ 1, 2, 3, 4, 5 ]);
Standard inputCommand line argumentsApplication output - ElementType!(String)[] array(String)(String str);
- Convert a narrow string to an array type that fully supports random access. This is handled as a special case and always returns a dchar[], const(dchar)[], or immutable(dchar)[] depending on the constness of the input.
- auto assocArray(Range)(Range r);
- Returns a newly allocated associative array out of elements of the input range,
which must be a range of tuples (Key, Value).
Example:
auto a = assocArray(zip([0, 1, 2], ["a", "b", "c"])); assert(a == [0:"a", 1:"b", 2:"c"]); auto b = assocArray([ tuple("foo", "bar"), tuple("baz", "quux") ]); assert(b == ["foo":"bar", "baz":"quux"]);
Standard inputCommand line argumentsApplication output - auto uninitializedArray(T, I...)(I sizes);
- Returns 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 may be a multidimensional
array. In this case sizes may be specified for any number of dimensions from 1
to the number in T.
Examples:
double[] arr = uninitializedArray!(double[])(100); assert(arr.length == 100); double[][] matrix = uninitializedArray!(double[][])(42, 31); assert(matrix.length == 42); assert(matrix[0].length == 31);
Standard inputCommand line argumentsApplication output - @trusted auto minimallyInitializedArray(T, I...)(I sizes);
- Returns a new array of type T allocated on the garbage collected heap. Initialization is guaranteed only for pointers, references and slices, for preservation of memory safety.
- pure nothrow @safe bool empty(T)(in T[] a);
- Implements the range interface primitive empty for built-in
arrays. Due to the fact that nonmember functions can be called with
the first argument using the dot notation, array.empty is
equivalent to empty(array).
Example:
auto a = [ 1, 2, 3 ]; assert(!a.empty); assert(a[3 .. $].empty);
Standard inputCommand line argumentsApplication output - pure nothrow @safe T[] save(T)(T[] a);
- Implements the range interface primitive save for built-in
arrays. Due to the fact that nonmember functions can be called with
the first argument using the dot notation, array.save is
equivalent to save(array). The function does not duplicate the
content of the array, it simply returns its argument.
Example:
auto a = [ 1, 2, 3 ]; auto b = a.save; assert(b is a);
Standard inputCommand line argumentsApplication output - void popFront(A)(ref A a);
- Implements the range interface primitive popFront for built-in
arrays. Due to the fact that nonmember functions can be called with
the first argument using the dot notation, array.popFront is
equivalent to popFront(array). For narrow strings,
popFront automaticaly advances to the next code
point.
Example:
int[] a = [ 1, 2, 3 ]; a.popFront(); assert(a == [ 2, 3 ]);
Standard inputCommand line argumentsApplication output - void popBack(A)(ref A a);
- Implements the range interface primitive popBack for built-in
arrays. Due to the fact that nonmember functions can be called with
the first argument using the dot notation, array.popBack is
equivalent to popBack(array). For narrow strings, popFront automaticaly eliminates the last code point.
Example:
int[] a = [ 1, 2, 3 ]; a.popBack(); assert(a == [ 1, 2 ]);
Standard inputCommand line argumentsApplication output - T front(T)(T[] a);
- Implements the range interface primitive front for built-in
arrays. Due to the fact that nonmember functions can be called with
the first argument using the dot notation, array.front is
equivalent to front(array). For narrow strings, front automaticaly returns the first code point as a dchar.
Example:
int[] a = [ 1, 2, 3 ]; assert(a.front == 1);
Standard inputCommand line argumentsApplication output - T back(T)(T[] a);
- Implements the range interface primitive back for built-in
arrays. Due to the fact that nonmember functions can be called with
the first argument using the dot notation, array.back is
equivalent to back(array). For narrow strings, back automaticaly returns the last code point as a dchar.
Example:
int[] a = [ 1, 2, 3 ]; assert(a.back == 3);
Standard inputCommand line argumentsApplication output - void insertInPlace(T, U...)(ref T[] array, size_t pos, U stuff);
void insertInPlace(T, U...)(ref T[] array, size_t pos, U stuff); - Inserts stuff (which must be an input range or any number of
implicitly convertible items) in array at position pos.
Example:
int[] a = [ 1, 2, 3, 4 ]; a.insertInPlace(2, [ 1, 2 ]); assert(a == [ 1, 2, 1, 2, 3, 4 ]); a.insertInPlace(3, 10u, 11); assert(a == [ 1, 2, 1, 10, 11, 2, 3, 4]);
Standard inputCommand line argumentsApplication output - bool sameHead(T)(T[] lhs, T[] rhs);
- Returns whether the fronts 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.
- bool sameTail(T)(T[] lhs, T[] rhs);
- Returns whether the backs 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 $.
- ElementEncodingType!(S)[] replicate(S)(S s, size_t n);
- Returns an array that consists of s (which must be an input range) repeated n times. This function allocates, fills, and returns a new array. For a lazy version, refer to std.range.repeat.
- S[] split(S)(S s);
- Split the string s into an array of words, using whitespace as delimiter. Runs of whitespace are merged together (no empty words are produced).
- auto splitter(C)(C[] s);
- Splits a string by whitespace.
Example:
auto a = " a bcd ef gh "; assert(equal(splitter(a), ["", "a", "bcd", "ef", "gh"][]));
Standard inputCommand line argumentsApplication output - Unqual!(S1)[] split(S1, S2)(S1 s, S2 delim);
- Splits s into an array, using delim as the delimiter.
- ElementEncodingType!(ElementType!(RoR))[] join(RoR, R)(RoR ror, R sep);
ElementEncodingType!(ElementType!(RoR))[] join(RoR)(RoR ror); - Concatenates all of the ranges in ror together into one array using
sep as the separator if present.
Examples:
assert(join(["hello", "silly", "world"], " ") == "hello silly world"); assert(join(["hello", "silly", "world"]) == "hellosillyworld"); assert(join([[1, 2, 3], [4, 5]], [72, 73]) == [1, 2, 3, 72, 73, 4, 5]); assert(join([[1, 2, 3], [4, 5]]) == [1, 2, 3, 4, 5]);
Standard inputCommand line argumentsApplication output - E[] replace(E, R1, R2)(E[] subject, R1 from, R2 to);
- Replace occurrences of from with to in subject. Returns a new array without changing the contents of subject, or the original array if no match is found.
- void replaceInto(E, Sink, R1, R2)(Sink sink, E[] subject, R1 from, R2 to);
- Same as above, but outputs the result via OutputRange sink. If no match is found the original array is transfered to sink as is.
- void replaceInPlace(T, Range)(ref T[] array, size_t from, size_t to, Range 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.
Example:
int[] a = [ 1, 2, 3, 4 ]; a.replaceInPlace(1, 3, [ 9, 9, 9 ]); assert(a == [ 1, 9, 9, 9, 4 ]);
Standard inputCommand line argumentsApplication output - E[] replaceFirst(E, R1, R2)(E[] subject, R1 from, R2 to);
- Replaces the first occurrence of from with to in a. Returns a new array without changing the contents of subject, or the original array if no match is found.
- inout(T)[] replaceSlice(T)(inout(T)[] s, in T[] slice, in T[] replacement);
- Returns an array that is s with slice replaced by replacement[].
- struct Appender(A : T[], T);
- Implements an output range that appends data to an array. This is
recommended over a ~= data when appending many elements because it is more
efficient.
Example:
auto app = appender!string(); string b = "abcdefg"; foreach (char c; b) app.put(c); assert(app.data == "abcdefg"); int[] a = [ 1, 2 ]; auto app2 = appender(a); app2.put(3); app2.put([ 4, 5, 6 ]); assert(app2.data == [ 1, 2, 3, 4, 5, 6 ]);
Standard inputCommand line argumentsApplication output- this(T[] arr);
- Construct 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.
- 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.
- const size_t capacity();
- Returns 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.
- inout inout(T)[] data();
- Returns the managed array.
- void put(U)(U item);
- Appends one item to the managed array.
- void put(Range)(Range items);
- Appends an entire range to the managed array.
- void clear();
- Clears the managed array. This allows the elements of the array to be reused for appending. Note that clear is disabled for immutable or const element types, due to the possibility that Appender might overwrite immutable data.
- void shrinkTo(size_t newlength);
- Shrinks the managed array to the given length. Passing in a length that's greater than the current array length throws an enforce exception.
- struct RefAppender(A : T[], T);
- An 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.
- this(T[]* arr);
- Construct a ref appender 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. RefAppender assumes that arr is a non-null value. Note, do not use builtin appending (i.e. ~=) on the original array passed in until you are done with the appender, because calls to the appender override those appends.
- const size_t capacity();
- Returns 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.
- inout inout(T)[] data();
- Returns the managed array.
- Appender!(E[]) appender(A : E[], E)(A array = null);
- Convenience function that returns an Appender!(A) object initialized with array.
- RefAppender!(E[]) appender(A : E[]*, E)(A array);
- Convenience function that returns a RefAppender!(A) object initialized with array. Don't use null for the array pointer, use the other version of appender instead.