Browse thread
[Caml-list] Hashtbl and destructive operations on keys
-
Thomas Fischbacher
-
Remi Vanicat
- Thomas Fischbacher
-
Remi Vanicat
[
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: | Thomas Fischbacher <Thomas.Fischbacher@P...> |
| Subject: | Re: [Caml-list] Hashtbl and destructive operations on keys |
On Mon, 22 Mar 2004, Remi Vanicat wrote:
> > So, could I please get this officially sanctioned? :-)
>
> This is not an official answers, but it is what ocaml tell me :
>
> # let tbl = create 10;;
> val tbl : ('_a, '_b) Hashtbl.t = <abstr>
> # let r = ref 10;;
> val r : int ref = {contents = 10}
> # replace tbl r 50;;
> - : unit = ()
> # r := 1;;
> - : unit = ()
> # find tbl r;;
> Exception: Not_found.
Please look closely at my original posting. This is not what I was
intending to do.
What I rather want to do is roughly along the following lines:
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
;;
let result =
(
let ht = Hashtbl.create 10 in
let arr = Array.make 4 0 in
for i = 0 to 1000 do
for k = 0 to 4-1 do
arr.(k) <- Random.int 5;
done;
nonconsing_accum (+) Array.copy ht arr 1;
done;
Hashtbl.fold (fun k v so_far -> (k,v)::so_far) ht [];
)
;;
Former experience with CMU CL tells me that this technique of not
unnecessarily consing hash keys can easily lead to a performance gain of
a factor of 10 - depending on the application, of course.
But I have a very bad feeling about it as long as I do not have any
guarantee that I really can re-use the corpus of a hash key passed to
Hashtbl.replace.
Yes, one answer certainly is "just make the hash value a ref and work on
its contents". But there are indeed some crazy situations where one is
better off using the above technique.
--
regards, tf@cip.physik.uni-muenchen.de (o_
Thomas Fischbacher - http://www.cip.physik.uni-muenchen.de/~tf //\
(lambda (n) ((lambda (p q r) (p p q r)) (lambda (g x y) V_/_
(if (= x 0) y (g g (- x 1) (* x y)))) n 1)) (Debian GNU)
-------------------
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