add a few function in CCWBTree

This commit is contained in:
Simon Cruanes 2015-09-07 22:40:54 +02:00
parent 257c2ad71c
commit dab3ea6052
2 changed files with 21 additions and 0 deletions

View file

@ -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

View file

@ -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