Re: C++ implementation of Alexandrescu's Range ideas
Mathias Gaunard wrote:
On 5 oct, 20:20, "Thomas J. Gritzan" <phygon_antis...@gmx.de> wrote:
No. You can express the same things with both concepts because you can
build adapters that map one concept to the other (there are adapters in
Boost.Range).
Boost.Range is just a glorified pair of iterators, Alexandrescu's
ranges are fundamentally different, they replace iterators altogether.
I have already discussed in the past how some things that you can do
with iterators can't be done with Alexandrescu's ranges.
Basically anything that involves recombining beginnings and ends.
If I have two ranges, [a, b] and [c, b], with c in [a, b], with
iterators I can generate in constant time the range [a, c], which
cannot be done with the interface Alexandrescu has provided, since it
lacks a way of representing positions effectively and to combine or
recombine them into ranges.
In other words, scrapping iterators is not possible without losing
some expressivity. And I believe Alexandrescu acknowledges that too.
I sure do.
Speaking of range recombination (and using your notation), I recently
realized one extra primitive is needed: bool sameFront(r1, r2) that
returns true if and only if ranges r1 and r2 have the same front (they
may have distinct backs).
Without sameFront it is, I think, impossible to implement STL's rotate
on singly-linked lists. I was vaguely aware of the matter as I was
working on rotate, but I pedaled my way out by comparing the addresses
of the front()s of the two ranges. Languages that don't allow address
taking need a full-fledged primitive.
With sameFront() in tow you can create a range that, given two ranges
[a, b] and [c, d] returns a range that effectively iterates [a, c]. That
range would have a distinct type though: it would store both ranges and
then bump "a" until the ranges have the same front, i.e. a == c.
The operation not being type-invariant it is less expressive than the
iterators version. However, it is also more general and could be
specialized for certain ranges to stay type-invariant.
Andrei
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]