Sophie

Sophie

distrib > Mageia > 5 > i586 > media > core-release > by-pkgid > 6e204a966e8c42d976f99a1700ce5f20 > files > 2247

ghc-7.4.2-4.mga5.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Data.HashTable</title><link href="ocean.css" rel="stylesheet" type="text/css" title="Ocean" /><script src="haddock-util.js" type="text/javascript"></script><script type="text/javascript">//<![CDATA[
window.onload = function () {pageLoad();setSynopsis("mini_Data-HashTable.html");};
//]]>
</script></head><body><div id="package-header"><ul class="links" id="page-menu"><li><a href="index.html">Contents</a></li><li><a href="doc-index.html">Index</a></li></ul><p class="caption">base-4.5.1.0: Basic libraries</p></div><div id="content"><div id="module-header"><table class="info"><tr><th>Portability</th><td>portable</td></tr><tr><th>Stability</th><td>provisional</td></tr><tr><th>Maintainer</th><td>libraries@haskell.org</td></tr><tr><th>Safe Haskell</th><td>Trustworthy</td></tr></table><p class="caption">Data.HashTable</p></div><div id="table-of-contents"><p class="caption">Contents</p><ul><li><a href="#g:1">Basic hash table operations
</a></li><li><a href="#g:2">Converting to and from lists
</a></li><li><a href="#g:3">Hash functions
</a></li><li><a href="#g:4">Diagnostics
</a></li></ul></div><div id="description"><p class="caption">Description</p><div class="doc"><p>An implementation of extensible hash tables, as described in
 Per-Ake Larson, <em>Dynamic Hash Tables</em>, CACM 31(4), April 1988,
 pp. 446--457.  The implementation is also derived from the one
 in GHC's runtime system (<code>ghc/rts/Hash.{c,h}</code>).
