www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - The proposal for bidirectional iterators

The bidirectional iterator proposal has an easy way to shrink the
range from either end, but it seems to be missing a big chunk of the
C++ bidir iterator functionality which is to move the same value
either forward or backward with equal ease.

For instance in a case where you are trying to maintain a sorted
linked list, and you have some iterator that points into the middle.
The key of that value is changed and now you want to move that element
to its new sorted location.  With a C++ bidir iterator you can ++ to
look at the element before, -- to look at the element after and swap
with either neighbor as appropriate.  It looks like it will be
cumbersome at best to do this sort of operation with the proposed
bidirectional iterators.

Tying in with my previous comment on thinking about a Range as an
iterator plus end sentinel,
what would make more sense to me would be for the bidir to add the following:

r.prev  -- move backwards one (like C++ operator--)
r.atStart -- head sentinel checker.  (like comparing with rbegin() in C++)

In that sense a bidirectional range becomes like a conflation of two
ranges that share a single "current" pointer.  I like the feel of
that.  It seems liek the "right" generalization of iterators to me for
this case.

Sep 09 2008