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-Thread: 103376,f2690a5e963b61b6 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!news.glorb.com!hwmnpeer01.lga!hwmedia!hw-filter.lga!fe04.lga.POSTED!53ab2750!not-for-mail From: Mikhail Terekhov User-Agent: Mozilla Thunderbird 1.0.2 (Windows/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: GCC 4.0 Ada.Containers Cursor danger. References: <1120474891.635131.216700@g44g2000cwa.googlegroups.com> <1120575076.876798.108220@g44g2000cwa.googlegroups.com> <1120583470.429264.325450@g43g2000cwa.googlegroups.com> <42cb8d21$0$22761$9b4e6d93@newsread2.arcor-online.net> <42cd064c$0$10817$9b4e6d93@newsread4.arcor-online.net> <42cda8c4$0$22780$9b4e6d93@newsread2.arcor-online.net> <1u3hh2597i4ne$.1ryetugksbmus.dlg@40tude.net> <1120834341.499757.133770@g43g2000cwa.googlegroups.com> <1121093867.964444.232420@g14g2000cwa.googlegroups.com> <42d2bc2d$0$20148$9b4e6d93@newsread2.arcor-online.net> <1121134291.379399.79460@z14g2000cwz.googlegroups.com> <42d46b51$0$18005$9b4e6d93@newsread4.arcor-online.net> In-Reply-To: <42d46b51$0$18005$9b4e6d93@newsread4.arcor-online.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: X-Trace: badccedimaighclehdmemkagmlnkebeigmjocliikommamaklkofpbhdcomdnfbnjbpgffabkfbgngnifipihpcpekfjbnfkokjlgbkgmachabmcbkmbejjpbccnommhbcfagklmibllijll NNTP-Posting-Date: Wed, 13 Jul 2005 21:11:03 MST Date: Thu, 14 Jul 2005 00:11:04 -0400 Xref: g2news1.google.com comp.lang.ada:3619 Date: 2005-07-14T00:11:04-04:00 List-Id: Georg Bauhaus wrote: > > Here is a silly example demonstrating a subprogram that is > independent of any specific container. That is, you can choose > any instance in place of My_Container. > As arguments to the following function, use cursors > returned by Floor and Ceiling (to mark a range of > associations in a map.) Use First and Last of a set to express > forall. Do the same using a vector. > > > with My_Container; > -- some instance of some Ada.Container ("abstract" if you wish) > > function gen_find > (first, > last: My_Container.Cursor; > item: My_Container.Cursor) return My_Container.Cursor > > -- The first cursor of the sequence from `first` to `last` that designates > -- an elemenent equal to the element designated by `item`. > -- `No_Element` if not found. > is > use My_Container; > > p: Cursor := first; > off: constant Cursor := Next(last); > begin > loop > exit when p = off or else Element(p) = Element(item); > next(p); > end loop; > > if p = off then > return No_Element; > else > return p; > end if; > > end gen_find; The problem with your example is that it is based on the wrong assumptions: - First is that all containers have an order. Again, sets and hashes in general are *not ordered*. To impose an order on them standard needs to put a restriction on implementarion algorithms. You example is completely legal generic function in the current Ada.Containers framework. But if you decide at some point to add unordered containers, then you generic function became invalid because for unordered containers "first" and "last" have no sence at all. - Second is that find/search operation makes sence only for a mapping container i.e. vector or hash; in this case it returns a key corresponding to the element they search for (index for vectors is just a special kind of key) if element has been found and some kind of No_Element key or exception if there is no such element. For a not mapping container i.e. set, list or tree the find/search degenerates into membership test because there is no sence to search for the element if you already have it. True generic operation for all container types can be membership test not find/search. Another problem with your example is that it is essentially a NOP. If you already have a cursor for your element why to search for it? Another problem with your example is that there is no collection per se in the gen_find function signature. This is actually a feature of the Ada.Containers itself. Some times cursors replace collections completely! That kind of indirect interface is very dangerous in my view. Now to answer you question, just replace "My_Container" by "My_Vector_Container", then "item: My_Vector_Container.Cursor" by "item: My_Vector_Container.Element_Type" and then "Cursor" by "Index" in that order and you will get a "vectorized" version of you function ;) Mikhail