diff --git a/src/core/CCHashtbl.ml b/src/core/CCHashtbl.ml index 66971f1b..a2ec9922 100644 --- a/src/core/CCHashtbl.ml +++ b/src/core/CCHashtbl.ml @@ -30,6 +30,10 @@ let values tbl k = Hashtbl.iter (fun _ v -> k v) tbl let keys_list tbl = Hashtbl.fold (fun k _ a -> k::a) tbl [] let values_list tbl = Hashtbl.fold (fun _ v a -> v::a) tbl [] +let add_list tbl k v = + let l = try Hashtbl.find tbl k with Not_found -> [] in + Hashtbl.replace tbl k (v::l) + let incr ?(by=1) tbl x = let n = get_or tbl x ~or_:0 in if n+by <= 0 @@ -125,6 +129,11 @@ module type S = sig and returns [or_] otherwise (if [k] doesn't belong in [tbl]) @since NEXT_RELEASE *) + val add_list : 'a list t -> key -> 'a -> unit + (** [add_list tbl x y] adds [y] to the list [x] is bound to. If [x] is + not bound, it becomes bound to [[y]]. + @since NEXT_RELEASE *) + val incr : ?by:int -> int t -> key -> unit (** [incr ?by tbl x] increments or initializes the counter associated with [x]. If [get tbl x = None], then after update, [get tbl x = Some 1]; @@ -236,6 +245,10 @@ module Make(X : Hashtbl.HashedType) assert_bool "2 removed" (not (T.mem tbl 2)); *) + let add_list tbl k v = + let l = try find tbl k with Not_found -> [] in + replace tbl k (v::l) + let decr ?(by=1) tbl x = try let n = find tbl x in diff --git a/src/core/CCHashtbl.mli b/src/core/CCHashtbl.mli index b43e4b33..2e51e6bd 100644 --- a/src/core/CCHashtbl.mli +++ b/src/core/CCHashtbl.mli @@ -53,6 +53,11 @@ val decr : ?by:int -> ('a, int) Hashtbl.t -> 'a -> unit val to_seq : ('a,'b) Hashtbl.t -> ('a * 'b) sequence (** Iterate on bindings in the table *) +val add_list : ('a, 'b list) Hashtbl.t -> 'a -> 'b -> unit +(** [add_list tbl x y] adds [y] to the list [x] is bound to. If [x] is + not bound, it becomes bound to [[y]]. + @since NEXT_RELEASE *) + val add_seq : ('a,'b) Hashtbl.t -> ('a * 'b) sequence -> unit (** Add the corresponding pairs to the table, using {!Hashtbl.add}. @since NEXT_RELEASE *) @@ -101,6 +106,11 @@ module type S = sig and returns [or_] otherwise (if [k] doesn't belong in [tbl]) @since NEXT_RELEASE *) + val add_list : 'a list t -> key -> 'a -> unit + (** [add_list tbl x y] adds [y] to the list [x] is bound to. If [x] is + not bound, it becomes bound to [[y]]. + @since NEXT_RELEASE *) + val incr : ?by:int -> int t -> key -> unit (** [incr ?by tbl x] increments or initializes the counter associated with [x]. If [get tbl x = None], then after update, [get tbl x = Some 1];