The HashTable structure implements hash tables that are polymorphic
in the key type.
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
structure HashTable
Interface
type ('a, 'b) hash_table
val mkTable : (('a -> word) * (('a * 'a) -> bool)) -> (int * exn)
-> ('a,'b) hash_table
val clear : ('a, 'b) hash_table -> unit
val insert : ('a, 'b) hash_table -> ('a * 'b) -> unit
val insertWith : ('b * 'b -> 'b) -> ('a, 'b) hash_table -> 'a * 'b -> unit
val insertWithi : ('a * 'b * 'b -> 'b) -> ('a, 'b) hash_table -> 'a * 'b -> unit
val inDomain : ('a, 'b) hash_table -> 'a -> bool
val lookup : ('a, 'b) hash_table -> 'a -> 'b
val find : ('a, 'b) hash_table -> 'a -> 'b option
val findAndRemove : ('a, 'b) hash_table -> 'a -> 'b option
val remove : ('a, 'b) hash_table -> 'a -> 'b
val numItems : ('a, 'b) hash_table -> int
val listItems : ('a, 'b) hash_table -> 'b list
val listItemsi : ('a, 'b) hash_table -> ('a * 'b) list
val app : ('b -> unit) -> ('a, 'b) hash_table -> unit
val appi : (('a * 'b) -> unit) -> ('a, 'b) hash_table -> unit
val map : ('b -> 'c) -> ('a, 'b) hash_table -> ('a, 'c) hash_table
val mapi : (('a * 'b) -> 'c) -> ('a, 'b) hash_table -> ('a, 'c) hash_table
val fold : (('b *'c) -> 'c) -> 'c -> ('a, 'b) hash_table -> 'c
val foldi : (('a * 'b * 'c) -> 'c) -> 'c -> ('a, 'b) hash_table -> 'c
val modify : ('b -> 'b) -> ('a, 'b) hash_table -> unit
val modifyi : (('a * 'b) -> 'b) -> ('a, 'b) hash_table -> unit
val filter : ('b -> bool) -> ('a, 'b) hash_table -> unit
val filteri : (('a * 'b) -> bool) -> ('a, 'b) hash_table -> unit
val copy : ('a, 'b) hash_table -> ('a, 'b) hash_table
val bucketSizes : ('a, 'b) hash_table -> int list
Description
type ('a, 'b) hash_table-
The type of imperative hash tables indexed by
'avalues val mkTable : 'a -> word) * (('a * 'a) -> bool -> (int * exn) -> ('a,'b) hash_table-
mkTable (hash, same) (n, ex)creates a new hash table that uses thehashfunction to compute hash values for keys and thesamefunction to test key equality. 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, 'b) hash_table -> unit-
clear tblremoves all of the entries in the table. val insert : ('a, 'b) hash_table -> ('a * 'b) -> unit-
insert tbl (key, item)inserts a mapping fromkeytoitemintotbl. Any existing mapping ofkeyis discarded. val insertWith : ('b * 'b → 'b) -> ('a, 'b) hash_table -> 'a * 'b -> unit-
insertWith comb (tbl, key, v)adds the mapping fromkeytovaluetotbl, wherevalue = comb(v', v), iftblalready contained a mapping fromkeytov'; otherwise,value = v. val insertWithi : ('a * 'b * 'b → 'b) -> ('a, 'b) hash_table -> 'a * 'b -> 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, 'b) hash_table -> 'a -> bool-
inDomain tbl keyreturnstrueif, and only if,keyis in the domain of the table
val lookup : ('a, 'b) hash_table -> 'a -> 'b-
lookup tbl keyreturns the item thatkeymaps to ifkeyis in the domain oftbl. Otherwise, the table’s exception is raised. val find : ('a, 'b) hash_table -> 'a -> 'b option-
find tbl keyreturns theSOME vifkeyis mapped tovintbl. Otherwise, it returnsNONE. val findAndRemove : ('a, 'b) hash_table → 'a → 'b option-
findAndRemove (tbl, key)returnsSOME vand removeskeyfrom the table iftblmapskeytov. Ifkeyis not in the domain oftbl, thenNONEis returned andtblis unchanged.
val remove : ('a, 'b) hash_table -> 'a -> 'b-
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, 'b) hash_table -> int-
numItems tblreturns the number of entries in the table. val listItems : ('a, 'b) hash_table -> 'b list-
listItems tblreturns a list of the items in the range oftbl. val listItemsi : ('a, 'b) hash_table -> ('a * 'b) list-
listItemsi tblreturns a list of the key-value entries intbl. val app : ('b -> unit) -> ('a, 'b) hash_table -> unit-
app f tblapplies the functionfto each item in the range oftbl. val appi : (('a * 'b) -> unit) -> ('a, 'b) hash_table -> unit-
appi f tblapplies the functionfto each item in the key-value entries intbl. val map : ('b -> 'c) -> ('a, 'b) hash_table -> ('a, 'c) hash_table-
map f tblcreates a new table with an entry(key, f(lookup tbl key))in the new table for everykeyintbl. The new table inherits its hash and key-equality functions, and exception fromtbl. val mapi : (('a * 'b) -> 'c) -> ('a, 'b) hash_table -> ('a, 'c) hash_table-
mapi f tblcreates a new table with an entry(key, f(key, lookup tbl key))in the new table for everykeyintbl. The new table inherits its hash and key-equality functions, and exception fromtbl. val fold : (('b *'c) -> 'c) -> 'c -> ('a, 'b) hash_table -> 'c-
fold f init tblfolds the functionfover the items in the range oftblusinginitas an initial value. val foldi : (('a * 'b * 'c) -> 'c) -> 'c -> ('a, 'b) hash_table -> 'c-
foldi f init tblfolds the functionfover the key-velu entries intblusinginitas an initial value. val modify : ('b -> 'b) -> ('a, 'b) 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 : (('a * 'b) -> 'b) -> ('a, 'b) 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 : ('b -> bool) -> ('a, 'b) hash_table -> unit-
filter pred tblremoves any entry(key, item)fromtblfor whichpred itemreturnsfalse. val filteri : (('a * 'b) -> bool) -> ('a, 'b) hash_table -> unit-
filteri pred tblremoves any entry(key, item)fromtblfor whichpred(key, item)returnsfalse. val copy : ('a, 'b) hash_table -> ('a, 'b) hash_table-
copy tblcreates a copy oftbl. This expression is equivalent tomap (fn x => x) tbl val bucketSizes : ('a, 'b) 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.