-
Notifications
You must be signed in to change notification settings - Fork 0
/
staticaa.d
156 lines (127 loc) · 3.3 KB
/
staticaa.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import std.stdio;
import std.traits;
int getAALen(T)(T aa)
if (isAssociativeArray!T) {
uint len = aa.keys.length;
uint[] hashes, checks;
hashes.length = len;
foreach(i, k; aa.keys)
hashes[i] = k.hashOf;
uint repeat = 1;
while(repeat--) {
checks.length = len;
checks[] = 0;
foreach(h; hashes) {
if (checks[h % len]) {
repeat = 1;
len += 1;
break;
} else
checks[h % len] = h;
}
}
return len;
}
struct StaticAA(K, V, int mapLen, int Len) {
int[mapLen] off_map;
uint[Len] keyHashes;
K[Len] keys;
V[Len] values;
alias length = Len;
enum INVALID = -1;
this(V[K] start) {
int i;
off_map[] = INVALID;
assert(Len == start.length);
foreach(k, v; start) {
uint hash = k.hashOf;
int off = hash % mapLen;
assert(off_map[off] == INVALID); //this should ensure there's no overlaps
keyHashes[i] = hash;
keys[i] = k;
values[i] = v;
off_map[off] = i;
i++;
}
}
ref V opIndex(const K ind) {
uint hash = ind.hashOf;
uint off = hash % mapLen;
assert(off_map[off] != INVALID);
assert(keyHashes[off_map[off]] == hash);
return values[off_map[off]];
}
void opIndex(const K ind, V val) {
uint hash = ind.hashOf;
uint off = hash % mapLen;
assert(off_map[off] != INVALID);
assert(keyHashes[off_map[off]] == hash);
values[off_map[off]] = val;
}
int opApply(int delegate(ref V) dg) {
int ret;
foreach(i, ref kh; keyHashes) {
if (kh) {
ret = dg(values[i]);
if (ret)
break;
}
}
return ret;
}
int opApply(int delegate(ref const K, ref V) dg) {
int ret;
foreach(i, ref kh; keyHashes) {
if (kh) {
ret = dg(keys[i], values[i]);
if (ret)
break;
}
}
return ret;
}
}
unittest {
import std.stdio;
enum AA = ["one":1, "two":2, "three":3, "four":4, "five":5, "six":6, "seven":7, "eight":8, "nine":9, "zero":0];
auto SAA = StaticAA!(string, int, getAALen(AA), AA.length)(AA);
// writeln(getAALen(AA));
//verify static and AA are identical with keys and values.
foreach(k, v; AA) {
assert(SAA[k] == v);
}
/*
foreach(k; SAA.keys)
if (k)
writeln(k);
foreach(v; SAA.values)
if (v)
writeln(v);
foreach(k, v; AA)
writeln(k, "\t", v);
foreach(v; AA)
writeln(v);
*/
/*
foreach(k, v; SAA)
writeln(k, "\t", v);
foreach(v; SAA)
writeln(v);
*/
/*
writeln(AA);
writeln(SAA);
writeln(SAA.keys);
writeln(SAA.values);
*/
//make sure these compile
foreach(k, v; SAA){}
foreach(ref k, v; SAA){}
foreach(k, ref v; SAA){}
foreach(ref k, ref v; SAA){}
foreach(v; SAA){}
foreach(ref v; SAA){}
assert(SAA["one"] == 1);
SAA["one"] = 100;
assert(SAA["one"] == 100);
}