]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/coroutine/example/asymmetric/same_fringe.cpp
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)
14 #include <boost/bind.hpp>
15 #include <boost/range.hpp>
16 #include <boost/shared_ptr.hpp>
17 #include <boost/coroutine/all.hpp>
21 typedef boost::shared_ptr
< node
> ptr_t
;
23 // Each tree node has an optional left subtree, an optional right subtree
24 // and a value of its own. The value is considered to be between the left
25 // subtree and the right.
30 node(const std::string
& v
):
31 left(), right(), value(v
)
34 node(ptr_t l
, const std::string
& v
, ptr_t r
):
35 left(l
), right(r
), value(v
)
38 static ptr_t
create(const std::string
& v
)
40 return ptr_t(new node(v
));
43 static ptr_t
create(ptr_t l
, const std::string
& v
, ptr_t r
)
45 return ptr_t(new node(l
, v
, r
));
49 node::ptr_t
create_left_tree_from(const std::string
& root
)
67 node::ptr_t
create_right_tree_from(const std::string
& root
)
85 // recursively walk the tree, delivering values in order
86 void traverse(node::ptr_t n
,boost::coroutines::asymmetric_coroutine
<std::string
>::push_type
& out
)
88 if (n
->left
) traverse(n
->left
,out
);
90 if (n
->right
) traverse(n
->right
,out
);
96 node::ptr_t
left_d(create_left_tree_from("d"));
97 boost::coroutines::asymmetric_coroutine
<std::string
>::pull_type
left_d_reader(
98 boost::bind(traverse
, left_d
, _1
));
99 std::cout
<< "left tree from d:\n";
100 std::copy(boost::begin(left_d_reader
),
101 boost::end(left_d_reader
),
102 std::ostream_iterator
<std::string
>(std::cout
, " "));
103 std::cout
<< std::endl
;
105 node::ptr_t
right_b(create_right_tree_from("b"));
106 boost::coroutines::asymmetric_coroutine
<std::string
>::pull_type
right_b_reader(
107 boost::bind(traverse
, right_b
, _1
));
108 std::cout
<< "right tree from b:\n";
109 std::copy(boost::begin(right_b_reader
),
110 boost::end(right_b_reader
),
111 std::ostream_iterator
<std::string
>(std::cout
, " "));
112 std::cout
<< std::endl
;
114 node::ptr_t
right_x(create_right_tree_from("x"));
115 boost::coroutines::asymmetric_coroutine
<std::string
>::pull_type
right_x_reader(
116 boost::bind(traverse
, right_x
, _1
));
117 std::cout
<< "right tree from x:\n";
118 std::copy(boost::begin(right_x_reader
),
119 boost::end(right_x_reader
),
120 std::ostream_iterator
<std::string
>(std::cout
, " "));
121 std::cout
<< std::endl
;
125 node::ptr_t
left_d(create_left_tree_from("d"));
126 boost::coroutines::asymmetric_coroutine
<std::string
>::pull_type
left_d_reader(
127 boost::bind(traverse
, left_d
, _1
));
129 node::ptr_t
right_b(create_right_tree_from("b"));
130 boost::coroutines::asymmetric_coroutine
<std::string
>::pull_type
right_b_reader(
131 boost::bind(traverse
, right_b
, _1
));
133 std::cout
<< "left tree from d == right tree from b? "
135 << std::equal(boost::begin(left_d_reader
),
136 boost::end(left_d_reader
),
137 boost::begin(right_b_reader
))
142 node::ptr_t
left_d(create_left_tree_from("d"));
143 boost::coroutines::asymmetric_coroutine
<std::string
>::pull_type
left_d_reader(
144 boost::bind(traverse
, left_d
, _1
));
146 node::ptr_t
right_x(create_right_tree_from("x"));
147 boost::coroutines::asymmetric_coroutine
<std::string
>::pull_type
right_x_reader(
148 boost::bind(traverse
, right_x
, _1
));
150 std::cout
<< "left tree from d == right tree from x? "
152 << std::equal(boost::begin(left_d_reader
),
153 boost::end(left_d_reader
),
154 boost::begin(right_x_reader
))
158 std::cout
<< "Done" << std::endl
;