caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Possible function to add to Hashtbl?
@ 2001-10-19  4:20 Jeremy Fincher
  2001-10-19 12:41 ` Jean-Christophe Filliatre
  0 siblings, 1 reply; 2+ messages in thread
From: Jeremy Fincher @ 2001-10-19  4:20 UTC (permalink / raw)
  To: caml-list

On the rare occasions I do find myself using the using multiple bindings of 
a
single key in a hashtbl, I also find myself needing a "remove_all" function,
which removes all the bindings to a particular key in a hashtbl.  Perhaps 
such a
function should be added to the module?

I'm sure there's a more efficient way to implement such a function in the
hashtbl module, but here's what I've been using thus far:

-----
let hashtbl_remove_all h key =
  for i=1 to List.length (Hashtbl.find_all h key) do
    Hashtbl.remove h key;
  done
-----

Jeremy


_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp

-------------------
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] 2+ messages in thread

* Re: [Caml-list] Possible function to add to Hashtbl?
  2001-10-19  4:20 [Caml-list] Possible function to add to Hashtbl? Jeremy Fincher
@ 2001-10-19 12:41 ` Jean-Christophe Filliatre
  0 siblings, 0 replies; 2+ messages in thread
From: Jean-Christophe Filliatre @ 2001-10-19 12:41 UTC (permalink / raw)
  To: Jeremy Fincher; +Cc: caml-list


 > I'm sure there's a more efficient way to implement such a function in the
 > hashtbl module, but here's what I've been using thus far:
 > 
 > -----
 > let hashtbl_remove_all h key =
 >   for i=1 to List.length (Hashtbl.find_all h key) do
 >     Hashtbl.remove h key;
 >   done
 > -----

To avoid allocating the list, you can do like this:

======================================================================
let rec remove_all h k = 
  try
    let _ = Hashtbl.find h k in Hashtbl.remove h k; remove_all h k
  with Not_found -> 
    ()
======================================================================

But conversely, you compute the hash key several times.

-- 
Jean-Christophe Filliatre (http://www.lri.fr/~filliatr)

-------------------
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] 2+ messages in thread

end of thread, other threads:[~2001-10-19 12:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-10-19  4:20 [Caml-list] Possible function to add to Hashtbl? Jeremy Fincher
2001-10-19 12:41 ` Jean-Christophe Filliatre

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