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 -> t
val numItems : t -> int
val isEmpty : t -> bool
val fold : (elem * 'b -> 'b) * t * 'b -> 'b
val all : (elem -> bool) * t -> bool
val exists : (elem -> bool) * t -> bool
val app : (elem -> unit) * t -> unit
val filter : (elem -> bool) * t -> unit
val contains : t * elem -> bool
val containsAll : t * t -> bool
val containsAllFromList : t * elem list -> bool
val add : t * elem -> unit
val addAll : t * t -> unit
val addAllFromList : t * elem list -> unit
val remove : t * elem -> unit
val removeAll : t * t -> unit
val removeAllFromList : t * elem list -> unit
val equals : t * t -> bool
val toList : t -> elem list
val fromList : elem list -> t
val 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
true
ifset
is empty andfalse
otherwise.
- Returns
fold (f, set, base)
- Combines the elements of
set
using the functionf
, starting withbase
. The functionf
should take as an argument a tuple of the next element ofset
to process and the result accumulated so far.
- Combines the elements of
all (f, set)
- Determines whether all of the elements of
set
satisfy the predicatef
(i.e., whetherf
returns true for all of the elements).
- Determines whether all of the elements of
exists (f, set)
- Determines whether any of the elements of
set
satisfy the predicatef
(i.e., whetherf
returns true for at least one of the elements).
- Determines whether any of the elements of
app (f, set)
- Applies
f
to each element inset
for the purpose of the side effects. The order in whichf
will be applied is not guaranteed.
- Applies
filter (f, set)
- Removes from
set
all of the elements that do not pass the predicatef
.
- Removes from
contains (set, e)
- Determines whether
set
contains the elemente
.
- Determines whether
containsAll (set, elems)
- Determines whether
set
contains all of the elements ofelems
. In other words, determines whetherelems
is a subset ofset
.
- Determines whether
containsAllFromList (set, elems)
- Determines whether
set
contains all of the elements ofelems
.
- Determines whether
add (set, e)
- Adds the element
e
to the setset
. This modifies the set.
- Adds the element
addAll (set, elems)
- Adds all of the elements of
elems
to the setset
. This modifiesset
to be the union ofset
andelems
.
- Adds all of the elements of
addAllFromList (set, elems)
- Adds all of the elements of
elems
to the setset
. This modifiesset
.
- Adds all of the elements of
remove (set, e)
- Removes the element
e
fromset
. This modifiesset
.
- Removes the element
removeAll (set, elems)
- Removes all of the elements of
elems
fromset
. This modifiesset
to be the set difference ofset
andelems
.
- Removes all of the elements of
removeAllFromList (set, elems)
- Removes all of the elements of
elems
fromset
. 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.