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,ASCII Path: g2news1.google.com!postnews.google.com!p35g2000prf.googlegroups.com!not-for-mail From: Michael R Newsgroups: comp.lang.ada Subject: Re: Ada.Containers Hash function for a set of small integers Date: Fri, 23 Apr 2010 15:47:38 -0700 (PDT) Organization: http://groups.google.com Message-ID: <0590cf17-12ea-401d-9dc3-02365139b37e@p35g2000prf.googlegroups.com> References: <50701baa-7c05-450c-a42d-c699516ddc00@t14g2000prm.googlegroups.com> NNTP-Posting-Host: 208.91.1.10 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1272062858 6379 127.0.0.1 (23 Apr 2010 22:47:38 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 23 Apr 2010 22:47:38 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: p35g2000prf.googlegroups.com; posting-host=208.91.1.10; posting-account=iokpWwkAAAC4FdoU8cY5F_WfowBALHIE User-Agent: G2/1.0 X-HTTP-Via: 1.1 techops-proxy03.eng.vmware.com:3128 (squid/2.7.STABLE6) X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 GTBDFff GTB7.0,gzip(gfe) Xref: g2news1.google.com comp.lang.ada:10176 Date: 2010-04-23T15:47:38-07:00 List-Id: On Apr 23, 2:39=A0pm, Simon Wright wrote: > Michael R writes: > > Hi Folks, > > > I'd like to use the generic Hashed_Maps container to store mappings > > from a set of small integers (three) to Wide_Strings > > (Indefinite_Hashed_Maps): > > > (1, 10, 4) =3D> "ABC", > > (10, 3, 5) =3D> "XYZ", > > > Does anyone have recommendations on how best to implement the Hash > > function for keys like this? > > 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}: > > =A0 =A0function Instance_Hash (I : Instance) return Natural is > =A0 =A0 =A0 type M is mod 2**31; > =A0 =A0 =A0 Result : M :=3D 0; > =A0 =A0begin > =A0 =A0 =A0 Result :=3D Result xor M (I.A mod 2**31); > =A0 =A0 =A0 Result :=3D Result * 10019; > =A0 =A0 =A0 Result :=3D Result xor M (I.B mod 2**31); > =A0 =A0 =A0 Result :=3D Result * 10019; > =A0 =A0 =A0 Result :=3D Result xor M (I.C mod 2**31); > =A0 =A0 =A0 Result :=3D Result * 10019; > =A0 =A0 =A0 return Natural (Result); > =A0 =A0end Instance_Hash; > > I believe the 10019 came from Knuth, but can't see a reference. Hi, 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 :=3D Result xor M'Mod (I.A); compiled OK. Take care, Michael.