add CCHashtbl.add_list

This commit is contained in:
Simon Cruanes 2016-02-21 21:26:02 +01:00
parent 485c6a11b5
commit 26298516b5
2 changed files with 23 additions and 0 deletions

View file

@ -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 keys_list tbl = Hashtbl.fold (fun k _ a -> k::a) tbl []
let values_list tbl = Hashtbl.fold (fun _ v a -> v::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 incr ?(by=1) tbl x =
let n = get_or tbl x ~or_:0 in let n = get_or tbl x ~or_:0 in
if n+by <= 0 if n+by <= 0
@ -125,6 +129,11 @@ module type S = sig
and returns [or_] otherwise (if [k] doesn't belong in [tbl]) and returns [or_] otherwise (if [k] doesn't belong in [tbl])
@since NEXT_RELEASE *) @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 val incr : ?by:int -> int t -> key -> unit
(** [incr ?by tbl x] increments or initializes the counter associated with [x]. (** [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]; 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)); 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 = let decr ?(by=1) tbl x =
try try
let n = find tbl x in let n = find tbl x in

View file

@ -53,6 +53,11 @@ val decr : ?by:int -> ('a, int) Hashtbl.t -> 'a -> unit
val to_seq : ('a,'b) Hashtbl.t -> ('a * 'b) sequence val to_seq : ('a,'b) Hashtbl.t -> ('a * 'b) sequence
(** Iterate on bindings in the table *) (** 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 val add_seq : ('a,'b) Hashtbl.t -> ('a * 'b) sequence -> unit
(** Add the corresponding pairs to the table, using {!Hashtbl.add}. (** Add the corresponding pairs to the table, using {!Hashtbl.add}.
@since NEXT_RELEASE *) @since NEXT_RELEASE *)
@ -101,6 +106,11 @@ module type S = sig
and returns [or_] otherwise (if [k] doesn't belong in [tbl]) and returns [or_] otherwise (if [k] doesn't belong in [tbl])
@since NEXT_RELEASE *) @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 val incr : ?by:int -> int t -> key -> unit
(** [incr ?by tbl x] increments or initializes the counter associated with [x]. (** [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]; If [get tbl x = None], then after update, [get tbl x = Some 1];