std.array - D Programming Language (original) (raw)
Functions and types that manipulate built-in arrays and associative arrays.
ForeachType!Range[] array
(Range)(Range r
)
if (isIterable!Range && !isAutodecodableString!Range && !isInfinite!Range);
ForeachType!(typeof((*Range).init))[] array
(Range)(Range r
)
if (is(Range == U*, U) && isIterable!U && !isAutodecodableString!Range && !isInfinite!Range);
Allocates an array and initializes it with copies of the elements of range r
.
Narrow strings are handled as follows:
- If autodecoding is turned on (default), then they are handled as a separate overload.
- If autodecoding is turned off, then this is equivalent to duplicating the array.
Parameters:
Range r | range (or aggregate with opApply function) whose elements are copied into the allocated array |
---|
Returns:
allocated and initialized array
Examples:
auto a = array([1, 2, 3, 4, 5][]); writeln(a);
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
NOTEThis function is never used when autodecoding is turned off.
Parameters:
String str | isNarrowString to be converted to an array of dchar |
---|
Returns:
a dchar[], const(dchar)[], or immutable(dchar)[] depending on the constness of the input.
Examples:
import std.range.primitives : isRandomAccessRange; import std.traits : isAutodecodableString;
static if (isAutodecodableString!string) writeln("Hello D".array); else writeln("Hello D".array); static if (isAutodecodableString!wstring) writeln("Hello D"w.array); else writeln("Hello D"w.array); static assert(isRandomAccessRange!dstring == true);
auto assocArray
(Range)(Range r
)
if (isInputRange!Range);
auto assocArray
(Keys, Values)(Keys keys
, Values values
)
if (isInputRange!Values && isInputRange!Keys);
Returns a newly allocated associative array from a range of key/value tuples or from a range of keys and a range of values.
Returns:
A newly allocated associative array out of elements of the input range, which must be a range of tuples (Key, Value) or a range of keys and a range of values. If given two ranges of unequal lengths after the elements of the shorter are exhausted the remaining elements of the longer will not be considered. Returns a null associative array reference when given an empty range.
DuplicatesAssociative arrays have unique keys. If r contains duplicate keys, then the result will contain the value of the last pair for that key in r.
Examples:
import std.range : repeat, zip; import std.typecons : tuple; import std.range.primitives : autodecodeStrings; auto a = assocArray(zip([0, 1, 2], ["a", "b", "c"])); static assert(is(typeof(a) == string[int])); writeln(a); auto b = assocArray([ tuple("foo", "bar"), tuple("baz", "quux") ]); static assert(is(typeof(b) == string[string])); writeln(b); static if (autodecodeStrings) alias achar = dchar; else alias achar = immutable(char); auto c = assocArray("ABCD", true.repeat); static assert(is(typeof(c) == bool[achar])); bool[achar] expected = ['D':true, 'A':true, 'B':true, 'C':true]; writeln(c);
auto byPair
(AA)(AA aa
)
if (isAssociativeArray!AA);
Construct a range iterating over an associative array by key/value tuples.
Parameters:
AA aa | The associative array to iterate over. |
---|
Returns:
A forward rangeof Tuple's of key and value pairs from the given associative array. The members of each pair can be accessed by name (.key and .value). or by integer index (0 and 1 respectively).
Examples:
import std.algorithm.sorting : sort; import std.typecons : tuple, Tuple;
auto aa = ["a": 1, "b": 2, "c": 3]; Tuple!(string, int)[] pairs;
foreach (pair; aa.byPair) { if (pair.key == "b") pairs ~= tuple("B", pair.value); else pairs ~= pair; }
pairs.sort(); assert(pairs == [ tuple("B", 2), tuple("a", 1), tuple("c", 3) ]);
nothrow @system auto uninitializedArray
(T, I...)(I sizes
)
if (isDynamicArray!T && allSatisfy!(isIntegral, I) && hasIndirections!(ElementEncodingType!T));
nothrow @trusted auto uninitializedArray
(T, I...)(I sizes
)
if (isDynamicArray!T && allSatisfy!(isIntegral, I) && !hasIndirections!(ElementEncodingType!T));
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 0 to the number in T.
uninitializedArray is nothrow and weakly pure.
uninitializedArray is @system if the uninitialized element type has pointers.
Parameters:
T | The type of the resulting array elements |
---|---|
I sizes | The length dimension(s) of the resulting array |
Returns:
An array of T with I.length dimensions.
Examples:
double[] arr = uninitializedArray!(double[])(100); writeln(arr.length); double[][] matrix = uninitializedArray!(double[][])(42, 31); writeln(matrix.length); writeln(matrix[0].length); char*[] ptrs = uninitializedArray!(char*[])(100); writeln(ptrs.length);
nothrow @trusted auto minimallyInitializedArray
(T, I...)(I sizes
)
if (isDynamicArray!T && allSatisfy!(isIntegral, I));
Returns a new array of type T allocated on the garbage collected heap.
Partial initialization is done for types with indirections, for preservation of memory safety. Note that elements will only be initialized to 0, but not necessarily the element type's .init.
minimallyInitializedArray is nothrow and weakly pure.
Parameters:
T | The type of the array elements |
---|---|
I sizes | The length dimension(s) of the resulting array |
Returns:
An array of T with I.length dimensions.
Examples:
import std.algorithm.comparison : equal; import std.range : repeat;
auto arr = minimallyInitializedArray!(int[])(42); writeln(arr.length); auto arr2 = new int[42]; assert(arr2.equal(0.repeat(42)));
@trusted CommonType!(T[], U[]) overlap
(T, U)(T[] a
, U[] b
)
if (is(typeof(a
.ptr < b
.ptr) == bool));
Returns 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 overlapping slice, returns that slice. Otherwise, returns the null slice.
Parameters:
T[] a | The first array to compare |
---|---|
U[] b | The second array to compare |
Returns:
The overlapping portion of the two arrays.
Examples:
int[] a = [ 10, 11, 12, 13, 14 ]; int[] b = a[1 .. 3]; writeln(overlap(a, b)); b = b.dup; assert(overlap(a, b).empty);
static test()() @nogc { auto a = "It's three o'clock"d; auto b = a[5 .. 10]; return b.overlap(a); }
static assert(test == "three"d);
Examples:
import std.meta : AliasSeq;
bool isSliceOf(T)(const scope T[] part, const scope T[] whole) { return part.overlap(whole) is part; }
auto x = [1, 2, 3, 4, 5];
assert(isSliceOf(x[3..$], x)); assert(isSliceOf(x[], x)); assert(!isSliceOf(x, x[3..$])); assert(!isSliceOf([7, 8], x)); assert(isSliceOf(null, x));
assert(isSliceOf(null, null));
foreach (T; AliasSeq!(int[], const(int)[], immutable(int)[], const int[], immutable int[])) { T a = [1, 2, 3, 4, 5]; T b = a; T c = a[1 .. $]; T d = a[0 .. 1]; T e = null;
assert(isSliceOf(a, a));
assert(isSliceOf(b, a));
assert(isSliceOf(a, b));
assert(isSliceOf(c, a));
assert(isSliceOf(c, b));
assert(!isSliceOf(a, c));
assert(!isSliceOf(b, c));
assert(isSliceOf(d, a));
assert(isSliceOf(d, b));
assert(!isSliceOf(a, d));
assert(!isSliceOf(b, d));
assert(isSliceOf(e, a));
assert(isSliceOf(e, b));
assert(isSliceOf(e, c));
assert(isSliceOf(e, d));
assert(!isSliceOf(a[$ .. $], a));
assert(isSliceOf(a[0 .. 0], a));
assert(isSliceOf(a, a[0.. $]));
assert(isSliceOf(a[0 .. $], a));
}
void insertInPlace
(T, U...)(ref T[] array
, size_t pos
, U stuff
)
if (!isSomeString!(T[]) && allSatisfy!(isInputRangeOrConvertible!T, U) && (U.length > 0));
void insertInPlace
(T, U...)(ref T[] array
, size_t pos
, U stuff
)
if (isSomeString!(T[]) && allSatisfy!(isCharOrStringOrDcharRange, U));
Inserts stuff
(which must be an input range or any number of implicitly convertible items) in array
at position pos
.
Parameters:
T[] array | The array that stuff will be inserted into. |
---|---|
size_t pos | The position in array to insert the stuff. |
U stuff | An input range, or any number of implicitly convertible items to insert into array. |
Examples:
int[] a = [ 1, 2, 3, 4 ]; a.insertInPlace(2, [ 1, 2 ]); writeln(a); a.insertInPlace(3, 10u, 11); writeln(a); union U { float a = 3.0; int b; }
U u1 = { b : 3 }; U u2 = { b : 4 }; U u3 = { b : 5 }; U[] unionArr = [u2, u3]; unionArr.insertInPlace(2, [u1]); writeln(unionArr); unionArr.insertInPlace(0, [u3, u2]); writeln(unionArr); static class C { int a; float b;
this(int a, float b) { this.a = a; this.b = b; }
}
C c1 = new C(42, 1.0); C c2 = new C(0, 0.0); C c3 = new C(int.max, float.init);
C[] classArr = [c1, c2, c3]; insertInPlace(classArr, 3, [c2, c3]); C[5] classArr1 = classArr; writeln(classArr1); insertInPlace(classArr, 0, c3, c1); C[7] classArr2 = classArr; writeln(classArr2);
pure nothrow @nogc @safe bool sameHead
(T)(in T[] lhs
, in 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.
Parameters:
T[] lhs | the first array to compare |
---|---|
T[] rhs | the second array to compare |
Returns:
true if lhs.ptr == rhs.ptr, false otherwise.
Examples:
auto a = [1, 2, 3, 4, 5]; auto b = a[0 .. 2];
assert(a.sameHead(b));
pure nothrow @nogc @trusted bool sameTail
(T)(in T[] lhs
, in 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 $.
Parameters:
T[] lhs | the first array to compare |
---|---|
T[] rhs | the second array to compare |
Returns:
true if both arrays are the same length and lhs.ptr == rhs.ptr,false otherwise.
Examples:
auto a = [1, 2, 3, 4, 5]; auto b = a[3..$];
assert(a.sameTail(b));
ElementEncodingType!S[] replicate
(S)(S s
, size_t n
)
if (isDynamicArray!S);
ElementType!S[] replicate
(S)(S s
, size_t n
)
if (isInputRange!S && !isDynamicArray!S);
Parameters:
S s | an input range or a dynamic array |
---|---|
size_t n | number of times to repeat s |
Returns:
An array that consists of s
repeated n
times. This function allocates, fills, and returns a new array.
Examples:
auto a = "abc"; auto s = replicate(a, 3);
writeln(s); auto b = [1, 2, 3]; auto c = replicate(b, 3);
writeln(c); auto d = replicate(b, 0);
writeln(d);
pure @safe S[] split
(S)(S s
)
if (isSomeString!S);
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));
auto split
(alias isTerminator, Range)(Range range
)
if (isForwardRange!Range && is(typeof(unaryFun!isTerminator(range
.front))));
Eagerly splits range
into an array, using sep
as the delimiter.
When no delimiter is provided, strings are split into an array of words, using whitespace as delimiter. Runs of whitespace are merged together (no empty words are produced).
The range
must be a forward range. The separator can be a value of the same type as the elements in range
or it can be another forward range
.
Parameters:
S s | the string to split by word if no separator is given |
---|---|
Range range | the range to split |
Separator sep | a value of the same type as the elements of range or another |
isTerminator | a predicate that splits the range when it returns true. |
Returns:
An array containing the divided parts of range
(or the words of s
).
Examples:
import std.uni : isWhite; writeln("Learning,D,is,fun".split(",")); writeln("Learning D is fun".split!isWhite); writeln("Learning D is fun".split(" D "));
Examples:
string str = "Hello World!"; writeln(str.split); string str2 = "Hello\t\tWorld\t!"; writeln(str2.split);
Examples:
writeln(split("hello world")); writeln(split("192.168.0.1", ".")); auto a = split([1, 2, 3, 4, 5, 1, 2, 3, 4, 5], [2, 3]); writeln(a);
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)));
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));
ElementEncodingType!(ElementType!RoR)[] join
(RoR)(RoR ror
)
if (isInputRange!RoR && isInputRange!(Unqual!(ElementType!RoR)));
Eagerly concatenates all of the ranges in ror
together (with the GC) into one array using sep
as the separator if present.
Parameters:
RoR ror | An input range of input ranges |
---|---|
R sep | An input range, or a single element, to join the ranges on |
Returns:
An array of elements
Examples:
writeln(join(["hello", "silly", "world"], " ")); writeln(join(["hello", "silly", "world"])); writeln(join([[1, 2, 3], [4, 5]], [72, 73])); writeln(join([[1, 2, 3], [4, 5]])); const string[] arr = ["apple", "banana"]; writeln(arr.join(",")); writeln(arr.join());
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.
Parameters:
E[] subject | the array to scan |
---|---|
R1 from | the item to replace |
R2 to | the item to replace all instances of from with |
Returns:
A new array without changing the contents of subject
, or the original array if no match is found.
Examples:
writeln("Hello Wörld".replace("o Wö", "o Wo")); writeln("Hello Wörld".replace("l", "h"));
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.
Parameters:
E[] subject | the array to scan |
---|---|
R1 from | the item to replace |
R2 to | the item to replace all instances of from with |
size_t changed | the number of replacements |
Returns:
A new array without changing the contents of subject
, or the original array if no match is found.
Examples:
size_t changed = 0; writeln("Hello Wörld".replace("o Wö", "o Wo", changed)); writeln(changed); changed = 0; writeln("Hello Wörld".replace("l", "h", changed)); import std.stdio : writeln; writeln(changed); writeln(changed);
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 intosink
.
Parameters:
Sink sink | an output range |
---|---|
E[] subject | the array to scan |
R1 from | the item to replace |
R2 to | the item to replace all instances of from with |
Examples:
auto arr = [1, 2, 3, 4, 5]; auto from = [2, 3]; auto to = [4, 6]; auto sink = appender!(int[])();
replaceInto(sink, arr, from, to);
writeln(sink.data);
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 intosink
. changed
counts how many replacements took place.
Parameters:
Sink sink | an output range |
---|---|
E[] subject | the array to scan |
R1 from | the item to replace |
R2 to | the item to replace all instances of from with |
size_t changed | the number of replacements |
Examples:
auto arr = [1, 2, 3, 4, 5]; auto from = [2, 3]; auto to = [4, 6]; auto sink = appender!(int[])();
size_t changed = 0; replaceInto(sink, arr, from, to, changed);
writeln(sink.data); writeln(changed);
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
.
Parameters:
T[] subject | the array to scan |
---|---|
size_t from | the starting index |
size_t to | the ending index |
Range stuff | the items to replace in-between from and to |
Returns:
A new array without changing the contents of subject
.
Examples:
auto a = [ 1, 2, 3, 4 ]; auto b = a.replace(1, 3, [ 9, 9, 9 ]); writeln(a); writeln(b);
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.
Parameters:
T[] array | the array to scan |
---|---|
size_t from | the starting index |
size_t to | the ending index |
Range stuff | the items to replace in-between from and to |
Examples:
int[] a = [1, 4, 5]; replaceInPlace(a, 1u, 2u, [2, 3, 4]); writeln(a); replaceInPlace(a, 1u, 2u, cast(int[])[]); writeln(a); replaceInPlace(a, 1u, 3u, a[2 .. 4]); writeln(a);
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
.
Parameters:
E[] subject | the array to scan |
---|---|
R1 from | the item to replace |
R2 to | the item to replace from with |
Returns:
A new array without changing the contents of subject
, or the original array if no match is found.
Examples:
auto a = [1, 2, 2, 3, 4, 5]; auto b = a.replaceFirst([2], [1337]); writeln(b); auto s = "This is a foo foo list"; auto r = s.replaceFirst("foo", "silly"); writeln(r);
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
.
Parameters:
E[] subject | the array to scan |
---|---|
R1 from | the item to replace |
R2 to | the item to replace from with |
Returns:
A new array without changing the contents of subject
, or the original array if no match is found.
Examples:
auto a = [1, 2, 2, 3, 4, 5]; auto b = a.replaceLast([2], [1337]); writeln(b); auto s = "This is a foo foo list"; auto r = s.replaceLast("foo", "silly"); writeln(r);
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 the items given.
Parameters:
inout(T)[] s | the base of the new array |
---|---|
T[] slice | the slice of s to be replaced |
T[] replacement | the items to replace slice with |
Returns:
A new array that is s
with slice
replaced byreplacement
[].
Examples:
auto a = [1, 2, 3, 4, 5]; auto b = replaceSlice(a, a[1 .. 4], [0, 0, 0]);
writeln(b);
struct Appender
(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 performance hit of looking up slice capacityfor each append.
Parameters:
A | the array type to simulate. |
---|
Examples:
auto app = appender!string(); string b = "abcdefg"; foreach (char c; b) app.put(c); writeln(app[]); int[] a = [ 1, 2 ]; auto app2 = appender(a); app2.put(3); app2.put([ 4, 5, 6 ]); writeln(app2[]);
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.
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.
Parameters:
size_t newCapacity | the capacity the Appender should have |
---|
@property size_t capacity
() const;
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, 0 will be returned.
@property size_t length
() const;
Returns:
The number of elements appended.
@property inout(T)[] data
() inout;
Use opSlice() from now on.
Returns:
The managed array.
@property @safe inout(T)[] opSlice
() inout;
Returns:
The managed array.
void put
(U)(U item
)
if (InPlaceAppender!A.canPutItem!U);
Appends item
to the managed array. Performs encoding forchar types if A is a differently typed char array.
Parameters:
U item | the single item to append |
---|
void put
(Range)(Range items
)
if (InPlaceAppender!A.canPutRange!Range);
Appends an entire range to the managed array. Performs encoding forchar elements if A is a differently typed char array.
Parameters:
Range items | the range of items to append |
---|
template opOpAssign
(string op : "~")
Appends to the managed array.
pure nothrow @safe void clear
();
Clears the managed array. This allows the elements of the array to be reused for appending.
Noteclear is disabled for immutable or const element types, due to the possibility that Appender might overwrite immutable data.
pure @safe void shrinkTo
(size_t newlength
);
Shrinks the managed array to the given length.
Throws:
Exception if newlength is greater than the current array length.
NoteshrinkTo is disabled for immutable or const element types.
string toString
()() const;
void toString
(Writer)(ref scope Writer w
, ref scope const FormatSpec!char fmt
) const
if (isOutputRange!(Writer, char));
Gives a string in the form of Appender!(A)(data).
Returns:
A string if writer is not set; void otherwise.
struct RefAppender
(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.
TipUse the arrayPtr overload of appender for construction with type-inference.
Parameters:
A | The array type to simulate |
---|
Examples:
int[] a = [1, 2]; auto app2 = appender(&a); writeln(app2[]); writeln(a); app2 ~= 3; writeln(app2.length); app2 ~= [4, 5, 6]; writeln(app2[]); writeln(a); app2.reserve(5); assert(app2.capacity >= 5);
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.
NoteDo not use built-in appending (i.e. ~=) on the original array until you are done with the appender, because subsequent calls to the appender will reallocate the array data without those appends.
Parameters:
A* arr | Pointer to an array. Must not be null. |
---|
void opDispatch
(string fn, Args...)(Args args
)
if (__traits(compiles, (Appender!A a) => mixin("a." ~ fn ~ "(args
)")));
Wraps remaining Appender methods such as put.
Parameters:
fn | Method name to call. |
---|---|
Args 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.
@property size_t capacity
() const;
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.
@property size_t length
() const;
Returns:
The number of elements appended.
@property inout(ElementEncodingType!A)[] opSlice
() inout;
Returns:
the managed array.
InPlaceAppender!A inPlaceAppender
(A)()
if (isDynamicArray!A);
InPlaceAppender!(E[]) inPlaceAppender
(A : E[], E)(auto ref A array
);
Convenience function that returns a InPlaceAppender instance, optionally initialized with array
.
Appender!A appender
(A)()
if (isDynamicArray!A);
Appender!(E[]) appender
(A : E[], E)(auto ref A array
);
Convenience function that returns an Appender instance, optionally initialized with array
.
Examples:
auto w = appender!string; w.reserve(10); assert(w.capacity >= 10);
w.put('a'); w.put("bc"); w ~= 'd'; w ~= "ef";
writeln(w[]);
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.
Examples:
int[] a = [1, 2]; auto app2 = appender(&a); writeln(app2[]); writeln(a); app2 ~= 3; app2 ~= [4, 5, 6]; writeln(app2[]); writeln(a); app2.reserve(5); assert(app2.capacity >= 5);
T[n] staticArray
(T, size_t n)(auto ref T[n] a
);
U[n] staticArray
(U, T, size_t n)(auto ref T[n] a
)
if (!is(T == U) && is(T : U));
Constructs a static array from a dynamic array whose length is known at compile-time. The element type can be inferred or specified explicitly:
- [1, 2].staticArray returns int[2]
- [1, 2].staticArray!float returns float[2]
Note staticArray
returns by value, so expressions involving large arrays may be inefficient.
Returns:
A static array constructed from a
.
Examples:
static array from array literal
auto a = [0, 1].staticArray; static assert(is(typeof(a) == int[2])); writeln(a);
Examples:
static array from array with implicit casting of elements
auto b = [0, 1].staticArray!long; static assert(is(typeof(b) == long[2])); writeln(b);
auto staticArray
(size_t n, T)(scope T a
)
if (isInputRange!T);
auto staticArray
(size_t n, T)(scope T a
, out size_t rangeLength
)
if (isInputRange!T);
auto staticArray
(Un : U[n], U, size_t n, T)(scope T a
)
if (isInputRange!T && is(ElementType!T : U));
auto staticArray
(Un : U[n], U, size_t n, T)(scope T a
, out size_t rangeLength
)
if (isInputRange!T && is(ElementType!T : U));
auto staticArray
(alias a)()
if (isInputRange!(typeof(a)));
auto staticArray
(U, alias a)()
if (isInputRange!(typeof(a)));
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 be combined, if the source range elements are implicitly convertible to the requested element type (eg: 2.iota.staticArray
!(long[2])).
When the range a
is known at compile time, it can be given as a template argument to avoid having to specify the number of elements (e.g.: staticArray
!(2.iota) or staticArray
!(double, 2.iota)).
Parameters:
T a | The input range. If there are less elements than the specified length of the static array, the rest of it is default-initialized. If there are more than specified, the first elements up to the specified length are used. |
---|---|
size_t rangeLength | Output for the number of elements used from a. Optional. |
Examples:
static array from range + size
import std.range : iota;
auto input = 3.iota; auto a = input.staticArray!2; static assert(is(typeof(a) == int[2])); writeln(a); auto b = input.staticArray!(long[4]); static assert(is(typeof(b) == long[4])); writeln(b);
Examples:
static array from CT range
import std.range : iota;
enum a = staticArray!(2.iota); static assert(is(typeof(a) == int[2])); writeln(a); enum b = staticArray!(long, 2.iota); static assert(is(typeof(b) == long[2])); writeln(b);
Copyright © 1999-2025 by the D Language Foundation | Page generated byDdoc on Mon May 5 08:33:23 2025