From b79586e9f1bb3dacf84e86fa62aea28751892741 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Tue, 14 Apr 2015 23:11:24 +0600 Subject: Abandon the idea of default data in Vytree.insert and use explicit data list for multilevel insert. --- src/vytree.ml | 20 +++++++++++--------- src/vytree.mli | 3 ++- 2 files changed, 13 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/vytree.ml b/src/vytree.ml index 6eccc55..728e26d 100644 --- a/src/vytree.ml +++ b/src/vytree.ml @@ -11,6 +11,7 @@ type node_type = Leaf | Tag | Other exception Empty_path exception Duplicate_child exception Nonexistent_path +exception Insert_error of string let make data name = { name = name; data = data; children = [] } @@ -59,26 +60,27 @@ 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 default_data node path data = - match path with - | [] -> raise Empty_path - | [name] -> +let rec insert node path data_list = + match (path, data_list) with + | ([], _) -> raise Empty_path + | (_ :: _, []) -> raise (Insert_error "No data found") + | ([name], [datum]) -> begin (* Check for duplicate item *) let last_child = find node name in match last_child with - | None -> insert_immediate node name data + | None -> insert_immediate node name datum | (Some _) -> raise Duplicate_child end - | name :: names -> + | (name :: names, datum :: data) -> let next_child = find node name in match next_child with | Some next_child' -> - let new_node = insert default_data next_child' names data in + let new_node = insert next_child' names data in replace node new_node | None -> - let next_child' = make default_data name in - let new_node = insert default_data next_child' names data in + let next_child' = make datum name in + let new_node = insert next_child' names data in adopt node new_node let delete node path = diff --git a/src/vytree.mli b/src/vytree.mli index d268db4..cebbcde 100644 --- a/src/vytree.mli +++ b/src/vytree.mli @@ -3,6 +3,7 @@ type 'a t exception Empty_path exception Duplicate_child exception Nonexistent_path +exception Insert_error of string type position = Before of string | After of string | Default @@ -15,7 +16,7 @@ val name_of_node : 'a t -> string val data_of_node : 'a t -> 'a val children_of_node : 'a t -> 'a t list -val insert : 'a -> 'a t -> string list -> 'a -> 'a t +val insert : 'a t -> string list -> 'a list -> 'a t val delete : 'a t -> string list -> 'a t -- cgit v1.2.3