+ if TagSet.is_finite ts
+ then `Positive(TagSet.positive ts)
+ else `Negative(TagSet.negative ts)
+
+ let inter_text a b =
+ match b with
+ | `Positive s -> let r = Ptset.Int.inter a s in (r,Ptset.Int.mem Tag.pcdata r, true)
+ | `Negative s -> let r = Ptset.Int.diff a s in (r, Ptset.Int.mem Tag.pcdata r, false)
+
+
+ module type ResultSet =
+ sig
+ type t
+ type elt = [` Tree ] Tree.node
+ val empty : t
+ val cons : elt -> t -> t
+ val concat : t -> t -> t
+ val iter : ( elt -> unit) -> t -> unit
+ val fold : ( elt -> 'a -> 'a) -> t -> 'a -> 'a
+ val map : ( elt -> elt) -> t -> t
+ val length : t -> int
+ val merge : merge_conf -> elt -> t -> t -> t
+ val mk_quick_tag_loop : (elt -> elt -> 'a*t array) -> 'a -> int -> Tree.t -> Tag.t -> (elt -> elt -> 'a*t array)
+ val mk_quick_star_loop : (elt -> elt -> 'a*t array) -> 'a -> int -> Tree.t -> (elt -> elt -> 'a*t array)
+ end
+
+ module Integer : ResultSet =
+ struct
+ type t = int
+ type elt = [`Tree] Tree.node
+
+ let empty = 0
+ let cons _ x = x+1
+ let concat x y = x + y
+ let iter _ _ = failwith "iter not implemented"
+ let fold _ _ _ = failwith "fold not implemented"
+ let map _ _ = failwith "map not implemented"
+ let length x = x
+ let merge2 conf t res1 res2 =
+ let rb,rb1,rb2,mark = conf in
+ if rb then
+ let res1 = if rb1 then res1 else 0
+ and res2 = if rb2 then res2 else 0
+ in
+ if mark then 1+res1+res2
+ else res1+res2
+ else 0
+ let merge conf t res1 res2 =
+ match conf with
+ NO -> 0
+ | MARK -> 1
+ | MARK1 -> res1+1
+ | ONLY1 -> res1
+ | ONLY2 -> res2
+ | ONLY12 -> res1+res2
+ | MARK2 -> res2+1
+ | MARK12 -> res1+res2+1
+
+ let mk_quick_tag_loop _ sl ss tree tag = ();
+ fun t ctx ->
+ (sl, Array.make ss (Tree.subtree_tags tree tag t))
+ let mk_quick_star_loop _ sl ss tree = ();
+ fun t ctx ->
+ (sl, Array.make ss (Tree.subtree_elements tree t))
+
+ end
+
+ module IdSet : ResultSet=
+ struct
+ type elt = [`Tree] Tree.node
+ type node = Nil
+ | Cons of elt * node
+ | Concat of node*node
+
+ and t = { node : node;
+ length : int }
+
+ let empty = { node = Nil; length = 0 }
+
+ let cons e t = { node = Cons(e,t.node); length = t.length+1 }
+ let concat t1 t2 = { node = Concat(t1.node,t2.node); length = t1.length+t2.length }
+ let append e t = { node = Concat(t.node,Cons(e,Nil)); length = t.length+1 }
+
+ let fold f l acc =
+ let rec loop acc t = match t with
+ | Nil -> acc
+ | Cons (e,t) -> loop (f e acc) t
+ | Concat (t1,t2) -> loop (loop acc t1) t2
+ in
+ loop acc l.node
+
+ let length l = l.length
+
+
+ let iter f l =
+ let rec loop = function
+ | Nil -> ()
+ | Cons (e,t) -> f e; loop t
+ | Concat(t1,t2) -> loop t1;loop t2
+ in loop l.node
+
+ let map f l =
+ let rec loop = function
+ | Nil -> Nil
+ | Cons(e,t) -> Cons(f e, loop t)
+ | Concat(t1,t2) -> Concat(loop t1,loop t2)
+ in
+ { l with node = loop l.node }
+
+ let merge conf t res1 res2 =
+ match conf with
+ NO -> empty
+ | MARK -> cons t empty
+ | ONLY1 -> res1
+ | ONLY2 -> res2
+ | ONLY12 -> { node = (Concat(res1.node,res2.node));
+ length = res1.length + res2.length ;}
+ | MARK12 -> { node = Cons(t,(Concat(res1.node,res2.node)));
+ length = res1.length + res2.length + 1;}
+ | MARK1 -> { node = Cons(t,res1.node);
+ length = res1.length + 1;}
+ | MARK2 -> { node = Cons(t,res2.node);
+ length = res2.length + 1;}
+
+ let mk_quick_tag_loop f _ _ _ _ = f
+ let mk_quick_star_loop f _ _ _ = f
+ end
+ module GResult(Doc : sig val doc : Tree.t end) = struct
+ type bits
+ type elt = [` Tree] Tree.node
+ external create_empty : int -> bits = "caml_result_set_create"
+ external set : bits -> int -> unit = "caml_result_set_set"
+ external next : bits -> int -> int = "caml_result_set_next"
+ external count : bits -> int = "caml_result_set_count"
+ external clear : bits -> elt -> elt -> unit = "caml_result_set_clear"
+
+ external set_tag_bits : bits -> Tag.t -> Tree.t -> elt -> elt = "caml_set_tag_bits"
+ type t =
+ { segments : elt list;
+ bits : bits;
+ }
+
+ let ebits =
+ let size = (Tree.subtree_size Doc.doc Tree.root) in
+ create_empty (size*2+1)
+
+ let empty = { segments = [];
+ bits = ebits }
+
+ let cons e t =
+ let rec loop l = match l with
+ | [] -> { bits = (set t.bits (Obj.magic e);t.bits);
+ segments = [ e ] }
+ | p::r ->
+ if Tree.is_binary_ancestor Doc.doc e p then
+ loop r
+ else
+ { bits = (set t.bits (Obj.magic e);t.bits);
+ segments = e::l }
+ in
+ loop t.segments
+
+ let concat t1 t2 =
+ if t2.segments == [] then t1
+ else
+ if t1.segments == [] then t2
+ else
+ let h2 = List.hd t2.segments in
+ let rec loop l = match l with
+ | [] -> t2.segments
+ | p::r ->
+ if Tree.is_binary_ancestor Doc.doc p h2 then
+ l
+ else
+ p::(loop r)
+ in
+ { bits = t1.bits;
+ segments = loop t1.segments
+ }
+
+ let iter f t =
+ let rec loop i =
+ if i == -1 then ()
+ else (f ((Obj.magic i):elt);loop (next t.bits i))
+ in loop (next t.bits 0)
+
+ let fold f t acc =
+ let rec loop i acc =
+ if i == -1 then acc
+ else loop (next t.bits i) (f ((Obj.magic i):elt) acc)
+ in loop (next t.bits 0) acc
+
+ let map _ _ = failwith "noop"
+ (*let length t = let cpt = ref 0 in
+ iter (fun _ -> incr cpt) t; !cpt *)
+ let length t = count t.bits
+
+ let clear_bits t =
+ let rec loop l = match l with
+ [] -> ()
+ | idx::ll ->
+ clear t.bits idx (Tree.closing Doc.doc idx); loop ll
+ in
+ loop t.segments;empty
+
+ let merge (rb,rb1,rb2,mark) elt t1 t2 =
+ if rb then
+(* let _ = Printf.eprintf "Lenght before merging is %i %i\n"
+ (List.length t1.segments) (List.length t2.segments)
+ in *)
+ match t1.segments,t2.segments with
+ [],[] -> if mark then cons elt empty else empty
+ | [_],[] when rb1 -> if mark then cons elt t1 else t1
+ | [], [_] when rb2 -> if mark then cons elt t2 else t2
+ | [_],[_] when rb1 && rb2 -> if mark then cons elt empty else
+ concat t1 t2
+ | _ ->
+ let t1 = if rb1 then t1 else clear_bits t1
+ and t2 = if rb2 then t2 else clear_bits t2
+ in
+ (if mark then cons elt (concat t1 t2)
+ else concat t1 t2)
+ else
+ let _ = clear_bits t1 in
+ clear_bits t2
+
+ let merge conf t t1 t2 =
+ match t1.segments,t2.segments,conf with
+ | _,_,NO -> let _ = clear_bits t1 in clear_bits t2
+ | [],[],(MARK1|MARK2|MARK12|MARK) -> cons t empty
+ | [],[],_ -> empty
+ | [_],[],(ONLY1|ONLY12) -> t1
+ | [_],[],(MARK1|MARK12) -> cons t t1
+ | [],[_],(ONLY2|ONLY12) -> t2
+ | [],[_],(MARK2|MARK12) -> cons t t2
+ | [_],[_],ONLY12 -> concat t1 t2
+ | [_],[_],MARK12 -> cons t empty
+ | _,_,MARK -> let _ = clear_bits t2 in cons t (clear_bits t1)
+ | _,_,ONLY1 -> let _ = clear_bits t2 in t1
+ | _,_,ONLY2 -> let _ = clear_bits t1 in t2
+ | _,_,ONLY12 -> concat t1 t2
+ | _,_,MARK1 -> let _ = clear_bits t2 in cons t t1
+ | _,_,MARK2 -> let _ = clear_bits t1 in cons t t2
+ | _,_,MARK12 -> cons t (concat t1 t2)
+
+ let mk_quick_tag_loop _ sl ss tree tag = ();
+ fun t _ ->
+ let res = empty in
+ let first = set_tag_bits empty.bits tag tree t in
+ let res =
+ if first == Tree.nil then res else
+ cons first res
+ in
+ (sl, Array.make ss res)