Boost logo

Boost :

Subject: [boost] [yield_iterator] new iterator lib with C# yield return/yield break
From: Oliver Kowalke (oliver.kowalke_at_[hidden])
Date: 2011-06-20 14:15:08


I've uploaded a new lib 'yield_iterator'
(http://www.boostpro.com/vault/index.php?action=downloadfile&filename=boost.yield_iterator-0.1.0.zip&directory=Iterators&).
Documentation can be found also here:
http://ok73.ok.funpic.de/boost/libs/yield_iterator/docu/html/ .

The lib provides the same functionality as C# does with the 'yield
return' and 'yield break' statements - this lib provides an easy way to
iterate complex containers. The library uses boost.context internally.

For instance iterating an tree would usally require to traverse the
complete tree and store the nodes in a STL container - the begin/end
iterators of this STL container are used to iterate the tree.
With boost.yield_iterator you have only to derive a class from
yield_adapter and overload the function 'void iterate()'.
In 'void iterate()' you can iterate locally over the internal data and
return appropriate elements via 'yield_return()'.

class tree_adapter : public boost::yield_adapter< leaf >,
                                        public visitor
{
private:
        node::ptr_t root_;

public:
        tree_adapter() :
                boost::yield_adapter< leaf >(),
                root_()
        {}

        tree_adapter( node::ptr_t root) :
                boost::yield_adapter< leaf >( true),
                root_( root)
        { BOOST_ASSERT( root_); }

        void visit( branch & b)
        {
                if ( b.left) b.left->accept( * this);
                if ( b.right) b.right->accept( * this);
        }

        void visit( leaf & l)
        { yield_return( l); }

        void iterate()
        { root_->accept( * this); }
};

int main( int argc, char * argv[])
{
        {
                node::ptr_t root = create_tree();
                boost::yield_iterator< tree_adapter > e;
                for (
                                boost::yield_iterator< tree_adapter > i( root);
                                i != e; ++i) {
                        std::cout << i->value << "";
                }
        }

        std::cout << "\nDone" << std::endl;

        return EXIT_SUCCESS;
}

The library contains a example iterating over a tree via visitor pattern
an yield_iterator.

I hope I get some feedback.

best regards,
Oliver


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