caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Florent Monnier <fmonnier@linux-nantes.fr.eu.org>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Portable hash
Date: Thu, 13 Nov 2008 22:42:27 +0100	[thread overview]
Message-ID: <200811132242.27240.fmonnier@linux-nantes.fr.eu.org> (raw)
In-Reply-To: <491C9472.6050603@wp.pl>

> How hard would it
> be to tailor it to, say, work always with 31 bits?

Hashtbl.hash will return a 31 bit integers on both 32 or 64 architectures:

file: ocaml-3.10.2/byterun/hash.c

CAMLprim value caml_hash_univ_param(value count, value limit, value obj)
{
 [...]

  return Val_long(hash_accu & 0x3FFFFFFF);
  /* The & has two purposes: ensure that the return value is positive
     and give the same result on 32 bit and 64 bit architectures. */
}

# max_int (* the 31 bit one *) = 0x3FFFFFFF ;;
- : bool = true

> 2. and should not change with a platform or compiler version.
If you wish to get a code that won't change for a futur ocaml version, just 
extract the current hash function of ocaml to include it in your own code.
You can do this because the code of the stdlib is LGPL.
____________

currently I have some problems with Hashtbl.hash because it doesn't hash 
values of kind integers, so if (x = y + 1) I get ((hash x) = (hash y) + 1) 
which results in a poor repartition.

Does someone know how to hash an integer ?
Here there are hashing functions for integers:
- http://www.concentric.net/~Ttwang/tech/inthash.htm
- http://burtleburtle.net/bob/hash/integer.html
but they are for 32 bit unsigned integers.
How can I adapt it for 31 bit integers ?

Or would it be a good solution to convert the bits of the integer to a 
bool list and then give it to Hashtbl.hash ?
At least with this solution I haven't ((hash x) = (hash y) + 1) anymore.


      reply	other threads:[~2008-11-13 21:44 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-13 20:56 Dawid Toton
2008-11-13 21:42 ` Florent Monnier [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200811132242.27240.fmonnier@linux-nantes.fr.eu.org \
    --to=fmonnier@linux-nantes.fr.eu.org \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).