[yield_iterator] new iterator lib with C# yield return/yield break

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

On 20/06/2011 20:15, Oliver Kowalke wrote:
I've uploaded a new lib 'yield_iterator' [...] The library contains a example iterating over a tree via visitor pattern an yield_iterator.
I hope I get some feedback.
I've played a lot with coroutine-based iterators. The interesting stuff is to be able to build an input iterator from an output iterator. I didn't really look at what you did but I'm interested in knowing where you store the stack.

yield_adapter contains a context object which handles register and stack swapping. The stack is stored in the context object too. boost.context could be used as the base of a coroutine implementation (becuase it is more low-level). Oliver -------- Original-Nachricht --------
Datum: Tue, 21 Jun 2011 00:51:32 +0200 Von: Mathias Gaunard <mathias.gaunard@ens-lyon.org> An: boost@lists.boost.org Betreff: Re: [boost] [yield_iterator] new iterator lib with C# yield return/yield break
On 20/06/2011 20:15, Oliver Kowalke wrote:
I've uploaded a new lib 'yield_iterator' [...] The library contains a example iterating over a tree via visitor pattern an yield_iterator.
I hope I get some feedback.
I've played a lot with coroutine-based iterators.
The interesting stuff is to be able to build an input iterator from an output iterator.
I didn't really look at what you did but I'm interested in knowing where you store the stack.
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- NEU: FreePhone - kostenlos mobil telefonieren! Jetzt informieren: http://www.gmx.net/de/go/freephone

On 21/06/2011 07:19, Oliver Kowalke wrote:
yield_adapter contains a context object which handles register and stack swapping. The stack is stored in the context object too. boost.context could be used as the base of a coroutine implementation (becuase it is more low-level).
That doesn't really answer my question. Where do you store the stack with regard to the iterator? Iterators need to be copyable, so you can't just store it in the iterator itself. So you use an intrusive_ptr to deal with this (which is not thread-safe, by the way). This could cause some problems with comparison. Your operator== seems incorrect, it compares the values, and does very weird things with null values.

Where do you store the stack with regard to the iterator?
because the iterator does the context switch (each time the iterator is incremented it jumps back to 'void iterate()' function) the context jump is handled by context aggregated inside yield_adapter hence yield_iterator.
Iterators need to be copyable, so you can't just store it in the iterator itself.
It would be a limitation that copying the iterator let point it to the same yield_adapter. the reason is that the context class is not copyable only movrable.
Your operator== seems incorrect, it compares the values, and does very weird things with null values.
because the code uses pointers to transfer the address of the object wich should be dereferenced by the iterator (the address it transfered between the context switches). End of iteration is indicated by NULL poitners. -- Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de

On Jun 21, 2011 2:15 AM, "Oliver Kowalke" <oliver.kowalke@gmx.de> wrote:
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.
I'm under the impression that we generally iterate through trees by pointing the iterator to the node of the tree, instead of storing everything into a stack. I would probably have to do this if the tree was an object shared across several threads. Is this meant for this type of usage? Cheers, Sylvain

If the threads don't modify the tree you could use the yield-iterator - otherwise you would have to protected the tree against modifications anyway. The yield-iterator can be used if the tree is shared between several threads. If you don't use thread-specific storage you could move the iterator from on thread to another (but you can't use it in multiple threads at the same time -> critical section). Oliver -------- Original-Nachricht --------
Datum: Tue, 21 Jun 2011 10:12:48 +0800 Von: Sylvain Bougerel <sylvain.bougerel.devel@gmail.com> An: boost@lists.boost.org Betreff: Re: [boost] [yield_iterator] new iterator lib with C# yield return/yield break
On Jun 21, 2011 2:15 AM, "Oliver Kowalke" <oliver.kowalke@gmx.de> wrote:
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.
I'm under the impression that we generally iterate through trees by pointing the iterator to the node of the tree, instead of storing everything into a stack.
I would probably have to do this if the tree was an object shared across several threads. Is this meant for this type of usage?
Cheers, Sylvain _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- NEU: FreePhone - kostenlos mobil telefonieren! Jetzt informieren: http://www.gmx.net/de/go/freephone
participants (3)
-
Mathias Gaunard
-
Oliver Kowalke
-
Sylvain Bougerel