From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 30008BC69 for ; Tue, 14 Aug 2007 13:08:19 +0200 (CEST) Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.179]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l7EB8IE3014725 for ; Tue, 14 Aug 2007 13:08:18 +0200 Received: from [141.84.136.30] (helo=[86.148.179.45]) by mrelayeu.kundenserver.de (node=mrelayeu5) with ESMTP (Nemesis), id 0ML25U-1IKuFx2VyL-0000iX; Tue, 14 Aug 2007 13:08:16 +0200 Message-ID: <46C18D1B.2070303@functionality.de> Date: Tue, 14 Aug 2007 12:08:11 +0100 From: Thomas Fischbacher User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8) Gecko/20060904 Debian/1.7.8-1sarge7.2.2 X-Accept-Language: en MIME-Version: 1.0 To: Richard Jones Cc: caml-list@inria.fr Subject: Re: [Caml-list] Weak hash table for attaching extra data to an object References: <20070814101535.GA14485@furbychan.cocan.org> In-Reply-To: <20070814101535.GA14485@furbychan.cocan.org> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V01U2FsdGVkX196AhgQNXuTPi2R/k9b4vLiW+BE0Nw+dLVswLx DaW7qApn7thDRbDvwYn6yxYaNaK7mOdZehccexJY+szuEaKt5p enImaEYUfstAVUVcOgfxg== X-Miltered: at discorde with ID 46C18D22.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; hash:01 node:01 nodes:01 arises:01 algebra:01 hash:01 finalizers:01 garbage:01 garbage:01 wrote:01 symbolic:01 caml-list:01 data:02 data:02 tree:02 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