]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/coroutine/example/symmetric/merge_arrays.cpp
2 // Copyright Keld Helsgaun 2000, Oliver Kowalke 2014.
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>
14 #include <boost/bind.hpp>
15 #include <boost/foreach.hpp>
17 typedef boost::coroutines::symmetric_coroutine
< void > coro_t
;
23 std::vector
< int > & to_
;
25 void run_( coro_t::yield_type
& yield
)
27 while ( idx
< from
.size() )
29 if ( other
->from
[other
->idx
] < from
[idx
])
31 to_
.push_back(from
[idx
++]);
33 while ( to_
.size() < max_
)
34 to_
.push_back( other
->from
[other
->idx
++]);
37 merger( merger
const&);
38 merger
& operator=( merger
const&);
41 std::vector
< int > const& from
;
44 coro_t::call_type coro
;
46 merger( std::vector
< int > const& from_
, std::vector
< int > & to
, std::size_t max
) :
52 coro( boost::bind( & merger::run_
, this, _1
) )
59 std::vector
< int > merge( std::vector
< int > const& a
, std::vector
< int > const& b
)
62 merger
ma( a
, c
, a
.size() + b
. size() );
63 merger
mb( b
, c
, a
.size() + b
. size() );
73 void print( std::string
const& name
, std::vector
< int > const& v
)
75 std::cout
<< name
<< " : ";
76 BOOST_FOREACH( int itm
, v
)
77 { std::cout
<< itm
<< " "; }
81 int main( int argc
, char * argv
[])
99 std::vector
< int > c
= merge( a
, b
);
102 std::cout
<< "Done" << std::endl
;