comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <mheaney@on2.com>
Subject: Re: List Container Strawman 1.4
Date: Wed, 2 Jan 2002 19:09:52 -0500
Date: 2002-01-02T19:09:52-05:00	[thread overview]
Message-ID: <u3782cper6a931@corp.supernews.com> (raw)
In-Reply-To: 3C2EB518.5040805@mail.com


"Hyman Rosen" <hyrosen@mail.com> wrote in message
news:3C2EB518.5040805@mail.com...
> The generic copy seems good, but all of that address manipulation seems
> very un-Adalike to me (and I'm not an Ada programmer!).

Yes, that's true, although the address manipulation is confined to a single
module.

However, I thought about the problem some more and realized that you can
generalize the copy algorithm to work for any kind of array (so that the
elements don't have to be aliased) and to any container type.

Here's the spec for Generic_Copy:
generic

   type Source_Type (<>) is private;


   type Target_Type (<>) is limited private;


   with function Succ

      (Source : Source_Type) return Source_Type is <>;

   with procedure Copy

      (Source : in Source_Type;

      Target : in out Target_Type) is <>;

procedure Generic_Copy

  (First : in Source_Type;

   Back : in Source_Type;

   Target : in out Target_Type);

The idiom for copying an array to a list would look like:

declare

   Items : constant Integer_Array := (1, 2, 3, 4, 5);

   List : List_Type;

   procedure Copy
     (Source : in     Positive;
      Target : in out Iterator_Type) is
      pragma Warnings (Off, Target);
   begin
      Push_Back (List, Items (Source));
   end;

   procedure Copy is
      new Generic_Copy
            (Positive,
             Iterator_Type,
             Integer'Succ);

   Iter : Iterator_Type;

begin

   Copy (Items'First, Items'First + Items'Length, Iter);

end;



The idiom for copying a list to an array is:

   declare
      Items : array (1 .. Length (List)) of Integer;

      procedure Copy
        (Source : in     Iterator_Type;
         Target : in out Positive) is
      begin
         Items (Target) := Item (Source);
         Target := Target + 1;
      end;

      procedure Copy is
         new Generic_Copy
               (Iterator_Type,
                Positive);

      Index : Positive := Items'First;
   begin
      Copy (First (List), Back (List), Index);
   end;



The fact that you can declare subprograms locally means you don't need
STL-style insert iterators, because you can just refer to the container
object directly.  And using the index subtype as the array iterator means
you can use any kind of array, irrespective of whether it is constrained, or
its items aliased.

If you're curious, the body of Generic_Copy is:

procedure Generic_Copy
  (First  : in     Source_Type;
   Back   : in     Source_Type;
   Target : in out Target_Type) is

   Source : Source_Type := First;

begin

   while Source /= Back loop
      Copy (Source, Target);
      Source := Succ (Source);
   end loop;

end Generic_Copy;








  reply	other threads:[~2002-01-03  0:09 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-12-13  3:23 List Container Strawman 1.4 Ted Dennison
2001-12-13 18:11 ` Brian Hanson
2001-12-13 23:02 ` Nick Roberts
2001-12-14 15:19   ` Ted Dennison
2001-12-14 23:54     ` Ted Dennison
2001-12-15  2:06       ` Server - tasking and long lived connections Eric Merritt
2001-12-15  3:10         ` James Rogers
2001-12-15 12:10           ` Florian Weimer
2001-12-15 14:38         ` Larry Kilgallen
2001-12-15 16:51         ` Steve Doiel
2001-12-17  9:15         ` Thierry Lelegard
2001-12-17  9:34           ` Jean-Pierre Rosen
2001-12-17 10:16             ` Thierry Lelegard
2001-12-18  9:08               ` Jean-Pierre Rosen
2001-12-17 15:08             ` Larry Kilgallen
2001-12-17 15:39               ` Pat Rogers
2001-12-19 18:20         ` Matthew Heaney
2001-12-19 18:50           ` Eric Merritt
2001-12-15  1:20     ` List Container Strawman 1.4 Nick Roberts
2001-12-15 20:29       ` Ted Dennison
2001-12-16 18:45         ` Nick Roberts
2001-12-21 15:53           ` Ted Dennison
2001-12-21 16:42             ` Marin David Condic
2001-12-21 18:28               ` Ted Dennison
2001-12-21 18:47                 ` Marin David Condic
2001-12-21 19:39                   ` Ted Dennison
2001-12-21 19:48                     ` Marin David Condic
2001-12-22 12:29                     ` Simon Wright
2001-12-21 20:03                   ` Nick Roberts
2001-12-21 16:52             ` Marin David Condic
2001-12-21 18:41               ` Ted Dennison
2001-12-21 19:14                 ` Marin David Condic
2001-12-21 21:13                   ` Ted Dennison
2001-12-22  5:34                     ` John B. Matthews
2001-12-21 20:19                 ` Stephen Leake
2001-12-21 21:35                   ` Ted Dennison
2001-12-24 11:58               ` Florian Weimer
2001-12-24 14:42                 ` Eric Merritt
2001-12-24 22:47                 ` Ted Dennison
2001-12-25 22:15                   ` Florian Weimer
2001-12-28 13:58                     ` Ted Dennison
2001-12-21 17:43             ` Stephen Leake
2001-12-21 18:44               ` Ted Dennison
2001-12-16 21:53         ` Larry Hazel
2001-12-15 22:27           ` Ted Dennison
2001-12-16  4:32             ` Darren New
2001-12-24 13:53               ` Florian Weimer
2001-12-15 23:19 ` Florian Weimer
2001-12-16  4:46   ` Ted Dennison
2001-12-24 13:57     ` Florian Weimer
2001-12-28 14:00       ` Ted Dennison
2001-12-28 16:43         ` Hyman Rosen
2001-12-28 19:12           ` Nick Roberts
2001-12-28 19:49           ` Matthew Heaney
2001-12-29 23:23             ` Matthew Heaney
2001-12-30  6:31               ` Hyman Rosen
2002-01-03  0:09                 ` Matthew Heaney [this message]
2002-01-03  0:20                   ` Brian Rogoff
2001-12-17  8:34   ` Mark Lundquist
2001-12-18 21:56     ` Florian Weimer
2001-12-18 21:54       ` Larry Kilgallen
2001-12-18 22:34       ` Mark Lundquist
2001-12-19  4:03         ` Nick Roberts
2001-12-24 13:54           ` Florian Weimer
replies disabled

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