From: Simon Wright <simon@pushface.org>
Subject: Re: New to Ada need help implementing Warshall's algorithm
Date: Mon, 26 Sep 2016 21:40:50 +0100
Date: 2016-09-26T21:40:50+01:00 [thread overview]
Message-ID: <lya8eu5r8t.fsf@pushface.org> (raw)
In-Reply-To: d30f3527-8ffb-48c4-9118-99e8eb153dc1@googlegroups.com
Stephen Leake <stephen_leake@stephe-leake.org> writes:
> On Friday, September 23, 2016 at 10:07:53 AM UTC-5, James Brewer wrote:
>>
>> It is three diffent sets of data. I think I may try to simplify the
>> input data to a 0,1 2d array for each set, read the set in and then
>> process each set writing it to an output file (the last two I will
>> have to figure out).
>
> Here's a working program that does the IO, to get you started. It
> reads from a file, writes to standard out.
And here's a different one, which works out the size of the matrix by
finding the number of unique names in the input. The structure supports
strings for names, but the implementation only handles single-character
names because I was being lazy.
Input:
e b
a b
b c
b d
d a
d e
c e
Output:
a 3
b 2
c 4
d 5
e 1
01000
00011
01000
10000
10100
Code:
with Ada.Command_Line;
with Ada.Containers.Indefinite_Ordered_Maps;
with Ada.Text_IO; use Ada.Text_IO;
procedure Brewer is
File : File_Type;
package Names_To_Indices
is new Ada.Containers.Indefinite_Ordered_Maps (String, Positive);
Names : Names_To_Indices.Map;
procedure Add_If_New (Name : String)
is
use type Ada.Containers.Count_Type;
begin
if not Names.Contains (Name) then
Names.Insert (Name, Positive (Names.Length + 1));
end if;
end Add_If_New;
begin
Open (File, In_File, Ada.Command_Line.Argument (1));
-- each line is to have format Name_1 Name_2, implying there is a
-- path from Name_1 to Name_2.
loop
begin
declare
Line : constant String := Get_Line (File);
begin
-- I'm being lazy here and assuming single-character Names.
Add_If_New (Line (1 .. 1));
Add_If_New (Line (3 .. 3));
end;
exception
when others => exit;
end;
end loop;
Reset (File);
-- output name-to-index (ordered by name, not index, sorry)
for J in Names.Iterate loop
Put_Line (Names_To_Indices.Key (J)
& " "
& Names_To_Indices.Element (J)'Img);
end loop;
-- now we know the size of the matrix
declare
Size : constant Positive := Positive (Names.Length);
Matrix : array (1 .. Size, 1 .. Size) of Boolean
:= (others => (others => False));
begin
-- populate the matrix
loop
begin
declare
Line : constant String := Get_Line (File);
From : constant Positive := Names.Element (Line (1 .. 1));
To : constant Positive := Names.Element (Line (3 .. 3));
begin
Matrix (From, To) := True;
end;
exception
when others => exit;
end;
end loop;
Close (File);
-- dump the matrix
for J in Matrix'Range (1) loop
for K in Matrix'Range (2) loop
Put (if Matrix (J, K) then '1' else '0');
end loop;
New_Line;
end loop;
end;
end Brewer;
next prev parent reply other threads:[~2016-09-26 20:40 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-09-21 22:05 New to Ada need help implementing Warshall's algorithm James Brewer
2016-09-23 4:31 ` Shark8
2016-09-23 6:26 ` Simon Wright
2016-09-23 15:07 ` James Brewer
2016-09-25 16:06 ` Stephen Leake
2016-09-26 20:40 ` Simon Wright [this message]
2016-09-23 14:54 ` James Brewer
2018-02-12 17:45 ` Lucretia
2016-09-26 17:38 ` James Brewer
2016-09-26 18:29 ` Stephen Leake
2018-02-12 15:36 ` jre11712
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox