+
+let stats tree =
+ let h = Hashtbl.create 1024 in
+ let depth = ref 0 in
+ let numleaves = ref 0 in
+ let numtexts = ref 0 in
+ let rec traverse tree t p d =
+ if is_nil t then
+ let oldc =
+ try
+ Hashtbl.find h p
+ with Not_found -> 0
+ in
+ Hashtbl.replace h p (oldc + 1);
+ if d > !depth then depth := d;
+ incr numleaves
+ else
+ let label = tree_tag tree t in
+ if label == Tag.pcdata || label == Tag.attribute_data then incr numtexts;
+ iter_siblings tree t (label::p) (d+1)
+ and iter_siblings tree t p d =
+ if is_nil t then () else
+ let fs = tree_first_child tree t in
+ traverse tree fs p d;
+ let ns = tree_next_sibling tree t in
+ iter_siblings tree ns p d
+ in
+ traverse tree.doc root [] 0;
+ let sumdepth = Hashtbl.fold (fun p c acc -> (List.length p) * c + acc) h 0 in
+ let alltags = Ptset.Int.union tree.elements tree.attributes in
+ Logger.print err_formatter "Statistics :@\n\
+Average depth: %f@\n\
+Longest path: %i@\n\
+Number of distinct paths: %i@\n\
+Number of nodes: %i@\n\
+Number of leaves: %i@\n\
+Number of pcdata/cdata nodes: %i@\n\
+Number of distinct tags: %i@\n\
+Largest tag id: %i@\n@?"
+ (float_of_int sumdepth /. float_of_int !numleaves)
+ !depth
+ (Hashtbl.length h)
+ (tree_subtree_size tree.doc root)
+ !numleaves
+ !numtexts
+ (Ptset.Int.cardinal alltags)
+ (Ptset.Int.max_elt alltags)
+
+(*
+ Logger.print err_formatter "Average depth: %f, number of leaves %i@\n@?" ((float_of_int a)/. (float_of_int b)) b
+;;
+
+*)
+
+type tree_pointer = tree
+let get_tree_pointer x = x.doc