+
+let tagged_lowest t tag =
+ let rec loop_lowest i =
+ let j = tree_tagged_desc t.doc i tag in
+ if tree_is_nil j then i else loop_lowest j
+ in
+ match t.node with
+ | Node i ->
+ let j = loop_lowest i in
+ { t with
+ node = norm(
+ if tree_is_nil j then
+ if (tree_tag_id t.doc i) == tag
+ then i
+ else j
+ else j) }
+ | Nil -> t
+ | _ -> assert false
+
+
+let tagged_next t tag =
+ match t.node with
+ | Node(i) ->
+ let n = tree_tagged_foll_below t.doc i tag (Obj.magic 0)
+ in
+ if tree_is_nil n then mk_nil t
+ else
+ tagged_lowest { t with node = Node n } tag
+ | Nil -> t
+ | _ -> assert false
+
+let rec binary_parent t =
+ let res =
+ match t.node with
+ | Node(0) -> { t with node = Nil }
+ | Node(i) ->
+ let j = tree_prev_sibling t.doc i in
+ if tree_is_nil j then
+ let idoc = tree_prev_text t.doc i in
+ if equal_node nil idoc then
+ { t with node = Node (tree_parent t.doc i) }
+ else
+ { t with node = Text(idoc,i) }
+ else
+ let idoc = tree_prev_text t.doc i in
+ if equal_node nil idoc then
+ { t with node = Node (j) }
+ else { t with node = Text(idoc,i) }
+ | Text(d,i) ->
+ if tree_is_nil i then
+ let n = tree_parent_doc t.doc d in
+ let lc = tree_last_child t.doc n in
+ if tree_is_nil lc then {t with node = Node n }
+ else { t with node = Node lc }
+ else
+ let j = tree_prev_sibling t.doc i in
+ if tree_is_nil j then
+ { t with node = Node (tree_parent t.doc i) }
+ else { t with node = Node j }
+ | Nil -> t
+ in match res.node with
+ | Text(idoc,t) ->
+ if (Array.length !contains_array) != 0
+ then if in_array !contains_array idoc then res
+ else binary_parent res
+ else res
+ | _ -> res
+
+let benchmark_text t =
+ let doc = t.doc in
+ match (root t).node with
+ | Node i -> let _,size = tree_doc_ids doc i in
+ Printf.eprintf "%i will take ~ %i seconds\n%!"
+ size (size/10000) ;
+ let a = Array.create size "" in
+ for i = 0 to size
+ do
+ a.(i) <- text_get_tc_text t.doc (i+1)
+ done; a
+ | _ -> assert false
+
+let doc_ids (t:t) : (int*int) =
+ (Obj.magic (
+ match t.node with
+ | Node i -> tree_doc_ids t.doc i
+ | Text (i,_) -> (i,i)
+ | Nil -> (nil,nil)
+ ))
+
+let subtree_tags t tag = match t.node with
+ | Nil -> 0
+ | Node(i) -> tree_subtree_tags t.doc i tag
+ | Text(_,i) -> tree_subtree_tags t.doc i tag
+
+let get_text t = match t.node with
+ | Text(i,_) -> get_cached_text t.doc i
+ | _ -> ""