Remplacer la fonction List.sort par les fonctions d'evaluations. La liste de fonction...
[tatoo.git] / src / table.ml
index 8e4f2e6..5876e7e 100644 (file)
@@ -5,19 +5,158 @@ 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
 
-(*28/01/2014  
-  parametres : tree  l'arbre xml
-               n     un noeud
-               m     move   
-  retour :un noeud qui correspond ॆ la relation r
-*)
+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)
+
+let compare_node tree a b =
+  compare (Naive_tree.preorder tree a ) (Naive_tree.preorder tree b )
+
+module Tas = struct
+type 'a tas =
+  | Vide
+  | Noeud of 'a tas * 'a * 'a tas
+
+let comp_node tree a b = (Naive_tree.preorder tree a )< (Naive_tree.preorder tree b )
+  
+let rec size t =
+  match t with
+      Vide -> 0
+    | Noeud (t1,racine,t2) -> 1+ size t1 + size t2
+
+let rec height t =
+  match t with
+      Vide -> 0
+    | Noeud (t1,racine,t2) -> 1 + max (height t1) (height t2)
+
+let equilibre t =
+  let rec aux t =
+    match t with
+       Vide -> 0
+      | Noeud (t1,racine,t2) -> 1 + min (aux t1) (aux t2)
+  in
+  let max_h = height t in
+  let min_h = aux t in
+  if max_h- min_h >1 then false
+  else true
+
+let  is_tas t =
+  if not (equilibre t) then false
+  else
+    let rec aux n t =
+      match t with
+         Vide -> true
+       | Noeud (Vide,racine,Vide)  -> racine >= n
+       | Noeud (t1,racine, t2) -> (aux racine t1) && (aux racine t2)
+    in
+    aux 0 t
+
+let rec pop tree t =
+  match t with
+      Vide -> failwith "Tas vide"
+    | Noeud (t1, racine, t2) ->  begin
+      match t1,t2 with 
+         Vide,t2 -> t2
+       | t1,Vide -> t1
+       | Noeud (t3,r1,t4),Noeud (t5,r2,t6) -> if comp_node tree r1 r2 then Noeud (pop tree t1, r1,t2)
+         else Noeud (pop tree t2, r2, t1)   
+    end
+
+let rec push tree t a =
+  match t with
+      Vide -> Noeud(Vide,a,Vide)
+    | Noeud (t1,r,t2) ->  if comp_node tree a r then Noeud (t2,a,push tree t1 r)
+      else Noeud(t2,r, push tree t1 a)
+
+let tas_of_list tree l =
+  List.fold_left (push tree) Vide l
+
+let is_empty t = (size t )== 0
+
+let rec list_of_tas tree t =
+  match t with
+      Vide -> []
+    | Noeud(t1,r,t2) -> r::(list_of_tas tree  (pop tree t))
+
+let sort_of_list tree l =
+  let t = tas_of_list tree l in
+  list_of_tas tree t
+
+end
+
+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 print_node_list tree l =
   List.iter (fun node ->
@@ -26,10 +165,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) -> 
@@ -60,8 +199,7 @@ let rec eval_relation tree m n =
 *)
 
 
-let compare_node tree a b =
-  compare (Naive_tree.preorder tree a ) (Naive_tree.preorder tree b ) 
 
 let rec eval_move tree ls m =
   match m with
@@ -93,76 +231,139 @@ and eval_star tree ls lr =
     end
   done;
   let l = Hashtbl.fold (fun k _ acc -> k::acc) h [] in
-  List.sort (compare_node tree) l
+  l
+ (*  
+ Tas.sort_of_list tree l 
+  List.sort (compare_node tree) l*)
+
+let rec compare_node_list tree l1 l2 =
+  match l1,l2 with
+      [],[] -> 0
+    | _,[] -> 1
+    | [],_ -> -1
+    | n1::ll1,n2::ll2 -> let b = compare_node tree n1 n2 in
+                        if b=0 then compare_node_list tree ll1 ll2 
+                        else b
+                          
+let get_descendant tree ln =
+  let rec aux n acc =
+    if n == Naive_tree.nil then  acc
+    else let n1 = Naive_tree.first_child tree n in 
+        let acc1 = aux n1 (n::acc) in
+        let n2 = Naive_tree.next_sibling tree n in
+        let acc2 = aux n2 acc1 in
+        acc2
+  in
+  let l = List.fold_left (fun acc n -> if List.mem n acc then acc 
+    else let n1 = Naive_tree.first_child tree n in
+        aux n1 acc) [] ln 
+  in
+  List.rev l 
+
+let get_child tree ln =
+  let rec aux n acc =
+    if n == Naive_tree.nil then acc
+    else 
+      let n1 = Naive_tree.next_sibling tree n in
+      aux n1 (n::acc)
+  in
+  let ll = List.map (fun  n->
+    let n1 = Naive_tree.first_child tree n in
+    let res = aux n1 [] in
+    List.rev res
+  )  ln in
+  List.fold_left (fun acc l -> union_list tree acc l) [] ll
+
+  
+let get_followingSibling tree ln =
+  let rec aux n acc =
+    let n1 = Naive_tree.next_sibling tree n in
+    if n1 == Naive_tree.nil then acc
+    else aux n1 (n1::acc)
+  in
+  let ll = List.map (fun n -> let res = aux n [] in
+                             List.rev res ) ln in
+  List.fold_left (fun acc l1 -> union_list tree acc l1) [] ll 
+  
+let rec get_firstBling tree n pred =
+  if n== Naive_tree.nil then pred
+  else get_firstBling tree (Naive_tree.prev_sibling tree n) n
     
