Commits

camlspotter committed 9cf9a1d

find_or_add

  • Participants
  • Parent commits fb6a370

Comments (0)

Files changed (2)

File lib/hashset.ml

 let mem = Hashtbl.mem
 let find = Hashtbl.find
 let find_opt t k = try Some (Hashtbl.find t k) with Not_found -> None
+
+let find_or_add t k = 
+  try Hashtbl.find t k with Not_found -> 
+    add t k; k
+    
 let iter f = Hashtbl.iter (fun v _ -> f v)
 let fold f = Hashtbl.fold (fun v _ st -> f v st)
 let elements = Hashtbl.length

File lib/hashset.mli

 (** Poorman's hashset by Hashtbl *)
 
+(* CR jfuruse: It is mainly for hash consing but it wastes memory... Self contradiction. *)
 type 'a t
 val create : ?random:bool -> int -> 'a t
 val add : 'a t -> 'a -> unit
 val find_opt : 'a t -> 'a -> 'a option 
 (** [find] and [find_opt] finds the 'same' element in the set. Good for hash consing *)
 
+val find_or_add : 'a t -> 'a -> 'a
+
 val iter : ('a -> unit) -> 'a t -> unit
 val fold : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b
 val elements : 'a t -> int