module TS =
struct
- type t = Nil | Cons of Tree.t * t | Concat of t*t
+ type t = Nil
+ | Sing of Tree.t
+ | Cons of Tree.t*t
+ | ConsCat of Tree.t * t * t
+ | Concat of t*t
let empty = Nil
let cons e t = Cons(e,t)
- let concat t1 t2 = Concat (t1,t2)
- let append e t = Concat(t,Cons(e,Nil))
+ let concat t1 t2 = Concat(t1,t2)
+ let append e t = Concat(t,Sing(e))
+
+
+
let fold f l acc =
let rec loop acc = function
| Nil -> acc
- | Cons(e,t) -> loop (f e acc) t
- | Concat(t1,t2) -> loop (loop acc t1) t2
+ | Sing e -> f e acc
+ | Cons (e,t) -> loop (f e acc) t
+ | ConsCat (e,t1,t2) -> loop (loop (f e acc) t1) t2
+ | Concat (t1,t2) -> loop (loop acc t1) t2
in
loop acc l
let iter f l =
let rec loop = function
| Nil -> ()
- | Cons(e,t) -> let _ = f e in loop t
- | Concat(t1,t2) -> let _ = loop t1 in loop t2
+ | Sing e -> f e
+ | Cons (e,t) -> f e; loop t
+ | ConsCat(e,t1,t2) ->
+ f e; loop t1; loop t2
+ | Concat(t1,t2) -> loop t1;loop t2
in loop l
end
}
external hash_const_variant : [> ] -> int = "%identity"
-external int_bool : bool -> int = "%identity"
+external vb : bool -> int = "%identity"
let hash_node_form t = match t with
| False -> 0
| True -> 1
| And(f1,f2) -> (2+17*f1.fkey + 37*f2.fkey) (*land max_int *)
| Or(f1,f2) -> (3+101*f1.fkey + 253*f2.fkey) (*land max_int *)
- | Atom(v,b,s) -> ((hash_const_variant v) + (3846*(int_bool b) +257) + (s lsl 13 - s)) (*land max_int *)
+ | Atom(v,b,s) -> ((hash_const_variant v) + (3846*(vb b) +257) + (s lsl 13 - s)) (*land max_int *)
module FormNode =
let hash = k_hash
end
-module HTagSet =
-struct
- type key = Ptset.t*Tag.t
- let equal (s1,s2) (t1,t2) = (s2 == t2) && Ptset.equal s1 t1
- let hash (s,t) = ((Ptset.hash s)) lsl 31 lxor (Tag.hash t)
-
-type 'a t =
- { mutable size: int; (* number of elements *)
- mutable data: (key,'a) bucketlist array } (* the buckets *)
-
-and ('a, 'b) bucketlist =
- Empty
- | Cons of 'a * 'b * ('a, 'b) bucketlist
-
-let create initial_size =
- let s = min (max 1 initial_size) Sys.max_array_length in
- { size = 0; data = Array.make s Empty }
-
-let clear h =
- for i = 0 to Array.length h.data - 1 do
- h.data.(i) <- Empty
- done;
- h.size <- 0
-
-let copy h =
- { size = h.size;
- data = Array.copy h.data }
-
-let length h = h.size
-
-let resize tbl =
- let odata = tbl.data in
- let osize = Array.length odata in
- let nsize = min (2 * osize + 1) Sys.max_array_length in
- if nsize <> osize then begin
- let ndata = Array.create nsize Empty in
- let rec insert_bucket = function
- Empty -> ()
- | Cons(key, data, rest) ->
- insert_bucket rest; (* preserve original order of elements *)
- let nidx = (hash key) mod nsize in
- ndata.(nidx) <- Cons(key, data, ndata.(nidx)) in
- for i = 0 to osize - 1 do
- insert_bucket odata.(i)
- done;
- tbl.data <- ndata;
- end
-
-let add h key info =
- let i = (hash key) mod (Array.length h.data) in
- let bucket = Cons(key, info, h.data.(i)) in
- h.data.(i) <- bucket;
- h.size <- succ h.size;
- if h.size > Array.length h.data lsl 1 then resize h
-
-let remove h key =
- let rec remove_bucket = function
- Empty ->
- Empty
- | Cons(k, i, next) ->
- if equal k key
- then begin h.size <- pred h.size; next end
- else Cons(k, i, remove_bucket next) in
- let i = (hash key) mod (Array.length h.data) in
- h.data.(i) <- remove_bucket h.data.(i)
-
-let rec find_rec key = function
- Empty ->
- raise Not_found
- | Cons(k, d, rest) ->
- if equal key k then d else find_rec key rest
-
-let find h key =
- match h.data.((hash key) mod (Array.length h.data)) with
- Empty -> raise Not_found
- | Cons(k1, d1, rest1) ->
- if equal key k1 then d1 else
- match rest1 with
- Empty -> raise Not_found
- | Cons(k2, d2, rest2) ->
- if equal key k2 then d2 else
- match rest2 with
- Empty -> raise Not_found
- | Cons(k3, d3, rest3) ->
- if equal key k3 then d3 else find_rec key rest3
-
-let find_all h key =
- let rec find_in_bucket = function
- Empty ->
- []
- | Cons(k, d, rest) ->
- if equal k key
- then d :: find_in_bucket rest
- else find_in_bucket rest in
- find_in_bucket h.data.((hash key) mod (Array.length h.data))
-
-let replace h key info =
- let rec replace_bucket = function
- Empty ->
- raise Not_found
- | Cons(k, i, next) ->
- if equal k key
- then Cons(k, info, next)
- else Cons(k, i, replace_bucket next) in
- let i = (hash key) mod (Array.length h.data) in
- let l = h.data.(i) in
- try
- h.data.(i) <- replace_bucket l
- with Not_found ->
- h.data.(i) <- Cons(key, info, l);
- h.size <- succ h.size;
- if h.size > Array.length h.data lsl 1 then resize h
-
-let mem h key =
- let rec mem_in_bucket = function
- | Empty ->
- false
- | Cons(k, d, rest) ->
- equal k key || mem_in_bucket rest in
- mem_in_bucket h.data.((hash key) mod (Array.length h.data))
-
-let iter f h =
- let rec do_bucket = function
- Empty ->
- ()
- | Cons(k, d, rest) ->
- f k d; do_bucket rest in
- let d = h.data in
- for i = 0 to Array.length d - 1 do
- do_bucket d.(i)
- done
-
-let fold f h init =
- let rec do_bucket b accu =
- match b with
- Empty ->
- accu
- | Cons(k, d, rest) ->
- do_bucket rest (f k d accu) in
- let d = h.data in
- let accu = ref init in
- for i = 0 to Array.length d - 1 do
- accu := do_bucket d.(i) !accu
- done;
- !accu
-
-
-end
-
-
-
-
-
-
-
-
-
-
-
-
+module HTagSet = Hashtbl.Make(HTagSetKey)
type dispatch = { first : Tree.t -> Tree.t;
flabel : string;
next : Tree.t -> Tree.t -> Tree.t;
nlabel : string;
+ consres : Tree.t -> TS.t -> TS.t -> bool -> bool -> TS.t
}
+
+type formlist = Nil | Cons of state*formula*int*formlist
+
+let f_hash (h,s,t) = h * 41+((Ptset.hash s) lsl 10 ) lxor (Ptset.hash t)*4097
+module HFormlistKey =
+struct
+ type t = int*Ptset.t*Ptset.t
+ let equal (h1,s1,t1) (h2,s2,t2) = h1==h2 && s1 == s2 && t1 == t2
+ let hash = f_hash
+end
+module HFormlist = Hashtbl.Make (HFormlistKey)
+
type t = {
id : int;
mutable states : Ptset.t;
init : Ptset.t;
mutable final : Ptset.t;
universal : Ptset.t;
+ starstate : Ptset.t option;
(* Transitions of the Alternating automaton *)
phi : (state,(TagSet.t*(bool*formula*bool)) list) Hashtbl.t;
- sigma : (dispatch*bool*formula) HTagSet.t;
+ sigma : (dispatch*bool*formlist*Ptset.t*Ptset.t) HTagSet.t;
}
module Pair (X : Set.OrderedType) (Y : Set.OrderedType) =
pl2 acc )
pl1 PL.empty
-
and dnf f =
try
Hashtbl.find dnf_hash f.fid
Format.fprintf ppf "\n")l;
Format.fprintf ppf "NFA transitions :\n------------------------------\n";
- HTagSet.iter (fun (qs,t) (disp,b,f) ->
- pr_st ppf (Ptset.elements qs);
- Format.fprintf ppf ",%s %s " (Tag.to_string t) (if b then "=>" else "->");
- pr_frm ppf f;
- Format.fprintf ppf "(fid=%i) left=" f.fid;
- let (l,ll,_),(r,rr,_) = f.st in
- pr_st ppf (Ptset.elements l);
- Format.fprintf ppf ", ";
- pr_st ppf (Ptset.elements ll);
+(* HTagSet.iter (fun (qs,t) (disp,b,_,flist,_,_) ->
+ let (ls,lls,_),(rs,rrs,_) =
+ List.fold_left (fun ((a1,b1,c1),(a2,b2,c2)) (_,f) ->
+ let (x1,y1,z1),(x2,y2,z2) = f.st in
+ ((Ptset.union x1 a1),(Ptset.union y1 b1),(Ptset.union c1 z1)),
+ ((Ptset.union x2 a2),(Ptset.union y2 b2),(Ptset.union c2 z2)))
+ ((Ptset.empty,Ptset.empty,Ptset.empty),
+ (Ptset.empty,Ptset.empty,Ptset.empty))
+ flist
+ in
+ pr_st ppf (Ptset.elements qs);
+ Format.fprintf ppf ",%s %s " (Tag.to_string t) (if b then "=>" else "->");
+ List.iter (fun (q,f) ->
+ Format.fprintf ppf "\n%i," q;
+ pr_frm ppf f) flist;
+ Format.fprintf ppf "\nleft=";
+ pr_st ppf (Ptset.elements ls);
+ Format.fprintf ppf " , ";
+ pr_st ppf (Ptset.elements lls);
Format.fprintf ppf ", right=";
- pr_st ppf (Ptset.elements r);
+ pr_st ppf (Ptset.elements rs);
Format.fprintf ppf ", ";
- pr_st ppf (Ptset.elements rr);
- Format.fprintf ppf ", first=%s, next=%s\n" disp.flabel disp.nlabel;
- ) a.sigma;
+ pr_st ppf (Ptset.elements rrs);
+ Format.fprintf ppf ", first=%s, next=%s\n\n" disp.flabel disp.nlabel;
+ ) a.sigma; *)
Format.fprintf ppf "=======================================\n%!"
module Transitions = struct
(* test some inlining *)
| True -> true,true,true
| False -> false,false,false
- | Atom((`Left|`LLeft),b,q) -> if b == (Ptset.mem q s1) then (true,true,false) else false,false,false
- | Atom(_,b,q) -> if b == (Ptset.mem q s2) then (true,false,true) else false,false,false
| _ ->
try
- HFEval.find hfeval (f.fid,s1,s2)
+ HFEval.find hfeval (f.fid,s1,s2)
with
- | Not_found -> let r =
+ | Not_found -> let r =
match f.pos with
+ | Atom((`Left|`LLeft),b,q) ->
+ if b == (Ptset.mem q s1)
+ then (true,true,false)
+ else false,false,false
+ | Atom(_,b,q) ->
+ if b == (Ptset.mem q s2)
+ then (true,false,true)
+ else false,false,false
| Or(f1,f2) ->
let b1,rl1,rr1 = eval f1
in
in eval f
- let fstate_pool = Hashtbl.create 11
+ let h_formlist = HFormlist.create 511
+
+ let form_list_fold_left f acc fl =
+ let rec loop acc fl =
+ match fl with
+ | Nil -> acc
+ | Cons(s,frm,h,fll) -> loop (f acc s frm h) fll
+ in
+ loop acc fl
+
- let merge_pred a b = match a,b with
- | Some(f1), Some(f2) -> Some(fun x -> f1 x || f2 x)
- | None,None -> None
- | None,Some(_) -> b
- | Some(_),None -> a
+ let rec eval_formlist s1 s2 = function
+ | Nil -> Ptset.empty,false,false,false
+ | Cons(q,f,h,fl) ->
+ let k = (h,s1,s2)
+ in
+ try HFormlist.find h_formlist k
+ with
+ Not_found ->
+ let s,b',b1',b2' = eval_formlist s1 s2 fl in
+ let b,b1,b2 = eval_form_bool f s1 s2 in
+ let r = if b then (Ptset.add q s, b'||b, b1'||b1,b2'||b2)
+ else s,b',b1',b2'
+ in
+ HFormlist.add h_formlist k r;r
- let acc_pred p l1 l2 = match p with
- | `Left _ -> p::l1,l2
- | `Right _ -> l1,p::l2
- | _ -> l1,l2
else `Negative(TagSet.negative ts)
+
+ let cons_res e s1 s2 b1 b2 =
+ if b1&&b2 then
+ if s2 == TS.Nil && s1 == TS.Nil
+ then TS.Sing e
+ else if s1 == TS.Nil
+ then TS.Cons (e,s2)
+ else if s2 == TS.Nil
+ then TS.Cons (e,s1)
+ else TS.ConsCat(e,s1,s2)
+ else if not(b1 || b2)
+ then TS.Sing e
+ else if b1 then if s1 == TS.Nil then TS.Sing e else TS.Cons(e,s1)
+ else if s2 = TS.Nil then TS.Sing e else TS.Cons(e,s2)
+
+ let cat_res _ s1 s2 b1 b2 =
+ if b1&&b2 then if s1 == TS.Nil && s2 == TS.Nil then TS.Nil
+ else
+ if s1 == TS.Nil
+ then s2
+ else
+ if s2 == TS.Nil then s1 else TS.Concat(s1,s2)
+ else if not(b1 || b2)
+ then TS.Nil
+ else if b1 then s1
+ else s2
+
let merge_trans t a tag q acc =
- List.fold_left (fun (accf,accm,acchtrue) (ts,(m,f,pred)) ->
+ List.fold_left (fun (accf,accm,acchtrue,acchash) (ts,(m,f,pred)) ->
if TagSet.mem tag ts
then
- let tmpf,hastrue =
- if is_true f then
- let newfinal =
- try Hashtbl.find fstate_pool f.fid with
- | Not_found -> let s = mk_state() in
- a.states <- Ptset.add s a.states;
- a.final <- Ptset.add s a.final;
- Hashtbl.add fstate_pool f.fid s;s
- in
- (atom_ `Left true newfinal),true
- else f,false in
- (or_ tmpf accf,accm||m,acchtrue||hastrue)
- else (accf,accm,acchtrue)
+ let acchash = acchash+31*f.fid+42*q in
+ (Cons(q,f,acchash,accf),accm||m,acchtrue||(is_true f),acchash)
+ else (accf,accm,acchtrue,acchash)
) acc (try Hashtbl.find a.phi q with Not_found -> [])
let inter_text a b =
let next_sibling_ctx x _ = Tree.next_sibling x
let r_ignore _ x = x
-
-
let get_trans t a tag r =
try
- let dispatch,mark,f =
HTagSet.find a.sigma (r,tag)
- in f.st,dispatch,f,mark,r
with
Not_found ->
- let f,mark,_,accq =
- Ptset.fold (fun q (accf,accm,acchtrue,accq) ->
- let naccf,naccm,nacctrue =
- merge_trans t a tag q (accf,accm,acchtrue )
+ let fl,mark,_,_,accq =
+ Ptset.fold (fun q (accf,accm,acchtrue,acchash,accq) ->
+ let naccf,naccm,nacctrue,acchash =
+ merge_trans t a tag q (accf,accm,acchtrue,acchash )
in
- if is_false naccf then (naccf,naccm,nacctrue,accq)
- else (naccf,naccm,nacctrue,Ptset.add q accq)
+ (* if is_false naccf then (naccf,naccm,nacctrue,accq)
+ else *) (naccf,naccm,nacctrue,acchash,Ptset.add q accq)
)
- r (false_,false,false,Ptset.empty)
+ r (Nil,false,false,17,Ptset.empty)
in
- let (ls,lls,_),(rs,rrs,_) = f.st in
+ let (ls,lls,llls),(rs,rrs,rrrs) =
+ form_list_fold_left (fun ((a1,b1,c1),(a2,b2,c2)) _ f _ ->
+ let (x1,y1,z1),(x2,y2,z2) = f.st in
+ ((Ptset.union x1 a1),(Ptset.union y1 b1),(Ptset.union c1 z1)),
+ ((Ptset.union x2 a2),(Ptset.union y2 b2),(Ptset.union c2 z2)))
+ ((Ptset.empty,Ptset.empty,Ptset.empty),
+ (Ptset.empty,Ptset.empty,Ptset.empty))
+ fl
+ in
let tb,ta =
Tree.tags t tag
in
and tll,htllt,llfin = inter_text tb (tags a lls)
and tr,htrt,rfin = inter_text ta (tags a rs)
and trr,htrrt,rrfin = inter_text ta (tags a rrs)
- in
+ in(*
+ let _ =
+ Format.fprintf Format.err_formatter "Tag %s, right_states " (Tag.to_string tag);
+ pr_st Format.err_formatter (Ptset.elements rs);
+ Format.fprintf Format.err_formatter " tags = ";
+ Ptset.iter (fun t -> Format.fprintf Format.err_formatter "%s "
+ (Tag.to_string t)) tr;
+ Format.fprintf Format.err_formatter ", next_states ";
+ pr_st Format.err_formatter (Ptset.elements rrs);
+ Format.fprintf Format.err_formatter " tags = ";
+ Ptset.iter (fun t -> Format.fprintf Format.err_formatter "%s "
+ (Tag.to_string t)) trr;
+ Format.fprintf Format.err_formatter "\n%!";
+
+ in*)
let first,flabel =
if (llfin && lfin) then (* no stars *)
(if htlt || htllt then (Tree.text_below, "#text_below")
else if htrt || htrrt then (Tree.next_sibling_ctx,"#next_sibling_ctx")
else (Tree.node_sibling_ctx,"#node_sibling_ctx")
in
- let dispatch = { first = first; flabel = flabel; next = next; nlabel = nlabel}
+ let dispatch = { first = first; flabel = flabel; next = next; nlabel = nlabel;
+ consres = if mark then cons_res else cat_res }
in
- HTagSet.add a.sigma (accq,tag) (dispatch,mark,f);
- f.st,dispatch,f,mark,accq
+ HTagSet.add a.sigma (accq,tag) (dispatch,mark,fl,llls,rrrs);
+ dispatch,mark,fl,llls,rrrs
- let rec accepting_among a t orig ctx =
- let rest = Ptset.inter orig a.universal in
- let r = Ptset.diff orig rest in
- if Ptset.is_empty r then rest,0,TS.empty else
- if Tree.is_nil t
- then orig,0,TS.empty
- else
- let ((_,_,llls),(_,_,rrrs)),dispatch,formula,mark,r' =
- get_trans t a (Tree.tag t) r
- in
- let s1,n1,res1 = accepting_among a (dispatch.first t) llls t in
- let s2,n2,res2 = accepting_among a (dispatch.next t ctx) rrrs ctx in
- let rb,rb1,rb2 = eval_form_bool formula s1 s2 in
- if rb
- then
- let n1,res1 = if rb1 then n1,res1 else 0,TS.empty
- and n2,res2 = if rb2 then n2,res2 else 0,TS.empty
- in
- if mark
- then r',1+n1+n2,TS.Cons(t,(TS.Concat(res1,res2)))
- else r',n1+n2,TS.Concat(res1,res2)
- else Ptset.empty,0,TS.empty
-
- let rec accepting_among_count a t orig ctx =
- let rest = Ptset.inter orig a.universal in
- let r = Ptset.diff orig rest in
- if Ptset.is_empty r then rest,0 else
- if Tree.is_node t
- then
- let ((_,_,llls),(_,_,rrrs)),dispatch,formula,mark,r' =
- get_trans t a (Tree.tag t) r
- in
- let s1,res1 = accepting_among_count a (dispatch.first t) llls t
- and s2,res2 = accepting_among_count a (dispatch.next t ctx) rrrs ctx
- in
- let rb,rb1,rb2 = eval_form_bool formula s1 s2 in
- if rb
- then
- let res1 = if rb1 then res1 else 0
- and res2 = if rb2 then res2 else 0
- in r', if mark then 1+res1+res2 else res1+res2
- else Ptset.empty,0
- else orig,0
-
-
+
+ let rec accepting_among a t r ctx =
+ if Tree.is_nil t || Ptset.is_empty r then Ptset.empty,0,TS.Nil else
+ let dispatch,mark,flist,llls,rrrs =
+ get_trans t a (Tree.tag t) r
+ in
+ let s1,n1,res1 = accepting_among a (dispatch.first t) llls t in
+ let s2,n2,res2 = accepting_among a (dispatch.next t ctx) rrrs ctx in
+ let r',rb,rb1,rb2 = eval_formlist s1 s2 flist in
+ r',(vb rb)*((vb mark) + (vb rb1)* n1 + (vb rb2)*n2),if rb then
+ dispatch.consres t res1 res2 rb1 rb2
+ else TS.Nil
+
let run a t =
let st,n,res = accepting_among a t a.init t in
if Ptset.is_empty (st) then TS.empty,0 else res,n
-
+
+ let rec accepting_among_count_no_star a t r ctx =
+ if Tree.is_nil t||Ptset.is_empty r then Ptset.empty,0 else
+ let dispatch,mark,flist,llls,rrrs =
+ get_trans t a (Tree.tag t) r
+ in
+ let s1,res1 = accepting_among_count_no_star a (dispatch.first t) llls t
+ and s2,res2 = accepting_among_count_no_star a (dispatch.next t ctx) rrrs ctx
+ in
+ let r',rb,rb1,rb2 = eval_formlist s1 s2 flist
+ in
+ r',(vb rb)*((vb mark) + (vb rb1)*res1 + (vb rb2)*res2)
+
+
+
+ let rec accepting_among_count_star a t n =
+ if Tree.is_nil t then n else
+ if (Tree.tag t == Tag.attribute)
+ then accepting_among_count_star a (Tree.node_sibling t) n
+ else accepting_among_count_star a (Tree.node_sibling t)
+ (accepting_among_count_star a (Tree.node_child t) (1+n))
+
+ let rec accepting_among_count_may_star starstate a t r ctx =
+ if r == starstate then starstate,(accepting_among_count_star a t 0)
+ else
+ if Tree.is_nil t||Ptset.is_empty r then Ptset.empty,0 else
+ let dispatch,mark,flist,llls,rrrs =
+ get_trans t a (Tree.tag t) r
+ in
+ let s1,res1 = accepting_among_count_may_star starstate a (dispatch.first t) llls t
+ and s2,res2 = accepting_among_count_may_star starstate a (dispatch.next t ctx) rrrs ctx
+ in
+ let r',rb,rb1,rb2 = eval_formlist s1 s2 flist
+ in
+ r',(vb rb)*((vb mark) + (vb rb1)*res1 + (vb rb2)*res2)
+
let run_count a t =
- let st,res = accepting_among_count a t a.init t in
+
+ let st,res = match a.starstate with
+ | None -> accepting_among_count_no_star a t a.init t
+ | Some s -> accepting_among_count_may_star s a t a.init t
+ in
if Ptset.is_empty (st) then 0 else res