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!news.germany.com!newsfeed.arcor.de!news.arcor.de!not-for-mail Date: Fri, 15 Jul 2005 01:03:34 +0200 From: Georg Bauhaus User-Agent: Debian Thunderbird 1.0.2 (X11/20050331) 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> <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: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <42d6ef47$0$7644$9b4e6d93@newsread2.arcor-online.net> Organization: Arcor NNTP-Posting-Date: 15 Jul 2005 01:03:36 MEST NNTP-Posting-Host: f73b19d9.newsread2.arcor-online.net X-Trace: DXC=hQf;:fH@51:::C@ZGckeZ1Q5U85hF6f;4jW\KbG]kaM8W9f7BB2SXd=o[FDVX0Fj]68JM^O\[iId3gLW1dLZ`6b4 X-Complaints-To: abuse@arcor.de Xref: g2news1.google.com comp.lang.ada:3627 Date: 2005-07-15T01:03:36+02:00 List-Id: Mikhail Terekhov wrote: > - 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. Ada.Containers require that the elements of a set say can be had one after the other through iteration using cursors. I can hardly think of this as a restriction in practice. On the contrary, why should I be satisfied with just membership test? Or with a fixed set of operations on sets? Or why should I abandon sets in favor of some other containers, because they fit better with someone's ideas of how a mathematical structure is sometimes defined? ;-) Say I want a subset B of A, B := {x: x in A | x >= 0}; This could be done using a generic algorithm which is similar to the Generic_Find algorithm that Matt has presented in that there will be a generic formal procedure that acts as the predicate ">= 0?". generic with function Predicate(C: Cursor) return Boolean; function Subset(S: Set) return Set; function Subset(S: Set) return Set is C: Cursor := First(S); result: Set; begin while Has_Element(C) loop if Predicate(C) then Include(result, Element(C)); end if; Next(C); end loop; return result; end Subset; (The example doesn't really need iteration using cursors explicitly because Iterate(S, Conditional_Include'access) or similar will do. However, that's just an ad hoc example that I could think of and I wanted to show cursors :-) > 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. For me, First and Last (or whatever you call them) *do* make sense for any reasonable notion of a set in programming, sorted or not, even when used in computer science. Their purpose then is to start and end the enumeration of the elements in the set. You can think of cursors as elements of a mathematical index set (yes!) that is associated with the elements in some set. Such as is used for example in proofs by induction, or when you want to transform a set into a sorted set, etc. I find Cursors useful when I want to put an index set into operations inside a computer. When I'm can use any container, how could I implement index sets without Cursors? > - Second is that find/search operation makes sence only > for a mapping container i.e. vector or hash; Find is a membership test, how could it not make sense for a set? > there is no sence to search for the element > if you already have it. (I had called my procedure "silly".) Programming is not equal to logic ;-) Notice two uses: (1) > True generic operation for all container types can be membership > test not find/search. This is a membership test, but only accidentally. Think of an extended algorithm that gives you a set of cursors of all occurences of some item in a container, whatever kind of container it is. > 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? (2) The Cursor "item" designates an element in _some_ container. This need not be the container that is searched. Start and Stop can refer to a different container. The algorithm will find the element in that container. > Some times cursors replace > collections completely! That kind of indirect interface is very > dangerous in my view. And Cursors enable programmers to do their work. Cursors add indirection. Imagine programming without indirection, whatever the indirection mechanism looks like. > 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 ;) Forcing every client program to use vectors... -- Georg