aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGaëtan Gilbert2019-02-22 14:45:10 +0100
committerGaëtan Gilbert2019-02-22 14:45:10 +0100
commit196de2c3a8033561861c345248dd05adcad95d3d (patch)
treea295e792b0938a02db3f4172fb6716b85ed6ef02
parent24f833218177ad75604634e00166928d24ca84e0 (diff)
Implement hmap.update
-rw-r--r--clib/hMap.ml21
1 files changed, 10 insertions, 11 deletions
diff --git a/clib/hMap.ml b/clib/hMap.ml
index 92c6cbd434..09ffb39c21 100644
--- a/clib/hMap.ml
+++ b/clib/hMap.ml
@@ -408,18 +408,17 @@ struct
let filter_range f s =
filter (fun x _ -> f x = 0) s
- (* Not as efficient as the original version *)
let update k f m =
- try
- match f (Some (find k m)) with
- | None -> remove k m
- | Some v ->
- let m = remove k m in
- add k v m
- with Not_found ->
- match f None with
- | None -> m
- | Some v -> add k v m
+ let aux = function
+ | None -> (match f None with
+ | None -> None
+ | Some v -> Some (Map.singleton k v))
+ | Some m ->
+ let m = Map.update k f m in
+ if Map.is_empty m then None
+ else Some m
+ in
+ Int.Map.update (M.hash k) aux m
module Unsafe =
struct