From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id CAA24386; Sun, 29 Feb 2004 02:37:40 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id CAA24826 for ; Sun, 29 Feb 2004 02:37:39 +0100 (MET) Received: from mailgate5.cinetic.de (mailgate5.cinetic.de [217.72.192.165]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i1T1bcae006542 for ; Sun, 29 Feb 2004 02:37:38 +0100 Received: from web.de (fmomail04.dlan.cinetic.de [172.20.0.176]) by mailgate5.cinetic.de (8.11.6p2/8.11.2/SuSE Linux 8.11.0-0.4) with SMTP id i1T1bcQ13979 for caml-list@inria.fr; Sun, 29 Feb 2004 02:37:38 +0100 Date: Sun, 29 Feb 2004 02:37:38 +0100 Message-Id: <200402290137.i1T1bcQ13979@mailgate5.cinetic.de> MIME-Version: 1.0 From: "Ingo Bormuth" To: caml-list@inria.fr Subject: [Caml-list] database cache as tree of weak pointers Organization: http://freemail.web.de/ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Miltered: at concorde by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; ingo:99 pointers:01 pointers:01 val:01 comming:01 ingo:99 nur:99 linked:01 ocaml:01 mutable:01 nodes:02 nodes:02 node:02 node:02 tree:02 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk I need to implement a cache for a tree like database (directory service). So I thought about using objects linked together by weak pointers. module Str_Map = Map.Make (String);; class node (parent: node option) = object (self) val mutable node_map = Str_Map.empty method add (name: string) (other: node) = let weak_ptr = Weak.create 1 in Weak.set weak_ptr 0 (Some other); node_map <- Str_Map.add name weak_ptr node_map; end;; let a = ref (new node None) ;; let b = ref (new node (Some !a)) ;; let c = ref (new node (Some !a)) ;; !a#add "B_IN_A" !b ;; !a#add "C_IN_A" !c ;; Every node has a reference to its parent node which ensures that its parent node will not be taken away by the GC. So every node stayes as long in memory until all its child nodes have been taken away. node_map maps the names of child nodes to their weak pointer. So I can access every node simply by following its path in the tree. I know every name is mapped to an array holding just oneweak pointer. That doesn't look very nice but I don't see an alternative. Is it possible to define the module Str_Map inside the node class ? Is this way really going to work propperly ? Is there a more elegant solution ? Is parent really a reference ? I really don't want to steal you time. I'm comming from C++ being relatively new to oCaml. It took me a long time to think of that little code snippet and I feel I need some feedback on wheather the way of thinking if the one intended by the language design. Thank you in advance - Ingo Bormuth ______________________________________________________________________________ Extra-Konto: 2,50 %* Zinsen p. a. ab dem ersten Euro! Nur hier mit 25 Euro-Tankgutschein & ExtraPramie! https://extrakonto.web.de/?mc=021110 ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners