comp.lang.ada
 help / color / mirror / Atom feed
* Re: accesing internal codes used for an enumeration
  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
  2 siblings, 0 replies; 8+ messages in thread
From: Jacob Sparre Andersen @ 2001-05-07 15:33 UTC (permalink / raw)


Marius:

> 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.

It can be read from the LRM, although I think it could be formulated
a bit clearer.

> 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.

You should declare an integer type that is suitable for the mapping.
An idea for the implementation:

   for Names'Size use 16;

   type Name_Indices is range 0 .. 2**16 - 1;
   for Name_Indices'Size use 16;

   function To_Index (Name : Names) return Name_Indices is
     new Ada.Unchecked_Conversion (Source => Names,
                                   Target => Name_Indices);

Jacob
-- 
"... det er ul�kkert, at man selektivt vil t�mme
 ulande/�steuropa for velkvalificerede mennesker."
 Christian Mikkelsen



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: accesing internal codes used for an enumeration
  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
  2 siblings, 0 replies; 8+ messages in thread
From: Ted Dennison @ 2001-05-07 15:54 UTC (permalink / raw)


In article <mailman.989244246.26083.comp.lang.ada@ada.eu.org>, Marius Amado
Alves says...
>
>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);

What this does is tell the compiler to use 123 as the actual bit pattern that
represents "Ping" when it is stored in memory (or perhaps on disk). There is no
way to get at that value, short of converting it to something else in an
"unchecked" manner. 

If all you want to do is make force a specific bit pattern on those objects when
they are stored, then this is the way to do it. If instead you just want a
mapping that you can use internally in your program, you would probably be
better off doing something like the following:

type Names is (Ping, Pong);
type Name_Integer_Mapping is array (Names) of Integer;
Name_Map : constant Name_Integer_Mapping := (Ping => 123, Pong => 456);


---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



^ permalink raw reply	[flat|nested] 8+ messages in thread

* accesing internal codes used for an enumeration
@ 2001-05-07 16:03 Marius Amado Alves
  2001-05-07 15:33 ` Jacob Sparre Andersen
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Marius Amado Alves @ 2001-05-07 16:03 UTC (permalink / raw)
  To: comp.lang.ada

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.

/* The point of a lexicon is to access its elements by either key.  I
expect the implementation above would be eficient.  It is already simple
to declare/define.  The alternative is of course to use arrays, but then a
search procedure is needed.  I also wanted to avoid this complication. */

-- 
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





^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: accesing internal codes used for an enumeration
@ 2001-05-07 18:53 Beard, Frank
  2001-05-08  5:56 ` Jacob Sparre Andersen
  0 siblings, 1 reply; 8+ messages in thread
From: Beard, Frank @ 2001-05-07 18:53 UTC (permalink / raw)
  To: 'comp.lang.ada@ada.eu.org'

I'm not sure where it is, other than the description of enumerated
types.  Since enumerated types map to an underlying integer
representation, your Uncheck_Conversion will be from the enumerated
type to an integer type (defined large enough to hold the all the
possible numbers).  If you wanted to be lazy, you could take the
basic Standard Integer (-2**32-1 .. (2**32-1) - 1).

In which case, you would have the following:

  type Names is (Ping, Pong);
  for Names use (Ping => 123, Pong => 456);

  function To_Integer is new
     Ada.Unchecked_Conversion(Source => Names,
                              Target => integer);

  name : Names := Names'first;
  x    : integer := 0;

begin

  x := To_Integer(name);  -- x is now 123


Hope this helps.
Frank

-----Original Message-----
From: Marius Amado Alves [mailto:alves@systran.lu]
Sent: Monday, May 07, 2001 12:03 PM
To: comp.lang.ada@ada.eu.org
Subject: accesing internal codes used for an enumeration


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.

/* The point of a lexicon is to access its elements by either key.  I
expect the implementation above would be eficient.  It is already simple
to declare/define.  The alternative is of course to use arrays, but then a
search procedure is needed.  I also wanted to avoid this complication. */

-- 
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


_______________________________________________
comp.lang.ada mailing list
comp.lang.ada@ada.eu.org
http://ada.eu.org/mailman/listinfo/comp.lang.ada




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: accesing internal codes used for an enumeration
       [not found] <Pine.LNX.4.21.0105071543050.9145-100000@lux38>
@ 2001-05-07 21:18 ` Marius Amado Alves
  0 siblings, 0 replies; 8+ messages in thread
From: Marius Amado Alves @ 2001-05-07 21:18 UTC (permalink / raw)
  To: comp.lang.ada

> 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





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: accesing internal codes used for an enumeration
  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
  2 siblings, 0 replies; 8+ messages in thread
From: Marc A. Criley @ 2001-05-08  3:51 UTC (permalink / raw)


Marius Amado Alves wrote:
> 
> 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.

If you're using GNAT and don't mind going non-portable, GNAT provides
the 'Enum_Rep attribute to extract the specified enumeration
representation.  (See the GNAT Reference Manual.)

Marc A. Criley
Senior Staff Engineer
Quadrus Corporation
www.quadruscorp.com



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: accesing internal codes used for an enumeration
  2001-05-07 18:53 Beard, Frank
@ 2001-05-08  5:56 ` Jacob Sparre Andersen
  0 siblings, 0 replies; 8+ messages in thread
From: Jacob Sparre Andersen @ 2001-05-08  5:56 UTC (permalink / raw)


Frank:

> I'm not sure where it is, other than the description of enumerated
> types.  Since enumerated types map to an underlying integer
> representation, your Uncheck_Conversion will be from the enumerated
> type to an integer type (defined large enough to hold the all the
> possible numbers).

For an unchecked conversion to work, the sizes have to match
exactly!

Jacob
-- 
Warning: Dates in calendars are closer than they appear.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: accesing internal codes used for an enumeration
@ 2001-05-10 18:45 Beard, Frank
  0 siblings, 0 replies; 8+ messages in thread
From: Beard, Frank @ 2001-05-10 18:45 UTC (permalink / raw)
  To: 'comp.lang.ada@ada.eu.org'

> For an unchecked conversion to work, the sizes have to match
> exactly!

Not on DEC Ada (or Compaq Ada, whatever it's called).

But, that's what I meant by "(defined large enough to hold
the all the possible numbers)".  Re-reading it I can see
that it's unclear.  I tend to be terse, but I meant the
size of the integer (8, 16, 32, etc.) has to be big enough
for the values used in the underlying representation.  And
yes they have to be the same size (for portability).  I
was taking that as a given (or implied), but thanks for
making it clearer.

Frank

-----Original Message-----
From: Jacob Sparre Andersen [mailto:sparre@nbi.dk]
Sent: Tuesday, May 08, 2001 1:57 AM
To: comp.lang.ada@ada.eu.org
Subject: Re: accesing internal codes used for an enumeration


Frank:

> I'm not sure where it is, other than the description of enumerated
> types.  Since enumerated types map to an underlying integer
> representation, your Uncheck_Conversion will be from the enumerated
> type to an integer type (defined large enough to hold the all the
> possible numbers).

For an unchecked conversion to work, the sizes have to match
exactly!

Jacob
-- 
Warning: Dates in calendars are closer than they appear.
_______________________________________________
comp.lang.ada mailing list
comp.lang.ada@ada.eu.org
http://ada.eu.org/mailman/listinfo/comp.lang.ada




^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2001-05-10 18:45 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-05-10 18:45 accesing internal codes used for an enumeration Beard, Frank
     [not found] <Pine.LNX.4.21.0105071543050.9145-100000@lux38>
2001-05-07 21:18 ` Marius Amado Alves
  -- 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox