comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthew_heaney@acm.org>
Subject: Re: "out" or "access"
Date: 1998/11/16
Date: 1998-11-16T00:00:00+00:00	[thread overview]
Message-ID: <m390hcobvl.fsf@mheaney.ni.net> (raw)
In-Reply-To: x7vogqobgyd.fsf@pogner.demon.co.uk

Simon Wright <simon@pogner.demon.co.uk> writes:

> > From time-to-time, it's desirable to change the values of the items in
> > the data structure, without resorting to inefficient "solutions" like
> > copying the original data structure, and changing the source values
> > prior to insertion into the target structure.  You just want to change
> > the values in place.
> 
> Yes, I'm going through the same throes with the Booch Components at
> the moment.
> 
> Having the ability to change components in place during iteration
> causes a whole lot of grief, in the BCs showing up as
> 
> generic
>   type Item is private;
>   type Item_Ptr is access all Item;
> package BC.Containers is


Here are some ideas:

o Just use a passive iterator:

generic
   with procedure Process 
     (Item : in out Item_Type;
      Quit : in out Boolean);
procedure Modify_Items (Stack : in out Stack_Type);


o Use an read-only active iterator, but with a special op that takes the
structure as an access parameter:

type Stack_Iterator is private;

type Stack_Access is access constant Stack_Type;

function Start_At_Top
  (Stack : Stack_Access)
  return Stack_Iterator;

function Set_Item
  (Stack    : access Stack_Type;
   Iterator : in     Stack_Iterator)
   return Item_Access;

Use it like:

   Stack : aliased Stack_Type;
...
   declare
      Iterator : Stack_Iterator := 
         Start_At_Top (Stack'Access);
   begin
      for I in 1 .. Get_Length (Stack) loop
        declare
           Item : Item_Type renames
             Set_Item (Stack'Access, Iterator).all;
        begin
           Item := Item + 1;
        end;
      end loop;
   end;


This is legal Ada, without requiring any "tricks."


o  Use an normally read-only active iterator, but use
Sys.Addr_To_Acc_Conv to convert the read-only view of the structure into
a read-write view:

type Stack_Iterator is private;

type Stack_Access is access constant Stack_Type;

function Start_At_Top
  (Stack : Stack_Access)
  return Stack_Iterator;

but now Set_Item looks like:

function Set_Item
  (Iterator : Stack_Iterator)
   return Item_Access;

The implementation is

function Set_Item 
  (Iterator : Stack_Iterator) 
  return Item_Access is

  Read_Only : Stack_Type renames
     Iterator.Stack.all;

  package Conversions is
    new Sys.Addr_To_Acc_Conv (Stack_Type);
  use Conversions;

  Read_Write : Stack_Type renames
     To_Pointer (Read_Only'Address).all;
begin
  return Read_Write.Items (Iterator.Index)'Access;
end Set_Item;


Now you only have to say:


   Stack : aliased Stack_Type;
...
   declare
      Iterator : Stack_Iterator := 
         Start_At_Top (Stack'Access);
   begin
      for I in 1 .. Get_Length (Stack) loop
        declare
           Item : Item_Type renames
             Set_Item (Iterator).all;
        begin
           Item := Item + 1;
        end;
      end loop;
   end;






  reply	other threads:[~1998-11-16  0:00 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-10-21  0:00 "out" or "access" =:-) Vincent
1998-10-21  0:00 ` Jeff Carter
1998-10-21  0:00   ` Pat Rogers
1998-10-21  0:00     ` Martin C. Carlisle
1998-10-22  0:00       ` Pat Rogers
1998-10-22  0:00     ` Robert A Duff
1998-10-21  0:00 ` Tucker Taft
1998-10-22  0:00   ` Pascal Obry
1998-10-29  0:00     ` Robert A Duff
1998-10-29  0:00       ` Matthew Heaney
1998-10-29  0:00         ` Robert A Duff
1998-10-30  0:00           ` dennison
1998-10-30  0:00             ` Matthew Heaney
1998-10-30  0:00               ` Robert A Duff
1998-10-31  0:00                 ` Matthew Heaney
1998-11-01  0:00                   ` Robert A Duff
1998-11-01  0:00                     ` Matthew Heaney
1998-11-01  0:00                       ` Robert A Duff
1998-11-02  0:00                         ` Matthew Heaney
1998-11-03  0:00                           ` Simon Wright
1998-11-16  0:00                             ` Matthew Heaney [this message]
1998-10-31  0:00                 ` dewar
1998-10-31  0:00                   ` Matthew Heaney
1998-10-21  0:00 ` dennison
replies disabled

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