diff options
author | Daniil Baturin <daniil@baturin.org> | 2015-03-02 06:31:15 +0600 |
---|---|---|
committer | Daniil Baturin <daniil@baturin.org> | 2015-03-02 06:31:15 +0600 |
commit | 56708c95bb3d0ac089b0635b6110da1d4cf4e586 (patch) | |
tree | b13cf50bd70aaee41acbb91059c888e8b3280434 | |
parent | 16e35790560758d4e6e999a6b7a287c1ef40af02 (diff) | |
download | vyconf-56708c95bb3d0ac089b0635b6110da1d4cf4e586.tar.gz vyconf-56708c95bb3d0ac089b0635b6110da1d4cf4e586.zip |
Remove "child" from insert/delete etc. function names.
It's not like there's anything else we can insert or delete.
-rw-r--r-- | src/vytree.ml | 44 | ||||
-rw-r--r-- | src/vytree.mli | 7 | ||||
-rw-r--r-- | test/vyconf_tree_test.ml | 38 |
3 files changed, 44 insertions, 45 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 diff --git a/test/vyconf_tree_test.ml b/test/vyconf_tree_test.ml index 2bf40be..bf9f982 100644 --- a/test/vyconf_tree_test.ml +++ b/test/vyconf_tree_test.ml @@ -17,7 +17,7 @@ let test_make_node test_ctxt = children list *) let test_insert_immediate_child test_ctxt = let node = make "root" () in - let node' = insert_child () node ["foo"] () in + let node' = insert () node ["foo"] () in assert_equal (children_of_node node') [make "foo" ()] @@ -25,8 +25,8 @@ let test_insert_immediate_child test_ctxt = end of the children list *) let test_insert_multiple_children test_ctxt = let node = make "root" () in - let node' = insert_child () node ["foo"] () in - let node'' = insert_child () node' ["bar"] () in + let node' = insert () node ["foo"] () in + let node'' = insert () node' ["bar"] () in assert_equal (children_of_node node'') [make "foo" (); make "bar" ()] @@ -34,7 +34,7 @@ let test_insert_multiple_children test_ctxt = two levels deep *) let test_insert_multi_level test_ctxt = let node = make "root" () in - let node' = insert_child () node ["foo"; "bar"] () in + let node' = insert () node ["foo"; "bar"] () in let bar = make "bar" () in let foo = make_full "foo" () [bar] in let root = make_full "root" () [foo] in @@ -43,52 +43,52 @@ let test_insert_multi_level test_ctxt = (* Inserting duplicate child fails *) let test_insert_duplicate_child test_ctxt = let node = make "root" () in - let node' = insert_child () node ["foo"] () in - assert_raises Duplicate_child (fun () -> insert_child () node' ["foo"] ()) + let node' = insert () node ["foo"] () in + assert_raises Duplicate_child (fun () -> insert () node' ["foo"] ()) (* list_children correctly returns a list of children names *) let test_list_children test_ctxt = let node = make "root" () in - let node' = insert_child () node ["foo"] () in - let node'' = insert_child () node' ["bar"] () in + let node' = insert () node ["foo"] () in + let node'' = insert () node' ["bar"] () in assert_equal (list_children node'') ["foo"; "bar"] (* Deleting a child, well, deletes it *) let test_delete_immediate_child test_ctxt = let node = make "root" () in - let node' = insert_child () node ["foo"] () in - let node'' = delete_child node' ["foo"] in + let node' = insert () node ["foo"] () in + let node'' = delete node' ["foo"] in assert_equal node node'' (* Deleting a child at multi-level path works *) let test_delete_multi_level test_ctxt = let node = make "root" () in - let node' = insert_child () node ["foo"; "bar"] () in - let foo_node = insert_child () node ["foo"] () in - let node'' = delete_child node' ["foo"; "bar"] in + let node' = insert () node ["foo"; "bar"] () in + let foo_node = insert () node ["foo"] () in + let node'' = delete node' ["foo"; "bar"] in assert_equal node'' foo_node (* Attempt to delete a node at non-existent path raises an exception *) let test_delete_nonexistent test_ctxt = let node = make "root" () in - assert_raises Nonexistent_path (fun () -> delete_child node ["foo"; "bar"]) + assert_raises Nonexistent_path (fun () -> delete node ["foo"; "bar"]) (* get_child works with immediate children *) let test_get_immediate_child test_ctxt = let node = make "root" () in - let node' = insert_child () node ["foo"] () in - assert_equal (name_of_node (get_child node' ["foo"])) "foo" + let node' = insert () node ["foo"] () in + assert_equal (name_of_node (get node' ["foo"])) "foo" (* get_child works with multi-level paths *) let test_get_child_multilevel test_ctxt = let node = make "root" () in - let node' = insert_child () node ["foo"; "bar"] () in - assert_equal (name_of_node (get_child node' ["foo"; "bar"])) "bar" + let node' = insert () node ["foo"; "bar"] () in + assert_equal (name_of_node (get node' ["foo"; "bar"])) "bar" (* get_child raises Nonexistent_path for non-existent paths *) let test_get_child_nonexistent test_ctxt = let node = make "root" () in - assert_raises Nonexistent_path (fun () -> get_child node ["foo"; "bar"]) + assert_raises Nonexistent_path (fun () -> get node ["foo"; "bar"]) let suite = "VyConf tree tests" >::: [ |