Some more optimisations
[SXSI/xpathcomp.git] / tree.ml
1 (******************************************************************************)
2 (*  SXSI : XPath evaluator                                                    *)
3 (*  Kim Nguyen (Kim.Nguyen@nicta.com.au)                                      *)
4 (*  Copyright NICTA 2008                                                      *)
5 (*  Distributed under the terms of the LGPL (see LICENCE)                     *)
6 (******************************************************************************)
7 INCLUDE "utils.ml"
8
9 type tree
10 type 'a node = int
11 type node_kind = [`Text | `Tree ]
12     
13 let compare_node : 'a node -> 'a node -> int = (-)
14 let equal_node : 'a node -> 'a node -> bool = (==)
15   
16 (* abstract type, values are pointers to a XMLTree C++ object *)
17
18 external int_of_node : 'a node -> int = "%identity"
19   
20 external parse_xml_uri : string -> int -> bool -> bool -> tree = "caml_call_shredder_uri"         
21 external parse_xml_string :  string -> int -> bool -> bool -> tree = "caml_call_shredder_string"
22   
23 external save_tree : tree -> string -> unit = "caml_xml_tree_save"
24 external load_tree : string ->  int -> tree = "caml_xml_tree_load"
25   
26 external nullt : unit -> 'a node = "caml_xml_tree_nullt"
27
28 let nil : 'a node = Obj.magic (-1)
29
30 external text_get_tc_text : tree -> [`Text] node -> string = "caml_text_collection_get_text" 
31                 
32 external text_is_empty : tree -> [`Text ] node -> bool = "caml_text_collection_empty_text" 
33
34 let text_is_empty t n =
35   (equal_node nil n) || text_is_empty t n
36     
37
38
39 external text_is_contains : tree -> string -> bool = "caml_text_collection_is_contains" 
40 external text_count_contains : tree -> string -> int = "caml_text_collection_count_contains" 
41 external text_count : tree -> string -> int = "caml_text_collection_count" 
42 external text_contains : tree -> string -> [`Text ] node array = "caml_text_collection_contains" 
43 external text_unsorted_contains : tree -> string -> unit = "caml_text_collection_unsorted_contains"
44 external get_cached_text : tree -> [`Text] node -> string = "caml_text_collection_get_cached_text"
45
46
47 external tree_serialize : tree -> string -> unit = "caml_xml_tree_serialize"
48
49 external tree_unserialize : string -> tree = "caml_xml_tree_unserialize"
50       
51 external tree_root : tree -> [`Tree] node = "caml_xml_tree_root" 
52
53 let tree_is_nil x = equal_node x nil
54
55 external tree_parent : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_parent" 
56 external tree_parent_doc : tree -> [`Text ] node -> [`Tree ] node = "caml_xml_tree_parent_doc" 
57 external tree_prev_doc : tree -> [`Text ] node -> [`Tree ] node = "caml_xml_tree_prev_doc" 
58 external tree_first_child : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_first_child" 
59 external tree_next_sibling : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_next_sibling" 
60 external tree_prev_sibling : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_prev_sibling" 
61 external tree_is_leaf : tree -> [`Tree] node -> bool = "caml_xml_tree_is_leaf" 
62 external tree_last_child : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_last_child"
63 external tree_is_first_child : tree -> [`Tree] node -> bool = "caml_xml_tree_is_first_child"
64
65 (*    external tag : tree -> [`Tree ] node -> T = "caml_xml_tree_tag"*)
66 external tree_tag_id : tree -> [`Tree ] node -> Tag.t = "caml_xml_tree_tag_id" 
67     
68
69 let tree_is_last t n = equal_node nil (tree_next_sibling t n)
70     
71 external tree_prev_text : tree -> [`Tree] node -> [`Text ] node = "caml_xml_tree_prev_text" 
72
73 external tree_my_text : tree -> [`Tree] node -> [`Text ] node = "caml_xml_tree_my_text" 
74 external tree_next_text : tree -> [`Tree] node -> [`Text ] node = "caml_xml_tree_next_text" 
75 external tree_doc_ids : tree -> [`Tree ] node -> [`Text ] node * [`Text ] node = "caml_xml_tree_doc_ids" 
76
77 let text_size tree = int_of_node (snd ( tree_doc_ids tree (Obj.magic 0) ))
78
79 let get_cached_text t x =
80   if x == -1 then ""
81   else 
82      get_cached_text t x
83
84
85 external tree_text_xml_id : tree -> [`Text ] node -> int = "caml_xml_tree_text_xml_id" 
86 external tree_node_xml_id : tree -> [`Tree ] node -> int = "caml_xml_tree_node_xml_id" 
87 external tree_is_ancestor : tree -> [`Tree ] node -> [`Tree ] node -> bool = "caml_xml_tree_is_ancestor" 
88 external tree_tagged_desc : tree -> [`Tree ] node -> Tag.t -> [`Tree ] node = "caml_xml_tree_tagged_desc" 
89 external tree_tagged_foll_below : tree -> [`Tree ] node -> Tag.t -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_tagged_foll_below" 
90 external tree_subtree_tags : tree -> [`Tree ] node -> Tag.t -> int = "caml_xml_tree_subtree_tags" 
91 (*
92 external tree_select_below : tree -> [`Tree ] node -> Ptset.int_vector -> Ptset.int_vector -> [`Tree ] node = "caml_xml_tree_select_below" 
93 external tree_select_desc_only : tree -> [`Tree ] node -> Ptset.int_vector -> [`Tree ] node = "caml_xml_tree_select_desc_only" 
94 external tree_select_next : tree -> [`Tree ] node -> Ptset.int_vector -> Ptset.int_vector -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_select_next" 
95 external tree_select_foll_only : tree -> [`Tree ] node -> Ptset.int_vector -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_select_foll_only" 
96 external tree_select_desc_or_foll_only : tree -> [`Tree ] node -> Ptset.int_vector -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_select_foll_only" *)
97   
98 type descr = 
99   | Nil 
100   | Node of [`Tree] node
101   | Text of [`Text] node * [`Tree] node
102       
103 type t = { doc : tree;            
104            node : descr;
105            ttable : (Tag.t,(Ptset.Int.t*Ptset.Int.t)) Hashtbl.t;
106          }
107
108 let text_size t = text_size t.doc
109 module MemUnion = Hashtbl.Make (struct 
110       type t = Ptset.Int.t*Ptset.Int.t
111       let equal (x,y) (z,t) = (Ptset.Int.equal x z)&&(Ptset.Int.equal y t)
112       let equal a b = equal a b || equal b a
113       let hash (x,y) =   (* commutative hash *)
114         let x = Ptset.Int.hash x 
115         and y = Ptset.Int.hash y 
116         in
117           if x < y then HASHINT2(x,y) else HASHINT2(y,x)
118     end)
119
120 let collect_tags tree =
121   let h_union = MemUnion.create BIG_H_SIZE in
122   let pt_cup s1 s2 =
123       try
124         MemUnion.find h_union (s1,s2)
125       with
126         | Not_found -> let s = Ptset.Int.union s1 s2
127           in
128             MemUnion.add h_union (s1,s2) s;s
129   in    
130   let h_add = Hashtbl.create BIG_H_SIZE in
131   let pt_add t s = 
132     let k = HASHINT2(Tag.hash t,Ptset.Int.hash s) in
133       try
134         Hashtbl.find h_add k
135       with
136       | Not_found -> let r = Ptset.Int.add t s in
137           Hashtbl.add h_add k r;r
138   in
139   let h = Hashtbl.create BIG_H_SIZE in
140   let sing = Ptset.Int.singleton Tag.pcdata in    
141   let update t sb sa =
142     let sbelow,safter = 
143       try
144         Hashtbl.find h t 
145       with
146         | Not_found -> 
147             (sing,sing)
148     in
149       Hashtbl.replace h t (pt_cup sbelow sb, pt_cup safter sa)
150   in
151   let rec loop id acc = 
152     if equal_node id nil
153     then (Ptset.Int.empty,acc)
154     else
155       let below2,after2 = loop (tree_next_sibling tree id) acc in
156       let below1,after1 = loop (tree_first_child tree id) after2 in
157       let tag = tree_tag_id tree id in
158         update tag below1 after2;
159         pt_add tag (pt_cup below1 below2), (pt_add tag after1)
160   in
161   let b,a = loop (tree_root tree) Ptset.Int.empty in
162     update Tag.pcdata b a;
163     h
164
165
166
167
168
169 let contains_array = ref [| |]
170 let contains_index = Hashtbl.create 4096 
171 let in_array _ i =
172   try
173     Hashtbl.find contains_index i
174   with
175       Not_found -> false
176
177 let init_contains t s = 
178   let a = text_contains t.doc s 
179   in
180     Array.fast_sort (compare) a;
181     contains_array := a;
182     Array.iter (fun x -> Hashtbl.add contains_index x true) !contains_array
183       
184 let count_contains t s = text_count_contains t.doc s
185 let unsorted_contains t s = text_unsorted_contains t.doc s
186
187 let init_naive_contains t s =
188   let i,j = tree_doc_ids t.doc (tree_root t.doc)
189   in
190   let regexp = Str.regexp_string s in
191   let matching arg = 
192     try
193       let _ = Str.search_forward regexp arg 0;
194       in true
195     with _ -> false
196   in
197   let rec loop n acc l = 
198     if n >= j then acc,l
199     else
200       let s = get_cached_text t.doc n
201       in
202         if matching s 
203         then loop (n+1) (n::acc) (l+1) 
204         else loop (n+1) acc l
205   in
206   let acc,l = loop i [] 0 in
207   let a = Array.create l nil in
208   let _ = List.fold_left (fun cpt e -> a.(cpt) <- e; (cpt-1)) (l-1) acc
209   in
210     contains_array := a
211           
212
213
214 module DocIdSet = struct
215   include Set.Make (struct type t = [`Text] node
216                            let compare = compare_node end)
217     
218 end
219 let is_nil t = t.node == Nil
220
221 let is_node t = t.node != Nil
222
223 let node_of_t t  =
224   let _ = Tag.init (Obj.magic t) in
225   let table = collect_tags t 
226   in
227 (*
228   let _ = Hashtbl.iter (fun t (sb,sa) ->
229                           Printf.eprintf "'%s' -> { " (Tag.to_string t);
230                           Ptset.iter (fun i ->  Printf.eprintf "'%s' " (Tag.to_string i)) sb;
231                           Printf.eprintf "}\n { ";
232                           Ptset.iter (fun i ->  Printf.eprintf "'%s' " (Tag.to_string i)) sa;
233                           Printf.eprintf "} \n----------------------------------\n";
234                        ) table in
235   let i,j = tree_doc_ids t (tree_root t) in
236     Printf.eprintf "%i docs, range from %i to %i\n%!" (Array.length s) i j;
237     Array.iter (fun i -> print_endline (">>>" ^ i ^ "<<<")) s; *)
238     { doc= t; 
239       node = Node(tree_root t);
240       ttable = table;
241     }
242 let finalize _ = Printf.eprintf "Release the string list !\n%!"
243 ;;
244
245 let parse f str =
246   node_of_t
247     (f str 
248        !Options.sample_factor 
249        !Options.index_empty_texts
250        !Options.disable_text_collection)
251     
252 let parse_xml_uri str = parse parse_xml_uri str
253 let parse_xml_string str =  parse parse_xml_string str
254
255      
256 external pool : tree -> Tag.pool = "%identity"
257
258 let save t str = (save_tree t.doc str)
259 ;;
260
261 let load ?(sample=64) str = 
262   node_of_t (load_tree str sample)
263     
264
265
266
267 let tag_pool t = pool t.doc
268   
269 let compare a b = match a.node,b.node  with
270   | Nil, Nil -> 0
271   | Nil,_ -> 1
272   | _ , Nil -> -1
273   | Node(i),Node(j) -> compare_node i j
274   | Text(i,_), Text(j,_) -> compare_node i j
275   | Node(i), Text(_,j) -> compare_node i j
276   | Text(_,i), Node(j) -> compare_node i j 
277
278 let equal a b = (compare a b) == 0
279   
280   
281 let norm (n : [`Tree ] node ) =  if n == -1 then Nil else Node (n)
282   
283 let nts = function
284     Nil -> "Nil"
285   | Text (i,j) -> Printf.sprintf "Text (%i, %i)" i j
286   | Node (i) -> Printf.sprintf "Node (%i)"  i
287       
288 let dump_node t = nts t.node
289
290 let mk_nil t = { t with node = Nil }              
291 let root n = { n with node = norm (tree_root n.doc) }
292
293 let is_root n = match n.node with
294   | Node(t) -> (int_of_node t) == 0 
295   | _ -> false
296       
297 let is_left n = match n.node with
298   | Node(t) -> (tree_is_first_child n.doc t) && (equal_node nil (tree_prev_text n.doc t))
299   | Text(_,t) -> tree_is_nil t || tree_is_first_child n.doc t
300   | _ -> false
301
302 let is_below_right t1 t2 =
303   match (t1.node,t2.node) with
304     | Nil,_ | _,Nil -> false
305     | Node(i1), Node(i2)   -> 
306         tree_is_ancestor t1.doc (tree_parent t1.doc i1) i2
307         && not (tree_is_ancestor t1.doc i1 i2)
308     | Text(_,i1),Node(i2) -> i1 == i2 ||
309         (tree_is_ancestor t1.doc (tree_parent t1.doc i1) i2 && i1 < i2)
310     | Text(_,i1),Text(i,_) ->   
311         let x,y = tree_doc_ids t1.doc i1 in
312           i >= x && i <= y          
313     | Node(i1), Text(i,_) -> 
314         let i2 = tree_next_sibling t1.doc i1 in
315         let x,y = tree_doc_ids t1.doc i2 in
316           i >= x && i <= y
317
318 let parent n =  
319   let node' = 
320     match n.node with (* inlined parent *)
321       | Node(t) when (int_of_node t)== 0 -> Nil
322       | Node(t) -> 
323           let txt = tree_prev_text n.doc t in
324             if text_is_empty n.doc txt then
325               let ps = tree_prev_sibling n.doc t in
326                 if tree_is_nil ps
327                 then
328                   Node(tree_parent n.doc t)
329                 else Node(ps)
330             else
331               Text(txt,t)
332       | Text(i,t) ->
333           let ps = tree_prev_doc n.doc i in
334             if tree_is_nil ps
335             then Node (tree_parent_doc n.doc i)
336             else Node(ps)
337       | _ -> failwith "parent"
338   in
339     { n with node = node' }
340
341 let node_child n =
342   match n.node with
343     | Node i ->  { n with node= norm(tree_first_child n.doc i) }
344     | _ -> { n with node = Nil }
345
346 let node_sibling n =
347   match n.node with
348     | Node i ->  { n with node= norm(tree_next_sibling n.doc i) }
349     | _ -> { n with node = Nil }
350
351 let node_sibling_ctx  n _ = 
352   match n.node with
353     | Node i ->  { n with node= norm(tree_next_sibling n.doc i) }
354     | _ -> { n with node = Nil }
355
356
357 let first_child n = 
358   let node' = 
359     match n.node with
360       | Node (t) -> 
361           let fs = tree_first_child n.doc t in
362             if equal_node nil fs
363             then 
364               let txt = tree_my_text n.doc t in
365                 if equal_node nil txt
366                 then Nil
367                 else Text(txt,nil)
368             else
369               let txt = tree_prev_text n.doc fs in
370                 if equal_node nil txt
371                 then Node(fs)
372                 else Text(txt, fs) 
373       | Text(_,_) -> Nil
374       | Nil -> failwith "first_child"
375   in
376     { n with node = node'}
377       
378 let next_sibling n = 
379   let node' =
380     match n.node with
381       | Text (_,ns) -> norm ns
382       | Node(t) ->
383           let ns = tree_next_sibling n.doc t in
384           let txt = tree_next_text n.doc t in
385             if equal_node nil txt
386             then norm ns
387             else Text(txt, ns)
388       | Nil -> failwith "next_sibling"
389   in
390     { n with node = node'}
391           
392 let next_sibling_ctx n _ = next_sibling n
393           
394 let left = first_child 
395 let right = next_sibling
396     
397 let id t = 
398   match t.node with
399     | Node(n)  -> tree_node_xml_id t.doc n
400     | Text(i,_)  -> tree_text_xml_id t.doc i
401     | _ ->  -1 
402         
403 let tag t =
404   match t.node with 
405   | Text(_) -> Tag.pcdata
406   | Node(n) -> tree_tag_id t.doc n
407   | Nil -> Tag.nullt
408
409 (*
410 let select_next tb tf t s = 
411   match s.node  with
412     | Node (below) -> begin
413         match t.node with
414           | Node( n)  ->
415               { t with node = norm (tree_select_next t.doc n (Ptset.Int.to_int_vector tb) (Ptset.Int.to_int_vector tf) below) }
416           | Text (i,n)  when equal_node nil n ->
417               let p = tree_parent_doc t.doc i in
418                 { t with node = norm (tree_select_next t.doc p (Ptset.Int.to_int_vector tb) (Ptset.Int.to_int_vector tf) below) }
419           | Text(_,n)  ->
420               if Ptset.mem (tree_tag_id t.doc n) (Ptset.Int.union tb tf)
421               then { t with node=Node(n) }
422               else
423                 let vb = Ptset.Int.to_int_vector tb in
424                 let vf = Ptset.Int.to_int_vector tf in
425                 let node = 
426                   let dsc = tree_select_below t.doc n vb vf in
427                     if equal_node nil dsc
428                     then tree_select_next t.doc n vb vf below
429                     else dsc
430                 in
431                   { t with node = norm node }
432           | _ -> {t with node = Nil }
433       end
434         
435     | _ -> { t with node = Nil }
436
437   
438
439
440   let select_foll_only  tf t s = 
441     match s.node  with
442       | Node (below)  -> 
443           begin
444             match t.node with
445             | Node(n) ->
446                 { t with node= norm (tree_select_foll_only t.doc n (Ptset.Int.to_int_vector tf) below) }
447             | Text(i,n)  when equal_node nil n ->
448                 let p = tree_parent_doc t.doc i in
449                   { t with node= norm (tree_select_foll_only t.doc p (Ptset.Int.to_int_vector tf) below) }
450             |  Text(_,n) ->
451                  if Ptset.mem (tree_tag_id t.doc n) tf
452                  then { t with node=Node(n) }
453                  else
454                    let vf = Ptset.Int.to_int_vector tf in
455                    let node = 
456                      let dsc = tree_select_desc_only t.doc n vf in
457                        if tree_is_nil dsc
458                        then tree_select_foll_only t.doc n vf below
459                        else dsc
460                    in
461                      { t with node = norm node }
462             | _ -> { t with node = Nil }
463         end         
464       | _ -> {t with node=Nil }   
465
466 let select_below  tc td t=
467   match t.node with
468     | Node( n) -> 
469         let vc = Ptset.Int.to_int_vector tc
470         in
471         let vd = Ptset.Int.to_int_vector td
472         in
473           { t with node= norm(tree_select_below t.doc n vc vd) }
474     | _ -> { t with node=Nil }
475         
476         
477 let select_desc_only  td t =
478   match t.node with
479     | Node(n) -> 
480         let vd = Ptset.Int.to_int_vector td
481         in
482           { t with node = norm(tree_select_desc_only t.doc n vd) }
483     | _ -> { t with node = Nil }
484
485 *)
486 let tagged_desc tag t =
487   match t.node with
488     | Node(n) ->        
489           { t with node = norm(tree_tagged_desc t.doc n tag) }
490     | _ -> { t with node = Nil }
491
492
493 let tagged_foll_ctx tag t s =
494     match s.node  with
495       | Node (below)  -> 
496           begin
497             match t.node with
498             | Node(n) ->
499                 { t with node= norm (tree_tagged_foll_below t.doc n tag below) }
500             | Text(i,n)  when equal_node nil n ->
501                 let p = tree_prev_doc t.doc i in
502                   { t with node= norm (tree_tagged_foll_below t.doc p tag below) }
503             |  Text(_,n) ->
504                  if (tree_tag_id t.doc n) == tag
505                  then { t with node=Node(n) }
506                  else
507                    let node = 
508                      let dsc = tree_tagged_desc t.doc n tag in
509                        if tree_is_nil dsc
510                        then tree_tagged_foll_below t.doc n tag below
511                        else dsc
512                    in
513                      { t with node = norm node }
514             | _ -> { t with node = Nil }
515           end       
516       | _ -> {t with node=Nil }   
517
518
519
520
521 let last_idx = ref 0
522 let array_find a i j =
523   let l = Array.length a in
524   let rec loop idx x y =
525     if x > y || idx >= l then nil
526         else
527           if a.(idx) >= x then if a.(idx) > y then nil else (last_idx := idx;a.(idx))
528           else loop (idx+1) x y
529   in
530     if a.(0) > j || a.(l-1) < i then nil
531     else loop !last_idx i j 
532
533
534         
535 let text_below t = 
536   let l = Array.length !contains_array in
537       match t.node with
538         | Node(n)  -> 
539             let i,j = tree_doc_ids t.doc n in
540             let id = if l == 0 then i else (array_find !contains_array i j)
541             in
542 (*            Printf.printf "Looking for text below node %i with tag %s in range %i %i, in array : [|\n%!"
543                 n (Tag.to_string (tree_tag_id t.doc n)) i j;
544               Array.iter (fun i -> Printf.printf "%i " (int_of_node i )) !contains_array;
545               Printf.printf "|]\nResult is %i\n%!" id;        *)
546               if id == nil then  
547                 { t with  node=Nil }
548               else
549                 { t with  node = Text(id, tree_next_sibling t.doc (tree_prev_doc t.doc id)) }
550         | _ -> (*Printf.printf "Here\n%!"; *)
551             { t with node = Nil }
552             
553 let text_next t root =
554   let l = Array.length !contains_array in
555       let inf = match t.node with
556         | Node(n)  -> snd(tree_doc_ids t.doc n)+1
557         | Text(i,_)  -> i+1
558         | _ -> assert false
559       in
560         match root.node with
561           | Node (n)  ->
562               let _,j = tree_doc_ids t.doc n in      
563               let id = if l == 0 then if inf > j then nil else inf
564               else array_find !contains_array inf j
565               in
566                 if id == nil then  { t with node= Nil }
567                 else
568                   { t with node = Text(id,tree_next_sibling t.doc (tree_prev_doc t.doc id)) }
569           | _ -> { t with node = Nil}
570                   
571
572 (*                
573     let subtree_tags t tag =
574       match t with 
575           { doc = d; node = Node(NC n) } -> 
576             subtree_tags d n tag
577         | _ -> 0
578
579     let select_desc_array = ref [| |]
580     let idx = ref 0
581
582     let init_tagged_next t tagid =
583       let l = subtree_tags (root t) tagid
584       in
585         tagged_desc_array := Array.create l { t with node= Nil };
586         let i = ref 0 in
587           let rec collect t =
588             if is_node t then begin
589               if tag t == tagid then
590                 begin
591                   !tagged_desc_array.(!i) <- t;
592                   incr i;
593                 end;
594               collect (first_child t);
595               collect (next_sibling t)
596             end;
597           in
598             collect t;
599             idx := 0
600
601     let print_id ppf v = 
602       let pr x= Format.fprintf ppf x in
603         match v with
604             { node=Nil } -> pr "NULLT: -1"
605           | { node=String(i) } | { node=Node(SC(i,_)) } -> pr "DocID: %i" (int_of_node i)
606           | { node=Node(NC(i)) } -> pr "Node: %i" (int_of_node i)
607               
608               
609           
610 (*    let tagged_next t tag = 
611       if !idx >= Array.length !tagged_desc_array 
612       then {t with node=Nil}
613       else
614         let r = !tagged_desc_array.(!idx) 
615         in
616           incr idx; r
617 *)                
618
619
620     let has_tagged_foll t tag = is_node (tagged_foll t tag)
621     let has_tagged_desc t tag = is_node (tagged_desc t tag)
622
623     let contains t s = 
624       Array.fold_left (fun a i -> DocIdSet.add i a) DocIdSet.empty (Text.contains t.doc s)
625
626
627     let contains_old t s = 
628       let regexp = Str.regexp_string s in
629       let matching arg = 
630         try
631           let _ = Str.search_forward regexp arg 0;
632           in true
633         with _ -> false
634       in
635       let rec find t acc = match t.node with
636         | Nil -> acc
637         | String i ->
638             if  matching (string t) then DocIdSet.add i acc else acc
639         | Node(_) ->  (find (left t )) ((find (right t))  acc)
640       in
641         find t DocIdSet.empty
642
643
644     let contains_iter t s = 
645       let regexp = Str.regexp_string s in
646       let matching arg = 
647         try
648           let _ = Str.search_forward regexp arg 0;
649           in true
650         with _ -> false
651       in
652       let size = Text.size t.doc in
653       let rec find acc n = 
654         if n == size then acc
655         else
656           find 
657             (if matching (Text.get_cached_text t.doc (Obj.magic n)) then 
658              DocIdSet.add (Obj.magic n) acc
659            else acc) (n+1)
660       in
661         find DocIdSet.empty 0
662
663
664
665
666     let count_contains t s =   Text.count_contains t.doc s
667 *)
668
669   let count t s = text_count t.doc s
670 (*
671     let is_left t =
672       if is_root t then false
673       else
674       if tag (parent t) == Tag.pcdata then false
675       else
676         let u = left (parent t) in
677           (id t) == (id u)
678 *)
679   let print_xml_fast outc t =
680     let rec loop ?(print_right=true) t = 
681       match t.node with 
682       | Nil -> ()    
683       | Text(i,n) -> output_string outc (get_cached_text t.doc i);
684           if print_right
685           then loop (right t)
686       | Node (n) -> 
687           let tg = Tag.to_string (tag t) in
688           let l = left t 
689           and r = right t 
690           in
691             output_char outc  '<';
692             output_string outc  tg;
693             ( match l.node with
694                   Nil -> output_string outc  "/>"
695                 | Node(_) when Tag.equal (tag l) Tag.attribute -> 
696                     (loop_attributes (left l);
697                      match (right l).node with
698                        | Nil -> output_string outc  "/>"
699                        | _ -> 
700                            output_char outc  '>'; 
701                            loop (right l);
702                            output_string outc  "</";
703                            output_string outc  tg;
704                            output_char outc '>' )
705                 | _ ->
706                     output_char outc  '>'; 
707                     loop l;
708                     output_string outc "</";
709                     output_string outc tg;
710                     output_char outc '>'
711             );if print_right then loop r
712     and loop_attributes a =      
713         match a.node with 
714           | Node(_) ->
715               let value =
716                 match (left a).node with
717                   | Text(i,_) -> (get_cached_text a.doc i)
718                   | _ -> assert false
719               in
720                 output_char outc ' ';
721                 output_string outc (Tag.to_string (tag a));
722                 output_string outc "=\"";
723                 output_string outc value;
724                 output_char outc '"';
725                 loop_attributes (right a)
726           | _ -> ()
727     in
728         loop ~print_right:false t
729           
730           
731     let print_xml_fast outc t = 
732       if Tag.to_string (tag t) = "" then
733         print_xml_fast outc (first_child t)
734       else print_xml_fast outc t
735         
736
737 let tags_below t tag = 
738   fst(Hashtbl.find t.ttable tag)
739
740 let tags_after t tag = 
741   snd(Hashtbl.find t.ttable tag)
742
743 let tags t tag = Hashtbl.find t.ttable tag
744
745 let  tagged_lowest t tag = 
746   let rec loop_lowest i = 
747     let j = tree_tagged_desc t.doc i tag in
748     if tree_is_nil j then i else loop_lowest j
749   in
750     match t.node with
751       | Node i ->
752           let j = loop_lowest i in 
753             { t with 
754                 node = norm(
755                   if tree_is_nil j then
756                     if (tree_tag_id t.doc i) == tag
757                     then i
758                     else j
759                   else j) }
760       | Nil -> t
761       | _ -> assert false
762           
763           
764 let tagged_next t tag = 
765   match t.node with
766     | Node(i) -> 
767         let n = tree_tagged_foll_below t.doc i tag (Obj.magic 0)
768         in
769           if tree_is_nil  n then mk_nil t
770           else 
771             tagged_lowest { t with node = Node n } tag
772     | Nil -> t
773     | _ -> assert false
774
775 let rec binary_parent t = 
776   let res = 
777   match t.node with
778   | Node(0) -> { t with node = Nil }
779   | Node(i) ->
780       let j = tree_prev_sibling t.doc i in
781         if tree_is_nil j then
782           let idoc = tree_prev_text t.doc i in
783             if equal_node nil idoc then
784               { t with node = Node (tree_parent t.doc i) }
785             else 
786               { t with node = Text(idoc,i) }
787         else
788           let idoc = tree_prev_text t.doc i in
789             if equal_node nil idoc then
790               { t with node = Node (j) }
791             else { t with node = Text(idoc,i) }
792   | Text(d,i) ->       
793       if tree_is_nil i then
794         let n = tree_parent_doc t.doc d in
795         let lc = tree_last_child t.doc n in
796           if tree_is_nil lc then {t with node = Node n }
797           else { t with node = Node lc }
798       else
799         let j = tree_prev_sibling t.doc i in
800           if tree_is_nil j then
801             { t with node = Node (tree_parent t.doc i) }
802           else { t with node = Node j }
803   | Nil -> t
804   in match res.node with
805     | Text(idoc,t) -> 
806         if (Array.length !contains_array) != 0
807         then if in_array !contains_array idoc then res
808         else binary_parent res
809         else res
810     | _ -> res
811
812 let benchmark_text t =
813   let doc = t.doc in
814     match (root t).node with
815       | Node i -> let _,size = tree_doc_ids doc i in
816           Printf.eprintf "%i will take ~ %i seconds\n%!"
817             size (size/10000) ;
818         let a = Array.create size "" in
819           for i = 0 to size 
820           do
821             a.(i) <- text_get_tc_text t.doc (i+1)
822           done; a
823       | _ -> assert false
824
825 let doc_ids (t:t) : (int*int) = 
826   (Obj.magic (
827     match t.node with
828       | Node i -> tree_doc_ids t.doc i
829       | Text (i,_) -> (i,i)
830       | Nil -> (nil,nil)
831    ))
832
833 let subtree_tags t tag = match t.node with
834   | Nil -> 0
835   | Node(i) -> tree_subtree_tags t.doc i tag
836   | Text(_,i) -> tree_subtree_tags t.doc i tag
837
838 let get_text t = match t.node with
839   | Text(i,_) -> get_cached_text t.doc i
840   | _ -> ""