*)
open Stdext
+module SymbolMap = Map.Make(Symbol)
+
module Node = struct
type t = {
name: Symbol.t;
perms: Perms.Node.t;
value: string;
- children: t list;
+ children: t SymbolMap.t;
}
let create _name _perms _value =
- { name = Symbol.of_string _name; perms = _perms; value = _value; children = []; }
+ { name = Symbol.of_string _name; perms = _perms; value = _value; children = SymbolMap.empty; }
let get_owner node = Perms.Node.get_owner node.perms
let get_children node = node.children
let set_perms node nperms = { node with perms = nperms }
let add_child node child =
- { node with children = child :: node.children }
+ let children = SymbolMap.add child.name child node.children in
+ { node with children }
let exists node childname =
let childname = Symbol.of_string childname in
- List.exists (fun n -> Symbol.equal n.name childname) node.children
+ SymbolMap.mem childname node.children
let find node childname =
let childname = Symbol.of_string childname in
- List.find (fun n -> Symbol.equal n.name childname) node.children
+ SymbolMap.find childname node.children
let replace_child node child nchild =
- (* this is the on-steroid version of the filter one-replace one *)
- let rec replace_one_in_list l =
- match l with
- | [] -> []
- | h :: tl when Symbol.equal h.name child.name -> nchild :: tl
- | h :: tl -> h :: replace_one_in_list tl
- in
- { node with children = (replace_one_in_list node.children) }
+ { node with
+ children = SymbolMap.update child.name
+ (function None -> None | Some _ -> Some nchild)
+ node.children
+ }
let del_childname node childname =
let sym = Symbol.of_string childname in
- let rec delete_one_in_list l =
- match l with
- | [] -> raise Not_found
- | h :: tl when Symbol.equal h.name sym -> tl
- | h :: tl -> h :: delete_one_in_list tl
- in
- { node with children = (delete_one_in_list node.children) }
+ { node with children =
+ SymbolMap.update sym
+ (function None -> raise Not_found | Some _ -> None)
+ node.children
+ }
let del_all_children node =
- { node with children = [] }
+ { node with children = SymbolMap.empty }
(* check if the current node can be accessed by the current connection with rperm permissions *)
let check_perm node connection request =
raise Define.Permission_denied;
end
-let rec recurse fct node = fct node; List.iter (recurse fct) node.children
+let rec recurse fct node = fct node; SymbolMap.iter (fun _ -> recurse fct) node.children
(** [recurse_map f tree] applies [f] on each node in the tree recursively *)
let recurse_map f =
let rec walk node =
- f { node with children = List.rev_map walk node.children |> List.rev }
+ f { node with children = SymbolMap.map walk node.children }
in
walk
Node.check_perm cnode perm Perms.READ;
cnode.Node.children in
Path.apply store.root path do_ls in
- List.rev (List.map (fun n -> Symbol.to_string n.Node.name) children)
+ SymbolMap.fold (fun k _ accu -> Symbol.to_string k :: accu) children []
let getperms store perm path =
if path = [] then (
let rec _traversal path node =
f path node;
let node_path = Path.of_path_and_name path (Symbol.to_string node.Node.name) in
- List.iter (_traversal node_path) (List.rev node.Node.children)
+ SymbolMap.iter (fun _ -> _traversal node_path) node.Node.children
in
_traversal [] root_node