|
7 | 7 | (rnrs:make-eq-hashtable)))
|
8 | 8 | ((capacity weakness)
|
9 | 9 | (if weakness
|
10 |
| - (begin |
11 |
| - (unless (weak-eq-hashtables-supported) |
12 |
| - (error 'make-eq-hashtable "weak hashtables unsupported")) |
13 |
| - (if capacity |
14 |
| - (make-weak-eq-hashtable capacity) |
15 |
| - (make-weak-eq-hashtable))) |
| 10 | + (cond |
| 11 | + ((memq weakness (weak-eq-hashtables-supported)) |
| 12 | + (if capacity |
| 13 | + ((make-weak-eq-hashtable-procedure weakness) capacity) |
| 14 | + ((make-weak-eq-hashtable-procedure weakness)))) |
| 15 | + ((memq weakness (ephemeral-eq-hashtables-supported)) |
16 | 16 | (if capacity
|
17 |
| - (rnrs:make-eq-hashtable capacity) |
18 |
| - (rnrs:make-eq-hashtable)))))) |
| 17 | + ((make-ephemeral-eq-hashtable-procedure weakness) capacity) |
| 18 | + ((make-ephemeral-eq-hashtable-procedure weakness)))) |
| 19 | + (else (error 'make-eq-hashtable "weakness not supported" weakness))) |
| 20 | + (if capacity |
| 21 | + (rnrs:make-eq-hashtable capacity) |
| 22 | + (rnrs:make-eq-hashtable)))))) |
19 | 23 |
|
20 | 24 | (define make-eqv-hashtable
|
21 | 25 | (case-lambda
|
|
26 | 30 | (rnrs:make-eqv-hashtable)))
|
27 | 31 | ((capacity weakness)
|
28 | 32 | (if weakness
|
29 |
| - (begin |
30 |
| - (unless (weak-eqv-hashtables-supported) |
31 |
| - (error 'make-eqv-hashtable "weak hashtables unsupported")) |
| 33 | + (cond |
| 34 | + ((memq weakness (weak-eqv-hashtables-supported)) |
| 35 | + (if capacity |
| 36 | + ((make-weak-eqv-hashtable-procedure weakness) capacity) |
| 37 | + ((make-weak-eqv-hashtable-procedure weakness)))) |
| 38 | + ((memq weakness (ephemeral-eqv-hashtables-supported)) |
32 | 39 | (if capacity
|
33 |
| - (make-weak-eqv-hashtable capacity) |
34 |
| - (make-weak-eqv-hashtable))) |
| 40 | + ((make-ephemeral-eqv-hashtable-procedure weakness) capacity) |
| 41 | + ((make-ephemeral-eqv-hashtable-procedure weakness)))) |
| 42 | + (else (error 'make-eqv-hashtable "weakness not supported" weakness))) |
35 | 43 | (if capacity
|
36 | 44 | (rnrs:make-eqv-hashtable capacity)
|
37 | 45 | (rnrs:make-eqv-hashtable))))))
|
38 | 46 |
|
39 | 47 | (define make-hashtable
|
40 | 48 | (case-lambda
|
41 |
| - ((hash equiv) (rnrs:make-hashtable hash equiv)) |
| 49 | + ((hash equiv) |
| 50 | + (if hash |
| 51 | + (rnrs:make-hashtable (if (pair? hash) (car hash) hash) equiv) |
| 52 | + (cond |
| 53 | + ((eq? equiv eq?) (make-eq-hashtable)) |
| 54 | + ((eq? equiv eqv?) (make-eqv-hashtable)) |
| 55 | + (else (error 'make-hashtable |
| 56 | + "hash procedure cannot be #f except with eq? or eqv?" |
| 57 | + hash equiv))))) |
42 | 58 | ((hash equiv capacity)
|
43 |
| - (if capacity |
44 |
| - (rnrs:make-hashtable hash equiv capacity) |
45 |
| - (rnrs:make-hashtable hash equiv))) |
| 59 | + (if hash |
| 60 | + (if capacity |
| 61 | + (rnrs:make-hashtable (if (pair? hash) (car hash) hash) equiv |
| 62 | + capacity) |
| 63 | + (rnrs:make-hashtable (if (pair? hash) (car hash) hash) equiv)) |
| 64 | + (cond |
| 65 | + ((eq? equiv eq?) (make-eq-hashtable capacity)) |
| 66 | + ((eq? equiv eqv?) (make-eqv-hashtable capacity)) |
| 67 | + (else (error 'make-hashtable |
| 68 | + "hash procedure cannot be #f except with eq? or eqv?" |
| 69 | + hash equiv))))) |
46 | 70 | ((hash equiv capacity weakness)
|
47 |
| - (cond |
48 |
| - ((and (not hash) (eq? equiv eq?)) |
49 |
| - (make-eq-hashtable capacity weakness)) |
50 |
| - ((and (not hash) (eq? equiv eqv?)) |
51 |
| - (make-eqv-hashtable capacity weakness)) |
52 |
| - (else |
53 |
| - (let ((hash (if (pair? hash) (car hash) hash))) ;; why? |
54 |
| - (if weakness |
55 |
| - (begin |
56 |
| - (unless (weak-hashtables-supported) |
57 |
| - (error 'make-hashtable "weak hashtables unsupported")) |
| 71 | + (if hash |
| 72 | + (let ((hash (if (pair? hash) (car hash) hash))) ;; why? - read spec |
| 73 | + (if weakness |
| 74 | + (cond |
| 75 | + ((memq weakness (weak-hashtables-supported)) |
| 76 | + (if capacity |
| 77 | + ((make-weak-hashtable-procedure weakness) hash equiv |
| 78 | + capacity) |
| 79 | + ((make-weak-hashtable-procedure weakness) hash equiv))) |
| 80 | + ((memq weakness (ephemeral-hashtables-supported)) |
| 81 | + (if capacity |
| 82 | + ((make-ephemeral-hashtable-procedure weakness) hash equiv |
| 83 | + capacity) |
| 84 | + ((make-ephemeral-hashtable-procedure weakness) hash equiv))) |
| 85 | + (else (error 'make-hashtable "weakness not supported" weakness))) |
58 | 86 | (if capacity
|
59 |
| - (make-weak-hashtable hash equiv capacity) |
60 |
| - (make-weak-hashtable hash equiv))) |
61 |
| - (if capacity |
62 |
| - (rnrs:make-hashtable hash equiv capacity) |
63 |
| - (rnrs:make-hashtable hash equiv))))))))) |
| 87 | + (rnrs:make-hashtable hash equiv capacity) |
| 88 | + (rnrs:make-hashtable hash equiv)))) |
| 89 | + (cond ; hash function not provided |
| 90 | + ((eq? equiv eq?) |
| 91 | + (make-eq-hashtable capacity weakness)) |
| 92 | + ((eq? equiv eqv?) |
| 93 | + (make-eqv-hashtable capacity weakness)) |
| 94 | + (else (error 'make-hashtable |
| 95 | + "hash procedure cannot be #f except with eq? or eqv?" |
| 96 | + hash equiv))))))) |
64 | 97 |
|
65 | 98 | (define (alist->eq-hashtable . args)
|
66 | 99 | (apply alist->hashtable #f eq? args))
|
|
121 | 154 |
|
122 | 155 | (define hashtable-update!
|
123 | 156 | (case-lambda
|
124 |
| - ((hashtable key proc) (hashtable-update! hashtable key proc nil)) |
| 157 | + ((hashtable key proc) |
| 158 | + (rnrs:hashtable-update! hashtable key |
| 159 | + (lambda (value) |
| 160 | + (if (nil? value) |
| 161 | + (error "No such key in hashtable." |
| 162 | + hashtable key) |
| 163 | + (proc value))) |
| 164 | + nil)) |
125 | 165 | ((hashtable key proc default)
|
126 |
| - (rnrs:hashtable-update! |
127 |
| - hashtable key |
128 |
| - (lambda (value) |
129 |
| - (if (nil? value) |
130 |
| - (error "No such key in hashtable." hashtable key) |
131 |
| - (proc value))) |
132 |
| - default)))) |
| 166 | + (rnrs:hashtable-update! hashtable key proc default)))) |
133 | 167 |
|
134 |
| -;;; XXX This could be implemented at the platform level to eliminate the second |
135 |
| -;;; lookup for the key. |
136 | 168 | (define (hashtable-intern! hashtable key default-proc)
|
137 |
| - (let ((value (hashtable-ref hashtable key nil))) |
138 |
| - (if (nil? value) |
139 |
| - (let ((value (default-proc))) |
140 |
| - (hashtable-set! hashtable key value) |
141 |
| - value) |
142 |
| - value))) |
| 169 | + (if (hashtable-cell-support) |
| 170 | + (let ((cell (hashtable-cell hashtable key nil))) |
| 171 | + (if (nil? (hashtable-cell-value cell)) |
| 172 | + (let ((value (default-proc))) |
| 173 | + (set-hashtable-cell-value! cell value) |
| 174 | + value) |
| 175 | + (hashtable-cell-value cell))) |
| 176 | + (let ((value (rnrs:hashtable-ref hashtable key nil))) |
| 177 | + (if (nil? value) |
| 178 | + (let ((value (default-proc))) |
| 179 | + (hashtable-set! hashtable key value) |
| 180 | + value) |
| 181 | + value)))) |
143 | 182 |
|
144 | 183 | (define hashtable-copy
|
145 | 184 | (case-lambda
|
|
171 | 210 |
|
172 | 211 | #;(define hashtable-keys rnrs:hashtable-keys)
|
173 | 212 |
|
174 |
| -(define (hashtable-values hashtable) |
175 |
| - (let-values (((keys values) (hashtable-entries hashtable))) |
176 |
| - values)) |
| 213 | +;;; Defined in helpers.sls |
| 214 | + |
| 215 | +;; (define (hashtable-values hashtable) |
| 216 | +;; (let-values (((keys values) (hashtable-entries hashtable))) |
| 217 | +;; values)) |
177 | 218 |
|
178 | 219 | #;(define hashtable-entries rnrs:hashtable-entries)
|
179 | 220 |
|
|
215 | 256 | keys values)))
|
216 | 257 |
|
217 | 258 | (define (hashtable-merge! hashtable-dest hashtable-source)
|
218 |
| - (hashtable-walk hashtable-source |
219 |
| - (lambda (key value) |
220 |
| - (hashtable-set! hashtable-dest key value))) |
| 259 | + (let-values (((keys values) (hashtable-entries hashtable-source))) |
| 260 | + (vector-for-each (lambda (key value) |
| 261 | + (hashtable-set! hashtable-dest key value)) |
| 262 | + keys values)) |
221 | 263 | hashtable-dest)
|
222 | 264 |
|
223 | 265 | (define (hashtable-sum hashtable init proc)
|
224 |
| - (let-values (((keys vals) (hashtable-entry-lists hashtable))) |
225 |
| - (fold proc init keys vals))) |
| 266 | + (let-values (((keys vals) (hashtable-entries hashtable))) |
| 267 | + (let ((size (vector-length keys))) |
| 268 | + (let loop ((i 0) (result init)) |
| 269 | + (if (fx>=? i size) |
| 270 | + result |
| 271 | + (loop (fx+ i 1) (proc (vector-ref keys i) |
| 272 | + (vector-ref vals i) |
| 273 | + result))))))) |
226 | 274 |
|
227 | 275 | (define (hashtable-map->lset hashtable proc)
|
228 |
| - (hashtable-sum hashtable '() |
229 |
| - (lambda (key value accumulator) |
230 |
| - (cons (proc key value) accumulator)))) |
| 276 | + (let-values (((keys vals) (hashtable-entries hashtable))) |
| 277 | + (let ((size (vector-length keys))) |
| 278 | + (let loop ((i 0) (accumulator '())) |
| 279 | + (if (fx>=? i size) |
| 280 | + accumulator |
| 281 | + (loop (fx+ i 1) (cons (proc (vector-ref keys i) (vector-ref vals i)) |
| 282 | + accumulator))))))) |
231 | 283 |
|
232 | 284 | ;;; XXX If available, let-escape-continuation might be more efficient than
|
233 | 285 | ;;; call/cc here.
|
|
241 | 293 | (return #f #f #f))))
|
242 | 294 |
|
243 | 295 | (define (hashtable-empty? hashtable)
|
244 |
| - (zero? (hashtable-size hashtable))) |
| 296 | + (fxzero? (hashtable-size hashtable))) |
245 | 297 |
|
246 | 298 | ;;; XXX A platform-level implementation could avoid allocating the constant true
|
247 | 299 | ;;; function and the lookup for the key in the delete operation.
|
|
269 | 321 |
|
270 | 322 | #;(define hashtable-hash-function rnrs-hashtable-hash-function)
|
271 | 323 |
|
272 |
| -(define (hashtable-weakness hashtable) #f) |
| 324 | +;;; Defined in helpers.sls |
| 325 | +#;(define (hashtable-weakness hashtable) #f) |
273 | 326 |
|
274 | 327 | #;(define hashtable-mutable? rnrs-hashtable-mutable?)
|
275 | 328 |
|
276 | 329 | (define *hash-salt*
|
277 | 330 | (let ((seed (get-environment-variable "SRFI_126_HASH_SEED")))
|
278 | 331 | (if (or (not seed) (string=? seed ""))
|
279 | 332 | (random-integer (greatest-fixnum))
|
280 |
| - (modulo |
281 |
| - (fold (lambda (char result) |
282 |
| - (+ (char->integer char) result)) |
283 |
| - 0 |
284 |
| - (string->list seed)) |
285 |
| - (greatest-fixnum))))) |
| 333 | + (mod (string-hash seed) (greatest-fixnum))))) |
286 | 334 |
|
287 | 335 | (define (hash-salt) *hash-salt*)
|
288 | 336 |
|
|
0 commit comments