optimiser la fonction eval_query_tree par l'ajoutation d'un hashtbl 'table_query_tree'
authorHuibo SHI <shihuibo19@gmail.com>
Tue, 18 Mar 2014 15:24:30 +0000 (16:24 +0100)
committerHuibo SHI <shihuibo19@gmail.com>
Tue, 18 Mar 2014 15:24:30 +0000 (16:24 +0100)
src/query_tree.ml
src/table_driver.ml

index 79aee48..018036b 100644 (file)
@@ -125,25 +125,35 @@ let debug tree q l =
     Format.pp_print_flush Format.std_formatter ();
   end
 
+let table_query_tree = Hashtbl.create 97 
 
+  
 let rec eval_query_tree tree start q =
   let resultat = 
-    match q with
-      | Start ->  start
-      | Dom -> all_nodes tree
-      | Tag t -> element_by_tag tree t
-      | 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
+    begin
+      try
+       Hashtbl.find table_query_tree q
+      with Not_found ->        
+       let res =
+       match q with
+         | Start -> start
+         | Dom ->  all_nodes tree          
+         | Tag t -> element_by_tag tree t                   
+         | 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
+       Hashtbl.add table_query_tree q res;
+       compteur := !compteur + (List.length res);
+       res
+    end
   in
   debug tree q resultat;
-  compteur := !compteur + (List.length resultat);
   resultat
-  
index d75c5df..371dbdf 100644 (file)
@@ -9,15 +9,16 @@ let main () =
   let () = Table_options.parse () in
   let doc =
     let fd, close_fd = match !Table_options.input_file with
-       None | Some "-" | Some "/dev/stdin" ->stdin,ignore (*qu'est-ce que c'est ignore?????*)
+       None | Some "-" | Some "/dev/stdin" ->stdin,ignore 
       | Some input ->
        let fd = open_in input in fd, fun() -> close_in fd
     in
     (*let inc = open_in Sys.argv.(1) in*)
     let doc = Naive_tree.load_xml_file fd in
-    close_fd();  (*ca sert a fermer fd????*)
+    close_fd(); 
     doc
   in
+  (*Hashtbl.clear Query_tree.table_query_tree;  il faut vider le hashtbl?????????????????*)
   let queries =
     List.map ( fun q ->
       parse_xpath q)