Easy way to find the key in a map given a binding, Ocaml -
i not familiar map module in ocaml.
i have simple map m: string map.make(int).t int string, , know every binding (:string) unique. write funciton find_from_string_to_int: string -> string map.make(int).t -> int, help? thank much!
if infrequent operation, easiest way fold on (key,value) bindings of map smap.fold:
# module smap = map.make(string);; # let reverse v t = smap.fold (fun k v' acc -> if v = v' k else acc) t none;; val reverse : 'a -> 'a smap.t -> smap.key option = <fun> if common operation, you're looking bidirectional map (a map can queried in both "directions"). easiest way carry around pair of maps: int imap.t * string smap.t (accesses in both direction logarithmic, while reverse above linear in size of map). implement dedicated data structure, that's not worth added complexity.
ps: reverse optimized quit iteration if value found, if it's uncommon operation it's not important anyway.
Comments
Post a Comment