Class Set
An unordered Collection of unique items.
Sets provide no particular ordering of their items, only either containing or not containing elements. Any particular concrete Set implementation may have an internal ordering, but this ordering does not affect the operation of the Set.
The Set interface provides a base set of operations for interacting with any abstract Set type. Abstract data types may provide addtional specific operations based on the particular implemented type. Concrete implementations, such as HashSets ultimately determine the properties of the concrete Set such as time and space complexity for any operations.
The Set interface provides certain optional methods in addition to those in Collection. Some Sets, such as immutable or type-restricted data types, may not be able to provide full functionality for these methods. All Sets are guaranteed to provide a required set of behaviors without exception and, unless otherwise noted, a method is required. All Sets should attempt to provide optional functionality, if they're able, regardless.
Implements: Collection, Enumerable
Functions
| Set.fromTable() | Creates a new Set from a set-formatted table. |
| Set.new() | Creates a new Set interface instance. |
Methods
| Set:Add(item) | Adds an item to the Set. |
| Set:AddAll(items) | Adds all provided items to the Set. |
| Set:Clear() | Removes everything from the Set. |
| Set:Contains(item) | Determines whether the Set contains an item. |
| Set:ContainsAll(items) | Determines whether the Set contains all of the items. |
| Set:ContainsAny(items) | Determines whether the Set contains any of the provided items. |
| Set:Count() | Gets the number of items in the Set. |
| Set:Empty() | Determines whether the Set has no elements. |
| Set:Enumerator() | Creates an enumerator for the Set. |
| Set:Except(items) | Transforms the Set to remove all elements from a Collection. |
| Set:Intersect(items) | Transforms the Set to keep only elements from a Collection. |
| Set:Overlaps(items) | Determines whether the Set elements overlaps a Collection. |
| Set:ProperSubsetOf(items) | Determines whether the Set is a strict subset of a Collection. |
| Set:ProperSupersetOf(items) | Determines whether the Set is a strict superset of a Collection. |
| Set:Remove(item) | Removes the specified item from the Set. |
| Set:RemoveAll(items) | Removes all provided items from the Set. |
| Set:RetainAll(items) | Removes all items except those provided from the Set. |
| Set:SetEquals(items) | Determines whether the Set contains the same elements as a Collection. |
| Set:SubsetOf(items) | Determines whether the Set is a subset of a Collection. |
| Set:SupersetOf(items) | Determines whether the Set is a superset of a Collection. |
| Set:SymmetricExcept(items) | Transforms the Set to keep only elements in it or a Collection, not both. |
| Set:ToArray() | Creates a new array indexed table of this Set. |
| Set:ToTable() | Creates a new table of this Set. |
| Set:Union(items) | Transforms the Set to include all elements from a Collection. |
Functions
Methods- Set.fromTable()
-
Creates a new Set from a set-formatted table.
This constructor creates a new Set copy of the table using only its keys. A
typical set-formatted table consists of keys that have the value
true, although any non-nil value will result in a valid Set. The values associated with the keys in the table will not be preserved in the Set.Returns:
-
the new Set
- Set.new()
-
Creates a new Set interface instance.
This should only be used when implementing a new Set.
Access:
-
private
Returns:
-
the new Set interface
Methods
- Set:Add(item)
-
Adds an item to the Set.
This method is optional. All Set implementations should attempt to implement this method, but some may be unable to do so or may need to impose additional conditions to do so.
This method should always be overridden regardless of implementation. If unimplemented, it should return an error specific to the optional functionality that can't be provided by this Set.
Inherited from:
Parameters:
- item the item to add
Returns:
-
true if the Set changed as a result, false otherwise
- Set:AddAll(items)
-
Adds all provided items to the Set.
This method is optional. All Set implementations should attempt to implement this method, but some may be unable to do so or may need to impose additional conditions to do so.
This method should always be overridden regardless of implementation. If unimplemented, it should return an error specific to the optional functionality that can't be provided by this Set.
Inherited from:
Parameters:
- items the Collection of items to add to this Set
Returns:
-
true if the Set changed as a result, false otherwise
- Set:Clear()
-
Removes everything from the Set.
This method is optional. All Set implementations should attempt to implement this method, but some may be unable to do so or may need to impose additional conditions to do so.
This method should always be overridden regardless of implementation. If unimplemented, it should return an error specific to the optional functionality that can't be provided by this Set.
Inherited from:
- Set:Contains(item)
-
Determines whether the Set contains an item.
Inherited from:
Parameters:
- item the item to locate in the Set
Returns:
-
true if the item is in the Set, false otherwise
- Set:ContainsAll(items)
-
Determines whether the Set contains all of the items.
Checks for items provided in another Collection in an arbitrary,
deterministic order. The order is the same as the order of enumeration.
Inherited from:
Parameters:
- items the Collection of items to locate in this Set
Returns:
-
true if all items are in the Set, false otherwise
- Set:ContainsAny(items)
-
Determines whether the Set contains any of the provided items.
Checks for items provided in another Collection in an arbitrary,
deterministic order. The order is the same as the order of enumeration.
Inherited from:
Parameters:
- items the Collection of items to locate in this Set
Returns:
-
true if any items are in the Set, false otherwise
- Set:Count()
-
Gets the number of items in the Set.
Inherited from:
Returns:
-
the number of items
- Set:Empty()
-
Determines whether the Set has no elements.
Inherited from:
Returns:
-
true if the Set empty, false otherwise
- Set:Enumerator()
-
Creates an enumerator for the Set.
The enumerator can be used directly in a generic for loop similar to pairs
or ipairs.
Inherited from:
Returns:
- the enumerator generator
- the invariant state
- the control variable state
- Set:Except(items)
-
Transforms the Set to remove all elements from a Collection.
This method is optional. All Set implementations should attempt to implement this method, but some may be unable to do so or may need to impose additional conditions to do so.
This method should always be overridden regardless of implementation. If unimplemented, it should return an error specific to the optional functionality that can't be provided by this Set.
Parameters:
- items the Collection of items to except
- Set:Intersect(items)
-
Transforms the Set to keep only elements from a Collection.
This method is optional. All Set implementations should attempt to implement this method, but some may be unable to do so or may need to impose additional conditions to do so.
This method should always be overridden regardless of implementation. If unimplemented, it should return an error specific to the optional functionality that can't be provided by this Set.
Parameters:
- items the Collection of items to intersect
- Set:Overlaps(items)
-
Determines whether the Set elements overlaps a Collection.
Parameters:
- items the Collection of items to locate in this Set
Returns:
-
true if they overlap, false otherwise
- Set:ProperSubsetOf(items)
-
Determines whether the Set is a strict subset of a Collection.
A proper or strict subset is one where every element in this set is
contained in the other, and the other contains at least one additional
element.
Parameters:
- items the Collection of items to check against
Returns:
-
true if the Set is a proper subset, false otherwise
- Set:ProperSupersetOf(items)
-
Determines whether the Set is a strict superset of a Collection.
A proper or strict superset is one where every element in the other is
contained in this set, and this set contains at least one additional
element.
Parameters:
- items the Collection of items to check against
Returns:
-
true if the Set is a proper superset, false otherwise
- Set:Remove(item)
-
Removes the specified item from the Set.
This method is optional. All Set implementations should attempt to implement this method, but some may be unable to do so or may need to impose additional conditions to do so.
This method should always be overridden regardless of implementation. If unimplemented, it should return an error specific to the optional functionality that can't be provided by this Set.
Inherited from:
Parameters:
- item the item to remove from the Set
Returns:
-
true if the Set changed as a result, false otherwise
- Set:RemoveAll(items)
-
Removes all provided items from the Set.
Removes each instance of a provided item.
This method is optional. All Set implementations should attempt to implement this method, but some may be unable to do so or may need to impose additional conditions to do so.
This method should always be overridden regardless of implementation. If unimplemented, it should return an error specific to the optional functionality that can't be provided by this Set.
Inherited from:
Parameters:
- items the Collection of items to remove from this Set
Returns:
-
true if the Set changed as a result, false otherwise
- Set:RetainAll(items)
-
Removes all items except those provided from the Set.
Retains only the items contained in the specified Collection. If there are
duplicates they are all kept.
This method is optional. All Set implementations should attempt to implement this method, but some may be unable to do so or may need to impose additional conditions to do so.
This method should always be overridden regardless of implementation. If unimplemented, it should return an error specific to the optional functionality that can't be provided by this Set.
Inherited from:
Parameters:
- items the Collection of items to retain in this Set
Returns:
-
true if the Set changed as a result, false otherwise
- Set:SetEquals(items)
-
Determines whether the Set contains the same elements as a Collection.
Determines only that the same exact Sets of elements are contained and does
not care for duplicates or any other associated data.
Parameters:
- items the Collection of items to check against
Returns:
-
true if the Sets are equal, false otherwise
- Set:SubsetOf(items)
-
Determines whether the Set is a subset of a Collection.
A subset is one where every element in this Set is contained in the other.
Parameters:
- items the Collection of items to check against
Returns:
-
true if the Set is a superset, false otherwise
- Set:SupersetOf(items)
-
Determines whether the Set is a superset of a Collection.
A superset is one where every element in the other is contained in this set.
Parameters:
- items the Collection of items to check against
Returns:
-
true if the Set is a superset, false otherwise
- Set:SymmetricExcept(items)
-
Transforms the Set to keep only elements in it or a Collection, not both.
This method is optional. All Set implementations should attempt to implement this method, but some may be unable to do so or may need to impose additional conditions to do so.
This method should always be overridden regardless of implementation. If unimplemented, it should return an error specific to the optional functionality that can't be provided by this Set.
Parameters:
- items the Collection of items to symmetric except
- Set:ToArray()
-
Creates a new array indexed table of this Set.
Inherited from:
Returns:
-
the array indexed table
See also:
- Set:ToTable()
-
Creates a new table of this Set.
The indices of the table are the elements of the set. The only values in the
table are true if the element exists, or nil if it does not.
Inherited from:
Returns:
-
the table
- Set:Union(items)
-
Transforms the Set to include all elements from a Collection.
This method is optional. All Set implementations should attempt to implement this method, but some may be unable to do so or may need to impose additional conditions to do so.
This method should always be overridden regardless of implementation. If unimplemented, it should return an error specific to the optional functionality that can't be provided by this Set.
Parameters:
- items the Collection of items to union