</p></div></div><div id="synopsis"><p id="control.syn" class="caption expander" onclick="toggleSection('syn')">Synopsis</p><ul id="section.syn" class="hide" onclick="toggleSection('syn')"><li class="src short"><span class="keyword">data</span>  <a href="#t:HashTable">HashTable</a> key val</li><li class="src short"><a href="#v:new">new</a> ::  (key -&gt; key -&gt; <a href="Data-Bool.html#t:Bool">Bool</a>) -&gt; (key -&gt; <a href="Data-Int.html#t:Int32">Int32</a>) -&gt; <a href="System-IO.html#t:IO">IO</a> (<a href="Data-HashTable.html#t:HashTable">HashTable</a> key val)</li><li class="src short"><a href="#v:newHint">newHint</a> ::  (key -&gt; key -&gt; <a href="Data-Bool.html#t:Bool">Bool</a>) -&gt; (key -&gt; <a href="Data-Int.html#t:Int32">Int32</a>) -&gt; <a href="Data-Int.html#t:Int">Int</a> -&gt; <a href="System-IO.html#t:IO">IO</a> (<a href="Data-HashTable.html#t:HashTable">HashTable</a> key val)</li><li class="src short"><a href="#v:insert">insert</a> ::  <a href="Data-HashTable.html#t:HashTable">HashTable</a> key val -&gt; key -&gt; val -&gt; <a href="System-IO.html#t:IO">IO</a> <a href="../ghc-prim-0.2.0.0/GHC-Tuple.html#t:-40--41-">()</a></li><li class="src short"><a href="#v:delete">delete</a> ::  <a href="Data-HashTable.html#t:HashTable">HashTable</a> key val -&gt; key -&gt; <a href="System-IO.html#t:IO">IO</a> <a href="../ghc-prim-0.2.0.0/GHC-Tuple.html#t:-40--41-">()</a></li><li class="src short"><a href="#v:lookup">lookup</a> ::  <a href="Data-HashTable.html#t:HashTable">HashTable</a> key val -&gt; key -&gt; <a href="System-IO.html#t:IO">IO</a> (<a href="Data-Maybe.html#t:Maybe">Maybe</a> val)</li><li class="src short"><a href="#v:update">update</a> ::  <a href="Data-HashTable.html#t:HashTable">HashTable</a> key val -&gt; key -&gt; val -&gt; <a href="System-IO.html#t:IO">IO</a> <a href="Data-Bool.html#t:Bool">Bool</a></li><li class="src short"><a href="#v:fromList">fromList</a> :: <a href="Data-Eq.html#t:Eq">Eq</a> key =&gt; (key -&gt; <a href="Data-Int.html#t:Int32">Int32</a>) -&gt; [(key, val)] -&gt; <a href="System-IO.html#t:IO">IO</a> (<a href="Data-HashTable.html#t:HashTable">HashTable</a> key val)</li><li class="src short"><a href="#v:toList">toList</a> ::  <a href="Data-HashTable.html#t:HashTable">HashTable</a> key val -&gt; <a href="System-IO.html#t:IO">IO</a> [(key, val)]</li><li class="src short"><a href="#v:hashInt">hashInt</a> :: <a href="Data-Int.html#t:Int">Int</a> -&gt; <a href="Data-Int.html#t:Int32">Int32</a></li><li class="src short"><a href="#v:hashString">hashString</a> :: <a href="Data-String.html#t:String">String</a> -&gt; <a href="Data-Int.html#t:Int32">Int32</a></li><li class="src short"><a href="#v:prime">prime</a> :: <a href="Data-Int.html#t:Int32">Int32</a></li><li class="src short"><a href="#v:longestChain">longestChain</a> ::  <a href="Data-HashTable.html#t:HashTable">HashTable</a> key val -&gt; <a href="System-IO.html#t:IO">IO</a> [(key, val)]</li></ul></div><div id="interface"><h1 id="g:1">Basic hash table operations
</h1><div class="top"><p class="src"><span class="keyword">data</span>  <a name="t:HashTable" class="def">HashTable</a> key val </p></div><div class="top"><p class="src"><a name="v:new" class="def">new</a></p><div class="subs arguments"><p class="caption">Arguments</p><table><tr><td class="src">:: (key -&gt; key -&gt; <a href="Data-Bool.html#t:Bool">Bool</a>)</td><td class="doc"><p><code>eq</code>: An equality comparison on keys
</p></td></tr><tr><td class="src">-&gt; (key -&gt; <a href="Data-Int.html#t:Int32">Int32</a>)</td><td class="doc"><p><code>hash</code>: A hash function on keys
</p></td></tr><tr><td class="src">-&gt; <a href="System-IO.html#t:IO">IO</a> (<a href="Data-HashTable.html#t:HashTable">HashTable</a> key val)</td><td class="doc"><p>Returns: an empty hash table
</p></td></tr></table></div><div class="doc"><p>Creates a new hash table.  The following property should hold for the <code>eq</code>
 and <code>hash</code> functions passed to <code><a href="Data-HashTable.html#v:new">new</a></code>:
