sets

Overview

Module containing methods shared across set implementations.

Summary

differenceReturns the set that would be created by removing from set1 all elements in intersection(set1,set2).
equalsReturns true if both sets have the same size and contain the same elements.
hashReturns the sum of utils.hash(i) for i in set.
intersectionComputes and returns the intersection of set1 and set2.
differenceReturns the set that would be created by removing from set1 all elements in intersection(set1,set2).
removeAllEquivalent to calling set:remove(e) for e in iter(elements).
removeElementEquivalent to set:remove(element).
retainOnlyRemoves all elements from set which do not also appear in elements.
toStringReturns a string representation of set.
unionReturns the union of set1 and set2 as a new set.
uniqueFilterCreate and return a uniqueness filter based on set.
xorReturns the set of elements which appear in either set1 or set2, but not in both.

Detail

sets.difference(set1, set2 [,setType])

Returns the set that would be created by removing from set1 all elements in intersection(set1,set2).

The returned set is created by a call to:

(setType or getmetatable(set1)):new()

synonyms: __sub, minus

sets.equals(set1, set2)

Returns true if both sets have the same size and contain the same elements.

This function does not care whether both sets are the same type - a HashSet and SkipSet may be equal according to this function.

sets.hash(set)

Returns the sum of utils.hash(i) for i in set.

This is the hash function used by all set implementations.

sets.intersection(set1, set2 [, setType])

Computes and returns the intersection of set1 and set2.

The returned set is created by a call to:

(setType or getmetatable(set1)):new()

sets.difference(set1, set2 [,setType])

Returns the set that would be created by removing from set1 all elements in intersection(set1,set2).

The returned set is created by a call to:

(setType or getmetatable(set1)):new()

synonyms: __sub, minus

sets.removeAll(set, elements)

Equivalent to calling set:remove(e) for e in iter(elements).

sets.removeElement(set, element)

Equivalent to set:remove(element).

In a sequence, the remove() method takes as input an index while the removeElement method takes as input the element to be removed. This method has the same semantics across both set and sequence implementations.

sets.retainOnly(set, elements)

Removes all elements from set which do not also appear in elements.

If elements is also a set, s, set is modified to be the intersection of set and s.

sets.toString(set)

Returns a string representation of set.

The order in which elements appear in the representation is the same as the iteration order of elements of the set.

Example:

> print(HashSet:make(1,1,3,2,5,6,4))
{1, 2, 3, 4, 5, 6}

sets.union(set1, set2 [, setType])

Returns the union of set1 and set2 as a new set.

The returned set is created by a call to:

(setType or getmetatable(set1)):make(...)

synonyms: __add

sets.uniqueFilter(set)

Create and return a uniqueness filter based on set.

The filter is a function which returns true only for objects it has not yet seen. This can be used in conjuction with iter.filter. For example:

> v = vector(1,1,1,2,3,3,4,1,5,5)
> print(vector(iter.filter(v, HashSet:uniqueFilter())))
[1, 2, 3, 4, 5]

sets.xor(set1, set2 [,setType])

Returns the set of elements which appear in either set1 or set2, but not in both.

This operation is also often called the symmetric set difference.