+ let tagged_desc t tag =
+ if tag = Tag.attribute || tag = Tag.pcdata then failwith "tagged_desc"
+ else match t with
+ | { doc=d; node=Node(NC n) } -> { t with node = norm (tagged_desc d n tag) }
+ | _ -> { t with node=Nil }
+
+*)
+let select_next tb tf t s =
+ match s.node with
+ | Node (below) -> begin
+ match t.node with
+ | Node( n) ->
+ { t with node = norm (tree_select_next t.doc n (Ptset.to_int_vector tb) (Ptset.to_int_vector tf) below) }
+ | Text (i,n) when equal_node nil n ->
+ let p = tree_parent_doc t.doc i in
+ { t with node = norm (tree_select_next t.doc p (Ptset.to_int_vector tb) (Ptset.to_int_vector tf) below) }
+ | Text(_,n) ->
+ if Ptset.mem (tree_tag_id t.doc n) (Ptset.union tb tf)
+ then { t with node=Node(n) }
+ else
+ let vb = Ptset.to_int_vector tb in
+ let vf = Ptset.to_int_vector tf in
+ let node =
+ let dsc = tree_select_below t.doc n vb vf in
+ if equal_node nil dsc
+ then tree_select_next t.doc n vb vf below
+ else dsc
+ in
+ { t with node = norm node }
+ | _ -> {t with node = Nil }
+ end
+
+ | _ -> { t with node = Nil }
+
+
+
+
+ let select_foll_only tf t s =
+ match s.node with
+ | Node (below) ->
+ begin
+ match t.node with
+ | Node(n) ->
+ { t with node= norm (tree_select_foll_only t.doc n (Ptset.to_int_vector tf) below) }
+ | Text(i,n) when equal_node nil n ->
+ let p = tree_parent_doc t.doc i in
+ { t with node= norm (tree_select_foll_only t.doc p (Ptset.to_int_vector tf) below) }
+ | Text(_,n) ->
+ if Ptset.mem (tree_tag_id t.doc n) tf
+ then { t with node=Node(n) }
+ else
+ let vf = Ptset.to_int_vector tf in
+ let node =
+ let dsc = tree_select_desc_only t.doc n vf in
+ if tree_is_nil dsc
+ then tree_select_foll_only t.doc n vf below
+ else dsc
+ in
+ { t with node = norm node }
+ | _ -> { t with node = Nil }
+ end
+ | _ -> {t with node=Nil }
+
+let select_below tc td t=
+ match t.node with
+ | Node( n) ->
+ let vc = Ptset.to_int_vector tc
+ in
+ let vd = Ptset.to_int_vector td
+ in
+ { t with node= norm(tree_select_below t.doc n vc vd) }
+ | _ -> { t with node=Nil }
+
+
+let select_desc_only td t =
+ match t.node with
+ | Node(n) ->
+ let vd = Ptset.to_int_vector td
+ in
+ { t with node = norm(tree_select_desc_only t.doc n vd) }
+ | _ -> { t with node = Nil }
+
+
+let tagged_desc tag t =
+ match t.node with
+ | Node(n) ->
+ { t with node = norm(tree_tagged_desc t.doc n tag) }
+ | _ -> { t with node = Nil }
+
+
+let tagged_foll_below tag t s =
+ match s.node with
+ | Node (below) ->
+ begin
+ match t.node with
+ | Node(n) ->
+ { t with node= norm (tree_tagged_foll_below t.doc n tag below) }
+ | Text(i,n) when equal_node nil n ->
+ let p = tree_prev_doc t.doc i in
+ { t with node= norm (tree_tagged_foll_below t.doc p tag below) }
+ | Text(_,n) ->
+ if (tree_tag_id t.doc n) == tag
+ then { t with node=Node(n) }
+ else
+ let node =
+ let dsc = tree_tagged_desc t.doc n tag in
+ if tree_is_nil dsc
+ then tree_tagged_foll_below t.doc n tag below
+ else dsc
+ in
+ { t with node = norm node }
+ | _ -> { t with node = Nil }
+ end
+ | _ -> {t with node=Nil }
+
+
+let last_idx = ref 0
+let array_find a i j =
+ let l = Array.length a in
+ let rec loop idx x y =
+ if x > y || idx >= l then nil
+ else
+ if a.(idx) >= x then if a.(idx) > y then nil else (last_idx := idx;a.(idx))
+ else loop (idx+1) x y
+ in
+ if a.(0) > j || a.(l-1) < i then nil
+ else loop !last_idx i j