# cp.collect.Set

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:

  • union (A ⋃ B): a | b or a + b
  • intersection (A ∩ B): a & b
  • complement (Ac): -a
  • difference (A - B): a - b
  • symetric difference (A ⊕ B) a ~ b

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:

a + b | c ~= a + (b | c)

# API Overview

Constants - Useful values which cannot be changed

  • everything
  • nothing

Functions - API calls offered directly by the extension

  • complement
  • difference
  • has
  • intersection
  • is
  • isComplement
  • size
  • symetricDifference
  • union

Constructors - API calls which return an object, typically one that offers API methods

  • clone
  • fromList
  • fromMap
  • of

Methods - API calls which can only be made on an object returned by a constructor

  • complement
  • difference
  • has
  • intersection
  • isComplement
  • size
  • symetricDifference
  • union

# API Documentation

# Constants

# everything

Signature cp.collect.Set.everything <cp.collect.Set>
Type Constant
Description A Set which contains the whole universe.
Notes None
Source src/extensions/cp/collect/Set.lua line 694

# nothing

Signature cp.collect.Set.nothing <cp.collect.Set>
Type Constant
Description An empty Set.
Notes None
Source src/extensions/cp/collect/Set.lua line 689

# Functions

# complement

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
  • The new Set.
Notes None
Examples None
Source src/extensions/cp/collect/Set.lua line 455

# difference

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
  • The new Set.
Notes None
Examples None
Source src/extensions/cp/collect/Set.lua line 392

# has

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.
Notes None
Examples None
Source src/extensions/cp/collect/Set.lua line 310

# intersection

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.
Notes None
Examples None
Source src/extensions/cp/collect/Set.lua line 360

# is

Signature cp.collect.Set.is(thing) -> boolean
Type Function
Description Checks if the thing is a Set.
Parameters
  • thing - The thing to check.
Returns
  • true if it is a Set.
Notes None
Examples None
Source src/extensions/cp/collect/Set.lua line 213

# isComplement

Signature cp.collect.Set.isComplement(set) -> boolean
Type Function
Description Checks if the set is a complement set.
Parameters
  • set - The set to check.
Returns
  • true if the set is a complement.
Notes None
Examples None
Source src/extensions/cp/collect/Set.lua line 496

# size

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.
Examples None
Source src/extensions/cp/collect/Set.lua line 476

# symetricDifference

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. The resulting table will contain items that only occur in the left or right set, but not both.
Parameters
  • left - The left Set.
  • right - The right Set.
Returns
  • The new Set.
Notes None
Examples None
Source src/extensions/cp/collect/Set.lua line 425

# union

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 Sets.
Notes None
Examples None
Source src/extensions/cp/collect/Set.lua line 324

# Constructors

# clone

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
  • set - The set to clone.
Returns
  • The new Set instance.
Notes None
Examples None
Source src/extensions/cp/collect/Set.lua line 292

# fromList

| | | | --------------------------------------------|-------------------------------------------------------------------------------------| | 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. Any duplicate items will only occur in the Set once. | | Parameters |

  • list - The table that contains items as a list to add to the Set. E.g. {"foo", "bar"}</li></ul> | | **Returns** | <ul><li>The new Set`.
| | Notes | None | | Examples | None | | Source | src/extensions/cp/collect/Set.lua line 244 |


# fromMap

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. Keys with values other than true will be ignored.
Parameters
  • map - The table that contains key/value items to add to the set. E.g. {foo = true, bar = true}
Returns
  • The new Set.
Notes None
Examples None
Source src/extensions/cp/collect/Set.lua line 268

# of

Signature cp.collect.Set.of(...) -> cp.collect.Set
Type Constructor
Description Creates a new Set instance, containing the items in the parameter list.
Parameters
  • The set items.
Returns
  • The new Set instance.
Notes None
Examples None
Source src/extensions/cp/collect/Set.lua line 231

# Methods

# complement

Signature cp.collect.Set:complement() -> cp.collect.Set
Type Method
Description Returns a new Set which is the complement of the current Set.
Parameters
  • None
Returns
  • The new Set.
Notes
  • You can also use the - or ~ prefix operators. E.g. -a or ~a.
Examples None
Source src/extensions/cp/collect/Set.lua line 567

# difference

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
  • right - The right Set.
Returns
  • The new Set.
Notes
  • You can also use the - operator. E.g. a - b.
Examples None
Source src/extensions/cp/collect/Set.lua line 553

# has

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.
Examples None
Source src/extensions/cp/collect/Set.lua line 511

# intersection

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 Sets passed in.
Parameters
  • ... - The list of Sets to create an intersection from.
Returns
  • The new Set.
Notes
  • You can also use the & operator. E.g. a & b.
Examples None
Source src/extensions/cp/collect/Set.lua line 539

# isComplement

Signature cp.collect.Set:isComplement() -> boolean
Type Method
Description Checks if the set is a complement set.
Parameters
  • None
Returns
  • true if the set is a complement.
Notes None
Examples None
Source src/extensions/cp/collect/Set.lua line 595

# size

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 how many values have been removed from the universal set of all things.
Parameters
  • None
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.
Examples None
Source src/extensions/cp/collect/Set.lua line 606

# symetricDifference

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. The resulting table will contain items that only occur in the left or right set, but not both.
Parameters
  • right - The right Set.
Returns
  • The new Set.
Notes
  • You can also use the ~ operator. E.g. a ~ b.
Examples None
Source src/extensions/cp/collect/Set.lua line 581

# union

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 Sets passed in.
Parameters
  • ... - The list of Sets 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.
Examples None
Source src/extensions/cp/collect/Set.lua line 525