From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.1 required=5.0 tests=AWL,DNS_FROM_SECURITYSAGE autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 189B1BB84 for ; Thu, 13 Nov 2008 22:44:12 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqEBAPIuHEnVJFBbbGdsb2JhbACTXg0LBQMJEQO+YoNX X-IronPort-AV: E=Sophos;i="4.33,598,1220220000"; d="scan'208";a="17187340" Received: from mx-out.libertysurf.net (HELO mail.libertysurf.net) ([213.36.80.91]) by mail2-smtp-roc.national.inria.fr with ESMTP; 13 Nov 2008 22:44:11 +0100 Received: from [192.168.1.2] (91.168.187.152) by mail.libertysurf.net (7.3.118.8) id 49083E4000369418 for caml-list@yquem.inria.fr; Thu, 13 Nov 2008 23:43:07 +0100 From: Florent Monnier To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Portable hash Date: Thu, 13 Nov 2008 22:42:27 +0100 User-Agent: KMail/1.9.7 References: <491C9472.6050603@wp.pl> In-Reply-To: <491C9472.6050603@wp.pl> MIME-Version: 1.0 Content-Disposition: inline Message-Id: <200811132242.27240.fmonnier@linux-nantes.fr.eu.org> X-Face: -0"dKXwF0PiXr]fa$^)NJY7$;waqUckGcM7&q,VU?Xv\[=CiVM]g]pDs^xmfU9+Q=Z,OdfMHUR-7Ao%evJh.=aiq,#r0Ux0dm'!l|zeAXj||$>1_(Lv4Hc",&F}sbHeK0`SBA$_|XP Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; hash:01 hashtbl:01 hash:01 integers:01 ocaml-:01 byterun:01 camlprim:01 val:01 bool:01 compiler:01 ocaml:01 ocaml:01 stdlib:01 lgpl:01 hashtbl:01 > 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.