-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmapping.sld
850 lines (737 loc) · 32.9 KB
/
mapping.sld
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
(define-library (schemepunk mapping)
(export
mapping mapping-unfold
mapping/ordered mapping-unfold/ordered
mapping? mapping-contains? mapping-empty? mapping-disjoint?
mapping-ref mapping-ref/default mapping-key-comparator
mapping-adjoin mapping-adjoin!
mapping-set mapping-set!
mapping-replace mapping-replace!
mapping-delete mapping-delete! mapping-delete-all mapping-delete-all!
mapping-intern mapping-intern!
mapping-update mapping-update! mapping-update/default mapping-update!/default
mapping-pop mapping-pop!
mapping-search mapping-search!
mapping-size mapping-find mapping-count mapping-any? mapping-every?
mapping-keys mapping-values mapping-entries
mapping-map mapping-map->list mapping-for-each mapping-fold
mapping-filter mapping-filter!
mapping-remove mapping-remove!
mapping-partition mapping-partition!
mapping-copy mapping->alist alist->mapping alist->mapping!
alist->mapping/ordered alist->mapping/ordered!
mapping=? mapping<? mapping>? mapping<=? mapping>=?
mapping-union mapping-intersection mapping-difference mapping-xor
mapping-union! mapping-intersection! mapping-difference! mapping-xor!
make-mapping-comparator
mapping-comparator
mapping-min-key mapping-max-key
mapping-min-value mapping-max-value
mapping-min-entry mapping-max-entry
mapping-key-predecessor mapping-key-successor
mapping-range= mapping-range< mapping-range> mapping-range<= mapping-range>=
mapping-range=! mapping-range<! mapping-range>! mapping-range<=! mapping-range>=!
mapping-split mapping-split!
mapping-catenate mapping-catenate!
mapping-map/monotone mapping-map/monotone! mapping-map-values mapping-map-values!
mapping-fold/reverse
comparator?)
(import (scheme base)
(scheme case-lambda)
(schemepunk syntax)
(schemepunk list)
(schemepunk comparator)
(schemepunk show span)
(schemepunk show block)
(schemepunk show block datum))
(cond-expand
(gauche
; Gauche is the only R7RS whose 146 is faster than (schemepunk btree),
; though it cheats by using a HAMT, making (srfi 146) = (srfi 146 hash).
(import (rename (srfi 146) (mapping=? %mapping=?)
(make-mapping-comparator %make-mapping-comparator)
(mapping-comparator %mapping-comparator))
(only (gauche base) <tree-map> define-method object-equal?)
(only (gauche treeutil) tree-map-compare-as-sequences))
(begin
; Gauche raises an error comparing mappings with different comparators,
; even though the SRFI explicitly says this is not an error.
(define (mapping=? value-comparator x . xs)
(define key-comparator (mapping-key-comparator x))
(and (every (λ=> (mapping-key-comparator _) (eq? key-comparator _)) xs)
(apply %mapping=? `(,value-comparator ,x ,@xs))))
; Gauche 0.9.6's tree map implementation has a typo in its comparator,
; so we monkey-patch it with dynamic methods.
(define-method object-equal? ((a <tree-map>) (b <tree-map>))
(zero? (tree-map-compare-as-sequences a b)))
; Gauche tree maps are not hashable, and so can't be put in sets.
; This is a problem for Datalog, so we add a hashing function.
(define (mapping-hash value-comparator m)
(define key-comparator (mapping-key-comparator m))
(mapping-fold (λ(k v h)
(chain (modulo (* h 33) (hash-bound))
(+ _ (comparator-hash key-comparator k))
(* _ 33)
(modulo _ (hash-bound))
(+ _ (comparator-hash value-comparator v))))
(hash-salt)
m))
(define (make-mapping-comparator value-cmpr)
(make-comparator
mapping?
(cut mapping=? value-cmpr <> <>)
(cut tree-map-compare-as-sequences <> <> value-cmpr)
(cut mapping-hash value-cmpr <>)))
(define mapping-comparator (make-mapping-comparator (make-default-comparator)))))
(else
(import (schemepunk btree))
(begin
(define *not-found* (list 'not-found))
(define (empty-mapping comparator)
(assume (comparator? comparator))
; According to benchmarks, higher-degree B-trees are faster
; in every Scheme except Kawa.
(btree comparator (cond-expand (kawa 3) (else 7))))
(define (mapping comparator . args)
(fold-right-in-pairs (λ(k v m) (btree-set! m k v))
(empty-mapping comparator)
args))
;; Constructors
(define (mapping-unfold stop? mapper successor seed comparator)
(assume (procedure? stop?))
(assume (procedure? mapper))
(assume (procedure? successor))
(let loop ((mapping (empty-mapping comparator))
(seed seed))
(if (stop? seed)
mapping
(let1-values (k v) (mapper seed)
(loop (btree-set! mapping k v)
(successor seed))))))
;; Predicates
(define mapping? btree?)
(define (mapping-empty? mapping)
(assume (mapping? mapping))
(btree-empty? mapping))
(define (mapping-contains? mapping key)
(assume (mapping? mapping))
(not (eq? *not-found* (btree-ref mapping key (λ () *not-found*)))))
(define (mapping-disjoint? mapping1 mapping2)
(assume (mapping? mapping1))
(assume (mapping? mapping2))
(call/cc
(λ return
(btree-fold (λ((k . _) _)
(when (mapping-contains? mapping2 k)
(return #f))
#f)
#f
mapping1)
#t)))
;; Accessors
(define mapping-ref
(case-lambda
((mapping key)
(assume (mapping? mapping))
(btree-ref mapping key (λ ()
(error "mapping-ref: key not in mapping" key))))
((mapping key failure)
(assume (mapping? mapping))
(assume (procedure? failure))
(btree-ref mapping key failure))
((mapping key failure success)
(assume (mapping? mapping))
(assume (procedure? failure))
(assume (procedure? success))
(let1 result (btree-ref mapping key (λ () *not-found*))
(if (eq? result *not-found*) (failure) (success result))))))
(define (mapping-ref/default mapping key default)
(mapping-ref mapping key (λ () default)))
(define mapping-key-comparator btree-key-comparator)
;; Updaters
(define (mapping-set mapping . args)
(assume (mapping? mapping))
(fold-in-pairs (λ(k v m) (btree-set m k v)) mapping args))
(define (mapping-set! mapping . args)
(assume (mapping? mapping))
(fold-in-pairs (λ(k v m) (btree-set! m k v)) mapping args))
(define (mapping-adjoin mapping . args)
(assume (mapping? mapping))
(fold-in-pairs
(λ(k v m) (if (mapping-contains? m k) m (btree-set m k v)))
mapping
args))
(define (mapping-adjoin! mapping . args)
(assume (mapping? mapping))
(fold-in-pairs
(λ(k v m) (if (mapping-contains? m k) m (btree-set! m k v)))
mapping
args))
(define (mapping-replace mapping k v)
(assume (mapping? mapping))
(if (mapping-contains? mapping k) (btree-set mapping k v) mapping))
(define (mapping-replace! mapping k v)
(assume (mapping? mapping))
(if (mapping-contains? mapping k) (btree-set! mapping k v) mapping))
(define (mapping-delete mapping . keys)
(mapping-delete-all mapping keys))
(define (mapping-delete! mapping . keys)
(mapping-delete-all! mapping keys))
(define (mapping-delete-all mapping keys)
(assume (mapping? mapping))
(assume (list? keys))
(fold (λ(k m) (btree-delete m k)) mapping keys))
(define (mapping-delete-all! mapping keys)
(assume (mapping? mapping))
(assume (list? keys))
(fold (λ(k m) (btree-delete! m k)) mapping keys))
(define (mapping-intern mapping key failure)
(assume (procedure? failure))
(mapping-ref mapping key
(λ () (let1 value (failure)
(values (btree-set mapping key value) value)))
(λ value (values mapping value))))
(define (mapping-intern! mapping key failure)
(assume (procedure? failure))
(mapping-ref mapping key
(λ () (let1 value (failure)
(values (btree-set! mapping key value) value)))
(λ value (values mapping value))))
(define mapping-update
(case-lambda
((mapping key updater)
(assume (procedure? updater))
(mapping-ref mapping key
(λ () (error "mapping-update: key not in mapping" key))
(λ=> (updater _) (btree-set mapping key _))))
((mapping key updater failure)
(assume (procedure? updater))
(assume (procedure? failure))
(mapping-ref mapping key
(λ () (btree-set mapping key (updater (failure))))
(λ=> (updater _) (btree-set mapping key _))))
((mapping key updater failure success)
(assume (procedure? updater))
(assume (procedure? failure))
(assume (procedure? success))
(mapping-ref mapping key
(λ () (btree-set mapping key (updater (failure))))
(λ=> (success _) (updater _) (btree-set mapping key _))))))
(define mapping-update!
(case-lambda
((mapping key updater)
(assume (procedure? updater))
(mapping-ref mapping key
(λ () (error "mapping-update!: key not in mapping" key))
(λ=> (updater _) (btree-set! mapping key _))))
((mapping key updater failure)
(assume (procedure? updater))
(assume (procedure? failure))
(mapping-ref mapping key
(λ () (btree-set! mapping key (updater (failure))))
(λ=> (updater _) (btree-set! mapping key _))))
((mapping key updater failure success)
(assume (procedure? updater))
(assume (procedure? failure))
(assume (procedure? success))
(mapping-ref mapping key
(λ () (btree-set! mapping key (updater (failure))))
(λ=> (success _) (updater _) (btree-set! mapping key _))))))
(define (mapping-update/default mapping key updater default)
(mapping-update mapping key updater (λ () default)))
(define (mapping-update!/default mapping key updater default)
(mapping-update! mapping key updater (λ () default)))
(define mapping-pop
(case-lambda
((mapping)
(mapping-pop mapping (λ () (error "mapping-pop: mapping is empty"))))
((mapping failure)
(assume (mapping? mapping))
(assume (procedure? failure))
(let1-values (popped new-mapping) (btree-pop mapping)
(if popped
(values new-mapping (car popped) (cdr popped))
(failure))))))
(define mapping-pop!
(case-lambda
((mapping)
(mapping-pop! mapping (λ () (error "mapping-pop!: mapping is empty"))))
((mapping failure)
(assume (mapping? mapping))
(assume (procedure? failure))
(match (btree-pop! mapping)
((k . v) (values mapping k v))
(else (failure))))))
(define (mapping-search mapping key failure success)
(define (insert value obj)
(values (btree-set mapping key value) obj))
(define (ignore obj)
(values mapping obj))
(define (update new-key new-value obj)
(chain (if (=? (mapping-key-comparator mapping) key new-key)
mapping
(btree-delete mapping key))
(btree-set _ new-key new-value)
(values _ obj)))
(define (remove obj)
(values (btree-delete mapping key) obj))
(assume (procedure? failure))
(assume (procedure? success))
(mapping-ref mapping key
(λ () (failure insert ignore))
(λ value (success key value update remove))))
(define (mapping-search! mapping key failure success)
(define (insert value obj)
(values (btree-set! mapping key value) obj))
(define (ignore obj)
(values mapping obj))
(define (update new-key new-value obj)
(chain (if (=? (mapping-key-comparator mapping) key new-key)
mapping
(btree-delete! mapping key))
(btree-set! _ new-key new-value)
(values _ obj)))
(define (remove obj)
(values (btree-delete! mapping key) obj))
(assume (procedure? failure))
(assume (procedure? success))
(mapping-ref mapping key
(λ () (failure insert ignore))
(λ value (success key value update remove))))
;; The whole mapping
(define (mapping-size mapping)
(assume (mapping? mapping))
(btree-fold (λ (_ n) (+ n 1)) 0 mapping))
(define (mapping-find predicate? mapping failure)
(assume (mapping? mapping))
(assume (procedure? predicate?))
(assume (procedure? failure))
(call/cc
(λ return
(btree-fold (λ((k . v) _)
(when (predicate? k v) (return k v))
#f)
#f
mapping)
(failure))))
(define (mapping-count predicate? mapping)
(assume (mapping? mapping))
(assume (procedure? predicate?))
(btree-fold (λ((k . v) n) (if (predicate? k v) (+ n 1) n))
0
mapping))
(define (mapping-any? predicate? mapping)
(assume (mapping? mapping))
(assume (procedure? predicate?))
(call/cc
(λ return
(btree-fold (λ((k . v) _) (when (predicate? k v) (return #t)) #f)
#f
mapping))))
(define (mapping-every? predicate? mapping)
(assume (mapping? mapping))
(assume (procedure? predicate?))
(call/cc
(λ return
(btree-fold (λ((k . v) _) (unless (predicate? k v) (return #f)) #t)
#t
mapping))))
(define (mapping-keys mapping)
(assume (mapping? mapping))
(btree-fold-right (λ((k . _) ks) (cons k ks)) '() mapping))
(define (mapping-values mapping)
(assume (mapping? mapping))
(btree-fold-right (λ((_ . v) vs) (cons v vs)) '() mapping))
(define (mapping-entries mapping)
(assume (mapping? mapping))
(car+cdr
(btree-fold-right (λ((k . v) (ks . vs)) `((,k ,@ks) . (,v ,@vs)))
'(() . ())
mapping)))
;; Mapping and folding
(define (mapping-map proc comparator mapping)
(assume (procedure? proc))
(assume (comparator? comparator))
(assume (mapping? mapping))
(btree-fold (λ((k . v) m) (let1-values (k2 v2) (proc k v)
(btree-set! m k2 v2)))
(empty-mapping comparator)
mapping))
(define (mapping-map/monotone proc comparator mapping)
(assume (procedure? proc))
(assume (comparator? comparator))
(assume (mapping? mapping))
(btree-map/monotone proc comparator mapping))
(define (mapping-map/monotone! proc comparator mapping)
(assume (procedure? proc))
(assume (comparator? comparator))
(assume (mapping? mapping))
(btree-map/monotone! proc comparator mapping))
(define (mapping-for-each proc mapping)
(assume (procedure? proc))
(assume (mapping? mapping))
(btree-fold (λ((k . v) _) (proc k v) #f) #f mapping))
(define (mapping-fold proc nil mapping)
(assume (procedure? proc))
(assume (mapping? mapping))
(btree-fold (λ((k . v) m) (proc k v m)) nil mapping))
(define (mapping-map->list proc mapping)
(assume (procedure? proc))
(assume (mapping? mapping))
(reverse
(btree-fold (λ((k . v) xs) (cons (proc k v) xs))
'()
mapping)))
(define (mapping-filter predicate? mapping)
(assume (mapping? mapping))
(assume (procedure? predicate?))
(btree-fold (λ((k . v) m) (if (predicate? k v) m (btree-delete m k)))
mapping
mapping))
(define (mapping-remove predicate? mapping)
(assume (procedure? predicate?))
(mapping-filter (λ(k v) (not (predicate? k v))) mapping))
(define (mapping-partition predicate? mapping)
(assume (mapping? mapping))
(assume (procedure? predicate?))
(let1 comparator (mapping-key-comparator mapping)
(car+cdr
(btree-fold (λ((k . v) (yes . no))
(if (predicate? k v)
(cons (btree-set! yes k v) no)
(cons yes (btree-set! no k v))))
(cons (empty-mapping comparator)
(empty-mapping comparator))
mapping))))
; mapping-filter!, mapping-remove!, and mapping-partition!
; cannot be usefully implemented in a linear-update fashion,
; because mappings cannot be mutated while being iterated over.
(define mapping-filter! mapping-filter)
(define mapping-remove! mapping-remove)
(define mapping-partition! mapping-partition)
;; Copying and conversion
(define (mapping-copy mapping)
(assume (mapping? mapping))
(btree-copy mapping))
(define (mapping->alist mapping)
(assume (mapping? mapping))
(btree->alist mapping))
(define (alist->mapping comparator alist)
(assume (comparator? comparator))
(alist->mapping! (empty-mapping comparator) alist))
(define (alist->mapping! mapping alist)
(assume (mapping? mapping))
(assume (list? alist))
(fold (λ((k . v) m) (btree-set! m k v)) mapping alist))
;; Submappings
(define mapping=?
(case-lambda
((comparator mapping1 mapping2)
(assume (comparator? comparator))
(assume (mapping? mapping1))
(assume (mapping? mapping2))
(btree=? comparator mapping1 mapping2))
((comparator mapping1 mapping2 . rest)
(assume (comparator? comparator))
(assume (mapping? mapping1))
(assume (mapping? mapping2))
(and (btree=? comparator mapping1 mapping2)
(apply mapping=? `(,comparator ,mapping2 ,@rest))))))
(define mapping<?
(case-lambda
((comparator mapping1 mapping2)
(assume (comparator? comparator))
(assume (mapping? mapping1))
(assume (mapping? mapping2))
(and (eq? (mapping-key-comparator mapping1)
(mapping-key-comparator mapping2))
(not (= (mapping-size mapping1) (mapping-size mapping2)))
(btree-subset? comparator mapping1 mapping2)))
((comparator mapping1 mapping2 . rest)
(and (mapping<? comparator mapping1 mapping2)
(apply mapping<? `(,comparator ,mapping2 ,@rest))))))
(define mapping>?
(case-lambda
((comparator mapping1 mapping2)
(assume (comparator? comparator))
(assume (mapping? mapping1))
(assume (mapping? mapping2))
(and (eq? (mapping-key-comparator mapping1)
(mapping-key-comparator mapping2))
(not (= (mapping-size mapping1) (mapping-size mapping2)))
(btree-subset? comparator mapping2 mapping1)))
((comparator mapping1 mapping2 . rest)
(and (mapping>? comparator mapping1 mapping2)
(apply mapping>? `(,comparator ,mapping2 ,@rest))))))
(define mapping<=?
(case-lambda
((comparator mapping1 mapping2)
(assume (comparator? comparator))
(assume (mapping? mapping1))
(assume (mapping? mapping2))
(and (eq? (mapping-key-comparator mapping1)
(mapping-key-comparator mapping2))
(btree-subset? comparator mapping1 mapping2)))
((comparator mapping1 mapping2 . rest)
(and (mapping<=? comparator mapping1 mapping2)
(apply mapping<=? `(,comparator ,mapping2 ,@rest))))))
(define mapping>=?
(case-lambda
((comparator mapping1 mapping2)
(assume (comparator? comparator))
(assume (mapping? mapping1))
(assume (mapping? mapping2))
(and (eq? (mapping-key-comparator mapping1)
(mapping-key-comparator mapping2))
(btree-subset? comparator mapping2 mapping1)))
((comparator mapping1 mapping2 . rest)
(and (mapping>=? comparator mapping1 mapping2)
(apply mapping>=? `(,comparator ,mapping2 ,@rest))))))
;; Set theory operations
(define (%mapping-union% x y)
(btree-fold (λ((k . v) m) (btree-set m k v)) y x))
(define (%mapping-union!% x y)
(btree-fold (λ((k . v) m) (btree-set! m k v)) y x))
(define (%mapping-intersection% x y)
(mapping-filter (λ(k _) (mapping-contains? y k)) x))
(define (%mapping-difference% x y)
(mapping-filter (λ(k _) (not (mapping-contains? y k))) x))
(define (%mapping-xor% x y)
(%mapping-union% (%mapping-difference% x y)
(%mapping-difference% y x)))
(define (%mapping-xor!% x y)
(%mapping-union!% (%mapping-difference% x y)
(%mapping-difference% y x)))
(define mapping-union
(case-lambda
((mapping1) mapping1)
((mapping1 mapping2)
(assume (mapping? mapping1))
(assume (mapping? mapping2))
(%mapping-union% mapping1 mapping2))
((mapping1 mapping2 . rest)
(assume (mapping? mapping1))
(assume (mapping? mapping2))
(apply mapping-union
(cons (%mapping-union% mapping1 mapping2) rest)))))
(define mapping-union!
(case-lambda
((mapping1) mapping1)
((mapping1 mapping2)
(assume (mapping? mapping1))
(assume (mapping? mapping2))
(%mapping-union!% mapping1 mapping2))
((mapping1 mapping2 . rest)
(assume (mapping? mapping1))
(assume (mapping? mapping2))
(apply mapping-union!
(cons (%mapping-union!% mapping1 mapping2) rest)))))
(define mapping-intersection
(case-lambda
((mapping1) mapping1)
((mapping1 mapping2)
(assume (mapping? mapping1))
(assume (mapping? mapping2))
(%mapping-intersection% mapping1 mapping2))
((mapping1 mapping2 . rest)
(assume (mapping? mapping1))
(assume (mapping? mapping2))
(apply mapping-intersection
(cons (%mapping-intersection% mapping1 mapping2) rest)))))
(define mapping-intersection! mapping-intersection)
(define mapping-difference
(case-lambda
((mapping1) mapping1)
((mapping1 mapping2)
(assume (mapping? mapping1))
(assume (mapping? mapping2))
(%mapping-difference% mapping1 mapping2))
((mapping1 mapping2 . rest)
(assume (mapping? mapping1))
(assume (mapping? mapping2))
(apply mapping-difference
(cons (%mapping-difference% mapping1 mapping2) rest)))))
(define mapping-difference! mapping-difference)
(define mapping-xor
(case-lambda
((mapping1) mapping1)
((mapping1 mapping2)
(assume (mapping? mapping1))
(assume (mapping? mapping2))
(%mapping-xor% mapping1 mapping2))
((mapping1 mapping2 . rest)
(assume (mapping? mapping1))
(assume (mapping? mapping2))
(apply mapping-xor
(cons (%mapping-xor% mapping1 mapping2) rest)))))
(define mapping-xor!
(case-lambda
((mapping1) mapping1)
((mapping1 mapping2)
(assume (mapping? mapping1))
(assume (mapping? mapping2))
(%mapping-xor!% mapping1 mapping2))
((mapping1 mapping2 . rest)
(assume (mapping? mapping1))
(assume (mapping? mapping2))
(apply mapping-xor!
(cons (%mapping-xor!% mapping1 mapping2) rest)))))
;; Additional procedures for mappings with ordered keys
(define (mapping-min-key mapping)
(assume (mapping? mapping))
(call/cc
(λ return
(btree-fold (λ((k . _) _) (return k)) #f mapping)
(error "mapping-min-key: mapping is empty"))))
(define (mapping-max-key mapping)
(assume (mapping? mapping))
(call/cc
(λ return
(btree-fold-right (λ((k . _) _) (return k)) #f mapping)
(error "mapping-max-key: mapping is empty"))))
(define (mapping-min-value mapping)
(assume (mapping? mapping))
(call/cc
(λ return
(btree-fold (λ((_ . v) _) (return v)) #f mapping)
(error "mapping-min-value: mapping is empty"))))
(define (mapping-max-value mapping)
(assume (mapping? mapping))
(call/cc
(λ return
(btree-fold-right (λ((_ . v) _) (return v)) #f mapping)
(error "mapping-max-value: mapping is empty"))))
(define (mapping-min-entry mapping)
(assume (mapping? mapping))
(car+cdr
(call/cc
(λ return
(btree-fold (λ(e _) (return e)) #f mapping)
(error "mapping-min-entry: mapping is empty")))))
(define (mapping-max-entry mapping)
(assume (mapping? mapping))
(car+cdr
(call/cc
(λ return
(btree-fold-right (λ(e _) (return e)) #f mapping)
(error "mapping-max-entry: mapping is empty")))))
(define (mapping-key-predecessor mapping obj failure)
(assume (mapping? mapping))
(assume (procedure? failure))
(let1 comparator (mapping-key-comparator mapping)
(call/cc
(λ return
(btree-fold-right (λ((k . _) found?)
(if (<? comparator k obj) (return k) #f))
#f
mapping)
(failure)))))
(define (mapping-key-successor mapping obj failure)
(assume (mapping? mapping))
(assume (procedure? failure))
(let1 comparator (mapping-key-comparator mapping)
(call/cc
(λ return
(btree-fold (λ((k . _) found?)
(if (<? comparator obj k) (return k) #f))
#f
mapping)
(failure)))))
(define (mapping-range= mapping obj)
(mapping-ref mapping obj
(λ () (empty-mapping (mapping-key-comparator mapping)))
(λ value (btree-set! (empty-mapping (mapping-key-comparator mapping))
obj
value))))
(define (mapping-range< mapping obj)
(assume (mapping? mapping))
(let1 <? (comparator-ordering-predicate (mapping-key-comparator mapping))
(mapping-filter (λ(k _) (<? k obj)) mapping)))
(define (mapping-range> mapping obj)
(assume (mapping? mapping))
(let1 <? (comparator-ordering-predicate (mapping-key-comparator mapping))
(mapping-filter (λ(k _) (<? obj k)) mapping)))
(define (mapping-range<= mapping obj)
(assume (mapping? mapping))
(let1 <? (comparator-ordering-predicate (mapping-key-comparator mapping))
(mapping-filter (λ(k _) (not (<? obj k))) mapping)))
(define (mapping-range>= mapping obj)
(assume (mapping? mapping))
(let1 <? (comparator-ordering-predicate (mapping-key-comparator mapping))
(mapping-filter (λ(k _) (not (<? k obj))) mapping)))
(define (mapping-fold/reverse proc nil mapping)
(assume (procedure? proc))
(assume (mapping? mapping))
(btree-fold-right (λ((k . v) m) (proc k v m)) nil mapping))
(define (mapping-split mapping obj)
(values
(mapping-range< mapping obj)
(mapping-range<= mapping obj)
(mapping-range= mapping obj)
(mapping-range>= mapping obj)
(mapping-range> mapping obj)))
; This is a placeholder implementation of mapping-catenate that doesn't
; check for monotonicity or provide any performance over mapping-union.
(define (mapping-catenate comparator mapping1 key value mapping2)
(assume (comparator? comparator))
(assume (mapping? mapping1))
(assume (mapping? mapping2))
(%mapping-union% (btree-set mapping1 key value)
mapping2))
(define (mapping-catenate! comparator mapping1 key value mapping2)
(assume (comparator? comparator))
(assume (mapping? mapping1))
(assume (mapping? mapping2))
(%mapping-union!% (btree-set! mapping1 key value)
mapping2))
; TODO: These can be implemented more efficiently
(define mapping/ordered mapping)
(define mapping-unfold/ordered mapping-unfold)
(define alist->mapping/ordered alist->mapping)
(define alist->mapping/ordered! alist->mapping!)
(define mapping-range=! mapping-range=)
(define mapping-range<! mapping-range<)
(define mapping-range>! mapping-range>)
(define mapping-range<=! mapping-range<=)
(define mapping-range>=! mapping-range>=)
(define mapping-split! mapping-split)
;; Comparators
(define make-mapping-comparator make-btree-comparator)
(define mapping-comparator btree-comparator))))
(begin
(define (mapping-map-values proc mapping)
(assume (procedure? proc))
(mapping-map/monotone
(λ(k v) (values k (proc v)))
(mapping-key-comparator mapping)
mapping))
(define (mapping-map-values! proc comparator mapping)
(assume (procedure? proc))
(mapping-map/monotone!
(λ(k v) (values k (proc v)))
(mapping-key-comparator mapping)
mapping))
(define (mapping->block mapping)
(define color (datum-color-record))
(if (mapping-empty? mapping)
(make-block (list (text-span "#,(mapping)" color)))
(make-block
(list
(text-span "#,(mapping" color)
(whitespace-span))
(intercalate (whitespace-span)
(map
(λ((k . v))
(make-block
(list
(text-span "(" color)
(datum->block k)
(whitespace-span))
(list
(datum->block v))
(list
(text-span ")" color))))
(mapping->alist mapping)))
(list
(text-span ")" color)))))
(register-datum-writer! mapping? mapping->block)))