Re: problem with Hashtbl.add function...

From: Xavier Leroy (Xavier.Leroy@inria.fr)
Date: Tue Mar 02 1999 - 11:35:40 MET


Date: Tue, 2 Mar 1999 11:35:40 +0100
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: RICCI Pierre <pricci@cenatoulouse.dgac.fr>, caml-list@inria.fr
Subject: Re: problem with Hashtbl.add function...
In-Reply-To: <36D69BA0.883EDADD@cenatls.cena.dgac.fr>; from RICCI Pierre on Fri, Feb 26, 1999 at 02:03:28PM +0100

> The result of the add command might give me a hashing table with a
> key=time and data=aircrafts with their position for each key.
> The problem is I obtain a hashing table where key=time but
> data=aircrafts with always the same position for each one.

Your code is too sketchy to understand what is wrong, but my guess is
that you're calling Hashtbl.add several times with the same
"aircraft" record, which you mutate in place. E.g.:

        type aircraft = { mutable pos : int }

        let a = { pos = 10 } in
        Hashtbl.add tbl 1 a;
        a.pos <- 20;
        Hashtbl.add tbl 2 a

Both Hashtbl.find tbl 1 and Hashtbl.find tbl 2 will return {pos = 20},
since this is what you stored in the shared record a. What you wanted
is probably:

        let a = { pos = 10 } in
        Hashtbl.add tbl 1 a;
        let a' = {a with pos = 20 } in
        Hashtbl.add tbl 2 a'

> I think maybe it's the parameter "n" in (Hashtbl.hash_param n m x) which
> is too small,

No. The value of "n" may impact performance, but not semantics.

> Can anyone help me ?

My advice would be to make your type "aircraft" immutable, and write
your program without record assignment; it's much easier to
understand what is going on.

- Xavier Leroy



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:20 MET