| 1 |
;; Copyright (C) Marc Nieper-Wißkirchen (2016). All Rights Reserved. |
| 2 |
|
| 3 |
;; Permission is hereby granted, free of charge, to any person |
| 4 |
;; obtaining a copy of this software and associated documentation |
| 5 |
;; files (the "Software"), to deal in the Software without |
| 6 |
;; restriction, including without limitation the rights to use, copy, |
| 7 |
;; modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 |
;; of the Software, and to permit persons to whom the Software is |
| 9 |
;; furnished to do so, subject to the following conditions: |
| 10 |
|
| 11 |
;; The above copyright notice and this permission notice shall be |
| 12 |
;; included in all copies or substantial portions of the Software. |
| 13 |
|
| 14 |
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 15 |
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 |
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 17 |
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 18 |
;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 19 |
;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 20 |
;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 |
;; SOFTWARE. |
| 22 |
|
| 23 |
(library (srfi :146 hash) |
| 24 |
(export hashmap hashmap-unfold |
| 25 |
hashmap? hashmap-contains? hashmap-empty? hashmap-disjoint? |
| 26 |
hashmap-ref hashmap-ref/default hashmap-key-comparator |
| 27 |
hashmap-adjoin hashmap-adjoin! |
| 28 |
hashmap-set hashmap-set! |
| 29 |
hashmap-replace hashmap-replace! |
| 30 |
hashmap-delete hashmap-delete! hashmap-delete-all hashmap-delete-all! |
| 31 |
hashmap-intern hashmap-intern! |
| 32 |
hashmap-update hashmap-update! hashmap-update/default hashmap-update!/default |
| 33 |
hashmap-pop hashmap-pop! |
| 34 |
hashmap-search hashmap-search! |
| 35 |
hashmap-size hashmap-find hashmap-count hashmap-any? hashmap-every? |
| 36 |
hashmap-keys hashmap-values hashmap-entries |
| 37 |
hashmap-map hashmap-map->list hashmap-for-each hashmap-fold |
| 38 |
hashmap-filter hashmap-filter! |
| 39 |
hashmap-remove hashmap-remove! |
| 40 |
hashmap-partition hashmap-partition! |
| 41 |
hashmap-copy hashmap->alist alist->hashmap alist->hashmap! |
| 42 |
hashmap=? hashmap<? hashmap>? hashmap<=? hashmap>=? |
| 43 |
hashmap-union hashmap-intersection hashmap-difference hashmap-xor |
| 44 |
hashmap-union! hashmap-intersection! hashmap-difference! hashmap-xor! |
| 45 |
make-hashmap-comparator |
| 46 |
hashmap-comparator |
| 47 |
comparator?) |
| 48 |
(import (except (rnrs) |
| 49 |
define-record-type assoc filter find |
| 50 |
fold-right for-each map member partition |
| 51 |
remove) |
| 52 |
(srfi :1) |
| 53 |
(srfi :8) |
| 54 |
(srfi :9) |
| 55 |
(srfi :158) |
| 56 |
(srfi :128) |
| 57 |
(srfi :145) |
| 58 |
(srfi :146 gleckler hamt-map)) |
| 59 |
|
| 60 |
;; Copyright (C) Marc Nieper-Wißkirchen (2018). All Rights |
| 61 |
;; Reserved. |
| 62 |
|
| 63 |
;; Permission is hereby granted, free of charge, to any person |
| 64 |
;; obtaining a copy of this software and associated documentation |
| 65 |
;; files (the "Software"), to deal in the Software without |
| 66 |
;; restriction, including without limitation the rights to use, copy, |
| 67 |
;; modify, merge, publish, distribute, sublicense, and/or sell copies |
| 68 |
;; of the Software, and to permit persons to whom the Software is |
| 69 |
;; furnished to do so, subject to the following conditions: |
| 70 |
|
| 71 |
;; The above copyright notice and this permission notice shall be |
| 72 |
;; included in all copies or substantial portions of the Software. |
| 73 |
|
| 74 |
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 75 |
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 76 |
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 77 |
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 78 |
;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 79 |
;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 80 |
;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 81 |
;; SOFTWARE. |
| 82 |
|
| 83 |
;; Implementation layer |
| 84 |
|
| 85 |
(define (error* msg) |
| 86 |
(error 'mapping-hash msg)) |
| 87 |
|
| 88 |
(define (tree-search comparator tree obj failure success) |
| 89 |
(let ((entry (phm/get tree obj))) |
| 90 |
(if entry |
| 91 |
(success (car entry) (cdr entry) |
| 92 |
(lambda (new-key new-datum ret) |
| 93 |
(let ((tree (phm/remove tree obj))) |
| 94 |
(values (phm/put tree new-key (cons new-key new-datum)) |
| 95 |
ret))) |
| 96 |
(lambda (ret) |
| 97 |
(values (phm/remove tree obj) ret))) |
| 98 |
(failure (lambda (new-key new-datum ret) |
| 99 |
(values (phm/put tree new-key (cons new-key new-datum)) |
| 100 |
ret)) |
| 101 |
(lambda (ret) |
| 102 |
(values tree ret)))))) |
| 103 |
|
| 104 |
(define (tree-fold proc seed tree) |
| 105 |
(phm/for-each (lambda (key entry) |
| 106 |
(set! seed (proc (car entry) (cdr entry) seed))) |
| 107 |
tree) |
| 108 |
seed) |
| 109 |
|
| 110 |
(define (tree-for-each proc tree) |
| 111 |
(phm/for-each (lambda (key entry) |
| 112 |
(proc (car entry) (cdr entry))) |
| 113 |
tree)) |
| 114 |
|
| 115 |
(define (tree-generator tree) |
| 116 |
(make-coroutine-generator |
| 117 |
(lambda (yield) |
| 118 |
(tree-for-each (lambda item (yield item)) |
| 119 |
tree)))) |
| 120 |
|
| 121 |
;; New types |
| 122 |
|
| 123 |
(define-record-type <hashmap> |
| 124 |
(%make-hashmap comparator tree) |
| 125 |
hashmap? |
| 126 |
(comparator hashmap-key-comparator) |
| 127 |
(tree hashmap-tree)) |
| 128 |
|
| 129 |
(define (make-empty-hashmap comparator) |
| 130 |
(assume (comparator? comparator)) |
| 131 |
(%make-hashmap comparator |
| 132 |
(make-phm (comparator-hash-function comparator) |
| 133 |
(comparator-equality-predicate comparator)))) |
| 134 |
|
| 135 |
;; Exported procedures |
| 136 |
|
| 137 |
;; Constructors |
| 138 |
|
| 139 |
(define (hashmap comparator . args) |
| 140 |
(assume (comparator? comparator)) |
| 141 |
(hashmap-unfold null? |
| 142 |
(lambda (args) |
| 143 |
(values (car args) |
| 144 |
(cadr args))) |
| 145 |
cddr |
| 146 |
args |
| 147 |
comparator)) |
| 148 |
|
| 149 |
(define (hashmap-unfold stop? mapper successor seed comparator) |
| 150 |
(assume (procedure? stop?)) |
| 151 |
(assume (procedure? mapper)) |
| 152 |
(assume (procedure? successor)) |
| 153 |
(assume (comparator? comparator)) |
| 154 |
(let loop ((hashmap (make-empty-hashmap comparator)) |
| 155 |
(seed seed)) |
| 156 |
(if (stop? seed) |
| 157 |
hashmap |
| 158 |
(receive (key value) |
| 159 |
(mapper seed) |
| 160 |
(loop (hashmap-adjoin hashmap key value) |
| 161 |
(successor seed)))))) |
| 162 |
|
| 163 |
;; Predicates |
| 164 |
|
| 165 |
(define (hashmap-empty? hashmap) |
| 166 |
(assume (hashmap? hashmap)) |
| 167 |
(not (hashmap-any? (lambda (key value) #t) hashmap))) |
| 168 |
|
| 169 |
(define (hashmap-contains? hashmap key) |
| 170 |
(assume (hashmap? hashmap)) |
| 171 |
(call/cc |
| 172 |
(lambda (return) |
| 173 |
(hashmap-search hashmap |
| 174 |
key |
| 175 |
(lambda (insert ignore) |
| 176 |
(return #f)) |
| 177 |
(lambda (key value update remove) |
| 178 |
(return #t)))))) |
| 179 |
|
| 180 |
(define (hashmap-disjoint? hashmap1 hashmap2) |
| 181 |
(assume (hashmap? hashmap1)) |
| 182 |
(assume (hashmap? hashmap2)) |
| 183 |
(call/cc |
| 184 |
(lambda (return) |
| 185 |
(hashmap-for-each (lambda (key value) |
| 186 |
(when (hashmap-contains? hashmap2 key) |
| 187 |
(return #f))) |
| 188 |
hashmap1) |
| 189 |
#t))) |
| 190 |
|
| 191 |
;; Accessors |
| 192 |
|
| 193 |
(define hashmap-ref |
| 194 |
(case-lambda |
| 195 |
((hashmap key) |
| 196 |
(assume (hashmap? hashmap)) |
| 197 |
(hashmap-ref hashmap key (lambda () |
| 198 |
(error "hashmap-ref: key not in hashmap" key)))) |
| 199 |
((hashmap key failure) |
| 200 |
(assume (hashmap? hashmap)) |
| 201 |
(assume (procedure? failure)) |
| 202 |
(hashmap-ref hashmap key failure (lambda (value) |
| 203 |
value))) |
| 204 |
((hashmap key failure success) |
| 205 |
(assume (hashmap? hashmap)) |
| 206 |
(assume (procedure? failure)) |
| 207 |
(assume (procedure? success)) |
| 208 |
((call/cc |
| 209 |
(lambda (return-thunk) |
| 210 |
(hashmap-search hashmap |
| 211 |
key |
| 212 |
(lambda (insert ignore) |
| 213 |
(return-thunk failure)) |
| 214 |
(lambda (key value update remove) |
| 215 |
(return-thunk (lambda () (success value))))))))))) |
| 216 |
|
| 217 |
(define (hashmap-ref/default hashmap key default) |
| 218 |
(assume (hashmap? hashmap)) |
| 219 |
(hashmap-ref hashmap key (lambda () default))) |
| 220 |
|
| 221 |
;; Updaters |
| 222 |
|
| 223 |
(define (hashmap-adjoin hashmap . args) |
| 224 |
(assume (hashmap? hashmap)) |
| 225 |
(let loop ((args args) |
| 226 |
(hashmap hashmap)) |
| 227 |
(if (null? args) |
| 228 |
hashmap |
| 229 |
(receive (hashmap value) |
| 230 |
(hashmap-intern hashmap (car args) (lambda () (cadr args))) |
| 231 |
(loop (cddr args) hashmap))))) |
| 232 |
|
| 233 |
(define hashmap-adjoin! hashmap-adjoin) |
| 234 |
|
| 235 |
(define (hashmap-set hashmap . args) |
| 236 |
(assume (hashmap? hashmap)) |
| 237 |
(let loop ((args args) |
| 238 |
(hashmap hashmap)) |
| 239 |
(if (null? args) |
| 240 |
hashmap |
| 241 |
(receive (hashmap) |
| 242 |
(hashmap-update hashmap (car args) (lambda (value) (cadr args)) (lambda () #f)) |
| 243 |
(loop (cddr args) |
| 244 |
hashmap))))) |
| 245 |
|
| 246 |
(define hashmap-set! hashmap-set) |
| 247 |
|
| 248 |
(define (hashmap-replace hashmap key value) |
| 249 |
(assume (hashmap? hashmap)) |
| 250 |
(receive (hashmap obj) |
| 251 |
(hashmap-search hashmap |
| 252 |
key |
| 253 |
(lambda (insert ignore) |
| 254 |
(ignore #f)) |
| 255 |
(lambda (old-key old-value update remove) |
| 256 |
(update key value #f))) |
| 257 |
hashmap)) |
| 258 |
|
| 259 |
(define hashmap-replace! hashmap-replace) |
| 260 |
|
| 261 |
(define (hashmap-delete hashmap . keys) |
| 262 |
(assume (hashmap? hashmap)) |
| 263 |
(hashmap-delete-all hashmap keys)) |
| 264 |
|
| 265 |
(define hashmap-delete! hashmap-delete) |
| 266 |
|
| 267 |
(define (hashmap-delete-all hashmap keys) |
| 268 |
(assume (hashmap? hashmap)) |
| 269 |
(assume (list? keys)) |
| 270 |
(fold (lambda (key hashmap) |
| 271 |
(receive (hashmap obj) |
| 272 |
(hashmap-search hashmap |
| 273 |
key |
| 274 |
(lambda (insert ignore) |
| 275 |
(ignore #f)) |
| 276 |
(lambda (old-key old-value update remove) |
| 277 |
(remove #f))) |
| 278 |
hashmap)) |
| 279 |
hashmap keys)) |
| 280 |
|
| 281 |
(define hashmap-delete-all! hashmap-delete-all) |
| 282 |
|
| 283 |
(define (hashmap-intern hashmap key failure) |
| 284 |
(assume (hashmap? hashmap)) |
| 285 |
(assume (procedure? failure)) |
| 286 |
(call/cc |
| 287 |
(lambda (return) |
| 288 |
(hashmap-search hashmap |
| 289 |
key |
| 290 |
(lambda (insert ignore) |
| 291 |
(receive (value) |
| 292 |
(failure) |
| 293 |
(insert value value))) |
| 294 |
(lambda (old-key old-value update remove) |
| 295 |
(return hashmap old-value)))))) |
| 296 |
|
| 297 |
(define hashmap-intern! hashmap-intern) |
| 298 |
|
| 299 |
(define hashmap-update |
| 300 |
(case-lambda |
| 301 |
((hashmap key updater) |
| 302 |
(hashmap-update hashmap key updater (lambda () |
| 303 |
(error "hashmap-update: key not found in hashmap" key)))) |
| 304 |
((hashmap key updater failure) |
| 305 |
(hashmap-update hashmap key updater failure (lambda (value) |
| 306 |
value))) |
| 307 |
((hashmap key updater failure success) |
| 308 |
(assume (hashmap? hashmap)) |
| 309 |
(assume (procedure? updater)) |
| 310 |
(assume (procedure? failure)) |
| 311 |
(assume (procedure? success)) |
| 312 |
(receive (hashmap obj) |
| 313 |
(hashmap-search hashmap |
| 314 |
key |
| 315 |
(lambda (insert ignore) |
| 316 |
(insert (updater (failure)) #f)) |
| 317 |
(lambda (old-key old-value update remove) |
| 318 |
(update key (updater (success old-value)) #f))) |
| 319 |
hashmap)))) |
| 320 |
|
| 321 |
(define hashmap-update! hashmap-update) |
| 322 |
|
| 323 |
(define (hashmap-update/default hashmap key updater default) |
| 324 |
(hashmap-update hashmap key updater (lambda () default))) |
| 325 |
|
| 326 |
(define hashmap-update!/default hashmap-update/default) |
| 327 |
|
| 328 |
(define hashmap-pop |
| 329 |
(case-lambda |
| 330 |
((hashmap) |
| 331 |
(hashmap-pop hashmap (lambda () |
| 332 |
(error* "hashmap-pop: hashmap has no association")))) |
| 333 |
((hashmap failure) |
| 334 |
(assume (hashmap? hashmap)) |
| 335 |
(assume (procedure? failure)) |
| 336 |
((call/cc |
| 337 |
(lambda (return-thunk) |
| 338 |
(receive (key value) |
| 339 |
(hashmap-find (lambda (key value) #t) hashmap (lambda () (return-thunk failure))) |
| 340 |
(lambda () |
| 341 |
(values (hashmap-delete hashmap key) key value))))))))) |
| 342 |
|
| 343 |
(define hashmap-pop! hashmap-pop) |
| 344 |
|
| 345 |
(define (hashmap-search hashmap key failure success) |
| 346 |
(assume (hashmap? hashmap)) |
| 347 |
(assume (procedure? failure)) |
| 348 |
(assume (procedure? success)) |
| 349 |
(call/cc |
| 350 |
(lambda (return) |
| 351 |
(let*-values |
| 352 |
(((comparator) |
| 353 |
(hashmap-key-comparator hashmap)) |
| 354 |
((tree obj) |
| 355 |
(tree-search comparator |
| 356 |
(hashmap-tree hashmap) |
| 357 |
key |
| 358 |
(lambda (insert ignore) |
| 359 |
(failure (lambda (value obj) |
| 360 |
(insert key value obj)) |
| 361 |
(lambda (obj) |
| 362 |
(return hashmap obj)))) |
| 363 |
success))) |
| 364 |
(values (%make-hashmap comparator tree) |
| 365 |
obj))))) |
| 366 |
|
| 367 |
(define hashmap-search! hashmap-search) |
| 368 |
|
| 369 |
;; The whole hashmap |
| 370 |
|
| 371 |
(define (hashmap-size hashmap) |
| 372 |
(assume (hashmap? hashmap)) |
| 373 |
(hashmap-count (lambda (key value) |
| 374 |
#t) |
| 375 |
hashmap)) |
| 376 |
|
| 377 |
(define (hashmap-find predicate hashmap failure) |
| 378 |
(assume (procedure? predicate)) |
| 379 |
(assume (hashmap? hashmap)) |
| 380 |
(assume (procedure? failure)) |
| 381 |
(call/cc |
| 382 |
(lambda (return) |
| 383 |
(hashmap-for-each (lambda (key value) |
| 384 |
(when (predicate key value) |
| 385 |
(return key value))) |
| 386 |
hashmap) |
| 387 |
(failure)))) |
| 388 |
|
| 389 |
(define (hashmap-count predicate hashmap) |
| 390 |
(assume (procedure? predicate)) |
| 391 |
(assume (hashmap? hashmap)) |
| 392 |
(hashmap-fold (lambda (key value count) |
| 393 |
(if (predicate key value) |
| 394 |
(+ 1 count) |
| 395 |
count)) |
| 396 |
0 hashmap)) |
| 397 |
|
| 398 |
(define (hashmap-any? predicate hashmap) |
| 399 |
(assume (procedure? predicate)) |
| 400 |
(assume (hashmap? hashmap)) |
| 401 |
(call/cc |
| 402 |
(lambda (return) |
| 403 |
(hashmap-for-each (lambda (key value) |
| 404 |
(when (predicate key value) |
| 405 |
(return #t))) |
| 406 |
hashmap) |
| 407 |
#f))) |
| 408 |
|
| 409 |
(define (hashmap-every? predicate hashmap) |
| 410 |
(assume (procedure? predicate)) |
| 411 |
(assume (hashmap? hashmap)) |
| 412 |
(not (hashmap-any? (lambda (key value) |
| 413 |
(not (predicate key value))) |
| 414 |
hashmap))) |
| 415 |
|
| 416 |
(define (hashmap-keys hashmap) |
| 417 |
(assume (hashmap? hashmap)) |
| 418 |
(hashmap-fold (lambda (key value keys) |
| 419 |
(cons key keys)) |
| 420 |
'() hashmap)) |
| 421 |
|
| 422 |
(define (hashmap-values hashmap) |
| 423 |
(assume (hashmap? hashmap)) |
| 424 |
(hashmap-fold (lambda (key value values) |
| 425 |
(cons value values)) |
| 426 |
'() hashmap)) |
| 427 |
|
| 428 |
(define (hashmap-entries hashmap) |
| 429 |
(assume (hashmap? hashmap)) |
| 430 |
(values (hashmap-keys hashmap) |
| 431 |
(hashmap-values hashmap))) |
| 432 |
|
| 433 |
;; Hashmap and folding |
| 434 |
|
| 435 |
(define (hashmap-map proc comparator hashmap) |
| 436 |
(assume (procedure? proc)) |
| 437 |
(assume (comparator? comparator)) |
| 438 |
(assume (hashmap? hashmap)) |
| 439 |
(hashmap-fold (lambda (key value hashmap) |
| 440 |
(receive (key value) |
| 441 |
(proc key value) |
| 442 |
(hashmap-set hashmap key value))) |
| 443 |
(make-empty-hashmap comparator) |
| 444 |
hashmap)) |
| 445 |
|
| 446 |
(define (hashmap-for-each proc hashmap) |
| 447 |
(assume (procedure? proc)) |
| 448 |
(assume (hashmap? hashmap)) |
| 449 |
(tree-for-each proc (hashmap-tree hashmap))) |
| 450 |
|
| 451 |
(define (hashmap-fold proc acc hashmap) |
| 452 |
(assume (procedure? proc)) |
| 453 |
(assume (hashmap? hashmap)) |
| 454 |
(tree-fold proc acc (hashmap-tree hashmap))) |
| 455 |
|
| 456 |
(define (hashmap-map->list proc hashmap) |
| 457 |
(assume (procedure? proc)) |
| 458 |
(assume (hashmap? hashmap)) |
| 459 |
(hashmap-fold (lambda (key value lst) |
| 460 |
(cons (proc key value) lst)) |
| 461 |
'() |
| 462 |
hashmap)) |
| 463 |
|
| 464 |
(define (hashmap-filter predicate hashmap) |
| 465 |
(assume (procedure? predicate)) |
| 466 |
(assume (hashmap? hashmap)) |
| 467 |
(hashmap-fold (lambda (key value hashmap) |
| 468 |
(if (predicate key value) |
| 469 |
(hashmap-set hashmap key value) |
| 470 |
hashmap)) |
| 471 |
(make-empty-hashmap (hashmap-key-comparator hashmap)) |
| 472 |
hashmap)) |
| 473 |
|
| 474 |
(define hashmap-filter! hashmap-filter) |
| 475 |
|
| 476 |
(define (hashmap-remove predicate hashmap) |
| 477 |
(assume (procedure? predicate)) |
| 478 |
(assume (hashmap? hashmap)) |
| 479 |
(hashmap-filter (lambda (key value) |
| 480 |
(not (predicate key value))) |
| 481 |
hashmap)) |
| 482 |
|
| 483 |
(define hashmap-remove! hashmap-remove) |
| 484 |
|
| 485 |
(define (hashmap-partition predicate hashmap) |
| 486 |
(assume (procedure? predicate)) |
| 487 |
(assume (hashmap? hashmap)) |
| 488 |
(values (hashmap-filter predicate hashmap) |
| 489 |
(hashmap-remove predicate hashmap))) |
| 490 |
|
| 491 |
(define hashmap-partition! hashmap-partition) |
| 492 |
|
| 493 |
;; Copying and conversion |
| 494 |
|
| 495 |
(define (hashmap-copy hashmap) |
| 496 |
(assume (hashmap? hashmap)) |
| 497 |
hashmap) |
| 498 |
|
| 499 |
(define (hashmap->alist hashmap) |
| 500 |
(assume (hashmap? hashmap)) |
| 501 |
(hashmap-fold (lambda (key value alist) |
| 502 |
(cons (cons key value) alist)) |
| 503 |
'() hashmap)) |
| 504 |
|
| 505 |
(define (alist->hashmap comparator alist) |
| 506 |
(assume (comparator? comparator)) |
| 507 |
(assume (list? alist)) |
| 508 |
(hashmap-unfold null? |
| 509 |
(lambda (alist) |
| 510 |
(let ((key (caar alist)) |
| 511 |
(value (cdar alist))) |
| 512 |
(values key value))) |
| 513 |
cdr |
| 514 |
alist |
| 515 |
comparator)) |
| 516 |
|
| 517 |
(define (alist->hashmap! hashmap alist) |
| 518 |
(assume (hashmap? hashmap)) |
| 519 |
(assume (list? alist)) |
| 520 |
(fold (lambda (association hashmap) |
| 521 |
(let ((key (car association)) |
| 522 |
(value (cdr association))) |
| 523 |
(hashmap-set hashmap key value))) |
| 524 |
hashmap |
| 525 |
alist)) |
| 526 |
|
| 527 |
;; Subhashmaps |
| 528 |
|
| 529 |
(define hashmap=? |
| 530 |
(case-lambda |
| 531 |
((comparator hashmap) |
| 532 |
(assume (hashmap? hashmap)) |
| 533 |
#t) |
| 534 |
((comparator hashmap1 hashmap2) (%hashmap=? comparator hashmap1 hashmap2)) |
| 535 |
((comparator hashmap1 hashmap2 . hashmaps) |
| 536 |
(and (%hashmap=? comparator hashmap1 hashmap2) |
| 537 |
(apply hashmap=? comparator hashmap2 hashmaps))))) |
| 538 |
(define (%hashmap=? comparator hashmap1 hashmap2) |
| 539 |
(and (eq? (hashmap-key-comparator hashmap1) (hashmap-key-comparator hashmap2)) |
| 540 |
(%hashmap<=? comparator hashmap1 hashmap2) |
| 541 |
(%hashmap<=? comparator hashmap2 hashmap1))) |
| 542 |
|
| 543 |
(define hashmap<=? |
| 544 |
(case-lambda |
| 545 |
((comparator hashmap) |
| 546 |
(assume (hashmap? hashmap)) |
| 547 |
#t) |
| 548 |
((comparator hashmap1 hashmap2) |
| 549 |
(assume (comparator? comparator)) |
| 550 |
(assume (hashmap? hashmap1)) |
| 551 |
(assume (hashmap? hashmap2)) |
| 552 |
(%hashmap<=? comparator hashmap1 hashmap2)) |
| 553 |
((comparator hashmap1 hashmap2 . hashmaps) |
| 554 |
(assume (comparator? comparator)) |
| 555 |
(assume (hashmap? hashmap1)) |
| 556 |
(assume (hashmap? hashmap2)) |
| 557 |
(and (%hashmap<=? comparator hashmap1 hashmap2) |
| 558 |
(apply hashmap<=? comparator hashmap2 hashmaps))))) |
| 559 |
|
| 560 |
(define (%hashmap<=? comparator hashmap1 hashmap2) |
| 561 |
(assume (comparator? comparator)) |
| 562 |
(assume (hashmap? hashmap1)) |
| 563 |
(assume (hashmap? hashmap2)) |
| 564 |
(hashmap-every? (lambda (key value) |
| 565 |
(hashmap-ref hashmap2 key |
| 566 |
(lambda () |
| 567 |
#f) |
| 568 |
(lambda (stored-value) |
| 569 |
(=? comparator value stored-value)))) |
| 570 |
hashmap1)) |
| 571 |
|
| 572 |
(define hashmap>? |
| 573 |
(case-lambda |
| 574 |
((comparator hashmap) |
| 575 |
(assume (hashmap? hashmap)) |
| 576 |
#t) |
| 577 |
((comparator hashmap1 hashmap2) |
| 578 |
(assume (comparator? comparator)) |
| 579 |
(assume (hashmap? hashmap1)) |
| 580 |
(assume (hashmap? hashmap2)) |
| 581 |
(%hashmap>? comparator hashmap1 hashmap2)) |
| 582 |
((comparator hashmap1 hashmap2 . hashmaps) |
| 583 |
(assume (comparator? comparator)) |
| 584 |
(assume (hashmap? hashmap1)) |
| 585 |
(assume (hashmap? hashmap2)) |
| 586 |
(and (%hashmap>? comparator hashmap1 hashmap2) |
| 587 |
(apply hashmap>? comparator hashmap2 hashmaps))))) |
| 588 |
|
| 589 |
(define (%hashmap>? comparator hashmap1 hashmap2) |
| 590 |
(assume (comparator? comparator)) |
| 591 |
(assume (hashmap? hashmap1)) |
| 592 |
(assume (hashmap? hashmap2)) |
| 593 |
(not (%hashmap<=? comparator hashmap1 hashmap2))) |
| 594 |
|
| 595 |
(define hashmap<? |
| 596 |
(case-lambda |
| 597 |
((comparator hashmap) |
| 598 |
(assume (hashmap? hashmap)) |
| 599 |
#t) |
| 600 |
((comparator hashmap1 hashmap2) |
| 601 |
(assume (comparator? comparator)) |
| 602 |
(assume (hashmap? hashmap1)) |
| 603 |
(assume (hashmap? hashmap2)) |
| 604 |
(%hashmap<? comparator hashmap1 hashmap2)) |
| 605 |
((comparator hashmap1 hashmap2 . hashmaps) |
| 606 |
(assume (comparator? comparator)) |
| 607 |
(assume (hashmap? hashmap1)) |
| 608 |
(assume (hashmap? hashmap2)) |
| 609 |
(and (%hashmap<? comparator hashmap1 hashmap2) |
| 610 |
(apply hashmap<? comparator hashmap2 hashmaps))))) |
| 611 |
|
| 612 |
(define (%hashmap<? comparator hashmap1 hashmap2) |
| 613 |
(assume (comparator? comparator)) |
| 614 |
(assume (hashmap? hashmap1)) |
| 615 |
(assume (hashmap? hashmap2)) |
| 616 |
(%hashmap>? comparator hashmap2 hashmap1)) |
| 617 |
|
| 618 |
(define hashmap>=? |
| 619 |
(case-lambda |
| 620 |
((comparator hashmap) |
| 621 |
(assume (hashmap? hashmap)) |
| 622 |
#t) |
| 623 |
((comparator hashmap1 hashmap2) |
| 624 |
(assume (comparator? comparator)) |
| 625 |
(assume (hashmap? hashmap1)) |
| 626 |
(assume (hashmap? hashmap2)) |
| 627 |
(%hashmap>=? comparator hashmap1 hashmap2)) |
| 628 |
((comparator hashmap1 hashmap2 . hashmaps) |
| 629 |
(assume (comparator? comparator)) |
| 630 |
(assume (hashmap? hashmap1)) |
| 631 |
(assume (hashmap? hashmap2)) |
| 632 |
(and (%hashmap>=? comparator hashmap1 hashmap2) |
| 633 |
(apply hashmap>=? comparator hashmap2 hashmaps))))) |
| 634 |
|
| 635 |
(define (%hashmap>=? comparator hashmap1 hashmap2) |
| 636 |
(assume (comparator? comparator)) |
| 637 |
(assume (hashmap? hashmap1)) |
| 638 |
(assume (hashmap? hashmap2)) |
| 639 |
(not (%hashmap<? comparator hashmap1 hashmap2))) |
| 640 |
|
| 641 |
;; Set theory operations |
| 642 |
|
| 643 |
(define (%hashmap-union hashmap1 hashmap2) |
| 644 |
(hashmap-fold (lambda (key2 value2 hashmap) |
| 645 |
(receive (hashmap obj) |
| 646 |
(hashmap-search hashmap |
| 647 |
key2 |
| 648 |
(lambda (insert ignore) |
| 649 |
(insert value2 #f)) |
| 650 |
(lambda (key1 value1 update remove) |
| 651 |
(update key1 value1 #f))) |
| 652 |
hashmap)) |
| 653 |
hashmap1 hashmap2)) |
| 654 |
|
| 655 |
(define (%hashmap-intersection hashmap1 hashmap2) |
| 656 |
(hashmap-filter (lambda (key1 value1) |
| 657 |
(hashmap-contains? hashmap2 key1)) |
| 658 |
hashmap1)) |
| 659 |
|
| 660 |
(define (%hashmap-difference hashmap1 hashmap2) |
| 661 |
(hashmap-fold (lambda (key2 value2 hashmap) |
| 662 |
(receive (hashmap obj) |
| 663 |
(hashmap-search hashmap |
| 664 |
key2 |
| 665 |
(lambda (insert ignore) |
| 666 |
(ignore #f)) |
| 667 |
(lambda (key1 value1 update remove) |
| 668 |
(remove #f))) |
| 669 |
hashmap)) |
| 670 |
hashmap1 hashmap2)) |
| 671 |
|
| 672 |
(define (%hashmap-xor hashmap1 hashmap2) |
| 673 |
(hashmap-fold (lambda (key2 value2 hashmap) |
| 674 |
(receive (hashmap obj) |
| 675 |
(hashmap-search hashmap |
| 676 |
key2 |
| 677 |
(lambda (insert ignore) |
| 678 |
(insert value2 #f)) |
| 679 |
(lambda (key1 value1 update remove) |
| 680 |
(remove #f))) |
| 681 |
hashmap)) |
| 682 |
hashmap1 hashmap2)) |
| 683 |
|
| 684 |
(define hashmap-union |
| 685 |
(case-lambda |
| 686 |
((hashmap) |
| 687 |
(assume (hashmap? hashmap)) |
| 688 |
hashmap) |
| 689 |
((hashmap1 hashmap2) |
| 690 |
(assume (hashmap? hashmap1)) |
| 691 |
(assume (hashmap? hashmap2)) |
| 692 |
(%hashmap-union hashmap1 hashmap2)) |
| 693 |
((hashmap1 hashmap2 . hashmaps) |
| 694 |
(assume (hashmap? hashmap1)) |
| 695 |
(assume (hashmap? hashmap2)) |
| 696 |
(apply hashmap-union (%hashmap-union hashmap1 hashmap2) hashmaps)))) |
| 697 |
(define hashmap-union! hashmap-union) |
| 698 |
|
| 699 |
(define hashmap-intersection |
| 700 |
(case-lambda |
| 701 |
((hashmap) |
| 702 |
(assume (hashmap? hashmap)) |
| 703 |
hashmap) |
| 704 |
((hashmap1 hashmap2) |
| 705 |
(assume (hashmap? hashmap1)) |
| 706 |
(assume (hashmap? hashmap2)) |
| 707 |
(%hashmap-intersection hashmap1 hashmap2)) |
| 708 |
((hashmap1 hashmap2 . hashmaps) |
| 709 |
(assume (hashmap? hashmap1)) |
| 710 |
(assume (hashmap? hashmap2)) |
| 711 |
(apply hashmap-intersection (%hashmap-intersection hashmap1 hashmap2) hashmaps)))) |
| 712 |
(define hashmap-intersection! hashmap-intersection) |
| 713 |
|
| 714 |
(define hashmap-difference |
| 715 |
(case-lambda |
| 716 |
((hashmap) |
| 717 |
(assume (hashmap? hashmap)) |
| 718 |
hashmap) |
| 719 |
((hashmap1 hashmap2) |
| 720 |
(assume (hashmap? hashmap1)) |
| 721 |
(assume (hashmap? hashmap2)) |
| 722 |
(%hashmap-difference hashmap1 hashmap2)) |
| 723 |
((hashmap1 hashmap2 . hashmaps) |
| 724 |
(assume (hashmap? hashmap1)) |
| 725 |
(assume (hashmap? hashmap2)) |
| 726 |
(apply hashmap-difference (%hashmap-difference hashmap1 hashmap2) hashmaps)))) |
| 727 |
(define hashmap-difference! hashmap-difference) |
| 728 |
|
| 729 |
(define hashmap-xor |
| 730 |
(case-lambda |
| 731 |
((hashmap) |
| 732 |
(assume (hashmap? hashmap)) |
| 733 |
hashmap) |
| 734 |
((hashmap1 hashmap2) |
| 735 |
(assume (hashmap? hashmap1)) |
| 736 |
(assume (hashmap? hashmap2)) |
| 737 |
(%hashmap-xor hashmap1 hashmap2)) |
| 738 |
((hashmap1 hashmap2 . hashmaps) |
| 739 |
(assume (hashmap? hashmap1)) |
| 740 |
(assume (hashmap? hashmap2)) |
| 741 |
(apply hashmap-xor (%hashmap-xor hashmap1 hashmap2) hashmaps)))) |
| 742 |
(define hashmap-xor! hashmap-xor) |
| 743 |
|
| 744 |
;; Comparators |
| 745 |
|
| 746 |
(define (hashmap-equality comparator) |
| 747 |
(assume (comparator? comparator)) |
| 748 |
(lambda (hashmap1 hashmap2) |
| 749 |
(hashmap=? comparator hashmap1 hashmap2))) |
| 750 |
|
| 751 |
(define (hashmap-hash-function comparator) |
| 752 |
(assume (comparator? comparator)) |
| 753 |
(lambda (hashmap) |
| 754 |
0 ;; TODO |
| 755 |
#; |
| 756 |
(default-hash (hashmap->alist hashmap)))) |
| 757 |
|
| 758 |
(define (make-hashmap-comparator comparator) |
| 759 |
(make-comparator hashmap? |
| 760 |
(hashmap-equality comparator) |
| 761 |
#f |
| 762 |
(hashmap-hash-function comparator))) |
| 763 |
|
| 764 |
(define hashmap-comparator (make-hashmap-comparator (make-default-comparator))) |
| 765 |
|
| 766 |
(comparator-register-default! hashmap-comparator)) |