Fix Not_found bug (remove redeclaration of the exception)

This commit is contained in:
Suzanne Dupéron 2020-04-22 19:47:43 +01:00
parent 0580f21896
commit 8e9ee21cd4
6 changed files with 6 additions and 17 deletions

View File

@ -23,19 +23,17 @@ let remove key map =
let cmp k1 (k2,_) = map.cmp k1 k2 in
{map with tree = RB.remove ~cmp key map.tree}
exception Not_found
let find key map =
let cmp k1 (k2,_) = map.cmp k1 k2 in
try snd (RB.find ~cmp key map.tree) with
RB.Not_found -> raise Not_found
Not_found -> raise Not_found
let find_opt key map =
try Some (find key map) with Not_found -> None
let update key updater map =
match updater (find_opt key map) with
| None -> failwith "TODO: RedBlackTrees: remove not implemented" (* TODO: remove key *)
| None -> remove key map
| Some v -> add key v map
let bindings map =

View File

@ -42,8 +42,6 @@ val remove : 'key -> ('key, 'value) t -> ('key, 'value) t
[key] in the map [map]. If [key] is not bound in [map], the
exception [Not_found] is raised. *)
exception Not_found
val find : 'key -> ('key, 'value) t -> 'value
(* The value of the call [find_opt key map] is [Some value] if the key

View File

@ -17,11 +17,9 @@ let is_empty set = RB.is_empty set.tree
let add elt set = {set with tree = RB.add ~cmp:set.cmp RB.New elt set.tree}
exception Not_found
let find elt set =
try RB.find ~cmp:set.cmp elt set.tree with
RB.Not_found -> raise Not_found
Not_found -> raise Not_found
let find_opt elt set = RB.find_opt ~cmp:set.cmp elt set.tree

View File

@ -38,8 +38,6 @@ val add : 'elt -> 'elt t -> 'elt t
function of [set] (see [create]). If [elt] is not in [set], then
the exception [Not_found] is raised. *)
exception Not_found
val find : 'elt -> 'elt t -> 'elt
(* The call [find_opt elt set] is similar to [find elt set], except

View File

@ -67,16 +67,15 @@ let remove : type a b . cmp:(a -> b -> int) -> a -> b t -> b t = fun ~cmp elt tr
Int (colour, new_left, lroot, right)
) in
let rec bst_delete : a -> b t -> b t = fun elt -> function
| Ext -> failwith "remove in red-black tree: element not found"
| Ext -> raise Not_found
| Int (colour, left, root, right) as current ->
let c = cmp elt root in
if c = 0 then bst_shift_up current
else if c < 0 then Int (colour, bst_delete elt left, root, right)
else Int (colour, left, root, bst_delete elt right)
in
bst_delete elt tree
exception Not_found
try bst_delete elt tree
with Not_found -> tree
let rec find ~cmp elt = function
Ext -> raise Not_found

View File

@ -39,8 +39,6 @@ val remove: cmp:('a -> 'b -> int) -> 'a -> 'b t -> 'b t
to a node of the tree [t], such that [cmp x y = true]. If none, the
exception [Not_found] is raised. *)
exception Not_found
val find: cmp:('a -> 'b -> int) -> 'a -> 'b t -> 'b
(* The value of call [find_opt ~cmp x t] is [Some y] if there is an