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 OAA24974; Mon, 26 Jan 2004 14:43:01 +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 OAA25470 for ; Mon, 26 Jan 2004 14:42:59 +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 i0QDgwP24780 for ; Mon, 26 Jan 2004 14:42:58 +0100 (MET) Received: from localhost (localhost [127.0.0.1]) by mail.rsise.anu.edu.au (Postfix) with ESMTP id 5D1D67B4 for ; Tue, 27 Jan 2004 00:42:54 +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 B65EA7B3 for ; Tue, 27 Jan 2004 00:42:53 +1100 (EST) Received: by pulp.anu.edu.au (Postfix, from userid 1560) id BDFEFC087A; Tue, 27 Jan 2004 00:46:31 +1100 (EST) Date: Tue, 27 Jan 2004 00:46:31 +1100 From: Pietro Abate To: ocaml ml Subject: Re: [Caml-list] sharing problem ... Message-ID: <20040126134631.GA10429@pulp.anu.edu.au> Mail-Followup-To: Pietro Abate , ocaml ml References: <20040126104345.GA3978@pulp.anu.edu.au> <6DCE648A-5003-11D8-8717-00039310CAE8@inria.fr> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="YZ5djTAD1cGYuMQK" Content-Disposition: inline In-Reply-To: <6DCE648A-5003-11D8-8717-00039310CAE8@inria.fr> 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 caml-list:01 damien:01 immutable:01 val:01 2004:99 damien:01 hashtbl:01 mymap:01 struct:01 struct:01 val:01 X-Attachments: name="htest1.ml" Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk --YZ5djTAD1cGYuMQK Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Tnx for you comments. My example was wrong as Damien pointed out because sets are immutable and I was using them in a very wrong way. I got around the problem using an object with a mutable val. I don't think this is the best solution but I couldn't figure out how to do it with using simple records... attacched there is the code snippet... tnx. p On Mon, Jan 26, 2004 at 02:27:46PM +0100, Damien Doligez wrote: > If you really need mutable sets, maybe you should use the Hashtbl > module instead of Set. -- ++ 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 --YZ5djTAD1cGYuMQK Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="htest1.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 set t = object val mutable value = MySet.empty val shared = t method shared = shared method copy = {<>} method add_list tl = value <- List.fold_left ( fun set term -> MySet.add term set ) value tl method print = if shared then print_string "(shared) : " else print_string ":"; print_string "["; MySet.iter ( fun e -> print_int e; print_string ";" ) value; print_endline "]" end class mystruct l = object(self) initializer List.iter (fun (s,t) -> self#add_name ~t:t s ) l val mutable sets = MyMap.empty method add_name ?(t=false) name= if not(MyMap.mem name sets) then sets <- MyMap.add name (new set t) sets else () method copy = let map_copy m = MyMap.fold ( fun k s m -> if s#shared then MyMap.add k s m else MyMap.add k s#copy m ) m MyMap.empty in {< sets = map_copy (sets) >} method add_elem name tl = try let set = MyMap.find name sets in set#add_list tl; (* sets <- MyMap.remove name sets; sets <- MyMap.add name set sets *) with Not_found -> Printf.printf "%s : " name; failwith "My not declared" method print = MyMap.iter ( fun k s -> print_string k; s#print ) 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 (); a1#add_elem "a" [6]; print_endline "a2: "; a2#print; print_endline "a1: "; a1#print; print_newline ();; --YZ5djTAD1cGYuMQK-- ------------------- 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