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=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI autolearn=unavailable autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,32ba5f24e70f4e55 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-05-07 12:20:08 PST Path: newsfeed.google.com!newsfeed.stanford.edu!news.tele.dk!213.56.195.71!fr.usenet-edu.net!usenet-edu.net!enst!enst.fr!not-for-mail From: Marius Amado Alves Newsgroups: comp.lang.ada Subject: Re: accesing internal codes used for an enumeration Date: Mon, 7 May 2001 21:18:35 +0000 (GMT) Organization: ENST, France Sender: comp.lang.ada-admin@ada.eu.org Message-ID: Reply-To: comp.lang.ada@ada.eu.org NNTP-Posting-Host: marvin.enst.fr Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Trace: avanie.enst.fr 989263206 4502 137.194.161.2 (7 May 2001 19:20:06 GMT) X-Complaints-To: usenet@enst.fr NNTP-Posting-Date: Mon, 7 May 2001 19:20:06 +0000 (UTC) To: comp.lang.ada@ada.eu.org Return-Path: X-Sender: alves@lux38 In-Reply-To: Errors-To: comp.lang.ada-admin@ada.eu.org X-BeenThere: comp.lang.ada@ada.eu.org X-Mailman-Version: 2.0.3 Precedence: bulk List-Help: List-Post: List-Subscribe: , List-Id: comp.lang.ada mail<->news gateway List-Unsubscribe: , List-Archive: Errors-To: comp.lang.ada-admin@ada.eu.org X-BeenThere: comp.lang.ada@ada.eu.org Xref: newsfeed.google.com comp.lang.ada:7283 Date: 2001-05-07T21:18:35+00:00 > I am trying to implement a lexicon (in the database sense) as an > enumeration with a representation clause. > > Example lexicon: > > Name <-> Code > ------------- > Ping 123 > Pong 456 > > Implementation: > > type Names is (Ping, Pong); > for Names use (Ping => 123, Pong => 456); > > I found out the hardway that attribute Pos does not return the internal > code. The ARM says this at 13.4(11), and tells you to use > Unchecked_Conversion instead--but does not instruct you on the types of > the conversion. Where might one learn that? Thanks. Replying to myself. Spent some time on it, so perhaps it is also a result of some value to you. It is tested. So we have: type Names is (Ping, Pong); Now a representation type i.e. a type for the internal codes must be set up e.g. type Codes is Integer range 0..999; Then the representation clause for Names'Size use Codes'Size; provides for clean instantiantions of the conversion functions: function Code is Unchecked_Conversion( Source => Names, Target => Codes); function Name is Unchecked_Conversion( Source => Codes, Target => Names); (Integer could be used for Target above, but in that case the compiler may--most correctly--warn about differing sizes.) The representation clause above also ensures at compile time (I think) that the actual internal codes are good: for Names use (Ping => 123, Pong => 456); Or, just to be sure: for Names use (Ping => Codes(123), Pong => Codes(456)); So now we can go from Names to Codes e.g. Ping_Code := Code(Ping); -- Ping_Code is 123 and vice-versa e.g. Ping_Name := Name(123); -- Ping_Name is of course PING And voila, a lexicon with no arrays, sparse or otherwise :-) Throw in an efficiency comparison with the latter idiom and it's Ada Letters stuff: any volunteer for coauthoring this part? ;-) -- Marius Amado Alves alves@systran.lu Project Tradaut-Pt Systran Luxembourg 12, Rue de Vianden L-2680 LUXEMBOURG Tel 352 + 45 46 53 Fax 352 + 45 74 75 Mob 351 +939354002