comp.lang.ada
 help / color / mirror / Atom feed
From: Rob Solomon <usenet@drrob1-noreply.com>
Subject: Table of pointers question
Date: Wed, 23 Sep 2009 20:47:56 -0400
Date: 2009-09-23T20:47:56-04:00	[thread overview]
Message-ID: <utflb5p8871f6p2531cr81tkdteacm786a@4ax.com> (raw)

I am working my way thru Ada As A Second Language by Norman Cohen (c)
1996

This confuses me.

It is a simple sorting routine that swaps pointers rather than the
data.  Note that the variables are more like Modula-2 syntax as I am
very comfortable with that.  And it is easier to type.

with Ada.Text_IO, Ada.Strings.Bounded; use Ada.Text_IO;
procedure PrintDirectory is
  maxLineLength : constant := 80;
  maxEntries    : constant := 1000;
  
  package InputLines is new Ada.Strings.Bounded.Generic_Bounded_Length
(MaxLineLength);
  
  use InputLines;
  
  type DirectoryEntryType is
    record
      NamePart, StreetPart, CityPart: Bounded_String;
    end record;
  
  type DirectoryEntryPointerType is access DirectoryEntryType;
  
  NewEntry        : DirectoryEntryPointerType;
  Buffer          : String (1..MaxLineLength);
  Length          : Integer range 0..MaxLineLength;
  OutOfPlaceEntry : DirectoryEntryPointerType;
  NumberOfEntries : Integer range 0..MaxEntries := 0;
  No_Exchanges    : Boolean;
  
  EntryList        : array (1..MaxEntries) of DirectoryEntryType;
  EntryPointerList : array (1..MaxEntries) of
DirectoryEntryPointerType;
  
begin
  -- read data into entry list
  WHILE NOT End_Of_File LOOP
    Get_Line(Buffer, length);
    NewEntry.NamePart := To_Bounded_String(Buffer(1..Length));
    Get_Line(Buffer, Length);
    NewEntry.StreetPart := To_Bounded_String(Buffer(1..Length));
    Get_Line(Buffer, Length);
    NewEntry.CityPart := To_Bounded_String(Buffer(1..Length));
    NumberOfEntries := NumberOfEntries + 1;
    EntryPointerList(NumberOfEntries) := new
DirectoryEntryType'(NewEntry);  -- MY QUESTION HERE
  END LOOP;
  
  -- sort entryList using bubble sort
  
  LOOP
    No_Exchanges := True;
    FOR I in 1 .. NumberOfEntries - 1 LOOP
      IF EntryPointerList(I).NamePart > EntryPointerList(I+1).NamePart
THEN
        OutOfPlaceEntry := EntryPointerList(I+1);
        EntryPointerList(I+1) := EntryPointerList(I);
        EntryPointerList(I) := OutOfPlaceEntry;
        No_Exchanges := False;
      END IF:
    END LOOP;
    EXIT WHEN No_Exchanges;
  END LOOP;
  
  -- write sorted data
  FOR I in 1 .. NumberOfEntries LOOP
    Put_Line(To_String(EntryPointerList(I).NamePart));
    Put_Line(To_String(EntryPointerList(I).StreetPart));
    Put_Line(To_String(EntryPointerList(I).CityPart));
  END LOOP;


My question is that I would expect to have an array that contains the
data, and a second array that is an array of pointers to the 1st
array.  The example does not define that.

How does the line:
EntryPointerList(NumberOfEntries) := new DirectoryEntryType'(NewEntry)
;

Do what's needed?

Thanks



             reply	other threads:[~2009-09-24  0:47 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-24  0:47 Rob Solomon [this message]
2009-09-24  1:34 ` Table of pointers question Adam Beneschan
2009-09-24  6:39   ` tmoran
2009-09-25 16:52   ` björn lundin
2009-09-25 17:12     ` Adam Beneschan
2009-09-24  2:00 ` (see below)
2009-09-24  3:46 ` Jeffrey R. Carter
2009-09-24  6:59 ` Georg Bauhaus
2009-09-24  7:06   ` Georg Bauhaus
2009-09-24 14:55   ` Adam Beneschan
2009-09-26 13:45     ` Rob Solomon
2009-09-24  6:59 ` Stephen Leake
2009-09-26 13:50   ` Rob Solomon
     [not found] ` <3cadnZif2YjGbyfXnZ2dnUVZ_tmdnZ2d@earthlink.com>
2009-09-24 12:49   ` Robert A Duff
2009-09-26 13:36   ` Rob Solomon
2009-09-26 14:51     ` John B. Matthews
     [not found]     ` <3YSdnY7SXPNd_yPXnZ2dnUVZ_sydnZ2d@earthlink.com>
2009-09-26 18:58       ` Rob Solomon
2009-09-26 21:00       ` Georg Bauhaus
2009-09-27  5:53     ` Stephen Leake
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox