]> gitweb.factorcode.org Git - factor.git/blob - core/sets/sets-docs.factor
sets: adding union! and intersection.
[factor.git] / core / sets / sets-docs.factor
1 USING: assocs hashtables help.markup help.syntax kernel
2 quotations sequences vectors ;
3 IN: sets
4
5 ARTICLE: "sets" "Sets"
6 "A set is an unordered list of elements. Words for working with sets are in the " { $vocab-link "sets" } " vocabulary." $nl
7 "All sets are instances of a mixin class:"
8 { $subsections
9     set
10     set?
11 }
12 { $subsections "set-operations" "set-implementations" } ;
13
14 ABOUT: "sets"
15
16 ARTICLE: "set-operations" "Operations on sets"
17 "To test if an object is a member of a set:"
18 { $subsections in? }
19 "All sets can be represented as a sequence, without duplicates, of their members:"
20 { $subsections members }
21 "To get the number of elements in a set:"
22 { $subsections cardinality }
23 "Sets can have members added or removed destructively:"
24 { $subsections
25     adjoin
26     delete
27     clear-set
28     union!
29 }
30 "To test if a set is the empty set:"
31 { $subsections null? }
32 "Basic mathematical operations, which any type of set may override for efficiency:"
33 { $subsections
34     diff
35     intersect
36     union
37 }
38 "Mathematical predicates on sets, which may be overridden for efficiency:"
39 { $subsections
40     intersects?
41     subset?
42     set=
43 }
44 "An optional generic word for creating sets of the same class as a given set:"
45 { $subsections set-like }
46 "An optional generic word for creating a set with a fast lookup operation, if the set itself has a slow lookup operation:"
47 { $subsections fast-set }
48 "For set types that allow duplicates, like sequence sets, some additional words test for duplication:"
49 { $subsections
50     all-unique?
51     duplicates
52 }
53 "Utilities for sets and sequences:"
54 { $subsections
55      within
56      without
57 } ;
58
59 ARTICLE: "set-implementations" "Set implementations"
60 "There are several implementations of sets in the Factor library. More can be added if they implement the words of the set protocol, the basic set operations."
61 { $subsections
62     "sequence-sets"
63     "hash-sets"
64     "bit-sets"
65 } ;
66
67 ARTICLE: "sequence-sets" "Sequences as sets"
68 "Any sequence can be used as a set. The members of this set are the elements of the sequence. Calling the word " { $link members } " on a sequence returns a copy of the sequence with only one listing of each member. Destructive operations " { $link adjoin } " and " { $link delete } " only work properly on growable sequences like " { $link vector } "s."
69 $nl
70 "Care must be taken in writing efficient code using sequence sets. Testing for membership with " { $link in? } ", as well as the destructive set operations, take time proportional to the size of the sequence. Another representation, like " { $link "hash-sets" } ", would take constant time for membership tests. But binary operations like " { $link union } " are asymptotically optimal, taking time proportional to the sum of the size of the inputs."
71 $nl
72 "As one particular example, " { $link POSTPONE: f } " is a representation of the empty set, since it is an empty sequence." ;
73
74 HELP: set
75 { $class-description "The class of all sets. Custom implementations of the set protocol should be declared as instances of this mixin for all set implementation to work correctly." } ;
76
77 HELP: adjoin
78 { $values { "elt" object } { "set" set } }
79 { $description "Destructively adds " { $snippet "elt" } " to " { $snippet "set" } ". For sequences, this guarantees that this element is not duplicated, and that it is at the end of the sequence." $nl "Each mutable set type is expected to implement a method on this generic word." }
80 { $examples
81     { $example
82         "USING: prettyprint sets kernel ;"
83         "V{ \"beans\" \"salsa\" \"cheese\" } clone"
84         "\"nachos\" over adjoin"
85         "\"salsa\" over adjoin"
86         "."
87         "V{ \"beans\" \"cheese\" \"nachos\" \"salsa\" }"
88     }
89 }
90 { $side-effects "set" } ;
91
92 HELP: ?adjoin
93 { $values { "elt" object } { "set" set } { "?" "a boolean" } }
94 { $description "A version of " { $link adjoin } " which returns whether the element was added to the set." }
95 { $notes "This is slightly less efficient than " { $link adjoin } " due to the initial membership test." } ;
96
97 HELP: delete
98 { $values { "elt" object } { "set" set } }
99 { $description "Destructively removes " { $snippet "elt" } " from " { $snippet "set" } ". If the element is not present, this does nothing." $nl "Each mutable set type is expected to implement a method on this generic word." }
100 { $side-effects "set" } ;
101
102 HELP: clear-set
103 { $values { "set" set } }
104 { $contract "Removes all entries from the set." }
105 { $side-effects "set" } ;
106
107 HELP: members
108 { $values { "set" set } { "seq" sequence } }
109 { $description "Creates a sequence with a single copy of each member of the set." $nl "Each set type is expected to implement a method on this generic word." } ;
110
111 HELP: in?
112 { $values { "elt" object } { "set" set } { "?" "a boolean" } }
113 { $description "Tests whether the element is a member of the set." $nl "Each set type is expected to implement a method on this generic word as part of the set protocol." } ;
114
115 HELP: adjoin-at
116 { $values { "value" object } { "key" object } { "assoc" assoc } }
117 { $description "Adds " { $snippet "value" } " to the set stored at " { $snippet "key" } " of " { $snippet "assoc" } "." }
118 { $side-effects "assoc" } ;
119
120 HELP: duplicates
121 { $values { "set" set } { "seq" sequence } }
122 { $description "Outputs a sequence consisting of elements which occur more than once in " { $snippet "set" } "." }
123 { $examples
124     { $example "USING: sets prettyprint ;" "{ 1 2 3 1 2 1 } duplicates ." "{ 1 2 1 }" }
125 } ;
126
127 HELP: all-unique?
128 { $values { "set" set } { "?" "a boolean" } }
129 { $description "Tests whether a set contains any repeated elements." }
130 { $example
131     "USING: sets prettyprint ;"
132     "{ 0 1 1 2 3 5 } all-unique? ."
133     "f"
134 } ;
135
136 HELP: diff
137 { $values { "set1" set } { "set2" set } { "set" set } }
138 { $description "Outputs a set consisting of elements present in " { $snippet "set1" } " but not " { $snippet "set2" } ", comparing elements for equality." 
139 "This word has a default definition which works for all sets, but set implementations may override the default for efficiency."
140 } { $examples
141     { $example "USING: sets prettyprint ;" "{ 1 2 3 } { 2 3 4 } diff ." "{ 1 }" }
142 } ;
143
144 HELP: intersect
145 { $values { "set1" set } { "set2" set } { "set" set } }
146 { $description "Outputs a set consisting of elements present in both " { $snippet "set1" } " and " { $snippet "set2" } "."
147 "This word has a default definition which works for all sets, but set implementations may override the default for efficiency." }
148 { $examples
149     { $example "USING: sets prettyprint ;" "{ 1 2 3 } { 2 3 4 } intersect ." "{ 2 3 }" }
150 } ;
151
152 HELP: intersection
153 { $values { "sets" sequence } { "set/f" "a " { $link set } " or " { $link f } } }
154 { $description "Outputs the intersection of all the sets of the sequence " { $snippet "sets" } ", or " { $link f } " if " { $snippet "sets" } " is empty." } ;
155
156 HELP: union
157 { $values { "set1" set } { "set2" set } { "set" set } }
158 { $description "Outputs a set consisting of elements present in either " { $snippet "set1" } " or " { $snippet "set2" } " which does not contain duplicate values."
159 "This word has a default definition which works for all sets, but set implementations may override the default for efficiency." }
160 { $examples
161     { $example "USING: sets prettyprint ;" "{ 1 2 3 } { 2 3 4 } union ." "{ 1 2 3 4 }" }
162 } ;
163
164 { diff intersect union } related-words
165
166 HELP: union!
167 { $values { "set1" set } { "set2" set } }
168 { $description "Adds all members from " { $snippet "set2" } " to " { $snippet "set1" } "." }
169 { $side-effects "set1" } ;
170
171 HELP: intersects?
172 { $values { "set1" set } { "set2" set } { "?" "a boolean" } }
173 { $description "Tests if " { $snippet "set1" } " and " { $snippet "set2" } " have any elements in common." }
174 { $notes "If one of the sets is empty, the result is always " { $link f } "." } ;
175
176 HELP: subset?
177 { $values { "set1" set } { "set2" set } { "?" "a boolean" } }
178 { $description "Tests if every element of " { $snippet "set1" } " is contained in " { $snippet "set2" } "." }
179 { $notes "If " { $snippet "set1" } " is empty, the result is always " { $link t } "." } ;
180
181 HELP: set=
182 { $values { "set1" set } { "set2" set } { "?" "a boolean" } }
183 { $description "Tests if both sets contain the same elements, disregrading order and duplicates." } ;
184
185 HELP: gather
186 { $values
187      { "seq" sequence } { "quot" quotation }
188      { "newseq" sequence } }
189 { $description "Maps a quotation onto a sequence, concatenates the results of the mapping, and removes duplicates." } ;
190
191 HELP: set-like
192 { $values { "set" set } { "exemplar" set } { "set'" set } }
193 { $description "If the conversion is defined for the exemplar, converts the set into a set of the exemplar's class. This is not guaranteed to create a new set, for example if the input set and exemplar are of the same class." $nl
194 "Set implementations may optionally implement a method on this generic word. The default implementation returns its input set." }
195 { $examples
196     { $example "USING: sets prettyprint ;" "{ 1 2 3 } HS{ } set-like ." "HS{ 1 2 3 }" }
197 } ;
198
199 HELP: within
200 { $values { "seq" sequence } { "set" set } { "subseq" sequence } }
201 { $description "Returns the subsequence of the given sequence consisting of members of the set. This may contain duplicates, if the sequence has duplicates." } ;
202
203 HELP: without
204 { $values { "seq" sequence } { "set" set } { "subseq" sequence } }
205 { $description "Returns the subsequence of the given sequence consisting of things that are not members of the set. This may contain duplicates, if the sequence has duplicates." } ;
206
207 HELP: null?
208 { $values { "set" set } { "?" "a boolean" } }
209 { $description "Tests whether the given set is empty. This outputs " { $snippet "t" } " when given a null set of any type." } ;
210
211 HELP: cardinality
212 { $values { "set" set } { "n" "a non-negative integer" } }
213 { $description "Returns the number of elements in the set. All sets support this operation." } ;
214
215 HELP: combine
216 { $values { "sets" "a sequence of sets" } { "set/f" "a " { $link set } " or " { $link f } } }
217 { $description "Outputs the union of a sequence of sets, or " { $link f } " if the sequence is empty." } ;