An implementation of a logical set
, which contains a single unique
reference of each item in it. For example:
Set(1,2,2,3) == Set(1,1,2,3,3) == Set(1,2,3)
You can combine sets in a couple of ways. For example, a union
:
Set(1,2):union(Set(2,3)) == Set(1,2,3)
Set(1,2) | Set(2,3) == Set(1,2,3)
...or an intersection
:
Set(1,2):intersection(Set(2,3)) == Set(2)
Set(1,2) & Set(2,3) == Set(2)
As indicated above, you can use operators for common set operations. Specifically:
Keep in mind that Lua's operator precedence may be different to that of standard set operations, so it's probably best to group operations in brackets if you combine more than one in a single statement. For example:
Signature |
cp.collect.Set.everything <cp.collect.Set> |
Type |
Constant |
Description |
A Set which contains the whole universe. |
Signature |
cp.collect.Set.nothing <cp.collect.Set> |
Type |
Constant |
Description |
An empty Set . |
Signature |
cp.collect.Set.complement(set) -> cp.collect.Set |
Type |
Function |
Description |
Returns a Set which is the complement of the provided set. |
Parameters |
- set - The
Set to complement.
|
Returns |
|
Signature |
cp.collect.Set.difference(left, right) -> cp.collect.Set |
Type |
Function |
Description |
Returns a new Set which is the set of values in left that are not in right . |
Parameters |
- left - The left
Set . - right - The right
Set .
|
Returns |
|
Signature |
cp.collect.Set.has(set, value) -> boolean |
Type |
Function |
Description |
Checks if the set has the specified value. |
Parameters |
- set - The
Set to check. - value - The value to check for.
|
Returns |
true if the value is contained in the Set .
|
Signature |
cp.collect.Set.intersection(left, right) -> cp.collect.Set |
Type |
Function |
Description |
|
Parameters |
- left - The left
Set - right - The right
Set .
|
Returns |
- A new
Set which contains an intersection left and right .
|
Signature |
cp.collect.Set.is(thing) -> boolean |
Type |
Function |
Description |
Checks if the thing is a Set . |
Parameters |
- thing - The thing to check.
|
Returns |
|
Signature |
cp.collect.Set.isComplement(set) -> boolean |
Type |
Function |
Description |
Checks if the set is a complement set. |
Parameters |
|
Returns |
true if the set is a complement.
|
Signature |
cp.collect.Set.size(set) -> number |
Type |
Function |
Description |
Returns the size of the set. |
Parameters |
- set - The set to find the size of.
|
Returns |
- the number of values in the set, or the number of values removed from a complement set.
|
Notes |
- If the set is empty,
0 is returned. - If the set is a complement, this will return a negative number indicating how many values have been removed from the universal set of all things.
- If the set is a complement of an empty set,
nil is returned to indicate the size is infinite.
|
Signature |
cp.collect.Set.symetricDifference(left, right) -> cp.collect.Set |
Type |
Function |
Description |
Performs a symetric difference of keys with a value of true in the left and right table into a new table. |
Parameters |
- left - The left
Set . - right - The right
Set .
|
Returns |
|
Signature |
cp.collect.Set.union(left, right) -> cp.collect.Set |
Type |
Function |
Description |
Returns a new Set which is a union of the left and right |
Parameters |
- left - The left
Set . - right - The right
Set .
|
Returns |
- A new
Set which contains a union of the left and right Set s.
|
Signature |
cp.collect.Set.clone(set) -> cp.collect.Set |
Type |
Constructor |
Description |
Creates a new Set which is a clone of the provided Set . |
Parameters |
|
Returns |
|
Signature |
cp.collect.Set.fromList(list) -> cp.collect.Set |
Type |
Constructor |
Description |
Creates a new Set instance, containing the unique items in the table collected as a list from 1 to n . |
Parameters |
- list - The table that contains items as a list to add to the
Set . E.g. `{"foo", "bar"}
|
Returns |
|
Signature |
cp.collect.Set.fromMap(map) -> cp.collect.Set |
Type |
Constructor |
Description |
Creates a new Set instance, containing the items in the provided table who's key value is true . |
Parameters |
- map - The table that contains key/value items to add to the set. E.g.
{foo = true, bar = true}
|
Returns |
|
Signature |
cp.collect.Set.of(...) -> cp.collect.Set |
Type |
Constructor |
Description |
Creates a new Set instance, containing the items in the parameter list. |
Parameters |
|
Returns |
|
Signature |
cp.collect.Set:complement() -> cp.collect.Set |
Type |
Method |
Description |
Returns a new Set which is the complement of the current Set . |
Parameters |
|
Returns |
|
Notes |
- You can also use the
- or ~ prefix operators. E.g. -a or ~a .
|
Signature |
cp.collect.Set:difference(right) -> cp.collect.Set |
Type |
Method |
Description |
Returns a new Set which is the set of values in this Set that are not in right . |
Parameters |
|
Returns |
|
Notes |
- You can also use the
- operator. E.g. a - b .
|
Signature |
cp.collect.Set:has(value) -> boolean |
Type |
Method |
Description |
Checks if this set has the specified value. |
Parameters |
- value - The value to check for.
|
Returns |
true if the Set contains the value .
|
Notes |
- You can also check for specific values via
mySet['key'] or mySet.key .
|
Signature |
cp.collect.Set:intersection(...) -> cp.collect.Set |
Type |
Method |
Description |
Creates a new Set which is an intersection of the current values plus other Set s passed in. |
Parameters |
- ... - The list of
Set s to create an intersection from.
|
Returns |
|
Notes |
- You can also use the
& operator. E.g. a & b .
|
Signature |
cp.collect.Set:isComplement() -> boolean |
Type |
Method |
Description |
Checks if the set is a complement set. |
Parameters |
|
Returns |
true if the set is a complement.
|
Signature |
cp.collect.Set:size() -> number |
Type |
Method |
Description |
Returns the size of the Set . If the set is a complement, this will return a negative number indicating |
Parameters |
|
Returns |
- the number of values in the set, or the number of values removed from a complement set.
|
Notes |
- If the set is empty,
0 is returned. - If the set is a complement, this will return a negative number indicating how many values have been removed from the universal set of all things.
- If the set is a complement of an empty set,
nil is returned to indicate the size is infinite.
|
Signature |
cp.collect.Set:symetricDifference(right) -> cp.collect.Set |
Type |
Method |
Description |
Performs a symetric difference of keys with a value of true in the left and right table into a new table. |
Parameters |
|
Returns |
|
Notes |
- You can also use the
~ operator. E.g. a ~ b .
|
Signature |
cp.collect.Set:union(...) -> cp.collect.Set |
Type |
Method |
Description |
Creates a new set which is a union of the current set plus other Set s passed in. |
Parameters |
- ... - The list of
Set s to create a union from.
|
Returns |
- The new
Set which is a union.
|
Notes |
- You can also use the
| or + operator. E.g. a | b or a + b .
|