1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset= ISO-8859-1">
<TITLE>
Module Hashtbl: hash tables and hash functions
</TITLE>
</HEAD>
<BODY >
<A HREF="manual040.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual042.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<A HREF="manual030.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<HR>
<H2>17.11 Module <TT>Hashtbl</TT>: hash tables and hash functions</H2><A NAME="s:Hashtbl"></A>
<A NAME="@manual323"></A><BLOCKQUOTE>
Hash tables are hashed association tables, with in-place modification.
</BLOCKQUOTE>
<H3>Generic interface </H3>
<PRE>
type ('a, 'b) t
</PRE>
<BLOCKQUOTE>
The type of hash tables from type <CODE>'a</CODE> to type <CODE>'b</CODE>.
</BLOCKQUOTE>
<PRE>
val create : int -> ('a,'b) t
</PRE>
<A NAME="@manual324"></A><BLOCKQUOTE>
<CODE>Hashtbl.create n</CODE> creates a new, empty hash table, with
initial size <CODE>n</CODE>. The table grows as needed, so <CODE>n</CODE> is
just an initial guess. Better results are said to be
achieved when <CODE>n</CODE> is a prime number.
</BLOCKQUOTE>
<PRE>
val clear : ('a, 'b) t -> unit
</PRE>
<A NAME="@manual325"></A><BLOCKQUOTE>
Empty a hash table.
</BLOCKQUOTE>
<PRE>
val add : ('a, 'b) t -> 'a -> 'b -> unit
</PRE>
<A NAME="@manual326"></A><BLOCKQUOTE>
<CODE>Hashtbl.add tbl x y</CODE> adds a binding of <CODE>x</CODE> to <CODE>y</CODE> in table <CODE>tbl</CODE>.
Previous bindings for <CODE>x</CODE> are not removed, but simply
hidden. That is, after performing <CODE>Hashtbl.remove tbl x</CODE>,
the previous binding for <CODE>x</CODE>, if any, is restored.
(Same behavior as with association lists.)
</BLOCKQUOTE>
<PRE>
val find : ('a, 'b) t -> 'a -> 'b
</PRE>
<A NAME="@manual327"></A><BLOCKQUOTE>
<CODE>Hashtbl.find tbl x</CODE> returns the current binding of <CODE>x</CODE> in <CODE>tbl</CODE>,
or raises <CODE>Not_found</CODE> if no such binding exists.
</BLOCKQUOTE>
<PRE>
val find_all : ('a, 'b) t -> 'a -> 'b list
</PRE>
<A NAME="@manual328"></A><BLOCKQUOTE>
<CODE>Hashtbl.find_all tbl x</CODE> returns the list of all data
associated with <CODE>x</CODE> in <CODE>tbl</CODE>.
The current binding is returned first, then the previous
bindings, in reverse order of introduction in the table.
</BLOCKQUOTE>
<PRE>
val mem : ('a, 'b) t -> 'a -> bool
</PRE>
<A NAME="@manual329"></A><BLOCKQUOTE>
<CODE>Hashtbl.mem tbl x</CODE> checks if <CODE>x</CODE> is bound in <CODE>tbl</CODE>.
</BLOCKQUOTE>
<PRE>
val remove : ('a, 'b) t -> 'a -> unit
</PRE>
<A NAME="@manual330"></A><BLOCKQUOTE>
<CODE>Hashtbl.remove tbl x</CODE> removes the current binding of <CODE>x</CODE> in <CODE>tbl</CODE>,
restoring the previous binding if it exists.
It does nothing if <CODE>x</CODE> is not bound in <CODE>tbl</CODE>.
</BLOCKQUOTE>
<PRE>
val iter : ('a -> 'b -> unit) -> ('a, 'b) t -> unit
</PRE>
<A NAME="@manual331"></A><BLOCKQUOTE>
<CODE>Hashtbl.iter f tbl</CODE> applies <CODE>f</CODE> to all bindings in table <CODE>tbl</CODE>.
<CODE>f</CODE> receives the key as first argument, and the associated value
as second argument. The order in which the bindings are passed to
<CODE>f</CODE> is unspecified. Each binding is presented exactly once
to <CODE>f</CODE>.
</BLOCKQUOTE>
<H3>Functorial interface </H3>
<PRE>
module type HashedType =
sig
type t
val equal: t -> t -> bool
val hash: t -> int
end
</PRE>
<A NAME="@manual332"></A><BLOCKQUOTE>
The input signature of the functor <CODE>Hashtbl.Make</CODE>.
<CODE>t</CODE> is the type of keys.
<CODE>equal</CODE> is the equality predicate used to compare keys.
<CODE>hash</CODE> is a hashing function on keys, returning a non-negative
integer. It must be such that if two keys are equal according
to <CODE>equal</CODE>, then they must have identical hash values as computed
by <CODE>hash</CODE>.
Examples: suitable (<CODE>equal</CODE>, <CODE>hash</CODE>) pairs for arbitrary key
types include
(<CODE>(=)</CODE>, <CODE>Hashtbl.hash</CODE>) for comparing objects by structure, and
(<CODE>(==)</CODE>, <CODE>Hashtbl.hash</CODE>) for comparing objects by addresses
(e.g. for mutable or cyclic keys).
</BLOCKQUOTE>
<PRE>
module type S =
sig
type key
type 'a t
val create: int -> 'a t
val clear: 'a t -> unit
val add: 'a t -> key -> 'a -> unit
val remove: 'a t -> key -> unit
val find: 'a t -> key -> 'a
val find_all: 'a t -> key -> 'a list
val mem: 'a t -> key -> bool
val iter: (key -> 'a -> unit) -> 'a t -> unit
end
module Make(H: HashedType): (S with type key = H.t)
</PRE>
<A NAME="@manual333"></A><BLOCKQUOTE>
The functor <CODE>Hashtbl.Make</CODE> returns a structure containing
a type <CODE>key</CODE> of keys and a type <CODE>'a t</CODE> of hash tables
associating data of type <CODE>'a</CODE> to keys of type <CODE>key</CODE>.
The operations perform similarly to those of the generic
interface, but use the hashing and equality functions
specified in the functor argument <CODE>H</CODE> instead of generic
equality and hashing.
</BLOCKQUOTE>
<H3>The polymorphic hash primitive </H3>
<PRE>
val hash : 'a -> int
</PRE>
<A NAME="@manual334"></A><BLOCKQUOTE>
<CODE>Hashtbl.hash x</CODE> associates a positive integer to any value of
any type. It is guaranteed that
if <CODE>x = y</CODE>, then <CODE>hash x = hash y</CODE>.
Moreover, <CODE>hash</CODE> always terminates, even on cyclic
structures.
</BLOCKQUOTE>
<PRE>
val hash_param : int -> int -> 'a -> int
</PRE>
<A NAME="@manual335"></A><BLOCKQUOTE>
<CODE>Hashtbl.hash_param n m x</CODE> computes a hash value for <CODE>x</CODE>, with the
same properties as for <CODE>hash</CODE>. The two extra parameters <CODE>n</CODE> and
<CODE>m</CODE> give more precise control over hashing. Hashing performs a
depth-first, right-to-left traversal of the structure <CODE>x</CODE>, stopping
after <CODE>n</CODE> meaningful nodes were encountered, or <CODE>m</CODE> nodes,
meaningful or not, were encountered. Meaningful nodes are: integers;
floating-point numbers; strings; characters; booleans; and constant
constructors. Larger values of <CODE>m</CODE> and <CODE>n</CODE> means that more
nodes are taken into account to compute the final hash
value, and therefore collisions are less likely to happen.
However, hashing takes longer. The parameters <CODE>m</CODE> and <CODE>n</CODE>
govern the tradeoff between accuracy and speed.
</BLOCKQUOTE>
<HR>
<A HREF="manual040.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual042.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<A HREF="manual030.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
</BODY>
</HTML>
|