Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Hashtbl.iter
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] Hashtbl.iter
From: Maya Ramanath <maya@dsl.serc.iisc.ernet.in>

> The question is with regard to Hashtbl.iter.  Basically, what happens when
> I'm doing an iteration on the Hashtbl and at the same time add something
> new to the hashtable ?  For ex:
> 	let xyz ht key value =
> 	  (* Do something with key, value *)
> 	  if (some-condition-holds) then
>    	    ht.add something new
> 
> 	let abc ht =
> 	  Hashtbl.iter (xyz ht) ht
> 
> I would like to know if the new things that I added to the hashtable will
> be picked up by the iterator.

If you look at the source in hashtbl.ml, clearly the answer is
"undefined behaviour". If your new element is added in a bucket
after the one your are currently processing then yes, otherwise no,
and the point of hashtables is that you don't know in which bucket you
will end up...

If you want to avoid this behaviour, use Map.Make.

Jacques Garrigue
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr