From dab3ea6052b842237ea8dcf5095a557a72a599d9 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 7 Sep 2015 22:40:54 +0200 Subject: [PATCH] add a few function in `CCWBTree` --- src/data/CCWBTree.ml | 14 ++++++++++++++ src/data/CCWBTree.mli | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/src/data/CCWBTree.ml b/src/data/CCWBTree.ml index 89e3740e..9d76fd31 100644 --- a/src/data/CCWBTree.ml +++ b/src/data/CCWBTree.ml @@ -27,6 +27,8 @@ module type S = sig val is_empty : _ t -> bool + val singleton : key -> 'a -> 'a t + val mem : key -> _ t -> bool val get : key -> 'a t -> 'a option @@ -45,6 +47,11 @@ module type S = sig val remove : key -> 'a t -> 'a t + val update : key -> ('a option -> 'a option) -> 'a t -> 'a t + (** [update k f m] calls [f (Some v)] if [get k m = Some v], [f None] + otherwise. Then, if [f] returns [Some v'] it binds [k] to [v'], + if [f] returns [None] it removes [k] *) + val cardinal : _ t -> int val weight : _ t -> int @@ -273,6 +280,13 @@ module MakeFull(K : KEY) : S with type key = K.t = struct List.for_all (fun (k,_) -> let m' = M.remove k m in M.balanced m') l) *) + let update k f m = + let maybe_v = get k m in + match maybe_v, f maybe_v with + | None, None -> m + | Some _, None -> remove k m + | _, Some v -> add k v m + (* TODO union, intersection *) let rec nth_exn i m = match m with diff --git a/src/data/CCWBTree.mli b/src/data/CCWBTree.mli index f74c27b0..6219c46f 100644 --- a/src/data/CCWBTree.mli +++ b/src/data/CCWBTree.mli @@ -32,6 +32,8 @@ module type S = sig val is_empty : _ t -> bool + val singleton : key -> 'a -> 'a t + val mem : key -> _ t -> bool val get : key -> 'a t -> 'a option @@ -50,6 +52,11 @@ module type S = sig val remove : key -> 'a t -> 'a t + val update : key -> ('a option -> 'a option) -> 'a t -> 'a t + (** [update k f m] calls [f (Some v)] if [get k m = Some v], [f None] + otherwise. Then, if [f] returns [Some v'] it binds [k] to [v'], + if [f] returns [None] it removes [k] *) + val cardinal : _ t -> int val weight : _ t -> int