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