summaryrefslogtreecommitdiff
path: root/lib/std/htab.myr
blob: b728124d8f092a8d0a9ef0c03ad0bd3d1a995172 (plain)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use "alloc"
use "die"
use "extremum"
use "option"
use "types"

pkg std =
	type htab(@k, @v) = struct
		hash	: (k : @k	-> uint64)
		eq	: (a : @k, b : @k -> bool)

		nelt	: size
		ndead	: size
		keys	: @k[:]
		vals	: @v[:]
		hashes	: uint64[:]
		dead	: bool[:]
	;;

	generic mkht	: (h : (k : @k -> uint64), eq : (a : @k, b : @k -> bool) -> htab(@k, @v)#)
	generic htinit	: (ht : htab(@k, @v)#, h : (k : @k -> uint64), eq : (a : @k, b : @k -> bool) -> void)
	generic htfree	: (ht : htab(@k, @v)# -> void)
	generic htput	: (ht : htab(@k, @v)#, k : @k, v : @v -> void)
	generic htdel	: (ht : htab(@k, @v)#, k : @k -> void)
	generic htget	: (ht : htab(@k, @v)#, k : @k -> option(@v))
	generic htgetv	: (ht : htab(@k, @v)#, k : @k, fallback : @v-> @v)
	generic hthas	: (ht : htab(@k, @v)#, k : @k -> bool)
	generic htkeys	: (ht : htab(@k, @v)# -> @k[:])
	generic htcount	: (ht : htab(@k, @v)# -> std.size)

	generic byhtkeyvals	: (ht : htab(@k, @v)# -> htkviter(@k, @v))
	impl iterable htkviter(@k, @v) -> (@k, @v)
	type htkviter(@k, @v) = struct
		ht	: std.htab(@k, @v)#
		idx	: size
	;;
;;

const Initsz = 32

generic hash = {ht, k
	var h

	h = ht.hash(k)
	if h == 0
		-> 1
	else
		-> h
	;;
}

generic resize = {ht, sz
	var oldk
	var oldv
	var oldh
	var oldd

	oldk = ht.keys
	oldv = ht.vals
	oldh = ht.hashes
	oldd = ht.dead
	ht.keys = slalloc(sz)
	ht.vals = slalloc(sz)
	ht.hashes = slzalloc(sz)
	ht.dead = slzalloc(sz)

	ht.nelt = 0
	ht.ndead = 0
	for var i = 0; i < oldk.len; i++
		if oldh[i] != 0 && !oldd[i]
			htput(ht, oldk[i], oldv[i])
		;;
	;;
	slfree(oldk)
	slfree(oldv)
	slfree(oldh)
	slfree(oldd)
}

generic idx = {ht, k
	var i, di
	var h

	di = 0
	h = hash(ht, k)
	i = h & (ht.keys.len - 1)
	while true
		while ht.hashes[i] != 0 && !ht.dead[i] && ht.hashes[i] != h
			di++
			i = (h + di) & (ht.keys.len - 1)
		;;
		if ht.hashes[i] == 0
			-> `None
		;;
		if ht.hashes[i] == h && !ht.dead[i] && ht.eq(ht.keys[i], k)
			break
		;;
		di++
		i = (h + di) & (ht.keys.len - 1)
	;;
	-> `Some i
}

generic mkht = {h, eq
	var ht

	ht = alloc()
	htinit(ht, h, eq)

	-> ht
}

generic htinit = {ht, h, eq

	ht.hash = h
	ht.eq = eq

	ht.nelt = 0
	ht.ndead = 0
	ht.keys = slalloc(Initsz)
	ht.vals = slalloc(Initsz)
	ht.hashes = slzalloc(Initsz)
	ht.dead = slzalloc(Initsz)
}

generic htfree = {ht
	slfree(ht.keys)
	slfree(ht.vals)
	slfree(ht.hashes)
	slfree(ht.dead)
	free(ht)
}

generic htput = {ht, k, v
	var i, di
	var h
	var neltincr

	di = 0
	h = hash(ht, k)
	i = h & (ht.keys.len - 1)
	neltincr = 1
	while ht.hashes[i] != 0 && !ht.dead[i]
		if ht.hashes[i] == h &&	ht.eq(ht.keys[i], k)
			neltincr = 0
			break
		;;
		di++
		i = (h + di) & (ht.keys.len - 1)
	;;
	ht.nelt += neltincr
	ht.hashes[i] = h
	ht.keys[i] = k
	ht.vals[i] = v
	ht.dead[i] = false
	if ht.keys.len < ht.nelt * 2
		resize(ht, 2*ht.keys.len)
	;;
}

generic htdel = {ht, k
	match idx(ht, k)
	| `Some i:
		ht.dead[i] = true
		ht.nelt--
		ht.ndead++
		/* remove tombstones if we shrink enough */
		if ht.keys.len < ht.ndead * 4
			resize(ht, ht.keys.len)
		;;
	| _:	
		/* do nothing */
	;;
}

generic htget = {ht, k
	match idx(ht, k)
	| `Some i:	-> `Some ht.vals[i]
	| `None:	-> `None
	;;
}

generic htgetv = {ht, k, v
	match idx(ht, k)
	| `Some i:	-> ht.vals[i]
	| `None:	-> v
	;;
}

generic hthas = {ht, k
	match idx(ht, k)
	| `Some i:	-> true
	| `None:	-> false
	;;
}

generic htkeys = {ht
	var keys
	var j

	keys = slalloc(ht.nelt)
	j = 0
	for var i = 0; i < ht.keys.len; i++
		if ht.hashes[i] != 0 && !ht.dead[i]
			keys[j++] = ht.keys[i]
		;;
	;;
	-> keys
}

generic htcount = {ht
	-> ht.nelt
}

generic byhtkeyvals = {ht
	-> [.ht = ht, .idx = 0]
}

impl iterable htkviter(@k, @v) -> (@k, @v) =
	__iternext__ = {itp, valp
		var i, ht

		ht = itp.ht
		for i = itp.idx; i < ht.keys.len; i++
			if ht.hashes[i] != 0 && !ht.dead[i]
				itp.idx = i + 1
				valp# = (ht.keys[i], ht.vals[i])
				-> true
			;;
		;;
		itp.idx = i
		-> false
	}

	__iterfin__ = {itp, valp -> void
	}
;;