caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Thomas Fischbacher <tf@functionality.de>
To: Richard Jones <rich@annexia.org>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Weak hash table for attaching extra data to an object
Date: Tue, 14 Aug 2007 12:08:11 +0100	[thread overview]
Message-ID: <46C18D1B.2070303@functionality.de> (raw)
In-Reply-To: <20070814101535.GA14485@furbychan.cocan.org>


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


  reply	other threads:[~2007-08-14 11:08 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-08-14 10:15 Richard Jones
2007-08-14 11:08 ` Thomas Fischbacher [this message]
2007-08-14 11:48   ` [Caml-list] " Till Varoquaux
2007-08-14 20:44   ` Jon Harrop
2007-08-14 23:09     ` Stefano Zacchiroli
2007-08-15  0:33       ` Jon Harrop
2007-08-15 12:33         ` Daniel Bünzli
2007-08-16 14:54         ` Markus Mottl
2007-08-15  1:28     ` skaller
2007-08-15 19:04     ` Richard Jones
2007-08-16 16:17       ` Some observations and measurements with using Weak Hashtable to annotate a tree (was: Re: [Caml-list] Weak hash table for attaching extra data to an object) Richard Jones
2007-08-14 16:22 ` [Caml-list] Weak hash table for attaching extra data to an object Richard Jones
2007-08-14 23:24   ` Till Varoquaux
2007-08-14 23:35   ` Fernando Alegre
2007-08-15  7:59     ` Richard Jones

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=46C18D1B.2070303@functionality.de \
    --to=tf@functionality.de \
    --cc=caml-list@inria.fr \
    --cc=rich@annexia.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).