From: Marius Amado Alves <alves@systran.lu>
To: comp.lang.ada@ada.eu.org
Subject: Re: accesing internal codes used for an enumeration
Date: Mon, 7 May 2001 21:18:35 +0000 (GMT)
Date: 2001-05-07T21:18:35+00:00 [thread overview]
Message-ID: <mailman.989263205.30989.comp.lang.ada@ada.eu.org> (raw)
In-Reply-To: <Pine.LNX.4.21.0105071543050.9145-100000@lux38>
> 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
next parent reply other threads:[~2001-05-07 21:18 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <Pine.LNX.4.21.0105071543050.9145-100000@lux38>
2001-05-07 21:18 ` Marius Amado Alves [this message]
2001-05-10 18:45 accesing internal codes used for an enumeration Beard, Frank
-- strict thread matches above, loose matches on Subject: below --
2001-05-07 18:53 Beard, Frank
2001-05-08 5:56 ` Jacob Sparre Andersen
2001-05-07 16:03 Marius Amado Alves
2001-05-07 15:33 ` Jacob Sparre Andersen
2001-05-07 15:54 ` Ted Dennison
2001-05-08 3:51 ` Marc A. Criley
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox