mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
add CCHashtbl.add_list
This commit is contained in:
parent
485c6a11b5
commit
26298516b5
2 changed files with 23 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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];
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue