Boost logo

Boost :

From: John Max Skaller (skaller_at_[hidden])
Date: 2001-08-10 23:51:24


Daryle Walker wrote:

> > ?? But that is just the idea of an iterator in the first place.
> > What am I missing?
>
> I see an iterator as traversing over a container or a virtual container.

        That's your problem then. :-)

> Generators in C++, with an operator()(), don't seem to be connected to a
> container, but just create values.

        They create a sequence of values. Isn't that a 'virtual container'?

> I sometimes want a generator to look like an iterator.
> Now operator()() would "become" operator*() followed by
> operator++(); the new-state returning functionality has been split up into
> current-state returning and moving to the next state.

        Yes.

> Now I also have an
> option of evaluating any previous states (bidirectional), at least by single
> step. We could have the option of skipping several states (random access),
> if it's efficient.

        Yes. But I still see nothing new here.
You have described succinctly:

> Now operator()() would "become" operator*() followed by
> operator++(); the new-state returning functionality has been split up into
> current-state returning and moving to the next state.

precisely what an iterator already IS.

        Actually, you left out termination, which iterators do
by comparison, as opposed to, say, testing for a special return value,
or throwing an exception. And that mechanism works fine
for 'subranges' of infinite sequences.

        Another way: if we have a mapping

        f: N -> T // N == natural numbers

then an iterator is nothing more than a way of enumerating
the pairs (i,f(i)) in order of i, over time. That is,
it is a space time transform: it changes a space,
called a container IF the space is physical memory, into
a time series. There is nothing in the basic notion
that restricts the 'space' to be physical memory:
for example you can iterate over the data coming
down a serial line, in which case the iteration
is mapping the space 'time' back into 'time'. :-)

The key thing about iterators is that the client
both reads and writes the data:

        x = *i; // read data on RHS, write on LHS

This is different from being called with the data,
or writing a recursive descent on a tree
to visit all the nodes and having to embed the operation
you want _inside_ the recursion.

-- 
John (Max) Skaller, mailto:skaller_at_[hidden] 
10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850
New generation programming language Felix  http://felix.sourceforge.net
Literate Programming tool Interscript     
http://Interscript.sourceforge.net

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk