]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/mathql_interpreter/diff.ml
cvsignore for html documentation dir
[helm.git] / helm / ocaml / mathql_interpreter / diff.ml
index 41e41c73be8ebecdd485d27c9a9c78c805248d96..b4e09196ee5c42e097cbc2c250f2db317d0f491b 100644 (file)
  *)
 
 (*
- * implementazione del comando DIFF
- *)
+ * vecchia implementazione del comando DIFF
 
-(*
- * 
- *)
-let xres_fill_context hr h1 l1 =
- match l1 with
-    [] -> []
- |   _ ->
-     let hh = List.combine h1 l1
-     in
-      List.map
-       (fun x ->
-        if (List.mem_assoc x hh) then
-        List.assoc x hh
-       else
-        ""
-       )
-       hr
+exception NotCompatible;;
+
+(* intersect_attributes is successful iff there is no attribute with *)
+(* two different values in the two lists. The returned list is the   *)
+(* union of the two lists.                                           *)
+let rec intersect_attributes (attr1, attr2) =
+ match attr1, attr2 with
+    [],_ -> attr2
+  | _,[] -> attr1
+  | (key1,value1)::tl1, (key2,_)::_ when key1 < key2 ->
+      (key1,value1)::(intersect_attributes (tl1,attr2))
+  | (key1,_)::_, (key2,value2)::tl2 when key2 < key1 ->
+      (key2,value2)::(intersect_attributes (attr1,tl2))
+  | entry1::tl1, entry2::tl2 when entry1 = entry2 ->
+     entry1::(intersect_attributes (tl1,tl2))
+  | _, _ -> raise NotCompatible  (* same keys, different values *)
+;;
+
+let rec diff_ex l1 l2 =
+ let module S = Mathql_semantics in
+  match (l1, l2) with
+     [],_ -> []
+   | l,[] -> l
+   | {S.uri = uri1}::_, {S.uri = uri2}::tl2 when uri2 < uri1 ->
+      (diff_ex l1 tl2)
+   | {S.uri = uri1 ; S.attributes = attributes1}::tl1,
+     {S.uri = uri2}::_ when uri1 < uri2 ->
+      {S.uri = uri1 ; S.attributes = attributes1 ; S.extra = ""}::(diff_ex tl1 l2)
+   | {S.uri = uri1 ; S.attributes = attributes1}::tl1,
+     {S.uri = uri2 ; S.attributes = attributes2}::tl2 ->
+       try
+        let attributes' = intersect_attributes (attributes1, attributes2) in 
+         diff_ex tl1 tl2
+       with
+        NotCompatible ->
+         {S.uri = uri1 ; S.attributes = attributes1 ; S.extra = ""}::(diff_ex tl1 tl2)
 ;;
+*)
 
 (*
  * implementazione del comando DIFF
  *)
-let diff_ex alist1 alist2 =
- let head1 = List.hd alist1
- and tail1 = List.tl alist1
- and head2 = List.hd alist2
- and tail2 = List.tl alist2 (* e fin qui ... *)
- in
-  match (head1, head2) with
-     ([], _) -> assert false (* gli header non devono mai essere vuoti *)
-  |  (_, []) -> assert false (* devono contenere almeno [retVal] *)
-  |  (_,  _) -> let headr = (head2 @
-                            (List.find_all
-                            (function t -> not (List.mem t head2))
-                            head1)
-                           ) in (* header del risultato finale *)
-      List.append (* il risultato finale e' la concatenazione ...*)
-       [headr] (* ... dell'header costruito prima ...*)
-       (match (tail1, tail2) with (* e di una coda "unione" *)
-           ([], _) -> tail2 (* va bene perche' l'altra lista e' vuota *)
-        |  (_, []) -> tail1 (* va bene perche' l'altra lista e' vuota *)
-        |  (_,  _) ->
-           let first = (* parte dell'unione che riguarda solo il primo set *)
-            List.map (fun l -> [List.hd l] @
-                      xres_fill_context
-                       (List.tl headr) (List.tl head1) (List.tl l)
-                     ) tail1
-            in
-            List.fold_left
-             (fun par x ->
-              let y = (* elemento candidato ad entrare *)
-               [List.hd x]
-               @
-               xres_fill_context
-                (List.tl headr) (List.tl head2) (List.tl x)
-              in
-               par @ if (List.find_all (fun t -> t = y) par) = [] then
-                      [y]
-                     else
-                      []
-             )
-             first
-             tail2
-(*          first @
-            List.map (fun l -> [List.hd l] @
-                      xres_fill_context
-                       (List.tl headr) (List.tl head2) (List.tl l)
-                     ) tail2
-*)
-       ) (* match *)
+let rec diff_ex rs1 rs2 =
+  match (rs1, rs2) with
+     [],_ -> []
+   | l,[] -> l
+   | (uri1,l)::tl1,(uri2,_)::_ when uri1 < uri2 -> (uri1,l)::(diff_ex tl1 rs2)
+   | (uri1,_)::_,(uri2,_)::tl2 when uri2 < uri1 -> (diff_ex rs1 tl2)
+   | (uri1,_)::tl1, (uri2,_)::tl2 -> (diff_ex tl1 tl2)
 ;;
 
-(** TEST **)
 
-(*
-let h1 = ["retVal";     "a";      "b"];;
-let l1 = ["pippo";      "3";      "3"];;
-let l3 = ["pluto";      "7";      "8"]
-let r1 = [h1; l1; l3];;
 
-(*let h2 = ["retVal";               "b";      "c"];;
-let l2 = ["pippo";                "3";      "1"];;*)
-let h2 = ["retVal";     "a";      "b"];;
-let l2 = ["pippo";      "3";      "3"];;
-let r2 = [h2; l2];;
 
-List.map (fun l -> List.iter print_endline l) (xres_union (r1, r2));;
-*)
+let diff_ex l1 l2 =
+ let before = Sys.time () in
+ let res = diff_ex l1 l2 in
+ let after = Sys.time () in
+  let ll1 = string_of_int (List.length l1) in
+  let ll2 = string_of_int (List.length l2) in
+  let diff = string_of_float (after -. before) in
+  print_endline
+   ("DIFF(" ^ ll1 ^ ", " ^ ll2 ^ ") = " ^ string_of_int (List.length res) ^
+    ": " ^ diff ^ "s") ;
+  flush stdout ;
+  res
+;;
+