add set-subset?

This commit is contained in:
Matthew Flatt 2010-04-23 17:00:51 -06:00
parent 6272d0511a
commit d824fef4f7
2 changed files with 24 additions and 0 deletions

View File

@ -6,6 +6,7 @@
set-empty? set-count set-empty? set-count
set-member? set-add set-remove set-member? set-add set-remove
set-union set-intersect set-subtract set-union set-intersect set-subtract
set-subset?
set-map set-for-each set-map set-for-each
(rename-out [*in-set in-set]) (rename-out [*in-set in-set])
for/set for/seteq for/seteqv for/set for/seteq for/seteqv
@ -163,6 +164,18 @@
(for/fold ([set set]) ([set2 (in-list sets)]) (for/fold ([set set]) ([set2 (in-list sets)])
(set-subtract set set2))])) (set-subtract set set2))]))
(define (set-subset? set1 set2)
(unless (set? set1) (raise-type-error 'set-subset? "set" 0 set1 set2))
(unless (set? set2) (raise-type-error 'set-subset? "set" 1 set1 set2))
(let ([ht1 (set-ht set1)]
[ht2 (set-ht set2)])
(unless (and (eq? (hash-eq? ht1) (hash-eq? ht2))
(eq? (hash-eqv? ht1) (hash-eqv? ht2)))
(raise-mismatch-error 'set-subset? "second set's equivalence predicate is not the same as the first set: "
set2))
(for/and ([v (in-hash-keys ht2)])
(hash-ref ht1 v #f))))
(define (set-map set proc) (define (set-map set proc)
(unless (set? set) (raise-type-error 'set-map "set" 0 set proc)) (unless (set? set) (raise-type-error 'set-map "set" 0 set proc))
(unless (and (procedure? proc) (unless (and (procedure? proc)

View File

@ -52,11 +52,13 @@ to a later element takes precedence over the later element.}
Returns @scheme[#t] if @scheme[set] has no members, @scheme[#f] Returns @scheme[#t] if @scheme[set] has no members, @scheme[#f]
otherwise.} otherwise.}
@defproc[(set-member? [set set?] [v any/c]) boolean?]{ @defproc[(set-member? [set set?] [v any/c]) boolean?]{
Returns @scheme[#t] if @scheme[v] is in @scheme[set], @scheme[#f] Returns @scheme[#t] if @scheme[v] is in @scheme[set], @scheme[#f]
otherwise.} otherwise.}
@defproc[(set-add [set set?] [v any/c]) set?]{ @defproc[(set-add [set set?] [v any/c]) set?]{
@margin-note{Like operations on immutable hash tables, ``constant @margin-note{Like operations on immutable hash tables, ``constant
@ -101,6 +103,15 @@ runs in time proportional to the total size of all given
@scheme[set]s except the first one.} @scheme[set]s except the first one.}
@defproc[(set-subset? [set set?] [set2 set?]) boolean?]{
Returns @scheme[#t] if every member of @scheme[set2] is in
@scheme[set], @scheme[#f] otherwise. The @scheme[set] and
@scheme[set2] must use the same equivalence predicate
(@scheme[equal?], @scheme[eq?], or @scheme[eqv?]). This operation
runs in time proportional to the size of @scheme[set2].}
@defproc[(set-map [set set?] @defproc[(set-map [set set?]
[proc (any/c . -> . any/c)]) [proc (any/c . -> . any/c)])
(listof any/c)]{ (listof any/c)]{