6.2.1 Set Operations

Method: empty? (set)  s

Returns true iff s is empty. O(1).

Method: empty-set-like (set)  c
Method: empty-set-like (bag)  c

Returns an empty set of the same type and organization as c.

Method: size (set)  s
Method: set-size (set)  s

Returns the number of elements in s. O(1).

Method: arb (set)  s

Returns an arbitrary element of s. Specifically, on a nonempty set, returns two values: an arbitrary element and true; on an empty set, returns two false values. O(1).

Method: contains? (set)  s x

Returns true iff x is an element of s. O(log n).

Method: lookup (set)  s x

If s contains x, returns two values, true and the element found; else false and nil. (This is useful for canonicalizing instances; the element found may be a different instance from x.) O(log n).

Method: with (set)  s x

Returns a set, of the same class and organization as s, that contains all elements of s and also x. If s already contained a value equal to x, according to its comparison function, returns s itself. (This is so you can find out whether the element was added simply by calling eq on s and the result.) O(log n).

Macro: includef s x

A modify macro that expands to with. If place s (which must be setf-able) holds a set, updates it so the set now contains x.

Method: less (set)  s x

Returns a set, of the same class and organization as s, that contains all elements of s except x. If s did not contain a value equal to x, according to its comparison function, returns s itself. (This is so you can find out whether the element was removed simply by calling eq on s and the result.) O(log n).

Macro: excludef s x

A modify macro that expands to less. If place s (which must be setf-able) holds a set, updates it so the set does not contain x.

Method: index (set)  s x

If s contains x, returns the index of x within the ordering of s; otherwise nil. O(log n).

Method: at-index (set)  s idx

Returns the element at index idx within the ordering of s. (On a wb-set, this is the same as at-rank.) idx must be in [0 .. size(s)); otherwise, signals an error of type simple-type-error. O(log n).

Method: union (set set)  s1 s2

Returns a set of the same type and organization as s1, that contains all elements that are in either s1 or s2. Implements left preference: for any pair of equal objects in the two sets, the result contains the instance that was in s1. Normally O(n).

Macro: unionf s1 s2

A modify macro that expands to union. If place s1 (which must be setf-able) holds a set, updates it so the set now contains all elements of s2.

Method: intersection (set set)  s1 s2

Returns a set of the same type and organization as s1, that contains all elements that are in both s1 and s2. Implements left preference: for any pair of equal objects in the two sets, the result contains the instance that was in s1. Normally O(n).

Macro: intersectf s1 s2

A modify macro that expands to intersection. If place s1 (which must be setf-able) holds a set, updates it so the set now contains only elements that are also in s2.

Method: set-difference (set set)  s1 s2

Returns a set of the same type and organization as s1, that contains all elements that are in s1 and not in s2. Normally O(n).

Method: set-difference-2 (set set)  s1 s2

Returns, as two values: () a set of the same type and organization as s1, that contains all elements that are in s1 and not in s2, and () a set of the same type and organization as s2, that contains all elements that are in s2 and not in s1. Normally O(n).

Method: subset? (set set)  s1 s2

Returns true iff every element of s1 is also in s2. Normally O(n).

Function: proper-subset? s1 s2

Returns true iff every element of s1 is also in s2, and s1 is smaller than s2. Normally O(n).

Method: disjoint? (set set)  s1 s2

Returns true iff s1 and s2 have no elements in common. Normally O(n).

Macro: do-set (var set &optional value) &body body

For each element of set, binds var to it and executes body. When done, returns value if supplied, otherwise nil.

Method: iterator (set)  s

Returns a stateful iterator over the set. The :get operation returns two values: if there is another element remaining, returns it and a true second value, else nil and false. Creating an iterator takes O(log n) time; the :get operation is amortized O(1).

Method: fun-iterator (set)  s

Returns a functional iterator over the set. The :first operation returns two values: if the iterator is not empty, returns the first element and a true second value, else nil and false. Creating an iterator takes O(log n) time; the :first operation is O(1), and :rest is amortized O(1).

