diff options
author | Daniil Baturin <daniil@baturin.org> | 2015-04-23 05:29:31 +0600 |
---|---|---|
committer | Daniil Baturin <daniil@baturin.org> | 2015-04-23 05:29:31 +0600 |
commit | ccab6d041c7ca307486bb344dd2a4e669e9bb9fe (patch) | |
tree | 2c2409dec310ef02af2847badc891d1acc1a30bb /src | |
parent | 3b2b0d009ba7962da8b12c62f119fa4b1178e858 (diff) | |
download | vyconf-ccab6d041c7ca307486bb344dd2a4e669e9bb9fe.tar.gz vyconf-ccab6d041c7ca307486bb344dd2a4e669e9bb9fe.zip |
Make insert require all but the last elements in the path to exist in the tree.
Making it linear time at cost of knowing the data for all path elements
was probably a bad idea.
Diffstat (limited to 'src')
-rw-r--r-- | src/vytree.ml | 26 | ||||
-rw-r--r-- | src/vytree.mli | 2 |
2 files changed, 11 insertions, 17 deletions
diff --git a/src/vytree.ml b/src/vytree.ml index 728e26d..10ccf0e 100644 --- a/src/vytree.ml +++ b/src/vytree.ml @@ -60,28 +60,22 @@ 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_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 datum - | (Some _) -> raise Duplicate_child - end - | (name :: names, datum :: data) -> +let rec insert 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 + | (Some _) -> raise Duplicate_child) + | name :: names -> let next_child = find node name in match next_child with | Some next_child' -> let new_node = insert next_child' names data in replace node new_node | None -> - let next_child' = make datum name in - let new_node = insert next_child' names data in - adopt node new_node + raise (Insert_error "Path does not exist") let delete node path = do_with_child delete_immediate node path diff --git a/src/vytree.mli b/src/vytree.mli index cebbcde..8224fdd 100644 --- a/src/vytree.mli +++ b/src/vytree.mli @@ -16,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 t -> string list -> 'a list -> 'a t +val insert : 'a t -> string list -> 'a -> 'a t val delete : 'a t -> string list -> 'a t |