From: Florent Monnier <fmonnier@linux-nantes.fr.eu.org>
To: caml-list@yquem.inria.fr
Cc: David MENTRE <dmentre@linux-france.org>
Subject: Re: [Caml-list] Hashtbl.remove legal within Hashtbl.iter for the same hash table?
Date: Sun, 11 May 2008 16:42:13 +0200 [thread overview]
Message-ID: <200805111642.14164.fmonnier@linux-nantes.fr.eu.org> (raw)
In-Reply-To: <87tzh5kxhl.fsf@linux-france.org>
> Hello,
Hello David,
> Probably a newbie question but anyway: is it allowed to do a
> Hashtbl.remove while doing a Hashtbl.iter on the same hash table?
I don't know if it is legal, but at least it works:
# let h = Hashtbl.create 8 ;;
# for i = 0 to pred 8 do
Hashtbl.add h i (char_of_int((int_of_char 'A') + i))
done ;;
# Hashtbl.iter (fun i v ->
if i = 3 then Hashtbl.remove h 5;
Printf.printf " %d %c\n" i v) h ;;
0 A
1 B
2 C
3 D
4 E
6 G
7 H
But perhaps is it implementation dependant, in which case another
implementation could react in a different way...
The ExtLib reacts in the same way than the standard one.
> More precisely, at one point while doing a "Hashtbl.iter f h" my
> function "f" is called with something like "f k v". Can I do a
> "Hashtbl.remove h k" within the body of "f"?
it seems to work too when we remove the current itered key
(with both implementations)
I don't know if this module was written with in mind to allow this behavior,
but for what I understand from the manual : "in-place modification" should
mean that this structure is purely imperative and that this behavior is
"legal/allowed".
______
Perhaps a more sure method could be to get an enum from the hash table, and
then iter on this enum (with the ExtLib), see below.
With this method you are sure that *all* the keys will be itered, including
the hidden contents, which is different than the previous example.
Then you don't need to worry about a perhaps implementation dependant
behavior.
ocaml -I +/site-lib/extlib extLib.cma
# open ExtLib ;;
# open ExtHashtbl ;;
# let h = Hashtbl.create 8 ;;
# for i = 0 to pred 8 do
Hashtbl.add h i (char_of_int((int_of_char 'A') + i))
done ;;
(* this one would not appear with the sdt Hashtbl.iter ! *)
# Hashtbl.add h 6 'Z' ;;
# let iter_all_hashtbl f h =
let keys = Hashtbl.keys h
and vals = Hashtbl.values h in
Enum.iter2 f keys vals
;;
# let f i v =
if i = 3 then Hashtbl.remove h 3;
Printf.printf " %d %c\n" i v
;;
# iter_all_hashtbl f h ;;
0 A
1 B
2 C
3 D
4 E
5 F
6 Z
6 G
7 H
# iter_all_hashtbl f h ;;
0 A
1 B
2 C
4 E
5 F
6 Z
6 G
7 H
--
Florent
next prev parent reply other threads:[~2008-05-11 14:35 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-05-11 9:20 David MENTRE
2008-05-11 14:42 ` Florent Monnier [this message]
2008-05-11 14:46 ` [Caml-list] " Till Varoquaux
2008-05-11 15:10 ` Florent Monnier
2008-05-11 22:45 ` Till Varoquaux
2008-05-11 23:08 ` Martin Jambon
2008-05-12 9:29 ` David MENTRE
2008-05-11 15:07 ` Florent Monnier
2008-05-11 14:44 ` Julien Peeters
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=200805111642.14164.fmonnier@linux-nantes.fr.eu.org \
--to=fmonnier@linux-nantes.fr.eu.org \
--cc=caml-list@yquem.inria.fr \
--cc=dmentre@linux-france.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox