]>
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)
12 #include <boost/bind.hpp>
13 #include <boost/coroutine/all.hpp>
14 #include <boost/foreach.hpp>
16 typedef boost::coroutines::symmetric_coroutine
< void > coro_t
;
22 std::vector
< int > & to_
;
24 void run_( coro_t::yield_type
& yield
)
26 while ( idx
< from
.size() )
28 if ( other
->from
[other
->idx
] < from
[idx
])
30 to_
.push_back(from
[idx
++]);
32 while ( to_
.size() < max_
)
33 to_
.push_back( other
->from
[other
->idx
++]);
36 merger( merger
const&);
37 merger
& operator=( merger
const&);
40 std::vector
< int > const& from
;
43 coro_t::call_type coro
;
45 merger( std::vector
< int > const& from_
, std::vector
< int > & to
, std::size_t max
) :
51 coro( boost::bind( & merger::run_
, this, _1
) )
58 std::vector
< int > merge( std::vector
< int > const& a
, std::vector
< int > const& b
)
61 merger
ma( a
, c
, a
.size() + b
. size() );
62 merger
mb( b
, c
, a
.size() + b
. size() );
72 void print( std::string
const& name
, std::vector
< int > const& v
)
74 std::cout
<< name
<< " : ";
75 BOOST_FOREACH( int itm
, v
)
76 { std::cout
<< itm
<< " "; }
80 int main( int argc
, char * argv
[])
98 std::vector
< int > c
= merge( a
, b
);
101 std::cout
<< "Done" << std::endl
;