Module std.prototype.set
Set Prototype.
This module returns a table of set operators, as well as the prototype for a Set container object.
Every possible object or primitive value is always present in any Set container exactly zero or one times.
In addition to the functionality described here, Set containers also have all the methods and metamethods of the prototype.container.prototype (except where overridden here).
Prototype Chain
table `-> Container `-> Set
Objects
prototype | Set prototype object. |
Initialisation
prototype._init | Set object initialisation. |
Metamethods
prototype:__add (s) | Union operation. |
prototype:__sub (s) | Difference operation. |
prototype:__mul (s) | Intersection operation. |
prototype:__div (s) | Symmetric difference operation. |
prototype:__le (s) | Subset operation. |
prototype:__lt (s) | Proper subset operation. |
prototype:__tostring () | Return a string representation of this set. |
Module Functions
delete (set, e) | Delete an element from a set. |
difference (set1, set2) | Find the difference of two sets. |
elems (set) | Iterator for sets. |
equal (set1, set2) | Find whether two sets are equal. |
insert (set, e) | Insert an element into a set. |
intersection (set1, set2) | Find the intersection of two sets. |
difference (set, e) | Say whether an element is in a set. |
proper_subset (set1, set2) | Find whether one set is a proper subset of another. |
subset (set1, set2) | Find whether one set is a subset of another. |
symmetric_difference (set1, set2) | Find the symmetric difference of two sets. |
union (set1, set2) | Find the union of two sets. |
Objects
- prototype
-
Set prototype object.
Fields:
- _type string object name (default "Set")
See also:
Usage:
local Set = require "std.prototype.set".prototype assert (prototype.type (Set) == "Set")
Initialisation
- prototype._init
-
Set object initialisation.
Returns partially initialised Set container with contents from t.
Parameters:
Metamethods
- prototype:__add (s)
-
Union operation.
Parameters:
- s prototype another set
Returns:
-
prototype
everything from this set plus everything from s
See also:
Usage:
union = this + s
- prototype:__sub (s)
-
Difference operation.
Parameters:
- s prototype another set
Returns:
-
prototype
everything from this set that is not also in s
See also:
Usage:
difference = this - s
- prototype:__mul (s)
-
Intersection operation.
Parameters:
- s prototype another set
Returns:
-
prototype
anything in both this set and in s
See also:
Usage:
intersection = this * s
- prototype:__div (s)
-
Symmetric difference operation.
Parameters:
- s prototype another set
Returns:
-
prototype
everything in this set or in s but not in both
See also:
Usage:
symmetric_difference = this / s
- prototype:__le (s)
-
Subset operation.
Parameters:
- s prototype another set
Returns:
-
boolean
true
if everything in this set is also in sSee also:
Usage:
issubset = this <= s
- prototype:__lt (s)
-
Proper subset operation.
Parameters:
- s prototype another set
Returns:
-
boolean
true
if s is not equal to this set, but does contain everything from this setSee also:
Usage:
ispropersubset = this < s
- prototype:__tostring ()
-
Return a string representation of this set.
Returns:
-
string
string representation of a set.
See also:
Module Functions
- delete (set, e)
-
Delete an element from a set.
Parameters:
- set prototype a set
- e element
Returns:
-
prototype
the modified set
Usage:
set.delete (available, found)
- difference (set1, set2)
-
Find the difference of two sets.
Parameters:
Returns:
-
prototype
a copy of set1 with elements of set2 removed
Usage:
all = set.difference (all, Set {32, 49, 56})
- elems (set)
-
Iterator for sets.
Parameters:
- set prototype a set
Returns:
-
set iterator
Usage:
for code in set.elems (isprintable) do print (code) end
- equal (set1, set2)
-
Find whether two sets are equal.
Parameters:
Returns:
-
boolean
true
if set1 and set2 each contain identical elements,false
otherwiseUsage:
if set.equal (keys, Set {META, CTRL, "x"}) then process (keys) end
- insert (set, e)
-
Insert an element into a set.
Parameters:
- set prototype a set
- e element
Returns:
-
prototype
the modified set
Usage:
for byte = 32,126 do set.insert (isprintable, string.char (byte)) end
- intersection (set1, set2)
-
Find the intersection of two sets.
Parameters:
Returns:
-
prototype
a new set with elements in both set1 and set2
Usage:
common = set.intersection (a, b)
- difference (set, e)
-
Say whether an element is in a set.
Parameters:
- set prototype a set
- e element
Returns:
true
if e is in set, otherwisefalse
otherwiseUsage:
if not set.member (keyset, pressed) then return nil end
- proper_subset (set1, set2)
-
Find whether one set is a proper subset of another.
Parameters:
Returns:
-
boolean
true
if set2 contains all elements in set1 but not only those elements,false
otherwiseUsage:
if set.proper_subset (a, b) then for e in set.elems (set.difference (b, a)) do set.delete (b, e) end end assert (set.equal (a, b))
- subset (set1, set2)
-
Find whether one set is a subset of another.
Parameters:
Returns:
-
boolean
true
if all elements in set1 are also in set2,false
otherwiseUsage:
if set.subset (a, b) then a = b end
- symmetric_difference (set1, set2)
-
Find the symmetric difference of two sets.
Parameters:
Returns:
-
prototype
a new set with elements that are in set1 or set2
but not both
Usage:
unique = set.symmetric_difference (a, b)
- union (set1, set2)
-
Find the union of two sets.
Parameters:
Returns:
-
prototype
a copy of set1 with elements in set2 merged in
Usage:
all = set.union (a, b)