* problem with Hashtbl.add function...
@ 1999-02-26 13:03 RICCI Pierre
1999-03-02 10:35 ` Xavier Leroy
0 siblings, 1 reply; 2+ messages in thread
From: RICCI Pierre @ 1999-02-26 13:03 UTC (permalink / raw)
To: caml-list
Hi, I have a little problem with the function Hashtbl.add.
I use it in a form like this:
List.iter (fun a:->Hashtbl.add tbl !time a) !b;
where (b:aircraft) is a list of structure like
type aircraft = {
pln : pln;
mutable where : point3D;
mutable heading : seconds;
mutable cfl : level;
mutable phase : phase;
mutable g_speed : kts;
mutable v_speed : feetpmn;
mutable sector : string;
mutable instruction : (action * int * int) option;
err_gspeed : int; (* 100 +/- 5
*)
err_vspeed : int (* 100 +/- 20
*)
}
where pln, point3D... are structures too.
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.
I think maybe it's the parameter "n" in (Hashtbl.hash_param n m x) which
is too small, but I dont know how to change it. If, I want to rewrite
the add function in my program, using n parameter with 30 by example,
the compilator tells me he doesn't know the data field of h.
let add h key info =
let i = (hash_param 10 100 key) mod (Array.length h.data) in
let bucket = Cons(key, info, h.data.(i)) in
h.data.(i) <- bucket;
if bucket_too_long h.max_len bucket then resize hash h
Can anyone help me ?
PS: Please escuse me if the error is too simple but I don't see the
mistake.
bye.
--
____________________________________________________________
Pierre RICCI | Email : pricci@cenatls.cena.dgac.fr
| Tel : (+33) 62 25 95 28
CENA Toulouse | Fax : (+33) 62 25 95 99
____________________________________________________________
Cellule MSS ( Modelisation Stochastique et Statistiques )
____________________________________________________________
7 avenue Edouard Belin BP 4005 (site de l'ENAC)
31055 Toulouse Cedex FRANCE
____________________________________________________________
************************************************************
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: problem with Hashtbl.add function...
1999-02-26 13:03 problem with Hashtbl.add function RICCI Pierre
@ 1999-03-02 10:35 ` Xavier Leroy
0 siblings, 0 replies; 2+ messages in thread
From: Xavier Leroy @ 1999-03-02 10:35 UTC (permalink / raw)
To: RICCI Pierre, caml-list
> 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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~1999-03-02 12:32 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-02-26 13:03 problem with Hashtbl.add function RICCI Pierre
1999-03-02 10:35 ` Xavier Leroy
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox