# LispKit Set

Library `(lispkit set)` provides a native implementation of sets. Its API design is compatible to the R6RS-style API of library `(lispkit hashtable)`.

A set is a data structure for representing collections of objects. Any object can be used as element, provided a hash function and a suitable equivalence function is available. A hash function is a procedure that maps elements to exact integer objects. It is the programmer’s responsibility to ensure that the hash function is compatible with the equivalence function, which is a procedure that accepts two objects and returns true if they are equivalent and `#f` otherwise. Standard sets for arbitrary objects based on the `eq?`, `eqv?`, and `equal?` predicates are provided.

## Constructors

set-type-tag [constant]

Symbol representing the `set` type. The `type-for` procedure of library `(lispkit type)` returns this symbol for all set objects.

(make-eq-set) [procedure]

Create a new empty set using `eq?` as equivalence function.

(make-eqv-set) [procedure]

Create a new empty set using `eqv?` as equivalence function.

(make-equal-set) [procedure]

Create a new empty set using `equal?` as equivalence function.

(make-set hash equiv) [procedure]
(make-set hash equiv k)

Create a new empty set using the given hash function hash and equivalence function equiv. An initial capacity k can be provided optionally.

(eq-set element …) [procedure]

Create a new set using `eq?` as equivalence function. Initialize it with the values element … .

(eqv-set element …) [procedure]

Create a new set using `eqv?` as equivalence function. Initialize it with the values element … .

(equal-set element …) [procedure]

Create a new set using `equal?` as equivalence function. Initialize it with the values element … .

## Inspection

(set-equivalence-function s) [procedure]

Returns the equivalence function used by set s.

(set-hash-function s) [procedure]

Returns the hash function used by set s.

(set-mutable? s) [procedure]

Returns `#t` if set s is mutable.

## Predicates

(set? obj) [procedure]

Returns `#t` if obj is a set.

(set-empty? obj) [procedure]

Returns `#t` if obj is an empty set.

(set=? s1 s2) [procedure]

Returns `#t` if set s1 and set s2 are using the same equivalence function and contain the same elements.

(disjoint? s1 s2) [procedure]

Returns `#t` if set s1 and set s2 are disjoint sets.

(subset? s1 s2) [procedure]

Returns `#t` if set s1 is a subset of set s2.

(proper-subset? s1 s2) [procedure]

Returns `#t` if set s1 is a proper subset of set s2, i.e. s1 is a subset of s2 and s1 is not equivalent to s2.

(set-contains? s element) [procedure]

Returns `#` if set s contains element.

(set-any? s proc) [procedure]

Returns true if there is at least one element in set s for which procedure proc returns true (i.e. not `#f`).

(set-every? s proc) [procedure]

Returns true if procedure proc returns true (i.e. not `#f`) for all elements of set s.

## Procedures

(set-size s) [procedure]

Returns the number of elements in set s.

(set-elements s) [procedure]

Returns the elements of set s as a vector.

(set-copy s) [procedure]
(set-copy s mutable)

Copies set s creating an immutable copy if `mutable` is set to `#f` or if `mutable` is not provided.

(set-for-each s proc) [procedure]

Applies procedure proc to all elements of set s in an undefined order.

(set-filter s pred) [procedure]

Creates a new set containing the elements of set s for which the procedure pred returns true.

(set-union s s1 …) [procedure]

Creates a new set containing the union of s with s1 ….

(set-intersection s s1 …) [procedure]

Creates a new set containing the intersection of s with s1 ….

(set-difference s s1 …) [procedure]

Creates a new set containing the difference of `s` and the sets in s1 … .

(set->list s) [procedure]

Returns the elements of set s as a list.

(list->eq-set elements) [procedure]

Creates a new set using the equivalence function `eq?` from the values in list elements.

(list->eqv-set elements) [procedure]

Creates a new set using the equivalence function `eqv?` from the values in list elements.

(list->equal-set elements) [procedure]

Creates a new set using the equivalence function `equal?` from the values in list elements.

## Mutators

(set-adjoin! s element …) [procedure]

Adds element … to the set s.

(set-delete! s element …) [procedure]

Deletes element … from the set s.

(set-clear! s) [procedure]
(set-clear! s k)

Clears set s and reserves a capacity of k elements if k is provided.

(list->set! s elements) [procedure]

Adds the values of list elements to set s.

(set-filter! s pred) [procedure]

Removes all elements from set s for which procedure pred returns `#f`.

(set-union! s s1 …) [procedure]

Stores the union of set s and sets s1 … in s.

(set-intersection! s s1 …) [procedure]

Stores the intersection of set s and the sets s1 … in s.

(set-difference! s s1 …) [procedure]

Stores the difference of set s and the sets s1 … in s.