X-Git-Url: http://git.nguyen.vg/gitweb/?a=blobdiff_plain;f=src%2Fnaive_tree.ml;h=a13a56dd21b1616283df446ad9fe383c39ba2728;hb=1947a98f419c2bf50378fed783406818b010c60e;hp=6625be0cfe22e08e84e054998b5171c31fc0a691;hpb=41dd1fed04cabad212f10fce3484545f6e9d9444;p=tatoo.git diff --git a/src/naive_tree.ml b/src/naive_tree.ml index 6625be0..a13a56d 100644 --- a/src/naive_tree.ml +++ b/src/naive_tree.ml @@ -50,6 +50,7 @@ let rec dummy = { type t = { root : node; size : int; + by_preorder : node array; (* TODO add other intersting stuff *) } @@ -71,7 +72,9 @@ struct "NODE " ^ string_of_int n.preorder) let debug_node fmt node = - Format.fprintf fmt "{ tag=%s; preorder=%i; data=%S; first_child=%a; next_sibling=%a; parent=%a }" + Format.fprintf fmt + "{ tag=%s; preorder=%i; data=%S;\ +first_child=%a; next_sibling=%a; parent=%a }" (QName.to_string node.tag) node.preorder node.data @@ -81,7 +84,8 @@ struct let debug_ctx fmt ctx = - Format.fprintf fmt "Current context: { preorder = %i\n; stack = \n%a\n }\n-------------\n" + Format.fprintf fmt "Current context: { preorder = %i\n; stack = \n%a\n }\ +\n-------------\n" ctx.current_preorder (Pretty.print_list ~sep:";\n" debug_node) ctx.stack @@ -207,8 +211,19 @@ struct Expat.final psr; let root = List.hd ctx.stack in root.next_sibling <- nil; + let a = Array.create ctx.current_preorder nil in + let rec loop n = + if n != nil then + begin + a.(n.preorder) <- n; + loop n.first_child; + loop n.next_sibling; + end + in + loop root; { root = root; - size = ctx.current_preorder + size = ctx.current_preorder; + by_preorder = a } ) @@ -312,10 +327,19 @@ let root t = t.root let size t = t.size let first_child _ n = n.first_child let next_sibling _ n = n.next_sibling + +let is_first_child _ n = n.parent.first_child == n +let is_next_sibling _ n = n.parent.next_sibling == n + +let prev_sibling t n = if is_next_sibling t n then n.parent else nil +let parent_of_first t n = if is_first_child t n then n.parent else nil + let parent _ n = n.parent let tag _ n = n.tag let data _ n = n.data let kind _ n = n.kind let preorder _ n = n.preorder - +let by_preorder t i = + if i >= 0 && i < t.size then Array.unsafe_get t.by_preorder i + else let e = Invalid_argument "by_preorder" in raise e let print_node fmt n = Parser.debug_node fmt n