Hash Sets
This library provides a HashTable-based mutable set implementation.
The HashSet functor takes as arguments the type of the elements of the hash set, the hashing function, and an equality comparison.
functor HashSet(type elem; val hash : elem -> word; val eq : eleme * elem -> bool) where type elem = elem
Most of the time (when using structural equality), one can use MLton.hash as
the hash function and op= as the equality comparison. See the basic usage
below for an example.
Basic Usage
structure Set = HashSet(type elem = int; val hash = MLton.hash; val eq = op=)
val set = Set.makeEmpty ()
val _ = Set.addAllFromList (set, [1,3,5])
val has4 = Set.contains (set, 4)
val _ = print ("4: " ^ Bool.toString has4 ^ "\n")
val has5 = Set.contains (set, 5)
val _ = print ("5: " ^ Bool.toString has5 ^ "\n")
val _ = Set.remove (set, 5)
val has5 = Set.contains (set, 5)
val _ = print ("5: " ^ Bool.toString has5 ^ "\n")
This results in the output
4: false
5: true
5: false
Interface
Types
type elem- The type of the elements in the set
type t- The type of the set itself
Methods
val makeEmpty : unit -> tval numItems : t -> intval isEmpty : t -> boolval fold : (elem * 'b -> 'b) * t * 'b -> 'bval all : (elem -> bool) * t -> boolval exists : (elem -> bool) * t -> boolval app : (elem -> unit) * t -> unitval filter : (elem -> bool) * t -> unitval contains : t * elem -> boolval containsAll : t * t -> boolval containsAllFromList : t * elem list -> boolval add : t * elem -> unitval addAll : t * t -> unitval addAllFromList : t * elem list -> unitval remove : t * elem -> unitval removeAll : t * t -> unitval removeAllFromList : t * elem list -> unitval equals : t * t -> boolval toList : t -> elem listval fromList : elem list -> tval copy : t -> t
Method Overview
makeEmpty ()- Creates a new empty set.
numItems set- Returns how many elements are in
set.
- Returns how many elements are in
isEmpty set- Returns
trueifsetis empty andfalseotherwise.
- Returns
fold (f, set, base)- Combines the elements of
setusing the functionf, starting withbase. The functionfshould take as an argument a tuple of the next element ofsetto process and the result accumulated so far.
- Combines the elements of
all (f, set)- Determines whether all of the elements of
setsatisfy the predicatef(i.e., whetherfreturns true for all of the elements).
- Determines whether all of the elements of
exists (f, set)- Determines whether any of the elements of
setsatisfy the predicatef(i.e., whetherfreturns true for at least one of the elements).
- Determines whether any of the elements of
app (f, set)- Applies
fto each element insetfor the purpose of the side effects. The order in whichfwill be applied is not guaranteed.
- Applies
filter (f, set)- Removes from
setall of the elements that do not pass the predicatef.
- Removes from
contains (set, e)- Determines whether
setcontains the elemente.
- Determines whether
containsAll (set, elems)- Determines whether
setcontains all of the elements ofelems. In other words, determines whetherelemsis a subset ofset.
- Determines whether
containsAllFromList (set, elems)- Determines whether
setcontains all of the elements ofelems.
- Determines whether
add (set, e)- Adds the element
eto the setset. This modifies the set.
- Adds the element
addAll (set, elems)- Adds all of the elements of
elemsto the setset. This modifiessetto be the union ofsetandelems.
- Adds all of the elements of
addAllFromList (set, elems)- Adds all of the elements of
elemsto the setset. This modifiesset.
- Adds all of the elements of
remove (set, e)- Removes the element
efromset. This modifiesset.
- Removes the element
removeAll (set, elems)- Removes all of the elements of
elemsfromset. This modifiessetto be the set difference ofsetandelems.
- Removes all of the elements of
removeAllFromList (set, elems)- Removes all of the elements of
elemsfromset. This modifiesset.
- Removes all of the elements of
equals (lhs, rhs)- Determines whether the two sets are equal. Two sets are equal if they contain the same elements.
toList set- Creates a list containing the elements of
set.
- Creates a list containing the elements of
fromList elems- Creates a set containing the elements in the list.
copy set- Creates a copy of the set. Modifications to the copy do not affect the original set, nor do modifications to the original set affect the copy.