Browse thread
Weak hash table for attaching extra data to an object
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Till Varoquaux <till.varoquaux@g...> |
| Subject: | Re: [Caml-list] Weak hash table for attaching extra data to an object |
I have a small library that could do the trick. It is built a a hashtable whose keys are weak, you need to recompact it from time to time in order to free associated values. One strategy would be to recompact when n values are freed. I tried to limit the performance cost by storing collision values in a separate table. The code can be browsed online at: http://till.varoquaux.free.fr/projects/mini_js/WeakHt.ml.html There's a very small dependency on another file, i use Option.get (defined as in extlib: let get = function | Some v -> v | None -> raise No_value ) It is licensed under lgpl+special linking clause. If you need a more permissive license let me know. This can be downloaded via darcs: darcs get http://till.varoquaux.free.fr/projects/mini_js/WeakHt.ml.html or directly at: http://till.varoquaux.free.fr/projects/mini_js/WeakHt.ml There no mli file (I'm lazy). Constructive criticism is welcome. If you need automatic collection of values and have a hard time implementing I can work on it. Cheers, Till P.S.: I wasn't aware of HWeak when I did this library, I don't know how it compares performance wise. Having never had to profile this library I actually don't know how bad it really is. This can certainly also be worked on. On 8/14/07, Thomas Fischbacher <tf@functionality.de> wrote: > > Richard Jones wrote: > > > I have a module which I can't edit[1]. This module, let's call it > > Document, stores a representation of an XML tree in internal node > > objects: > > > > type intnode > > > > What I want to do is -- completely outside Document -- attach my own > > extra information to these nodes. > > Ah, so the situation really is somewhat common, and not something > just I am running into occasionally. (The problem in particular > also arises occasionally when writing symbolic algebra code when one > wants to attach meta-data (e.g. on typesetting) to terms without > touching the underlying term representations in any way.) > > > The key point is that if the > > garbage collector collects an intnode, then I want my extra > > information to be garbage collected too. > > > > I wonder if anyone has done this, or can suggest a better way to solve > > this problem? The issue of attaching extra data to some existing > > object would seem to be a fairly common one ... > > You might be able to "fake" a weak hash table by using a trick: for a > key-value entry k1 -> v1, you take k1 and v1 and rebuild the outermost > data structure to get k2 with k2 = k1 and k2 != k1 (oh hey, now that > looks confusing, right?), as well as v2 = v1 and v2 != v1. Now, you > enter k2 -> v2 into an ordinary hash table and register finalizers for > k1 and v1 which remove this k2 -> v2 entry from your hash. That might > do. > > -- > best regards, > Thomas Fischbacher > tf@functionality.de > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > -- http://till-varoquaux.blogspot.com/