[mercury-users] Inst problem with hash tables
andrea bini
andrea.bini at studenti.unimi.it
Mon Sep 22 23:51:45 AEST 2008
----- Messaggio Originale -----
Da: Ralph Becket <rafe at csse.unimelb.edu.au>
Data: Lunedi', Settembre 22, 2008 4:49 am
Oggetto: Re: [mercury-users] Inst problem with hash tables
A: mercury-users at csse.unimelb.edu.au
> What you need is something like this:
>
> :- func unsafe_promise_unique_hash_table(hash_table(K, V)::in) =
> (hash_table(K, V)::hash_table_uo) is det.
>
> :- pragma foreign_proc("C",
> unsafe_promise_unique_hash_table(HT0::in) = (HT::hash_table_uo),
> [promise_pure, will_not_call_mercury],
> "
> HT = HT0;
> ").
>
> I'm just testing a version of this which I've added to the hash_table
> module. I'll let you know once it's been reviewed.
Using this function I get no compiling errors but I haven't managed to save and load a hash table anyway.
Consider this sample program:
main(!IO) :-
% generate a list like this [{"a",1},{"b",2}, ... ]
%
different_strings(500,1,Strings),
add_ints(Strings,0,IStrings),
% the ht is filled from the list using strings
% as keys and ints as values
%
new_default(string_double_hash) = Table,
fill_table(IStrings,Table,Table1),
% write hash table to disk
%
io.open_binary_output("ht.dat",Res1,!IO),
(
if
Res1 = ok(HTdat)
then
io.write_binary(HTdat,Table1,!IO),
io.close_binary_output(HTdat,!IO)
else
error("Cannot open ht.dat!")
),
% reads the hash table back from disk and prints it
%
io.open_binary_input("ht.dat",Res2,!IO),
(
if
Res2 = ok(HTdat2)
then
io.read_binary(HTdat2,ResTab,!IO),
(
if
ResTab = ok(Table20)
then
unsafe_promise_unique_hash_table(Table20) = Table2,
io.print(Table2,!IO)
else
error("Invalid ht.dat!")
),
io.close_binary_output(HTdat2,!IO)
else
error("Cannot open ht.dat!")
).
It is compiled with no errors but when run it reports the error "Invalid ht.dat". I think that the problem resides in the high-order term in the hash table structure. It is written to disk just as the string '<<predicate>>'.
How can I fix this problem?
Thanks!
Cheers,
Andrea Bini
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20080922/05a8f88f/attachment.html>
More information about the users
mailing list