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
'a
values 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 thehash
function to compute hash values for keys and thesame
function to test key equality. The table will be initially sized to hold at leastn
items. The exceptionex
is raised by thelookup
andremove
functions when the search key is not in the domain. val clear : ('a, 'b) hash_table -> unit
-
clear tbl
removes all of the entries in the table. val insert : ('a, 'b) hash_table -> ('a * 'b) -> unit
-
insert tbl (key, item)
inserts a mapping fromkey
toitem
intotbl
. Any existing mapping ofkey
is discarded. val insertWith : ('b * 'b → 'b) -> ('a, 'b) hash_table -> 'a * 'b -> unit
-
insertWith comb (tbl, key, v)
adds the mapping fromkey
tovalue
totbl
, wherevalue = comb(v', v)
, iftbl
already contained a mapping fromkey
tov'
; otherwise,value = v
. val insertWithi : ('a * 'b * 'b → 'b) -> ('a, 'b) hash_table -> 'a * 'b -> unit`
-
insertWithi comb (tbl, key, v)
adds the mapping fromkey
tovalue
totbl
, wherevalue = comb(key, v', v)
, ifm
already contained a mapping fromkey
tov'
; otherwise,value = v
. val inDomain : ('a, 'b) hash_table -> 'a -> bool
-
inDomain tbl key
returnstrue
if, and only if,key
is in the domain of the table
val lookup : ('a, 'b) hash_table -> 'a -> 'b
-
lookup tbl key
returns the item thatkey
maps to ifkey
is in the domain oftbl
. Otherwise, the table’s exception is raised. val find : ('a, 'b) hash_table -> 'a -> 'b option
-
find tbl key
returns theSOME v
ifkey
is mapped tov
intbl
. Otherwise, it returnsNONE
. val findAndRemove : ('a, 'b) hash_table → 'a → 'b option
-
findAndRemove (tbl, key)
returnsSOME v
and removeskey
from the table iftbl
mapskey
tov
. Ifkey
is not in the domain oftbl
, thenNONE
is returned andtbl
is unchanged.
val remove : ('a, 'b) hash_table -> 'a -> 'b
-
remove tbl key
returns the item thatkey
maps to ifkey
is in the domain oftbl
and removes it from the table. Otherwise, the table’s exception is raised. val numItems : ('a, 'b) hash_table -> int
-
numItems tbl
returns the number of entries in the table. val listItems : ('a, 'b) hash_table -> 'b list
-
listItems tbl
returns a list of the items in the range oftbl
. val listItemsi : ('a, 'b) hash_table -> ('a * 'b) list
-
listItemsi tbl
returns a list of the key-value entries intbl
. val app : ('b -> unit) -> ('a, 'b) hash_table -> unit
-
app f tbl
applies the functionf
to each item in the range oftbl
. val appi : (('a * 'b) -> unit) -> ('a, 'b) hash_table -> unit
-
appi f tbl
applies the functionf
to each item in the key-value entries intbl
. val map : ('b -> 'c) -> ('a, 'b) hash_table -> ('a, 'c) hash_table
-
map f tbl
creates a new table with an entry(key, f(lookup tbl key))
in the new table for everykey
intbl
. 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 tbl
creates a new table with an entry(key, f(key, lookup tbl key))
in the new table for everykey
intbl
. 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 tbl
folds the functionf
over the items in the range oftbl
usinginit
as an initial value. val foldi : (('a * 'b * 'c) -> 'c) -> 'c -> ('a, 'b) hash_table -> 'c
-
foldi f init tbl
folds the functionf
over the key-velu entries intbl
usinginit
as an initial value. val modify : ('b -> 'b) -> ('a, 'b) hash_table -> unit
-
modify f tbl
applies the functionf
for 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 tbl
applies the functionf
for 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 tbl
removes any entry(key, item)
fromtbl
for whichpred item
returnsfalse
. val filteri : (('a * 'b) -> bool) -> ('a, 'b) hash_table -> unit
-
filteri pred tbl
removes any entry(key, item)
fromtbl
for whichpred(key, item)
returnsfalse
. val copy : ('a, 'b) hash_table -> ('a, 'b) hash_table
-
copy tbl
creates a copy oftbl
. This expression is equivalent tomap (fn x => x) tbl
val bucketSizes : ('a, 'b) hash_table -> int list
-
bucketSizes tbl
returns a list of the current number of items per bucket. This function allows users to gauge the quality of their hashing function.