lu.container

Containers.

Members

Structs

Buffer
struct Buffer(T, Flag!"dynamic" dynamic = No.dynamic, size_t originalSize = 128)

Simple buffer/queue for storing and fetching items of any type T. Does not use manual memory allocation.

CircularBuffer
struct CircularBuffer(T, Flag!"dynamic" dynamic = No.dynamic, size_t originalSize = 16)

Simple circular-ish buffer for storing items of type T that discards elements when the maximum size is reached. Does not use manual memory allocation.

MutexedAA
struct MutexedAA(AA : V[K], V, K)

An associative array and a Mutex. Wraps associative array operations in mutex locks.

RehashingAA
struct RehashingAA(AA : V[K], V, K)

A wrapper around a native associative array that you can controllably set to automatically rehash as entries are added.

Examples

1 {
2     Buffer!string buffer;
3 
4     buffer.put("abc");
5     buffer.put("def");
6     assert(!buffer.empty);
7     assert(buffer.front == "abc");
8     buffer.popFront();
9     assert(buffer.front == "def");
10     buffer.popFront();
11     assert(buffer.empty);
12 }
13 {
14     Buffer!(char, Yes.dynamic, 3) buffer;
15 
16     assert(!buffer.buf.length);
17     buffer ~= 'a';
18     assert(buffer.buf.length == 3);
19     buffer ~= 'b';
20     buffer ~= 'c';
21     assert(buffer.length == 3);
22     buffer ~= 'd';
23     assert(buffer.buf.length > 3);
24     assert(buffer[0..5] == "abcd");
25     buffer.clear();
26     assert(buffer.empty);
27 }
28 {
29     RehashingAA!(int[string]) aa;
30     aa.minimumNeededForRehash = 2;
31 
32     aa["abc"] = 123;
33     aa["def"] = 456;
34     assert((aa.newKeysSinceLastRehash == 2), aa.newKeysSinceLastRehash.to!string);
35     assert((aa.numRehashes == 0), aa.numRehashes.to!string);
36     aa["ghi"] = 789;
37     assert((aa.numRehashes == 1), aa.numRehashes.to!string);
38     assert((aa.newKeysSinceLastRehash == 0), aa.newKeysSinceLastRehash.to!string);
39     aa.rehash();
40     assert((aa.numRehashes == 2), aa.numRehashes.to!string);
41 
42     auto realAA = cast(int[string])aa;
43     assert("abc" in realAA);
44     assert("def" in realAA);
45 
46     auto alsoRealAA = aa.aaOf;
47     assert("ghi" in realAA);
48     assert("jkl" !in realAA);
49 
50     auto aa2 = aa.dup;
51     aa2["jkl"] = 123;
52     assert("jkl" in aa2);
53     assert("jkl" !in aa);
54 }
55 {
56     MutexedAA!(string[int]) aa;
57     aa.setup();  // important!
58 
59     aa[1] = "one";
60     aa[2] = "two";
61     aa[3] = "three";
62 
63     auto hasOne = aa.has(1);
64     assert(hasOne);
65     assert(aa[1] == "one");
66 
67     assert(aa[2] == "two");
68 
69     auto three = aa.get(3);
70     assert(three == "three");
71 
72     auto four = aa.get(4, "four");
73     assert(four == "four");
74 
75     auto five = aa.require(5, "five");
76     assert(five == "five");
77     assert(aa[5] == "five");
78 
79     auto keys = aa.keys;
80     assert(keys.canFind(1));
81     assert(keys.canFind(5));
82     assert(!keys.canFind(6));
83 
84     auto values = aa.values;
85     assert(values.canFind("one"));
86     assert(values.canFind("four"));
87     assert(!values.canFind("six"));
88 
89     aa.rehash();
90 }

Meta