1 (* Copyright (C) 2000, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 exception NotCompatible;;
28 (* intersect_attributes is successful iff there is no attribute with *)
29 (* two different values in the two lists. The returned list is the *)
30 (* union of the two lists. *)
31 let rec intersect_attributes (attr1, attr2) =
32 match attr1, attr2 with
35 | (key1,value1)::tl1, (key2,_)::_ when key1 < key2 ->
36 (key1,value1)::(intersect_attributes (tl1,attr2))
37 | (key1,_)::_, (key2,value2)::tl2 when key2 < key1 ->
38 (key2,value2)::(intersect_attributes (attr1,tl2))
39 | entry1::tl1, entry2::tl2 when entry1 = entry2 ->
40 entry1::(intersect_attributes (tl1,tl2))
41 | _, _ -> raise NotCompatible (* same keys, different values *)
44 (* preserves order and gets rid of duplicates *)
45 let rec intersect_ex l1 l2 =
46 let module S = Mathql_semantics in
50 | {S.uri = uri1}::tl1,
51 {S.uri = uri2}::_ when uri1 < uri2 -> intersect_ex tl1 l2
53 {S.uri = uri2}::tl2 when uri2 < uri1 -> intersect_ex l1 tl2
54 | {S.uri = uri1 ; S.attributes = attributes1}::tl1,
55 {S.uri = uri2 ; S.attributes = attributes2}::tl2 ->
57 let attributes' = intersect_attributes (attributes1,attributes2) in
58 {S.uri = uri1 ; S.attributes = attributes' ; S.extra = ""}::(intersect_ex tl1 tl2)
64 let intersect_ex l1 l2 =
66 (*let _ = print_string ("INTERSECT ")
67 and t = Unix.time () in*)
70 ((head1::tail1), (head2::tail2)) ->
71 (match (head1, head2) with
72 ([], _) -> assert false (* gli header non devono mai essere vuoti *)
73 | (_, []) -> assert false (* devono contenere almeno [retVal] *)
75 (match (tail1, tail2) with
76 ([], _) -> [["retVal"]] (* se una delle due code e' vuota... *)
77 | (_, []) -> [["retVal"]] (* ... l'intersezione e' vuota *)
81 (function t -> not (List.mem t head2))
84 ] (* header del risultato finale *)
86 intersect_tails (List.tl head1) tail1 (List.tl head2) tail2
89 (fun par1 elem1 -> par1 @
93 (xres_join_context (List.tl head1) (List.tl elem1)
94 (List.tl head2) (List.tl elem2)
98 (fun elem2 -> (* trova tutti gli elementi della lista tail2 *)
99 ((List.hd elem1) = (List.hd elem2)) && (* che stanno in tail1 *)
100 not ((xres_join_context (List.tl head1) (List.tl elem1)
101 (List.tl head2) (List.tl elem2)) = [])
102 (* e per i quali la xres_join_context non sia vuota *)
104 tail2 (* List.find_all *)
108 tail1 (* per ogni elemento di tail1 applica la List.fold_left *)
114 (*let _ = print_endline (string_of_float (Unix.time () -. t)); flush stdout in*)
116 let before = Unix.time () in
117 let res = intersect_ex l1 l2 in
118 let after = Unix.time () in
119 let ll1 = string_of_int (List.length l1) in
120 let ll2 = string_of_int (List.length l2) in
121 let diff = string_of_float (after -. before) in
123 ("INTERSECT(" ^ ll1 ^ "," ^ ll2 ^ ") = " ^ string_of_int (List.length res) ^