The MONO_HASH_TABLE signature defines an interface to imperative hash
tables with monomorphic keys. The SML/NJ Library provides two
specialized implementations of this signature, as well as a functor for
constructing additional implementations.
The tables are implemented as an array of buckets, which are lists of key-value pairs. The number of buckets grows with the number of table entries.
Synopsis
signature MONO_HASH_TABLE
structure AtomTable :> MONO_HASH_TABLE where type Key.hash_key = Atom.atom
structure IntHashTable :> MONO_HASH_TABLE where type Key.hash_key = int
structure WordHashTable :> MONO_HASH_TABLE where type Key.hash_key = word
Interface
structure Key : HASH_KEY
type 'a hash_table
val mkTable : (int * exn) -> 'a hash_table
val clear : 'a hash_table -> unit
val insert : 'a hash_table -> (Key.hash_key * 'a) -> unit
val insertWith : ('a * 'a -> 'a) -> 'a hash_table -> Key.hash_key * 'a -> unit
val insertWithi : (Key.hash_key * 'a * 'a -> 'a)
-> 'a hash_table
-> Key.hash_key * 'a
-> unit
val inDomain : 'a hash_table -> Key.hash_key -> bool
val lookup : 'a hash_table -> Key.hash_key -> 'a
val find : 'a hash_table -> Key.hash_key -> 'a option
val findAndRemove : 'a hash_table -> Key.hash_key -> 'a option
val remove : 'a hash_table -> Key.hash_key -> 'a
val numItems : 'a hash_table -> int
val listItems : 'a hash_table -> 'a list
val listItemsi : 'a hash_table -> (Key.hash_key * 'a) list
val app : ('a -> unit) -> 'a hash_table -> unit
val appi : ((Key.hash_key * 'a) -> unit) -> 'a hash_table -> unit
val map : ('a -> 'b) -> 'a hash_table -> 'b hash_table
val mapi : ((Key.hash_key * 'a) -> 'b) -> 'a hash_table -> 'b hash_table
val fold : (('a * 'b) -> 'b) -> 'b -> 'a hash_table -> 'b
val foldi : ((Key.hash_key * 'a * 'b) -> 'b) -> 'b -> 'a hash_table -> 'b
val modify : ('a -> 'a) -> 'a hash_table -> unit
val modifyi : ((Key.hash_key * 'a) -> 'a) -> 'a hash_table -> unit
val filter : ('a -> bool) -> 'a hash_table -> unit
val filteri : ((Key.hash_key * 'a) -> bool) -> 'a hash_table -> unit
val copy : 'a hash_table -> 'a hash_table
val bucketSizes : 'a hash_table -> int list
Description
structure Key : HASH_KEY-
This substructure defines the type of keys used to index the tables and hash and equality functions on the key type.
type 'a hash_table-
The type of imperative hash tables indexed by
Key.hash_keyvalues val mkTable : (int * exn) -> 'a hash_table-
mkTable (n, ex)creates a new hash table; the table will be initially sized to hold at leastnitems. The exceptionexis raised by thelookupandremovefunctions when the search key is not in the domain. val clear : 'a hash_table -> unit-
clear tblremoves all of the entries in the table. val insert : 'a hash_table -> (Key.hash_key * 'a) -> unit-
insert tbl (key, item)inserts a mapping fromkeytoitemintotbl. Any existing mapping ofkeyis discarded. val insertWith : ('a * 'a → 'a) -> 'a hash_table -> Key.hash_key * 'a -> unit-
insertWith comb (tbl, key, v)adds the mapping fromkeytovaluetotbl, wherevalue = comb(v', v), iftblalready contained a mapping fromkeytov'; otherwise,value = v. val insertWithi : (Key.hash_key * 'a * 'a → 'a) -> 'a hash_table -> Key.hash_key * 'a -> unit-
insertWithi comb (tbl, key, v)adds the mapping fromkeytovaluetotbl, wherevalue = comb(key, v', v), ifmalready contained a mapping fromkeytov'; otherwise,value = v. val inDomain : 'a hash_table -> Key.hash_key -> bool-
inDomain tbl keyreturnstrueif, and only if,keyis in the domain of the table
val lookup : 'a hash_table -> Key.hash_key -> 'a-
lookup tbl keyreturns the item thatkeymaps to ifkeyis in the domain oftbl. Otherwise, the table’s exception is raised. val find : 'a hash_table -> Key.hash_key -> 'a option-
find tbl keyreturns theSOME vifkeyis mapped tovintbl. Otherwise, it returnsNONE. val findAndRemove : 'a hash_table → Key.hash_key → 'a option-
findAndRemove (tbl, key)returnsSOME vand removeskeyfrom the table iftblmapskeytov. Ifkeyis not in the domain oftbl, thenNONEis returned andtblis unchanged.
val remove : 'a hash_table -> Key.hash_key -> 'a-
remove tbl keyreturns the item thatkeymaps to ifkeyis in the domain oftbland removes it from the table. Otherwise, the table’s exception is raised. val numItems : 'a hash_table -> int-
numItems tblreturns the number of entries in the table. val listItems : 'a hash_table -> 'a list-
listItems tblreturns a list of the items in the range oftbl. val listItemsi : 'a hash_table -> (Key.hash_key * 'a) list-
listItemsi tblreturns a list of the key-value entries intbl. val app : ('a -> unit) -> 'a hash_table -> unit-
app f tblapplies the functionfto each item in the range oftbl. val appi : ((Key.hash_key * 'a) -> unit) -> 'a hash_table -> unit-
appi f tblapplies the functionfto each item in the key-value entries intbl. val map : ('a -> 'b) -> 'a hash_table -> 'b hash_table-
map f tblcreates a new table with an entry(key, f(lookup tbl key))in the new table for everykeyintbl. The exception for the new table is copied fromtbl. val mapi : ((Key.hash_key * 'a) -> 'b) -> 'a hash_table -> 'b hash_table-
mapi f tblcreates a new table with an entry(key, f(key, lookup tbl key))in the new table for everykeyintbl. The exception for the new table is copied fromtbl. val fold : (('a * 'b) -> 'b) -> 'b -> 'a hash_table -> 'b-
fold f init tblfolds the functionfover the items in the range oftblusinginitas an initial value. val foldi : ((Key.hash_key * 'a * 'b) -> 'b) -> 'b -> 'a hash_table -> 'b-
foldi f init tblfolds the functionfover the key-value entries intblusinginitas an initial value. val modify : ('a -> 'a) -> 'a hash_table -> unit-
modify f tblapplies the functionffor effect to the items in the range oftbl, replacing the old items with the result of applyingf. val modifyi : ((Key.hash_key * 'a) -> 'a) -> 'a hash_table -> unit-
modifyi f tblapplies the functionffor effect to the key-value entries intbl, replacing the old items with the result of applyingf. val filter : ('a -> bool) -> 'a hash_table -> unit-
filter pred tblremoves any entry(key, item)fromtblfor whichpred itemreturnsfalse. val filteri : ((Key.hash_key * 'a) -> bool) -> 'a hash_table -> unit-
filteri pred tblremoves any entry(key, item)fromtblfor whichpred(key, item)returnsfalse. val copy : 'a hash_table -> 'a hash_table-
copy tblcreates a copy oftbl. This expression is equivalent tomap (fn x => x) tbl val bucketSizes : 'a hash_table -> int list-
bucketSizes tblreturns a list of the current number of items per bucket. This function allows users to gauge the quality of their hashing function.
Instances
structure AtomTable-
This structure implements hash tables keyed by the
Atom.atomtype.
structure IntHashTable-
This structure implements hash tables keyed by the default
inttype.
structure WordHashTable-
This structure implements hash tables keyed by the default
wordtype.