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
180
181
182
183
184
185
|
# Currently just a hash map from cstring to an arbitrary pointer
# but looking to expand this in the future with generics
struct MapNode {
~void
key,
value,
bool active
}
/; method MapNode
/; deleted [bool]
return self.active == false && self.key !== null
;/
;/
struct Map {
uint
count,
size,
~MapNode _under
}
uint MAP_MIN_SIZE = 8
uint MAP_MAX_GROW = 4096
/; method Map
/; _alloc
MapNode tmp
tmp.key = null
tmp.value = null
tmp.active = false
self._under = _alloc(self.size * len tmp)
/; loop (int i = 0; i < self.size) [i++]
self._under{i} = tmp
;/
;/
/; init
self.count = 0
self.size = MAP_MIN_SIZE
self._alloc()
;/
/; _over [bool]
uint usage = (self.count * 10) / self.size
return usage > 7
;/
/; _grow
~MapNode old_list = self._under
uint old_size = self.size
self.count = 0
/; if (self.size < MAP_MAX_GROW / 2)
self.size = old_size * 2
;; else
self.size = old_size + MAP_MAX_GROW
;/
self._alloc()
/; loop (int i = 0; i < old_size) [i++]
MapNode to_copy = old_list{i}
/; if (to_copy.active)
uint idx = self._index(to_copy.key)
self._under{idx} = to_copy
;; else if (to_copy.key !== null)
_delete(to_copy.key)
;/
;/
_delete(old_list)
;/
/; _hash (~uint8 key, uint64 k0) [uint64]
crypto.hash.SipHashKey sk
sk.k0 = k0
sk.k1 = k0
uint in_len = text.cstr.length(key)
uint64 out = 0
crypto.hash.siphash(key, in_len, ~sk, ~out, 8)
return out % self.size
;/
/; _index (~uint8 key) [int]
# Default hash
uint h = self._hash(key, 0)
int first_free = -1
/; loop (int i = 0; i < self.size) [i++]
uint try = h + (i + i * i) / 2
try = try % self.size
MapNode node = self._under{try}
/; if (node.key == null)
/; if (first_free < 0)
return try
;/
return first_free
;; else if (node.deleted() == true && first_free < 0)
first_free = try
;; else if (node.key !== null)
/; if (text.cstr.eq(node.key, key))
return try
;/
;/
;/
return first_free
;/
/; set (~uint8 key, ~void value)
/; if (self._over())
self._grow()
;/
uint idx = self._index(key)
~MapNode n = ~self._under{idx}
/; if (n`.active == false)
self.count++
;; else
n`.value = value
return
;/
/; if (n`.key !== null)
_delete(n`.key)
;/
n`.key = text.cstr.copy(key)
n`.value = value
n`.active = true
;/
/; get (~uint8 key) [~void]
uint idx = self._index(key)
MapNode n = self._under{idx}
/; if (n.key == null || n.active == false)
return null
;/
return n.value
;/
/; del (~uint8 key)
uint idx = self._index(key)
MapNode n = self._under{idx}
/; if (n.active == true)
return
;/
n.active = false
self._under{idx} = n
self.count--
;/
/; end
/; loop (int i = 0; i < self.size) [i++]
MapNode n = self._under{i}
/; if (n.key !== null)
_delete(n.key)
;/
;/
_delete(self._under)
self._under = null
self.size = 0
self.count = 0
;/
;/
|