]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/intersect.ml
faster database format implemented
[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  * implementazione del comando INTERSECT
28  *)
29
30 (*
31  * eccezione sollevata quando il join dei contesti
32  * deve essere vuoto
33  *)
34 exception Join_must_be_empty;;
35
36 (*
37  * join fra due contesti
38  *)
39 let xres_join_context h1 l1 h2 l2 =
40  match (l1, l2) with
41     ([], _) -> l2
42  |  (_, []) -> l1
43  |  (_,  _) ->
44      let hh = h2 @ (List.find_all (function t -> not (List.mem t h2)) h1)
45      and m1 = List.combine h1 l1
46      and m2 = List.combine h2 l2
47      in
48        List.map
49         (fun elem ->
50          let value1 = try (List.assoc elem m1) with Not_found -> List.assoc elem m2
51          and value2 = try (List.assoc elem m2) with Not_found -> List.assoc elem m1
52          in
53           if value1 = value2 then value1 else raise Join_must_be_empty
54         )
55         hh
56 ;;
57
58 (*
59  *
60  *)
61 let intersect_tails h1 t1 h2 t2 =
62  let rec aux t1 t2 =
63   match (t1, t2) with
64      ([], _)
65   |  (_, []) -> []
66   |  ((l1::tl1)::tll1, (l2::tl2)::tll2) ->
67        if l1 = l2 then
68         try
69          (*match xres_join_context h1 tl1 h2 tl2 with
70             [] -> aux tll1 tll2
71           | t  -> (l1::(xres_join_context h1 tl1 h2 tl2))::(aux tll1 tll2)*)
72           (l1::(tl1 @ tl2))::(aux tll1 tll2)
73         with
74          Join_must_be_empty -> aux tll1 tll2
75        else
76         if l1 < l2 then
77          aux tll1 t2
78         else
79          aux t1 tll2
80    | _ -> assert false
81  in
82   aux t1 t2
83 ;;
84
85 (*
86  * implementazione del comando INTERSECT
87  *)
88 let intersect_ex l1 l2 =
89  let _ = print_string ("INTERSECT ")
90  and t = Unix.time () in
91   let result = 
92  match (l1, l2) with
93     ((head1::tail1), (head2::tail2)) ->
94      (match (head1, head2) with
95          ([], _) -> assert false (* gli header non devono mai essere vuoti *)
96       |  (_, []) -> assert false (* devono contenere almeno [retVal] *)
97       |  (_,  _) ->
98           (match (tail1, tail2) with
99               ([], _) -> [["retVal"]] (* se una delle due code e' vuota... *)
100            |  (_, []) -> [["retVal"]] (* ... l'intersezione e' vuota *)
101            |  (_,  _) ->
102                [head2 @
103                 (List.find_all
104                  (function t -> not (List.mem t head2))
105                  head1
106                 )
107                ] (* header del risultato finale *)
108                @
109                intersect_tails (List.tl head1) tail1 (List.tl head2) tail2
110                (*
111                List.fold_left
112                 (fun par1 elem1 -> par1 @
113                  List.map
114                   (fun elem2 ->
115                    [(List.hd elem1)] @
116                    (xres_join_context (List.tl head1) (List.tl elem1)
117                                       (List.tl head2) (List.tl elem2)
118                    )
119                   )
120                   (List.find_all (* *)
121                    (fun elem2 ->    (* trova tutti gli elementi della lista tail2 *)
122                     ((List.hd elem1) = (List.hd elem2)) && (* che stanno in tail1 *)
123                     not ((xres_join_context (List.tl head1) (List.tl elem1)
124                                             (List.tl head2) (List.tl elem2)) = [])
125                     (* e per i quali la xres_join_context non sia vuota *)
126                    )
127                    tail2 (* List.find_all *)
128                   )
129                  )
130                []
131                tail1 (* per ogni elemento di tail1 applica la List.fold_left *)
132                *)
133           ) (* match *)
134      )
135  | _ -> []
136   in
137    let _ = print_endline (string_of_float (Unix.time () -. t)); flush stdout in
138     result
139 ;;
140