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
k
to values of typev
.
- A hash table mapping keys of type
Methods
val mkTable : (('a -> word) * (('a * 'a) -> bool)) * int -> ('a,'b) hash_table
val clear : ('a, 'b) hash_table -> unit
val insert : ('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 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
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.hash
as the hash function andop=
as the equality comparison for constructing a hash table.HashString.hash
is 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
find
instead.)
- 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
key
from the table. The removed item is returned. If the table does not contain a mapping forkey
an 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
f
to the values in the table.
- Applies the function
appi (f, table)
- Applies the function
f
to the key-value pairs in the table.
- Applies the function
map (f, table)
- Creates a new table with the same keys as
table
but with values determined by the result of applyingf
to 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
table
but with values determined by the result of applyingf
to 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
table
using the functionf
, starting withbase
. The functionf
should take as an argument a tuple of the next value intable
to 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 functionf
should take as an argument a tuple of the next key-value pair intable
to process and the result accumulated so far.
- Combines the key-value pairs of table using the function
modify (f, table)
- Replaces each value in
table
with the result of applyingf
to the original value. This modifiestable
.
- Replaces each value in
modifyi (f, table)
- Replaces each value in
table
with the result of applyingf
to the original key-value pair. This modifiestable
.
- Replaces each value in
filter (f, table)
- Removes all of the mappings in
table
where the value does not makef
true.
- Removes all of the mappings in
filteri (f, table)
- Removes all of the mappings in
table
where the key-value pair does not makef
true.
- 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.