caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] hashing big ints
@ 2011-12-06  7:22 Damien Pous
  2011-12-06  7:44 ` Jérémie Dimino
  2011-12-06  8:52 ` Goswin von Brederlow
  0 siblings, 2 replies; 4+ messages in thread
From: Damien Pous @ 2011-12-06  7:22 UTC (permalink / raw)
  To: caml-list

Dear caml-list,

Let me re-raise an apparently old and possibly dumb question : how to
hash big ints ?

Hashtbl.hash doesn't work (ocaml 3.12.1):

# Hashtbl.hash (Big_int.big_int_of_int 42);;
- : int = 1
# Hashtbl.hash (Big_int.big_int_of_int 33);;
- : int = 1

(Certainly because big ints hide their content in custom blocks.)

I found several discussions about this kind of problem (comparison of
nums, etc...), which were more focused on how to let
Pervasive.compare/equal and Hashtbl.hash behave correctly in a uniform
way.

My question is simpler : I just need to write my own hashing function,
so that I can call Hashtbl.Make. I currently use something like:

let rec hash x =
  try int_of_big_int x
  with _ -> hash (shift_right_big_int x Sys.word_size)

Some of you certainly know how to get a better behaved function. (I
don't know anything about hashing theory, except that my function is
certainly not a good one: it doesn't contain any prime number...)

Thanks for your help,
Damien

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

end of thread, other threads:[~2011-12-06 15:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-06  7:22 [Caml-list] hashing big ints Damien Pous
2011-12-06  7:44 ` Jérémie Dimino
2011-12-06  8:52 ` Goswin von Brederlow
2011-12-06 15:18   ` Damien Pous

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).