Commit b629c379 authored by Taddeüs Kroes's avatar Taddeüs Kroes

funclang series4: Added some comments and fixed existing key issue.

parent 18b75311
......@@ -15,17 +15,17 @@ let rec insert trie key value = match trie with
Empty -> Node (key, value, []) (* Root *)
| Node (k, v, c) ->
if k = key then
raise (Failure "Inserted key already exists in trie")
else if not (starts_with key k) then
raise (Failure "Inserted key does not start with node key")
else
(* Key already exists, replace its value *)
Node (k, value, c)
else if starts_with key k then
(* Key prefix matches, trye to insert in children *)
let match_node node = match node with
Empty -> false
| Node (k, v, c) -> starts_with key k
in
let matches = map match_node c in
if fold_left (||) false matches then
(* Match in child node, do not add new child *)
(* Match in child node, insert in matching child *)
let apply_match (matched, node) =
if matched then insert node key value else node
in
......@@ -33,6 +33,9 @@ let rec insert trie key value = match trie with
else
(* No match in any child node, add new child *)
Node (k, v, c @ [Node (key, value, [])])
else
(* Key prefix does not match, key cannot be inserted *)
raise (Failure "Inserted key does not start with node key")
(* Helper function, checks if a trie is not empty *)
let non_empty = function | Empty -> false | _ -> true
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment