]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/diff.ml
Initial revision
[helm.git] / helm / ocaml / mathql_interpreter / diff.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 (*
27  * vecchia implementazione del comando DIFF
28  
29
30 exception NotCompatible;;
31
32 (* intersect_attributes is successful iff there is no attribute with *)
33 (* two different values in the two lists. The returned list is the   *)
34 (* union of the two lists.                                           *)
35 let rec intersect_attributes (attr1, attr2) =
36  match attr1, attr2 with
37     [],_ -> attr2
38   | _,[] -> attr1
39   | (key1,value1)::tl1, (key2,_)::_ when key1 < key2 ->
40       (key1,value1)::(intersect_attributes (tl1,attr2))
41   | (key1,_)::_, (key2,value2)::tl2 when key2 < key1 ->
42       (key2,value2)::(intersect_attributes (attr1,tl2))
43   | entry1::tl1, entry2::tl2 when entry1 = entry2 ->
44      entry1::(intersect_attributes (tl1,tl2))
45   | _, _ -> raise NotCompatible  (* same keys, different values *)
46 ;;
47
48  
49 let rec diff_ex l1 l2 =
50  let module S = Mathql_semantics in
51   match (l1, l2) with
52      [],_ -> []
53    | l,[] -> l
54    | {S.uri = uri1}::_, {S.uri = uri2}::tl2 when uri2 < uri1 ->
55       (diff_ex l1 tl2)
56    | {S.uri = uri1 ; S.attributes = attributes1}::tl1,
57      {S.uri = uri2}::_ when uri1 < uri2 ->
58       {S.uri = uri1 ; S.attributes = attributes1 ; S.extra = ""}::(diff_ex tl1 l2)
59    | {S.uri = uri1 ; S.attributes = attributes1}::tl1,
60      {S.uri = uri2 ; S.attributes = attributes2}::tl2 ->
61        try
62         let attributes' = intersect_attributes (attributes1, attributes2) in 
63          diff_ex tl1 tl2
64        with
65         NotCompatible ->
66          {S.uri = uri1 ; S.attributes = attributes1 ; S.extra = ""}::(diff_ex tl1 tl2)
67 ;;
68 *)
69
70 (*
71  * implementazione del comando DIFF
72  *)
73 let rec diff_ex rs1 rs2 =
74   match (rs1, rs2) with
75      [],_ -> []
76    | l,[] -> l
77    | (uri1,l)::tl1,(uri2,_)::_ when uri1 < uri2 -> (uri1,l)::(diff_ex tl1 rs2)
78    | (uri1,_)::_,(uri2,_)::tl2 when uri2 < uri1 -> (diff_ex rs1 tl2)
79    | (uri1,_)::tl1, (uri2,_)::tl2 -> (diff_ex tl1 tl2)
80 ;;
81
82
83
84 (*
85 let diff_ex l1 l2 =
86  let before = Sys.time () in
87  let res = diff_ex l1 l2 in
88  let after = Sys.time () in
89   let ll1 = string_of_int (List.length l1) in
90   let ll2 = string_of_int (List.length l2) in
91   let diff = string_of_float (after -. before) in
92   print_endline
93    ("DIFF(" ^ ll1 ^ ", " ^ ll2 ^ ") = " ^ string_of_int (List.length res) ^
94     ": " ^ diff ^ "s") ;
95   flush stdout ;
96   res
97 ;;
98 *)