]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/intersect.ml
intersect improved in speed
[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         with
73          Join_must_be_empty -> aux tll1 tll2
74        else
75         if l1 < l2 then
76          aux tll1 t2
77         else
78          aux t1 tll2
79    | _ -> assert false
80  in
81   aux t1 t2
82 ;;
83
84 (*
85  * implementazione del comando INTERSECT
86  *)
87 let intersect_ex l1 l2 =
88  match (l1, l2) with
89     ((head1::tail1), (head2::tail2)) ->
90      (match (head1, head2) with
91          ([], _) -> assert false (* gli header non devono mai essere vuoti *)
92       |  (_, []) -> assert false (* devono contenere almeno [retVal] *)
93       |  (_,  _) ->
94           (match (tail1, tail2) with
95               ([], _) -> [["retVal"]] (* se una delle due code e' vuota... *)
96            |  (_, []) -> [["retVal"]] (* ... l'intersezione e' vuota *)
97            |  (_,  _) ->
98                [head2 @
99                 (List.find_all
100                  (function t -> not (List.mem t head2))
101                  head1
102                 )
103                ] (* header del risultato finale *)
104                @
105                intersect_tails (List.tl head1) tail1 (List.tl head2) tail2
106                (*
107                List.fold_left
108                 (fun par1 elem1 -> par1 @
109                  List.map
110                   (fun elem2 ->
111                    [(List.hd elem1)] @
112                    (xres_join_context (List.tl head1) (List.tl elem1)
113                                       (List.tl head2) (List.tl elem2)
114                    )
115                   )
116                   (List.find_all (* *)
117                    (fun elem2 ->    (* trova tutti gli elementi della lista tail2 *)
118                     ((List.hd elem1) = (List.hd elem2)) && (* che stanno in tail1 *)
119                     not ((xres_join_context (List.tl head1) (List.tl elem1)
120                                             (List.tl head2) (List.tl elem2)) = [])
121                     (* e per i quali la xres_join_context non sia vuota *)
122                    )
123                    tail2 (* List.find_all *)
124                   )
125                  )
126                []
127                tail1 (* per ogni elemento di tail1 applica la List.fold_left *)
128                *)
129           ) (* match *)
130      )
131  | _ -> []
132 ;;
133