Hash Tables (Dictionaries)
This library provides mutable hash-based dictionary data structures called hash
tables. The module HashString defines a hash function for strings for use with
hash tables. The module HashTable provides the hash table type and operations.
The implementation is based on the hash tables from SML/NJ.
Basic Usage
(* Crate an empty hash table. *)
val table = HashTable.mkTable(HashString.hashString, op=, 10);
val x = table.find(table, "hello");
(* val x = NONE *)
val _ = HashTable.insert(table, ("hello", 42));
val y = HashTable.find(table, "hello");
(* val y = SOME 42 *)
val _ = HashTable.insert(table, ("hello", 43));
val z = HashTable.find(table, "hello");
(* val z = SOME 43 *)
val _ = HashTable.remove(table, "hello");
val w = HashTable.find(table, "hello");
(* val w = NONE *)
HashString
Interface
val hashString: string -> word
Description
hashString str- Hash a string to a word
HashTable
Interface
Types
type ('k, 'v) hash_table- A hash table mapping keys of type
kto values of typev.
- A hash table mapping keys of type
Methods
val mkTable : (('a -> word) * (('a * 'a) -> bool)) * int -> ('a,'b) hash_tableval clear : ('a, 'b) hash_table -> unitval insert : ('a, 'b) hash_table * ('a * 'b) -> unitval inDomain : ('a, 'b) hash_table * 'a -> boolval lookup : ('a, 'b) hash_table * 'a -> 'bval find : ('a, 'b) hash_table * 'a -> 'b optionval remove : ('a, 'b) hash_table * 'a -> 'bval numItems : ('a, 'b) hash_table -> intval listItems : ('a, 'b) hash_table -> 'b listval listItemsi : ('a, 'b) hash_table -> ('a * 'b) listval app : ('b -> unit) * ('a, 'b) hash_table -> unitval appi : (('a * 'b) -> unit) * ('a, 'b) hash_table -> unitval map : ('b -> 'c) * ('a, 'b) hash_table -> ('a, 'c) hash_tableval mapi : (('a * 'b) -> 'c) * ('a, 'b) hash_table -> ('a, 'c) hash_tableval fold : (('b *'c) -> 'c) * 'c * ('a, 'b) hash_table -> 'cval foldi : (('a * 'b * 'c) -> 'c) * 'c * ('a, 'b) hash_table -> 'cval modify : ('b -> 'b) * ('a, 'b) hash_table -> unitval modifyi : (('a * 'b) -> 'b) * ('a, 'b) hash_table -> unitval filter : ('b -> bool) * ('a, 'b) hash_table -> unitval filteri : (('a * 'b) -> bool) * ('a, 'b) hash_table -> unitval copy : ('a, 'b) hash_table -> ('a, 'b) hash_tableval bucketSizes : ('a, 'b) hash_table -> int list
Method Overview
hash_table (hashf, eq, len)- Creates a new empty hash table, using the given hashing function and
equality predicate and initial hash table capacity. Most of the time (when
using structural equality), one can use
MLton.hashas the hash function andop=as the equality comparison for constructing a hash table.HashString.hashis a hash function that is specialized for strings.
- Creates a new empty hash table, using the given hashing function and
equality predicate and initial hash table capacity. Most of the time (when
using structural equality), one can use
clear table- Removes all elements from the table. This modifies
table.
- Removes all elements from the table. This modifies
insert (table, (key, value))- Inserts an item into the table. If the key is already mapped to a value, the
value is replaced. This modifies
table.
- Inserts an item into the table. If the key is already mapped to a value, the
value is replaced. This modifies
inDomain (table, key)- Determines if the table maps the key to some value.
lookup (table, key)- Finds the value associated with a key in the table. If the table does not
map the key to a value, an exception is raised. (It is recommended to use
findinstead.)
- Finds the value associated with a key in the table. If the table does not
map the key to a value, an exception is raised. (It is recommended to use
find (table, key)- Finds the value associated with a key in the table. If the table maps the
key to the value
v, then the result asSOME v. If the table does not map the key to a value, the result isNONE.
- Finds the value associated with a key in the table. If the table maps the
key to the value
remove (table, key)- Removes the mapping for
keyfrom the table. The removed item is returned. If the table does not contain a mapping forkeyan exception is raised. This modifiestable.
- Removes the mapping for
numItems table- Returns the number of keys mapped by the table.
listItems table- Returns a list of the values mapped to by keys in the table.
listItemsi table- Returns an association list containing the mappings (pairs of keys and values) in the table.
app (f, table)- Applies the function
fto the values in the table.
- Applies the function
appi (f, table)- Applies the function
fto the key-value pairs in the table.
- Applies the function
map (f, table)- Creates a new table with the same keys as
tablebut with values determined by the result of applyingfto the values in the original table.
- Creates a new table with the same keys as
mapi (f, table)- Creates a new table with the same keys as
tablebut with values determined by the result of applyingfto the key-value pairs in the original table.
- Creates a new table with the same keys as
fold (f, base, table)- Combines the values of
tableusing the functionf, starting withbase. The functionfshould take as an argument a tuple of the next value intableto process and the result accumulated so far.
- Combines the values of
foldi (f, default, table)- Combines the key-value pairs of table using the function
f, starting withbase. The functionfshould take as an argument a tuple of the next key-value pair intableto process and the result accumulated so far.
- Combines the key-value pairs of table using the function
modify (f, table)- Replaces each value in
tablewith the result of applyingfto the original value. This modifiestable.
- Replaces each value in
modifyi (f, table)- Replaces each value in
tablewith the result of applyingfto the original key-value pair. This modifiestable.
- Replaces each value in
filter (f, table)- Removes all of the mappings in
tablewhere the value does not makeftrue.
- Removes all of the mappings in
filteri (f, table)- Removes all of the mappings in
tablewhere the key-value pair does not makeftrue.
- Removes all of the mappings in
copy table- Creates a new copy of
table.
- Creates a new copy of
bucketSizes table- Returns a list of the sizes buckets used in the table. This is for debugging and improving the hash functions used with hash tables.