]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/coroutine/example/symmetric/merge_arrays.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / coroutine / example / symmetric / merge_arrays.cpp
1
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)
6
7 #include <boost/coroutine/all.hpp>
8
9 #include <cstdlib>
10 #include <cstddef>
11 #include <iostream>
12 #include <vector>
13
14 #include <boost/bind.hpp>
15 #include <boost/foreach.hpp>
16
17 typedef boost::coroutines::symmetric_coroutine< void > coro_t;
18
19 class merger
20 {
21 private:
22 std::size_t max_;
23 std::vector< int > & to_;
24
25 void run_( coro_t::yield_type & yield)
26 {
27 while ( idx < from.size() )
28 {
29 if ( other->from[other->idx] < from[idx])
30 yield( other->coro);
31 to_.push_back(from[idx++]);
32 }
33 while ( to_.size() < max_)
34 to_.push_back( other->from[other->idx++]);
35 }
36
37 merger( merger const&);
38 merger & operator=( merger const&);
39
40 public:
41 std::vector< int > const& from;
42 std::size_t idx;
43 merger * other;
44 coro_t::call_type coro;
45
46 merger( std::vector< int > const& from_, std::vector< int > & to, std::size_t max) :
47 max_( max),
48 to_( to),
49 from( from_),
50 idx( 0),
51 other( 0),
52 coro( boost::bind( & merger::run_, this, _1) )
53 {}
54
55 void run()
56 { coro(); }
57 };
58
59 std::vector< int > merge( std::vector< int > const& a, std::vector< int > const& b)
60 {
61 std::vector< int > c;
62 merger ma( a, c, a.size() + b. size() );
63 merger mb( b, c, a.size() + b. size() );
64
65 ma.other = & mb;
66 mb.other = & ma;
67
68 ma.run();
69
70 return c;
71 }
72
73 void print( std::string const& name, std::vector< int > const& v)
74 {
75 std::cout << name << " : ";
76 BOOST_FOREACH( int itm, v)
77 { std::cout << itm << " "; }
78 std::cout << "\n";
79 }
80
81 int main( int argc, char * argv[])
82 {
83 std::vector< int > a;
84 a.push_back( 1);
85 a.push_back( 5);
86 a.push_back( 6);
87 a.push_back( 10);
88 print( "a", a);
89
90 std::vector< int > b;
91 b.push_back( 2);
92 b.push_back( 4);
93 b.push_back( 7);
94 b.push_back( 8);
95 b.push_back( 9);
96 b.push_back( 13);
97 print( "b", b);
98
99 std::vector< int > c = merge( a, b);
100 print( "c", c);
101
102 std::cout << "Done" << std::endl;
103
104 return EXIT_SUCCESS;
105 }