]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/intersect.ml
the db connection parameters are now parametrized istead of hard-coded
[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
27 (* Catenates two lists preserving order and getting rid of duplicates *)
28 let rec append (l1,l2) =
29   match l1,l2 with
30      [],_ -> l2
31    | _,[] -> l1
32    | s1::tl1, s2::_ when s1 < s2 -> s1::append (tl1,l2)                     
33    | s1::_, s2::tl2 when s2 < s1 -> s2::append (l1,tl2) 
34    | s1::tl1, s2::tl2 -> s1::append (tl1,tl2) 
35
36 ;;
37
38
39 (* Sums two attribute groups preserving order *)
40 let rec sum_groups(gr1, gr2) =
41   match gr1, gr2 with
42      [],_ -> gr2
43    | _,[] -> gr1
44    | (key1,l1)::tl1, (key2,l2)::_ when key1 < key2 -> (key1,l1)::(sum_groups (tl1,gr2))
45    | (key1,l1)::_, (key2,l2)::tl2 when key2 < key1 -> (key2,l2)::(sum_groups (gr1,tl2))
46    | (key1,l1)::tl1, (key2,l2)::tl2 -> (key1,(append (l1,l2)))::(sum_groups (tl1,tl2))
47
48 ;;
49
50 (* Product between an attribute set and a group of attributes *)
51 let rec sub_prod (aset, gr) =           (*prende un aset e un gr e fa la somma tra tutti i gruppi di aset e gr *)
52   match aset with
53       [] -> []
54     | gr1::tl1 -> sum_groups (gr1, gr)::(sub_prod(tl1, gr)) 
55 ;;
56
57
58 (* Cartesian product between two attribute sets*)
59 let rec prod (as1, as2) =
60   match as1, as2 with
61     [],_ -> as2
62   | _,[] -> as1
63   | gr1::tl1, _ -> (sub_prod (as2, gr1))@(prod (tl1, as2))  (* chiamo la sub_prod con un el. as1 e as2 *)
64 ;;
65
66 (* Intersection between two resource sets, preserves order and gets rid of duplicates *)
67 let intersect_ex rs1 rs2 =
68   let rec intersect_aux rs1 rs2 =
69     match (rs1, rs2) with
70        [],_
71      | _,[] -> []
72      | (uri1,_)::tl1,
73        (uri2,_)::_ when uri1 < uri2 -> intersect_aux tl1 rs2
74      | (uri1,_)::_,
75        (uri2,_)::tl2 when uri2 < uri1 -> intersect_aux rs1 tl2
76      | (uri1,as1)::tl1,
77        (uri2,as2)::tl2 -> (uri1, prod(as1,as2))::intersect_aux tl1 tl2 
78   in
79   let before = Sys.time () in
80   let res = intersect_aux rs1 rs2 in
81   let after = Sys.time () in
82   let ll1 = string_of_int (List.length rs1) in
83   let ll2 = string_of_int (List.length rs2) in
84   let diff = string_of_float (after -. before) in
85   print_endline
86    ("INTERSECT(" ^ ll1 ^ "," ^ ll2 ^ ") = " ^ string_of_int (List.length res) ^
87     ": " ^ diff ^ "s") ;
88   flush stdout ;
89   res
90 ;;
91