Re: Help needed for ada package



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.

.



Relevant Pages

  • Re: RunCommand acCmdRecordsGoToNext
    ... Another thing that may come in handy if that doesn't is to loop through the ... The database I am utilizing this ... increasing intRecordCount on each loop. ... Dim strPassword As String ...
    (microsoft.public.access.forms)
  • Re: Sql Connection
    ... ..i dont like the code either....again a example of a loop ... making calls to the database .... ... what i did yesterday is just get the parsed data to a temp table on the ... the question is the open connection where the Datareader is used insid ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: View contents of a folder
    ... For example, if your backup files are located on your D drive, in the backup ... to loop through that directory looking for other files that match the ... allows you to copy the backup file over your backend database, ... >> Folder browser and let you select a file via that mechanism. ...
    (microsoft.public.access.queries)
  • Re: How to Delete Rows in Excel In a Do Loop
    ... have to delete there are some that have a entry in each and every cell ... Richard Buttrey wrote: ... time you go through the loop. ... Now copy this formula down column A for the whole database ...
    (microsoft.public.excel.worksheet.functions)
  • Re: Converting from Access to SQL2k via Excel- HELP!!!!!
    ... Then create a new query and paste the following into its SQL ... > In your database you'd need to start with a query that ... The function that you pointed me to allows me to concatenate all ... The problem that I have is that I need to put it into a loop ...
    (microsoft.public.access.conversion)