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