Commit e052f91d authored by Taddeus Kroes's avatar Taddeus Kroes

Merge branch 'master' of ssh://vo20.nl/git/uva

parents 9aedb141 b629c379
...@@ -15,17 +15,17 @@ let rec insert trie key value = match trie with ...@@ -15,17 +15,17 @@ let rec insert trie key value = match trie with
Empty -> Node (key, value, []) (* Root *) Empty -> Node (key, value, []) (* Root *)
| Node (k, v, c) -> | Node (k, v, c) ->
if k = key then if k = key then
raise (Failure "Inserted key already exists in trie") (* Key already exists, replace its value *)
else if not (starts_with key k) then Node (k, value, c)
raise (Failure "Inserted key does not start with node key") else if starts_with key k then
else (* Key prefix matches, trye to insert in children *)
let match_node node = match node with let match_node node = match node with
Empty -> false Empty -> false
| Node (k, v, c) -> starts_with key k | Node (k, v, c) -> starts_with key k
in in
let matches = map match_node c in let matches = map match_node c in
if fold_left (||) false matches then 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) = let apply_match (matched, node) =
if matched then insert node key value else node if matched then insert node key value else node
in in
...@@ -33,6 +33,9 @@ let rec insert trie key value = match trie with ...@@ -33,6 +33,9 @@ let rec insert trie key value = match trie with
else else
(* No match in any child node, add new child *) (* No match in any child node, add new child *)
Node (k, v, c @ [Node (key, value, [])]) 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 *) (* Helper function, checks if a trie is not empty *)
let non_empty = function | Empty -> false | _ -> true 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