The ORD_SET signature defines an interface to finite sets of
ordered elements. The SML/NJ Library provides a number of
different implementations of this interface. Functors are
provided for constructing sets for user-defined item types;
in addition, a number of instances for specific types
are also provided.
Synopsis
signature ORD_SET
structure AtomSet : ORD_SET where type Key.ord_key = Atom.atom
structure AtomBinarySet : ORD_SET where type Key.ord_key = Atom.atom
structure AtomRedBlackSet : ORD_SET where type Key.ord_key = Atom.atom
structure IntBinarySet : ORD_SET where type Key.ord_key = int
structure IntListSet : ORD_SET where type Key.ord_key = int
structure IntRedBlackSet : ORD_SET where type Key.ord_key = int
structure WordRedBlackSet : ORD_SET where type Key.ord_key = word
Interface
structure Key : ORD_KEY
type item = Key.ord_key
type set
val empty : set
val singleton : item -> set
val fromList : item list -> set
val toList : set -> item list
val add : set * item -> set
val add' : (item * set) -> set
val addList : set * item list -> set
val subtract : set * item -> set
val subtract' : (item * set) -> set
val subtractList : set * item list -> set
val delete : set * item -> set
val member : set * item -> bool
val isEmpty : set -> bool
val minItem : set -> item
val maxItem : set -> item
val equal : (set * set) -> bool
val compare : (set * set) -> order
val isSubset : (set * set) -> bool
val disjoint : set * set -> bool
val numItems : set -> int
val listItems : set -> item list
val union : set * set -> set
val intersection : set * set -> set
val difference : set * set -> set
val combineWith : (item * bool * bool -> bool) -> set * set -> set
val map : (item -> item) -> set -> set
val mapPartial : (item -> item option) -> set -> set
val app : (item -> unit) -> set -> unit
val foldl : (item * 'b -> 'b) -> 'b -> set -> 'b
val foldr : (item * 'b -> 'b) -> 'b -> set -> 'b
val partition : (item -> bool) -> set -> (set * set)
val filter : (item -> bool) -> set -> set
val exists : (item -> bool) -> set -> bool
val all : (item -> bool) -> set -> bool
val find : (item -> bool) -> set -> item option
Description
`structure Key : ORD_KEY-
This substructure defines the type of elements in the set and the comparison function used to order them.
type item = Key.ord_key-
The type of elements in the set.
type set-
A finite set of
itemvalues. val empty : set-
The empty set.
val singleton : item -> set-
singleton itemreturns a singleton set containingitem. val fromList : item list -> set-
fromList itemsreturns the set containing the list of items.
val toList : set -> item list-
toList setreturns a list of the items inset. The items will be sorted in increasing order. val add : set * item -> set-
add (set, item)adds the item to the set. val add' : (item * set) -> set-
add' (item, set)adds the item to the set. val addList : set * item list -> set-
addList (set, items)adds the list of items to the set. val subtract : set * item -> set-
subtract (set, item)removes the objectitemfromset. Acts as the identity ifitemis not in the set. val subtract' : (item * set) -> set-
subtract' (item, set)removes the objectitemfromset. Acts as the identity ifitemis not in the set. val subtractList : set * item list -> set-
subtractList (set, items)removes the items from the set. val delete : set * item -> set-
delete (set, item)removes the objectitemfromset. Unlikesubtract, thedeletefunction raises theNotFoundexception ifitemis not in the set. val member : set * item -> bool-
member (item, set)returnstrueif, and only if,itemis an element ofset. val isEmpty : set -> bool-
isEmpty setreturns true if, and only if,setis empty. val minItem : set -> item-
minItem setreturns the smallest element of the set. This function raises theEmptyexception if the set is empty. val maxItem : set -> item-
minItem setreturns the largest element of the set. This function raises theEmptyexception if the set is empty. val equal : (set * set) -> bool-
equal (set1, set2)returns true if, and only if, the two sets are equal (i.e., they contain the same elements). val compare : (set * set) -> order-
compare (set1, set2)returns the lexical order of the two sets. val isSubset : (set * set) -> bool-
isSubset (set1, set2)returns true if, and only if,set1is a subset ofset2(i.e., every element ofset1is an element ofset2). val disjoint : set * set -> bool-
equal (set1, set2)returns true if, and only if, the two sets are disjoint (i.e., their intersection is empty). val numItems : set -> int-
numItems setreturns the number of items in theset. val union : set * set -> set-
union (set1, set2)returns the union of the two sets. val intersection : set * set -> set-
intersection (set1, set2)returns the intersection of the two sets. val difference : set * set -> set-
difference (set1, set2)returns the difference of the two sets; i.e., the set of items that are inset1, but not inset2. val combineWith : (item * bool * bool -> bool) -> set * set -> set-
combineWith pred (set1, set2)returns the combination of the two sets, where thepredfunction is used to determine which elements from the input sets are included in the output. For each elementxin the union of the two sets, thepredfunction is called with the argument(x, p1, p2), wherep1is true ifxis a member ofsetandp2is true ifxis a member ofset2. It the call topredreturnstrue, thenxis included in the result set. val map : (item -> item) -> set -> set-
map f setconstructs a new set from the result of applying the functionfto the elements ofset. This expression is equivalent tofromList (List.map f (toList set)) val mapPartial : (item -> item option) -> set -> set`-
mapPartial f setconstructs a new set from the result of applying the functionfto the elements ofset. This expression is equivalent tofromList (List.mapPartial f (toList set)) val app : (item -> unit) -> set -> unit-
app f setapplies the functionfto the items inset. This expression is equivalent toList.app f (toList set) val foldl : (item * 'b -> 'b) -> 'b -> set -> 'b-
foldl f init setfolds the functionfover the items insetin increasing order usinginitas the initial value. This expression is equivalent toList.foldl f init (toList set) val foldr : (item * 'b -> 'b) -> 'b -> set -> 'b-
foldr f init setfolds the functionfover the items insetin decreasing order usinginitas the initial value. This expression is equivalent toList.foldr f init (toList set) val partition : (item -> bool) -> set -> (set * set)-
partition pred setreturns a pair of disjoint sets(tSet, fSet), where the predicatepredreturns true for every element oftSet,falsefor every element offSet, andsetis the union oftSetandfSet. val filter : (item -> bool) -> set -> set-
filter pred setfilters out any elements of set for which the predicatepredreturns false. This expression is equivalent to#1 (partition pred set) val exists : (item -> bool) -> set -> bool-
all pred setreturnstrueif, and only if,pred itemreturns true for all elementsiteminset. Elements are checked in increasing order. val all : (item -> bool) -> set -> bool-
exists pred setreturnstrueif, and only if, there exists an elementiteminsetsuch thatpred itemreturnstrue. Elements are checked in increasing order. val find : (item -> bool) -> set -> item option-
find pred setreturnsSOME itemif there exists an objectitemin the set for whichpred itemreturnstrue; otherwiseNONEis returned. Items are tested in increasing order.
Deprecated functions
The following functions are part of the interface, but have been deprecated.
val listItems : set -> item list`-
Use
toListinstead.
Instances
structure AtomSet-
This structure is an alias for
AtomRedBlackSet.
structure AtomBinarySet-
Sets of atoms implemented using balanced binary trees. Note that it is recommended that one use the
AtomSetstructure as it provides better performance.
structure AtomRedBlackSet-
Sets of atoms implemented using red-black trees.
structure IntBinarySet-
Sets of ints implemented using balanced binary trees. Note that it is recommended that one use the
IntRedBlackSetstructure as it provides better performance.
structure IntListSet-
Sets of words implemented using sorted lists. This implementation is fast for small sets, but does not scale well to large sizes.
structure IntRedBlackSet-
Sets of ints implemented using red-black binary trees.
structure WordRedBlackSet-
Sets of words implemented using red-black binary trees.