</p><pre>   eq A B  =&gt;  hash A == hash B
</pre></div></div><div class="top"><p class="src"><a name="v:newHint" class="def">newHint</a></p><div class="subs arguments"><p class="caption">Arguments</p><table><tr><td class="src">:: (key -&gt; key -&gt; <a href="Data-Bool.html#t:Bool">Bool</a>)</td><td class="doc"><p><code>eq</code>: An equality comparison on keys
</p></td></tr><tr><td class="src">-&gt; (key -&gt; <a href="Data-Int.html#t:Int32">Int32</a>)</td><td class="doc"><p><code>hash</code>: A hash function on keys
</p></td></tr><tr><td class="src">-&gt; <a href="Data-Int.html#t:Int">Int</a></td><td class="doc"><p><code>minSize</code>: initial table size
</p></td></tr><tr><td class="src">-&gt; <a href="System-IO.html#t:IO">IO</a> (<a href="Data-HashTable.html#t:HashTable">HashTable</a> key val)</td><td class="doc"><p>Returns: an empty hash table
</p></td></tr></table></div><div class="doc"><p>Creates a new hash table with the given minimum size.
</p></div></div><div class="top"><p class="src"><a name="v:insert" class="def">insert</a> ::  <a href="Data-HashTable.html#t:HashTable">HashTable</a> key val -&gt; key -&gt; val -&gt; <a href="System-IO.html#t:IO">IO</a> <a href="../ghc-prim-0.2.0.0/GHC-Tuple.html#t:-40--41-">()</a></p><div class="doc"><p>Inserts a key/value mapping into the hash table.
</p><p>Note that <code><a href="Data-HashTable.html#v:insert">insert</a></code> doesn't remove the old entry from the table -
 the behaviour is like an association list, where <code><a href="Data-HashTable.html#v:lookup">lookup</a></code> returns
 the most-recently-inserted mapping for a key in the table.  The
 reason for this is to keep <code><a href="Data-HashTable.html#v:insert">insert</a></code> as efficient as possible.  If
 you need to update a mapping, then we provide <code><a href="Data-HashTable.html#v:update">update</a></code>.
</p></div></div><div class="top"><p class="src"><a name="v:delete" class="def">delete</a> ::  <a href="Data-HashTable.html#t:HashTable">HashTable</a> key val -&gt; key -&gt; <a href="System-IO.html#t:IO">IO</a> <a href="../ghc-prim-0.2.0.0/GHC-Tuple.html#t:-40--41-">()</a></p><div class="doc"><p>Remove an entry from the hash table.
</p></div></div><div class="top"><p class="src"><a name="v:lookup" class="def">lookup</a> ::  <a href="Data-HashTable.html#t:HashTable">HashTable</a> key val -&gt; key -&gt; <a href="System-IO.html#t:IO">IO</a> (<a href="Data-Maybe.html#t:Maybe">Maybe</a> val)</p><div class="doc"><p>Looks up the value of a key in the hash table.
</p></div></div><div class="top"><p class="src"><a name="v:update" class="def">update</a> ::  <a href="Data-HashTable.html#t:HashTable">HashTable</a> key val -&gt; key -&gt; val -&gt; <a href="System-IO.html#t:IO">IO</a> <a href="Data-Bool.html#t:Bool">Bool</a></p><div class="doc"><p>Updates an entry in the hash table, returning <code><a href="Data-Bool.html#v:True">True</a></code> if there was
 already an entry for this key, or <code><a href="Data-Bool.html#v:False">False</a></code> otherwise.  After <code><a href="Data-HashTable.html#v:update">update</a></code>
 there will always be exactly one entry for the given key in the table.
</p><p><code><a href="Data-HashTable.html#v:insert">insert</a></code> is more efficient than <code><a href="Data-HashTable.html#v:update">update</a></code> if you don't care about
 multiple entries, or you know for sure that multiple entries can't
 occur.  However, <code><a href="Data-HashTable.html#v:update">update</a></code> is more efficient than <code><a href="Data-HashTable.html#v:delete">delete</a></code> followed
 by <code><a href="Data-HashTable.html#v:insert">insert</a></code>.
</p></div></div><h1 id="g:2">Converting to and from lists
</h1><div class="top"><p class="src"><a name="v:fromList" class="def">fromList</a> :: <a href="Data-Eq.html#t:Eq">Eq</a> key =&gt; (key -&gt; <a href="Data-Int.html#t:Int32">Int32</a>) -&gt; [(key, val)] -&gt; <a href="System-IO.html#t:IO">IO</a> (<a href="Data-HashTable.html#t:HashTable">HashTable</a> key val)</p><div class="doc"><p>Convert a list of key/value pairs into a hash table.  Equality on keys
 is taken from the Eq instance for the key type.
