2 type query_tree_desc = Binop of op * query_tree * query_tree
3 | Axis of Xpath.Ast.axis * query_tree
6 | Tag of QNameSet.t * Tree.NodeKind.t
8 and op = Union | Inter | Diff
11 mutable desc : query_tree_desc;
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)
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
29 and compare_axis a1 a2 =
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
40 if q.hash != -1 then q.hash
41 else match q.desc with
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
51 module QTreeHash = Hashtbl.Make(QTree)
53 let compare_node tree a b =
54 compare (Naive_tree.preorder tree a ) (Naive_tree.preorder tree b )
56 let comp_node t n1 n2 = (Naive_tree.preorder t n1) < (Naive_tree.preorder t n2)
59 let rec union_list t l1 l2 =
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)
67 let rec merge_list t l1 l2 =
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)
75 let rec inter_list t l1 l2 =
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)
83 let rec diff_list t l1 l2 =
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
91 let print_node_list tree l =
92 List.iter (fun node ->
93 Naive_tree.print_xml stdout tree node;
97 let rec print_query_tree fmt q =
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
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)"
110 and print_binop fmt o =
112 | Union -> Format.fprintf fmt "Union"
113 | Inter -> Format.fprintf fmt "Inter"
114 | Diff -> Format.fprintf fmt "Diff"
117 let rec compare_node_list tree l1 l2 =
122 | n1::ll1,n2::ll2 -> let b = compare_node tree n1 n2 in
123 if b=0 then compare_node_list tree ll1 ll2
126 let get_list_ordred tree ll =
127 let l1 = List.fold_left (fun acc l -> merge_list tree acc l) [] ll in
130 let get_descendant tree ln =
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
139 let ll = List.map (fun n ->
140 let n1 = Naive_tree.first_child tree n in
142 get_list_ordred tree ll
143 (* let l = List.fold_left (fun acc n -> if List.mem n acc then acc
144 else let n1 = Naive_tree.first_child tree n in
149 let get_child tree ln =
151 if n == Naive_tree.nil then acc
153 let n1 = Naive_tree.next_sibling tree n in
156 let ll = List.map (fun n->
157 let n1 = Naive_tree.first_child tree n in
159 get_list_ordred tree ll
162 let get_followingSibling tree ln =
164 let n1 = Naive_tree.next_sibling tree n in
165 if n1 == Naive_tree.nil then acc
166 else aux n1 (n1::acc)
168 let ll = List.map (fun n -> aux n [] ) ln in
169 get_list_ordred tree ll
172 let rec get_firstBling tree n pred =
173 if n== Naive_tree.nil then pred
174 else get_firstBling tree (Naive_tree.prev_sibling tree n) n
176 let get_parent tree ln =
177 List.fold_left (fun acc n ->
178 let n1 = get_firstBling tree n Naive_tree.nil in
179 let n2 = Naive_tree.parent_of_first tree n1 in
180 if n2 != Naive_tree.nil then union_list tree [n2] acc
185 let get_ancestor tree ln =
186 let rec aux tree l1 acc =
189 | _ -> let ll1 = get_parent tree l1 in
190 let acc1 = union_list tree acc ll1 in
193 let l = aux tree ln [] in
196 let get_preSibling tree ln =
198 let n1 = Naive_tree.prev_sibling tree n in
199 if n1 == Naive_tree.nil then acc
200 else aux n1 (n1::acc)
202 let ll = List.map (fun n -> aux n [] ) ln in
203 List.fold_left (fun acc l1 -> union_list tree acc l1) [] ll
207 let rec eval_axis tree ls a =
208 let open Xpath.Ast in
212 | Attribute -> get_child tree ls
214 | Child -> get_child tree ls
216 | Descendant c -> let ls2 = get_descendant tree ls in
219 else union_list tree ls2 ls
223 | FollowingSibling -> get_followingSibling tree ls
225 | Parent -> get_parent tree ls
228 let ls3 = get_ancestor tree ls in
231 else union_list tree ls3 ls
235 | PrecedingSibling -> get_preSibling tree ls
237 | Preceding -> let ls2 = eval_axis tree ls (Ancestor true) in
238 let ls3 = eval_axis tree ls2 PrecedingSibling in
239 let lp = eval_axis tree ls3 (Descendant true) in
242 | Following -> let ls2 = eval_axis tree ls (Ancestor true) in
243 let ls3 = eval_axis tree ls2 FollowingSibling in
244 let lf = eval_axis tree ls3 (Descendant true) in