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 (******************************************************************************)
11 external init_lib : unit -> unit = "sxsi_cpp_init"
13 exception CPlusPlusError of string
15 let () = Callback.register_exception "CPlusPlusError" (CPlusPlusError "")
19 type node = [ `Tree ] Node.t
23 external register_tag : tree -> string -> Tag.t = "caml_xml_tree_register_tag"
25 external tag_name : tree -> Tag.t -> string = "caml_xml_tree_get_tag_name"
27 let tag t = (); fun s ->
30 | "<@>" -> Tag.attribute
31 | "" -> Tag.document_node
32 | "<@$>" -> Tag.attribute_data
33 | _ -> register_tag t s
37 if t == Tag.pcdata then "<$>"
38 else if t == Tag.attribute_data then "<@$>"
39 else if t == Tag.attribute then "<@>"
40 else if t == Tag.nullt then "<!NIL!>"
47 Tag.to_string = to_string t;
48 Tag.translate = translate
54 external create : unit -> t = "caml_xml_tree_builder_create"
55 external open_document : t -> int -> bool -> int -> unit = "caml_xml_tree_builder_open_document"
56 external close_document : t -> tree = "caml_xml_tree_builder_close_document"
57 external open_tag : t -> string -> unit = "caml_xml_tree_builder_open_tag"
58 external close_tag : t -> string -> unit = "caml_xml_tree_builder_close_tag"
59 external text : t -> string -> unit = "caml_xml_tree_builder_text"
60 external trim : string -> string = "caml_trim"
66 (c == '\n' || c == '\t' || c == ' ') && loop len (i+1)
70 loop (String.length s) 0
74 let event_counter = ref 0 in
77 if !event_counter land 0xffffff == 0 then
78 Logger.print Format.err_formatter "Current position: %i@\n@?" (Expat.get_current_byte_index parser_))
82 if Buffer.length t > 0 then begin
83 let s = Buffer.contents t in
84 if (!Config.index_empty_texts) || not (is_whitespace s) then
93 let output_attr b name value =
94 let atname = "<@>" ^ name in
101 let start_element_handler parser_ b t tag attr_list =
108 List.iter (fun (name, value) -> output_attr b name value) l;
112 let end_element_handler parser_ b t tag =
116 let character_data_handler parser_ _ t text =
117 Buffer.add_string t text
119 let create_parser () =
120 let buf = Buffer.create 512 in
121 let build = create () in
122 let parser_ = Expat.parser_create ~encoding:None in
126 LOG ( __ "parsing" 2 "%s\n" "Finished parsing");
127 LOG ( __ "indexing" 2 "%s\n" "Starting index construction");
128 let r = close_document build
130 LOG ( __ "indexing" 2 "%s\n" "Finished index construction");
133 Expat.set_start_element_handler parser_ (start_element_handler parser_ build buf);
134 Expat.set_end_element_handler parser_ (end_element_handler parser_ build buf);
135 Expat.set_character_data_handler parser_ (character_data_handler parser_ build buf);
136 LOG ( __ "parsing" 2 "%s\n" "Started parsing");
137 open_document build !Config.sample_factor !Config.disable_text_collection !Config.text_index_type;
142 let parser_, finalizer = create_parser () in
143 Expat.parse parser_ s;
146 let parse_file file =
147 let in_chan = open_in file in
148 let buffer = String.create 4096 in
149 let parser_, finalizer = create_parser () in
153 let read = input in_chan buffer 0 4096 in
154 if read == 0 then raise End_of_file else
155 Expat.parse_sub parser_ buffer 0 read;
159 | End_of_file -> close_in in_chan
162 Utils.time ~msg:"Parsing XML file" parse ();
163 Utils.time ~msg:"Creating tree and text-collection index" finalizer ()
170 type bit_vector = string
172 external bool_of_int : int -> bool = "%identity"
173 external int_of_bool : bool -> int = "%identity"
175 let bit_vector_unsafe_get v i =
177 (((Char.code (String.unsafe_get v (i lsr 3))) lsr (i land 7)) land 1)
178 let chr (c:int) : char = Obj.magic (c land 0xff)
179 let bit_vector_unsafe_set v i b =
181 let c = Char.code v.[j] in
182 let bit = int_of_bool b in
183 let mask = bit lsl (i land 7) in
184 if b then v.[j] <- chr (c lor mask) else v.[j] <- (chr (c land (lnot mask)))
186 let bit_vector_create n =
187 let len = if n <= 0 then 0 else (n - 1) / 8 + 1 in
188 String.make len '\000'
192 external tag_list_alloc : int -> tag_list = "caml_tag_list_alloc"
193 external tag_list_set : tag_list -> int -> Tag.t -> unit = "caml_tag_list_set" "noalloc"
197 elements: Ptset.Int.t;
198 attributes: Ptset.Int.t;
199 attribute_array : tag_list;
200 children : Ptset.Int.t array;
201 siblings : Ptset.Int.t array;
202 descendants: Ptset.Int.t array;
203 followings: Ptset.Int.t array;
207 let tag_operations t = mk_tag_ops t.doc
209 external parse_xml_uri : string -> int -> bool -> bool -> int -> tree = "caml_call_shredder_uri"
210 external parse_xml_string : string -> int -> bool -> bool -> int -> tree = "caml_call_shredder_string"
212 external tree_print_xml_fast3 : tree -> [`Tree ] Node.t -> Unix.file_descr -> unit = "caml_xml_tree_print"
213 let print_xml t n fd =
214 tree_print_xml_fast3 t.doc n fd
217 external tree_save : tree -> Unix.file_descr -> string -> unit = "caml_xml_tree_save"
218 external tree_load : Unix.file_descr -> string -> bool -> int -> tree = "caml_xml_tree_load"
220 external nullt : unit -> 'a Node.t = "caml_xml_tree_nullt"
222 let nil : [`Tree ] Node.t = Node.nil
223 let root : [`Tree ] Node.t = Node.null
226 module HPtset = Hashtbl.Make(Ptset.Int)
228 let vector_htbl = HPtset.create MED_H_SIZE
229 let reinit () = HPtset.clear vector_htbl
231 let tag_list_of_set s =
233 HPtset.find vector_htbl s
236 let v = tag_list_alloc (Ptset.Int.cardinal s + 1) in
238 let () = Ptset.Int.iter (fun e -> tag_list_set v !i e; incr i) s in
239 let () = tag_list_set v !i Tag.nullt in
240 HPtset.add vector_htbl s v; v
242 (** tree interface *)
244 external tree_root : tree -> [`Tree] Node.t = "caml_xml_tree_root" "noalloc"
247 external tree_first_child : tree -> [`Tree] Node.t -> [`Tree] Node.t = "caml_xml_tree_first_child" "noalloc"
248 let first_child t n = tree_first_child t.doc n
250 external tree_first_element : tree -> [`Tree] Node.t -> [`Tree] Node.t = "caml_xml_tree_first_element" "noalloc"
251 let first_element t n = tree_first_element t.doc n
253 external tree_tagged_child : tree -> [`Tree] Node.t -> Tag.t -> [`Tree] Node.t = "caml_xml_tree_tagged_child" "noalloc"
254 let tagged_child t n tag = tree_tagged_child t.doc n tag
256 external tree_select_child : tree -> [`Tree ] Node.t -> tag_list -> [`Tree] Node.t = "caml_xml_tree_select_child" "noalloc"
257 let select_child t n tag_set = tree_select_child t.doc n tag_set
259 external tree_last_child : tree -> [`Tree] Node.t -> [`Tree] Node.t = "caml_xml_tree_last_child" "noalloc"
260 let last_child t n = tree_last_child t.doc n
263 external tree_next_sibling : tree -> [`Tree] Node.t -> [`Tree] Node.t = "caml_xml_tree_next_sibling" "noalloc"
264 let next_sibling t n = tree_next_sibling t.doc n
266 external tree_next_element : tree -> [`Tree] Node.t -> [`Tree] Node.t = "caml_xml_tree_next_element" "noalloc"
267 let next_element t n = tree_next_element t.doc n
270 external tree_tagged_sibling : tree -> [`Tree] Node.t -> Tag.t -> [`Tree] Node.t = "caml_xml_tree_tagged_sibling" "noalloc"
271 let tagged_sibling t n tag = tree_tagged_sibling t.doc n tag
274 external tree_select_sibling : tree -> [`Tree ] Node.t -> tag_list -> [`Tree] Node.t = "caml_xml_tree_select_sibling" "noalloc"
275 let select_sibling t n tag_set = tree_select_sibling t.doc n tag_set
277 external tree_prev_sibling : tree -> [`Tree] Node.t -> [`Tree] Node.t = "caml_xml_tree_prev_sibling" "noalloc"
278 let prev_sibling t n = tree_prev_sibling t.doc n
282 external tree_tagged_descendant : tree -> [`Tree ] Node.t -> Tag.t -> [`Tree ] Node.t = "caml_xml_tree_tagged_descendant" "noalloc"
283 let tagged_descendant t n tag = tree_tagged_descendant t.doc n tag
285 external tree_tagged_next : tree -> [`Tree ] Node.t -> Tag.t -> [`Tree ] Node.t = "caml_xml_tree_tagged_next" "noalloc"
286 let tagged_next t n tag = tree_tagged_next t.doc n tag
288 external tree_select_descendant : tree -> [`Tree ] Node.t -> tag_list -> [`Tree] Node.t = "caml_xml_tree_select_descendant" "noalloc"
289 let select_descendant t n tag_set = tree_select_descendant t.doc n tag_set
291 external tree_tagged_following_before : tree -> [`Tree ] Node.t -> Tag.t -> [`Tree ] Node.t -> [`Tree ] Node.t = "caml_xml_tree_tagged_following_before" "noalloc"
292 let tagged_following_before t n tag ctx = tree_tagged_following_before t.doc n tag ctx
294 external tree_select_following_before : tree -> [`Tree ] Node.t -> tag_list -> [`Tree] Node.t -> [`Tree] Node.t = "caml_xml_tree_select_following_before" "noalloc"
295 let select_following_before t n tag_set ctx = tree_select_following_before t.doc n tag_set ctx
297 external tree_parent : tree -> [`Tree] Node.t -> [`Tree] Node.t = "caml_xml_tree_parent" "noalloc"
298 let parent t n = tree_parent t.doc n
300 external tree_tag : tree -> [`Tree] Node.t -> Tag.t = "caml_xml_tree_tag" "noalloc"
301 let tag t n = tree_tag t.doc n
303 external tree_is_first_child : tree -> [ `Tree ] Node.t -> bool = "caml_xml_tree_is_first_child" "noalloc"
304 let is_first_child t n = tree_is_first_child t.doc n
306 external tree_is_right_descendant : tree -> [ `Tree ] Node.t -> [`Tree] Node.t -> bool =
307 "caml_xml_tree_is_right_descendant" "noalloc"
309 let is_right_descendant t n1 n2 = tree_is_right_descendant t.doc n1 n2
312 let node_tags t = Ptset.Int.add Tag.document_node t.descendants.(Tag.document_node)
314 let attribute_tags t = t.attributes
316 let element_tags t = t.elements
319 t.children.(tag), t.descendants.(tag), t.siblings.(tag), t.followings.(tag)
322 let dump_tag_table t =
324 let printer ppf set =
325 Logger.print ppf "%s: %a"
326 (Tag.to_string !tag) TagSet.print (TagSet.inj_positive set);
329 let set_printer msg set =
331 Logger.print err_formatter "%s :@\n" msg;
332 Pretty.pp_print_array ~sep:pp_force_newline printer err_formatter set;
333 Logger.print err_formatter "-----------------------------@\n";
335 set_printer "Child tags" t.children;
336 set_printer "Descendant tags" t.descendants;
337 set_printer "Sibling tags" t.siblings;
338 set_printer "Following tags" t.followings
340 external tree_subtree_tags : tree -> [`Tree] Node.t -> Tag.t -> int = "caml_xml_tree_subtree_tags" "noalloc"
341 let subtree_tags t n tag = tree_subtree_tags t.doc n tag
343 external tree_subtree_size : tree -> [`Tree] Node.t -> int = "caml_xml_tree_subtree_size" "noalloc"
344 let subtree_size t n = tree_subtree_size t.doc n
346 let rec iter_array_tag i a len tree node acc =
349 iter_array_tag (i+1) a len tree node
350 (acc - (tree_subtree_tags tree node a.(i)))
352 external tree_subtree_elements : tree -> [`Tree] Node.t -> int = "caml_xml_tree_subtree_elements" "noalloc"
354 let subtree_elements t node =
355 tree_subtree_elements t.doc node
357 let subtree_elements t node =
358 let size = tree_subtree_size t.doc node - 1 in
360 else let size = size - (tree_subtree_tags t.doc node Tag.pcdata) in
361 if size < 3 then size else
362 let a = t.attribute_array in
363 iter_array_tag 0 a (Array.length a) t.doc node size
366 external tree_closing : tree -> [`Tree] Node.t -> [`Tree] Node.t = "caml_xml_tree_closing" "noalloc"
367 let closing t n = tree_closing t.doc n
369 external tree_num_tags : tree -> int = "caml_xml_tree_num_tags" "noalloc"
370 let num_tags t = tree_num_tags t.doc
372 external tree_size : tree -> int = "caml_xml_tree_size" "noalloc"
373 let size t = tree_size t.doc
383 external hash : t -> int = "%identity"
384 external uid : t -> Uid.t = "%identity"
385 external equal : t -> t -> bool = "%eq"
386 external make : t -> int = "%identity"
387 external node : t -> int = "%identity"
388 external stats : unit -> unit = "%identity"
389 external init : unit -> unit = "%identity"
392 let to_ptset s = fold (Ptset.Int.add) s Ptset.Int.empty
396 Hashtbl.Make(struct type t = TagS.t * TagS.t
398 HASHINT2(Uid.to_int x.TagS.Node.id,
399 Uid.to_int y.TagS.Node.id)
406 Hashtbl.Make(struct type t = Tag.t * TagS.t
408 HASHINT2(x, Uid.to_int y.TagS.Node.id)
415 let add_cache = TagTSCache.create 1023
416 let union_cache = TSTSCache.create 1023
417 let subset_cache = TSTSCache.create 1023
420 TSTSCache.clear union_cache;
421 TSTSCache.clear subset_cache;
422 TagTSCache.clear add_cache
425 (x == y) || (x == TagS.empty) ||
426 if y == TagS.empty then false
430 TSTSCache.find subset_cache key
433 let z = TagS.subset x y in
434 TSTSCache.add subset_cache key z; z
436 let order ((x, y) as z) =
437 if x.TagS.Node.id <= y.TagS.Node.id then z
441 if _subset x y then y
442 else if _subset y x then x
444 let key = order (x, y) in
446 TSTSCache.find union_cache key
449 let z = TagS.union x y in
450 TSTSCache.add union_cache key z; z
455 TagTSCache.find add_cache key
458 let z = TagS.add t s in
459 TagTSCache.add add_cache key z;z
461 let child_sibling_labels tree =
462 let table_c = Array.create (tree_num_tags tree) TagS.empty in
463 let table_n = Array.copy table_c in
465 if node == nil then TagS.empty
467 let children = loop (tree_first_child tree node) in
468 let tag = tree_tag tree node in
470 let tc = table_c.(tag) in
471 if _subset children tc then ()
472 else table_c.(tag) <- _union tc children
474 let siblings = loop (tree_next_sibling tree node) in
476 let tn = table_n.(tag) in
477 if _subset siblings tn then ()
478 else table_n.(tag) <- _union tn siblings
485 let descendant_labels tree =
486 let table_d = Array.create (tree_num_tags tree) TagS.empty in
488 if node == nil then TagS.empty else
489 let d1 = loop (tree_first_child tree node) in
490 let d2 = loop (tree_next_sibling tree node) in
491 let tag = tree_tag tree node in
493 let td = table_d.(tag) in
494 if _subset d1 td then ()
495 else table_d.(tag) <- _union td d1;
497 _add tag (_union d1 d2)
502 let collect_labels tree =
503 let table_f = Array.create (tree_num_tags tree) TagS.empty in
504 let table_n = Array.copy table_f in
505 let table_c = Array.copy table_f in
506 let table_d = Array.copy table_f in
507 let rec loop node foll_siblings descendants followings =
508 if node == nil then foll_siblings, descendants, followings else
509 let tag = tree_tag tree node in
511 let tf = table_f.(tag) in
512 if _subset followings tf then ()
513 else table_f.(tag) <- _union tf followings in
515 let tn = table_n.(tag) in
516 if _subset foll_siblings tn then ()
517 else table_n.(tag) <- _union tn foll_siblings in
518 let children, n_descendants, n_followings =
519 loop (tree_last_child tree node) TagS.empty TagS.empty followings
522 let tc = table_c.(tag) in
523 if _subset children tc then ()
524 else table_c.(tag) <- _union tc children
527 let td = table_d.(tag) in
528 if _subset n_descendants td then ()
529 else table_d.(tag) <- _union td n_descendants
531 loop (tree_prev_sibling tree node)
532 (_add tag foll_siblings)
533 (_add tag (_union n_descendants descendants))
534 (_add tag n_followings)
536 ignore (loop root TagS.empty TagS.empty TagS.empty);
537 table_f, table_n, table_c, table_d
540 let is_nil t = t == nil
541 let is_node t = t != nil
542 let is_root t = t == root
545 LOG ( __ "indexing" 2 "%s\n" "Initializing tag structure");
546 let _ = Tag.init (mk_tag_ops t) in
547 LOG ( __ "indexing" 2 "%s\n" "Starting tag table construction");
548 let f, n, c, d = Utils.time ~msg:"Building tag relationship table" collect_labels t in
549 let c = Array.map TagS.to_ptset c in
550 let n = Array.map TagS.to_ptset n in
551 let f = Array.map TagS.to_ptset f in
552 let d = Array.map TagS.to_ptset d in
553 let () = clear_cache () in
554 let attributes = Ptset.Int.add Tag.attribute d.(Tag.attribute) in
555 let elements = Ptset.Int.add Tag.document_node
556 (Ptset.Int.remove Tag.pcdata
557 (Ptset.Int.diff d.(Tag.document_node) attributes))
560 attributes = attributes;
561 attribute_array = tag_list_of_set attributes;
571 let parse_xml_uri str = node_of_t (TreeBuilder.parse_file str)
572 let parse_xml_string str = node_of_t (TreeBuilder.parse_string str)
574 let size t = tree_size t.doc;;
576 let magic_string = "SXSI_INDEX"
577 let version_string = "4"
580 Unix.lseek fd 0 Unix.SEEK_CUR
582 let pr_pos fd = Logger.print err_formatter "At position %i@\n" (pos fd)
585 let sl = String.length s in
586 let ssl = Printf.sprintf "%020i" sl in
587 ignore (Unix.write fd ssl 0 20);
588 ignore (Unix.write fd s 0 (String.length s))
590 let rec really_read fd buffer start length =
591 if length <= 0 then () else
592 match Unix.read fd buffer start length with
593 0 -> raise End_of_file
594 | r -> really_read fd buffer (start + r) (length - r);;
597 let buffer = String.create 20 in
598 let _ = really_read fd buffer 0 20 in
599 let size = int_of_string buffer in
600 let buffer = String.create size in
601 let _ = really_read fd buffer 0 size in
604 let save_tag_table channel t =
605 let t = Array.map (fun s -> Array.of_list (Ptset.Int.elements s)) t in
606 Marshal.to_channel channel t []
609 let fd = Unix.openfile str [ Unix.O_WRONLY;Unix.O_TRUNC;Unix.O_CREAT] 0o644 in
610 let out_c = Unix.out_channel_of_descr fd in
611 let index_prefix = Filename.chop_suffix str ".srx" in
612 let _ = set_binary_mode_out out_c true in
613 output_string out_c magic_string;
614 output_char out_c '\n';
615 output_string out_c version_string;
616 output_char out_c '\n';
617 save_tag_table out_c t.children;
618 save_tag_table out_c t.siblings;
619 save_tag_table out_c t.descendants;
620 save_tag_table out_c t.followings;
621 (* we need to move the fd to the correct position *)
623 ignore (Unix.lseek fd (pos_out out_c) Unix.SEEK_SET);
624 tree_save t.doc fd index_prefix;
627 let load_tag_table channel =
628 let table : int array array = Marshal.from_channel channel in
629 Array.map (fun a -> Ptset.Int.from_list (Array.to_list a)) table
631 let load ?(sample=64) ?(load_text=true) str =
632 let fd = Unix.openfile str [ Unix.O_RDONLY ] 0o644 in
633 let in_c = Unix.in_channel_of_descr fd in
634 let index_prefix = Filename.chop_suffix str ".srx" in
635 let _ = set_binary_mode_in in_c true in
637 (let ms = input_line in_c in if ms <> magic_string then failwith "Invalid index file");
638 (let vs = input_line in_c in if vs <> version_string then failwith "Unsupported index format");
639 let c = load_tag_table in_c in
640 let s = load_tag_table in_c in
641 let d = load_tag_table in_c in
642 let f = load_tag_table in_c in
645 let c, s, d, f = time ~msg:"Loading tag table"(load_table) () in
646 ignore(Unix.lseek fd (pos_in in_c) Unix.SEEK_SET);
647 let xml_tree = tree_load fd index_prefix load_text sample in
648 let () = Tag.init (Obj.magic xml_tree) in
649 let attributes = Ptset.Int.add Tag.attribute d.(Tag.attribute) in
650 let elements = Ptset.Int.add Tag.document_node
651 (Ptset.Int.remove Tag.pcdata
652 (Ptset.Int.diff d.(Tag.document_node) attributes))
654 let tree = { doc = xml_tree;
655 attributes = attributes;
656 attribute_array = tag_list_of_set attributes;
669 let equal a b = a == b
673 | i -> Printf.sprintf "Node (%i)" i
675 let dump_node t = nts (Node.to_int t)
679 type query_result = { bv : bit_vector;
683 external tree_flush : tree -> Unix.file_descr -> unit = "caml_xml_tree_flush"
684 let flush t fd = tree_flush t.doc fd
686 external text_prefix : tree -> string -> bool -> query_result = "caml_text_collection_prefix_bv"
687 let text_prefix t s b = text_prefix t.doc s b
689 external text_suffix : tree -> string -> bool -> query_result = "caml_text_collection_suffix_bv"
690 let text_suffix t s b = text_suffix t.doc s b
692 external text_equals : tree -> string -> bool -> query_result = "caml_text_collection_equals_bv"
693 let text_equals t s b = text_equals t.doc s b
695 external text_contains : tree -> string -> bool -> query_result = "caml_text_collection_contains_bv"
696 let text_contains t s b = text_contains t.doc s b
699 module Predicate = Hcons.Make (
702 type t = (_t -> node -> bool) ref
703 let hash t = Hashtbl.hash t
704 let equal t1 t2 = t1 == t2
707 let string_of_query query =
709 | `Prefix -> "starts-with"
710 | `Suffix -> "ends-with"
711 | `Equals -> "equals"
712 | `Contains -> "contains"
715 let query_fun = function
716 | `Prefix -> text_prefix
717 | `Suffix -> text_suffix
718 | `Equals -> text_equals
719 | `Contains -> text_contains
722 let _pred_cache = Hashtbl.create 17
724 let mk_pred query s =
725 LOG ( __ "bottom-up" 3 "Calling mk_pred for '%s'\n" s);
726 let f = query_fun query in
727 let memo = ref (fun _ _ -> failwith "Undefined") in
728 memo := begin fun tree node ->
730 try Hashtbl.find _pred_cache (query,s) with
732 time ~count:1 ~msg:(Printf.sprintf "Computing text query %s(%s)"
733 (string_of_query query) s)
736 let bv = results.bv in
737 memo := begin fun _ n ->
738 let r = bit_vector_unsafe_get bv (Node.to_int n) in
739 LOG( __ "bottom-up" 3 "Running predicate on node %i = %b@\n" (Node.to_int n) r);
742 let r = bit_vector_unsafe_get bv (Node.to_int node) in
743 LOG( __ "bottom-up" 3 "Running predicate on node %i = %b@\n" (Node.to_int node) r);
749 let full_text_prefix t s = (text_prefix t s true).pos
751 let full_text_suffix t s = (text_suffix t s true).pos
753 let full_text_equals t s = (text_equals t s true).pos
755 let full_text_contains t s = (text_contains t s true).pos
757 let full_text_query q t s =
758 let res = (query_fun q) t s true in
759 Hashtbl.replace _pred_cache (q,s) res;
763 let h = Hashtbl.create 1024 in
765 let numleaves = ref 0 in
766 let numtexts = ref 0 in
767 let rec traverse tree t p d =
774 Hashtbl.replace h p (oldc + 1);
775 if d > !depth then depth := d;
778 let label = tree_tag tree t in
779 if label == Tag.pcdata || label == Tag.attribute_data then incr numtexts;
780 iter_siblings tree t (label::p) (d+1)
781 and iter_siblings tree t p d =
782 if is_nil t then () else
783 let fs = tree_first_child tree t in
784 traverse tree fs p d;
785 let ns = tree_next_sibling tree t in
786 iter_siblings tree ns p d
788 traverse tree.doc root [] 0;
789 let sumdepth = Hashtbl.fold (fun p c acc -> (List.length p) * c + acc) h 0 in
790 let alltags = Ptset.Int.union tree.elements tree.attributes in
791 Logger.print err_formatter "Statistics :@\n\
792 Average depth: %f@\n\
794 Number of distinct paths: %i@\n\
795 Number of nodes: %i@\n\
796 Number of leaves: %i@\n\
797 Number of pcdata/cdata nodes: %i@\n\
798 Number of distinct tags: %i@\n\
799 Largest tag id: %i@\n@?"
800 (float_of_int sumdepth /. float_of_int !numleaves)
803 (tree_subtree_size tree.doc root)
806 (Ptset.Int.cardinal alltags)
807 (Ptset.Int.max_elt alltags)
810 type tree_pointer = tree
811 let get_tree_pointer x = x.doc