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,a3736685ef876ab2 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!o6g2000hsd.googlegroups.com!not-for-mail From: Matthew Heaney Newsgroups: comp.lang.ada Subject: Re: OO Style with Ada Containers Date: Mon, 19 Nov 2007 14:16:52 -0800 (PST) Organization: http://groups.google.com Message-ID: <0319d921-4457-4b47-87f2-3f310aaa3d93@o6g2000hsd.googlegroups.com> References: <1195082906.420079.195000@d55g2000hsg.googlegroups.com> <1s27rv0gt4ujj$.3e2k326rp54d.dlg@40tude.net> <60e46dc9-d8ca-4f47-9e8a-f90a7d45e752@w28g2000hsf.googlegroups.com> NNTP-Posting-Host: 66.162.65.129 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1195510613 6790 127.0.0.1 (19 Nov 2007 22:16:53 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 19 Nov 2007 22:16:53 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: o6g2000hsd.googlegroups.com; posting-host=66.162.65.129; posting-account=umyUbgkAAADe5rQC4VzV-ffCoH4N30u3 User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9,gzip(gfe),gzip(gfe) Content-Disposition: inline Xref: g2news1.google.com comp.lang.ada:18516 Date: 2007-11-19T14:16:52-08:00 List-Id: On Nov 19, 4:29 pm, Maciej Sobczak wrote: > Then, considering the fact that elements in Ada.Containers cannot be > modified without naming the container in addition to providing the > cursor/iterator and this is different from both STL and Java (STL > dereferences iterators to l-values and Java is reference-oriented > anyway - in both cases iterators can be used to query as well as to > update elements), then I would say that Ada.Containers has a similar > "distance" to STL as it has to java.util. Yes, by design the container has to be named in any operation that modifies its elements, but this does *not* imply that the generic algorithm itself has to know anything about containers: generic type Cursor is private; type ET is private; with procedure Add (C : Cursor; E : ET) is <>; with function Next (C : Cursor) return Cursor is <>; with function Has_Element (C : Cursor) return Boolean is <>; procedure Generic_Algorithm (C : Cursor; E : ET); You can see here that the algorithm does not name a container. The body looks like this: procedure Generic_Algorithm (C : Cursor; E : ET) is CC : Cursor := C; begin while Has_Element (CC) loop Add (CC, E); CC := Next (CC); end loop; end Generic_Algorithm; Now let's instantiate using our integer vector container: procedure Op (V : in out Integer_Vectors.Vector) is procedure Add (C : Cursor; I : Integer) is procedure Process (E : in out Integer) is begin E := E + I; end Process; begin V.Update_Element (C, Process'Access); end Add; procedure Algorithm is new Generic_Algorithm (Cursor, Integer); begin Algorithm (V.First); end Op; The point of this example is to demonstrate that Ada provides mechanisms (locally-declared subprograms) to bind the algorithm to the container. > I see (except for Indefinite_XXX, which are Ada-specific), but that > still does not convince me. Well, I'm in a very good position to know whether the Ada container library was designed with the STL in mind! > C++ does not currently have any hash > containers (the "original" STL and some third-party implementations > might have them, though) No, hashed forms were added to the latest version of the C++ standard. > > > Do you by any chance find > > > something in C++ that is worth imitating? :-) > > > Yes, of course: the STL. > > What about IOStreams? > > Hey, we can even mix these two: > > copy(my_vector.begin(), > my_vector.end(), > ostream_iterator(cout, " ")); > > ;-) Oh, yes, I should have mentioned the iostream library. In Ada you could say: generic type ET is limited private; with procedure Process (E : ET) is <>; ... procedure Generic_Copy (...); procedure Op (V : Integer_Vectors.Vector) is procedure Process (E : Integer) is begin Integer_Text_IO.Put (E, Width => 0); Put (' '); end; procedure Copy is new Generic_Copy (Integer, Process); begin Copy (V.First); end Op; ;^) > > A generic algorithm in Ada would look something like: > > You mean: read-only algorithm. No, I meant "algorithm"; see the example above. > What about modifying algorithms? > Ada.Containers.Vectors.Generic_Sorting shows the problem. There is no problem. To bind a generic algorithm to a container object (that needs to be modified), use a locally-declared subprogram. > I like it. Really. It is not possible to do it in C++, the new classes > have to be composed with replaced operations. It shows that it can be done -- just differently from C++. Vive la difference! -Matt