From 409612297994996e82ca977db765958d466d1282 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sat, 30 Sep 2017 15:02:03 +0200 Subject: [PATCH] add `CCMap.union` --- src/core/CCMap.ml | 14 ++++++++++++++ src/core/CCMap.mli | 5 +++++ 2 files changed, 19 insertions(+) diff --git a/src/core/CCMap.ml b/src/core/CCMap.ml index 056c01d2..1f2a5d55 100644 --- a/src/core/CCMap.ml +++ b/src/core/CCMap.ml @@ -29,6 +29,11 @@ module type S = sig (** [merge_safe ~f a b] merges the maps [a] and [b] together. @since 0.17 *) + val union : (key -> 'a -> 'a -> 'a option) -> 'a t -> 'a t -> 'a t + (** Union of both maps, using the function to combine bindings + that belong to both inputs + @since NEXT_RELEASE *) + val of_seq : (key * 'a) sequence -> 'a t val add_seq : 'a t -> (key * 'a) sequence -> 'a t @@ -85,6 +90,15 @@ module Make(O : Map.OrderedType) = struct | Some v1, Some v2 -> f k (`Both (v1,v2))) a b + let union f a b = + merge + (fun k v1 v2 -> match v1, v2 with + | None, None -> assert false + | None, (Some _ as r) -> r + | Some _ as r, None -> r + | Some v1, Some v2 -> f k v1 v2) + a b + let add_seq m s = let m = ref m in s (fun (k,v) -> m := add k v !m); diff --git a/src/core/CCMap.mli b/src/core/CCMap.mli index 8ba47dd7..15e3c24a 100644 --- a/src/core/CCMap.mli +++ b/src/core/CCMap.mli @@ -32,6 +32,11 @@ module type S = sig (** [merge_safe ~f a b] merges the maps [a] and [b] together. @since 0.17 *) + val union : (key -> 'a -> 'a -> 'a option) -> 'a t -> 'a t -> 'a t + (** Union of both maps, using the function to combine bindings + that belong to both inputs + @since NEXT_RELEASE *) + val of_seq : (key * 'a) sequence -> 'a t (** Same as {!of_list} *)