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