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,14be6619f79b4573 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.megapath.net!news.megapath.net.POSTED!not-for-mail NNTP-Posting-Date: Tue, 14 Jun 2005 15:15:56 -0500 From: "Randy Brukardt" Newsgroups: comp.lang.ada References: Subject: Re: Hashing on System.Address Date: Tue, 14 Jun 2005 15:18:25 -0500 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4927.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4927.1200 Message-ID: <1MadnWME1OZhpzLfRVn-tA@megapath.net> NNTP-Posting-Host: 64.32.209.38 X-Trace: sv3-nlHs0EY9WpUYOqHk+HV5TN+U4y0uExpPuWduURb+yT4ABg1rD1n8cFftTEhM8gAwnxIbwLdrhSoNtjD!Hs+TthiNUiE3mX4lUg8Sq873jZru4zmMHyxhXDHXUOQBl80JZngsa6HxJ/ywXv7/1OUbB7PZCKw5 X-Complaints-To: abuse@megapath.net X-DMCA-Complaints-To: abuse@megapath.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.31 Xref: g2news1.google.com comp.lang.ada:11357 Date: 2005-06-14T15:18:25-05:00 List-Id: "Duncan Sands" wrote in message news:mailman.22.1118733848.17633.comp.lang.ada@ada-france.org... > Hi Mark, > > > You can always convert a value of type System.Address to a value of a > > signed or modular (implementation-defined) type. That should help when > > you want to hash it into a value of type Hash_Type. > > yes that helps - thanks for the suggestion. In fact that is what I'm already > doing. The problem is the next part... That can be simplified to: I have two > modular types (binary modulus), but I don't know their modulus. Values of > the first type should be hashed into values of the second type. This can be > done (I've done it) but somehow the code is much more horrible than I feel > it should be. One annoying point is that a program with a static expression > that would raise Constraint_Error is illegal, even if you would never get to the > Contraint_Error raising part in run-time. If you don't understand the relevance > of this remark, try to code this kind of hash thing yourself and you will soon > see :) That's one of the reasons that Ada 2006 adds the "Mod" attribute. You could use that to discard any extra bits. (If you want to keep them, well, that gets messier.). So I'd use something like: if Integer_Address'Last < Hash_Type'Modulus then return Hash_Type'Mod(Addr); elsif Integer_Address'Last / Hash_Type'Modulus < Hash_Type'Modulus then return Hash_Type'Mod(Addr) xor Hash_Type'Mod(Addr / Hash_Type'Modulus); else raise Program_Error; end if; You could of course continue to handle larger multiples in the same way. I don't remember if those static expressions are illegal because of typing. That can be fixed by making sure the entire expression is of type Universal_Integer: if Integer_Address'Pos(Integer_Address'Last) < Hash_Type'Modulus then return Hash_Type'Mod(Addr); elsif Integer_Address'Pos(Integer_Address'Last) / Hash_Type'Modulus < Hash_Type'Modulus then return Hash_Type'Mod(Addr) xor Hash_Type'Mod(Addr / Hash_Type'Modulus); else raise Program_Error; end if; Randy.