diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/vytree.ml | 44 | ||||
-rw-r--r-- | src/vytree.mli | 7 |
2 files changed, 25 insertions, 26 deletions
diff --git a/src/vytree.ml b/src/vytree.ml index 7ee3e65..689cf01 100644 --- a/src/vytree.ml +++ b/src/vytree.ml @@ -18,25 +18,25 @@ let name_of_node node = node.name let data_of_node node = node.data let children_of_node node = node.children -let insert_immediate_child node name data = +let insert_immediate node name data = let new_node = make name data in let children' = node.children @ [new_node] in { node with children = children' } -let delete_immediate_child node name = +let delete_immediate node name = let children' = Vylist.remove (fun x -> x.name = name) node.children in { node with children = children' } -let adopt_child node child = +let adopt node child = { node with children = (node.children @ [child]) } -let replace_child node child = +let replace node child = let children = node.children in let name = child.name in let children' = Vylist.replace (fun x -> x.name = name) child children in { node with children = children' } -let find_child node name = +let find node name = Vylist.find (fun x -> x.name = name) node.children let rec extract_names children = @@ -45,54 +45,54 @@ let rec extract_names children = let list_children node = extract_names node.children -let rec insert_child default_data node path data = +let rec insert default_data node path data = match path with | [] -> raise Empty_path | [name] -> begin (* Check for duplicate item *) - let last_child = find_child node name in + let last_child = find node name in match last_child with - | None -> insert_immediate_child node name data + | None -> insert_immediate node name data | (Some _) -> raise Duplicate_child end | name :: names -> - let next_child = find_child node name in + let next_child = find node name in match next_child with | Some next_child' -> - let new_node = insert_child default_data next_child' names data in - replace_child node new_node + let new_node = insert default_data next_child' names data in + replace node new_node | None -> let next_child' = make name default_data in - let new_node = insert_child default_data next_child' names data in - adopt_child node new_node + let new_node = insert default_data next_child' names data in + adopt node new_node -let rec delete_child node path = +let rec delete node path = match path with | [] -> raise Empty_path - | [name] -> delete_immediate_child node name + | [name] -> delete_immediate node name | name :: names -> - let next_child = find_child node name in + let next_child = find node name in match next_child with | None -> raise Nonexistent_path | Some next_child' -> - let new_node = delete_child next_child' names in - replace_child node new_node + let new_node = delete next_child' names in + replace node new_node -let rec get_child node path = +let rec get node path = match path with | [] -> raise Empty_path | [name] -> begin - let child = find_child node name in + let child = find node name in match child with | None -> raise Nonexistent_path | Some child' -> child' end | name :: names -> begin - let next_child = find_child node name in + let next_child = find node name in match next_child with | None -> raise Nonexistent_path - | Some child' -> get_child child' names + | Some child' -> get child' names end diff --git a/src/vytree.mli b/src/vytree.mli index 5fe4d37..9b91c80 100644 --- a/src/vytree.mli +++ b/src/vytree.mli @@ -11,11 +11,10 @@ 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_child : - 'a -> 'a t -> string list -> 'a -> 'a t +val insert : 'a -> 'a t -> string list -> 'a -> 'a t -val delete_child : 'a t -> string list -> 'a t +val delete : 'a t -> string list -> 'a t val list_children : 'a t -> string list -val get_child : 'a t -> string list -> 'a t +val get : 'a t -> string list -> 'a t |