Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[ANN] Weaktbl: a weak hash table library
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-10-02 (11:07)
From: skaller <skaller@u...>
Subject: Re: [Caml-list] [ANN] Weaktbl: a weak hash table library
On Tue, 2007-10-02 at 06:50 +0200, Zheng Li wrote:
> Hi,

> Weaktbl is yet another weak hash table library for OCaml. Its main features
> include:
>  * Both keys and associated values are weakly stored. A binding exists until
>    the key is no longer referenced anywhere

I'm a bit confused what it means..

If the key is weak, it is useless except for terms, i.e. variants
or records of another data structure, where you are placing a pointer
in the table as a key. You can't put values (int, float, etc) in the 
key field, because they're immediately unreferenced.

So you could use this thing as a cache, where for example you have
a tree and calculate some property of each node, and cache it
in the table.

The problem is .. physical comparison in Ocaml is unstable,
so you cannot hash pointers using polymorphic hash, 
but pointers are the only kind that persist. 

So I have to conclude you must use the Weaktbl with a functor,
instantiating the hash function to something which does a computation
on the value, and this claim:

 * The interface is fully compatible with the standard 
   Hashtbl library instead
   of the hash sub-module of the Weak library, so 
   basically you can also use it
   as an alternative of the standard Hashtbl

is misleading: I use Hashtbl extensively, but ALL my uses are
using the polymorphic interface with polymorphic hash,
so actually none of them will work with Weaktbl, which is
a pity.

Can you confirm or deny this analysis?

A weakly keyed table would be cool for term cache, since
it saves worrying about removing dangling references.

John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: