Struct Array

A variable-length array on type T with an index of type I that runs from first to last inclusive. The bounds are supplied at run-time.

struct Array(T, I) ;

A fixed-length array on type T with an index that runs from first to last inclusive. The bounds are supplied at compile-time.

Constructors

NameDescription
this Construct an Array from first to last inclusive.
this Construct an Array on an interval i.

Fields

NameTypeDescription
_payload T[]
m_first I
m_last I

Properties

NameTypeDescription
first[get] I
first[set] ISets new first index.
last[get] I
last[set] IResize to new last index.

Methods

NameDescription
fromFile
fromFile
opApply
opApply
opIndex Indexing operators yield or modify the value at a specified index.
opIndexAssign
resize Resize to new first and last boundaries.
toFile
toFile
toFile

Example

A variable-length array on type T with an index of type I that runs from first to last inclusive. The bounds are supplied at run-time.

A fixed-length array on type T with an index that runs from first to last inclusive. The bounds are supplied at compile-time.

auto arr = Array!int(-10, 10);
assert(arr.length == 21);
import std.stdio;
// writeln("arr.length * int.sizeof = ", arr.length * int.sizeof);
// assert(arr.sizeof == arr.length * int.sizeof);  // 94 != 32 (in 64 bit) 94 != 16 (in 32 bit). FIXME

foreach (ref e; arr)
    e = 42;
assert(arr[-10] == 42);
assert(arr[0]   == 42);
assert(arr[10]  == 42);

import std.conv : to;
foreach (i, ref e; arr)
    e = i.to!int;   // i is of type size_t.
assert(arr[-10] == -10);
assert(arr[0]   ==   0);
assert(arr[5]   ==   5);
assert(arr[10]  ==  10);

arr[5] = 15;
assert(arr[5]   == 15);

Example

A variable-length array on type T with an index of type I that runs from first to last inclusive. The bounds are supplied at run-time.

A fixed-length array on type T with an index that runs from first to last inclusive. The bounds are supplied at compile-time.

  // schema array toFile/fromFile
// type s(low,high:integer) = array[low..high] of integer;
struct s
{
    @disable this();
    this(int low, int high)
    {
        this.low = low;
        this.high = high;
        _payload = Array!int(low, high);
    }
    immutable int low, high;
  private:
    Array!int _payload;
    alias _payload this;
}

s t1 = s(-5, 5);
for (int n = t1.low; n <= t1.high; n++)
    t1[n] = n * 3;
import std.stdio;
File tmp = File.tmpfile();
t1.toFile(tmp);
tmp.flush;

assert(tmp.size == 11 * int.sizeof);

tmp.rewind;
auto buf = tmp.rawRead(new int[cast(uint)(tmp.size)]);
foreach (i, b; buf)
    assert(b == (i - 5) * 3);

tmp.rewind;
s t2 = s(-5, 5);
t2.fromFile(tmp);
assert(t2 == t1);

Example

A variable-length array on type T with an index of type I that runs from first to last inclusive. The bounds are supplied at run-time.

A fixed-length array on type T with an index that runs from first to last inclusive. The bounds are supplied at compile-time.

auto arr = Array!char(interval(5, 25));
assert(arr.length == 21);
arr[5] = 'a';
assert(arr[5] == 'a');
arr[25] = 'b';
assert(arr[25] == 'b');

auto arr2 = Array!(int, char)(interval('a', 'g'));
assert(arr2.length == 7);
arr2['a'] = 2;
assert(arr2['a'] == 2);
arr2['b'] = 4;
assert(arr2['b'] == 4);

// See also http://forum.dlang.org/post/akibggljgcmmacsbahmm@forum.dlang.org
import epcompat.enumeration;
enum E {One, Two, Three, Four}
mixin withEnum!E;
auto arr3 = Array!(int, E)(One, Four);
assert(arr3.length == 4);
arr3[Two] = 2;
assert(arr3[Two] == 2);
arr3[Four] = 4;
assert(arr3[Four] == 4);