-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnewAATest.d
179 lines (146 loc) · 3.06 KB
/
newAATest.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import std.stdio;
import newAA;
alias newAA.AssociativeArray AA;
void test1() {
AA!(string,int) aasi;
AA!(int,dstring) aaid;
AA!(dstring,int) aadi;
aasi["abc"] = 100;
aasi["def"] = 200;
assert(aasi["abc"] == 100);
assert(aasi["def"] == 200);
aaid[5] = "Five. Just five! OK?!";
aaid[11] = "Eleven, without the seven";
aaid[1000] = "One thousand";
aaid[50000] = "Fifty thousand";
aaid[65536] = "Sixty-five thousand something or other";
aaid[1048576] = "One million and blah blah blah it's a megabyte!";
writeln(aaid[1000]);
writeln(aaid[50000]);
assert((1234 in aaid) is null);
writeln(aaid.keys);
writeln(aaid.values);
}
// NOTE: this should only be necessary for development; once this code gets
// into druntime .toString should work automatically.
void dump(K,V)(AA!(K,V) aa) {
write("[");
string delim = "";
foreach (key, value; aa) {
static if (is(typeof(value)==string) ||
is(typeof(value)==wstring) ||
is(typeof(value)==dstring))
{
writef("%s%s: \"%s\"", delim, key, value);
} else {
writef("%s%s: %s", delim, key, value);
}
delim = ", ";
}
writeln("]");
}
void test2() {
const int[] key1 = [ 1,2,3 ];
const int[] key2 = [ 2,3,4 ];
const int[] key3 = [ 3,4,5 ];
AA!(const int[], string) aa1;
aa1[key1] = "abc";
aa1[key2] = "def";
aa1[key3] = "ghi";
dump(aa1);
foreach (v; aa1) {
writeln(v);
}
AA!(const int[], string) aa2;
aa2[key3] = "ghi";
aa2[key2] = "def";
aa2[key1] = "abc";
assert(aa2[key1] == "abc");
aa2.rehash;
assert(aa1==aa2);
}
void test3() {
AA!(string,string) aa;
aa["Hello"] = "Привет";
aa["Goodbye"] = "Пока";
aa["Thanks"] = "Спасибо";
foreach (k; aa.byKey) {
writeln(k);
}
foreach (v; aa.byValue) {
writeln(v);
}
dump(aa);
}
struct S(K) {
K key;
template keyComparable(L) {
enum bool keyComparable = is(typeof(key==L.init) == bool);
}
template keyDupCompat(L) {
static if (__traits(compiles, L.init.dup))
enum keyDupCompat = is(typeof(L.init.dup) : K);
else
enum keyDupCompat = false;
}
template keyIdupCompat(L) {
static if (__traits(compiles, L.init.idup))
enum keyIdupCompat = is(typeof(L.init.idup) : K);
else
enum keyIdupCompat = false;
}
template keyAssignable(L) {
enum bool keyAssignable = is(L : K) || keyDupCompat!L ||
keyIdupCompat!L;
}
bool equal(L)(L l) if (keyComparable!L)
{
return (key == l);
}
void set(L)(L l) if (keyAssignable!L)
{
static if (is(L : K)) {
key = l;
} else static if (keyDupCompat!L) {
key = l.dup;
} else static if (keyIdupCompat!L) {
key = l.idup;
}
}
}
unittest {
S!long s;
ubyte b = 123;
s.set(b);
assert(s.equal(123UL));
}
unittest {
S!dstring s;
dchar[] t = "abc"d.dup;
s.set(t);
const(dchar)[] u = "abc"d;
assert(s.equal(u));
}
void test4() {
{
auto s = S!string("abc");
char[] t = "abc".dup;
assert(s.equal(t));
//int i = 123;
//assert(s.equal(i));
}
{
auto s = S!(char[])("abc".dup);
string t = "abc";
assert(s.equal(t));
//s.set(123);
s.set("def");
assert(s.equal("def"));
}
}
void main() {
//test1();
//test2();
//test3();
//test4();
}