comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <mheaney@on2.com>
Subject: Re: OO Style with Ada Containers
Date: Mon, 19 Nov 2007 14:16:52 -0800 (PST)
Date: 2007-11-19T14:16:52-08:00	[thread overview]
Message-ID: <0319d921-4457-4b47-87f2-3f310aaa3d93@o6g2000hsd.googlegroups.com> (raw)
In-Reply-To: a07c57ed-45f6-415e-932a-bb7706ddeb5d@a39g2000pre.googlegroups.com

On Nov 19, 4:29 pm, Maciej Sobczak <see.my.homep...@gmail.com> 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<int>(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



  reply	other threads:[~2007-11-19 22:16 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-14 23:28 OO Style with Ada Containers braver
2007-11-14 23:50 ` Adam Beneschan
2007-11-14 23:59   ` braver
2007-11-15  0:24     ` braver
2007-11-15  9:36       ` Ludovic Brenta
2007-11-15 10:36         ` braver
2007-11-15 11:35           ` Ludovic Brenta
2007-11-15 13:50             ` braver
2007-11-19  2:45               ` Matthew Heaney
2007-11-15 18:22             ` braver
2007-11-15 20:18               ` Ludovic Brenta
2007-11-19  2:48                 ` Matthew Heaney
2007-11-19  2:47               ` Matthew Heaney
2007-11-19  2:39             ` Matthew Heaney
2007-11-19  2:38           ` Matthew Heaney
2007-11-19  2:36         ` Matthew Heaney
2007-11-19  2:24       ` Matthew Heaney
2007-11-23 10:28         ` braver
2007-11-23 13:29           ` Martin Krischik
2007-11-23 14:19             ` Georg Bauhaus
2007-11-25 13:38           ` Ludovic Brenta
2007-11-26  3:58             ` Matthew Heaney
2007-11-26  3:55           ` Matthew Heaney
2007-11-23 22:25         ` braver
2007-11-23 22:46           ` Pascal Obry
2007-11-23 22:52             ` braver
2007-11-26  4:09               ` Matthew Heaney
2007-11-26  4:07             ` Matthew Heaney
2007-11-26  4:03           ` Matthew Heaney
2007-11-26 13:45             ` Matthew Heaney
2007-11-26 19:09               ` braver
2007-11-26 20:29                 ` Matthew Heaney
2007-11-27 19:31                   ` Georg Bauhaus
2007-11-27 20:12                     ` Matthew Heaney
2007-11-25 14:08         ` braver
2007-11-26  4:21           ` Matthew Heaney
2007-11-19  1:04   ` Matthew Heaney
2007-11-15  8:43 ` Dmitry A. Kazakov
2007-11-15 14:04   ` Maciej Sobczak
2007-11-19  2:53     ` Matthew Heaney
2007-11-19 13:44       ` Maciej Sobczak
2007-11-19 14:44         ` Martin
2007-11-19 15:51         ` Matthew Heaney
2007-11-19 17:33           ` Markus E L
2007-11-19 21:29           ` Maciej Sobczak
2007-11-19 22:16             ` Matthew Heaney [this message]
2007-11-19 22:22               ` Matthew Heaney
2007-11-20 14:11               ` Maciej Sobczak
2007-11-20 17:00                 ` Matthew Heaney
2007-11-20 17:17                   ` Matthew Heaney
2007-11-20 21:13                   ` Maciej Sobczak
2007-11-20 21:57                     ` Matthew Heaney
2007-11-21  4:51                     ` Matthew Heaney
2007-11-21  9:18                       ` Georg Bauhaus
2007-11-21 15:59                         ` Maciej Sobczak
2007-11-21 17:41                           ` Georg Bauhaus
2007-11-21 22:25                         ` Jeffrey R. Carter
2007-11-20 18:06                 ` Georg Bauhaus
2007-11-19 16:19         ` Dmitry A. Kazakov
2007-11-19 20:45           ` Maciej Sobczak
2007-11-20  2:24             ` Matthew Heaney
2007-11-20  9:06             ` Dmitry A. Kazakov
2007-11-20 12:16               ` Georg Bauhaus
2007-11-21 15:17                 ` Dmitry A. Kazakov
2007-11-19  2:50   ` Matthew Heaney
2007-11-19  1:03 ` Matthew Heaney
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox