Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Library Reference

version 2.112.0

overview

Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
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 usinga local clone.

std.container.array

This module provides anArray type with deterministic memory usage not reliant on the GC, as an alternative to the built-in arrays.
This module is a submodule ofstd.container.

Sourcestd/container/array.d

License:
Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy atboost.org/LICENSE_1_0.txt).
Authors:
Andrei Alexandrescu
Examples:
auto arr = Array!int(0, 2, 3);writeln(arr[0]);// 0writeln(arr.front);// 0writeln(arr.back);// 3// reserve spacearr.reserve(1000);writeln(arr.length);// 3assert(arr.capacity >= 1000);// insertionarr.insertBefore(arr[1..$], 1);writeln(arr.front);// 0writeln(arr.length);// 4arr.insertBack(4);writeln(arr.back);// 4writeln(arr.length);// 5// set elementsarr[1] *= 42;writeln(arr[1]);// 42
Examples:
import std.algorithm.comparison : equal;auto arr = Array!int(1, 2, 3);// concatauto b = Array!int(11, 12, 13);arr ~= b;writeln(arr.length);// 6// slicingassert(arr[1 .. 3].equal([2, 3]));// removearr.linearRemove(arr[1 .. 3]);assert(arr[0 .. 2].equal([1, 11]));
Examples:
Array!bool packs together values efficiently by allocating one bit per element
auto arr = Array!bool([true,true,false,true,false]);writeln(arr.length);// 5
structArray(T) if (!is(immutable(T) == immutable(bool)));
Array type with deterministic control of memory. The memory allocated for the array is reclaimed as soon as possible; there is no reliance on the garbage collector.Array usesmalloc,realloc andfree for managing its own memory.
This means that pointers to elements of anArray will become dangling as soon as the element is removed from theArray. On the other hand the memory allocated by anArray will be scanned by the GC and GC managed objects referenced from anArray will be kept alive.

NoteWhen usingArray with range-based functions like those instd.algorithm,Array must be sliced to get a range (for example, usearray[].map! instead ofarray.map!). The container itself is not a range.

this(U)(U[]values...)
if (isImplicitlyConvertible!(U, T));

this(Tsingle);
Constructor taking a number of items.
this(Range)(Ranger)
if (isInputRange!Range && isImplicitlyConvertible!(ElementType!Range, T) && !is(Range == T[]));
Constructor taking aninput range
boolopEquals(const Arrayrhs) const;

boolopEquals(ref const Arrayrhs) const;
Comparison for equality.
aliasRange = RangeT!Array;

aliasConstRange = RangeT!(const(Array));

aliasImmutableRange = RangeT!(immutable(Array));
Defines the array's primary range, which is a random-access range.
ConstRange is a variant withconst elements.ImmutableRange is a variant withimmutable elements.
@property Arraydup();
Duplicates the array. The elements themselves are not transitively duplicated.

ComplexityΟ(length).

@property boolempty() const;
Returns:
true if and only if the array has no elements.

ComplexityΟ(1)

@property size_tlength() const;

size_topDollar() const;
Returns:
The number of elements in the array.

ComplexityΟ(1).

@property size_tcapacity();
Returns:
The maximum number of elements the array can store without reallocating memory and invalidating iterators upon insertion.

ComplexityΟ(1)

@system inout(T)[]data() inout;
Returns:
the internal representation of the array.

ComplexityΟ(1).

voidreserve(size_telements);
Ensures sufficient capacity to accommodatee elements. Ife < capacity, this method does nothing.

Postconditioncapacity >= e

NoteIf the capacity is increased, one should assume that all iterators to the elements are invalidated.

Complexityat mostΟ(length) ife > capacity, otherwiseΟ(1).

RangeopSlice();
Returns:
A range that iterates over elements of the array in forward order.

ComplexityΟ(1)

RangeopSlice(size_ti, size_tj);
Returns:
A range that iterates over elements of the array from indexi up to (excluding) indexj.

