comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: Ada.Containers Hash function for a set of small integers
Date: Sat, 24 Apr 2010 12:28:23 +0100
Date: 2010-04-24T12:28:23+01:00	[thread overview]
Message-ID: <m2wrvxw0mg.fsf@pushface.org> (raw)
In-Reply-To: 0590cf17-12ea-401d-9dc3-02365139b37e@p35g2000prf.googlegroups.com

Michael R <michael@zanyblue.com> writes:

> On Apr 23, 2:39 pm, Simon Wright <si...@pushface.org> wrote:

>> I don't know about 'best', but in ColdFrame
>> (http://coldframe.sourceforge.net/coldframe/) I would generate this hash
>> for use with Booch Maps, where the key components of Instance are
>> {A:Integer, B:Integer, C:Integer}:
>>
>>    function Instance_Hash (I : Instance) return Natural is
>>       type M is mod 2**31;
>>       Result : M := 0;
>>    begin
>>       Result := Result xor M (I.A mod 2**31);
>>       Result := Result * 10019;
>>       Result := Result xor M (I.B mod 2**31);
>>       Result := Result * 10019;
>>       Result := Result xor M (I.C mod 2**31);
>>       Result := Result * 10019;
>>       return Natural (Result);
>>    end Instance_Hash;
>>
>> I believe the 10019 came from Knuth, but can't see a reference.
>
> Thank you for this suggestion.  Just fyi, my GNAT 4.4.3 compiler
> generated
>
> xtest.adb:20:40: value not in range of type "Standard.Integer"
> xtest.adb:20:40: static expression fails Constraint_Check
>
> for "M (I.A mod 2**31)" expressions.  Rephrasing as
>
> Result := Result xor M'Mod (I.A);
>
> compiled OK.

I looked into this and found that for this specific case I would in fact
generate rather different code -- sorry about that, a bit like posting
untested Ada :-(

Actually it would be

   function Instance_Hash (I : Instance) return Natural is
      type M is mod 2**31;
      Result : M := 0;
   begin
      Result := Result xor Integer'Pos (I.A);
      Result := Result * 10019;
      Result := Result xor Integer'Pos (I.B);
      Result := Result * 10019;
      Result := Result xor Integer'Pos (I.C);
      return Natural (Result);
   end Instance_Hash;

and, before anyone rushes to check it out, yes, it fails with negative
values and I'm about to file a bug against it.

Thanks for the response.



  reply	other threads:[~2010-04-24 11:28 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-22 23:26 Ada.Containers Hash function for a set of small integers Michael R
2010-04-23  1:39 ` John B. Matthews
2010-04-23 21:39 ` Simon Wright
2010-04-23 22:47   ` Michael R
2010-04-24 11:28     ` Simon Wright [this message]
2010-04-26 18:37       ` Robert A Duff
2010-04-26 21:05         ` Simon Wright
2010-04-26 21:50           ` Adam Beneschan
2010-04-27  4:50             ` AdaMagica
2010-04-27 19:04               ` Simon Wright
2010-04-27 19:08             ` Simon Wright
2010-04-23 23:08   ` Jeffrey R. Carter
2010-04-26 15:33   ` Warren
2010-04-26 18:14     ` Jeffrey R. Carter
2010-04-26 18:32       ` Charmed Snark
2010-05-05  4:29   ` Yannick Duchêne (Hibou57)
2010-05-06 15:46     ` Warren
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox