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=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,9a7e0c43216f4def X-Google-Attributes: gid103376,public From: Matthew Heaney Subject: Re: "out" or "access" Date: 1998/11/16 Message-ID: #1/1 X-Deja-AN: 412292533 Sender: matt@mheaney.ni.net References: <908956499.754394@dedale.pandemonium.fr> <70mo3h$gll$1@cf01.edf.fr> <71cjab$ka8$1@nnrp1.dejanews.com> NNTP-Posting-Date: Sun, 15 Nov 1998 21:59:51 PDT Newsgroups: comp.lang.ada Date: 1998-11-16T00:00:00+00:00 List-Id: Simon Wright 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;