Commits

camlspotter  committed 452dd0e

added Hashtbl.find_or_add

  • Participants
  • Parent commits 5b47183

Comments (0)

Files changed (2)

File lib/xhashtbl.ml

 
 let find_default def tbl k = try Hashtbl.find tbl k with Not_found -> def
 
+let find_or_add deff tbl k = 
+  try Hashtbl.find tbl k with Not_found -> 
+    let def = deff k in
+    Hashtbl.replace tbl k def;
+    def
+
 let alter tbl k f =
   match f (find_opt tbl k) with
   | None -> Hashtbl.remove tbl k

File lib/xhashtbl.mli

 val to_list      : ('a, 'b) Hashtbl.t -> ('a * 'b) list
 val find_opt     : ('a, 'b) Hashtbl.t -> 'a -> 'b option
 val find_default : 'b -> ('a, 'b) Hashtbl.t -> 'a -> 'b
+val find_or_add  : ('a -> 'b) -> ('a, 'b) Hashtbl.t -> 'a -> 'b
 val alter        : ('a, 'b) Hashtbl.t -> 'a -> ('b option -> 'b option) -> unit
 val memoize      : ('a, 'b) Hashtbl.t -> ('a -> 'b) -> 'a -> 'b