-(*28/01/2014  
-  parametres : tree  l'arbre xml
-               ls    l'ensemble de noeuds
-               a     axis   
-  retour : l'ensemble de noeuds qui correspondent ॆ l'axe
-*)
+let get_parent tree ln =
+  let l = List.fold_left (fun acc n ->
+    let n1 = get_firstBling tree n Naive_tree.nil in
+    let n2 = Naive_tree.parent_of_first tree n1 in
+    if n2 == Naive_tree.nil or List.mem n2 acc then acc
+    else union_list tree [n2] acc   
+  ) [] ln
+  in
+  l
 
-let keep_elements t l =
-   List.filter (fun n -> match Naive_tree.kind t n with
-     | Element | Text | Document | Attribute -> true | _ -> false) l
+  
 
-let keep_attributs t l =
-  List.filter (fun n -> match Naive_tree.kind t n with
-    | Attribute ->true | _ -> false) l
+let get_ancestor tree ln =
+  let rec aux tree l1 acc =
+    match l1 with
+       [] -> acc
+      | _ -> let ll1 = get_parent tree l1 in
+            let acc1 = union_list tree acc ll1 in
+            aux tree ll1  acc1
+  in  
+  let l = aux tree ln [] in
+  l
+
+let get_preSibling tree ln =
+  let rec aux n acc =
+    let n1 = Naive_tree.prev_sibling tree n in
+    if n1 == Naive_tree.nil then acc
+    else aux n1 (n1::acc)
+  in
+  let ll = List.map (fun n -> aux n [] ) ln in
+  List.fold_left (fun acc l1 -> union_list tree acc l1) [] ll 
+  
+    
 
 let rec eval_axis tree ls a =
   let open Xpath.Ast in
        match a with
-           Self -> keep_elements tree ls
+           Self -> ls
              
-         | Attribute -> let lfc = eval_move tree ls Firstchild in
-                        let lc = eval_star tree lfc [Nextsibling] in
-                        keep_attributs tree lc
+         | Attribute -> get_child tree ls
            
-         | Child -> let lfc = eval_move tree ls Firstchild in
-                    let lc = eval_star tree lfc [Nextsibling] in
-                    keep_elements tree lc
+         | Child -> get_child tree ls
                       
-         | Descendant c -> let lfc = eval_move tree ls Firstchild in                
-                           let ls2 = eval_star tree lfc [Firstchild;Nextsibling] in
+         | Descendant c -> let ls2 = get_descendant tree ls in
                            let ldes =
-                           if not c then ls2
-                           else List.merge (compare_node tree) ls2 ls
+                             if not c then ls2
+                             else union_list tree ls2 ls
                            in
-                           keep_elements tree ldes
+                           ldes
                              
-         | FollowingSibling -> let lnexts = eval_move tree ls Nextsibling in
-                               let lfs = eval_star tree lnexts [Nextsibling] in
-                               keep_elements tree lfs
+         | FollowingSibling -> get_followingSibling tree ls
                                  
-         | Parent -> let lprevs = eval_star tree ls [Prevsibling] in
-                     let lp = eval_move tree lprevs Revfirstchild in
-                     keep_elements tree lp
+         | Parent -> get_parent tree ls
                        
-         | Ancestor b -> let ls2 = eval_star tree ls [Revfirstchild;Prevsibling] in
-                         let ls3 = eval_move tree ls2 Revfirstchild in
+         | Ancestor b -> 
+                         let ls3 = get_ancestor tree ls in
                          let lac =
                          if not b then ls3
-                         else List.merge (compare_node tree ) ls3 ls
+                         else union_list tree ls3 ls    
                          in
-                         keep_elements tree lac
+                         lac
                            
-         | PrecedingSibling -> let ls2 = eval_star tree ls [Prevsibling] in
-                               let lps = eval_move tree ls2 Prevsibling in
-                               keep_elements tree lps
+         | PrecedingSibling -> get_preSibling tree ls
                                  
          | Preceding -> let ls2 = eval_axis tree ls (Ancestor true) in
                         let ls3 = eval_axis tree ls2 PrecedingSibling in
                         let lp = eval_axis tree ls3 (Descendant true) in
-                        keep_elements tree lp
+                        lp
                         
          | Following -> let ls2 = eval_axis tree ls (Ancestor true) in
                         let ls3 = eval_axis tree ls2 FollowingSibling in
                         let lf = eval_axis tree ls3 (Descendant true) in
-                        keep_elements tree lf
-
-
-
+                        lf
+