coroutine: update of interface

[SVN r85105]
This commit is contained in:
Oliver Kowalke
2013-07-22 15:03:42 +00:00
parent 865902f9b5
commit 01235d2ee7
14 changed files with 2037 additions and 802 deletions

View File

@@ -1,5 +1,5 @@
// Copyright Oliver Kowalke 2009.
// Copyright Nat Goodspeed 2013.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
@@ -7,109 +7,155 @@
#include <cstddef>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <string>
#include <utility>
#include <boost/bind.hpp>
#include <boost/range.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/coroutine/all.hpp>
#include "tree.h"
std::pair< node::ptr_t, node::ptr_t > create_eq_trees()
struct node
{
branch::ptr_t tree1 = branch::create(
leaf::create( "A"),
branch::create(
leaf::create( "B"),
leaf::create( "C") ) );
typedef boost::shared_ptr< node > ptr_t;
branch::ptr_t tree2 = branch::create(
branch::create(
leaf::create( "A"),
leaf::create( "B") ),
leaf::create( "C") );
// Each tree node has an optional left subtree, an optional right subtree
// and a value of its own. The value is considered to be between the left
// subtree and the right.
ptr_t left, right;
std::string value;
return std::make_pair( tree1, tree2);
// construct leaf
node(const std::string& v):
left(), right(), value(v)
{}
// construct nonleaf
node(ptr_t l, const std::string& v, ptr_t r):
left(l), right(r), value(v)
{}
static ptr_t create(const std::string& v)
{
return ptr_t(new node(v));
}
static ptr_t create(ptr_t l, const std::string& v, ptr_t r)
{
return ptr_t(new node(l, v, r));
}
};
node::ptr_t create_left_tree_from(const std::string& root)
{
/* --------
root
/ \
b e
/ \
a c
-------- */
return node::create(
node::create(
node::create("a"),
"b",
node::create("c")),
root,
node::create("e"));
}
std::pair< node::ptr_t, node::ptr_t > create_diff_trees()
node::ptr_t create_right_tree_from(const std::string& root)
{
branch::ptr_t tree1 = branch::create(
leaf::create( "A"),
branch::create(
leaf::create( "B"),
leaf::create( "C") ) );
branch::ptr_t tree2 = branch::create(
branch::create(
leaf::create( "A"),
leaf::create( "X") ),
leaf::create( "C") );
return std::make_pair( tree1, tree2);
/* --------
root
/ \
a d
/ \
c e
-------- */
return node::create(
node::create("a"),
root,
node::create(
node::create("c"),
"d",
node::create("e")));
}
#ifdef BOOST_COROUTINES_UNIDIRECT
bool match_trees( boost::coroutines::coroutine< leaf & >::pull_type & c1,
boost::coroutines::coroutine< leaf & >::pull_type & c2)
// recursively walk the tree, delivering values in order
void traverse(node::ptr_t n,boost::coroutines::coroutine<std::string>::push_type& out)
{
typedef boost::range_iterator< boost::coroutines::coroutine< leaf & >::pull_type >::type iterator_t;
iterator_t i1( boost::begin( c1) );
iterator_t e1( boost::end( c1) );
iterator_t i2( boost::begin( c2) );
return std::equal( i1, e1, i2);
if (n->left) traverse(n->left,out);
out(n->value);
if (n->right) traverse(n->right,out);
}
int main()
{
{
std::pair< node::ptr_t, node::ptr_t > pt = create_eq_trees();
boost::coroutines::coroutine< leaf & >::pull_type te1( boost::bind( enumerate_leafs, _1, pt.first) );
boost::coroutines::coroutine< leaf & >::pull_type te2( boost::bind( enumerate_leafs, _1, pt.second) );
bool result = match_trees( te1, te2);
std::cout << std::boolalpha << "eq. trees matched == " << result << std::endl;
node::ptr_t left_d(create_left_tree_from("d"));
boost::coroutines::coroutine<std::string>::pull_type left_d_reader(
boost::bind(traverse, left_d, _1));
std::cout << "left tree from d:\n";
std::copy(boost::begin(left_d_reader),
boost::end(left_d_reader),
std::ostream_iterator<std::string>(std::cout, " "));
std::cout << std::endl;
node::ptr_t right_b(create_right_tree_from("b"));
boost::coroutines::coroutine<std::string>::pull_type right_b_reader(
boost::bind(traverse, right_b, _1));
std::cout << "right tree from b:\n";
std::copy(boost::begin(right_b_reader),
boost::end(right_b_reader),
std::ostream_iterator<std::string>(std::cout, " "));
std::cout << std::endl;
node::ptr_t right_x(create_right_tree_from("x"));
boost::coroutines::coroutine<std::string>::pull_type right_x_reader(
boost::bind(traverse, right_x, _1));
std::cout << "right tree from x:\n";
std::copy(boost::begin(right_x_reader),
boost::end(right_x_reader),
std::ostream_iterator<std::string>(std::cout, " "));
std::cout << std::endl;
}
{
std::pair< node::ptr_t, node::ptr_t > pt = create_diff_trees();
boost::coroutines::coroutine< leaf & >::pull_type te1( boost::bind( enumerate_leafs, _1, pt.first) );
boost::coroutines::coroutine< leaf & >::pull_type te2( boost::bind( enumerate_leafs, _1, pt.second) );
bool result = match_trees( te1, te2);
std::cout << std::boolalpha << "diff. trees matched == " << result << std::endl;
node::ptr_t left_d(create_left_tree_from("d"));
boost::coroutines::coroutine<std::string>::pull_type left_d_reader(
boost::bind(traverse, left_d, _1));
node::ptr_t right_b(create_right_tree_from("b"));
boost::coroutines::coroutine<std::string>::pull_type right_b_reader(
boost::bind(traverse, right_b, _1));
std::cout << "left tree from d == right tree from b? "
<< std::boolalpha
<< std::equal(boost::begin(left_d_reader),
boost::end(left_d_reader),
boost::begin(right_b_reader))
<< std::endl;
}
{
node::ptr_t left_d(create_left_tree_from("d"));
boost::coroutines::coroutine<std::string>::pull_type left_d_reader(
boost::bind(traverse, left_d, _1));
node::ptr_t right_x(create_right_tree_from("x"));
boost::coroutines::coroutine<std::string>::pull_type right_x_reader(
boost::bind(traverse, right_x, _1));
std::cout << "left tree from d == right tree from x? "
<< std::boolalpha
<< std::equal(boost::begin(left_d_reader),
boost::end(left_d_reader),
boost::begin(right_x_reader))
<< std::endl;
}
std::cout << "Done" << std::endl;
return EXIT_SUCCESS;
}
#else
bool match_trees( coro_t & c1, coro_t & c2)
{
typedef boost::range_iterator< coro_t >::type iterator_t;
iterator_t i1( boost::begin( c1) );
iterator_t e1( boost::end( c1) );
iterator_t i2( boost::begin( c2) );
return std::equal( i1, e1, i2);
}
int main()
{
{
std::pair< node::ptr_t, node::ptr_t > pt = create_eq_trees();
coro_t te1( boost::bind( enumerate_leafs, _1, pt.first) );
coro_t te2( boost::bind( enumerate_leafs, _1, pt.second) );
bool result = match_trees( te1, te2);
std::cout << std::boolalpha << "eq. trees matched == " << result << std::endl;
}
{
std::pair< node::ptr_t, node::ptr_t > pt = create_diff_trees();
coro_t te1( boost::bind( enumerate_leafs, _1, pt.first) );
coro_t te2( boost::bind( enumerate_leafs, _1, pt.second) );
bool result = match_trees( te1, te2);
std::cout << std::boolalpha << "diff. trees matched == " << result << std::endl;
}
std::cout << "Done" << std::endl;
return EXIT_SUCCESS;
}
#endif