Set
Import Set for unique, ordered values. Values require Ord(a), and views
traverse them in ascending order. The representation and constructor are
private. Membership and single-value updates are O(log n); ordered traversal
is O(n).
Type
Set
Set(a) stores at most one occurrence of each value of type a.
Construction
setEmpty
setSingleton
setFromList
Inserts values from left to right and removes duplicates. This is
O(n log n).
setToList
Returns values in ascending order in O(n).
Querying
setSize
setIsEmpty
setContains
Updating and combining
setInsert
Inserting a duplicate does not change the set.
setRemove
Removes a value when present. An absent value returns an equivalent set. This
is O(log n).
setUnion
Returns every value present in either set. The implementation is
O(m log(n + m)) for inputs of sizes n and m.
setIntersection
Returns values present in both sets. This is O(n × (log n + log m)) worst
case.
setDifference
Returns values from the first set that are absent from the second. This is
O(n × (log n + log m)) worst case.
setIsSubset
Returns whether every value in the first set occurs in the second. After a
missing value is found, later membership lookups are skipped, but traversal of
the first set continues. This is O(n log m) worst case.
Transforming and traversal
setFilter
Keeps values whose predicate returns True. Callbacks run in ascending order;
rebuilding the result is O(n log n) worst case.
setMap
Transforms values in ascending input order and rebuilds ordering for b.
Duplicate outputs collapse to one value. This is O(n log n) plus callback
work.
setFoldLeft
Folds values from least to greatest in O(n) plus callback work.
setFoldRight
Folds values from greatest to least in O(n) plus callback work.