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