</p></div></div><div class="top"><p class="src"><a name="v:toList" class="def">toList</a> ::  <a href="Data-HashTable.html#t:HashTable">HashTable</a> key val -&gt; <a href="System-IO.html#t:IO">IO</a> [(key, val)]</p><div class="doc"><p>Converts a hash table to a list of key/value pairs.
</p></div></div><h1 id="g:3">Hash functions
</h1><div class="doc"><p>This implementation of hash tables uses the low-order <em>n</em> bits of the hash
 value for a key, where <em>n</em> varies as the hash table grows.  A good hash
 function therefore will give an even distribution regardless of <em>n</em>.
</p><p>If your keyspace is integrals such that the low-order bits between
 keys are highly variable, then you could get away with using <code><a href="Prelude.html#v:fromIntegral">fromIntegral</a></code>
 as the hash function.
</p><p>We provide some sample hash functions for <code><a href="Data-Int.html#t:Int">Int</a></code> and <code><a href="Data-String.html#t:String">String</a></code> below.
</p></div><div class="top"><p class="src"><a name="v:hashInt" class="def">hashInt</a> :: <a href="Data-Int.html#t:Int">Int</a> -&gt; <a href="Data-Int.html#t:Int32">Int32</a></p><div class="doc"><p>A sample (and useful) hash function for Int and Int32,
 implemented by extracting the uppermost 32 bits of the 64-bit
 result of multiplying by a 33-bit constant.  The constant is from
 Knuth, derived from the golden ratio:
</p><pre> golden = round ((sqrt 5 - 1) * 2^32)
</pre><p>We get good key uniqueness on small inputs
 (a problem with previous versions):
  (length $ group $ sort $ map hashInt [-32767..65536]) == 65536 + 32768
</p></div></div><div class="top"><p class="src"><a name="v:hashString" class="def">hashString</a> :: <a href="Data-String.html#t:String">String</a> -&gt; <a href="Data-Int.html#t:Int32">Int32</a></p><div class="doc"><p>A sample hash function for Strings.  We keep multiplying by the
 golden ratio and adding.  The implementation is:
</p><pre> hashString = foldl' f golden
   where f m c = fromIntegral (ord c) * magic + hashInt32 m
         magic = 0xdeadbeef
</pre><p>Where hashInt32 works just as hashInt shown above.
</p><p>Knuth argues that repeated multiplication by the golden ratio
 will minimize gaps in the hash space, and thus it's a good choice
 for combining together multiple keys to form one.
</p><p>Here we know that individual characters c are often small, and this
 produces frequent collisions if we use ord c alone.  A
 particular problem are the shorter low ASCII and ISO-8859-1
 character strings.  We pre-multiply by a magic twiddle factor to
 obtain a good distribution.  In fact, given the following test:
</p><pre> testp :: Int32 -&gt; Int
 testp k = (n - ) . length . group . sort . map hs . take n $ ls
   where ls = [] : [c : l | l &lt;- ls, c &lt;- ['\0'..'\xff']]
         hs = foldl' f golden
         f m c = fromIntegral (ord c) * k + hashInt32 m
         n = 100000
</pre><p>We discover that testp magic = 0.
</p></div></div><div class="top"><p class="src"><a name="v:prime" class="def">prime</a> :: <a href="Data-Int.html#t:Int32">Int32</a></p><div class="doc"><p>A prime larger than the maximum hash table size
</p></div></div><h1 id="g:4">Diagnostics
</h1><div class="top"><p class="src"><a name="v:longestChain" class="def">longestChain</a> ::  <a href="Data-HashTable.html#t:HashTable">HashTable</a> key val -&gt; <a href="System-IO.html#t:IO">IO</a> [(key, val)]</p><div class="doc"><p>This function is useful for determining whether your hash
 function is working well for your data set.  It returns the longest
 chain of key/value pairs in the hash table for which all the keys
 hash to the same bucket.  If this chain is particularly long (say,
 longer than 14 elements or so), then it might be a good idea to try
 a different hash function.
</p></div></div></div></div><div id="footer"><p>Produced by <a href="http://www.haskell.org/haddock/">Haddock</a> version 2.11.0</p></div></body></html>