]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/coroutine2/example/same_fringe.cpp
2 // Copyright Nat Goodspeed 2013.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSEstd::placeholders::_1_0.txt or copy at
5 // http://www.boost.org/LICENSEstd::placeholders::_1_0.txt)
14 #include <boost/coroutine2/all.hpp>
17 typedef std::shared_ptr
< node
> ptr_t
;
19 // Each tree node has an optional left subtree, an optional right subtree
20 // and a value of its own. The value is considered to be between the left
21 // subtree and the right.
26 node(const std::string
& v
):
27 left(),right(),value(v
) {
31 node(ptr_t l
, const std::string
& v
, ptr_t r
):
32 left(l
),right(r
),value(v
) {
35 static ptr_t
create(const std::string
& v
) {
36 return ptr_t(new node(v
));
39 static ptr_t
create(ptr_t l
, const std::string
& v
, ptr_t r
) {
40 return ptr_t(new node(l
, v
, r
));
44 node::ptr_t
create_left_tree_from(const std::string
& root
) {
62 node::ptr_t
create_right_tree_from(const std::string
& root
) {
80 // recursively walk the tree, delivering values in order
81 void traverse(node::ptr_t n
, boost::coroutines2::coroutine
<std::string
>::push_type
& out
) {
83 traverse(n
->left
,out
);
86 traverse(n
->right
,out
);
91 node::ptr_t
left_d(create_left_tree_from("d"));
92 node::ptr_t
right_b(create_right_tree_from("b"));
93 node::ptr_t
right_x(create_right_tree_from("x"));
95 boost::coroutines2::coroutine
<std::string
>::pull_type
left_d_reader(
96 [&]( boost::coroutines2::coroutine
<std::string
>::push_type
& out
) {
99 std::cout
<< "left tree from d:\n";
100 std::copy(begin(left_d_reader
),
102 std::ostream_iterator
<std::string
>(std::cout
, " "));
103 std::cout
<< std::endl
;
105 boost::coroutines2::coroutine
<std::string
>::pull_type
right_b_reader(
106 [&]( boost::coroutines2::coroutine
<std::string
>::push_type
& out
) {
107 traverse(right_b
,out
);
109 std::cout
<< "right tree from b:\n";
110 std::copy(begin(right_b_reader
),
112 std::ostream_iterator
<std::string
>(std::cout
, " "));
113 std::cout
<< std::endl
;
115 boost::coroutines2::coroutine
<std::string
>::pull_type
right_x_reader(
116 [&]( boost::coroutines2::coroutine
<std::string
>::push_type
& out
) {
117 traverse(right_x
,out
);
119 std::cout
<< "right tree from x:\n";
120 std::copy(begin(right_x_reader
),
122 std::ostream_iterator
<std::string
>(std::cout
, " "));
123 std::cout
<< std::endl
;
127 node::ptr_t
left_d(create_left_tree_from("d"));
128 node::ptr_t
right_b(create_right_tree_from("b"));
130 boost::coroutines2::coroutine
<std::string
>::pull_type
left_d_reader(
131 [&]( boost::coroutines2::coroutine
<std::string
>::push_type
& out
) {
132 traverse(left_d
,out
);
135 boost::coroutines2::coroutine
<std::string
>::pull_type
right_b_reader(
136 [&]( boost::coroutines2::coroutine
<std::string
>::push_type
& out
) {
137 traverse(right_b
,out
);
140 std::cout
<< "left tree from d == right tree from b? "
142 << std::equal(begin(left_d_reader
),
144 begin(right_b_reader
))
149 node::ptr_t
left_d(create_left_tree_from("d"));
150 node::ptr_t
right_x(create_right_tree_from("x"));
152 boost::coroutines2::coroutine
<std::string
>::pull_type
left_d_reader(
153 [&]( boost::coroutines2::coroutine
<std::string
>::push_type
& out
) {
154 traverse(left_d
,out
);
157 boost::coroutines2::coroutine
<std::string
>::pull_type
right_x_reader(
158 [&]( boost::coroutines2::coroutine
<std::string
>::push_type
& out
) {
159 traverse(right_x
,out
);
162 std::cout
<< "left tree from d == right tree from x? "
164 << std::equal(begin(left_d_reader
),
166 begin(right_x_reader
))
170 std::cout
<< "Done" << std::endl
;