Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "Ingo Bormuth" <Bormuth@web.de>
To: caml-list@inria.fr
Subject: [Caml-list] database cache as tree of weak pointers
Date: Sun, 29 Feb 2004 02:37:38 +0100	[thread overview]
Message-ID: <200402290137.i1T1bcQ13979@mailgate5.cinetic.de> (raw)

 
  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


             reply	other threads:[~2004-02-29  1:37 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-02-29  1:37 Ingo Bormuth [this message]
  -- strict thread matches above, loose matches on Subject: below --
2004-02-29  1:29 Ingo Bormuth

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=200402290137.i1T1bcQ13979@mailgate5.cinetic.de \
    --to=bormuth@web.de \
    --cc=caml-list@inria.fr \
    /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