Boost logo

Boost :

From: Vladimir Prus (ghost_at_[hidden])
Date: 2004-05-14 01:27:55


Neal D. Becker wrote:
> Thanks for the advice. I think I have it now.
>
> Problem:
>
> A function F accepts a pair of stl-style iterators as input. A nullary
> functor G can generate an output. Arrange for F to call G n times without
> storing to an intermediate sequence.
>
> Example: G is a random number generator.
>
> I wonder if there is a more elegant approach?

I have a couple of questions

1. Is this really forward_traversal_iterator? E.g. if your function extracts
data from stream, then you can't store copies of iterators and make a second
pass over the data.

2. Why did you declared 'advance'. IIRC, this is not required from forward
iterators.

As for improving this, I also have two ideas:

1. Some time ago I've created an utility class for creating input iterators,
called "eof_iterator". Reimplementing function_input_iterator with it yeilds
somewhat smaller code, see:

    http://zigzag.cs.msu.su:7813/iterators/function_input_iterator2.hpp

the eof iterator itself is at:

    http://zigzag.cs.msu.su:7813/iterators/eof_iterator.hpp

There are some drawbacks, though: the size of iterator will be bigger.

2. It's possible to create iterator which just calls functional object
(without counting). As separate "until_iterator" would take any iterator and
a functional object and iterate until the functional object returns true.
So, your iterator will be implemented like:

    typedef function_input_iterator<SomeFunc> it;
    class CountTo { ... };
    typedef until_iterator<it, CountTo> uit;
    copy(uit(it(some_func), CountTo(10)), uit(), ........)

I actually believe that such until_iterator can be rather usefull sometimes.

- Volodya


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