]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/intersect.ml
e56d1d1f35b2c40ca06473efb32a3a6804f8060b
[helm.git] / helm / ocaml / mathql_interpreter / intersect.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 exception NotCompatible;;
27
28 (* Catenates two lists preserving order and getting rid of duplicates *)
29 let rec append (l1,l2) =
30   match l1,l2 with
31      [],_ -> l2
32    | _,[] -> l1
33    | s1::tl1, s2::_ when s1 < s2 -> s1::append (tl1,l2)                     
34    | s1::_, s2::tl2 when s2 < s1 -> s2::append (l1,tl2) 
35    | s1::tl1, s2::tl2 -> s1::append (tl1,tl2) 
36
37 ;;
38
39
40 (* Sums two attribute groups preserving order *)
41 let rec sum_groups(gr1, gr2) =
42   match gr1, gr2 with
43      [],_ -> gr2
44    | _,[] -> gr1
45    | (key1,l1)::tl1, (key2,l2)::_ when key1 < key2 -> (key1,l1)::(sum_groups (tl1,gr2))
46    | (key1,l1)::_, (key2,l2)::tl2 when key2 < key1 -> (key2,l2)::(sum_groups (gr1,tl2))
47    | (key1,l1)::tl1, (key2,l2)::tl2 -> (key1,(append (l1,l2)))::(sum_groups (tl1,tl2))
48
49 ;;
50
51 (* Product between an attribute set and a gropu of attributes *)
52 let rec sub_prod (aset, gr) =           (*prende un aset e un gr e fa la somma tra tutti i gruppi di aset e gr *)
53   match aset with
54       [] -> []
55     | gr1::tl1 -> sum_groups (gr1, gr)::(sub_prod(tl1, gr)) 
56 ;;
57
58
59 (* Cartesian product between two attribute sets*)
60 let rec prod (as1, as2) =
61   match as1, as2 with
62     [],_ -> as2
63   | _,[] -> as1
64   | gr1::tl1, _ -> (sub_prod (as2, gr1))@(prod (tl1, as2))  (* chiamo la sub_prod con un el. as1 e as2 *)
65 ;;
66
67 (* Intersection between two resource sets, preserves order and gets rid of duplicates *)
68 let intersect_ex rs1 rs2 =
69   let rec intersect_aux rs1 rs2 =
70     match (rs1, rs2) with
71        [],_
72      | _,[] -> []
73      | (uri1,as1)::tl1,
74        (uri2,as2)::_ when uri1 < uri2 -> intersect_aux tl1 rs2
75      | (uri1,as1)::_,
76        (uri2,as2)::tl2 when uri2 < uri1 -> intersect_aux rs1 tl2
77      | (uri1,as1)::tl1,
78        (uri2,as2)::tl2 -> (uri1, prod(as1,as2))::intersect_aux tl1 tl2 
79   in
80   let before = Sys.time () in
81   let res = intersect_aux rs1 rs2 in
82   let after = Sys.time () in
83   let ll1 = string_of_int (List.length rs1) in
84   let ll2 = string_of_int (List.length rs2) in
85   let diff = string_of_float (after -. before) in
86   print_endline
87    ("INTERSECT(" ^ ll1 ^ "," ^ ll2 ^ ") = " ^ string_of_int (List.length res) ^
88     ": " ^ diff ^ "s") ;
89   flush stdout ;
90   res
91 ;;
92
93 (*
94
95 let intersect_ex l1 l2 =
96  (* PRE-CLAUDIO
97  (*let _ = print_string ("INTERSECT ")
98  and t = Unix.time () in*)
99  let result = 
100   match (l1, l2) with
101      ((head1::tail1), (head2::tail2)) ->
102       (match (head1, head2) with
103           ([], _) -> assert false (* gli header non devono mai essere vuoti *)
104        |  (_, []) -> assert false (* devono contenere almeno [retVal] *)
105        |  (_,  _) ->
106            (match (tail1, tail2) with
107                ([], _) -> [["retVal"]] (* se una delle due code e' vuota... *)
108             |  (_, []) -> [["retVal"]] (* ... l'intersezione e' vuota *)
109             |  (_,  _) ->
110                 [head2 @
111                  (List.find_all
112                   (function t -> not (List.mem t head2))
113                   head1
114                  )
115                 ] (* header del risultato finale *)
116                 @
117                 intersect_tails (List.tl head1) tail1 (List.tl head2) tail2
118                 (*
119                 List.fold_left
120                  (fun par1 elem1 -> par1 @
121                   List.map
122                    (fun elem2 ->
123                     [(List.hd elem1)] @
124                     (xres_join_context (List.tl head1) (List.tl elem1)
125                                        (List.tl head2) (List.tl elem2)
126                     )
127                    )
128                    (List.find_all (* *)
129                     (fun elem2 ->    (* trova tutti gli elementi della lista tail2 *)
130                      ((List.hd elem1) = (List.hd elem2)) && (* che stanno in tail1 *)
131                      not ((xres_join_context (List.tl head1) (List.tl elem1)
132                                              (List.tl head2) (List.tl elem2)) = [])
133                      (* e per i quali la xres_join_context non sia vuota *)
134                     )
135                     tail2 (* List.find_all *)
136                    )
137                   )
138                 []
139                 tail1 (* per ogni elemento di tail1 applica la List.fold_left *)
140                 *)
141            ) (* match *)
142       )
143   | _ -> []
144    in
145     (*let _ = print_endline (string_of_float (Unix.time () -. t)); flush stdout in*)
146     result*)
147  let before = Sys.time () in
148  let res = intersect_ex l1 l2 in
149  let after = Sys.time () in
150   let ll1 = string_of_int (List.length l1) in
151   let ll2 = string_of_int (List.length l2) in
152   let diff = string_of_float (after -. before) in
153   print_endline
154    ("INTERSECT(" ^ ll1 ^ "," ^ ll2 ^ ") = " ^ string_of_int (List.length res) ^
155     ": " ^ diff ^ "s") ;
156   flush stdout ;
157   res
158 ;;
159
160 *)