Browse thread
[Caml-list] Hashtbl and destructive operations on keys
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | oliver@f... |
| Subject: | Re: [Caml-list] Hashtbl and destructive operations on keys |
On Tue, Mar 23, 2004 at 12:13:14AM +0100, Thomas Fischbacher wrote: [...] > > let nonconsing_accum f_combine f_copy ht h_key h_val = > if Hashtbl.mem ht h_key > then Hashtbl.replace ht h_key > (f_combine (Hashtbl.find ht h_key) h_val) > else Hashtbl.add ht (f_copy h_key) h_val > ;; > Why don't you sample all values first and afteryou have done it, using a function to apply your f_combine to all stuff, you get for each of the keys? You can get all values for a key with Hashtbl.find_all. So you only have to remember, which keys are in the hashtbl and then apply your f_combine to each of that keys. That also ha sthe advantage that you are free to apply different functions to your hashtbl-values, because you have saved all your values inside of the hash. So you may apply (+) as well as (*) or others, once you have sampled all your data. Because you are testing the existence of the key with each new value you want to insert in your hashtbl, you have unnecessary calls with each insert-operation (with each call to nonconsing_accum). IMHO it makes more sense to sample all data with Hashtbl.add first, and then do your operations on a per-key base with Hashtbl.find_all. (I hope I didn't miss your point.) Ciao, Oliver ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners