Im studying how far I can go using expression functions to have everything defined in specification package.
My goal is simple, I have an enumeration that defines a set of 5 possible contents
type Enum is (A1, A2, A3, A4, A5);
I define a possible specific content implementation where only 2 of 5 are active:
type TableType is array (Natural range <>) of Natural;
Table : constant TableType :=
(1 => A3,
2 => A5
);
From someone that call this package with Enum as argument I just can make a loop in the table to find the element of enum. But I want to make a lookup table that maps directly for save CPU.
I start with a lookup table where is possible to know wich element of the enumeration is active in this implementation. I was successfull with this expression function:
type WhoisactiveType is array (Enum) of boolean;
function isActive (elem : Enum) return boolean is
(for some I in Table'Range => (elem = Table(I)));
WhoisActive : WhoisactiveType := [ for I in Enum'Range => (isActive (I))];
But I didnt find the way to make the lookup table to know which internal index of the table are used for every element of the enum. I’v tried without success:
function Convert (elem : Enum) return Natural is
(for I in Table'Range => (if elem = Table(I) then I else 0));
Map : MapConvertionType := [for I in Enum'Range => (Convert (I))];
Of course, I was able to do all of this in the body. But the goal is to try to do in the specification package. For the body I just did this:
function Convert (elem : Enum) return Natural is
begin
for I in Table'Range loop
if table(I) = elem then
return I;
end if;
end loop;
return 0;
end Convert;
procedure InitMap is
begin
for I in Map'Range loop
Map(I) := Convert(I);
end loop;
end InitMap;
¿Does anybody knows how to build this simple lookup table in the specification package?
PD: I also tried to make Map expression function that work like this:
function Map (elem: Enum) return Natural is
(case elem is
when A3 =>1,
when A5 =>2,
when others =>0
);
But the this solution requires two parallel configurations for the same thing (Table variable content and Map function content) that could drive to possible mistakes.