]> gitweb.factorcode.org Git - factor.git/blob - core/sets/sets-docs.factor
fix more typos in docs.
[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 collection 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     diff!
30     intersect!
31 }
32 "To test if a set is the empty set:"
33 { $subsections null? }
34 "Basic mathematical operations, which any type of set may override for efficiency:"
35 { $subsections
36     diff
37     intersect
38     union
39 }
40 "Mathematical predicates on sets, which may be overridden for efficiency:"
41 { $subsections
42     intersects?
43     subset?
44     set=
45 }
46 "An optional generic word for creating sets of the same class as a given set:"
47 { $subsections set-like }
48 "An optional generic word for creating a set with a fast lookup operation, if the set itself has a slow lookup operation:"
49 { $subsections fast-set }
50 "For set types that allow duplicates, like sequence sets, some additional words test for duplication:"
51 { $subsections
52     all-unique?
53     duplicates
54 }
55 "Utilities for sets and sequences:"
56 { $subsections
57      within
58      without
59 } ;
60
61 ARTICLE: "set-implementations" "Set implementations"
62 "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."
63 { $subsections
64     "sequence-sets"
65     "hash-sets"
66     "bit-sets"
67 } ;
68
69 ARTICLE: "sequence-sets" "Sequences as sets"
70 "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."
71 $nl
72 "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."
73 $nl
74 "As one particular example, " { $link POSTPONE: f } " is a representation of the empty set, since it is an empty sequence." ;
75
76 HELP: set
77 { $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." } ;
78
79 HELP: adjoin
80 { $values { "elt" object } { "set" set } }
81 { $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." }
82 { $examples
83     { $example
84         "USING: prettyprint sets kernel ;"
85         "V{ \"beans\" \"salsa\" \"cheese\" } clone"
86         "\"nachos\" over adjoin"
87         "\"salsa\" over adjoin"
88         "."
89         "V{ \"beans\" \"cheese\" \"nachos\" \"salsa\" }"
90     }
91 }
92 { $side-effects "set" } ;
93
94 HELP: ?adjoin
95 { $values { "elt" object } { "set" set } { "?" boolean } }
96 { $description "A version of " { $link adjoin } " which returns whether the element was added to the set." } ;
97
98 HELP: delete
99 { $values { "elt" object } { "set" set } }
100 { $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." }
101 { $side-effects "set" } ;
102
103 HELP: ?delete
104 { $values { "elt" object } { "set" set } { "?" boolean } }
105 { $description "A version of " { $link delete } " which returns whether the element was removed from the set." } ;
106
107 HELP: clear-set
108 { $values { "set" set } }
109 { $contract "Removes all entries from the set." }
110 { $side-effects "set" } ;
111
112 HELP: members
113 { $values { "set" set } { "seq" sequence } }
114 { $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." } ;
115
116 HELP: in?
117 { $values { "elt" object } { "set" set } { "?" boolean } }
118 { $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." } ;
119
120 HELP: adjoin-at
121 { $values { "value" object } { "key" object } { "assoc" assoc } }
122 { $description "Adds " { $snippet "value" } " to the set stored at " { $snippet "key" } " of " { $snippet "assoc" } "." }
123 { $side-effects "assoc" } ;
124
125 HELP: duplicates
126 { $values { "set" set } { "seq" sequence } }
127 { $description "Outputs a sequence consisting of elements which occur more than once in " { $snippet "set" } "." }
128 { $examples
129     { $example "USING: sets prettyprint ;" "{ 1 2 3 1 2 1 } duplicates ." "{ 1 2 1 }" }
130 } ;
131
132 HELP: all-unique?
133 { $values { "set" set } { "?" boolean } }
134 { $description "Tests whether a set contains any repeated elements." }
135 { $example
136     "USING: sets prettyprint ;"
137     "{ 0 1 1 2 3 5 } all-unique? ."
138     "f"
139 } ;
140
141 HELP: diff
142 { $values { "set1" set } { "set2" set } { "set" set } }
143 { $description "Outputs a set consisting of elements present in " { $snippet "set1" } " but not " { $snippet "set2" } ", comparing elements for equality." $nl "This word has a default definition which works for all sets, but set implementations may override the default for efficiency."
144 } { $examples
145     { $example "USING: sets prettyprint ;" "{ 1 2 3 } { 2 3 4 } diff ." "{ 1 }" }
146 } ;
147
148 HELP: intersect
149 { $values { "set1" set } { "set2" set } { "set" set } }
150 { $description "Outputs a set consisting of elements present in both " { $snippet "set1" } " and " { $snippet "set2" } "."
151 "This word has a default definition which works for all sets, but set implementations may override the default for efficiency." }
152 { $examples
153     { $example "USING: sets prettyprint ;" "{ 1 2 3 } { 2 3 4 } intersect ." "{ 2 3 }" }
154 } ;
155
156 HELP: intersection
157 { $values { "sets" sequence } { "set/f" { $maybe set } } }
158 { $description "Outputs the intersection of all the sets of the sequence " { $snippet "sets" } ", or " { $link f } " if " { $snippet "sets" } " is empty." } ;
159
160 HELP: union
161 { $values { "set1" set } { "set2" set } { "set" set } }
162 { $description "Outputs a set consisting of elements present in either " { $snippet "set1" } " or " { $snippet "set2" } " which does not contain duplicate values." $nl "This word has a default definition which works for all sets, but set implementations may override the default for efficiency." }
163 { $examples
164     { $example "USING: sets prettyprint ;" "{ 1 2 3 } { 2 3 4 } union ." "{ 1 2 3 4 }" }
165 } ;
166
167 { diff intersect union } related-words
168
169 HELP: union!
170 { $values { "set1" set } { "set2" set } }
171 { $description "Adds all members from " { $snippet "set2" } " to " { $snippet "set1" } "." }
172 { $side-effects "set1" } ;
173
174 HELP: diff!
175 { $values { "set1" set } { "set2" set } }
176 { $description "Removes all members from " { $snippet "set1" } " contained in " { $snippet "set2" } "." }
177 { $side-effects "set1" } ;
178
179 HELP: intersect!
180 { $values { "set1" set } { "set2" set } }
181 { $description "Removes all members from " { $snippet "set1" } " not contained in " { $snippet "set2" } "." }
182 { $side-effects "set1" } ;
183
184 HELP: intersects?
185 { $values { "set1" set } { "set2" set } { "?" boolean } }
186 { $description "Tests if " { $snippet "set1" } " and " { $snippet "set2" } " have any elements in common." }
187 { $notes "If one of the sets is empty, the result is always " { $link f } "." } ;
188
189 HELP: subset?
190 { $values { "set1" set } { "set2" set } { "?" boolean } }
191 { $description "Tests if every element of " { $snippet "set1" } " is contained in " { $snippet "set2" } "." }
192 { $notes "If " { $snippet "set1" } " is empty, the result is always " { $link t } "." } ;
193
194 HELP: set=
195 { $values { "set1" set } { "set2" set } { "?" boolean } }
196 { $description "Tests if both sets contain the same elements, disregarding order and duplicates." } ;
197
198 HELP: gather
199 { $values
200      { "seq" sequence } { "quot" quotation }
201      { "newseq" sequence } }
202 { $description "Maps a quotation onto a sequence, concatenates the results of the mapping, and removes duplicates." } ;
203
204 HELP: set-like
205 { $values { "set" set } { "exemplar" set } { "set'" set } }
206 { $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 "Set implementations may optionally implement a method on this generic word. The default implementation returns its input set." }
207 { $examples
208     { $example "USING: sets prettyprint ;" "{ 1 2 3 } HS{ } set-like ." "HS{ 1 2 3 }" }
209 } ;
210
211 HELP: within
212 { $values { "seq" sequence } { "set" set } { "subseq" sequence } }
213 { $description "Returns the subsequence of the given sequence consisting of members of the set. This may contain duplicates, if the sequence has duplicates." } ;
214
215 HELP: without
216 { $values { "seq" sequence } { "set" set } { "subseq" sequence } }
217 { $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." } ;
218
219 HELP: null?
220 { $values { "set" set } { "?" boolean } }
221 { $description "Tests whether the given set is empty. This outputs " { $snippet "t" } " when given a null set of any type." } ;
222
223 HELP: cardinality
224 { $values { "set" set } { "n" "a non-negative integer" } }
225 { $description "Returns the number of elements in the set. All sets support this operation." } ;
226
227 HELP: combine
228 { $values { "sets" { $sequence set } } { "set/f" { $maybe set } } }
229 { $description "Outputs the union of a sequence of sets, or " { $link f } " if the sequence is empty." } ;
230
231 HELP: refine
232 { $values { "sets" { $sequence set } } { "set/f" { $maybe set } } }
233 { $description "Outputs the intersection of a sequence of sets, or " { $link f } " if the sequence is empty." } ;