From 74132f616269e0de282354dec602d494455da4dc Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Wed, 3 Jun 2015 13:51:25 +0600 Subject: Make tree insert position-aware. Make insertion at the beginning default behaviour. Implement insertion at the end. --- src/vytree.ml | 16 ++++++++++------ src/vytree.mli | 4 ++-- 2 files changed, 12 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/vytree.ml b/src/vytree.ml index ed5efd2..93d3d71 100644 --- a/src/vytree.ml +++ b/src/vytree.ml @@ -4,7 +4,7 @@ type 'a t = { children: 'a t list } -type position = Before of string | After of string | Default +type position = Before of string | After of string | End | Default type node_type = Leaf | Tag | Other @@ -21,10 +21,14 @@ let name_of_node node = node.name let data_of_node node = node.data let children_of_node node = node.children -let insert_immediate node name data = +let insert_immediate ?(position=Default) node name data = let new_node = make data name in - let children' = new_node :: node.children in - { node with children = children' } + let children' = + match position with + | Default -> new_node :: node.children + | End -> node.children @ [new_node] + | _ -> assert false + in { node with children = children' } let delete_immediate node name = let children' = Vylist.remove (fun x -> x.name = name) node.children in @@ -60,13 +64,13 @@ let rec do_with_child fn node path = let new_node = do_with_child fn next_child names in replace node new_node -let rec insert node path data = +let rec insert ?(position=Default) node path data = match path with | [] -> raise Empty_path | [name] -> (let last_child = find node name in match last_child with - | None -> insert_immediate node name data + | None -> insert_immediate ~position:position node name data | (Some _) -> raise Duplicate_child) | name :: names -> let next_child = find node name in diff --git a/src/vytree.mli b/src/vytree.mli index 4b52727..e912922 100644 --- a/src/vytree.mli +++ b/src/vytree.mli @@ -5,7 +5,7 @@ exception Duplicate_child exception Nonexistent_path exception Insert_error of string -type position = Before of string | After of string | Default +type position = Before of string | After of string | End | Default type node_type = Leaf | Tag | Other @@ -19,7 +19,7 @@ val children_of_node : 'a t -> 'a t list val find : 'a t -> string -> 'a t option val find_or_fail : 'a t -> string -> 'a t -val insert : 'a t -> string list -> 'a -> 'a t +val insert : ?position:position -> 'a t -> string list -> 'a -> 'a t val delete : 'a t -> string list -> 'a t -- cgit v1.2.3