-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhashmap.nt
74 lines (57 loc) · 1.58 KB
/
hashmap.nt
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
module hashmap;
macro import std.macro.assert;
import std.algorithm : sort;
void main() {
mut int[string] map;
map["foo"] = 5;
map["bar"] = 6;
map["foo"] = 7;
assert(map["foo"] == 7);
assert(map["bar"] == 6);
assert(map.has("bar"));
assert(!map.has("baz"));
{
mut int calls;
int deflt() { calls += 1; return 7; }
assert(map.get("bar", deflt) == 6);
assert(calls == 0);
assert(map.get("baz", deflt) == 7);
assert(calls == 1);
}
assert(map.keys == ["foo", "bar"] || map.keys == ["bar", "foo"]);
assert(map.values.sort((a, b) => a < b) == [6, 7]);
map.remove("foo");
assert(map.values == [6]);
map.remove("bar");
assert(map.values.empty);
mut int[int] map2;
map2[0] = 1;
map2[1] = 1;
assert(map2.has(0));
map2.clear;
assert(map2.keys.empty);
assert(map2.values.empty);
mut int[S] map3;
map3[S(3)] = 0;
assert(map3.keys == [S(3)] && map3.values == [0]);
mut int[int[]] map4;
map4[[2]] = 0;
assert(map4.keys == [[2]] && map4.values == [0]);
mut int[(:a | :b)] map5;
map5[:a] = 0;
assert(map5.keys == [:a] && map5.values == [0]);
mut int[(int a, int b)] map6;
map6[(1, 2)] = 0;
assert(map6.keys == [(1, 2)] && map6.values == [0]);
mut int[vec2f] map7;
map7[vec2f(1, 2)] = 0;
assert(map7.keys == [vec2f(1, 2)] && map7.values == [0]);
mut int[Enum] map8;
map8[Enum.A] = 0;
assert(map8.keys == [Enum.A] && map8.values == [0]);
}
enum Enum { A }
alias vec2f = Vector(float, 2);
struct S {
int a;
}