IN: sets
ARTICLE: "sets" "Sets"
-"A set is an unordered list of elements. Words for working with sets are in the " { $vocab-link "sets" } " vocabulary." $nl
+"A set is an unordered collection of elements. Words for working with sets are in the " { $vocab-link "sets" } " vocabulary." $nl
"All sets are instances of a mixin class:"
{ $subsections
- set
- set?
+ unordered-set
+ unordered-set?
}
{ $subsections "set-operations" "set-implementations" } ;
$nl
"As one particular example, " { $link POSTPONE: f } " is a representation of the empty set, since it is an empty sequence." ;
-HELP: set
+HELP: unordered-set
{ $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." } ;
HELP: adjoin
-{ $values { "elt" object } { "set" set } }
+{ $values { "elt" object } { "set" unordered-set } }
{ $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." }
{ $examples
{ $example
{ $side-effects "set" } ;
HELP: ?adjoin
-{ $values { "elt" object } { "set" set } { "?" boolean } }
+{ $values { "elt" object } { "set" unordered-set } { "?" boolean } }
{ $description "A version of " { $link adjoin } " which returns whether the element was added to the set." }
{ $notes "This is slightly less efficient than " { $link adjoin } " due to the initial membership test." } ;
HELP: delete
-{ $values { "elt" object } { "set" set } }
+{ $values { "elt" object } { "set" unordered-set } }
{ $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." }
{ $side-effects "set" } ;
HELP: clear-set
-{ $values { "set" set } }
+{ $values { "set" unordered-set } }
{ $contract "Removes all entries from the set." }
{ $side-effects "set" } ;
HELP: members
-{ $values { "set" set } { "seq" sequence } }
+{ $values { "set" unordered-set } { "seq" sequence } }
{ $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." } ;
HELP: in?
-{ $values { "elt" object } { "set" set } { "?" boolean } }
+{ $values { "elt" object } { "set" unordered-set } { "?" boolean } }
{ $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." } ;
HELP: adjoin-at
{ $side-effects "assoc" } ;
HELP: duplicates
-{ $values { "set" set } { "seq" sequence } }
+{ $values { "set" unordered-set } { "seq" sequence } }
{ $description "Outputs a sequence consisting of elements which occur more than once in " { $snippet "set" } "." }
{ $examples
{ $example "USING: sets prettyprint ;" "{ 1 2 3 1 2 1 } duplicates ." "{ 1 2 1 }" }
} ;
HELP: all-unique?
-{ $values { "set" set } { "?" boolean } }
+{ $values { "set" unordered-set } { "?" boolean } }
{ $description "Tests whether a set contains any repeated elements." }
{ $example
"USING: sets prettyprint ;"
} ;
HELP: diff
-{ $values { "set1" set } { "set2" set } { "set" set } }
+{ $values { "set1" unordered-set } { "set2" unordered-set } { "set" unordered-set } }
{ $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."
} { $examples
{ $example "USING: sets prettyprint ;" "{ 1 2 3 } { 2 3 4 } diff ." "{ 1 }" }
} ;
HELP: intersect
-{ $values { "set1" set } { "set2" set } { "set" set } }
+{ $values { "set1" unordered-set } { "set2" unordered-set } { "set" unordered-set } }
{ $description "Outputs a set consisting of elements present in both " { $snippet "set1" } " and " { $snippet "set2" } "."
"This word has a default definition which works for all sets, but set implementations may override the default for efficiency." }
{ $examples
} ;
HELP: intersection
-{ $values { "sets" sequence } { "set/f" { $maybe set } } }
+{ $values { "sets" sequence } { "set/f" { $maybe unordered-set } } }
{ $description "Outputs the intersection of all the sets of the sequence " { $snippet "sets" } ", or " { $link f } " if " { $snippet "sets" } " is empty." } ;
HELP: union
-{ $values { "set1" set } { "set2" set } { "set" set } }
+{ $values { "set1" unordered-set } { "set2" unordered-set } { "set" unordered-set } }
{ $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." }
{ $examples
{ $example "USING: sets prettyprint ;" "{ 1 2 3 } { 2 3 4 } union ." "{ 1 2 3 4 }" }
{ diff intersect union } related-words
HELP: union!
-{ $values { "set1" set } { "set2" set } }
+{ $values { "set1" unordered-set } { "set2" unordered-set } }
{ $description "Adds all members from " { $snippet "set2" } " to " { $snippet "set1" } "." }
{ $side-effects "set1" } ;
HELP: diff!
-{ $values { "set1" set } { "set2" set } }
+{ $values { "set1" unordered-set } { "set2" unordered-set } }
{ $description "Removes all members from " { $snippet "set1" } " contained in " { $snippet "set2" } "." }
{ $side-effects "set1" } ;
HELP: intersect!
-{ $values { "set1" set } { "set2" set } }
+{ $values { "set1" unordered-set } { "set2" unordered-set } }
{ $description "Removes all members from " { $snippet "set1" } " not contained in " { $snippet "set2" } "." }
{ $side-effects "set1" } ;
HELP: intersects?
-{ $values { "set1" set } { "set2" set } { "?" boolean } }
+{ $values { "set1" unordered-set } { "set2" unordered-set } { "?" boolean } }
{ $description "Tests if " { $snippet "set1" } " and " { $snippet "set2" } " have any elements in common." }
{ $notes "If one of the sets is empty, the result is always " { $link f } "." } ;
HELP: subset?
-{ $values { "set1" set } { "set2" set } { "?" boolean } }
+{ $values { "set1" unordered-set } { "set2" unordered-set } { "?" boolean } }
{ $description "Tests if every element of " { $snippet "set1" } " is contained in " { $snippet "set2" } "." }
{ $notes "If " { $snippet "set1" } " is empty, the result is always " { $link t } "." } ;
HELP: set=
-{ $values { "set1" set } { "set2" set } { "?" boolean } }
+{ $values { "set1" unordered-set } { "set2" unordered-set } { "?" boolean } }
{ $description "Tests if both sets contain the same elements, disregrading order and duplicates." } ;
HELP: gather
{ $description "Maps a quotation onto a sequence, concatenates the results of the mapping, and removes duplicates." } ;
HELP: set-like
-{ $values { "set" set } { "exemplar" set } { "set'" set } }
+{ $values { "set" unordered-set } { "exemplar" unordered-set } { "set'" unordered-set } }
{ $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." }
{ $examples
{ $example "USING: sets prettyprint ;" "{ 1 2 3 } HS{ } set-like ." "HS{ 1 2 3 }" }
} ;
HELP: within
-{ $values { "seq" sequence } { "set" set } { "subseq" sequence } }
+{ $values { "seq" sequence } { "set" unordered-set } { "subseq" sequence } }
{ $description "Returns the subsequence of the given sequence consisting of members of the set. This may contain duplicates, if the sequence has duplicates." } ;
HELP: without
-{ $values { "seq" sequence } { "set" set } { "subseq" sequence } }
+{ $values { "seq" sequence } { "set" unordered-set } { "subseq" sequence } }
{ $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." } ;
HELP: null?
-{ $values { "set" set } { "?" boolean } }
+{ $values { "set" unordered-set } { "?" boolean } }
{ $description "Tests whether the given set is empty. This outputs " { $snippet "t" } " when given a null set of any type." } ;
HELP: cardinality
-{ $values { "set" set } { "n" "a non-negative integer" } }
+{ $values { "set" unordered-set } { "n" "a non-negative integer" } }
{ $description "Returns the number of elements in the set. All sets support this operation." } ;
HELP: combine
-{ $values { "sets" "a sequence of sets" } { "set/f" { $maybe set } } }
+{ $values { "sets" "a sequence of sets" } { "set/f" { $maybe unordered-set } } }
{ $description "Outputs the union of a sequence of sets, or " { $link f } " if the sequence is empty." } ;
USING: assocs hashtables kernel math sequences vectors ;
IN: sets
-! Set protocol
-MIXIN: set
+! unordered-set protocol
+! The word name ``set`` is for dynamic variables.
+MIXIN: unordered-set
GENERIC: adjoin ( elt set -- )
GENERIC: ?adjoin ( elt set -- ? )
GENERIC: in? ( elt set -- ? )
! Defaults for some methods.
! Override them for efficiency
-M: set ?adjoin 2dup in? [ 2drop f ] [ adjoin t ] if ;
+M: unordered-set ?adjoin 2dup in? [ 2drop f ] [ adjoin t ] if ;
-M: set null? members null? ; inline
+M: unordered-set null? members null? ; inline
-M: set cardinality members length ;
+M: unordered-set cardinality members length ;
-M: set clear-set [ members ] keep [ delete ] curry each ;
+M: unordered-set clear-set [ members ] keep [ delete ] curry each ;
-M: set set-like drop ; inline
+M: unordered-set set-like drop ; inline
<PRIVATE
PRIVATE>
-M: set union [ (union) ] keep set-like ;
+M: unordered-set union [ (union) ] keep set-like ;
<PRIVATE
PRIVATE>
-M: set intersect [ (intersect) ] keep set-like ;
+M: unordered-set intersect [ (intersect) ] keep set-like ;
-M: set diff [ (diff) ] keep set-like ;
+M: unordered-set diff [ (diff) ] keep set-like ;
-M: set intersects?
+M: unordered-set intersects?
small/large sequence/tester any? ;
<PRIVATE
PRIVATE>
-M: set subset?
+M: unordered-set subset?
2dup [ cardinality ] bi@ > [ 2drop f ] [ (subset?) ] if ;
-M: set set=
+M: unordered-set set=
2dup [ cardinality ] bi@ eq? [ (subset?) ] [ 2drop f ] if ;
-M: set fast-set ;
+M: unordered-set fast-set ;
-M: set duplicates drop f ;
+M: unordered-set duplicates drop f ;
-M: set all-unique? drop t ;
+M: unordered-set all-unique? drop t ;
<PRIVATE
PRIVATE>
! Sequences are sets
-INSTANCE: sequence set
+INSTANCE: sequence unordered-set
M: sequence in?
member? ; inline
io.encodings.utf8 kernel libc linked-assocs locals make math
math.parser namespaces sequences sets strings yaml.config
yaml.conversion yaml.ffi hash-sets.identity ;
-FROM: sets => set ;
IN: yaml
ERROR: libyaml-parser-error
M: sequence (deref-aliases)
[ (deref-aliases) ] with map! ;
-M: set (deref-aliases)
+M: unordered-set (deref-aliases)
[ members (deref-aliases) ] [ clear-set ] [ swap union! ] tri ;
: assoc-map! ( assoc quot -- assoc' )
M: sequence (replace-aliases)
[ ?replace-aliases ] with map ;
-M: set (replace-aliases)
+M: unordered-set (replace-aliases)
[ members (replace-aliases) ] keep set-like ;
M: assoc (replace-aliases)
M: sequence (replace-anchors)
[ ?replace-anchors ] with map ;
-M: set (replace-anchors)
+M: unordered-set (replace-anchors)
[ members ?replace-anchors ] keep set-like ;
M: assoc (replace-anchors)
[ nip emit-assoc-body ]
[ 2drop emit-assoc-end ] 4tri ;
-M: set emit-value ( emitter event anchor set -- )
+M: unordered-set emit-value ( emitter event anchor set -- )
[ drop YAML_SET_TAG f emit-assoc-start ]
[ nip emit-set-body ]
[ 2drop emit-assoc-end ] 4tri ;