* [Caml-list] database cache as tree of weak pointers
@ 2004-02-29 1:29 Ingo Bormuth
0 siblings, 0 replies; 2+ messages in thread
From: Ingo Bormuth @ 2004-02-29 1:29 UTC (permalink / raw)
To: caml-list
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
______________________________________________________________________________
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
^ permalink raw reply [flat|nested] 2+ messages in thread
* [Caml-list] database cache as tree of weak pointers
@ 2004-02-29 1:37 Ingo Bormuth
0 siblings, 0 replies; 2+ messages in thread
From: Ingo Bormuth @ 2004-02-29 1:37 UTC (permalink / raw)
To: caml-list
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2004-02-29 1:37 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-02-29 1:29 [Caml-list] database cache as tree of weak pointers Ingo Bormuth
2004-02-29 1:37 Ingo Bormuth
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox