1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
CLASS::Set
summary::a set according to equality
related::Classes/IdentitySet, Classes/List, Classes/Dictionary
categories::Collections>Unordered
DESCRIPTION::
A Set is s collection of objects, no two of which are equal. Most of its methods are inherited from Collection. The contents of a Set are unordered. You must not depend on the order of items in a set. For an ordered set, see link::Classes/OrderedIdentitySet::.
INSTANCEMETHODS::
private::initSet, putCheck, fullCheck, grow, noCheckAdd
subsection::Adding and Removing
method::add
Add an Object to the Set. An object which is equal to an object already in the Set will not be added.
code::
Set[1, 2, 3].add(4).postln;
Set[1, 2, 3].add(3).postln;
Set["abc", "def", "ghi"].add("jkl").postln;
Set["abc", "def", "ghi"].add("def").postln;
::
method::remove
Remove an Object from the Set. Element is checked for equality (not for identity).
code::
Set[1, 2, 3].remove(3).postln;
::
subsection::Testing
method::includes
Returns true if the specified item is present in the Set. Elements are checked for equality (not for identity).
code::
Set[1, 2, 3].includes(2).postln;
::
method::findMatch
Returns the item, if it is present in the set. Otherwise returns nil. Element is checked for equality (not for identity).
code::
Set[1, 2, 3].findMatch(3).postln;
::
subsection::Iteration
method::do
Evaluates function for each item in the Set. The function is passed two arguments, the item and an integer index.
code::
Set[1, 2, 3, 300].do({ arg item, i; item.postln });
::
method::keyAt
Returns the object at the internal strong::index::. This index is not deterministic.
subsection::Set specific operations
method::sect, &
Return the set theoretical intersection of this and strong::that::. The function will search for objects occurring in both sets and return a new set containing those.
Elements are checked for equality (not for identity).
code::
a = Set[1, 2, 3]; b = Set[2, 3, 4, 5];
sect(a, b);
a & b // shorter syntax
::
method::union, |
Return the set theoretical union of this and strong::that::. The function combines the two sets into one without duplicates.
Elements are checked for equality (not for identity).
code::
a = Set[1, 2, 3]; b = Set[2, 3, 4, 5];
union(a, b);
a | b // shorter syntax
::
method::difference, -
Return the set of all items which are elements of this, but not of strong::that::. Elements are checked for equality (not for identity).
code::
a = Set[1, 2, 3]; b = Set[2, 3, 4, 5];
difference(a, b);
a - b // shorter syntax
::
method::symmetricDifference, --
Return the set of all items which are not elements of both this and strong::that::.
Elements are checked for equality (not for identity).
code::
a = Set[1, 2, 3]; b = Set[2, 3, 4, 5];
symmetricDifference(a, b);
a -- b // shorter syntax
::
method::isSubsetOf
Returns true if all elements of this are also elements of strong::that::. Elements are checked for equality (not for identity).
Since Set is an unordered collection, order doesn't matter in this comparison.
code::
a = Set[1, 2, 3, 4];
Set[1, 2].isSubsetOf(a); // true
Set[1, 5].isSubsetOf(a); // false
::
EXAMPLES::
code::
a = Set[1, 2, 3, 4];
b = a.powerset; // set of all parts
a.isSubsetOf(b); // false: no set is ever part of itself.
b.asArray.reduce(\union) == a; // true parts may not contain other elements that original
b.asArray.reduce(\difference).isEmpty; // true.
::
code::
// you can use Set to efficiently remove duplicates from an array:
a = [1, 2, 3, 4, 3, 5, 5, 2, 2, 1];
a.as(Set); // convert to set
a.as(Set).as(Array); // and convert back
::
|