+ let is_valid b = b != (Obj.magic dummy_val)
+ let get_replace tab idx default =
+ let e = get tab idx in
+ if e == default then
+ let ne = Array.copy e in (set tab idx ne;ne)
+ else e
+
+ let add h tag fl s1 s2 (data: StateSet.t*bool*merge_conf) =
+ let l1 = get_replace h (Uid.to_int fl.Formlist.Node.id) default_line1 in
+ let l2 = get_replace l1 (Uid.to_int s1.StateSet.Node.id) default_line2 in
+ let l3 = get_replace l2 (Uid.to_int s2.StateSet.Node.id) default_line3 in
+ set l3 tag (Obj.repr data)
+ end
+
+
+ let empty_res = StateSet.empty,RS.empty
+
+ let top_down1 a tree t slist ctx td_trans h_fold2 =
+ (* evaluation starts from the right so we put sl1,res1 at the end *)
+ let eval_fold2_slist fll t tag (sl2,res2) (sl1,res1) =
+ let data = Fold3Res.find h_fold2 tag fll sl1 sl2 in
+ if Fold3Res.is_valid data then
+ let r,b,conf = data in
+ (r,if b then RS.merge conf t res1 res2 else RS.empty)
+ else
+ let r,conf = eval_formlist tag sl1 sl2 fll in
+ let b = conf <> NO in
+ (Fold3Res.add h_fold2 tag fll sl1 sl2 (r,b,conf);
+ (r, if b then RS.merge conf t res1 res2 else RS.empty))
+
+ in
+ let rec loop t ctx slist _ =
+ if t == Tree.nil then empty_res else
+ let tag = Tree.tag tree t in
+ (Code3Cache.get_fun td_trans slist tag) t ctx slist tag
+
+ and loop_tag t ctx slist tag =
+ if t == Tree.nil then empty_res else
+ (Code3Cache.get_fun td_trans slist tag) t ctx slist tag
+
+ and mk_trans t ctx slist tag =
+ let fl_list,llist,rlist,ca,da,sa,fa =
+ StateSet.fold
+ (fun q acc ->
+ List.fold_left
+ (fun ((fl_acc,ll_acc,rl_acc,c_acc,d_acc,s_acc,f_acc) as acc)
+ (ts,t) ->
+ if (TagSet.mem tag ts)
+ then
+ let _,_,_,f,_ = t.Transition.node in
+ let (child,desc,below),(sibl,foll,after) = Formula.st f in
+ (Formlist.cons t fl_acc,
+ StateSet.union ll_acc below,
+ StateSet.union rl_acc after,
+ StateSet.union child c_acc,
+ StateSet.union desc d_acc,
+ StateSet.union sibl s_acc,
+ StateSet.union foll f_acc)
+ else acc ) acc (
+ try Hashtbl.find a.trans q
+ with
+ Not_found -> Printf.eprintf "Looking for state %i, doesn't exist!!!\n%!"
+ q;[]
+ )
+
+ ) slist (Formlist.nil,StateSet.empty,StateSet.empty,
+ StateSet.empty,StateSet.empty,StateSet.empty,StateSet.empty)
+
+ in
+ (* Logic to chose the first and next function *)
+ let tags_child,tags_below,tags_siblings,tags_after = Tree.tags tree tag in
+ let d_f = Algebra.decide a tags_child tags_below (StateSet.union ca da) true in
+ let d_n = Algebra.decide a tags_siblings tags_after (StateSet.union sa fa) false in
+ let f_kind,first = choose_jump_down tree d_f
+ and n_kind,next = choose_jump_next tree d_n in
+
+ let cont =
+ match f_kind,n_kind with
+ | `NIL,`NIL ->
+ fun t _ _ tag -> eval_fold2_slist fl_list t tag empty_res empty_res
+
+ | _,`NIL -> (
+ match f_kind with
+ |`TAG(tag1) ->
+ (fun t _ _ tag -> eval_fold2_slist fl_list t tag empty_res
+ (loop_tag (first t) t llist tag1))
+ | _ ->
+ fun t _ _ tag -> eval_fold2_slist fl_list t tag empty_res
+ (loop (first t) t llist tag)
+ )
+ | `NIL,_ -> (
+ match n_kind with
+ |`TAG(tag2) ->
+ fun t ctx _ tag ->
+ eval_fold2_slist fl_list t tag
+ (loop_tag (next t ctx) ctx rlist tag2)
+ empty_res
+
+ | _ ->
+ fun t ctx _ tag ->
+ eval_fold2_slist fl_list t tag
+ (loop (next t ctx) ctx rlist tag)
+ empty_res
+
+ )
+
+ | `TAG(tag1),`TAG(tag2) ->
+ fun t ctx _ tag ->
+ eval_fold2_slist fl_list t tag
+ (loop_tag (next t ctx) ctx rlist tag2)
+ (loop_tag (first t) t llist tag1)
+
+ | `TAG(tag1),`ANY ->
+ fun t ctx _ tag ->
+ eval_fold2_slist fl_list t tag
+ (loop (next t ctx) ctx rlist tag)
+ (loop_tag (first t) t llist tag1)
+
+ | `ANY,`TAG(tag2) ->
+ fun t ctx _ tag ->
+ eval_fold2_slist fl_list t tag
+ (loop_tag (next t ctx) ctx rlist tag2)
+ (loop (first t) t llist tag)
+
+
+ | _,_ ->
+ fun t ctx _ tag ->
+ eval_fold2_slist fl_list t tag
+ (loop (next t ctx) ctx rlist tag)
+ (loop (first t) t llist tag)
+
+
+
+ in
+ Code3Cache.set_fun td_trans slist tag cont;
+ cont t ctx slist tag
+ in
+ let _ = Code3Cache.init mk_trans in
+ loop t ctx slist Tag.dummy
+
+
+ let run_top_down1 a tree =
+ let _,res = top_down1 a tree Tree.root a.init Tree.root (Code3Cache.create BIG_A_SIZE) (Fold3Res.create BIG_A_SIZE)
+ in
+ res
+
+ module Configuration =
+ struct
+ module Ptss = Set.Make(StateSet)
+ module IMap = Map.Make(StateSet)
+ type t = { hash : int;
+ sets : Ptss.t;
+ results : RS.t IMap.t }
+ let empty = { hash = 0;
+ sets = Ptss.empty;
+ results = IMap.empty;
+ }
+ let is_empty c = Ptss.is_empty c.sets
+ let add c s r =
+ if Ptss.mem s c.sets then
+ { c with results = IMap.add s (RS.concat r (IMap.find s c.results)) c.results}
+ else
+ { hash = HASHINT2(c.hash,Uid.to_int s.StateSet.Node.id);
+ sets = Ptss.add s c.sets;
+ results = IMap.add s r c.results
+ }
+
+ let pr fmt c = Format.fprintf fmt "{";
+ Ptss.iter (fun s -> StateSet.print fmt s;
+ Format.fprintf fmt " ") c.sets;
+ Format.fprintf fmt "}\n%!";
+ IMap.iter (fun k d ->
+ StateSet.print fmt k;
+ Format.fprintf fmt "-> %i\n" (RS.length d)) c.results;
+ Format.fprintf fmt "\n%!"
+
+ let merge c1 c2 =
+ let acc1 =
+ IMap.fold
+ ( fun s r acc ->
+ IMap.add s
+ (try
+ RS.concat r (IMap.find s acc)
+ with
+ | Not_found -> r) acc) c1.results IMap.empty
+ in
+ let imap =
+ IMap.fold (fun s r acc ->
+ IMap.add s
+ (try
+ RS.concat r (IMap.find s acc)
+ with
+ | Not_found -> r) acc) c2.results acc1
+ in
+ let h,s =
+ Ptss.fold
+ (fun s (ah,ass) -> (HASHINT2(ah, Uid.to_int s.StateSet.Node.id ),
+ Ptss.add s ass))
+ (Ptss.union c1.sets c2.sets) (0,Ptss.empty)
+ in
+ { hash = h;
+ sets =s;
+ results = imap }
+
+ end
+
+ let h_fold = Hashtbl.create 511
+
+ let fold_f_conf tree t slist fl_list conf dir=
+ let tag = Tree.tag tree t in
+ let rec loop sl fl acc =
+ match SList.node sl,fl with
+ |SList.Nil,[] -> acc
+ |SList.Cons(s,sll), formlist::fll ->
+ let r',mcnf =
+ let key = SList.hash sl,Formlist.hash formlist,dir in
+ try
+ Hashtbl.find h_fold key
+ with
+ Not_found -> let res =
+ if dir then eval_formlist tag s StateSet.empty formlist
+ else eval_formlist tag StateSet.empty s formlist
+ in (Hashtbl.add h_fold key res;res)
+ in
+ let (rb,rb1,rb2,mark) = bool_of_merge mcnf in
+ if rb && ((dir&&rb1)|| ((not dir) && rb2))
+ then
+ let acc =
+ let old_r =
+ try Configuration.IMap.find s conf.Configuration.results
+ with Not_found -> RS.empty
+ in
+ Configuration.add acc r' (if mark then RS.cons t old_r else old_r)
+ in
+ loop sll fll acc
+ else loop sll fll acc
+ | _ -> assert false
+ in
+ loop slist fl_list Configuration.empty
+
+ let h_trans = Hashtbl.create 4096
+
+ let get_up_trans slist ptag a tree =
+ let key = (HASHINT2(Uid.to_int slist.SList.Node.id ,ptag)) in
+ try
+ Hashtbl.find h_trans key
+ with
+ | Not_found ->
+ let f_list =
+ Hashtbl.fold (fun q l acc ->
+ List.fold_left (fun fl_acc (ts,t) ->
+ if TagSet.mem ptag ts then Formlist.cons t fl_acc
+ else fl_acc)
+
+ acc l)
+ a.trans Formlist.nil
+ in
+ let res = SList.fold (fun _ acc -> f_list::acc) slist []
+ in
+ (Hashtbl.add h_trans key res;res)
+
+
+
+ let h_tdconf = Hashtbl.create 511
+ let rec bottom_up a tree t conf next jump_fun root dotd init accu =
+ if (not dotd) && (Configuration.is_empty conf ) then
+ accu,conf,next
+ else
+
+ let below_right = Tree.is_below_right tree t next in
+
+ let accu,rightconf,next_of_next =
+ if below_right then (* jump to the next *)
+ bottom_up a tree next conf (jump_fun next) jump_fun (Tree.next_sibling tree t) true init accu
+ else accu,Configuration.empty,next
+ in
+ let sub =
+ if dotd then
+ if below_right then prepare_topdown a tree t true
+ else prepare_topdown a tree t false
+ else conf
+ in
+ let conf,next =
+ (Configuration.merge rightconf sub, next_of_next)
+ in
+ if t == root then accu,conf,next else
+ let parent = Tree.binary_parent tree t in
+ let ptag = Tree.tag tree parent in
+ let dir = Tree.is_left tree t in
+ let slist = Configuration.Ptss.fold (fun e a -> SList.cons e a) conf.Configuration.sets SList.nil in
+ let fl_list = get_up_trans slist ptag a parent in
+ let slist = SList.rev (slist) in
+ let newconf = fold_f_conf tree parent slist fl_list conf dir in
+ let accu,newconf = Configuration.IMap.fold (fun s res (ar,nc) ->
+ if StateSet.intersect s init then
+ ( RS.concat res ar ,nc)
+ else (ar,Configuration.add nc s res))
+ (newconf.Configuration.results) (accu,Configuration.empty)
+ in
+
+ bottom_up a tree parent newconf next jump_fun root false init accu
+
+ and prepare_topdown a tree t noright =
+ let tag = Tree.tag tree t in
+ let r =
+ try
+ Hashtbl.find h_tdconf tag
+ with
+ | Not_found ->
+ let res = Hashtbl.fold (fun q l acc ->
+ if List.exists (fun (ts,_) -> TagSet.mem tag ts) l
+ then StateSet.add q acc
+ else acc) a.trans StateSet.empty
+ in Hashtbl.add h_tdconf tag res;res
+ in
+(* let _ = pr ", among ";
+ StateSet.print fmt (Ptset.Int.elements r);
+ pr "\n%!";
+ in *)
+ let r = SList.cons r SList.nil in
+ let set,res = top_down (~noright:noright) a tree t r t 1 (CodeCache.create BIG_A_SIZE) (Fold2Res.create 1024) in
+ let set = match SList.node set with
+ | SList.Cons(x,_) ->x
+ | _ -> assert false
+ in
+ Configuration.add Configuration.empty set res.(0)
+
+
+
+ let run_bottom_up a tree k =
+ let t = Tree.root in
+ let trlist = Hashtbl.find a.trans (StateSet.choose a.init)
+ in
+ let init = List.fold_left
+ (fun acc (_,t) ->
+ let _,_,_,f,_ = Transition.node t in
+ let _,_,l = fst ( Formula.st f ) in
+ StateSet.union acc l)
+ StateSet.empty trlist
+ in
+ let tree1,jump_fun =
+ match k with
+ | `TAG (tag) ->
+ (*Tree.tagged_lowest t tag, fun tree -> Tree.tagged_next tree tag*)
+ (Tree.tagged_descendant tree tag t, let jump = Tree.tagged_following_below tree tag
+ in fun n -> jump n t )
+ | `CONTAINS(_) -> (Tree.text_below tree t,let jump = Tree.text_next tree
+ in fun n -> jump n t)
+ | _ -> assert false
+ in
+ let tree2 = jump_fun tree1 in
+ let rec loop t next acc =
+ let acc,conf,next_of_next = bottom_up a tree t
+ Configuration.empty next jump_fun (Tree.root) true init acc
+ in
+ let acc = Configuration.IMap.fold
+ ( fun s res acc -> if StateSet.intersect init s
+ then RS.concat res acc else acc) conf.Configuration.results acc
+ in
+ if Tree.is_nil next_of_next (*|| Tree.equal next next_of_next *)then
+ acc
+ else loop next_of_next (jump_fun next_of_next) acc
+ in
+ loop tree1 tree2 RS.empty
+
+
+ end
+
+ let top_down_count a t = let module RI = Run(Integer) in Integer.length (RI.run_top_down a t)
+ let top_down_count1 a t = let module RI = Run(Integer) in Integer.length (RI.run_top_down1 a t)
+ let top_down a t = let module RI = Run(IdSet) in (RI.run_top_down a t)
+ let top_down1 a t = let module RI = Run(IdSet) in (RI.run_top_down1 a t)
+ let bottom_up_count a t k = let module RI = Run(Integer) in Integer.length (RI.run_bottom_up a t k)
+ let bottom_up a t k = let module RI = Run(IdSet) in (RI.run_bottom_up a t k)
+
+ module Test (Doc : sig val doc : Tree.t end) =
+ struct
+ module Results = GResult(Doc)
+ let top_down a t = let module R = Run(Results) in (R.run_top_down a t)
+ let top_down1 a t = let module R = Run(Results) in (R.run_top_down1 a t)
+ end
+