X-Git-Url: http://git.nguyen.vg/gitweb/?a=blobdiff_plain;f=src%2Fquery_tree.ml;h=d9a226fe973933688cbf57d5014c90594f43d96e;hb=72818d02fb469c39a3d8043300152beae3e7e162;hp=02ed498c67df3e9f70cff8a67b97399d41c6778b;hpb=273665d1294e6253fbd45137471e7dd568b38735;p=tatoo.git diff --git a/src/query_tree.ml b/src/query_tree.ml index 02ed498..d9a226f 100644 --- a/src/query_tree.ml +++ b/src/query_tree.ml @@ -1,14 +1,20 @@ open Table let compteur = ref 0 +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 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 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 mk_node q = {desc = q; id = -1; hash = -1} let rec compile_single_path p = let open Xpath.Ast in @@ -17,24 +23,24 @@ let rec compile_single_path p = and compile_step_list p = match p with - | [] -> Start + | [] -> mk_node Start | (a,(test,kind),el) :: r -> let qtree = compile_step_list r in - let res = Binop ( Inter,Axis (a,qtree), Tag (test,kind) ) in + let res = mk_node ( Binop ( Inter,mk_node( Axis (a,qtree)),mk_node (Tag (test,kind) )) ) in List.fold_left (fun acc e -> - Binop(Inter, acc, compile_expr e)) res el + mk_node (Binop(Inter, acc, compile_expr e))) res el and compile_expr (e : Xpath.Ast.expr ) = match e with | Fun_call (f, [ e0 ]) when (QName.to_string f) = "not" -> let qtree = compile_expr e0 in - Binop (Diff , Dom, qtree) + mk_node (Binop (Diff , mk_node (Dom), qtree)) | Binop (e1,op,e2) -> let qtree1 = compile_expr e1 in let qtree2 = compile_expr e2 in begin match op with - | Or -> Binop (Union , qtree1,qtree2) - | And -> Binop (Inter ,qtree1,qtree2) + | Or -> mk_node (Binop (Union , qtree1,qtree2)) + | And -> mk_node (Binop (Inter ,qtree1,qtree2)) | _ -> failwith "Unknown operator" end | Path p -> compile_path_rev p @@ -44,21 +50,21 @@ and compile_step_list p = match p with | [] -> assert false | [p] -> compile_single_path_rev p - | p::r -> List.fold_left (fun acc p -> Binop (Union , acc, compile_single_path_rev p) ) (compile_single_path_rev p) r + | p::r -> List.fold_left (fun acc p -> mk_node (Binop (Union , acc, compile_single_path_rev p)) ) (compile_single_path_rev p) r and compile_single_path_rev p = match p with - | Absolute p | Relative p -> compile_step_list_rev p (*(List.rev p)*) + | Absolute p | Relative p -> compile_step_list_rev p and compile_step_list_rev p = match p with - | [] -> Dom + | [] -> mk_node Dom | (a,(test,kind),el) :: r -> let qtree = compile_step_list_rev r in - let res = Binop (Inter , qtree, Tag(test,kind)) in + let res = mk_node (Binop (Inter , qtree,mk_node (Tag(test,kind)))) in let qtree2 = List.fold_left (fun acc e -> - Binop(Inter, acc, compile_expr e)) res el in + mk_node (Binop(Inter, acc, compile_expr e))) res el in let a_rev = axis_rev a in - Axis (a_rev , qtree2) + mk_node (Axis (a_rev , qtree2)) and axis_rev a = @@ -83,35 +89,10 @@ and compile_step_list p = let compile_xpath p = match p with | [] -> assert false | [p] -> compile_single_path p - | p::r -> List.fold_left (fun acc p -> Binop (Union , acc, compile_single_path p) ) (compile_single_path p) r + | 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 @@ -129,36 +110,64 @@ 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 mini_id = ref 0 +let mini_table = QTreeHash.create 17 + +let rec minimize_qtree q = + if q.id != -1 then q + else + try + QTreeHash.find mini_table q + with Not_found -> + let mdesc = + match q.desc with + (Start | Dom | Tag _) as d -> d + | Binop(op,q1,q2) -> let mq1 = minimize_qtree q1 in + let mq2 = minimize_qtree q2 in + Binop(op,mq1,mq2) + | Axis(a,q1) -> let mq1 = minimize_qtree q1 in + Axis(a,mq1) + in + q.desc <- mdesc; + q.hash <- QTree.hash q; + q.id <- !mini_id; + incr mini_id; + QTreeHash.add mini_table q q; + q + + + +let rec eval_qtree tree start q = let resultat = begin try - Hashtbl.find table_query_tree q + QTreeHash.find table_qtree q with Not_found -> let res = - match q with + 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_query_tree 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_query_tree tree start q1 in - let ls2 = eval_query_tree 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 = List.sort (Table.compare_node tree) res in - Hashtbl.add table_query_tree q res; - compteur := !compteur + (List.length res); + QTreeHash.add table_qtree q res; + compteur := !compteur + Bitvector.length res; (*????8*) res end in - debug tree q resultat; + (* debug tree q resultat;*) resultat