1) Utiliser bitvector pour preserver l'ordre pendant l'evaluation
[tatoo.git] / src / query_tree.ml
index 1f58ba6..d9a226f 100644 (file)
@@ -1,16 +1,18 @@
 open Table
 
 let compteur = ref 0
-let table_query_tree = Hashtbl.create 97 
 let table_qtree = QTreeHash.create 97
-
-let all_nodes tree = let root = Naive_tree.root tree in
-                    eval_axis tree [root] (Descendant true)
-
-let element_by_tag tree tagset kind = let dom = all_nodes tree in
-                             List.filter (fun c ->
-                               Tree.NodeKind.is_a (Naive_tree.kind tree c) kind &&
-                               QNameSet.mem (Naive_tree.tag tree c) tagset ) dom
+                       
+
+let element_by_tag tree tagset kind = let v = Bitvector.create (Naive_tree.size tree) in
+                                     for i=0 to (Bitvector.length v)-1 do
+                                       let c = Naive_tree.by_preorder tree i in
+                                       if (Tree.NodeKind.is_a (Naive_tree.kind tree c) kind &&
+                                             QNameSet.mem (Naive_tree.tag tree c) tagset )
+                                       then Bitvector.set v i true
+                                     done;
+                                     v
+                        
 
 let mk_node q = {desc = q; id = -1; hash = -1}
 
@@ -89,32 +91,7 @@ let compile_xpath p = match p with
   | [p] -> compile_single_path p
   | p::r -> List.fold_left (fun acc p -> mk_node (Binop (Union , acc, compile_single_path p) )) (compile_single_path p) r
 
-let comp_node t n1 n2 = (Naive_tree.preorder t n1) < (Naive_tree.preorder t n2)
-
-
-let rec union_list t l1 l2 =
-  match l1,l2 with
-    | [],l2 -> l2
-    | l1, [] -> l1
-    | h1::ll1, h2::ll2 -> if (comp_node t h2 h1) then h2 :: (union_list t l1 ll2)
-      else if (comp_node t h1 h2) then h1::(union_list t ll1 l2)
-      else h1 ::(union_list t ll1 ll2)
 
-let rec inter_list t l1 l2 =
-  match l1,l2 with
-    | [],l2 -> []
-    | l1, [] -> []
-    | h1::ll1, h2::ll2 -> if (comp_node t h1 h2) then inter_list t ll1 l2
-      else if (comp_node t h2 h1) then inter_list t l1 ll2
-      else h1 :: (inter_list t ll1 ll2)
-
-let rec diff_list t l1 l2 =
-  match l1,l2 with
-    | [],l2 -> []
-    | l1, [] -> l1
-    | h1::ll1, h2::ll2 -> if (comp_node t h1 h2) then h1::(diff_list t ll1 l2)
-      else if (comp_node t h2 h1)  then h2 :: (diff_list t l1 ll2)
-      else diff_list t ll1 ll2
 
 
 let do_debug = ref false
@@ -133,60 +110,6 @@ let debug tree q l =
     Format.pp_print_flush Format.std_formatter ();
   end
 
-
-
-
-let rec compare_query_tree q1 q2 =
-     q1.id==q2.id|| match q1.desc,q2.desc with
-     | Binop(op1,qt1,qt2),Binop(op2,qt3,qt4)->op1==op2&& ((compare_query_tree qt1 qt3 && compare_query_tree qt2 qt4) 
-                                                         ||  (compare_query_tree qt1 qt4 && compare_query_tree qt2 qt3))
-     | Axis(a1,qt1),Axis(a2,qt2) -> compare_axis a1 a2 && compare_query_tree qt1 qt2
-     | Tag(t1,k1),Tag(t2,k2) ->t1==t2&& k1==k2
-     | Dom,Dom | Start,Start ->true
-     | _,_ ->false
-
-and compare_axis a1 a2 =
-  match a1,a2 with
-      Self ,Self | Attribute, Attribute | Child , Child | Parent , Parent
-    | FollowingSibling , FollowingSibling         
-    | PrecedingSibling , PrecedingSibling
-    | Preceding , Preceding | Following , Following -> true
-    | Descendant b1, Descendant b2 -> b1==b2 
-    | Ancestor b1, Ancestor b2 -> b1==b2
-    | _,_ -> false
-
-
-let rec eval_query_tree tree start q =
-  let resultat = 
-    begin
-      try
-       Hashtbl.find table_query_tree q
-      with Not_found ->        
-       let res =
-       match q.desc with
-         | Start -> start
-         | Dom ->  all_nodes tree          
-         | Tag (t,k) -> element_by_tag tree t k                     
-         | Axis (a,q1) -> let ls = eval_query_tree tree start q1 in
-                          eval_axis tree ls a                     
-         | Binop (op,q1,q2)-> begin
-           let ls1 = eval_query_tree tree start q1 in
-           let ls2 = eval_query_tree tree start q2 in
-           match op with         
-             | Union -> union_list tree ls1 ls2            
-             | Inter -> inter_list tree ls1 ls2
-             | Diff -> diff_list tree ls1 ls2
-         end
-       in
-       let res = List.sort (Table.compare_node tree) res in
-       Hashtbl.add table_query_tree q res;
-       compteur := !compteur + (List.length res);
-       res
-    end
-  in
-  debug tree q resultat;
-  resultat
-
 let mini_id = ref 0
 let mini_table = QTreeHash.create 17 
 
@@ -223,25 +146,28 @@ let rec eval_qtree tree start q =
        let res =
        match q.desc with
          | Start -> start
-         | Dom ->  all_nodes tree          
+         | Dom -> (* Bitvector.create true (Naive_tree.size tree)*)
+           let v = Bitvector.create (Naive_tree.size tree) in
+           for i=0 to (Bitvector.length v)-1 do
+             Bitvector.set v i true
+           done;
+           v
          | Tag (t,k) -> element_by_tag tree t k                     
-         | Axis (a,q1) -> let ls = eval_qtree tree start q1 in
-                          eval_axis tree ls a                     
+         | Axis (a,q1) -> let v = eval_qtree tree start q1 in
+                          eval_axis tree v a                      
          | Binop (op,q1,q2)-> begin
-           let ls1 = eval_qtree tree start q1 in
-           let ls2 = eval_qtree tree start q2 in
+           let v1 = eval_qtree tree start q1 in
+           let v2 = eval_qtree tree start q2 in
            match op with         
-             | Union -> union_list tree ls1 ls2            
-             | Inter -> inter_list tree ls1 ls2
-             | Diff -> diff_list tree ls1 ls2
+             | Union -> Bitvector.union v1 v2      
+             | Inter -> Bitvector.inter v1 v2
+             | Diff -> Bitvector.diff  v1 v2
          end
        in
-       let res = Tas.sort_of_list tree res in
-(*     let res = List.sort (Table.compare_node tree) res in*)
        QTreeHash.add table_qtree q res;
-       compteur := !compteur + (List.length res);
+       compteur := !compteur + Bitvector.length res; (*????8*)
        res
     end
   in
-  debug tree q resultat;
+ (* debug tree q resultat;*)
   resultat