| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
(define (issue-deprecated-warnings?) #t) |
| 20 |
|
| 21 |
(define (issue-warning-deprecated name-of-deprecated-misfeature) |
| 22 |
(if (not (memq name-of-deprecated-misfeature already-warned)) |
| 23 |
(begin |
| 24 |
(set! already-warned |
| 25 |
(cons name-of-deprecated-misfeature already-warned)) |
| 26 |
(if (issue-deprecated-warnings?) |
| 27 |
(let ((out (current-error-port))) |
| 28 |
(display "WARNING: " out) |
| 29 |
(display name-of-deprecated-misfeature out) |
| 30 |
(newline out) |
| 31 |
(display " is deprecated by SRFI 125. See" out) |
| 32 |
(newline out) |
| 33 |
(display " " out) |
| 34 |
(display url:deprecated out) |
| 35 |
(newline out)))))) |
| 36 |
|
| 37 |
(define url:deprecated |
| 38 |
"http://srfi.schemers.org/srfi-125/srfi-125.html") |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
(define already-warned '()) |
| 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 |
(define %enforce-comparator-type-tests #t) |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
(define (%comparator-hash-function comparator) |
| 87 |
(let ((okay? (comparator-type-test-predicate comparator)) |
| 88 |
(hash-function (comparator-hash-function comparator))) |
| 89 |
(if %enforce-comparator-type-tests |
| 90 |
(lambda (x . rest) |
| 91 |
(cond ((not (okay? x)) |
| 92 |
(error #f "key rejected by hash-table comparator" |
| 93 |
x |
| 94 |
comparator)) |
| 95 |
((null? rest) |
| 96 |
(hash-function x)) |
| 97 |
(else |
| 98 |
(apply hash-function x rest)))) |
| 99 |
hash-function))) |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
(define %not-found (list '%not-found)) |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
(define %not-found-irritant (list 'not-found)) |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
(define %not-found-message "hash-table key not found") |
| 114 |
|
| 115 |
|
| 116 |
(define (%check-optional-arguments procname args) |
| 117 |
(if (memq 'thread-safe args) |
| 118 |
(error (string-append (symbol->string procname) |
| 119 |
": unsupported optional argument(s)") |
| 120 |
args))) |
| 121 |
|
| 122 |
(define (%get-hash-table-weakness args) |
| 123 |
(cond |
| 124 |
((memq 'ephemeral-values args) |
| 125 |
(if (or (memq 'ephemeral-keys args) |
| 126 |
(memq 'weak-keys args)) |
| 127 |
'ephemeral-key-and-value |
| 128 |
'ephemeral-value)) |
| 129 |
((memq 'ephemeral-keys args) |
| 130 |
(if (memq 'weak-values args) |
| 131 |
'ephemeral-key-and-value |
| 132 |
'ephemeral-key)) |
| 133 |
((memq 'weak-keys args) |
| 134 |
(if (memq 'weak-values args) |
| 135 |
'weak-key-and-value |
| 136 |
'weak-key)) |
| 137 |
((memq 'weak-values args) |
| 138 |
'weak-value) |
| 139 |
(else #f))) |
| 140 |
|
| 141 |
(define (%get-hash-table-capacity args) |
| 142 |
(find fixnum? args)) |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
(define (hash-table-every proc ht) |
| 148 |
(call-with-values |
| 149 |
(lambda () (hashtable-entries ht)) |
| 150 |
(lambda (keys vals) |
| 151 |
(let ((size (vector-length keys))) |
| 152 |
(let loop ((i 0)) |
| 153 |
(or (fx>=? i size) |
| 154 |
(let* ((key (vector-ref keys i)) |
| 155 |
(val (vector-ref vals i))) |
| 156 |
(and (proc key val) |
| 157 |
(loop (fx+ i 1)))))))))) |
| 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 |
— to |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
(define (make-hash-table comparator/equiv . rest) |
| 224 |
(if (comparator? comparator/equiv) |
| 225 |
(let ((equiv (comparator-equality-predicate comparator/equiv)) |
| 226 |
(hash-function (%comparator-hash-function comparator/equiv))) |
| 227 |
(%make-hash-table equiv hash-function rest)) |
| 228 |
(let* ((equiv comparator/equiv) |
| 229 |
(hash-function (if (and (not (null? rest)) |
| 230 |
(procedure? (car rest))) |
| 231 |
(car rest) |
| 232 |
#f)) |
| 233 |
(rest (if hash-function (cdr rest) rest))) |
| 234 |
(issue-warning-deprecated 'srfi-69-style:make-hash-table) |
| 235 |
(%make-hash-table equiv hash-function rest)))) |
| 236 |
|
| 237 |
(define (%make-hash-table equiv hash-function opts) |
| 238 |
(%check-optional-arguments 'make-hash-table opts) |
| 239 |
(let ((weakness (%get-hash-table-weakness opts)) |
| 240 |
(capacity (%get-hash-table-capacity opts))) |
| 241 |
|
| 242 |
(cond ((equal? equiv eq?) |
| 243 |
(make-eq-hashtable capacity weakness)) |
| 244 |
((equal? equiv eqv?) |
| 245 |
(make-eqv-hashtable capacity weakness)) |
| 246 |
(hash-function |
| 247 |
(make-hashtable hash-function equiv capacity weakness)) |
| 248 |
((equal? equiv equal?) |
| 249 |
(make-hashtable equal-hash equiv capacity weakness)) |
| 250 |
((equal? equiv string=?) |
| 251 |
(make-hashtable string-hash equiv capacity weakness)) |
| 252 |
((equal? equiv string-ci=?) |
| 253 |
(make-hashtable string-ci-hash equiv capacity weakness)) |
| 254 |
((equal? equiv symbol=?) |
| 255 |
(make-hashtable symbol-hash equiv capacity weakness)) |
| 256 |
(else |
| 257 |
(error "make-hash-table: unable to infer hash function" |
| 258 |
equiv))))) |
| 259 |
|
| 260 |
(define (hash-table comparator . rest) |
| 261 |
(let ((ht (apply make-hash-table comparator rest))) |
| 262 |
(let loop ((kvs rest)) |
| 263 |
(cond |
| 264 |
((null? kvs) #f) |
| 265 |
((null? (cdr kvs)) (error #f "hash-table: wrong number of arguments")) |
| 266 |
((hashtable-contains? ht (car kvs)) |
| 267 |
(error "hash-table: two equivalent keys were provided" |
| 268 |
(car kvs))) |
| 269 |
(else (hashtable-set! ht (car kvs) (cadr kvs)) |
| 270 |
(loop (cddr kvs))))) |
| 271 |
(hash-table-copy ht #f))) |
| 272 |
|
| 273 |
(define (hash-table-unfold stop? mapper successor seed comparator . rest) |
| 274 |
(let ((ht (apply make-hash-table comparator rest))) |
| 275 |
(let loop ((seed seed)) |
| 276 |
(if (stop? seed) |
| 277 |
ht |
| 278 |
(call-with-values |
| 279 |
(lambda () (mapper seed)) |
| 280 |
(lambda (key val) |
| 281 |
(hash-table-set! ht key val) |
| 282 |
(loop (successor seed)))))))) |
| 283 |
|
| 284 |
(define (alist->hash-table alist comparator/equiv . rest) |
| 285 |
(if (and (not (null? rest)) |
| 286 |
(procedure? (car rest))) |
| 287 |
(issue-warning-deprecated 'srfi-69-style:alist->hash-table)) |
| 288 |
(let ((ht (apply make-hash-table comparator/equiv rest)) |
| 289 |
(entries (reverse alist))) |
| 290 |
(for-each (lambda (entry) |
| 291 |
(hash-table-set! ht (car entry) (cdr entry))) |
| 292 |
entries) |
| 293 |
ht)) |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
(define (hash-table=? value-comparator ht1 ht2) |
| 307 |
(let ((val=? (comparator-equality-predicate value-comparator)) |
| 308 |
(n1 (hash-table-size ht1)) |
| 309 |
(n2 (hash-table-size ht2))) |
| 310 |
(and (= n1 n2) |
| 311 |
(eq? (hashtable-equivalence-function ht1) |
| 312 |
(hashtable-equivalence-function ht2)) |
| 313 |
(hash-table-every (lambda (key val1) |
| 314 |
(and (hash-table-contains? ht2 key) |
| 315 |
(val=? val1 |
| 316 |
(hashtable-ref ht2 key 'ignored)))) |
| 317 |
ht1)))) |
| 318 |
|
| 319 |
(define (hash-table-mutable? ht) |
| 320 |
(hashtable-mutable? ht)) |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
(define hash-table-ref |
| 325 |
(case-lambda |
| 326 |
((ht key) (hashtable-ref ht key)) |
| 327 |
((ht key failure) |
| 328 |
(let ((val (hashtable-ref ht key %not-found))) |
| 329 |
(if (eq? val %not-found) |
| 330 |
(failure) |
| 331 |
val))) |
| 332 |
((ht key failure success) |
| 333 |
(let ((val (hashtable-ref ht key %not-found))) |
| 334 |
(if (eq? val %not-found) |
| 335 |
(failure) |
| 336 |
(success val)))))) |
| 337 |
|
| 338 |
(define (hash-table-ref/default ht key default) |
| 339 |
(hashtable-ref ht key default)) |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
(define hash-table-set! |
| 344 |
(case-lambda |
| 345 |
((ht) #f) |
| 346 |
((ht key val) (hashtable-set! ht key val)) |
| 347 |
((ht key1 val1 key2 val2 . others) |
| 348 |
(hashtable-set! ht key1 val1) |
| 349 |
(hashtable-set! ht key2 val2) |
| 350 |
(apply hash-table-set! ht others)))) |
| 351 |
|
| 352 |
(define (hash-table-delete! ht . keys) |
| 353 |
(let ((count 0)) |
| 354 |
(for-each (lambda (key) |
| 355 |
(when (hashtable-contains? ht key) |
| 356 |
(set! count (fx+ 1 count)) |
| 357 |
(hashtable-delete! ht key))) |
| 358 |
keys) |
| 359 |
count)) |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
(define hash-table-update! |
| 365 |
(case-lambda |
| 366 |
((ht key updater) |
| 367 |
(hashtable-update! ht key updater)) |
| 368 |
|
| 369 |
((ht key updater failure) |
| 370 |
(let ((updater* (lambda (val) |
| 371 |
(if (eq? %not-found val) |
| 372 |
(updater (failure)) |
| 373 |
(updater val))))) |
| 374 |
(hashtable-update! ht key updater* %not-found))) |
| 375 |
|
| 376 |
((ht key updater failure success) |
| 377 |
(let* ((updater* (lambda (val) |
| 378 |
(if (eq? %not-found val) |
| 379 |
(updater (failure)) |
| 380 |
(success (updater val)))))) |
| 381 |
(hashtable-update! ht key updater* %not-found))))) |
| 382 |
|
| 383 |
(define (hash-table-update!/default ht key updater default) |
| 384 |
(hashtable-update! ht key updater default)) |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
(define (hash-table-keys ht) |
| 398 |
(vector->list (hashtable-keys ht))) |
| 399 |
|
| 400 |
(define (hash-table-values ht) |
| 401 |
(vector->list (hashtable-values ht))) |
| 402 |
|
| 403 |
(define (hash-table-entries ht) |
| 404 |
(call-with-values |
| 405 |
(lambda () (hashtable-entries ht)) |
| 406 |
(lambda (keys vals) |
| 407 |
(values (vector->list keys) |
| 408 |
(vector->list vals))))) |
| 409 |
|
| 410 |
(define (hash-table-find proc ht failure) |
| 411 |
(call-with-values |
| 412 |
(lambda () (hashtable-entries ht)) |
| 413 |
(lambda (keys vals) |
| 414 |
(let ((size (vector-length keys))) |
| 415 |
(let loop ((i 0)) |
| 416 |
(if (fx>=? i size) |
| 417 |
(failure) |
| 418 |
(let* ((key (vector-ref keys i)) |
| 419 |
(val (vector-ref vals i)) |
| 420 |
(x (proc key val))) |
| 421 |
(or x (loop (fx+ i 1)))))))))) |
| 422 |
|
| 423 |
(define (hash-table-count pred ht) |
| 424 |
(let ((count 0)) |
| 425 |
(call-with-values |
| 426 |
(lambda () (hashtable-entries ht)) |
| 427 |
(lambda (keys vals) |
| 428 |
(vector-for-each (lambda (key val) |
| 429 |
(if (pred key val) (set! count (fx+ count 1)))) |
| 430 |
keys vals))) |
| 431 |
count)) |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
(define (hash-table-map proc comparator ht) |
| 436 |
(let ((result (make-hash-table comparator))) |
| 437 |
(hash-table-for-each |
| 438 |
(lambda (key val) |
| 439 |
(hash-table-set! result key (proc val))) |
| 440 |
ht) |
| 441 |
result)) |
| 442 |
|
| 443 |
(define (hash-table-map->list proc ht) |
| 444 |
(call-with-values |
| 445 |
(lambda () (hash-table-entries ht)) |
| 446 |
(lambda (keys vals) |
| 447 |
(map proc keys vals)))) |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
(define (hash-table-for-each proc ht) |
| 454 |
(hashtable-walk ht proc)) |
| 455 |
|
| 456 |
(define (hash-table-map! proc ht) |
| 457 |
(hashtable-update-all! ht proc)) |
| 458 |
|
| 459 |
(define (hash-table-fold proc init ht) |
| 460 |
(if (hashtable? proc) |
| 461 |
(deprecated:hash-table-fold proc init ht) |
| 462 |
(hashtable-sum ht init proc))) |
| 463 |
|
| 464 |
(define (hash-table-prune! proc ht) |
| 465 |
(hashtable-prune! ht proc)) |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
(define (hash-table-empty-copy ht) |
| 472 |
(let* ((ht2 (hash-table-copy ht #t)) |
| 473 |
(ignored (hash-table-clear! ht2))) |
| 474 |
ht2)) |
| 475 |
|
| 476 |
(define (hash-table->alist ht) |
| 477 |
(call-with-values |
| 478 |
(lambda () (hash-table-entries ht)) |
| 479 |
(lambda (keys vals) |
| 480 |
(map cons keys vals)))) |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
(define (hash-table-union! ht1 ht2) |
| 485 |
(hash-table-for-each |
| 486 |
(lambda (key2 val2) |
| 487 |
(if (not (hashtable-contains? ht1 key2)) |
| 488 |
(hashtable-set! ht1 key2 val2))) |
| 489 |
ht2) |
| 490 |
ht1) |
| 491 |
|
| 492 |
(define (hash-table-intersection! ht1 ht2) |
| 493 |
(hash-table-for-each |
| 494 |
(lambda (key1 val1) |
| 495 |
(if (not (hashtable-contains? ht2 key1)) |
| 496 |
(hashtable-delete! ht1 key1))) |
| 497 |
ht1) |
| 498 |
ht1) |
| 499 |
|
| 500 |
(define (hash-table-difference! ht1 ht2) |
| 501 |
(hash-table-for-each |
| 502 |
(lambda (key1 val1) |
| 503 |
(if (hashtable-contains? ht2 key1) |
| 504 |
(hashtable-delete! ht1 key1))) |
| 505 |
ht1) |
| 506 |
ht1) |
| 507 |
|
| 508 |
(define (hash-table-xor! ht1 ht2) |
| 509 |
(hash-table-for-each |
| 510 |
(lambda (key2 val2) |
| 511 |
(if (hashtable-contains? ht1 key2) |
| 512 |
(hashtable-delete! ht1 key2) |
| 513 |
(hashtable-set! ht1 key2 val2))) |
| 514 |
ht2) |
| 515 |
ht1) |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
(define (deprecated:hash obj . rest) |
| 530 |
(issue-warning-deprecated 'hash) |
| 531 |
(default-hash obj)) |
| 532 |
|
| 533 |
(define (deprecated:string-hash obj . rest) |
| 534 |
(issue-warning-deprecated 'srfi-125:string-hash) |
| 535 |
(string-hash obj)) |
| 536 |
|
| 537 |
(define (deprecated:string-ci-hash obj . rest) |
| 538 |
(issue-warning-deprecated 'srfi-125:string-ci-hash) |
| 539 |
(string-ci-hash obj)) |
| 540 |
|
| 541 |
(define (deprecated:hash-by-identity obj . rest) |
| 542 |
(issue-warning-deprecated 'hash-by-identity) |
| 543 |
(deprecated:hash obj)) |
| 544 |
|
| 545 |
(define (deprecated:hash-table-equivalence-function ht) |
| 546 |
(issue-warning-deprecated 'hash-table-equivalence-function) |
| 547 |
(hashtable-equivalence-function ht)) |
| 548 |
|
| 549 |
(define (deprecated:hash-table-hash-function ht) |
| 550 |
(issue-warning-deprecated 'hash-table-hash-function) |
| 551 |
(hashtable-hash-function ht)) |
| 552 |
|
| 553 |
(define (deprecated:hash-table-exists? ht key) |
| 554 |
(issue-warning-deprecated 'hash-table-exists?) |
| 555 |
(hash-table-contains? ht key)) |
| 556 |
|
| 557 |
(define (deprecated:hash-table-walk ht proc) |
| 558 |
(issue-warning-deprecated 'hash-table-walk) |
| 559 |
(hash-table-for-each proc ht)) |
| 560 |
|
| 561 |
(define (deprecated:hash-table-fold ht proc seed) |
| 562 |
(issue-warning-deprecated 'srfi-69-style:hash-table-fold) |
| 563 |
(hash-table-fold proc seed ht)) |
| 564 |
|
| 565 |
(define (deprecated:hash-table-merge! ht1 ht2) |
| 566 |
(issue-warning-deprecated 'hash-table-merge!) |
| 567 |
(hash-table-union! ht1 ht2)) |
| 568 |
|
| 569 |
|