caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Portable hash
@ 2008-11-13 20:56 Dawid Toton
  2008-11-13 21:42 ` [Caml-list] " Florent Monnier
  0 siblings, 1 reply; 2+ messages in thread
From: Dawid Toton @ 2008-11-13 20:56 UTC (permalink / raw)
  To: caml-list

I'm looking for a way to calculate hashes of values of variuos types. It 
has to be:
1. proper function (i.e. x=y => (hash x)=(hash y) )
2. and should not change with a platform or compiler version.
3. It'd also help to have practically no collisions.

For some time I needed not to move data across machines and what I have 
been (wrongly) using for this so far is:
let hash x =  Digest.string (Marshal.to_string x [])

Recently I realized that it's incorrect as Marshal.to_string is not pure 
function and doesn't satisfy my first requirement. Indeed in some very 
rare cases I got different results from the same value. Only the 3rd 
point is satisfied very well.

I have to change my function before I run into problems and I'm considering:
1) Small hashes:

 let hash x =  |Hashtbl.hash_param large_int large_int x

Does anybody know what are properies of | |Hashtbl.hash_param? I mean: 
is the implementation stable? Can I have good distribution when all the 
data is examined (large parameters)?

Unfortunately it returns int of platform-dependent length (and even 
platform-depentent less significant bits of result?). How hard would it 
be to tailor it to, say, work always with 31 bits?
||
2) To serialize values with Sexp:

let hash to_sexp x = Digest.string (string_of_sexp (to_sexp x))
|
|This way I have the stablility because I can just keep implementation 
of Sexp untouched. But the performance is going to be even worse than 
with my original solution.|

Has anybody solved already this (or similar) problem?

Dawid Toton


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-11-13 21:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-11-13 20:56 Portable hash Dawid Toton
2008-11-13 21:42 ` [Caml-list] " Florent Monnier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).