VariantArrays

Stores set of variants.

Enables lightweight storage of polymorphic objects.

Each element is indexed by a corresponding VariantRef.

Members

Functions

allOf
inout(SomeKind)[] allOf()

Constant access to all elements of type SomeKind.

at
inout(SomeKind) at(size_t index)

Get reference to element of type SomeKind at index.

at
inout(SomeKind) at(Ref ref_)

Get reference to element of type SomeKind at ref_.

empty
bool empty()

Check if empty.

insertBack
Ref insertBack(SomeKind value)

Insert value at back.

insertBackMove
Ref insertBackMove(SomeKind value)

Move (emplace) value into back.

length
size_t length()
opOpAssign
void opOpAssign(SomeKind value)

Move (emplace) value into back.

peek
inout(SomeKind)* peek(Ref ref_)

Peek at element of type SomeKind at ref_.

reserve
void reserve(size_t newCapacity)

Reserve space for newCapacity elements of type SomeKind.

Static functions

makeRef
Ref makeRef(Ref.Size index)

Make reference to type SomeKind at offset index.

Examples

1 import nxt.minimal_fixed_array : MinimalFixedArray;
2 
3 alias Chars(uint capacity) = MinimalFixedArray!(char, capacity);
4 alias Chars7 = Chars!7;
5 alias Chars15 = Chars!15;
6 alias VA = VariantArrays!(ulong,
7                           Chars7,
8                           Chars15);
9 
10 VA data;
11 assert(data.length == 0);
12 assert(data.empty);
13 
14 const i0 = data.put(ulong(13));
15 assert(cast(size_t)i0 == 1);
16 
17 assert(i0.isA!ulong);
18 assert(data.at!ulong(0) == ulong(13));
19 assert(data.length == 1);
20 assert(!data.empty);
21 assert(data.allOf!ulong == [ulong(13)].s);
22 
23 const i1 = data.put(Chars7(`1234567`));
24 assert(cast(size_t)i1 == 2);
25 
26 // same order as in `Types`
27 assert(i0 < i1);
28 
29 assert(i1.isA!(Chars7));
30 assert(data.at!(Chars7)(0) == Chars7(`1234567`));
31 assert(data.allOf!(Chars7) == [Chars7(`1234567`)].s);
32 assert(data.length == 2);
33 
34 const i2 = data.put(Chars15(`123`));
35 assert(cast(size_t)i2 == 3);
36 
37 // same order as in `Types`
38 assert(i0 < i2);
39 assert(i1 < i2);
40 
41 assert(i2.isA!(Chars15));
42 assert(data.at!(Chars15)(0) == Chars15(`123`));
43 // TODO assert(data.allOf!(Chars15) == [Chars15(`123`)].s);
44 assert(data.length == 3);
45 
46 const i3 = data.put(Chars15(`1234`));
47 assert(cast(size_t)i3 == 7);
48 
49 // same order as in `Types`
50 assert(i0 < i3);
51 assert(i1 < i3);
52 assert(i2 < i3);            // same type, i2 added before i3
53 
54 assert(i3.isA!(Chars15));
55 assert(data.at!(Chars15)(1) == Chars15(`1234`));
56 assert(data.allOf!(Chars15) == [Chars15(`123`), Chars15(`1234`)].s);
57 assert(data.length == 4);

Meta