From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,76e8d825615718a X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,UTF8 Path: g2news1.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!newsfeed.straub-nv.de!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: Ada.Containers Hash function for a set of small integers Date: Sat, 24 Apr 2010 12:28:23 +0100 Organization: A noiseless patient Spider Message-ID: References: <50701baa-7c05-450c-a42d-c699516ddc00@t14g2000prm.googlegroups.com> <0590cf17-12ea-401d-9dc3-02365139b37e@p35g2000prf.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Date: Sat, 24 Apr 2010 11:28:25 +0000 (UTC) Injection-Info: news.eternal-september.org; posting-host="KCXegvZb5vh43D+f3BR6Ew"; logging-data="10580"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+9yTyGWaoFggeAg4gULBVbjirqFKL+Wh8=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (darwin) Cancel-Lock: sha1:PhotdKnQ6SJezbEsr/hxJANLxQY= sha1:hUE7+DVW9rGlLv2YtVSvAAO0GIg= Xref: g2news1.google.com comp.lang.ada:10191 Date: 2010-04-24T12:28:23+01:00 List-Id: Michael R writes: > On Apr 23, 2:39 pm, Simon Wright 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.