Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ada Enumerated Type Range

Tags:

enums

ada

As I understand it, Ada uses 0 based indexes on its enumerated types.. So in Status_Type below, the ordinal value goes from 0 to 5.

   type Status_Type is
     (Undefined,  
      Available,
      Fout,     
      Assigned,     
      Effected,  
      Cleared); 

My question is.. what are the ordinal values for the following examples? Do they start at 0 or do they start from the ordinal value from the super type?

   subtype Sub_Status_Type is Status_Type
     range Available.. Effected;

   subtype Un_Status_Type is Sub_Status_Type
     range Fout .. Assigned;

Would Sub_Status_Type ordinal values go from 1 to 4 or from 0 to 3?

Would Un_Status_Type ordinal values go from 3 to 4 or from 1 to 2 or from 0 to 1?

like image 264
mainstringargs Avatar asked Sep 07 '11 17:09

mainstringargs


4 Answers

For the subtypes, a 'pos will return the same value as it would have for the base type (1..4 and 2..3 respectively, I believe). Subtypes aren't really new and different types, so much as they are the same old type, but with some extra limitations on its possible values.

But it should be noted that these values are assigned under the scenes. It really should make no difference to you what they are, unless you are using the 'val and 'pos attributes, or you are interfacing to code written outside of Ada (or to hardware).

Plus, if it does end up mattering, you should know that the situation is actually much more complicated. 'pos and 'val don't return the actual bit value the compiler uses for those enumeration values when it generates code. They just return their "ordinal position"; their offset from the first value.

By default they will usually be the same thing. However, you can change the value assignments (but not the ordinal position assignments) yourself with a for ... use clause, like in the code below:

for Status_Type use
 (Undefined => 1,  
  Available => 2,
  Out       => 4,     
  Assigned  => 8,     
  Effected  => 16,  
  Cleared   => 32); 
like image 69
T.E.D. Avatar answered Sep 30 '22 19:09

T.E.D.


The position number is defined in terms of the base type. So Sub_Status_Type'Pos(Assigned) is the same as Status_Type'Pos(Assigned), and the position values of Sub_Status_Type go from 1 to 4, not 0 to 3.

(And note that the position number isn't affected by an enumeration representation clause; it always starts at 0 for the first value of the base type.)

Incidentally, it would have been easy enough to find out by running a small test program that prints the values of Sub_Status_Type'Pos(...) -- which would also have told you that you can't use the reserved word out as an identifier.

like image 40
Keith Thompson Avatar answered Sep 30 '22 17:09

Keith Thompson


As I understand it, Ada uses 0 based indexes on its enumerated types

Yes, it uses 0 for the indexes, or rather for the position of the values of the type. This is not the value of the enumeration literals, and not the binary representation of them.

what are the ordinal values for the following examples?

There are no "ordinal" values. The values of the type are the ones you specified. You are confusing "value", "representation", and "position" here.

The values of your Status_Type are Undefined, Available, Out, Assigned, Effected, and Cleared.

The positions are 0, 1, 2, 3, 4, and 5. These are what you can use to translate with 'Pos and 'Val.

The representation defaults to the position, but you can freely assign other values (as long as you keep the correct order). These are used if you write it to a file, or send it through a socket, or load it into a register..

like image 27
Rommudoh Avatar answered Sep 30 '22 17:09

Rommudoh


I think the best way to answer your questions is in reverse:

A subtype is, mathematically speaking, a continuous subset of its parent type. So, if the type SIZES is (1, 2, 3, 4, 5, 6, 7, 8) and you define a subtype MEDIUM as (4,5) the first element of MEDIUM is 4. Example:

Type Small_Natural is 0..16;
Subtype Small_Positive is Small_Natural'Succ(Small_Natural'First)..Small_Natural'Last;

This defines two small sets of possible-values, which are tightly related: namely that Positive numbers are all the Natural Numbers save Zero.

I used this form to illustrate that with a few text-changes we have the following example:

Type Device is ( Not_Present, Power_Save, Read, Write );
Subtype Device_State is Device'Succ(Device'First)..Device'Last;

And here we are modeling the intuitive notion that a device must be present to have a state, but note that the values in the subtype ARE [exactly] the values in the type from which they are derived.

This answers your second question: Yes, an element of an enumeration would have the same value that its parent-type would.

As to the first, I believe the starting position is actually implementation defined (if not then I assume the LM defaults it to 0). You are, however free to override that and provide your own numbering, the only restriction being that elements earlier in the enumeration are valued less than the value that you are assigning currently [IIRC].

like image 34
Shark8 Avatar answered Sep 30 '22 19:09

Shark8