15d39bcb712b6ae08870ad844bc302b2392ce678
[tatoo.git] / src / table.ml
1
2 type query_tree_desc = Binop of op * query_tree * query_tree
3                        | Axis of Xpath.Ast.axis * query_tree
4                        | Start 
5                        | Dom
6                        | Tag of QNameSet.t * Tree.NodeKind.t
7
8 and op = Union | Inter | Diff
9
10 and query_tree = {
11   mutable desc  : query_tree_desc;
12   mutable id : int;
13   mutable hash : int;
14 }
15
16
17 module QTree = struct
18   type t = query_tree
19   let rec equal q1 q2 =
20     q1 == q2 ||
21       (q1.id == q2.id && q1.id != -1) ||
22       match q1.desc, q2.desc with
23         | Binop(op1,qt1,qt2),Binop(op2,qt3,qt4)-> op1==op2&& (equal qt1 qt3 && equal qt2 qt4) 
24                                                            
25         | Axis(a1,qt1),Axis(a2,qt2) -> compare_axis a1 a2 && equal qt1 qt2
26         | Tag(t1,k1),Tag(t2,k2) -> t1==t2&& k1==k2
27         | Dom,Dom | Start,Start -> true
28         | _,_ ->false
29   and compare_axis a1 a2 =
30     match a1,a2 with
31         Self ,Self | Attribute, Attribute | Child , Child | Parent , Parent
32       | FollowingSibling , FollowingSibling        
33       | PrecedingSibling , PrecedingSibling
34       | Preceding , Preceding | Following , Following -> true
35       | Descendant b1, Descendant b2 -> b1==b2 
36       | Ancestor b1, Ancestor b2 -> b1==b2
37       | _,_ -> false
38
39   let rec hash q = 
40     if q.hash != -1 then q.hash
41     else match q.desc with
42         Dom -> 1
43       | Start -> 3
44       | Tag(s,_) -> 5 + 17*QNameSet.hash s
45       | Axis(a,q) -> 7 + 17 * Hashtbl.hash a + 23* hash q
46       | Binop(op,q1,q2) -> 11 + 17* Hashtbl.hash op + 23* hash q1 + 27* hash q2
47
48 end
49
50
51 module QTreeHash = Hashtbl.Make(QTree)
52
53 let compare_node tree a b =
54   compare (Naive_tree.preorder tree a ) (Naive_tree.preorder tree b )
55
56 let comp_node t n1 n2 = (Naive_tree.preorder t n1) < (Naive_tree.preorder t n2)
57
58
59 let rec union_list t l1 l2 =
60   match l1,l2 with
61     | [],l2 -> l2
62     | l1, [] -> l1
63     | h1::ll1, h2::ll2 -> if (comp_node t h2 h1) then h2 :: (union_list t l1 ll2)
64       else if (comp_node t h1 h2) then h1::(union_list t ll1 l2)
65       else h1 ::(union_list t ll1 ll2)
66
67 let rec merge_list t l1 l2 =
68   match l1,l2 with
69     | [],l2 -> l2
70     | l1,[] -> l1
71     | h1::ll1, h2::ll2 -> if (comp_node t h2 h1) then h1:: (merge_list t ll1 l2)
72       else if (comp_node t h1 h2) then h2:: (merge_list t l1 ll2)
73       else h1::(merge_list t ll1 ll2)
74
75 let rec inter_list t l1 l2 =
76   match l1,l2 with
77     | [],l2 -> []
78     | l1, [] -> []
79     | h1::ll1, h2::ll2 -> if (comp_node t h1 h2) then inter_list t ll1 l2
80       else if (comp_node t h2 h1) then inter_list t l1 ll2
81       else h1 :: (inter_list t ll1 ll2)
82
83 let rec diff_list t l1 l2 =
84   match l1,l2 with
85     | [],l2 -> []
86     | l1, [] -> l1
87     | h1::ll1, h2::ll2 -> if (comp_node t h1 h2) then h1::(diff_list t ll1 l2)
88       else if (comp_node t h2 h1)  then h2 :: (diff_list t l1 ll2)
89       else diff_list t ll1 ll2
90
91 let print_node_list tree l =
92   List.iter (fun node ->
93     Naive_tree.print_xml stdout tree node;
94     print_newline() 
95   ) l
96
97 let rec print_query_tree fmt q =
98   match q.desc with
99       Dom -> Format.fprintf fmt "Dom"
100     | Start -> Format.fprintf fmt "Start"
101     | Tag (t,k) -> Format.fprintf fmt "Tag(%a, %a)" QNameSet.print t Tree.NodeKind.print k
102     | Axis (a,q) ->
103       Format.fprintf fmt "%a(%a)" Xpath.Ast.print_axis a print_query_tree q
104     | Binop (op,q1,q2) -> 
105       Format.fprintf fmt "%a(%a, %a)"
106       print_binop  op
107       print_query_tree  q1 
108       print_query_tree  q2 
109  
110 and print_binop fmt o =
111   match o with
112     | Union -> Format.fprintf fmt "Union"
113     | Inter -> Format.fprintf fmt "Inter"
114     | Diff -> Format.fprintf fmt "Diff"
115
116            
117 let rec compare_node_list tree l1 l2 =
118   match l1,l2 with
119       [],[] -> 0
120     | _,[] -> 1
121     | [],_ -> -1
122     | n1::ll1,n2::ll2 -> let b = compare_node tree n1 n2 in
123                          if b=0 then compare_node_list tree ll1 ll2 
124                          else b
125
126 let get_list_ordred tree ll =
127   let l1 = List.fold_left (fun acc l -> merge_list tree acc l) [] ll in
128   List.rev l1
129
130 let get_descendant tree ln =
131   let rec aux n acc =
132     if n == Naive_tree.nil then  acc
133     else let n1 = Naive_tree.first_child tree n in 
134          let acc1 = aux n1 (n::acc) in
135          let n2 = Naive_tree.next_sibling tree n in
136          let acc2 = aux n2 acc1 in
137          acc2
138   in
139   let ll = List.map (fun n -> 
140     let n1 = Naive_tree.first_child tree n in
141     aux n1 [] ) ln in
142   get_list_ordred tree ll
143
144 let get_child tree ln =
145   let rec aux n acc =
146     if n == Naive_tree.nil then acc
147     else 
148       let n1 = Naive_tree.next_sibling tree n in
149       aux n1 (n::acc)
150   in
151   let ll = List.map (fun  n->
152     let n1 = Naive_tree.first_child tree n in
153     aux n1 [] )  ln in
154   get_list_ordred tree ll
155
156   
157 let get_followingSibling tree ln =
158   let rec aux n acc =
159     let n1 = Naive_tree.next_sibling tree n in
160     if n1 == Naive_tree.nil then acc
161     else aux n1 (n1::acc)
162   in
163   let ll = List.map (fun n -> aux n [] ) ln in
164   get_list_ordred tree ll
165   
166  
167 let rec get_firstBling tree n pred =
168   if n== Naive_tree.nil then pred
169   else get_firstBling tree (Naive_tree.prev_sibling tree n) n
170     
171 let get_parent tree ln =
172   List.fold_left (fun acc n ->
173     let n1 = get_firstBling tree n Naive_tree.nil in
174     let n2 = Naive_tree.parent_of_first tree n1 in
175     if n2 != Naive_tree.nil then union_list tree [n2] acc   
176     else acc
177   ) [] ln
178  
179
180 let get_ancestor tree ln =
181   let rec aux tree l1 acc =
182     match l1 with
183         [] -> acc
184       | _ -> let ll1 = get_parent tree l1 in
185              let acc1 = union_list tree acc ll1 in
186              aux tree ll1  acc1
187   in  
188   let l = aux tree ln [] in
189   l
190
191 let get_preSibling tree ln =
192   let rec aux n acc =
193     let n1 = Naive_tree.prev_sibling tree n in
194     if n1 == Naive_tree.nil then acc
195     else aux n1 (n1::acc)
196   in
197   let ll = List.map (fun n -> aux n [] ) ln in
198   List.fold_left (fun acc l1 -> union_list tree acc l1) [] ll 
199   
200     
201
202 let rec eval_axis tree ls a =
203   let open Xpath.Ast in
204         match a with
205             Self -> ls
206               
207           | Attribute -> get_child tree ls
208             
209           | Child -> get_child tree ls
210                        
211           | Descendant c -> let ls2 = get_descendant tree ls in
212                             let ldes =
213                               if not c then ls2
214                               else union_list tree ls2 ls
215                             in
216                             ldes
217                               
218           | FollowingSibling -> get_followingSibling tree ls
219                                   
220           | Parent -> get_parent tree ls
221                         
222           | Ancestor b -> 
223                           let ls3 = get_ancestor tree ls in
224                           let lac =
225                           if not b then ls3
226                           else union_list tree ls3 ls    
227                           in
228                           lac
229                             
230           | PrecedingSibling -> get_preSibling tree ls
231                                   
232           | Preceding -> let ls2 = eval_axis tree ls (Ancestor true) in
233                          let ls3 = eval_axis tree ls2 PrecedingSibling in
234                          let lp = eval_axis tree ls3 (Descendant true) in
235                          lp
236                          
237           | Following -> let ls2 = eval_axis tree ls (Ancestor true) in
238                          let ls3 = eval_axis tree ls2 FollowingSibling in
239                          let lf = eval_axis tree ls3 (Descendant true) in
240                          lf
241       
242              
243
244
245
246
247