Re: OO Style with Ada Containers



On 19 Lis, 23:16, Matthew Heaney <mhea...@xxxxxxx> 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:

[excellent examples with local subprograms]

I see your point. These examples are very good.

So let me try to get this interesting discussion a bit further.

Two points:

1.
The most important part of STL is the notion of range-based iteration.
Every single algorithm that iterates over something gets a pair of
iterators denoting the range to be visited.

sort(v.begin(), v.end());

But why not:

vector<int>::iterator middle = v.begin() +
(v.end() - v.begin()) / 2;

sort(v.begin(), middle);
sort(middle, v.end());

And then why not doing it in paraller... ;-)

Sorting only the first N elements?
(let's assume there are at least N)

sort(v.begin(), v.begin() + N);

And so on.
Ada.Containers does not support anything like this. The only available
algorithms work on whole containers.
This is more similar to Java approach, which has a sort method for
sorting whole lists (however, users can have a list-like view on the
part of the container, but this is not a range-based iteration, rather
smart application of view-like containers - in any case, the focus is
on containers, not on ranges).

2.
Another important part of STL is the notion of iterator category.
Depending on the category, the iterator can support different sets of
operations. The most powerful is RandomAccessIterator, which allows to
arbitrarily jump around the sequence in constant time.
Iterator to the vector is a random access iterator, because vector
itself is inherently random-accessible. This is why I was able to do
the above arithmetics to initialize the iterator to the middle of the
vector. I would not be able to do it in the same way with linked
lists.
Categories also allow the algorithms to automatically select the most
optimal implementation, depending on the abilities of the given
iterators.

Nothing like this exists in Ada, where cursors just resemble this:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/ListIterator.html#method_summary

The interfaces are not exactly equivalent, of course, but the
"paradigm" is preserved.
For example, skipping N elements in the vector requires going to the
vector, asking for index, doing computation on the index value and
getting the new cursor.

These are the two reasons that allow me to claim that Ada.Containers
are more similar to Java than to STL.
Ada supports the Java-like "paradigm" for containers and iteration.

In STL-like library I would expect range-based algorithms and
iterators that benefit from abilities of the underlying sequence.

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com
.



Relevant Pages

  • Re: Unified Ada library
    ... :> developers who think that the STL could only have been written in C++, ... Could you comment on the importance of iterators and generic ... we cannot present a standard interface to containers ...
    (comp.lang.ada)
  • Re: Ada Popularity: Comparison of Ada/Charles with C++ STL (and Perl)
    ... array), although every array may have semantics of container. ... for this reason containers shouldn't be all treated as abstract arrays, ... >> for wrapped type's iterators with wrappers for wrapped type's iterators' ... wrapper for containers here... ...
    (comp.lang.ada)
  • Re: Calling static member function through object instance
    ... (granted - some iterators lack all the features). ... containers (an object being organized in a map and a list - list for ... fifo and map for searching). ... think it's possible to have the current interface and a lower level ...
    (comp.lang.cpp)
  • Re: Release-mode only falure when comparing STL list::iterator-s
    ... shared by iterators of all containers of this STL implementation. ... could give you real life problems only if the library should decide to ... (in the 1998 version of the standard that I ...
    (microsoft.public.vc.stl)
  • Re: Why use STL?
    ... > Or am I completely misunderstanding what "iterators" are all about? ... It's the fact that STL container classes don't implement any algorithms (in ... operate on the standard containers and on any other containers you'd care to ...
    (microsoft.public.vc.stl)