caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Hashtbl.iter
@ 2001-10-17  6:17 Maya Ramanath
  2001-10-17  6:28 ` Jacques Garrigue
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Maya Ramanath @ 2001-10-17  6:17 UTC (permalink / raw)
  To: caml-list

Hi,

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.
			- Maya


-------------------
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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Hashtbl.iter
  2001-10-17  6:17 [Caml-list] Hashtbl.iter Maya Ramanath
@ 2001-10-17  6:28 ` Jacques Garrigue
       [not found] ` <9qj955$1l5$1@qrnik.zagroda>
  2001-10-17  8:49 ` Fabrice Le Fessant
  2 siblings, 0 replies; 5+ messages in thread
From: Jacques Garrigue @ 2001-10-17  6:28 UTC (permalink / raw)
  To: maya; +Cc: caml-list

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Hashtbl.iter
       [not found] ` <9qj955$1l5$1@qrnik.zagroda>
@ 2001-10-17  6:51   ` Marcin 'Qrczak' Kowalczyk
  2001-10-17  8:30     ` Jacques Garrigue
  0 siblings, 1 reply; 5+ messages in thread
From: Marcin 'Qrczak' Kowalczyk @ 2001-10-17  6:51 UTC (permalink / raw)
  To: caml-list

Wed, 17 Oct 2001 15:28:54 +0900, Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp> pisze:

> 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 if the table is resized because of the added element, you are
completely screwed. This undefined behavior is not limited to the
choice between processing the new element or not.

-- 
 __("<  Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZASTĘPCZA
QRCZAK

-------------------
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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Hashtbl.iter
  2001-10-17  6:51   ` Marcin 'Qrczak' Kowalczyk
@ 2001-10-17  8:30     ` Jacques Garrigue
  0 siblings, 0 replies; 5+ messages in thread
From: Jacques Garrigue @ 2001-10-17  8:30 UTC (permalink / raw)
  To: qrczak; +Cc: caml-list

From: "Marcin 'Qrczak' Kowalczyk" <qrczak@knm.org.pl>
> Wed, 17 Oct 2001 15:28:54 +0900, Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp> pisze:
> 
> > 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 if the table is resized because of the added element, you are
> completely screwed. This undefined behavior is not limited to the
> choice between processing the new element or not.

No, no. It is not that bad. This just means that a new array will be
built, replacing the one you are currently processing. As a result
further changes on the hashtable will have absolutely no impact on the
behaviour of Hashtbl.iter.

That said, it is not a good idea to modify a mutable data structure
while you are processing it. There are immutable ones for that.

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Hashtbl.iter
  2001-10-17  6:17 [Caml-list] Hashtbl.iter Maya Ramanath
  2001-10-17  6:28 ` Jacques Garrigue
       [not found] ` <9qj955$1l5$1@qrnik.zagroda>
@ 2001-10-17  8:49 ` Fabrice Le Fessant
  2 siblings, 0 replies; 5+ messages in thread
From: Fabrice Le Fessant @ 2001-10-17  8:49 UTC (permalink / raw)
  To: Maya Ramanath; +Cc: caml-list


Just iter once to create a list from the Hashtbl.t. Then, use
List.map on the list to add new element to the Hashtbl.t...

- Fabrice

-------------------
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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2001-10-17  8:49 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-10-17  6:17 [Caml-list] Hashtbl.iter Maya Ramanath
2001-10-17  6:28 ` Jacques Garrigue
     [not found] ` <9qj955$1l5$1@qrnik.zagroda>
2001-10-17  6:51   ` Marcin 'Qrczak' Kowalczyk
2001-10-17  8:30     ` Jacques Garrigue
2001-10-17  8:49 ` Fabrice Le Fessant

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).