Re: Help needed for ada package
- From: Ludovic Brenta <ludovic.brenta@xxxxxxxxxx>
- Date: Thu, 28 Jul 2005 20:33:32 +0200
Whoa, everyone seems to be going over the top, I think the solution
can be much simpler.
generic
type ID is (<>); --some discrete type to be put here
package POP is
--update database
type Rating is new Integer range 0..255;
procedure Rate (Citizen : in ID;
Badness : in Rating);
procedure Associate (Citizen_1, Citizen_2 : in ID);
--query the database
function Most_Dangerous return ID;
function Next_Member return ID;
function More_In_Group return Boolean;
--administrative
procedure reset;
end POP;
Since the type ID is discrete, why not just use that as the array index type?
package body POP is
Society : array (ID) of Rating := (others => Rating'First);
Associacion : array (ID, ID) of Boolean := (others => others => False));
pragma Pack (Association);
Last_Reported : ID := ID'First;
procedure Rate (Citizen : in ID;
Badness : in Rating) is
begin
Society (Citizen) := Rating;
end Rate;
procedure Associate (Citizen_1, Citizen_2 : in ID) is
begin
Association (Citizen_1, Citizen_2) := True;
end Associate;
function Most_Dagerous return ID is
Result : ID := ID'First;
begin
for J in Society'Range loop
if Society (J) > Result then
Result := J;
end if;
end loop;
return Result;
end Most_Dangerous;
function Next_Member return ID is
J : constant ID := Most_Dangerous;
begin
if Last_Reported = ID'First then
Last_Reported := J;
end if;
for K in ID'Succ (Last_Reported) .. Association'Last (2) loop
if Association (J, K) then
Last_Reported := K;
exit;
end loop;
end loop;
return Last_Reported;
end Next_Member;
function More_In_Group return Boolean is
J : constant ID := Most_Dangerous;
Result : Boolean := False;
begin
for K in ID'Succ (Last_Reported) .. Association'Last (2) loop
if Association (J, K) then
Result := True;
exit;
end loop;
end loop;
return Result;
end More_In_Group;
procedure Reset is
begin
Society := (others => Rating'First);
Association := (others => others => False));
Last_Reported := ID'First;
end Reset;
end POP;
Of course, the above solution is outrageously inefficient, both
memory- and CPU-wise. Optimisations are left as an exercise to the
reader.
--
Ludovic Brenta.
.
- Follow-Ups:
- Re: Help needed for ada package
- From: tmoran
- Re: Help needed for ada package
- References:
- Help needed for ada package
- From: strictly_mk
- Re: Help needed for ada package
- From: Steve
- Re: Help needed for ada package
- From: strictly_mk@xxxxxxxxxxx
- Re: Help needed for ada package
- From: Steve
- Re: Help needed for ada package
- From: strictly_mk@xxxxxxxxxxx
- Help needed for ada package
- Prev by Date: Re: Range checking not working as expected?
- Next by Date: Re: Help needed for ada package
- Previous by thread: Re: Help needed for ada package
- Next by thread: Re: Help needed for ada package
- Index(es):
Relevant Pages
|