From: Amit D. <adubey@CoLi.Uni-SB.DE> - 2003-02-28 17:03:06
|
> (* ****** Module ExtHash **** *) > > module ExtHash : > sig > > (* return all the keys of an Hashtable > if the key is several time in the Hashtable, the list will > contain it that many times *) > val keys : ('a,'b) Hashtbl.t -> 'a list > > (* return all the values of an Hashtable *) > val values : ('a,'b) Hashtbl.t -> 'b list > > (* remove all bindings for the given key *) > val remove_all : ('a,'b) Hashtbl.t -> 'a -> unit > > end I use hashtables extensively in my work, and there are two functions that I find invaluable. (The names, I admit, might be stupid :) (* return the element bound to the given key; if no such element exists, return a default value *) val find_always : ('a,'b) Hashtbl.t -> ~key : 'a -> ~default : 'b -> 'b val safe_find (* alternate name *) (* replace an old binding with a new one whose value depends on the old one. If no previous binding exists, use a default value *) val update : ('a, 'b) Hastbl.t -> ~key : 'a -> ~default : 'b -> ('b -> 'b) -> unit My primary use of hashtables is to store sparse arrays, which assume a value of `0.0' anywhere there is currently no binding. I'm not sure this is useful to people who don't use hashtables this way. -Amit |