Method: filter (function set)  pred s
Method: filter (symbol set)  pred s
Method: filter (map set)  pred s

Returns a new set, of the same type and organization as s, containing only those elements that satisfy pred. pred must be a function, an fbound symbol, or a boolean-valued map. O(n ⋅ tc(pred)) + O(r log r), where tc(pred) is the time complexity of pred and r is the size of the result.

Method: partition (function set)  pred s
Method: partition (symbol set)  pred s
Method: partition (map set)  pred s

Equivalent to (values (filter pred s) (filter (complement pred) s)) when pred is a function. Returns two values: first, a new set, of the same type and organization as s, containing only those elements that satisfy pred; second, a similar set containing only those elements that do not satisfy pred. pred must be a function, an fbound symbol, or a boolean-valued map. O(n ⋅ tc(pred)) + O(r log r), where tc(pred) is the time complexity of pred and r is the size of the result.

Method: image (function set)  fn s &key compare-fn-name
Method: image (symbol set)  fn s &key compare-fn-name
Method: image (map set)  fn s &key compare-fn-name

Returns a set containing the values of fn applied to all the elements of s. Since the result is a set, its elements will not necessarily correspond one-to-one to those of s; it will impose its own ordering, and will be smaller than s if fn returns the same value on two or more elements of s. The result will be of the same type as s. compare-fn-name, if supplied, will control its organization; otherwise, that will also be the same as for s. fn must be a function, an fbound symbol, or a map. O(n ⋅ tc(fn)) + O(r log r), where tc(fn) is the time complexity of fn and r is the size of the result.

Macro: imagef s fn

A modify macro that expands to image. If place s (which must be setf-able) holds a set, updates it so the place now contains the image of the set under fn.

Method: reduce (function set)  fn s &key key (initial-value nil)
Method: reduce (symbol set)  fn s &key key (initial-value nil)

Calls fn repeatedly with two arguments, a state variable and the next element of s, storing the result of each call back into the state variable, and finally returning that result. If initial-value is supplied, it is used as the initial value of the state variable. Otherwise: if s is nonempty, the initial value is the first element; if s is empty, the result of reduce is obtained by calling fn with no arguments. In all cases, if key is provided, it is called on each element and the result used in place of the element. (Time complexity depends on the behavior of fn.)

Method: find (t set)  item s &key key test

Scans linearly through s to find an element that matches item according to key and test. For each element of s, key, a function that defaults to the identity function, is called on it, and then test is called on item and the result; if it returns true, find returns the element. If no match is found, find returns nil. test defaults to equal?, not eql.

Normally O(n), but as a special case, if test is equal?, key is not supplied, and the set’s comparison function is compare, this does an O(log n) lookup.

Method: find-if (t set)  pred s &key key

Scans linearly through s to find an element for which the result of calling key on it satisfies pred. key and pred must be functions or fbound symbols. key defaults to the identity function. Returns the element found, or nil if none.

Method: find-if-not (t set)  pred s &key key

Scans linearly through s to find an element for which the result of calling key on it does not satisfy pred. key and pred must be functions or fbound symbols. key defaults to the identity function. Returns the element found, or nil if none.

Method: count (t set)  item s &key key test

Scans linearly through s, counting the number of elements that match item according to key and test. For each element of s, key is called on it, and then test is called on item and the result; if it returns true, the element is counted. Returns the count. key and test must be functions or fbound symbols. key defaults to the identity function; test defaults to equal?, not eql.

Normally O(n), but as a special case, if test is equal?, key is not supplied, and the set’s comparison function is compare, this does an O(log n) lookup to return 0 or 1.

Method: count-if (t set)  pred s &key key

Scans linearly through s to count the elements for which the result of calling key on them satisfies pred. Returns the count. pred and key must be functions or fbound symbols. key defaults to the identity function.

Method: count-if-not (t set)  pred s &key key

Scans linearly through s to count the elements for which the result of calling key on them does not satisfy pred. Returns the count. pred and key must be functions or fbound symbols. key defaults to the identity function.