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 LAA16262; Mon, 26 Jan 2004 11:40:17 +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 LAA16014 for ; Mon, 26 Jan 2004 11:40:15 +0100 (MET) Received: from mail.rsise.anu.edu.au (mail.rsise.anu.edu.au [150.203.208.4]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id i0QAeDP03068 for ; Mon, 26 Jan 2004 11:40:14 +0100 (MET) Received: from localhost (localhost [127.0.0.1]) by mail.rsise.anu.edu.au (Postfix) with ESMTP id 8CB93904 for ; Mon, 26 Jan 2004 21:40:09 +1100 (EST) Received: from pulp.anu.edu.au (pulp.anu.edu.au [150.203.126.25]) by mail.rsise.anu.edu.au (Postfix) with ESMTP id ECE2E7B3 for ; Mon, 26 Jan 2004 21:40:07 +1100 (EST) Received: by pulp.anu.edu.au (Postfix, from userid 1560) id 6BD91C087A; Mon, 26 Jan 2004 21:43:45 +1100 (EST) Date: Mon, 26 Jan 2004 21:43:45 +1100 From: Pietro Abate To: ocaml ml Subject: [Caml-list] sharing problem ... Message-ID: <20040126104345.GA3978@pulp.anu.edu.au> Mail-Followup-To: Pietro Abate , ocaml ml Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="y0ulUmNC+osPPQO6" Content-Disposition: inline X-Operating-System: GNU/Linux X-Organization: Research School of Information Science and Engineering (Australian National University) User-Agent: Mutt/1.5.5.1+cvs20040105i X-Scanned-By: AMaViS-ng/Claimv at mail.rsise.anu.edu.au X-Loop: caml-list@inria.fr X-Spam: no; 0.00; pietro:01 abate:01 pietro:01 abate:01 hashtbl:01 clone:01 mymap:01 mymap:01 struct:01 struct:01 val:01 clone:01 elem:01 printf:01 printf:01 X-Attachments: name="htest.ml" Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk --y0ulUmNC+osPPQO6 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi all, I'm trying to implement a (string * set) hashtbl where some element (set) should be shared between different objects. the main point is that when I clone an object I want to duplicate some sets and shared others (sticky sets) I tried to achieve this result with the method below. it use the set_copy to duplicate a set and does nothing when then element is marked as "shared". method copy = let set_copy s = MySet.fold ( fun e s -> MySet.add e s ) MySet.empty s in let map_copy m = MyMap.fold ( fun k (s,t) m -> (* this one should be the same set *) if t then MyMap.add k (s,t) m (* this one should be a copy !! *) else MyMap.add k (set_copy(s),t) m ) MyMap.empty m in {< sets = map_copy sets >} Of course this is not the right way of doing it as the map with key "a" (in the small example attached) is not shared at all... this is the result I'd like to get: a1: a(shared) : [1;2;3;] b:[1;2;3;] a2: a(shared) : [1;2;3;4;5;] b:[1;2;3;4;5;] a1: a(shared) : [1;2;3;4;5] <---------------here !!! b:[1;2;3;] how can I efficiently share elements in this kind of structure ? p -- ++ Our capacity for understanding is inversely proportional to how much we think we know. The more I know, the more I know I don't know... ++ Please avoid sending me Word or PowerPoint attachments. See http://www.fsf.org/philosophy/no-word-attachments.html --y0ulUmNC+osPPQO6 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="htest.ml" module MyMap = Map.Make( struct type t = string let compare = compare end) module MySet = Set.Make( struct type t = int let compare = compare end) class mystruct l = object(self) initializer List.iter (fun (h,s) -> self#add_name ~t:s h ) l val mutable sets = MyMap.empty method add_name ?(t=false) name= if not(MyMap.mem name sets) then sets <- MyMap.add name (MySet.empty,t) sets else () method clone = {< sets = sets >} method copy = let set_copy s = MySet.fold ( fun e s -> MySet.add e s ) MySet.empty s in let map_copy m = MyMap.fold ( fun k (s,t) m -> if t then MyMap.add k (s,t) m else MyMap.add k (set_copy(s),t) m ) MyMap.empty m in {< sets = map_copy sets >} method add_elem name tl = try let (set,t) = MyMap.find name sets in let set' = List.fold_left ( fun s t -> MySet.add t s ) set tl in sets <- MyMap.remove name sets; sets <- MyMap.add name (set',t) sets with Not_found -> Printf.printf "%s : " name; failwith "My not declared" method print = MyMap.iter ( fun k (s,t) -> print_string k ; if t then print_string "(shared) : " else print_string ":"; print_string "["; MySet.iter ( fun e -> print_int e; print_string ";" ) s; print_endline "]" ) sets end let a1 = new mystruct ["a",true;"b",false] ;; let _ = a1#add_elem "a" [1;2;3]; a1#add_elem "b" [1;2;3]; print_endline "a1: "; a1#print; print_newline ();; let a2 = a1#copy;; let _ = a2#add_elem "a" [4;5]; a2#add_elem "b" [4;5]; print_endline "a2: "; a2#print; print_newline (); print_endline "a1: "; a1#print; print_newline ();; --y0ulUmNC+osPPQO6-- ------------------- 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