mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-08 12:15:32 -05:00
add {CCMap,CCHashtbl}.get_or for lookup with default value
This commit is contained in:
parent
db67b19fe8
commit
708a92d027
4 changed files with 53 additions and 3 deletions
|
|
@ -1,7 +1,6 @@
|
||||||
|
|
||||||
(* This file is free software, part of containers. See file "license" for more details. *)
|
(* This file is free software, part of containers. See file "license" for more details. *)
|
||||||
|
|
||||||
|
|
||||||
(** {1 Extension to the standard Hashtbl} *)
|
(** {1 Extension to the standard Hashtbl} *)
|
||||||
|
|
||||||
type 'a sequence = ('a -> unit) -> unit
|
type 'a sequence = ('a -> unit) -> unit
|
||||||
|
|
@ -15,6 +14,15 @@ let get tbl x =
|
||||||
try Some (Hashtbl.find tbl x)
|
try Some (Hashtbl.find tbl x)
|
||||||
with Not_found -> None
|
with Not_found -> None
|
||||||
|
|
||||||
|
let get_or tbl x ~or_ =
|
||||||
|
try Hashtbl.find tbl x
|
||||||
|
with Not_found -> or_
|
||||||
|
|
||||||
|
(*$=
|
||||||
|
"c" (let tbl = of_list [1,"a"; 2,"b"] in get_or tbl 3 ~or_:"c")
|
||||||
|
"b" (let tbl = of_list [1,"a"; 2,"b"] in get_or tbl 2 ~or_:"c")
|
||||||
|
*)
|
||||||
|
|
||||||
let keys tbl k = Hashtbl.iter (fun key _ -> k key) tbl
|
let keys tbl k = Hashtbl.iter (fun key _ -> k key) tbl
|
||||||
|
|
||||||
let values tbl k = Hashtbl.iter (fun _ v -> k v) tbl
|
let values tbl k = Hashtbl.iter (fun _ v -> k v) tbl
|
||||||
|
|
@ -89,6 +97,11 @@ module type S = sig
|
||||||
val get : 'a t -> key -> 'a option
|
val get : 'a t -> key -> 'a option
|
||||||
(** Safe version of {!Hashtbl.find} *)
|
(** Safe version of {!Hashtbl.find} *)
|
||||||
|
|
||||||
|
val get_or : 'a t -> key -> or_:'a -> 'a
|
||||||
|
(** [get_or tbl k ~or_] returns the value associated to [k] if present,
|
||||||
|
and returns [or_] otherwise (if [k] doesn't belong in [tbl])
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
val keys : 'a t -> key sequence
|
val keys : 'a t -> key sequence
|
||||||
(** Iterate on keys (similar order as {!Hashtbl.iter}) *)
|
(** Iterate on keys (similar order as {!Hashtbl.iter}) *)
|
||||||
|
|
||||||
|
|
@ -96,11 +109,11 @@ module type S = sig
|
||||||
(** Iterate on values in the table *)
|
(** Iterate on values in the table *)
|
||||||
|
|
||||||
val keys_list : ('a, 'b) Hashtbl.t -> 'a list
|
val keys_list : ('a, 'b) Hashtbl.t -> 'a list
|
||||||
(** [keys_list t] is the list of keys in [t].
|
(** [keys t] is the list of keys in [t].
|
||||||
@since 0.8 *)
|
@since 0.8 *)
|
||||||
|
|
||||||
val values_list : ('a, 'b) Hashtbl.t -> 'b list
|
val values_list : ('a, 'b) Hashtbl.t -> 'b list
|
||||||
(** [values_list t] is the list of values in [t].
|
(** [values t] is the list of values in [t].
|
||||||
@since 0.8 *)
|
@since 0.8 *)
|
||||||
|
|
||||||
val map_list : (key -> 'a -> 'b) -> 'a t -> 'b list
|
val map_list : (key -> 'a -> 'b) -> 'a t -> 'b list
|
||||||
|
|
@ -131,6 +144,10 @@ module type S = sig
|
||||||
@since 0.13 *)
|
@since 0.13 *)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
(*$inject
|
||||||
|
module T = Make(CCInt)
|
||||||
|
*)
|
||||||
|
|
||||||
module Make(X : Hashtbl.HashedType)
|
module Make(X : Hashtbl.HashedType)
|
||||||
: S with type key = X.t and type 'a t = 'a Hashtbl.Make(X).t
|
: S with type key = X.t and type 'a t = 'a Hashtbl.Make(X).t
|
||||||
= struct
|
= struct
|
||||||
|
|
@ -140,6 +157,15 @@ module Make(X : Hashtbl.HashedType)
|
||||||
try Some (find tbl x)
|
try Some (find tbl x)
|
||||||
with Not_found -> None
|
with Not_found -> None
|
||||||
|
|
||||||
|
let get_or tbl x ~or_ =
|
||||||
|
try find tbl x
|
||||||
|
with Not_found -> or_
|
||||||
|
|
||||||
|
(*$=
|
||||||
|
"c" (let tbl = T.of_list [1,"a"; 2,"b"] in T.get_or tbl 3 ~or_:"c")
|
||||||
|
"b" (let tbl = T.of_list [1,"a"; 2,"b"] in T.get_or tbl 2 ~or_:"c")
|
||||||
|
*)
|
||||||
|
|
||||||
let keys tbl k = iter (fun key _ -> k key) tbl
|
let keys tbl k = iter (fun key _ -> k key) tbl
|
||||||
|
|
||||||
let values tbl k = iter (fun _ v -> k v) tbl
|
let values tbl k = iter (fun _ v -> k v) tbl
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,11 @@ type 'a printer = Format.formatter -> 'a -> unit
|
||||||
val get : ('a,'b) Hashtbl.t -> 'a -> 'b option
|
val get : ('a,'b) Hashtbl.t -> 'a -> 'b option
|
||||||
(** Safe version of {!Hashtbl.find} *)
|
(** Safe version of {!Hashtbl.find} *)
|
||||||
|
|
||||||
|
val get_or : ('a,'b) Hashtbl.t -> 'a -> or_:'b -> 'b
|
||||||
|
(** [get_or tbl k ~or_] returns the value associated to [k] if present,
|
||||||
|
and returns [or_] otherwise (if [k] doesn't belong in [tbl])
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
val keys : ('a,'b) Hashtbl.t -> 'a sequence
|
val keys : ('a,'b) Hashtbl.t -> 'a sequence
|
||||||
(** Iterate on keys (similar order as {!Hashtbl.iter}) *)
|
(** Iterate on keys (similar order as {!Hashtbl.iter}) *)
|
||||||
|
|
||||||
|
|
@ -64,6 +69,11 @@ module type S = sig
|
||||||
val get : 'a t -> key -> 'a option
|
val get : 'a t -> key -> 'a option
|
||||||
(** Safe version of {!Hashtbl.find} *)
|
(** Safe version of {!Hashtbl.find} *)
|
||||||
|
|
||||||
|
val get_or : 'a t -> key -> or_:'a -> 'a
|
||||||
|
(** [get_or tbl k ~or_] returns the value associated to [k] if present,
|
||||||
|
and returns [or_] otherwise (if [k] doesn't belong in [tbl])
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
val keys : 'a t -> key sequence
|
val keys : 'a t -> key sequence
|
||||||
(** Iterate on keys (similar order as {!Hashtbl.iter}) *)
|
(** Iterate on keys (similar order as {!Hashtbl.iter}) *)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,11 @@ module type S = sig
|
||||||
val get : key -> 'a t -> 'a option
|
val get : key -> 'a t -> 'a option
|
||||||
(** Safe version of {!find} *)
|
(** Safe version of {!find} *)
|
||||||
|
|
||||||
|
val get_or : key -> 'a t -> or_:'a -> 'a
|
||||||
|
(** [get_or k m ~or_] returns the value associated to [k] if present,
|
||||||
|
and returns [or_] otherwise (if [k] doesn't belong in [m])
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
val update : key -> ('a option -> 'a option) -> 'a t -> 'a t
|
val update : key -> ('a option -> 'a option) -> 'a t -> 'a t
|
||||||
(** [update k f m] calls [f (Some v)] if [find k m = v],
|
(** [update k f m] calls [f (Some v)] if [find k m = v],
|
||||||
otherwise it calls [f None]. In any case, if the result is [None]
|
otherwise it calls [f None]. In any case, if the result is [None]
|
||||||
|
|
@ -57,6 +62,10 @@ module Make(O : Map.OrderedType) = struct
|
||||||
try Some (find k m)
|
try Some (find k m)
|
||||||
with Not_found -> None
|
with Not_found -> None
|
||||||
|
|
||||||
|
let get_or k m ~or_ =
|
||||||
|
try find k m
|
||||||
|
with Not_found -> or_
|
||||||
|
|
||||||
let update k f m =
|
let update k f m =
|
||||||
let x =
|
let x =
|
||||||
try f (Some (find k m))
|
try f (Some (find k m))
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,11 @@ module type S = sig
|
||||||
val get : key -> 'a t -> 'a option
|
val get : key -> 'a t -> 'a option
|
||||||
(** Safe version of {!find} *)
|
(** Safe version of {!find} *)
|
||||||
|
|
||||||
|
val get_or : key -> 'a t -> or_:'a -> 'a
|
||||||
|
(** [get_or k m ~or_] returns the value associated to [k] if present,
|
||||||
|
and returns [or_] otherwise (if [k] doesn't belong in [m])
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
val update : key -> ('a option -> 'a option) -> 'a t -> 'a t
|
val update : key -> ('a option -> 'a option) -> 'a t -> 'a t
|
||||||
(** [update k f m] calls [f (Some v)] if [find k m = v],
|
(** [update k f m] calls [f (Some v)] if [find k m = v],
|
||||||
otherwise it calls [f None]. In any case, if the result is [None]
|
otherwise it calls [f None]. In any case, if the result is [None]
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue