Debian Codegraph @

scheme/ikarus.hash-tables.ss

raw
1 ;;; Ikarus Scheme -- A compiler for R6RS Scheme.
2 ;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License version 3 as
6 ;;; published by the Free Software Foundation.
7 ;;;
8 ;;; This program is distributed in the hope that it will be useful, but
9 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;;; General Public License for more details.
12 ;;;
13 ;;; You should have received a copy of the GNU General Public License
14 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16
17 (library (ikarus hash-tables)
18 (export make-eq-hashtable make-eqv-hashtable make-hashtable
19 hashtable-ref hashtable-set! hashtable?
20 hashtable-size hashtable-delete! hashtable-contains?
21 hashtable-update! hashtable-keys hashtable-mutable?
22 hashtable-clear! hashtable-entries hashtable-copy
23 hashtable-equivalence-function hashtable-hash-function
24 string-hash string-ci-hash symbol-hash)
25 (import
26 (ikarus system $pairs)
27 (ikarus system $vectors)
28 (ikarus system $tcbuckets)
29 (ikarus system $fx)
30 (except (ikarus)
31 make-eq-hashtable make-eqv-hashtable make-hashtable
32 hashtable-ref hashtable-set! hashtable?
33 hashtable-size hashtable-delete! hashtable-contains?
34 hashtable-update! hashtable-keys hashtable-mutable?
35 hashtable-clear! hashtable-entries hashtable-copy
36 hashtable-equivalence-function hashtable-hash-function
37 string-hash string-ci-hash symbol-hash))
38
39 (define-struct hasht (vec count tc mutable? hashf equivf hashf0))
40
41 ;;; directly from Dybvig's paper
42 (define tc-pop
43 (lambda (tc)
44 (let ([x ($car tc)])
45 (if (eq? x ($cdr tc))
46 #f
47 (let ([v ($car x)])
48 ($set-car! tc ($cdr x))
49 ($set-car! x #f)
50 ($set-cdr! x #f)
51 v)))))
52
53 ;;; assq-like lookup
54 (define direct-lookup
55 (lambda (x b)
56 (if (fixnum? b)
57 #f
58 (if (eq? x ($tcbucket-key b))
59 b
60 (direct-lookup x ($tcbucket-next b))))))
61
62 (define rehash-lookup
63 (lambda (h tc x)
64 (cond
65 [(tc-pop tc) =>
66 (lambda (b)
67 (if (eq? ($tcbucket-next b) #f)
68 (rehash-lookup h tc x)
69 (begin
70 (re-add! h b)
71 (if (eq? x ($tcbucket-key b))
72 b
73 (rehash-lookup h tc x)))))]
74 [else #f])))
75
76 (define get-bucket-index
77 (lambda (b)
78 (let ([next ($tcbucket-next b)])
79 (if (fixnum? next)
80 next
81 (get-bucket-index next)))))
82
83 (define replace!
84 (lambda (lb x y)
85 (let ([n ($tcbucket-next lb)])
86 (cond
87 [(eq? n x)
88 ($set-tcbucket-next! lb y)
89 (void)]
90 [else
91 (replace! n x y)]))))
92
93 (define re-add!
94 (lambda (h b)
95 (let ([vec (hasht-vec h)]
96 [next ($tcbucket-next b)])
97 ;;; first remove it from its old place
98 (let ([idx
99 (if (fixnum? next)
100 next
101 (get-bucket-index next))])
102 (let ([fst ($vector-ref vec idx)])
103 (cond
104 [(eq? fst b)
105 ($vector-set! vec idx next)]
106 [else
107 (replace! fst b next)])))
108 ;;; reset the tcbucket-tconc FIRST
109 ($set-tcbucket-tconc! b (hasht-tc h))
110 ;;; then add it to the new place
111 (let ([k ($tcbucket-key b)])
112 (let ([ih (pointer-value k)])
113 (let ([idx ($fxlogand ih ($fx- ($vector-length vec) 1))])
114 (let ([n ($vector-ref vec idx)])
115 ($set-tcbucket-next! b n)
116 ($vector-set! vec idx b)
117 (void))))))))
118
119 (define (get-bucket h x)
120 (define (get-hashed h x ih)
121 (let ([equiv? (hasht-equivf h)]
122 [vec (hasht-vec h)])
123 (let ([idx (bitwise-and ih ($fx- ($vector-length vec) 1))])
124 (let f ([b ($vector-ref vec idx)])
125 (cond
126 [(fixnum? b) #f]
127 [(equiv? x ($tcbucket-key b)) b]
128 [else (f ($tcbucket-next b))])))))
129 (cond
130 [(hasht-hashf h) =>
131 (lambda (hashf)
132 (get-hashed h x (hashf x)))]
133 [(and (eq? eqv? (hasht-equivf h)) (number? x))
134 (get-hashed h x (number-hash x))]
135 [else
136 (let ([pv (pointer-value x)]
137 [vec (hasht-vec h)])
138 (let ([ih pv])
139 (let ([idx ($fxlogand ih ($fx- ($vector-length vec) 1))])
140 (let ([b ($vector-ref vec idx)])
141 (or (direct-lookup x b)
142 (rehash-lookup h (hasht-tc h) x))))))]))
143
144 (define (get-hash h x v)
145 (cond
146 [(get-bucket h x) =>
147 (lambda (b) ($tcbucket-val b))]
148 [else v]))
149
150 (define (in-hash? h x)
151 (and (get-bucket h x) #t))
152
153 (define (del-hash h x)
154 (define unlink!
155 (lambda (h b)
156 (let ([vec (hasht-vec h)]
157 [next ($tcbucket-next b)])
158 ;;; first remove it from its old place
159 (let ([idx
160 (if (fixnum? next)
161 next
162 (get-bucket-index next))])
163 (let ([fst ($vector-ref vec idx)])
164 (cond
165 [(eq? fst b)
166 ($vector-set! vec idx next)]
167 [else
168 (replace! fst b next)])))
169 ;;; set next to be #f, denoting, not in table
170 ($set-tcbucket-next! b #f))))
171 (cond
172 [(get-bucket h x) =>
173 (lambda (b)
174 (unlink! h b)
175 ;;; don't forget the count.
176 (set-hasht-count! h (- (hasht-count h) 1)))]))
177
178 (define put-hash!
179 (lambda (h x v)
180 (define (put-hashed h x v ih)
181 (let ([equiv? (hasht-equivf h)]
182 [vec (hasht-vec h)])
183 (let ([idx (bitwise-and ih ($fx- ($vector-length vec) 1))])
184 (let f ([b ($vector-ref vec idx)])
185 (cond
186 [(fixnum? b)
187 ($vector-set! vec idx
188 (vector x v ($vector-ref vec idx)))
189 (let ([ct (hasht-count h)])
190 (set-hasht-count! h ($fxadd1 ct))
191 (when ($fx> ct ($vector-length vec))
192 (enlarge-table h)))]
193 [(equiv? x ($tcbucket-key b))
194 ($set-tcbucket-val! b v)]
195 [else (f ($tcbucket-next b))])))))
196 (cond
197 [(hasht-hashf h) =>
198 (lambda (hashf)
199 (put-hashed h x v (hashf x)))]
200 [(and (eq? eqv? (hasht-equivf h)) (number? x))
201 (put-hashed h x v (number-hash x))]
202 [else
203 (let ([pv (pointer-value x)]
204 [vec (hasht-vec h)])
205 (let ([ih pv])
206 (let ([idx ($fxlogand ih ($fx- ($vector-length vec) 1))])
207 (let ([b ($vector-ref vec idx)])
208 (cond
209 [(or (direct-lookup x b) (rehash-lookup h (hasht-tc h) x))
210 =>
211 (lambda (b)
212 ($set-tcbucket-val! b v)
213 (void))]
214 [else
215 (let ([bucket
216 ($make-tcbucket (hasht-tc h) x v
217 ($vector-ref vec idx))])
218 (if ($fx= (pointer-value x) pv)
219 ($vector-set! vec idx bucket)
220 (let* ([ih (pointer-value x)]
221 [idx
222 ($fxlogand ih ($fx- ($vector-length vec) 1))])
223 ($set-tcbucket-next! bucket ($vector-ref vec idx))
224 ($vector-set! vec idx bucket))))
225 (let ([ct (hasht-count h)])
226 (set-hasht-count! h ($fxadd1 ct))
227 (when ($fx> ct ($vector-length vec))
228 (enlarge-table h)))])))))])))
229
230 (define (update-hash! h x proc default)
231 (cond
232 [(get-bucket h x) =>
233 (lambda (b) ($set-tcbucket-val! b (proc ($tcbucket-val b))))]
234 [else (put-hash! h x (proc default))]))
235
236 (define enlarge-table
237 (lambda (h)
238 (define (enlarge-hashtable h hashf)
239 (define insert-b
240 (lambda (b vec mask)
241 (let* ([x ($tcbucket-key b)]
242 [ih (hashf x)]
243 [idx (bitwise-and ih mask)]
244 [next ($tcbucket-next b)])
245 ($set-tcbucket-next! b ($vector-ref vec idx))
246 ($vector-set! vec idx b)
247 (unless (fixnum? next)
248 (insert-b next vec mask)))))
249 (define move-all
250 (lambda (vec1 i n vec2 mask)
251 (unless ($fx= i n)
252 (let ([b ($vector-ref vec1 i)])
253 (unless (fixnum? b)
254 (insert-b b vec2 mask))
255 (move-all vec1 ($fxadd1 i) n vec2 mask)))))
256 (let* ([vec1 (hasht-vec h)]
257 [n1 ($vector-length vec1)]
258 [n2 ($fxsll n1 1)]
259 [vec2 (make-base-vec n2)])
260 (move-all vec1 0 n1 vec2 ($fx- n2 1))
261 (set-hasht-vec! h vec2)))
262 (cond
263 [(hasht-hashf h) =>
264 (lambda (hashf)
265 (enlarge-hashtable h hashf))]
266 [(eq? eq? (hasht-equivf h))
267 (enlarge-hashtable h
268 (lambda (x) (pointer-value x)))]
269 [else
270 (enlarge-hashtable h
271 (lambda (x)
272 (if (number? x)
273 (number-hash x)
274 (pointer-value x))))])))
275
276 (define init-vec
277 (lambda (v i n)
278 (if ($fx= i n)
279 v
280 (begin
281 ($vector-set! v i i)
282 (init-vec v ($fxadd1 i) n)))))
283
284 (define make-base-vec
285 (lambda (n)
286 (init-vec (make-vector n) 0 n)))
287
288 (define (clear-hash! h)
289 (let ([v (hasht-vec h)])
290 (init-vec v 0 (vector-length v)))
291 (unless (hasht-hashf h)
292 (set-hasht-tc! h
293 (let ([x (cons #f #f)])
294 (cons x x))))
295 (set-hasht-count! h 0))
296
297 (define (get-keys h)
298 (let ([v (hasht-vec h)] [n (hasht-count h)])
299 (let ([kv (make-vector n)])
300 (let f ([i ($fxsub1 n)] [j ($fxsub1 (vector-length v))] [kv kv] [v v])
301 (cond
302 [($fx= i -1) kv]
303 [else
304 (let ([b ($vector-ref v j)])
305 (if (fixnum? b)
306 (f i ($fxsub1 j) kv v)
307 (f (let f ([i i] [b b] [kv kv])
308 ($vector-set! kv i ($tcbucket-key b))
309 (let ([b ($tcbucket-next b)]
310 [i ($fxsub1 i)])
311 (cond
312 [(fixnum? b) i]
313 [else (f i b kv)])))
314 ($fxsub1 j) kv v)))])))))
315
316 (define (get-entries h)
317 (let ([v (hasht-vec h)] [n (hasht-count h)])
318 (let ([kv (make-vector n)] [vv (make-vector n)])
319 (let f ([i ($fxsub1 n)] [j ($fxsub1 (vector-length v))] [kv kv] [vv vv] [v v])
320 (cond
321 [($fx= i -1) (values kv vv)]
322 [else
323 (let ([b ($vector-ref v j)])
324 (if (fixnum? b)
325 (f i ($fxsub1 j) kv vv v)
326 (f (let f ([i i] [b b] [kv kv] [vv vv])
327 ($vector-set! kv i ($tcbucket-key b))
328 ($vector-set! vv i ($tcbucket-val b))
329 (let ([b ($tcbucket-next b)]
330 [i ($fxsub1 i)])
331 (cond
332 [(fixnum? b) i]
333 [else (f i b kv vv)])))
334 ($fxsub1 j) kv vv v)))])))))
335
336 (define (hasht-copy h mutable?)
337 (define (dup-hasht h mutable? n)
338 (let* ([hashf (hasht-hashf h)]
339 [tc (and (not hashf) (let ([x (cons #f #f)]) (cons x x)))])
340 (make-hasht (make-base-vec n) 0 tc mutable?
341 hashf (hasht-equivf h) (hasht-hashf0 h))))
342 (let ([v (hasht-vec h)] [n (hasht-count h)])
343 (let ([r (dup-hasht h mutable? (vector-length v))])
344 (let f ([i ($fxsub1 n)] [j ($fxsub1 (vector-length v))] [r r] [v v])
345 (cond
346 [($fx= i -1) r]
347 [else
348 (let ([b ($vector-ref v j)])
349 (if (fixnum? b)
350 (f i ($fxsub1 j) r v)
351 (f (let f ([i i] [b b] [r r])
352 (put-hash! r ($tcbucket-key b) ($tcbucket-val b))
353 (let ([b ($tcbucket-next b)] [i ($fxsub1 i)])
354 (cond
355 [(fixnum? b) i]
356 [else (f i b r)])))
357 ($fxsub1 j) r v)))])))))
358
359 ;;; public interface
360 (define (hashtable? x) (hasht? x))
361
362 (define make-eq-hashtable
363 (case-lambda
364 [()
365 (let ([x (cons #f #f)])
366 (let ([tc (cons x x)])
367 (make-hasht (make-base-vec 32) 0 tc #t #f eq? #f)))]
368 [(k)
369 (if (and (or (fixnum? k) (bignum? k)) (>= k 0))
370 (make-eq-hashtable)
371 (die 'make-eq-hashtable "invalid initial capacity" k))]))
372
373 (define make-eqv-hashtable
374 (case-lambda
375 [()
376 (let ([x (cons #f #f)])
377 (let ([tc (cons x x)])
378 (make-hasht (make-base-vec 32) 0 tc #t #f eqv? #f)))]
379 [(k)
380 (if (and (or (fixnum? k) (bignum? k)) (>= k 0))
381 (make-eqv-hashtable)
382 (die 'make-eqv-hashtable "invalid initial capacity" k))]))
383
384 (define make-hashtable
385 (case-lambda
386 [(hashf equivf) (make-hashtable hashf equivf 0)]
387 [(hashf equivf k)
388 (define who 'make-hashtable)
389 (define (wrap f)
390 (cond
391 [(or (eq? f symbol-hash)
392 (eq? f string-hash)
393 (eq? f string-ci-hash))
394 f]
395 [else
396 (lambda (k)
397 (let ([i (f k)])
398 (if (or (fixnum? i) (bignum? i))
399 i
400 (die #f "invalid return value from hash function" i))))]))
401 (unless (procedure? hashf)
402 (die who "hash function is not a procedure" hashf))
403 (unless (procedure? equivf)
404 (die who "equivalence function is not a procedure" equivf))
405 (if (and (or (fixnum? k) (bignum? k)) (>= k 0))
406 (make-hasht (make-base-vec 32) 0 #f #t (wrap hashf) equivf hashf)
407 (die who "invalid initial capacity" k))]))
408
409 (define hashtable-ref
410 (lambda (h x v)
411 (if (hasht? h)
412 (get-hash h x v)
413 (die 'hashtable-ref "not a hash table" h))))
414
415 (define hashtable-contains?
416 (lambda (h x)
417 (if (hasht? h)
418 (in-hash? h x)
419 (die 'hashtable-contains? "not a hash table" h))))
420
421 (define hashtable-set!
422 (lambda (h x v)
423 (if (hasht? h)
424 (if (hasht-mutable? h)
425 (put-hash! h x v)
426 (die 'hashtable-set! "hashtable is immutable" h))
427 (die 'hashtable-set! "not a hash table" h))))
428
429 (define hashtable-update!
430 (lambda (h x proc default)
431 (if (hasht? h)
432 (if (hasht-mutable? h)
433 (if (procedure? proc)
434 (update-hash! h x proc default)
435 (die 'hashtable-update! "not a procedure" proc))
436 (die 'hashtable-update! "hashtable is immutable" h))
437 (die 'hashtable-update! "not a hash table" h))))
438
439 (define hashtable-size
440 (lambda (h)
441 (if (hasht? h)
442 (hasht-count h)
443 (die 'hashtable-size "not a hash table" h))))
444
445 (define hashtable-delete!
446 (lambda (h x)
447 ;;; FIXME: should shrink table if number of keys drops below
448 ;;; (sqrt (vector-length (hasht-vec h)))
449 (if (hasht? h)
450 (if (hasht-mutable? h)
451 (del-hash h x)
452 (die 'hashtable-delete! "hash table is immutable" h))
453 (die 'hashtable-delete! "not a hash table" h))))
454
455 (define (hashtable-entries h)
456 (if (hasht? h)
457 (get-entries h)
458 (die 'hashtable-entries "not a hash table" h)))
459
460 (define (hashtable-keys h)
461 (if (hasht? h)
462 (get-keys h)
463 (die 'hashtable-keys "not a hash table" h)))
464
465 (define (hashtable-mutable? h)
466 (if (hasht? h)
467 (hasht-mutable? h)
468 (die 'hashtable-mutable? "not a hash table" h)))
469
470 (define (hashtable-clear! h)
471 (if (hasht? h)
472 (if (hasht-mutable? h)
473 (clear-hash! h)
474 (die 'hashtable-clear! "hash table is immutable" h))
475 (die 'hashtable-clear! "not a hash table" h)))
476
477 (define hashtable-copy
478 (case-lambda
479 [(h)
480 (if (hasht? h)
481 (if (hasht-mutable? h)
482 (hasht-copy h #f)
483 h)
484 (die 'hashtable-copy "not a hash table" h))]
485 [(h mutable?)
486 (if (hasht? h)
487 (if (or mutable? (hasht-mutable? h))
488 (hasht-copy h (and mutable? #t))
489 h)
490 (die 'hashtable-copy "not a hash table" h))]))
491
492 (define (hashtable-equivalence-function h)
493 (if (hasht? h)
494 (hasht-equivf h)
495 (die 'hashtable-equivalence-function "not a hash table" h)))
496
497 (define (hashtable-hash-function h)
498 (if (hasht? h)
499 (hasht-hashf0 h)
500 (die 'hashtable-hash-function "not a hash table" h)))
501
502 (define (string-hash s)
503 (if (string? s)
504 (foreign-call "ikrt_string_hash" s)
505 (die 'string-hash "not a string" s)))
506
507 (define (string-ci-hash s)
508 (if (string? s)
509 (foreign-call "ikrt_string_hash"
510 (string-foldcase s))
511 (die 'string-ci-hash "not a string" s)))
512
513 (define (symbol-hash s)
514 (if (symbol? s)
515 (foreign-call "ikrt_string_hash" (symbol->string s))
516 (die 'symbol-hash "not a symbol" s)))
517
518 (define (number-hash x)
519 (cond
520 [(fixnum? x) x]
521 [(flonum? x) (foreign-call "ikrt_flonum_hash" x)]
522 [(bignum? x) (foreign-call "ikrt_bignum_hash" x)]
523 [(ratnum? x)
524 (fxxor
525 (number-hash (numerator x))
526 (number-hash (denominator x)))]
527 [else
528 (fxxor
529 (number-hash (real-part x))
530 (number-hash (imag-part x)))]))
531
532 (set-rtd-printer! (type-descriptor hasht)
533 (lambda (x p wr)
534 (display "#<hashtable>" p)))
535 )