Boost logo

Boost Users :

Subject: [Boost-users] Generators and Iterators [was: Re: Overloading boost::for_each()]
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2010-08-30 15:26:48


On 30/08/2010 13:05, David Abrahams wrote:

> Hi Mathias,
>
> Thanks, but you only answered the first part of my question. I don't
> understand how you'd use such a thing.

I don't really understand your question, so I've put together a
practical example, that is available on the vault:
<http://www.boostpro.com/vault/index.php?action=downloadfile&filename=generator_iterator.zip&directory=&>

To demonstrate the idea, I've implemented a simplistic binary tree in
binary_tree.hpp, and provided for it a forward iterator and a global
generator.
As can be clearly seen, the global generator has a trivial, (and
possibly more efficient) implementation, while the iterator is a bit
more complicated and shouldn't fare as well.
I'm not even sure the iterator implementation is correct, which shows
writing iterators is quite more complicated.

I've implemented a range-based 'copy' function, that behaves just like
boost::copy from Boost.Range, except that if the type has a generator,
it can use it instead.
The same thing could be done for all standard algorithms: for_each,
accumulate, etc.

I've also implemented a facility to build an iterator from a generator
using coroutines (uses the latest version of boost.coroutine I could find).

I tried to do some simple timings, but I must be doing it wrong, since
the results can be extremely variable.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net