Boost logo

Boost :

From: Daryle Walker (darylew_at_[hidden])
Date: 2001-08-05 14:07:28


Looking at the (beginning of the) "(infinite) sequences using recurrence
relations" thread, I guess we need an iterating generator concept. It's a
generator with an iterator interface.

    operator *() gets the current element, possibly from a cache
    operator ++() goes to the next element
    Optionally, operator --() could go to the previous element

The result of operator *() would be more like a value than a reference.
Unlike standard generators, we allow bi-directionality and could allow
random access (with operators +, -, and []) if the sequence could be
predicted far enough.

The sequences would be like input iterators, except they don't have an end
element. Support for ends could be added if easy to implement, and we would
need operators == and != (and maybe <, >, <=, and >=) for comparisons.

As an example, I once made a Fibonacci sequence geniterator. It could be
written like:

//=========================================================================
template < typename Number >
class fibonacci
{
public:
    typedef Number value_type;

    explicit fibonacci( value_type initial = 1, value_type previous = 0 )
        : current_( initial ), previous_( previous )
        {}

    value_type operator *() const
        { return current_; }

    fibonacci & operator ++()
    {
        value_type const temp = current_ + previous_;
        previous_ = current_;
        current_ = temp;
        return *this;
    }

    fibonacci & operator --();
    {
        value_type const temp = current_ - previous_;
        current_ = previous_;
        previous_ = temp;
        return *this;
    }

    bool operator ==( fibonacci const &x ) const
    {
        return (this->current_ == x.current_)
         && (this->previous_ == x.previous_);
    }

private:
    value_type current_, previous_;
};
//=========================================================================

Someone here wrote a document about refactoring the iterator categories.
Would a geniterator fall in it somehow?

-- 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com

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