caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Pietro Abate <Pietro.Abate@anu.edu.au>
To: ocaml ml <caml-list@inria.fr>
Subject: Re: [Caml-list] sharing problem ...
Date: Tue, 27 Jan 2004 00:46:31 +1100	[thread overview]
Message-ID: <20040126134631.GA10429@pulp.anu.edu.au> (raw)
In-Reply-To: <6DCE648A-5003-11D8-8717-00039310CAE8@inria.fr>

[-- Attachment #1: Type: text/plain, Size: 784 bytes --]

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

[-- Attachment #2: htest1.ml --]
[-- Type: text/plain, Size: 2521 bytes --]



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



      reply	other threads:[~2004-01-26 13:43 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-01-26 10:43 Pietro Abate
2004-01-26 11:22 ` Nicolas Cannasse
2004-01-26 13:27 ` Damien Doligez
2004-01-26 13:46   ` Pietro Abate [this message]

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=20040126134631.GA10429@pulp.anu.edu.au \
    --to=pietro.abate@anu.edu.au \
    --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;
as well as URLs for NNTP newsgroup(s).