From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Received: by 2002:ae9:e703:: with SMTP id m3mr16408942qka.114.1591486803310; Sat, 06 Jun 2020 16:40:03 -0700 (PDT) X-Received: by 2002:a9d:218a:: with SMTP id s10mr11791440otb.329.1591486802953; Sat, 06 Jun 2020 16:40:02 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!news.gegeweb.eu!gegeweb.org!usenet-fr.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sat, 6 Jun 2020 16:40:02 -0700 (PDT) Complaints-To: groups-abuse@google.com Injection-Info: google-groups.googlegroups.com; posting-host=38.132.120.110; posting-account=Vahg2AoAAAA9l6fZ2tukpIK6QgjG8f76 NNTP-Posting-Host: 38.132.120.110 User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: CONSTRAINT ERROR: erroneous memory access From: jcupak@gmail.com Injection-Date: Sat, 06 Jun 2020 23:40:03 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Xref: reader01.eternal-september.org comp.lang.ada:58992 Date: 2020-06-06T16:40:02-07:00 List-Id: It has been a few years since I have written Ada; I used and taught Ada 95 = back when I was working for a defense contractor. But, now that I'm retired= , I wanted to get up to speed with Ada 2012, so I decided to implement a ma= in program to see how the Shortest_Paths generic package works (taken from = A.18.31, pages 574-576). But, when I read in the data and call the Shortest= _Path function, it returns with CONSTRAINT ERROR: erroneous memory access. = I've tried multiple times, but can't seem to figure out what I'm doing (or = NOT doing) wrong.=20 Here's the main program: with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; with Ada.Float_Text_IO; use Ada.Float_Text_IO; with Ada.Text_IO; use Ada.Text_IO; with Shortest_Paths; with Ada.Command_Line; use Ada.Command_Line; with DirectedEdge; use DirectedEdge; with Ada.Containers; use Ada.Containers; with Ada.Exceptions; procedure Main_Test is Input_File : Ada.Text_IO.File_Type; Vertices : Integer; -- Number of nodes Edges : Integer; -- Number of paths Tail : Integer; -- Node From Head : Integer; -- Node To Weight : Float; -- Path Weight/Distance/Cost -- Instantiate Shortest Paths package with 0..Integer'Last subtype package SP is new Shortest_Paths(Node =3D> Natural); =20 -- Use Edge'Read to read the Edge components into Item =20 -- Display directed edge components procedure Display(Edge : in SP.Edge) is begin Put(Edge.From, Width=3D>1); Put("->"); Put(Edge.To, Width=3D>1); Put(" "); Put(Float(Edge.Length), Fore =3D> 0, Aft =3D> 2, Exp =3D> 0); Put(" "= ); end Display; -- Display directed edge components at cursor -- Replace List'Write with Display procedure Display(Cursor: in SP.Adjacency_Lists.Cursor) is Edge : SP.Edge :=3D SP.Adjacency_Lists.Element(Cursor); begin Display(Edge); -- Let other procedure do all the work end Display; begin -- Open input file using arg 1 Open (File =3D> Input_File, Mode =3D> In_File, Name =3D> Argument(1)); -- ../tinyEWD.txt Set_Input(Input_File); -- Redirect input New_Line; Put("Processing '"); Put(Argument(1)); Put_Line("'"); -- Read number of nodes (vertices) Get(Vertices); New_Line; Put("Vertices: ");Put(Vertices, width=3D>2);New_Line; -- Read number of paths (edges) Get(Edges); Put("Edges: ");Put(Edges, Width=3D>2);New_Line(2); declare =20 -- Constrain Vertex to zero-based subrange subtype Vertex is Natural range 0..Vertices-1; =20 -- Adj is DLL of Adjacency Lists for each Vertex Adj : array (Vertex) of SP.Adjacency_Lists.List; =20 -- Edge is a record of Tail, Head, and Weight components Edge : SP.Edge; =20 begin =20 Put_Line("Creating Adjacency Lists"); New_Line; =20 -- For each node, create empty list of adjacent nodes Create_Empty_Adjacency_Lists: for Node in Vertex loop =20 -- Initialize each adjacency list to empty Adj(Node) :=3D SP.Adjacency_Lists.Empty_List; =20 end loop Create_Empty_Adjacency_Lists; =20 -- Display and append new edge to adjacency list for node -- Constrain Edge index to natural subrange Append_New_Edge: for E in 0..Edges-1 loop =20 Put("Edge: ");Put(E, Width=3D>2);Put(" "); =20 -- Get edge components from data file Get(Tail); -- Tail Get(Head); -- Head Get(Weight); -- Distance/Weight =20 -- Set edge components Edge.From :=3D Tail; Edge.To :=3D Head; Edge.Length :=3D SP.Distance(Weight); =20 -- Display Edge Display(Edge); Put(" Appended to edge ");Put(Tail,1); New_Line; =20 -- Append new edge to From adjacency list -- Indicating path to another node Adj(Edge.From).Append(Edge); =20 end loop Append_New_Edge; New_Line; =20 Close(Input_File); Set_Input(Standard_Input); -- Reset input source =20 Put_Line("Node Adjacency Lists"); =20 -- Display contents of each adjacency list Display_Adjacency_Lists: for Node in Vertex loop =20 Put("Adj[");Put(Node, Width=3D>1);Put("] "); =20 declare Edges : Ada.Containers.Count_Type; begin =20 -- How many edges are in this node? Edges :=3D SP.Adjacency_Lists.Length(Adj(Node)); Put(Integer(Edges), Width =3D> 1); if (Edges > 1) then Put(" Edges: "); else Put(" Edge: "); end if; =20 -- Iterate over all nodes in this adjacency list -- and Display each edge for each node SP.Adjacency_Lists.Iterate(Adj(Node), Process =3D> Display'Acce= ss); =20 end; New_Line; =20 end loop Display_Adjacency_Lists; New_Line; =20 -- Create Edge-Weighted Graph of Node and Adjacency List declare EWG : SP.Graphs.Vector; -- Edge Weighted Graphs Path : SP.Paths.List; -- Shortest Path begin =20 Put_Line("Creating Edge-Weighted Graphs."); EWG :=3D SP.Graphs.Empty_Vector; Put_Line("EWG Empty_Vector created."); =20 Put("Initializing Shortest Path to empty list..."); Path :=3D SP.Paths.Empty_List; Put_Line("done."); =20 for Vertex in 0..Vertices-1 loop Put("Vertex: ");Put(Vertex, Width =3D> 1); EWG.Append(Adj(Vertex)); Put_Line(" appended to EWG."); end loop; New_Line; =20 Put("EWG.Length =3D "); Put(Integer(EWG.Length), Width =3D> 1);New= _Line; =20 Put_Line("Finding shortest path from node 0 to node 6"); declare use Ada.Exceptions; begin -- Compute Shortest Path Path :=3D SP.Shortest_Path (EWG, Source =3D> 0, Target =3D> 6); exception when Error: others =3D> New_Line; Put_Line(Exception_Name(Error)); Put_Line(Exception_Message(Error)); New_Line; Return; end; =20 Put("The Shortest Path from Node 0 to Node 6 "); Put("contains "); Put(Integer(Path.Length)); Put(" entries."); -- Display path end; =20 =20 end; end Main_Test; And here's the test data (taken from Algorithms, by Sedwick): 8 15 4 5 0.35 5 4 0.35 4 7 0.37 5 7 0.28 7 5 0.28 5 1 0.32 0 4 0.38 0 2 0.26 7 3 0.39 1 3 0.29 2 7 0.34 6 2 0.40 3 6 0.52 6 0 0.58 6 4 0.93