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

-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Reply via email to