]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/union.ml
e2d9fcb01a7e75be68bc141783eca03ccb1423bf
[helm.git] / helm / ocaml / mathql_interpreter / union.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 UNION
28  *)
29
30
31 (* Merges two attribute group lists preserves order and gets rid of duplicates*)
32 let rec merge l1 l2 =
33   match (l1,l2) with
34      [],l
35    | l,[] -> l
36    | g1::tl1,g2::_ when g1 < g2 -> g1::(merge tl1 l2)
37    | g1::_,g2::tl2 when g2 < g1 -> g2::(merge l1 tl2)
38    | g1::tl1,g2::tl2 -> g1::(merge tl1 tl2)
39 ;;       
40
41 (* preserves order and gets rid of duplicates *)
42 let rec union_ex rs1 rs2 =
43   match (rs1, rs2) with
44      [],l
45    | l,[] -> l
46    | (uri1,l1)::tl1,(uri2,_)::_ when uri1 < uri2 -> (uri1,l1)::(union_ex tl1 rs2)
47    | (uri1,_)::_,(uri2,l2)::tl2 when uri2 < uri1 -> (uri2,l2)::(union_ex rs1 tl2)
48    | (uri1,l1)::tl1,(uri2,l2)::tl2 -> if l1 = l2 then (uri1,l1)::(union_ex tl1 tl2)
49                                       else (uri1,merge l1 l2)::(union_ex tl1 tl2)       
50 ;;
51
52