Browse files

[fix] IntMap: update and update_default were no ops on new key

update now follows BaseIntMap spec and raise Not_found
update_default has been optimised to only fold the map once
  • Loading branch information...
OpaOnWindowsNow committed Jul 29, 2011
1 parent 834bf9f commit 95c5f506138a7b50ceacd1ae4a884d9dc38616d7
Showing with 9 additions and 8 deletions.
  1. +9 −8 libbase/
@@ -141,18 +141,19 @@ let rec remove k t = match t with
| Lf (k',_v) when k = k' -> Empty
| n -> n
-let rec update k f t = match t with
+(* on_miss specify what to do when the key is not found
+ take the part of intmap that should contains the key *)
+let rec update_gen k f t ~on_miss = match t with
| Br (p,m,tl,tr) ->
if zerobit k m
- then Br (p,m,update k f tl,tr)
- else Br (p,m,tl,update k f tr)
+ then Br (p,m,update_gen k f tl ~on_miss,tr)
+ else Br (p,m,tl,update_gen k f tr ~on_miss)
| Lf (k',v) when k=k' -> Lf (k,f v)
- | n -> n
+ | n -> on_miss n
-(* FIXME: this could be more efficient: once you fail to find the binding, you can add it
- * instead of raising Not_found and then adding it *)
-let update_default key f default map =
- try update key f map with Not_found -> add key default map
+let raise_Not_found _ = raise Not_found
+let update k f t = update_gen k f t ~on_miss:raise_Not_found
+let update_default k f default t = update_gen k f t ~on_miss:(fun m -> add k default m)
let rec mem k t = match t with
| Br (_,m,tl,tr) -> if zerobit k m then mem k tl else mem k tr

0 comments on commit 95c5f50

Please sign in to comment.