- let timeall f x =
- let t1 = Unix.gettimeofday ()
- in let r = f x
- in
- timerefall := !timerefall +. ((Unix.gettimeofday()) -. t1);
- r
-
-*)
-
-
- let eval_form_bool f s1 s2 =
- let rec eval f = match f.pos with
- | Atom(`Left,b,q) -> if b == (Ptset.mem q s1) then (true,true,false) else false,false,false
- | Atom(`Right,b,q) -> if b == (Ptset.mem q s2) then (true,false,true) else false,false,false
- (* test some inlining *)
- | True -> true,true,true
- | False -> false,false,false
- | _ ->
- try
- HFEval.find hfeval (f.fid,s1,s2)
- with
- | Not_found -> let r =
- match f.pos with
- | Or(f1,f2) ->
- let b1,rl1,rr1 = eval f1
- in
- if b1 && rl1 && rr1 then (true,true,true)
- else
- let b2,rl2,rr2 = eval f2
- in
- let rl1,rr1 = if b1 then rl1,rr1 else false,false
- and rl2,rr2 = if b2 then rl2,rr2 else false,false
- in (b1 || b2, rl1||rl2,rr1||rr2)
- | And(f1,f2) ->
- let b1,rl1,rr1 = eval f1 in
- if b1 && rl1 && rr1 then (true,true,true)
- else if b1
- then let b2,rl2,rr2 = eval f2 in
- if b2 then (true,rl1||rl2,rr1||rr2)
- else (false,false,false)
- else (false,false,false)
- | _ -> assert false
- in
- HFEval.add hfeval (f.fid,s1,s2) r;
- r
- in eval f
-
-
- module HFEvalDir = Hashtbl.Make(
- struct
- type t = int*Ptset.t*[`Left | `Right ]
- let equal (a,b,c) (d,e,f) =
- a==d && (Ptset.equal b e) && (c = f)
- let hash_dir = function `Left -> 7919
- | `Right -> 3517
-
- let hash (a,b,c) =
- a+17*(Ptset.hash b) + 31*(hash_dir c)
- end)
-
- let hfeval_dir = HFEvalDir.create 4097
-
-
- let eval_dir dir f s =
- let rec eval f = match f.pos with
- | Atom(d,b,q) when d = dir -> if b == (Ptset.mem q s) then true_ else false_
- | Atom(_,b,q) -> f
- (* test some inlining *)
- | True -> true_
- | False -> false_
- | _ ->
- try
- HFEvalDir.find hfeval_dir (f.fid,s,dir)
- with
- | Not_found ->
- let r =
- match f.pos with
- | Or(f1,f2) ->
- let f1 = eval f1
- in
- if is_true f1 then true_
- else if is_false f1 then eval f2
- else or_ f1 f2
- | And(f1,f2) ->
- let f1 = eval f1 in
- if is_false f1 then false_
- else if is_true f1 then eval f2
- else and_ f1 f2
- | _ -> assert false
+ module CachedTransTable = Hashtbl.Make(SetTagKey)
+ let td_trans = CachedTransTable.create 4093
+
+
+ let empty_size n =
+ let rec loop acc = function 0 -> acc
+ | n -> loop (SList.cons StateSet.empty acc) (n-1)
+ in loop SList.nil n
+
+ let merge rb rb1 rb2 mark t res1 res2 =
+ if rb then
+ let res1 = if rb1 then res1 else RS.empty
+ and res2 = if rb2 then res2 else RS.empty
+ in
+ if mark then RS.cons t (RS.concat res1 res2)
+ else RS.concat res1 res2
+ else RS.empty
+
+
+ let top_down ?(noright=false) a tree t slist ctx slot_size =
+ let pempty = empty_size slot_size in
+ (* evaluation starts from the right so we put sl1,res1 at the end *)
+ let eval_fold2_slist fll t (sl2,res2) (sl1,res1) =
+ let res = Array.copy res1 in
+ let rec fold l1 l2 fll i aq =
+ match SList.node l1,SList.node l2, fll with
+ | SList.Cons(s1,ll1),
+ SList.Cons(s2,ll2),
+ fl::fll ->
+ let r',flags = eval_formlist s1 s2 fl in
+ let _ = res.(i) <- RS.merge flags t res1.(i) res2.(i)
+ in
+ fold ll1 ll2 fll (i+1) (SList.cons r' aq)
+
+ | SList.Nil, SList.Nil,[] -> aq,res
+ | _ -> assert false
+ in
+ fold sl1 sl2 fll 0 SList.nil
+ in
+ let null_result() = (pempty,Array.make slot_size RS.empty) in
+
+ let rec loop t slist ctx =
+ if t == Tree.nil then null_result() else get_trans t slist (Tree.tag tree t) ctx
+
+ and loop_tag tag t slist ctx =
+ if t == Tree.nil then null_result() else get_trans t slist tag ctx
+ and loop_no_right t slist ctx =
+ if t == Tree.nil then null_result() else get_trans ~noright:true t slist (Tree.tag tree t) ctx
+ and get_trans ?(noright=false) t slist tag ctx =
+ let cont =
+ try
+ CachedTransTable.find td_trans (tag,slist)
+ with
+ | Not_found ->
+ let fl_list,llist,rlist,ca,da,sa,fa =
+ SList.fold
+ (fun set (fll_acc,lllacc,rllacc,ca,da,sa,fa) -> (* For each set *)
+ let fl,ll,rr,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,_ = Transition.node t 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;[]
+ )
+
+ ) set (Formlist.nil,StateSet.empty,StateSet.empty,ca,da,sa,fa)
+ in fl::fll_acc, (SList.cons ll lllacc), (SList.cons rr rllacc),ca,da,sa,fa)
+ slist ([],SList.nil,SList.nil,StateSet.empty,StateSet.empty,StateSet.empty,StateSet.empty)
+ in
+ (* Logic to chose the first and next function *)
+ let _,tags_below,_,tags_after = Tree.tags tree tag in
+ let f_kind,first = choose_jump_down tree tags_below ca da a
+ and n_kind,next = if noright then (`NIL, fun _ _ -> Tree.nil )
+ else choose_jump_next tree tags_after sa fa a in
+ let empty_res = null_result() in
+ let cont =
+ match f_kind,n_kind with
+ | `NIL,`NIL ->
+ (fun _ _ -> eval_fold2_slist fl_list t empty_res empty_res )
+ | _,`NIL -> (
+ match f_kind with
+ |`TAG(tag) ->
+ (fun t _ -> eval_fold2_slist fl_list t empty_res
+ (loop_tag tag (first t) llist t))
+ | `ANY ->
+ (fun t _ -> eval_fold2_slist fl_list t empty_res
+ (loop (first t) llist t))
+ | _ -> assert false)
+
+ | `NIL,_ -> (
+ match n_kind with
+ |`TAG(tag) ->
+ (fun t ctx -> eval_fold2_slist fl_list t
+ (loop_tag tag (next t ctx) rlist ctx) empty_res)
+
+ | `ANY ->
+ (fun t ctx -> eval_fold2_slist fl_list t
+ (loop (next t ctx) rlist ctx) empty_res)
+
+ | _ -> assert false)
+
+ | `TAG(tag1),`TAG(tag2) ->
+ (fun t ctx -> eval_fold2_slist fl_list t
+ (loop (next t ctx) rlist ctx)
+ (loop (first t) llist t))
+
+ | `TAG(tag),`ANY ->
+ (fun t ctx ->
+ eval_fold2_slist fl_list t
+ (loop (next t ctx) rlist ctx)
+ (loop_tag tag (first t) llist t))
+ | `ANY,`TAG(tag) ->
+ (fun t ctx ->
+ eval_fold2_slist fl_list t
+ (loop_tag tag (next t ctx) rlist ctx)
+ (loop (first t) llist t) )
+ | `ANY,`ANY ->
+ (fun t ctx ->
+ eval_fold2_slist fl_list t
+ (loop (next t ctx) rlist ctx)
+ (loop (first t) llist t) )
+ | _ -> assert false