----- Messaggio Originale -----<br>Da: Ralph Becket <rafe@csse.unimelb.edu.au><br>Data: Lunedi', Settembre 22, 2008 4:49 am<br>Oggetto: Re: [mercury-users] Inst problem with hash tables<br>A: mercury-users@csse.unimelb.edu.au<br><br>> What you need is something like this:<br>> <br>> :- func unsafe_promise_unique_hash_table(hash_table(K, V)::in) = <br>>      (hash_table(K, V)::hash_table_uo) is det.<br>> <br>> :- pragma foreign_proc("C",<br>>      unsafe_promise_unique_hash_table(HT0::in) = (HT::hash_table_uo),<br>>  [promise_pure, will_not_call_mercury],<br>> "<br>>       HT = HT0;<br>> ").<br>> <br>> I'm just testing a version of this which I've added to the hash_table<br>> module.  I'll let you know once it's been reviewed.<br><br>Using this function I get no compiling errors but I haven't managed to save and load a hash table anyway.<br>Consider this sample program:<br><br><br>main(!IO) :-<br>    % generate a list like this [{"a",1},{"b",2}, ... ]<br>    %<br>    different_strings(500,1,Strings),<br>    add_ints(Strings,0,IStrings),<br><br>    % the ht is filled from the list using strings<br>    % as keys and ints as values<br>    %<br>    new_default(string_double_hash) = Table,<br>    fill_table(IStrings,Table,Table1),<br><br>    % write hash table to disk<br>    %<br>    io.open_binary_output("ht.dat",Res1,!IO),<br>    (<br>      if<br>        Res1 = ok(HTdat)<br>      then<br>        io.write_binary(HTdat,Table1,!IO),<br>        io.close_binary_output(HTdat,!IO)<br>      else<br>        error("Cannot open ht.dat!")<br>    ),<br><br>    % reads the hash table back from disk and prints it<br>    %<br>    io.open_binary_input("ht.dat",Res2,!IO),<br>    (<br>      if<br>        Res2 = ok(HTdat2)<br>      then<br>        io.read_binary(HTdat2,ResTab,!IO),<br>        (<br>          if<br>            ResTab = ok(Table20)<br>          then<br>            unsafe_promise_unique_hash_table(Table20) = Table2,<br>            io.print(Table2,!IO)<br>          else<br>            error("Invalid ht.dat!")<br>        ),<br>        io.close_binary_output(HTdat2,!IO)<br>      else<br>        error("Cannot open ht.dat!")<br>    ).<br><br><br><br>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>>'.<br><br>How can I fix this problem?<br><br>Thanks!<br>Cheers,<br><br>Andrea Bini<br><br><br>