Debian Codegraph @

%3a113/sets-and-bags.sls

raw
1 (library (srfi :113 sets-and-bags)
2
3 (export
4 set set-unfold
5 set? set-contains? set-empty? set-disjoint?
6 set-member set-element-comparator
7 set-adjoin set-adjoin! set-replace set-replace!
8 set-delete set-delete! set-delete-all set-delete-all! set-search!
9 set-size set-find set-count set-any? set-every?
10 set-map set-for-each set-fold
11 set-filter set-remove set-partition
12 set-filter! set-remove! set-partition!
13 set-copy set->list list->set list->set!
14 set=? set<? set>? set<=? set>=?
15 set-union set-intersection set-difference set-xor
16 set-union! set-intersection! set-difference! set-xor!
17 set-comparator
18
19 bag bag-unfold
20 bag? bag-contains? bag-empty? bag-disjoint?
21 bag-member bag-element-comparator
22 bag-adjoin bag-adjoin! bag-replace bag-replace!
23 bag-delete bag-delete! bag-delete-all bag-delete-all! bag-search!
24 bag-size bag-find bag-count bag-any? bag-every?
25 bag-map bag-for-each bag-fold
26 bag-filter bag-remove bag-partition
27 bag-filter! bag-remove! bag-partition!
28 bag-copy bag->list list->bag list->bag!
29 bag=? bag<? bag>? bag<=? bag>=?
30 bag-union bag-intersection bag-difference bag-xor
31 bag-union! bag-intersection! bag-difference! bag-xor!
32 bag-comparator
33 bag-sum bag-sum! bag-product bag-product!
34 bag-unique-size bag-element-count bag-for-each-unique bag-fold-unique
35 bag-increment! bag-decrement! bag->set set->bag set->bag!
36 bag->alist alist->bag)
37
38 (import (except (rnrs) define-record-type)
39 (only (rnrs r5rs) modulo)
40 (srfi :9)
41 (srfi :128 comparators)
42 (srfi :125 hashtables))
43
44 (begin
45
46 ;;;; Implementation of general sets and bags for SRFI 113
47
48 ;;; A "sob" object is the representation of both sets and bags.
49 ;;; This allows each set-* and bag-* procedure to be implemented
50 ;;; using the same code, without having to deal in ugly indirections
51 ;;; over the field accessors. There are three fields, "sob-multi?",
52 ;;; "sob-hash-table", and "sob-comparator."
53
54 ;;; The value of "sob-multi?" is #t for bags and #f for sets.
55 ;;; "Sob-hash-table" maps the elements of the sob to the number of times
56 ;;; the element appears, which is always 1 for a set, any positive value
57 ;;; for a bag. "Sob-comparator" is the comparator for the elements of
58 ;;; the set.
59
60 ;;; Note that sob-* procedures do not do type checking or (typically) the
61 ;;; copying required for supporting pure functional update. These things
62 ;;; are done by the set-* and bag-* procedures, which are externally
63 ;;; exposed (but trivial and mostly uncommented below).
64
65
66 ;;; Shim to convert from SRFI 69 to the future "intermediate hash tables"
67 ;;; SRFI. Unfortunately, hash-table-fold is incompatible between the two
68 ;;; and so is not usable.
69
70 ;; This will be just "make-hash-table" in future.
71
72 (define (make-hash-table/comparator comparator)
73 (make-hash-table comparator))
74
75 ;; These two procedures adjust for the mismatch between the hash functions
76 ;; of SRFI 114, which return a potentially unbounded non-negative integer,
77 ;; and the hash functions of SRFI 69, which expect to be able to pass
78 ;; a second argument which is an upper bound.
79
80 (define (modulizer hash-function)
81 (case-lambda
82 ((obj) (hash-function obj))
83 ((obj limit) (modulo (hash-function obj) limit))))
84
85 ;;; Record definition and core typing/checking procedures
86
87 (define-record-type sob
88 (raw-make-sob hash-table comparator multi?)
89 sob?
90 (hash-table sob-hash-table)
91 (comparator sob-comparator)
92 (multi? sob-multi?))
93
94 (define (set? obj) (and (sob? obj) (not (sob-multi? obj))))
95
96 (define (bag? obj) (and (sob? obj) (sob-multi? obj)))
97
98 (define (check-set obj) (if (not (set? obj)) (error "not a set" obj)))
99
100 (define (check-bag obj) (if (not (bag? obj)) (error "not a bag" obj)))
101
102 ;; These procedures verify that not only are their arguments all sets
103 ;; or all bags as the case may be, but also share the same comparator.
104
105 (define (check-all-sets list)
106 (for-each (lambda (obj) (check-set obj)) list)
107 (sob-check-comparators list))
108
109 (define (check-all-bags list)
110 (for-each (lambda (obj) (check-bag obj)) list)
111 (sob-check-comparators list))
112
113 (define (sob-check-comparators list)
114 (if (not (null? list))
115 (for-each
116 (lambda (sob)
117 (check-same-comparator (car list) sob))
118 (cdr list))))
119
120 ;; This procedure is used directly when there are exactly two arguments.
121
122 (define (check-same-comparator a b)
123 (if (not (eq? (sob-comparator a) (sob-comparator b)))
124 (error "different comparators" a b)))
125
126 ;; This procedure defends against inserting an element
127 ;; into a sob that violates its constructor, since
128 ;; typical hash-table implementations don't check for us.
129
130 (define (check-element sob element)
131 (comparator-check-type (sob-comparator sob) element))
132
133 ;;; Constructors
134
135 ;; Construct an arbitrary empty sob out of nothing.
136
137 (define (make-sob comparator multi?)
138 (raw-make-sob (make-hash-table/comparator comparator) comparator multi?))
139
140 ;; Copy a sob, sharing the constructor.
141
142 (define (sob-copy sob)
143 (raw-make-sob (hash-table-copy (sob-hash-table sob) #t)
144 (sob-comparator sob)
145 (sob-multi? sob)))
146
147 (define (set-copy set)
148 (check-set set)
149 (sob-copy set))
150
151 (define (bag-copy bag)
152 (check-bag bag)
153 (sob-copy bag))
154
155 ;; Construct an empty sob that shares the constructor of an existing sob.
156
157 (define (sob-empty-copy sob)
158 (make-sob (sob-comparator sob) (sob-multi? sob)))
159
160 ;; Construct a set or a bag and insert elements into it. These are the
161 ;; simplest external constructors.
162
163 (define (set comparator . elements)
164 (let ((result (make-sob comparator #f)))
165 (for-each (lambda (x) (sob-increment! result x 1)) elements)
166 result))
167
168 (define (bag comparator . elements)
169 (let ((result (make-sob comparator #t)))
170 (for-each (lambda (x) (sob-increment! result x 1)) elements)
171 result))
172
173 ;; The fundamental (as opposed to simplest) constructor: unfold the
174 ;; results of iterating a function as a set. In line with SRFI 1,
175 ;; we provide an opportunity to map the sequence of seeds through a
176 ;; mapper function.
177
178 (define (sob-unfold stop? mapper successor seed comparator multi?)
179 (let ((result (make-sob comparator multi?)))
180 (let loop ((seed seed))
181 (if (stop? seed)
182 result
183 (begin
184 (sob-increment! result (mapper seed) 1)
185 (loop (successor seed)))))))
186
187 (define (set-unfold continue? mapper successor seed comparator)
188 (sob-unfold continue? mapper successor seed comparator #f))
189
190 (define (bag-unfold continue? mapper successor seed comparator)
191 (sob-unfold continue? mapper successor seed comparator #t))
192
193 ;;; Predicates
194
195 ;; Just a wrapper of hash-table-contains?.
196
197 (define (sob-contains? sob member)
198 (hash-table-contains? (sob-hash-table sob) member))
199
200 (define (set-contains? set member)
201 (check-set set)
202 (sob-contains? set member))
203
204 (define (bag-contains? bag member)
205 (check-bag bag)
206 (sob-contains? bag member))
207
208 ;; A sob is empty if its size is 0.
209
210 (define (sob-empty? sob)
211 (= 0 (hash-table-size (sob-hash-table sob))))
212
213 (define (set-empty? set)
214 (check-set set)
215 (sob-empty? set))
216
217 (define (bag-empty? bag)
218 (check-bag bag)
219 (sob-empty? bag))
220
221 ;; Two sobs are disjoint if, when looping through one, we can't find
222 ;; any of its elements in the other. We have to try both ways:
223 ;; sob-half-disjoint checks just one direction for simplicity.
224
225 (define (sob-half-disjoint? a b)
226 (let ((ha (sob-hash-table a))
227 (hb (sob-hash-table b)))
228 (call/cc
229 (lambda (return)
230 (hash-table-for-each
231 (lambda (key val) (if (hash-table-contains? hb key) (return #f)))
232 ha)
233 #t))))
234
235 (define (set-disjoint? a b)
236 (check-set a)
237 (check-set b)
238 (check-same-comparator a b)
239 (and (sob-half-disjoint? a b) (sob-half-disjoint? b a)))
240
241 (define (bag-disjoint? a b)
242 (check-bag a)
243 (check-bag b)
244 (check-same-comparator a b)
245 (and (sob-half-disjoint? a b) (sob-half-disjoint? b a)))
246
247 ;; Accessors
248
249 ;; If two objects are indistinguishable by the comparator's
250 ;; equality procedure, only one of them will be represented in the sob.
251 ;; This procedure lets us find out which one it is; it will return
252 ;; the value stored in the sob that is equal to the element.
253 ;; Note that we have to search the whole hash table item by item.
254 ;; The default is returned if there is no such element.
255
256 (define (sob-member sob element default)
257 (define (same? a b) (=? (sob-comparator sob) a b))
258 (call/cc
259 (lambda (return)
260 (hash-table-for-each
261 (lambda (key val) (if (same? key element) (return key)))
262 (sob-hash-table sob))
263 default)))
264
265 (define (set-member set element default)
266 (check-set set)
267 (sob-member set element default))
268
269 (define (bag-member bag element default)
270 (check-bag bag)
271 (sob-member bag element default))
272
273 ;; Retrieve the comparator.
274
275 (define (set-element-comparator set)
276 (check-set set)
277 (sob-comparator set))
278
279 (define (bag-element-comparator bag)
280 (check-bag bag)
281 (sob-comparator bag))
282
283
284 ;; Updaters (pure functional and linear update)
285
286 ;; The primitive operation for adding an element to a sob.
287 ;; There are a few cases where we bypass this for efficiency.
288
289 (define (sob-increment! sob element count)
290 (check-element sob element)
291 (hash-table-update!/default
292 (sob-hash-table sob)
293 element
294 (if (sob-multi? sob)
295 (lambda (value) (+ value count))
296 (lambda (value) 1))
297 0))
298
299 ;; The primitive operation for removing an element from a sob. Note this
300 ;; procedure is incomplete: it allows the count of an element to drop below 1.
301 ;; Therefore, whenever it is used it is necessary to call sob-cleanup!
302 ;; to fix things up. This is done because it is unsafe to remove an
303 ;; object from a hash table while iterating through it.
304
305 (define (sob-decrement! sob element count)
306 (hash-table-update!/default
307 (sob-hash-table sob)
308 element
309 (lambda (value) (- value count))
310 0))
311
312 ;; This is the cleanup procedure, which happens in two passes: it
313 ;; iterates through the sob, deciding which elements to remove (those
314 ;; with non-positive counts), and collecting them in a list. When the
315 ;; iteration is done, it is safe to remove the elements using the list,
316 ;; because we are no longer iterating over the hash table. It returns
317 ;; its argument, because it is often tail-called at the end of some
318 ;; procedure that wants to return the clean sob.
319
320 (define (sob-cleanup! sob)
321 (let ((ht (sob-hash-table sob)))
322 (for-each (lambda (key) (hash-table-delete! ht key))
323 (nonpositive-keys ht))
324 sob))
325
326 (define (nonpositive-keys ht)
327 (let ((result '()))
328 (hash-table-for-each
329 (lambda (key value)
330 (when (<= value 0)
331 (set! result (cons key result))))
332 ht)
333 result))
334
335 ;; We expose these for bags but not sets.
336
337 (define (bag-increment! bag element count)
338 (check-bag bag)
339 (sob-increment! bag element count)
340 bag)
341
342 (define (bag-decrement! bag element count)
343 (check-bag bag)
344 (sob-decrement! bag element count)
345 (sob-cleanup! bag)
346 bag)
347
348 ;; The primitive operation to add elements from a list. We expose
349 ;; this two ways: with a list argument and with multiple arguments.
350
351 (define (sob-adjoin-all! sob elements)
352 (for-each
353 (lambda (elem)
354 (sob-increment! sob elem 1))
355 elements))
356
357 (define (set-adjoin! set . elements)
358 (check-set set)
359 (sob-adjoin-all! set elements)
360 set)
361
362 (define (bag-adjoin! bag . elements)
363 (check-bag bag)
364 (sob-adjoin-all! bag elements)
365 bag)
366
367
368 ;; These versions copy the set or bag before adjoining.
369
370 (define (set-adjoin set . elements)
371 (check-set set)
372 (let ((result (sob-copy set)))
373 (sob-adjoin-all! result elements)
374 result))
375
376 (define (bag-adjoin bag . elements)
377 (check-bag bag)
378 (let ((result (sob-copy bag)))
379 (sob-adjoin-all! result elements)
380 result))
381
382 ;; Given an element which resides in a set, this makes sure that the
383 ;; specified element is represented by the form given. Thus if a
384 ;; sob contains 2 and the equality predicate is =, then calling
385 ;; (sob-replace! sob 2.0) will replace the 2 with 2.0. Does nothing
386 ;; if there is no such element in the sob.
387
388 (define (sob-replace! sob element)
389 (let* ((comparator (sob-comparator sob))
390 (= (comparator-equality-predicate comparator))
391 (ht (sob-hash-table sob)))
392 (comparator-check-type comparator element)
393 (call/cc
394 (lambda (return)
395 (hash-table-for-each
396 (lambda (key value)
397 (when (= key element)
398 (hash-table-delete! ht key)
399 (hash-table-set! ht element value)
400 (return sob)))
401 ht)
402 sob))))
403
404 (define (set-replace! set element)
405 (check-set set)
406 (sob-replace! set element)
407 set)
408
409 (define (bag-replace! bag element)
410 (check-bag bag)
411 (sob-replace! bag element)
412 bag)
413
414 ;; Non-destructive versions that copy the set first. Yes, a little
415 ;; bit inefficient because it copies the element to be replaced before
416 ;; actually replacing it.
417
418 (define (set-replace set element)
419 (check-set set)
420 (let ((result (sob-copy set)))
421 (sob-replace! result element)
422 result))
423
424 (define (bag-replace bag element)
425 (check-bag bag)
426 (let ((result (sob-copy bag)))
427 (sob-replace! result element)
428 result))
429
430 ;; The primitive operation to delete elemnets from a list.
431 ;; Like sob-adjoin-all!, this is exposed two ways. It calls
432 ;; sob-cleanup! itself, so its callers don't need to (though it is safe
433 ;; to do so.)
434
435 (define (sob-delete-all! sob elements)
436 (for-each (lambda (element) (sob-decrement! sob element 1)) elements)
437 (sob-cleanup! sob)
438 sob)
439
440 (define (set-delete! set . elements)
441 (check-set set)
442 (sob-delete-all! set elements))
443
444 (define (bag-delete! bag . elements)
445 (check-bag bag)
446 (sob-delete-all! bag elements))
447
448 (define (set-delete-all! set elements)
449 (check-set set)
450 (sob-delete-all! set elements))
451
452 (define (bag-delete-all! bag elements)
453 (check-bag bag)
454 (sob-delete-all! bag elements))
455
456 ;; Non-destructive version copy first; this is inefficient.
457
458 (define (set-delete set . elements)
459 (check-set set)
460 (sob-delete-all! (sob-copy set) elements))
461
462 (define (bag-delete bag . elements)
463 (check-bag bag)
464 (sob-delete-all! (sob-copy bag) elements))
465
466 (define (set-delete-all set elements)
467 (check-set set)
468 (sob-delete-all! (sob-copy set) elements))
469
470 (define (bag-delete-all bag elements)
471 (check-bag bag)
472 (sob-delete-all! (sob-copy bag) elements))
473
474 ;; Flag used by sob-search! to represent a missing object.
475
476 (define missing (string-copy "missing"))
477
478 ;; Searches and then dispatches to user-defined procedures on failure
479 ;; and success, which in turn should reinvoke a procedure to take some
480 ;; action on the set (insert, ignore, replace, or remove).
481
482 (define (sob-search! sob element failure success)
483 (define (insert obj)
484 (sob-increment! sob element 1)
485 (values sob obj))
486 (define (ignore obj)
487 (values sob obj))
488 (define (update new-elem obj)
489 (sob-decrement! sob element 1)
490 (sob-increment! sob new-elem 1)
491 (values (sob-cleanup! sob) obj))
492 (define (remove obj)
493 (sob-decrement! sob element 1)
494 (values (sob-cleanup! sob) obj))
495 (let ((true-element (sob-member sob element missing)))
496 (if (eq? true-element missing)
497 (failure insert ignore)
498 (success true-element update remove))))
499
500 (define (set-search! set element failure success)
501 (check-set set)
502 (sob-search! set element failure success))
503
504 (define (bag-search! bag element failure success)
505 (check-bag bag)
506 (sob-search! bag element failure success))
507
508 ;; Return the size of a sob. If it's a set, we can just use the
509 ;; number of associations in the hash table, but if it's a bag, we
510 ;; have to add up the counts.
511
512 (define (sob-size sob)
513 (if (sob-multi? sob)
514 (let ((result 0))
515 (hash-table-for-each
516 (lambda (elem count) (set! result (+ count result)))
517 (sob-hash-table sob))
518 result)
519 (hash-table-size (sob-hash-table sob))))
520
521 (define (set-size set)
522 (check-set set)
523 (sob-size set))
524
525 (define (bag-size bag)
526 (check-bag bag)
527 (sob-size bag))
528
529 ;; Search a sob to find something that matches a predicate. You don't
530 ;; know which element you will get, so this is not as useful as finding
531 ;; an element in a list or other ordered container. If it's not there,
532 ;; call the failure thunk.
533
534 (define (sob-find pred sob failure)
535 (call/cc
536 (lambda (return)
537 (hash-table-for-each
538 (lambda (key value)
539 (if (pred key) (return key)))
540 (sob-hash-table sob))
541 (failure))))
542
543 (define (set-find pred set failure)
544 (check-set set)
545 (sob-find pred set failure))
546
547 (define (bag-find pred bag failure)
548 (check-bag bag)
549 (sob-find pred bag failure))
550
551 ;; Count the number of elements in the sob that satisfy the predicate.
552 ;; This is a special case of folding.
553
554 (define (sob-count pred sob)
555 (sob-fold
556 (lambda (elem total) (if (pred elem) (+ total 1) total))
557 0
558 sob))
559
560 (define (set-count pred set)
561 (check-set set)
562 (sob-count pred set))
563
564 (define (bag-count pred bag)
565 (check-bag bag)
566 (sob-count pred bag))
567
568 ;; Check if any of the elements in a sob satisfy a predicate. Breaks out
569 ;; early (with call/cc) if a success is found.
570
571 (define (sob-any? pred sob)
572 (call/cc
573 (lambda (return)
574 (hash-table-for-each
575 (lambda (elem value) (if (pred elem) (return #t)))
576 (sob-hash-table sob))
577 #f)))
578
579 (define (set-any? pred set)
580 (check-set set)
581 (sob-any? pred set))
582
583 (define (bag-any? pred bag)
584 (check-bag bag)
585 (sob-any? pred bag))
586
587 ;; Analogous to set-any?. Breaks out early if a failure is found.
588
589 (define (sob-every? pred sob)
590 (call/cc
591 (lambda (return)
592 (hash-table-for-each
593 (lambda (elem value) (if (not (pred elem)) (return #f)))
594 (sob-hash-table sob))
595 #t)))
596
597 (define (set-every? pred set)
598 (check-set set)
599 (sob-every? pred set))
600
601 (define (bag-every? pred bag)
602 (check-bag bag)
603 (sob-every? pred bag))
604
605
606 ;;; Mapping and folding
607
608 ;; A utility for iterating a command n times. This is used by sob-for-each
609 ;; to execute a procedure over the repeated elements in a bag. Because
610 ;; of the representation of sets, it works for them too.
611
612 (define (do-n-times cmd n)
613 (let loop ((n n))
614 (when (> n 0)
615 (cmd)
616 (loop (- n 1)))))
617
618 ;; Basic iterator over a sob.
619
620 (define (sob-for-each proc sob)
621 (hash-table-for-each
622 (lambda (key value) (do-n-times (lambda () (proc key)) value))
623 (sob-hash-table sob)))
624
625 (define (set-for-each proc set)
626 (check-set set)
627 (sob-for-each proc set))
628
629 (define (bag-for-each proc bag)
630 (check-bag bag)
631 (sob-for-each proc bag))
632
633 ;; Fundamental mapping operator. We map over the associations directly,
634 ;; because each instance of an element in a bag will be treated identically
635 ;; anyway; we insert them all at once with sob-increment!.
636
637 (define (sob-map comparator proc sob)
638 (let ((result (make-sob comparator (sob-multi? sob))))
639 (hash-table-for-each
640 (lambda (key value) (sob-increment! result (proc key) value))
641 (sob-hash-table sob))
642 result))
643
644 (define (set-map comparator proc set)
645 (check-set set)
646 (sob-map comparator proc set))
647
648 (define (bag-map comparator proc bag)
649 (check-bag bag)
650 (sob-map comparator proc bag))
651
652 ;; The fundamental deconstructor. Note that there are no left vs. right
653 ;; folds because there is no order. Each element in a bag is fed into
654 ;; the fold separately.
655
656 (define (sob-fold proc nil sob)
657 (let ((result nil))
658 (sob-for-each
659 (lambda (elem) (set! result (proc elem result)))
660 sob)
661 result))
662
663 (define (set-fold proc nil set)
664 (check-set set)
665 (sob-fold proc nil set))
666
667 (define (bag-fold proc nil bag)
668 (check-bag bag)
669 (sob-fold proc nil bag))
670
671 ;; Process every element and copy the ones that satisfy the predicate.
672 ;; Identical elements are processed all at once. This is used for both
673 ;; filter and remove.
674
675 (define (sob-filter pred sob)
676 (let ((result (sob-empty-copy sob)))
677 (hash-table-for-each
678 (lambda (key value)
679 (if (pred key) (sob-increment! result key value)))
680 (sob-hash-table sob))
681 result))
682
683 (define (set-filter pred set)
684 (check-set set)
685 (sob-filter pred set))
686
687 (define (bag-filter pred bag)
688 (check-bag bag)
689 (sob-filter pred bag))
690
691 (define (set-remove pred set)
692 (check-set set)
693 (sob-filter (lambda (x) (not (pred x))) set))
694
695 (define (bag-remove pred bag)
696 (check-bag bag)
697 (sob-filter (lambda (x) (not (pred x))) bag))
698
699 ;; Process each element and remove those that don't satisfy the filter.
700 ;; This does its own cleanup, and is used for both filter! and remove!.
701
702 (define (sob-filter! pred sob)
703 (hash-table-for-each
704 (lambda (key value)
705 (if (not (pred key)) (sob-decrement! sob key value)))
706 (sob-hash-table sob))
707 (sob-cleanup! sob))
708
709 (define (set-filter! pred set)
710 (check-set set)
711 (sob-filter! pred set))
712
713 (define (bag-filter! pred bag)
714 (check-bag bag)
715 (sob-filter! pred bag))
716
717 (define (set-remove! pred set)
718 (check-set set)
719 (sob-filter! (lambda (x) (not (pred x))) set))
720
721 (define (bag-remove! pred bag)
722 (check-bag bag)
723 (sob-filter! (lambda (x) (not (pred x))) bag))
724
725 ;; Create two sobs and copy the elements that satisfy the predicate into
726 ;; one of them, all others into the other. This is more efficient than
727 ;; filtering and removing separately.
728
729 (define (sob-partition pred sob)
730 (let ((res1 (sob-empty-copy sob))
731 (res2 (sob-empty-copy sob)))
732 (hash-table-for-each
733 (lambda (key value)
734 (if (pred key)
735 (sob-increment! res1 key value)
736 (sob-increment! res2 key value)))
737 (sob-hash-table sob))
738 (values res1 res2)))
739
740 (define (set-partition pred set)
741 (check-set set)
742 (sob-partition pred set))
743
744 (define (bag-partition pred bag)
745 (check-bag bag)
746 (sob-partition pred bag))
747
748 ;; Create a sob and iterate through the given sob. Anything that satisfies
749 ;; the predicate is left alone; anything that doesn't is removed from the
750 ;; given sob and added to the new sob.
751
752 (define (sob-partition! pred sob)
753 (let ((result (sob-empty-copy sob)))
754 (hash-table-for-each
755 (lambda (key value)
756 (if (not (pred key))
757 (begin
758 (sob-decrement! sob key value)
759 (sob-increment! result key value))))
760 (sob-hash-table sob))
761 (values (sob-cleanup! sob) result)))
762
763 (define (set-partition! pred set)
764 (check-set set)
765 (sob-partition! pred set))
766
767 (define (bag-partition! pred bag)
768 (check-bag bag)
769 (sob-partition! pred bag))
770
771
772 ;;; Copying and conversion
773
774 ;;; Convert a sob to a list; a special case of sob-fold.
775
776 (define (sob->list sob)
777 (sob-fold (lambda (elem list) (cons elem list)) '() sob))
778
779 (define (set->list set)
780 (check-set set)
781 (sob->list set))
782
783 (define (bag->list bag)
784 (check-bag bag)
785 (sob->list bag))
786
787 ;; Convert a list to a sob. Probably could be done using unfold, but
788 ;; since sobs are mutable anyway, it's just as easy to add the elements
789 ;; by side effect.
790
791 (define (list->sob! sob list)
792 (for-each (lambda (elem) (sob-increment! sob elem 1)) list)
793 sob)
794
795 (define (list->set comparator list)
796 (list->sob! (make-sob comparator #f) list))
797
798 (define (list->bag comparator list)
799 (list->sob! (make-sob comparator #t) list))
800
801 (define (list->set! set list)
802 (check-set set)
803 (list->sob! set list))
804
805 (define (list->bag! bag list)
806 (check-bag bag)
807 (list->sob! bag list))
808
809
810 ;;; Subsets
811
812 ;; All of these procedures follow the same pattern. The
813 ;; sob<op>? procedures are case-lambdas that reduce the multi-argument
814 ;; case to the two-argument case. As usual, the set<op>? and
815 ;; bag<op>? procedures are trivial layers over the sob<op>? procedure.
816 ;; The dyadic-sob<op>? procedures are where it gets interesting, so see
817 ;; the comments on them.
818
819 (define sob=?
820 (case-lambda
821 ((sob) #t)
822 ((sob1 sob2) (dyadic-sob=? sob1 sob2))
823 ((sob1 sob2 . sobs)
824 (and (dyadic-sob=? sob1 sob2)
825 (apply sob=? sob2 sobs)))))
826
827 (define (set=? . sets)
828 (check-all-sets sets)
829 (apply sob=? sets))
830
831 (define (bag=? . bags)
832 (check-all-bags bags)
833 (apply sob=? bags))
834
835 ;; First we check that there are the same number of entries in the
836 ;; hashtables of the two sobs; if that's not true, they can't be equal.
837 ;; Then we check that for each key, the values are the same (where
838 ;; being absent counts as a value of 0). If any values aren't equal,
839 ;; again they can't be equal.
840
841 (define (dyadic-sob=? sob1 sob2)
842 (call/cc
843 (lambda (return)
844 (let ((ht1 (sob-hash-table sob1))
845 (ht2 (sob-hash-table sob2)))
846 (if (not (= (hash-table-size ht1) (hash-table-size ht2)))
847 (return #f))
848 (hash-table-for-each
849 (lambda (key value)
850 (if (not (= value (hash-table-ref/default ht2 key 0)))
851 (return #f)))
852 ht1))
853 #t)))
854
855 (define sob<=?
856 (case-lambda
857 ((sob) #t)
858 ((sob1 sob2) (dyadic-sob<=? sob1 sob2))
859 ((sob1 sob2 . sobs)
860 (and (dyadic-sob<=? sob1 sob2)
861 (apply sob<=? sob2 sobs)))))
862
863 (define (set<=? . sets)
864 (check-all-sets sets)
865 (apply sob<=? sets))
866
867 (define (bag<=? . bags)
868 (check-all-bags bags)
869 (apply sob<=? bags))
870
871 ;; This is analogous to dyadic-sob=?, except that we have to check
872 ;; both sobs to make sure each value is <= in order to be sure
873 ;; that we've traversed all the elements in either sob.
874
875 (define (dyadic-sob<=? sob1 sob2)
876 (call/cc
877 (lambda (return)
878 (let ((ht1 (sob-hash-table sob1))
879 (ht2 (sob-hash-table sob2)))
880 (if (not (<= (hash-table-size ht1) (hash-table-size ht2)))
881 (return #f))
882 (hash-table-for-each
883 (lambda (key value)
884 (if (not (<= value (hash-table-ref/default ht2 key 0)))
885 (return #f)))
886 ht1))
887 #t)))
888
889 (define sob<?
890 (case-lambda
891 ((sob) #t)
892 ((sob1 sob2) (dyadic-sob<? sob1 sob2))
893 ((sob1 sob2 . sobs)
894 (and (dyadic-sob<? sob1 sob2)
895 (apply sob<? sob2 sobs)))))
896
897 (define (set<? . sets)
898 (check-all-sets sets)
899 (apply sob<? sets))
900
901 (define (bag<? . bags)
902 (check-all-bags bags)
903 (apply sob<? bags))
904
905 ;; Strict subset test is a bit more involved. At least one entry in ht1
906 ;; needs to have smaller value than the entry in ht2.
907 (define (dyadic-sob<? sob1 sob2)
908 (call/cc
909 (lambda (return)
910 (let ((ht1 (sob-hash-table sob1))
911 (ht2 (sob-hash-table sob2)))
912 (let ((smaller-count
913 (cond
914 ((< (hash-table-size ht1) (hash-table-size ht2)) 1)
915 ((= (hash-table-size ht1) (hash-table-size ht2)) 0)
916 (else (return #f)))))
917 (hash-table-for-each
918 (lambda (key value)
919 (let ((value2 (hash-table-ref/default ht2 key 0)))
920 (if (not (<= value value2))
921 (return #f)
922 (if (< value value2)
923 (set! smaller-count (+ smaller-count 1))))))
924 ht1)
925 (positive? smaller-count))))))
926
927 (define sob>?
928 (case-lambda
929 ((sob) #t)
930 ((sob1 sob2) (dyadic-sob>? sob1 sob2))
931 ((sob1 sob2 . sobs)
932 (and (dyadic-sob>? sob1 sob2)
933 (apply sob>? sob2 sobs)))))
934
935 (define (set>? . sets)
936 (check-all-sets sets)
937 (apply sob>? sets))
938
939 (define (bag>? . bags)
940 (check-all-bags bags)
941 (apply sob>? bags))
942
943 ;; > is the inverse of <. Again, this is only true dyadically.
944
945 (define (dyadic-sob>? sob1 sob2)
946 (dyadic-sob<? sob2 sob1))
947
948 (define sob>=?
949 (case-lambda
950 ((sob) #t)
951 ((sob1 sob2) (dyadic-sob>=? sob1 sob2))
952 ((sob1 sob2 . sobs)
953 (and (dyadic-sob>=? sob1 sob2)
954 (apply sob>=? sob2 sobs)))))
955
956 (define (set>=? . sets)
957 (check-all-sets sets)
958 (apply sob>=? sets))
959
960 (define (bag>=? . bags)
961 (check-all-bags bags)
962 (apply sob>=? bags))
963
964 ;; <= is the inverse of >=. Again, this is only true dyadically.
965
966 (define (dyadic-sob>=? sob1 sob2)
967 (dyadic-sob<=? sob2 sob1))
968
969
970 ;;; Set theory operations
971
972 ;; A trivial helper function which upper-bounds n by one if multi? is false.
973
974 (define (max-one n multi?)
975 (if multi? n (if (> n 1) 1 n)))
976
977 ;; The logic of union, intersection, difference, and sum is the same: the
978 ;; sob-* and sob-*! procedures do the reduction to the dyadic-sob-*!
979 ;; procedures. The difference is that the sob-* procedures allocate
980 ;; an empty copy of the first sob to accumulate the results in, whereas
981 ;; the sob-*! procedures work directly in the first sob.
982
983 ;; Note that there is no set-sum, as it is the same as set-union.
984
985 (define (sob-union sob1 . sobs)
986 (if (null? sobs)
987 sob1
988 (let ((result (sob-empty-copy sob1)))
989 (dyadic-sob-union! result sob1 (car sobs))
990 (for-each
991 (lambda (sob) (dyadic-sob-union! result result sob))
992 (cdr sobs))
993 result)))
994
995 ;; For union, we take the max of the counts of each element found
996 ;; in either sob and put that in the result. On the pass through
997 ;; sob2, we know that the intersection is already accounted for,
998 ;; so we just copy over things that aren't in sob1.
999
1000 (define (dyadic-sob-union! result sob1 sob2)
1001 (let ((sob1-ht (sob-hash-table sob1))
1002 (sob2-ht (sob-hash-table sob2))
1003 (result-ht (sob-hash-table result)))
1004 (hash-table-for-each
1005 (lambda (key value1)
1006 (let ((value2 (hash-table-ref/default sob2-ht key 0)))
1007 (hash-table-set! result-ht key (max value1 value2))))
1008 sob1-ht)
1009 (hash-table-for-each
1010 (lambda (key value2)
1011 (let ((value1 (hash-table-ref/default sob1-ht key 0)))
1012 (if (= value1 0)
1013 (hash-table-set! result-ht key value2))))
1014 sob2-ht)))
1015
1016 (define (set-union . sets)
1017 (check-all-sets sets)
1018 (apply sob-union sets))
1019
1020 (define (bag-union . bags)
1021 (check-all-bags bags)
1022 (apply sob-union bags))
1023
1024 (define (sob-union! sob1 . sobs)
1025 (for-each
1026 (lambda (sob) (dyadic-sob-union! sob1 sob1 sob))
1027 sobs)
1028 sob1)
1029
1030 (define (set-union! . sets)
1031 (check-all-sets sets)
1032 (apply sob-union! sets))
1033
1034 (define (bag-union! . bags)
1035 (check-all-bags bags)
1036 (apply sob-union! bags))
1037
1038 (define (sob-intersection sob1 . sobs)
1039 (if (null? sobs)
1040 sob1
1041 (let ((result (sob-empty-copy sob1)))
1042 (dyadic-sob-intersection! result sob1 (car sobs))
1043 (for-each
1044 (lambda (sob) (dyadic-sob-intersection! result result sob))
1045 (cdr sobs))
1046 (sob-cleanup! result))))
1047
1048 ;; For intersection, we compute the min of the counts of each element.
1049 ;; We only have to scan sob1. We clean up the result when we are
1050 ;; done, in case it is the same as sob1.
1051
1052 (define (dyadic-sob-intersection! result sob1 sob2)
1053 (let ((sob1-ht (sob-hash-table sob1))
1054 (sob2-ht (sob-hash-table sob2))
1055 (result-ht (sob-hash-table result)))
1056 (hash-table-for-each
1057 (lambda (key value1)
1058 (let ((value2 (hash-table-ref/default sob2-ht key 0)))
1059 (hash-table-set! result-ht key (min value1 value2))))
1060 sob1-ht)))
1061
1062 (define (set-intersection . sets)
1063 (check-all-sets sets)
1064 (apply sob-intersection sets))
1065
1066 (define (bag-intersection . bags)
1067 (check-all-bags bags)
1068 (apply sob-intersection bags))
1069
1070 (define (sob-intersection! sob1 . sobs)
1071 (for-each
1072 (lambda (sob) (dyadic-sob-intersection! sob1 sob1 sob))
1073 sobs)
1074 (sob-cleanup! sob1))
1075
1076 (define (set-intersection! . sets)
1077 (check-all-sets sets)
1078 (apply sob-intersection! sets))
1079
1080 (define (bag-intersection! . bags)
1081 (check-all-bags bags)
1082 (apply sob-intersection! bags))
1083
1084 (define (sob-difference sob1 . sobs)
1085 (if (null? sobs)
1086 sob1
1087 (let ((result (sob-empty-copy sob1)))
1088 (dyadic-sob-difference! result sob1 (car sobs))
1089 (for-each
1090 (lambda (sob) (dyadic-sob-difference! result result sob))
1091 (cdr sobs))
1092 (sob-cleanup! result))))
1093
1094 ;; For difference, we use (big surprise) the numeric difference, bounded
1095 ;; by zero. We only need to scan sob1, but we clean up the result in
1096 ;; case it is the same as sob1.
1097
1098 (define (dyadic-sob-difference! result sob1 sob2)
1099 (let ((sob1-ht (sob-hash-table sob1))
1100 (sob2-ht (sob-hash-table sob2))
1101 (result-ht (sob-hash-table result)))
1102 (hash-table-for-each
1103 (lambda (key value1)
1104 (let ((value2 (hash-table-ref/default sob2-ht key 0)))
1105 (hash-table-set! result-ht key (- value1 value2))))
1106 sob1-ht)))
1107
1108 (define (set-difference . sets)
1109 (check-all-sets sets)
1110 (apply sob-difference sets))
1111
1112 (define (bag-difference . bags)
1113 (check-all-bags bags)
1114 (apply sob-difference bags))
1115
1116 (define (sob-difference! sob1 . sobs)
1117 (for-each
1118 (lambda (sob) (dyadic-sob-difference! sob1 sob1 sob))
1119 sobs)
1120 (sob-cleanup! sob1))
1121
1122 (define (set-difference! . sets)
1123 (check-all-sets sets)
1124 (apply sob-difference! sets))
1125
1126 (define (bag-difference! . bags)
1127 (check-all-bags bags)
1128 (apply sob-difference! bags))
1129
1130 (define (sob-sum sob1 . sobs)
1131 (if (null? sobs)
1132 sob1
1133 (let ((result (sob-empty-copy sob1)))
1134 (dyadic-sob-sum! result sob1 (car sobs))
1135 (for-each
1136 (lambda (sob) (dyadic-sob-sum! result result sob))
1137 (cdr sobs))
1138 result)))
1139
1140 ;; Sum is just like union, except that we take the sum rather than the max.
1141
1142 (define (dyadic-sob-sum! result sob1 sob2)
1143 (let ((sob1-ht (sob-hash-table sob1))
1144 (sob2-ht (sob-hash-table sob2))
1145 (result-ht (sob-hash-table result)))
1146 (hash-table-for-each
1147 (lambda (key value1)
1148 (let ((value2 (hash-table-ref/default sob2-ht key 0)))
1149 (hash-table-set! result-ht key (+ value1 value2))))
1150 sob1-ht)
1151 (hash-table-for-each
1152 (lambda (key value2)
1153 (let ((value1 (hash-table-ref/default sob1-ht key 0)))
1154 (if (= value1 0)
1155 (hash-table-set! result-ht key value2))))
1156 sob2-ht)))
1157
1158
1159 ;; Sum is defined for bags only; for sets, it is the same as union.
1160
1161 (define (bag-sum . bags)
1162 (check-all-bags bags)
1163 (apply sob-sum bags))
1164
1165 (define (sob-sum! sob1 . sobs)
1166 (for-each
1167 (lambda (sob) (dyadic-sob-sum! sob1 sob1 sob))
1168 sobs)
1169 sob1)
1170
1171 (define (bag-sum! . bags)
1172 (check-all-bags bags)
1173 (apply sob-sum! bags))
1174
1175 ;; For xor exactly two arguments are required, so the above structures are
1176 ;; not necessary. This version accepts a result sob and computes the
1177 ;; absolute difference between the counts in the first sob and the
1178 ;; corresponding counts in the second.
1179
1180 ;; We start by copying the entries in the second sob but not the first
1181 ;; into the first. Then we scan the first sob, computing the absolute
1182 ;; difference of the values and writing them back into the first sob.
1183 ;; It's essential to scan the second sob first, as we are not going to
1184 ;; damage it in the process. (Hat tip: Sam Tobin-Hochstadt.)
1185
1186 (define (sob-xor! result sob1 sob2)
1187 (let ((sob1-ht (sob-hash-table sob1))
1188 (sob2-ht (sob-hash-table sob2))
1189 (result-ht (sob-hash-table result)))
1190 (hash-table-for-each
1191 (lambda (key value2)
1192 (let ((value1 (hash-table-ref/default sob1-ht key 0)))
1193 (if (= value1 0)
1194 (hash-table-set! result-ht key value2))))
1195 sob2-ht)
1196 (hash-table-for-each
1197 (lambda (key value1)
1198 (let ((value2 (hash-table-ref/default sob2-ht key 0)))
1199 (hash-table-set! result-ht key (abs (- value1 value2)))))
1200 sob1-ht)
1201 (sob-cleanup! result)))
1202
1203 (define (set-xor set1 set2)
1204 (check-set set1)
1205 (check-set set2)
1206 (check-same-comparator set1 set2)
1207 (sob-xor! (sob-empty-copy set1) set1 set2))
1208
1209 (define (bag-xor bag1 bag2)
1210 (check-bag bag1)
1211 (check-bag bag2)
1212 (check-same-comparator bag1 bag2)
1213 (sob-xor! (sob-empty-copy bag1) bag1 bag2))
1214
1215 (define (set-xor! set1 set2)
1216 (check-set set1)
1217 (check-set set2)
1218 (check-same-comparator set1 set2)
1219 (sob-xor! set1 set1 set2))
1220
1221 (define (bag-xor! bag1 bag2)
1222 (check-bag bag1)
1223 (check-bag bag2)
1224 (check-same-comparator bag1 bag2)
1225 (sob-xor! bag1 bag1 bag2))
1226
1227
1228 ;;; A few bag-specific procedures
1229
1230 (define (sob-product! n result sob)
1231 (let ((rht (sob-hash-table result)))
1232 (hash-table-for-each
1233 (lambda (elem count) (hash-table-set! rht elem (* count n)))
1234 (sob-hash-table sob))
1235 result))
1236
1237 (define (valid-n n)
1238 (and (integer? n) (exact? n) (positive? n)))
1239
1240 (define (bag-product n bag)
1241 (check-bag bag)
1242 (valid-n n)
1243 (sob-product! n (sob-empty-copy bag) bag))
1244
1245 (define (bag-product! n bag)
1246 (check-bag bag)
1247 (valid-n n)
1248 (sob-product! n bag bag))
1249
1250 (define (bag-unique-size bag)
1251 (check-bag bag)
1252 (hash-table-size (sob-hash-table bag)))
1253
1254 (define (bag-element-count bag elem)
1255 (check-bag bag)
1256 (hash-table-ref/default (sob-hash-table bag) elem 0))
1257
1258 (define (bag-for-each-unique proc bag)
1259 (check-bag bag)
1260 (hash-table-for-each
1261 (lambda (key value) (proc key value))
1262 (sob-hash-table bag)))
1263
1264 (define (bag-fold-unique proc nil bag)
1265 (check-bag bag)
1266 (let ((result nil))
1267 (hash-table-for-each
1268 (lambda (elem count) (set! result (proc elem count result)))
1269 (sob-hash-table bag))
1270 result))
1271
1272 (define (bag->set bag)
1273 (check-bag bag)
1274 (let ((result (make-sob (sob-comparator bag) #f)))
1275 (hash-table-for-each
1276 (lambda (key value) (sob-increment! result key value))
1277 (sob-hash-table bag))
1278 result))
1279
1280 (define (set->bag set)
1281 (check-set set)
1282 (let ((result (make-sob (sob-comparator set) #t)))
1283 (hash-table-for-each
1284 (lambda (key value) (sob-increment! result key value))
1285 (sob-hash-table set))
1286 result))
1287
1288 (define (set->bag! bag set)
1289 (check-bag bag)
1290 (check-set set)
1291 (check-same-comparator set bag)
1292 (hash-table-for-each
1293 (lambda (key value) (sob-increment! bag key value))
1294 (sob-hash-table set))
1295 bag)
1296
1297 (define (bag->alist bag)
1298 (check-bag bag)
1299 (bag-fold-unique
1300 (lambda (elem count list) (cons (cons elem count) list))
1301 '()
1302 bag))
1303
1304 (define (alist->bag comparator alist)
1305 (let* ((result (bag comparator))
1306 (ht (sob-hash-table result)))
1307 (for-each
1308 (lambda (assoc)
1309 (let ((element (car assoc)))
1310 (if (not (hash-table-contains? ht element))
1311 (sob-increment! result element (cdr assoc)))))
1312 alist)
1313 result))
1314
1315 ;;; Comparators
1316
1317 ;; Hash over sobs
1318 (define (sob-hash sob)
1319 (let* ((ht (sob-hash-table sob))
1320 (hash (comparator-hash-function (sob-comparator sob))))
1321 (sob-fold
1322 (lambda (element result) (+ (hash element) result))
1323 5381
1324 sob)))
1325
1326 ;; Set and bag comparator
1327
1328 (define set-comparator (make-comparator set? set=? #f sob-hash))
1329
1330 (define bag-comparator (make-comparator bag? bag=? #f sob-hash))
1331
1332
1333 ;;; Set/bag printer (for debugging)
1334
1335 (define (sob-print sob port)
1336 (display (if (sob-multi? sob) "&bag[" "&set[") port)
1337 (sob-for-each
1338 (lambda (elem) (display " " port) (write elem port))
1339 sob)
1340 (display " ]" port))
1341
1342 ;;; Register above comparators for use by default-comparator
1343 (comparator-register-default! set-comparator)
1344 (comparator-register-default! bag-comparator)
1345
1346 ))