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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,fed2e7871ca258cd X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-01-02 16:19:33 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!cpk-news-hub1.bbnplanet.com!news.gtei.net!newsfeed1.cidera.com!Cidera!easynews!sjc-peer.news.verio.net!news.verio.net!sjc-read.news.verio.net.POSTED!not-for-mail Newsgroups: comp.lang.ada From: Brian Rogoff Subject: Re: List Container Strawman 1.4 In-Reply-To: Message-ID: References: <87lmfsn2jj.fsf@deneb.enyo.de> <3C2CA143.6030006@mail.com> <3C2EB518.5040805@mail.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Date: Thu, 03 Jan 2002 00:20:03 GMT NNTP-Posting-Host: 192.220.65.223 X-Complaints-To: abuse@verio.net X-Trace: sjc-read.news.verio.net 1010017203 192.220.65.223 (Thu, 03 Jan 2002 00:20:03 GMT) NNTP-Posting-Date: Thu, 03 Jan 2002 00:20:03 GMT Organization: Verio Xref: archiver1.google.com comp.lang.ada:18469 Date: 2002-01-03T00:20:03+00:00 List-Id: Very nice Matthew! I think I'll dust off my old attempt at STL in Ada and incorporate these fragments. FWIW, I didn't even find the first version too bad, though I guess I'm thoroughly polluted by C, C++, and other non-Ada languages. Ummm, insert a smiley after "polluted" if you're a hypersensitive C++ programmer... -- Brian On Wed, 2 Jan 2002, Matthew Heaney wrote: > > "Hyman Rosen" 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; > > > > > >