1) ecrire la fonction hash et equal
[tatoo.git] / src / table.ml
index 91bb85e..f0e24ee 100644 (file)
@@ -1,4 +1,3 @@
-(*creer a 28/01/2014*)
 
 type move = Self
            | Firstchild
@@ -6,13 +5,59 @@ type move = Self
            | Revfirstchild
            | Prevsibling
 
-type query_tree = Binop of op * query_tree * query_tree
-                 | Axis of Xpath.Ast.axis * query_tree
-                 | Start 
-                 | Dom
-                 | Tag of QNameSet.t
+type query_tree_desc = Binop of op * query_tree * query_tree
+                      | Axis of Xpath.Ast.axis * query_tree
+                      | Start 
+                      | Dom
+                      | Tag of QNameSet.t * Tree.NodeKind.t
+
 and op = Union | Inter | Diff
 
+and query_tree = {
+  mutable desc  : query_tree_desc;
+  mutable id : int;
+  mutable hash : int;
+}
+
+
+module QTree = struct
+  type t = query_tree
+  let rec equal q1 q2 =
+    q1 == q2 ||
+      (q1.id == q2.id && q1.id != -1) ||
+      match q1.desc, q2.desc with
+       | Binop(op1,qt1,qt2),Binop(op2,qt3,qt4)-> op1==op2&& (equal qt1 qt3 && equal qt2 qt4) 
+                                                          
+       | Axis(a1,qt1),Axis(a2,qt2) -> compare_axis a1 a2 && equal 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 hash q = 
+    if q.hash != -1 then q.hash
+    else match q.desc with
+       Dom -> 1
+      | Start -> 3
+      | Tag(s,_) -> 5 + 17*QNameSet.hash s
+      | Axis(a,q) -> 7 + 17 * Hashtbl.hash a + 23* hash q
+      | Binop(op,q1,q2) -> 11 + 17* Hashtbl.hash op + 23* hash q1 + 27* hash q2
+
+end
+
+
+module QTreeHash = Hashtbl.Make(QTree)
+
+
+
 (*28/01/2014  
   parametres : tree  l'arbre xml
                n     un noeud
@@ -27,10 +72,10 @@ let print_node_list tree l =
   ) l
 
 let rec print_query_tree fmt q =
-  match q with
+  match q.desc with
       Dom -> Format.fprintf fmt "Dom"
     | Start -> Format.fprintf fmt "Start"
-    | Tag t -> Format.fprintf fmt "Tag(%a)" QNameSet.print t
+    | Tag (t,k) -> Format.fprintf fmt "Tag(%a, %a)" QNameSet.print t Tree.NodeKind.print k
     | Axis (a,q) ->
       Format.fprintf fmt "%a(%a)" Xpath.Ast.print_axis a print_query_tree q
     | Binop (op,q1,q2) -> 
@@ -103,54 +148,66 @@ and eval_star tree ls lr =
   retour : l'ensemble de noeuds qui correspondent ॆ l'axe
 *)
 
-let keep_elements t l =
+let keep_elements t l = (*
    List.filter (fun n -> match Naive_tree.kind t n with
-     | Element | Text | Document -> true | _ -> false) l
+     | Element | Text | Document | Attribute -> true | _ -> false) l
+                       *) l
+
+let keep_attributs t l = (*
+  List.filter (fun n -> match Naive_tree.kind t n with
+    | Attribute ->true | _ -> false) *) l
 
 let rec eval_axis tree ls a =
   let open Xpath.Ast in
-      let res =
-     (* let ls =  List.sort ( fun a b -> compare (Naive_tree.preorder tree a ) (Naive_tree.preorder tree b ) ) ls in ़crir  dans la log!!!!!*)
        match a with
            Self -> ls
              
-         | Attribute -> assert false
+         | Attribute -> let lfc = eval_move tree ls Firstchild in
+                        let lc = eval_star tree lfc [Nextsibling] in
+                        keep_attributs tree lc
            
          | Child -> let lfc = eval_move tree ls Firstchild in
-                    eval_star tree lfc [Nextsibling]
+                    let lc = eval_star tree lfc [Nextsibling] in
+                    keep_elements tree lc
                       
          | Descendant c -> let lfc = eval_move tree ls Firstchild in                
                            let ls2 = eval_star tree lfc [Firstchild;Nextsibling] in
-                           
-                         (* List.merge (compare_node tree) (if c then ls else [])
-                            (List.merge (compare_node tree) ls2 ls)*)
-                          
+                           let ldes =
                            if not c then ls2
                            else List.merge (compare_node tree) ls2 ls
+                           in
+                           keep_elements tree ldes
                              
          | FollowingSibling -> let lnexts = eval_move tree ls Nextsibling in
-                               eval_star tree lnexts [Nextsibling]
+                               let lfs = eval_star tree lnexts [Nextsibling] in
+                               keep_elements tree lfs
                                  
          | Parent -> let lprevs = eval_star tree ls [Prevsibling] in
-                     eval_move tree lprevs Revfirstchild
+                     let lp = eval_move tree lprevs Revfirstchild in
+                     keep_elements tree lp
                        
          | Ancestor b -> let ls2 = eval_star tree ls [Revfirstchild;Prevsibling] in
                          let ls3 = eval_move tree ls2 Revfirstchild in
+                         let lac =
                          if not b then ls3
                          else List.merge (compare_node tree ) ls3 ls
+                         in
+                         keep_elements tree lac
                            
          | PrecedingSibling -> let ls2 = eval_star tree ls [Prevsibling] in
-                               eval_move tree ls2 Prevsibling
+                               let lps = eval_move tree ls2 Prevsibling in
+                               keep_elements tree lps
                                  
          | Preceding -> let ls2 = eval_axis tree ls (Ancestor true) in
                         let ls3 = eval_axis tree ls2 PrecedingSibling in
-                        eval_axis tree ls3 (Descendant true) 
+                        let lp = eval_axis tree ls3 (Descendant true) in
+                        keep_elements tree lp
                         
          | Following -> let ls2 = eval_axis tree ls (Ancestor true) in
                         let ls3 = eval_axis tree ls2 FollowingSibling in
-                        eval_axis tree ls3 (Descendant true) 
-      in
-      keep_elements tree res
+                        let lf = eval_axis tree ls3 (Descendant true) in
+                        keep_elements tree lf
+