Preconditioni <=j &&j <= length

ComplexityΟ(1)

@property ref inout(T)front() inout;
Returns:
The first element of the array.

Preconditionempty == false

ComplexityΟ(1)

@property ref inout(T)back() inout;
Returns:
The last element of the array.

Preconditionempty == false

ComplexityΟ(1)

ref inout(T)opIndex(size_ti) inout;
Returns:
The element or a reference to the element at the specified index.

Preconditioni < length

ComplexityΟ(1)

voidopSliceAssign(Tvalue);

voidopSliceAssign(Tvalue, size_ti, size_tj);

voidopSliceUnary(string op)()
if (op == "++" || op == "--");

voidopSliceUnary(string op)(size_ti, size_tj)
if (op == "++" || op == "--");

voidopSliceOpAssign(string op)(Tvalue);

voidopSliceOpAssign(string op)(Tvalue, size_ti, size_tj);
Slicing operators executing the specified operation on the entire slice.

Preconditioni <j &&j < length

ComplexityΟ(slice.length)

ArrayopBinary(string op, Stuff)(Stuffstuff)
if (op == "~");
Returns:
A new array which is a concatenation ofthis and its argument.

ComplexityΟ(length + m), wherem is the number of elements instuff.

voidopOpAssign(string op, Stuff)(auto ref Stuffstuff)
if (op == "~");
Forwards toinsertBack.
voidclear();
Removes all the elements from the array and releases allocated memory.

Postconditionempty == true && capacity == 0

ComplexityΟ(length)

@property voidlength(size_tnewLength);
Sets the number of elements in the array tonewLength. IfnewLength is greater thanlength, the new elements are added to the end of the array and initialized withT.init. IfT is astruct whose default constructor is annotated with@disable,newLength must be lower than or equal tolength.

ComplexityGuaranteedΟ(abs(length - newLength)) ifcapacity >=newLength. Ifcapacity <newLength the worst case isΟ(newLength).

Precondition__traits(compiles, { static T _; }) ||newLength <=length

Postconditionlength ==newLength

TremoveAny();

aliasstableRemoveAny = removeAny;
Removes the last element from the array and returns it. Both stable and non-stable versions behave the same and guarantee that ranges iterating over the array are never invalidated.

Preconditionempty == false

Returns:
The element removed.

ComplexityΟ(1).

Throws:
Exception if the array is empty.
size_tinsertBack(Stuff)(Stuffstuff)
if (isImplicitlyConvertible!(Stuff, T) || isInputRange!Stuff && isImplicitlyConvertible!(ElementType!Stuff, T));

aliasinsert = insertBack;
Inserts the specified elements at the back of the array.stuff can be a value convertible toT or a range of objects convertible toT.
Returns:
The number of elements inserted.

ComplexityΟ(length + m) if reallocation takes place, otherwiseΟ(m), wherem is the number of elements instuff.

voidremoveBack();

aliasstableRemoveBack = removeBack;
Removes the value from the back of the array. Both stable and non-stable versions behave the same and guarantee that ranges iterating over the array are never invalidated.

Preconditionempty == false

ComplexityΟ(1).

Throws:
Exception if the array is empty.
size_tremoveBack(size_thowMany);

aliasstableRemoveBack = removeBack;
RemoveshowMany values from the back of the array. Unlike the unparameterized versions above, these functions do not throw if they could not removehowMany elements. Instead, ifhowMany > n, all elements are removed. The returned value is the effective number of elements removed. Both stable and non-stable versions behave the same and guarantee that ranges iterating over the array are never invalidated.
Returns:
The number of elements removed.

ComplexityΟ(howMany).

size_tinsertBefore(Stuff)(Ranger, Stuffstuff)
if (isImplicitlyConvertible!(Stuff, T));

