English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] database cache as tree of weak pointers
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-02-29 (01:29)
From: Ingo Bormuth <Bormuth@w...>
Subject: [Caml-list] database cache as tree of weak pointers

 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;
       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

Nachrichten, Musik und Spiele schnell und einfach per Quickstart im 
WEB.DE Screensaver - Gratis downloaden: http://screensaver.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