From 01f70cc8026d5497a3e60d5b72de906377338fab Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sun, 7 Feb 2021 13:03:03 -0500 Subject: [PATCH] update code and comments --- src/core/CCMap.ml | 31 ++++++++++++++++--------------- src/core/CCMap.mli | 9 +++++++++ 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/core/CCMap.ml b/src/core/CCMap.ml index 74481812..3c66b923 100644 --- a/src/core/CCMap.ml +++ b/src/core/CCMap.ml @@ -181,6 +181,15 @@ module Make(O : Map.OrderedType) = struct | Some v1, Some v2 -> f k v1 v2) a b + let update k f m = + let x = + try f (Some (M.find k m)) + with Not_found -> f None + in + match x with + | None -> M.remove k m + | Some v' -> M.add k v' m + let choose_opt m = try Some (M.choose m) with Not_found -> None @@ -229,15 +238,6 @@ module Make(O : Map.OrderedType) = struct | None -> raise Not_found | Some (k,v) -> k, v - let update k f m = - let x = - try f (Some (M.find k m)) - with Not_found -> f None - in - match x with - | None -> M.remove k m - | Some v' -> M.add k v' m - (*$= & ~printer:CCFormat.(to_string @@ Dump.(list (pair string int))) ["a", 1; "b", 20] \ (M.of_list ["b", 2; "c", 3] \ @@ -247,6 +247,10 @@ module Make(O : Map.OrderedType) = struct |> M.to_list |> List.sort CCOrd.compare) *) + (* === include M. + This will shadow some values depending on OCaml's current version + === *) + include M let get = find_opt @@ -271,10 +275,8 @@ module Make(O : Map.OrderedType) = struct let add_seq_with ~f m s = let combine k v = function None -> Some v | Some v0 -> Some (f k v v0) in - let m = ref m in - Seq.iter (fun (k,v) -> - m := update k (combine k v) !m) s; - !m + Seq.fold_left + (fun m (k,v) -> update k (combine k v) m) m s let of_seq s = add_seq empty s let of_seq_with ~f s = add_seq_with ~f empty s @@ -307,8 +309,7 @@ module Make(O : Map.OrderedType) = struct let add_list_with ~f m l = let combine k v = function None -> Some v | Some v0 -> Some (f k v v0) in List.fold_left - (fun m (k,v) -> - update k (combine k v) m) + (fun m (k,v) -> update k (combine k v) m) m l let of_list l = add_list empty l diff --git a/src/core/CCMap.mli b/src/core/CCMap.mli index a7ebacb6..5b3a66b9 100644 --- a/src/core/CCMap.mli +++ b/src/core/CCMap.mli @@ -84,6 +84,9 @@ module type S = sig val add_seq_with : f:(key -> 'a -> 'a -> 'a) -> 'a t -> (key * 'a) Seq.t -> 'a t (** [add_seq ~f m l] adds the given seq [l] of bindings to the map [m], using [f] to combine values that have the same key. + If a key occurs several times, all its bindings are combined using the + function [f], with [f key v1 v2] being called with [v1] occurring + later in the seq than [v2]. @since NEXT_RELEASE *) val of_seq : (key * 'a) Seq.t -> 'a t @@ -108,6 +111,9 @@ module type S = sig val add_iter_with : f:(key -> 'a -> 'a -> 'a) -> 'a t -> (key * 'a) iter -> 'a t (** [add_iter ~f m l] adds the given iter [l] of bindings to the map [m], using [f] to combine values that have the same key. + If a key occurs several times, all its bindings are combined using the + function [f], with [f key v1 v2] being called with [v1] occurring + later in the seq than [v2]. @since NEXT_RELEASE *) val of_iter : (key * 'a) iter -> 'a t @@ -149,6 +155,9 @@ module type S = sig val add_list_with : f:(key -> 'a -> 'a -> 'a) -> 'a t -> (key * 'a) list -> 'a t (** [add_list ~f m l] adds the given list [l] of bindings to the map [m], using [f] to combine values that have the same key. + If a key occurs several times, all its bindings are combined using the + function [f], with [f key v1 v2] being called with [v1] occurring + later in the seq than [v2]. @since NEXT_RELEASE *) val keys : _ t -> key iter