size_tinsertBefore(Stuff)(Ranger, Stuffstuff)
if (isInputRange!Stuff && isImplicitlyConvertible!(ElementType!Stuff, T));

aliasstableInsertBefore = insertBefore;

size_tinsertAfter(Stuff)(Ranger, Stuffstuff)
if (isImplicitlyConvertible!(Stuff, T) || isInputRange!Stuff && isImplicitlyConvertible!(ElementType!Stuff, T));

size_treplace(Stuff)(Ranger, Stuffstuff)
if (isInputRange!Stuff && isImplicitlyConvertible!(ElementType!Stuff, T));

size_treplace(Stuff)(Ranger, Stuffstuff)
if (isImplicitlyConvertible!(Stuff, T));
Insertsstuff before, after, or instead ranger, which must be a valid range previously extracted from this array.stuff can be a value convertible toT or a range of objects convertible toT. Both stable and non-stable version behave the same and guarantee that ranges iterating over the array are never invalidated.
Returns:
The number of values inserted.

ComplexityΟ(length + m), wherem is the length ofstuff.

Throws:
Exception ifr is not a range extracted from this array.
RangelinearRemove(Ranger);
Removes all elements belonging tor, which must be a range obtained originally from this array.
Returns:
A range spanning the remaining elements in the array that initially were right afterr.

ComplexityΟ(length)

Throws:
Exception ifr is not a valid range extracted from this array.
structArray(T) if (is(immutable(T) == immutable(bool)));
Array specialized forbool. Packs together values efficiently by allocating one bit per element.
structRange;
Defines the array's primary range.
@property Rangesave();

@property boolempty();

@property Tfront();

@property voidfront(boolvalue);

TmoveFront();

voidpopFront();

@property Tback();

@property voidback(boolvalue);

TmoveBack();

voidpopBack();

TopIndex(size_ti);

voidopIndexAssign(Tvalue, size_ti);

TmoveAt(size_ti);

@property size_tlength() const;

RangeopSlice(size_tlow, size_thigh);
Range primitives
this(U)(U[]values...)
if (isImplicitlyConvertible!(U, T));
Constructor taking a number of items.
this(Range)(Ranger)
if (isInputRange!Range && isImplicitlyConvertible!(ElementType!Range, T) && !is(Range == T[]));
Constructor taking aninput range
@property boolempty();
Property returningtrue if and only if the array has no elements.

ComplexityΟ(1)

@property Arraydup();
Returns:
A duplicate of the array.

ComplexityΟ(length).

@property size_tlength() const;
Returns the number of elements in the array.

ComplexityΟ(1).

@property size_tcapacity();
Returns:
The maximum number of elements the array can store without reallocating memory and invalidating iterators upon insertion.

ComplexityΟ(1).

voidreserve(size_te);
Ensures sufficient capacity to accommodatee elements. Ife < capacity, this method does nothing.

Postconditioncapacity >=e

NoteIf the capacity is increased, one should assume that all iterators to the elements are invalidated.

Complexityat mostΟ(length) ife > capacity, otherwiseΟ(1).

RangeopSlice();
Returns:
A range that iterates over all elements of the array in forward order.

ComplexityΟ(1)

RangeopSlice(size_ta, size_tb);
Returns:
A range that iterates the array between two specified positions.

ComplexityΟ(1)

@property boolfront();

@property voidfront(boolvalue);
Returns:
The first element of the array.

Preconditionempty == false

ComplexityΟ(1)

Throws:
Exception if the array is empty.
@property boolback();

@property voidback(boolvalue);
Returns:
The last element of the array.

Preconditionempty == false

ComplexityΟ(1)

Throws:
Exception if the array is empty.
boolopIndex(size_ti);

voidopIndexAssign(boolvalue, size_ti);

voidopIndexOpAssign(string op)(boolvalue, size_ti);

TmoveAt(size_ti);
Indexing operators yielding or modifyng the value at the specified index.

Preconditioni < length

ComplexityΟ(1)

