| 1 |
(define make-eq-hashtable |
| 2 |
(case-lambda |
| 3 |
(() (rnrs:make-eq-hashtable)) |
| 4 |
((capacity) |
| 5 |
(if capacity |
| 6 |
(rnrs:make-eq-hashtable capacity) |
| 7 |
(rnrs:make-eq-hashtable))) |
| 8 |
((capacity weakness) |
| 9 |
(if weakness |
| 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 |
(if capacity |
| 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)))))) |
| 23 |
|
| 24 |
(define make-eqv-hashtable |
| 25 |
(case-lambda |
| 26 |
(() (rnrs:make-eqv-hashtable)) |
| 27 |
((capacity) |
| 28 |
(if capacity |
| 29 |
(rnrs:make-eqv-hashtable capacity) |
| 30 |
(rnrs:make-eqv-hashtable))) |
| 31 |
((capacity weakness) |
| 32 |
(if weakness |
| 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)) |
| 39 |
(if capacity |
| 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))) |
| 43 |
(if capacity |
| 44 |
(rnrs:make-eqv-hashtable capacity) |
| 45 |
(rnrs:make-eqv-hashtable)))))) |
| 46 |
|
| 47 |
(define make-hashtable |
| 48 |
(case-lambda |
| 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))))) |
| 58 |
((hash equiv capacity) |
| 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))))) |
| 70 |
((hash equiv capacity weakness) |
| 71 |
(if hash |
| 72 |
(let ((hash (if (pair? hash) (car hash) hash))) |
| 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))) |
| 86 |
(if capacity |
| 87 |
(rnrs:make-hashtable hash equiv capacity) |
| 88 |
(rnrs:make-hashtable hash equiv)))) |
| 89 |
(cond |
| 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))))))) |
| 97 |
|
| 98 |
(define (alist->eq-hashtable . args) |
| 99 |
(apply alist->hashtable #f eq? args)) |
| 100 |
|
| 101 |
(define (alist->eqv-hashtable . args) |
| 102 |
(apply alist->hashtable #f eqv? args)) |
| 103 |
|
| 104 |
(define alist->hashtable |
| 105 |
(case-lambda |
| 106 |
((hash equiv alist) |
| 107 |
(alist->hashtable hash equiv #f #f alist)) |
| 108 |
((hash equiv capacity alist) |
| 109 |
(alist->hashtable hash equiv capacity #f alist)) |
| 110 |
((hash equiv capacity weakness alist) |
| 111 |
(let ((hashtable (make-hashtable hash equiv capacity weakness))) |
| 112 |
(for-each (lambda (entry) |
| 113 |
(hashtable-set! hashtable (car entry) (cdr entry))) |
| 114 |
(reverse alist)) |
| 115 |
hashtable)))) |
| 116 |
|
| 117 |
(define-enumeration weakness |
| 118 |
(weak-key |
| 119 |
weak-value |
| 120 |
weak-key-and-value |
| 121 |
ephemeral-key |
| 122 |
ephemeral-value |
| 123 |
ephemeral-key-and-value) |
| 124 |
weakness-set) |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
(define nil (cons #f #f)) |
| 131 |
(define (nil? obj) (eq? obj nil)) |
| 132 |
|
| 133 |
(define hashtable-ref |
| 134 |
(case-lambda |
| 135 |
((hashtable key) |
| 136 |
(let ((value (rnrs:hashtable-ref hashtable key nil))) |
| 137 |
(if (nil? value) |
| 138 |
(error "No such key in hashtable." hashtable key) |
| 139 |
value))) |
| 140 |
((hashtable key default) |
| 141 |
(rnrs:hashtable-ref hashtable key default)))) |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
(define (hashtable-lookup hashtable key) |
| 150 |
(let ((value (rnrs:hashtable-ref hashtable key nil))) |
| 151 |
(if (nil? value) |
| 152 |
(values #f #f) |
| 153 |
(values value #t)))) |
| 154 |
|
| 155 |
(define hashtable-update! |
| 156 |
(case-lambda |
| 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)) |
| 165 |
((hashtable key proc default) |
| 166 |
(rnrs:hashtable-update! hashtable key proc default)))) |
| 167 |
|
| 168 |
(define (hashtable-intern! hashtable key default-proc) |
| 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)))) |
| 182 |
|
| 183 |
(define hashtable-copy |
| 184 |
(case-lambda |
| 185 |
((hashtable) (hashtable-copy hashtable #f #f)) |
| 186 |
((hashtable mutable) (hashtable-copy hashtable mutable #f)) |
| 187 |
((hashtable mutable weakness) |
| 188 |
(when weakness |
| 189 |
(error 'hashtable-copy "No weak or ephemeral tables supported.")) |
| 190 |
(rnrs:hashtable-copy hashtable mutable)))) |
| 191 |
|
| 192 |
(define hashtable-clear! |
| 193 |
(case-lambda |
| 194 |
((hashtable) (rnrs:hashtable-clear! hashtable)) |
| 195 |
((hashtable capacity) |
| 196 |
(if capacity |
| 197 |
(cond-expand |
| 198 |
(ikarus (rnrs:hashtable-clear! hashtable)) |
| 199 |
(else (rnrs:hashtable-clear! hashtable capacity))) |
| 200 |
(rnrs:hashtable-clear! hashtable))))) |
| 201 |
|
| 202 |
(define hashtable-empty-copy |
| 203 |
(case-lambda |
| 204 |
((hashtable) (hashtable-empty-copy hashtable #f)) |
| 205 |
((hashtable capacity) |
| 206 |
(make-hashtable (hashtable-hash-function hashtable) |
| 207 |
(hashtable-equivalence-function hashtable) |
| 208 |
(if (eq? #t capacity) |
| 209 |
(hashtable-size hashtable) |
| 210 |
capacity) |
| 211 |
(hashtable-weakness hashtable))))) |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
(define (hashtable-key-list hashtable) |
| 224 |
(hashtable-map->lset hashtable (lambda (key value) key))) |
| 225 |
|
| 226 |
(define (hashtable-value-list hashtable) |
| 227 |
(hashtable-map->lset hashtable (lambda (key value) value))) |
| 228 |
|
| 229 |
(define (hashtable-entry-lists hashtable) |
| 230 |
(let ((keys '()) |
| 231 |
(vals '())) |
| 232 |
(hashtable-walk hashtable |
| 233 |
(lambda (key val) |
| 234 |
(set! keys (cons key keys)) |
| 235 |
(set! vals (cons val vals)))) |
| 236 |
(values keys vals))) |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
(define (hashtable-walk hashtable proc) |
| 244 |
(let-values (((keys values) (hashtable-entries hashtable))) |
| 245 |
(vector-for-each proc keys values))) |
| 246 |
|
| 247 |
(define (hashtable-update-all! hashtable proc) |
| 248 |
(let-values (((keys values) (hashtable-entries hashtable))) |
| 249 |
(vector-for-each (lambda (key value) |
| 250 |
(hashtable-set! hashtable key (proc key value))) |
| 251 |
keys values))) |
| 252 |
|
| 253 |
(define (hashtable-prune! hashtable proc) |
| 254 |
(let-values (((keys values) (hashtable-entries hashtable))) |
| 255 |
(vector-for-each (lambda (key value) |
| 256 |
(when (proc key value) |
| 257 |
(hashtable-delete! hashtable key))) |
| 258 |
keys values))) |
| 259 |
|
| 260 |
(define (hashtable-merge! hashtable-dest hashtable-source) |
| 261 |
(let-values (((keys values) (hashtable-entries hashtable-source))) |
| 262 |
(vector-for-each (lambda (key value) |
| 263 |
(hashtable-set! hashtable-dest key value)) |
| 264 |
keys values)) |
| 265 |
hashtable-dest) |
| 266 |
|
| 267 |
(define (hashtable-sum hashtable init proc) |
| 268 |
(let-values (((keys vals) (hashtable-entries hashtable))) |
| 269 |
(let ((size (vector-length keys))) |
| 270 |
(let loop ((i 0) (result init)) |
| 271 |
(if (fx>=? i size) |
| 272 |
result |
| 273 |
(loop (fx+ i 1) (proc (vector-ref keys i) |
| 274 |
(vector-ref vals i) |
| 275 |
result))))))) |
| 276 |
|
| 277 |
(define (hashtable-map->lset hashtable proc) |
| 278 |
(let-values (((keys vals) (hashtable-entries hashtable))) |
| 279 |
(let ((size (vector-length keys))) |
| 280 |
(let loop ((i 0) (accumulator '())) |
| 281 |
(if (fx>=? i size) |
| 282 |
accumulator |
| 283 |
(loop (fx+ i 1) (cons (proc (vector-ref keys i) (vector-ref vals i)) |
| 284 |
accumulator))))))) |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
(define (hashtable-find hashtable proc) |
| 289 |
(call/cc |
| 290 |
(lambda (return) |
| 291 |
(hashtable-walk hashtable |
| 292 |
(lambda (key value) |
| 293 |
(when (proc key value) |
| 294 |
(return key value #t)))) |
| 295 |
(return #f #f #f)))) |
| 296 |
|
| 297 |
(define (hashtable-empty? hashtable) |
| 298 |
(fxzero? (hashtable-size hashtable))) |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
(define (hashtable-pop! hashtable) |
| 303 |
(if (hashtable-empty? hashtable) |
| 304 |
(error "Cannot pop from empty hashtable." hashtable) |
| 305 |
(let-values (((key value found?) |
| 306 |
(hashtable-find hashtable (lambda (k v) #t)))) |
| 307 |
(hashtable-delete! hashtable key) |
| 308 |
(values key value)))) |
| 309 |
|
| 310 |
(define hashtable-inc! |
| 311 |
(case-lambda |
| 312 |
((hashtable key) (hashtable-inc! hashtable key 1)) |
| 313 |
((hashtable key number) |
| 314 |
(hashtable-update! hashtable key (lambda (v) (+ v number)) 0)))) |
| 315 |
|
| 316 |
(define hashtable-dec! |
| 317 |
(case-lambda |
| 318 |
((hashtable key) (hashtable-dec! hashtable key 1)) |
| 319 |
((hashtable key number) |
| 320 |
(hashtable-update! hashtable key (lambda (v) (- v number)) 0)))) |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
(define *hash-salt* |
| 332 |
(let ((seed (get-environment-variable "SRFI_126_HASH_SEED"))) |
| 333 |
(if (or (not seed) (string=? seed "")) |
| 334 |
(random-integer (greatest-fixnum)) |
| 335 |
(mod (string-hash seed) (greatest-fixnum))))) |
| 336 |
|
| 337 |
(define (hash-salt) *hash-salt*) |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|