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