Array!boolopBinary(string op, Stuff)(Stuffrhs)
if (op == "~");
Returns:
A new array which is a concatenation ofthis and its argument.

ComplexityΟ(length + m), wherem is the number of elements instuff.

Array!boolopOpAssign(string op, Stuff)(Stuffstuff)
if (op == "~");
Forwards toinsertBack.
voidclear();
Removes all the elements from the array and releases allocated memory.

Postconditionempty == true && capacity == 0

ComplexityΟ(length)

@property voidlength(size_tnewLength);
Sets the number of elements in the array tonewLength. IfnewLength is greater thanlength, the new elements are added to the end of the array and initialized withfalse.

ComplexityGuaranteedΟ(abs(length - newLength)) ifcapacity >=newLength. Ifcapacity <newLength the worst case isΟ(newLength).

Postconditionlength ==newLength

TremoveAny();

aliasstableRemoveAny = removeAny;
Removes the last element from the array and returns it. Both stable and non-stable versions behave the same and guarantee that ranges iterating over the array are never invalidated.

Preconditionempty == false

Returns:
The element removed.

ComplexityΟ(1).

Throws:
Exception if the array is empty.
size_tinsertBack(Stuff)(Stuffstuff)
if (is(Stuff : bool));

size_tinsertBack(Stuff)(Stuffstuff)
if (isInputRange!Stuff && is(ElementType!Stuff : bool));

aliasstableInsertBack = insertBack;

aliasinsert = insertBack;

aliasstableInsert = insertBack;

aliaslinearInsert = insertBack;

aliasstableLinearInsert = insertBack;
Inserts the specified elements at the back of the array.stuff can be a value convertible tobool or a range of objects convertible tobool.
Returns:
The number of elements inserted.

ComplexityΟ(length + m) if reallocation takes place, otherwiseΟ(m), wherem is the number of elements instuff.

voidremoveBack();

aliasstableRemoveBack = removeBack;
Removes the value from the back of the array. Both stable and non-stable versions behave the same and guarantee that ranges iterating over the array are never invalidated.

Preconditionempty == false

ComplexityΟ(1).

Throws:
Exception if the array is empty.
size_tremoveBack(size_thowMany);

aliasstableRemoveBack = removeBack;
RemoveshowMany values from the back of the array. Unlike the unparameterized versions above, these functions do not throw if they could not removehowMany elements. Instead, ifhowMany > n, all elements are removed. The returned value is the effective number of elements removed. Both stable and non-stable versions behave the same and guarantee that ranges iterating over the array are never invalidated.
Returns:
The number of elements removed.

ComplexityΟ(howMany).

size_tinsertBefore(Stuff)(Ranger, Stuffstuff);

aliasstableInsertBefore = insertBefore;

size_tinsertAfter(Stuff)(Ranger, Stuffstuff)
if (isImplicitlyConvertible!(Stuff, T) || isInputRange!Stuff && isImplicitlyConvertible!(ElementType!Stuff, T));

aliasstableInsertAfter = insertAfter;

size_treplace(Stuff)(Ranger, Stuffstuff)
if (is(Stuff : bool));

aliasstableReplace = replace;
Insertsstuff before, after, or instead ranger, which must be a valid range previously extracted from this array.stuff can be a value convertible tobool or a range of objects convertible tobool. Both stable and non-stable version behave the same and guarantee that ranges iterating over the array are never invalidated.
Returns:
The number of values inserted.

ComplexityΟ(length + m), wherem is the length ofstuff.

RangelinearRemove(Ranger);
Removes all elements belonging tor, which must be a range obtained originally from this array.
Returns:
A range spanning the remaining elements in the array that initially were right afterr.

ComplexityΟ(length)

Copyright © 1999-2026 by theD Language Foundation | Page generated byDdoc on Sat Feb 21 04:08:15 2026

[8]ページ先頭

©2009-2026 Movatter.jp