Version française
Home     About     Download     Resources     Contact us    
Browse thread
problem with Hashtbl.add function...
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: problem with Hashtbl.add function...
> 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