Array

Array of dynamic size.

If you add elements, new memory will be allocated automatically as needed. Typically there is more memory allocated than is currently in use. There is a tradeoff between wasted space and frequency of reallocations. The default behaviour is to double the capacity every time the allocated memory is filled up. This ensures that pushBack takes O(1) in amortized time. If you know the number of elements in advance, you can use reserve to avoid reallocations, but this is just an optimization and never necessary.

Constructors

this
this(size_t size)

constructor for given length

this
this(size_t size, V val)

constructor for given length and init

this
this(Stuff data)

constructor that gets content from arbitrary range

Destructor

~this
~this()

destructor

Postblit

this(this)
this(this)

post-blit that does a full copy

Members

Aliases

opCatAssign
alias opCatAssign = pushBack

convenience alias for pushBack

Functions

assign
void assign(size_t newsize, V v)

sets the size and fills everything with one value

back
inout(V) back()

last element, same as this[$-1]

capacity
size_t capacity()

number of elements this structure can hold without further allocations

clear
void clear()

remove all content but keep allocated memory (same as resize(0))

empty
bool empty()

check for emptiness

front
inout(V) front()

first element, same as this[0]

insert
void insert(size_t i, V data)

insert new element at given location. moves all elements behind

length
size_t length()

number of elements

opCmp
int opCmp(Array other)
Undocumented in source. Be warned that the author may not have intended to support it.
opDollar
size_t opDollar()

number of elements

opEquals
bool opEquals(Array other)
Undocumented in source. Be warned that the author may not have intended to support it.
opIndex
inout(V) opIndex(size_t i)

indexing

opSlice
inout(V)[] opSlice()

default range

opSlice
inout(V)[] opSlice(size_t a, size_t b)

subrange

opSliceAssign
void opSliceAssign(V v)

assign all elements to the same value

opSliceAssign
void opSliceAssign(V v, size_t a, size_t b)
Undocumented in source. Be warned that the author may not have intended to support it.
popBack
V popBack()

returns removed element

ptr
inout(V)* ptr()

pointer to the first element

pushBack
void pushBack(V val)

add some new element to the back

pushBack
void pushBack(Stuff data)

add multiple new elements to the back

remove
V remove(size_t i)

remove i'th element. moves all elements behind

reserve
void reserve(size_t newCap, bool overEstimate)

make sure this structure can contain given number of elements without further allocs

resize
void resize(size_t size, V v)
void resize(size_t size)

sets the size to some value. Either cuts of some values (but does not free memory), or fills new ones with V.init

toHash
hash_t toHash()
Undocumented in source. Be warned that the author may not have intended to support it.
toString
void toString(void delegate(const(char)[]) sink, FormatSpec!char fmt)
string toString()

convert to string

Examples

Array!int a;

a.pushBack(1);
a.pushBack([2,3,4,5]);
assert(a.popBack() == 5);
assert(equal(a[], [1,2,3,4]));

a[] = 0;
a[1..3] = 1;
a.resize(6, 2);
assert(equal(a[], [0,1,1,0,2,2]));

Meta