+let tree_is_last t n = equal_node nil (tree_next_sibling t n)
+
+external tree_prev_text : tree -> [`Tree] node -> [`Text ] node = "caml_xml_tree_prev_text"
+
+external tree_my_text : tree -> [`Tree] node -> [`Text ] node = "caml_xml_tree_my_text"
+external tree_next_text : tree -> [`Tree] node -> [`Text ] node = "caml_xml_tree_next_text"
+external tree_doc_ids : tree -> [`Tree ] node -> [`Text ] node * [`Text ] node = "caml_xml_tree_doc_ids"
+
+let text_size tree = int_of_node (snd ( tree_doc_ids tree (Obj.magic 0) ))
+
+let get_cached_text t x =
+ if x == -1 then ""
+ else
+ get_cached_text t x
+
+
+external tree_text_xml_id : tree -> [`Text ] node -> int = "caml_xml_tree_text_xml_id"
+external tree_node_xml_id : tree -> [`Tree ] node -> int = "caml_xml_tree_node_xml_id"
+external tree_is_ancestor : tree -> [`Tree ] node -> [`Tree ] node -> bool = "caml_xml_tree_is_ancestor"
+external tree_tagged_desc : tree -> [`Tree ] node -> Tag.t -> [`Tree ] node = "caml_xml_tree_tagged_desc"
+external tree_tagged_foll_below : tree -> [`Tree ] node -> Tag.t -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_tagged_foll_below"
+external tree_subtree_tags : tree -> [`Tree ] node -> Tag.t -> int = "caml_xml_tree_subtree_tags"
+(*
+external tree_select_below : tree -> [`Tree ] node -> Ptset.int_vector -> Ptset.int_vector -> [`Tree ] node = "caml_xml_tree_select_below"
+external tree_select_desc_only : tree -> [`Tree ] node -> Ptset.int_vector -> [`Tree ] node = "caml_xml_tree_select_desc_only"
+external tree_select_next : tree -> [`Tree ] node -> Ptset.int_vector -> Ptset.int_vector -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_select_next"
+external tree_select_foll_only : tree -> [`Tree ] node -> Ptset.int_vector -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_select_foll_only"
+external tree_select_desc_or_foll_only : tree -> [`Tree ] node -> Ptset.int_vector -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_select_foll_only" *)
+
+type descr =
+ | Nil
+ | Node of [`Tree] node
+ | Text of [`Text] node * [`Tree] node
+
+type t = { doc : tree;
+ node : descr;
+ ttable : (Tag.t,(Ptset.Int.t*Ptset.Int.t)) Hashtbl.t;
+ }
+
+let text_size t = text_size t.doc
+module MemUnion = Hashtbl.Make (struct
+ type t = Ptset.Int.t*Ptset.Int.t
+ let equal (x,y) (z,t) = (Ptset.Int.equal x z)&&(Ptset.Int.equal y t)
+ let equal a b = equal a b || equal b a
+ let hash (x,y) = (* commutative hash *)
+ let x = Ptset.Int.hash x
+ and y = Ptset.Int.hash y
+ in
+ if x < y then HASHINT2(x,y) else HASHINT2(y,x)
+ end)
+
+let collect_tags tree =
+ let h_union = MemUnion.create BIG_H_SIZE in
+ let pt_cup s1 s2 =
+ try
+ MemUnion.find h_union (s1,s2)
+ with
+ | Not_found -> let s = Ptset.Int.union s1 s2
+ in
+ MemUnion.add h_union (s1,s2) s;s
+ in
+ let h_add = Hashtbl.create BIG_H_SIZE in
+ let pt_add t s =
+ let k = HASHINT2(Tag.hash t,Ptset.Int.hash s) in
+ try
+ Hashtbl.find h_add k
+ with
+ | Not_found -> let r = Ptset.Int.add t s in
+ Hashtbl.add h_add k r;r
+ in
+ let h = Hashtbl.create BIG_H_SIZE in
+ let sing = Ptset.Int.singleton Tag.pcdata in
+ let update t sb sa =
+ let sbelow,safter =
+ try
+ Hashtbl.find h t
+ with
+ | Not_found ->
+ (sing,sing)
+ in
+ Hashtbl.replace h t (pt_cup sbelow sb, pt_cup safter sa)
+ in
+ let rec loop id acc =
+ if equal_node id nil
+ then (Ptset.Int.empty,acc)
+ else
+ let below2,after2 = loop (tree_next_sibling tree id) acc in
+ let below1,after1 = loop (tree_first_child tree id) after2 in
+ let tag = tree_tag_id tree id in
+ update tag below1 after2;
+ pt_add tag (pt_cup below1 below2), (pt_add tag after1)
+ in
+ let b,a = loop (tree_root tree) Ptset.Int.empty in
+ update Tag.pcdata b a;
+ h