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:05:33 PST Path: archiver1.google.com!news1.google.com!sn-xit-02!sn-post-01!supernews.com!corp.supernews.com!not-for-mail From: "Matthew Heaney" Newsgroups: comp.lang.ada Subject: Re: List Container Strawman 1.4 Date: Wed, 2 Jan 2002 19:09:52 -0500 Organization: Posted via Supernews, http://www.supernews.com Message-ID: References: <87lmfsn2jj.fsf@deneb.enyo.de> <3C2CA143.6030006@mail.com> <3C2EB518.5040805@mail.com> X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4807.1700 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4807.1700 X-Complaints-To: newsabuse@supernews.com Xref: archiver1.google.com comp.lang.ada:18468 Date: 2002-01-02T19:09:52-05:00 List-Id: "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;