From f201377ae801382856a44f769d396980a84184b8 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Sun, 1 Mar 2015 00:52:11 +0600 Subject: Move the source files back to src/ Splitting directories was a bit premature and unnecessary. --- _oasis | 7 +--- src/vylist.ml | 17 +++++++++ src/vylist.mli | 3 ++ src/vylist/vylist.ml | 17 --------- src/vylist/vylist.mli | 3 -- src/vytree.ml | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/vytree.mli | 19 ++++++++++ src/vytree/vytree.ml | 98 --------------------------------------------------- src/vytree/vytree.mli | 19 ---------- 9 files changed, 138 insertions(+), 143 deletions(-) create mode 100644 src/vylist.ml create mode 100644 src/vylist.mli delete mode 100644 src/vylist/vylist.ml delete mode 100644 src/vylist/vylist.mli create mode 100644 src/vytree.ml create mode 100644 src/vytree.mli delete mode 100644 src/vytree/vytree.ml delete mode 100644 src/vytree/vytree.mli diff --git a/_oasis b/_oasis index 8ef39b1..17612e7 100644 --- a/_oasis +++ b/_oasis @@ -11,14 +11,9 @@ OASISFormat: 0.4 BuildTools: ocamlbuild Plugins: META (0.4), DevFiles (0.4) -Library "vylist" - Path: src/vylist - Modules: Vylist - Library "vytree" - Path: src/vytree + Path: src Modules: Vytree - BuildDepends: vylist Executable "vyconfd" Path: src diff --git a/src/vylist.ml b/src/vylist.ml new file mode 100644 index 0000000..c7d0396 --- /dev/null +++ b/src/vylist.ml @@ -0,0 +1,17 @@ +let rec find p xs = + match xs with + | [] -> None + | x :: xs' -> if (p x) then (Some x) + else find p xs' + +let rec remove p xs = + match xs with + | [] -> [] + | x :: xs' -> if (p x) then xs' + else x :: (remove p xs) + +let rec replace p x xs= + match xs with + | [] -> [] + | x' :: xs' -> if (p x') then x :: xs' + else x' :: (replace p x xs') diff --git a/src/vylist.mli b/src/vylist.mli new file mode 100644 index 0000000..266ec95 --- /dev/null +++ b/src/vylist.mli @@ -0,0 +1,3 @@ +val find : ('a -> bool) -> 'a list -> 'a option +val remove : ('a -> bool) -> 'a list -> 'a list +val replace : ('a -> bool) -> 'a -> 'a list -> 'a list diff --git a/src/vylist/vylist.ml b/src/vylist/vylist.ml deleted file mode 100644 index c7d0396..0000000 --- a/src/vylist/vylist.ml +++ /dev/null @@ -1,17 +0,0 @@ -let rec find p xs = - match xs with - | [] -> None - | x :: xs' -> if (p x) then (Some x) - else find p xs' - -let rec remove p xs = - match xs with - | [] -> [] - | x :: xs' -> if (p x) then xs' - else x :: (remove p xs) - -let rec replace p x xs= - match xs with - | [] -> [] - | x' :: xs' -> if (p x') then x :: xs' - else x' :: (replace p x xs') diff --git a/src/vylist/vylist.mli b/src/vylist/vylist.mli deleted file mode 100644 index 266ec95..0000000 --- a/src/vylist/vylist.mli +++ /dev/null @@ -1,3 +0,0 @@ -val find : ('a -> bool) -> 'a list -> 'a option -val remove : ('a -> bool) -> 'a list -> 'a list -val replace : ('a -> bool) -> 'a -> 'a list -> 'a list diff --git a/src/vytree.ml b/src/vytree.ml new file mode 100644 index 0000000..7ee3e65 --- /dev/null +++ b/src/vytree.ml @@ -0,0 +1,98 @@ +type 'a vyconf_tree = { + name: string; + data: 'a; + children: 'a vyconf_tree list +} + +type 'a t = 'a vyconf_tree + +exception Empty_path +exception Duplicate_child +exception Nonexistent_path + +let make name data = { name = name; data = data; children = [] } + +let make_full name data children = { name = name; data = data; children = children } + +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 new_node = make name data in + let children' = node.children @ [new_node] in + { node with children = children' } + +let delete_immediate_child node name = + let children' = Vylist.remove (fun x -> x.name = name) node.children in + { node with children = children' } + +let adopt_child node child = + { node with children = (node.children @ [child]) } + +let replace_child 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 = + Vylist.find (fun x -> x.name = name) node.children + +let rec extract_names children = + List.map (fun x -> x.name) children + +let list_children node = + extract_names node.children + +let rec insert_child 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 + match last_child with + | None -> insert_immediate_child node name data + | (Some _) -> raise Duplicate_child + end + | name :: names -> + let next_child = find_child 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 + | 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 rec delete_child node path = + match path with + | [] -> raise Empty_path + | [name] -> delete_immediate_child node name + | name :: names -> + let next_child = find_child 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 rec get_child node path = + match path with + | [] -> raise Empty_path + | [name] -> + begin + let child = find_child 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 + match next_child with + | None -> raise Nonexistent_path + | Some child' -> get_child child' names + end diff --git a/src/vytree.mli b/src/vytree.mli new file mode 100644 index 0000000..1b42de7 --- /dev/null +++ b/src/vytree.mli @@ -0,0 +1,19 @@ +type 'a t + +exception Empty_path +exception Duplicate_child +exception Nonexistent_path + +val make : string -> 'a -> 'a t +val make_full : string -> 'a -> ('a t) list -> 'a t + +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 delete_child : 'a t -> string list -> 'a t + +val list_children : 'a t -> string list diff --git a/src/vytree/vytree.ml b/src/vytree/vytree.ml deleted file mode 100644 index 7ee3e65..0000000 --- a/src/vytree/vytree.ml +++ /dev/null @@ -1,98 +0,0 @@ -type 'a vyconf_tree = { - name: string; - data: 'a; - children: 'a vyconf_tree list -} - -type 'a t = 'a vyconf_tree - -exception Empty_path -exception Duplicate_child -exception Nonexistent_path - -let make name data = { name = name; data = data; children = [] } - -let make_full name data children = { name = name; data = data; children = children } - -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 new_node = make name data in - let children' = node.children @ [new_node] in - { node with children = children' } - -let delete_immediate_child node name = - let children' = Vylist.remove (fun x -> x.name = name) node.children in - { node with children = children' } - -let adopt_child node child = - { node with children = (node.children @ [child]) } - -let replace_child 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 = - Vylist.find (fun x -> x.name = name) node.children - -let rec extract_names children = - List.map (fun x -> x.name) children - -let list_children node = - extract_names node.children - -let rec insert_child 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 - match last_child with - | None -> insert_immediate_child node name data - | (Some _) -> raise Duplicate_child - end - | name :: names -> - let next_child = find_child 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 - | 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 rec delete_child node path = - match path with - | [] -> raise Empty_path - | [name] -> delete_immediate_child node name - | name :: names -> - let next_child = find_child 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 rec get_child node path = - match path with - | [] -> raise Empty_path - | [name] -> - begin - let child = find_child 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 - match next_child with - | None -> raise Nonexistent_path - | Some child' -> get_child child' names - end diff --git a/src/vytree/vytree.mli b/src/vytree/vytree.mli deleted file mode 100644 index 1b42de7..0000000 --- a/src/vytree/vytree.mli +++ /dev/null @@ -1,19 +0,0 @@ -type 'a t - -exception Empty_path -exception Duplicate_child -exception Nonexistent_path - -val make : string -> 'a -> 'a t -val make_full : string -> 'a -> ('a t) list -> 'a t - -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 delete_child : 'a t -> string list -> 'a t - -val list_children : 'a t -> string list -- cgit v1.2.3