Loading ...
Sorry, an error occurred while loading the content.

Re: "ocaml_beginners"::[] Hashtbl: safe to replace while iterating

Expand Messages
  • Frederic van der Plancke
    ... Indeed, from what you say it looks potentially dangerous. A safe option (but somewhat costlier) would be to store in the hashtable not the values
    Message 1 of 2 , Sep 2, 2005
    • 0 Attachment
      Hugo Ferreira wrote:
      >
      > Hi,
      >
      > I have the following code snippet:
      >
      > let complete_mgu mgu =
      > let sub k v =
      > Hashtbl.replace mgu k (substitute mgu v) ; ()
      > in
      > Hashtbl.iter ( sub ) mgu
      >
      > It simple iterates through a hash-table and replaces elements within it. Is
      > replacing/removing/adding safe to do while iterating through it?
      >
      > Note that I assume that only one element per key exists and need only change
      > the key's value. This should pose no problem in terms of keys but the manual
      > states that replace is equivalent to a remove followed by an add. This I
      > assume changes key/indexing and may be a problem.

      Indeed, from what you say it looks potentially dangerous.

      A safe option (but somewhat costlier) would be to store in the hashtable not the values themselves, but references to values, and change these (so that you don't touch the hashtable).

      i.e. (unchecked example with integer values) instead of

      Hashtbl.add key 123

      do

      Hashtbl.add key (ref 123)

      and then

      let complete_mgu mgu =
      let sub k v =
      v <- substitute mgu !v
      in
      Hashtbl.iter ( sub ) mgu

      But a more extensive redesign of your data structure or program might yield better solutions.

      Frédéric.
    • Hugo Ferreira
      Frédéric, ... Changing the data to a reference will mean changing quite a bit of code. I will simply use the above function to generate a new hash table and
      Message 2 of 2 , Sep 2, 2005
      • 0 Attachment
        Frédéric,

        On Friday 02 September 2005 13:02, Frederic van der Plancke wrote:
        > Hugo Ferreira wrote:
        > > Hi,
        > >
        > > I have the following code snippet:
        > >
        > > let complete_mgu mgu =
        > > let sub k v =
        > > Hashtbl.replace mgu k (substitute mgu v) ; ()
        > > in
        > > Hashtbl.iter ( sub ) mgu
        > >
        > > It simple iterates through a hash-table and replaces elements within it.
        > > Is replacing/removing/adding safe to do while iterating through it?
        > >
        > > Note that I assume that only one element per key exists and need only
        > > change the key's value. This should pose no problem in terms of keys but
        > > the manual states that replace is equivalent to a remove followed by an
        > > add. This I assume changes key/indexing and may be a problem.
        >
        > Indeed, from what you say it looks potentially dangerous.
        >
        > A safe option (but somewhat costlier) would be to store in the hashtable
        > not the values themselves, but references to values, and change these (so
        > that you don't touch the hashtable).
        >
        > i.e. (unchecked example with integer values) instead of
        >
        > Hashtbl.add key 123
        >
        > do
        >
        > Hashtbl.add key (ref 123)
        >
        > and then
        >
        > let complete_mgu mgu =
        > let sub k v =
        > v <- substitute mgu !v
        > in
        > Hashtbl.iter ( sub ) mgu
        >
        > But a more extensive redesign of your data structure or program might yield
        > better solutions.
        >

        Changing the data to a reference will mean changing quite a bit of code. I
        will simply use the above function to generate a new hash table and avoid the
        issue all together.

        Thanks for the suggestion,

        Regards,
        Hugo Ferreira.





        > Frédéric.
        >
        >
        >
        >
        > Archives up to August 22, 2005 are also downloadable at
        > http://www.connettivo.net/cntprojects/ocaml_beginners/ The archives of the
        > very official ocaml list (the seniors' one) can be found at
        > http://caml.inria.fr Attachments are banned and you're asked to be polite,
        > avoid flames etc. Yahoo! Groups Links
        >
        >
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.