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