2 // Copyright Nat Goodspeed 2013.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 #include <boost/coroutine/all.hpp>
16 #include <boost/bind.hpp>
17 #include <boost/range.hpp>
18 #include <boost/shared_ptr.hpp>
22 typedef boost::shared_ptr< node > ptr_t;
24 // Each tree node has an optional left subtree, an optional right subtree
25 // and a value of its own. The value is considered to be between the left
26 // subtree and the right.
31 node(const std::string& v):
32 left(), right(), value(v)
35 node(ptr_t l, const std::string& v, ptr_t r):
36 left(l), right(r), value(v)
39 static ptr_t create(const std::string& v)
41 return ptr_t(new node(v));
44 static ptr_t create(ptr_t l, const std::string& v, ptr_t r)
46 return ptr_t(new node(l, v, r));
50 node::ptr_t create_left_tree_from(const std::string& root)
68 node::ptr_t create_right_tree_from(const std::string& root)
86 // recursively walk the tree, delivering values in order
87 void traverse(node::ptr_t n,boost::coroutines::asymmetric_coroutine<std::string>::push_type& out)
89 if (n->left) traverse(n->left,out);
91 if (n->right) traverse(n->right,out);
97 node::ptr_t left_d(create_left_tree_from("d"));
98 boost::coroutines::asymmetric_coroutine<std::string>::pull_type left_d_reader(
99 boost::bind(traverse, left_d, _1));
100 std::cout << "left tree from d:\n";
101 std::copy(boost::begin(left_d_reader),
102 boost::end(left_d_reader),
103 std::ostream_iterator<std::string>(std::cout, " "));
104 std::cout << std::endl;
106 node::ptr_t right_b(create_right_tree_from("b"));
107 boost::coroutines::asymmetric_coroutine<std::string>::pull_type right_b_reader(
108 boost::bind(traverse, right_b, _1));
109 std::cout << "right tree from b:\n";
110 std::copy(boost::begin(right_b_reader),
111 boost::end(right_b_reader),
112 std::ostream_iterator<std::string>(std::cout, " "));
113 std::cout << std::endl;
115 node::ptr_t right_x(create_right_tree_from("x"));
116 boost::coroutines::asymmetric_coroutine<std::string>::pull_type right_x_reader(
117 boost::bind(traverse, right_x, _1));
118 std::cout << "right tree from x:\n";
119 std::copy(boost::begin(right_x_reader),
120 boost::end(right_x_reader),
121 std::ostream_iterator<std::string>(std::cout, " "));
122 std::cout << std::endl;
126 node::ptr_t left_d(create_left_tree_from("d"));
127 boost::coroutines::asymmetric_coroutine<std::string>::pull_type left_d_reader(
128 boost::bind(traverse, left_d, _1));
130 node::ptr_t right_b(create_right_tree_from("b"));
131 boost::coroutines::asymmetric_coroutine<std::string>::pull_type right_b_reader(
132 boost::bind(traverse, right_b, _1));
134 std::cout << "left tree from d == right tree from b? "
136 << std::equal(boost::begin(left_d_reader),
137 boost::end(left_d_reader),
138 boost::begin(right_b_reader))
143 node::ptr_t left_d(create_left_tree_from("d"));
144 boost::coroutines::asymmetric_coroutine<std::string>::pull_type left_d_reader(
145 boost::bind(traverse, left_d, _1));
147 node::ptr_t right_x(create_right_tree_from("x"));
148 boost::coroutines::asymmetric_coroutine<std::string>::pull_type right_x_reader(
149 boost::bind(traverse, right_x, _1));
151 std::cout << "left tree from d == right tree from x? "
153 << std::equal(boost::begin(left_d_reader),
154 boost::end(left_d_reader),
155 boost::begin(right_x_reader))
159 std::cout << "Done" << std::endl;