diff --git a/example/cpp11/symmetric/Jamfile.v2 b/example/cpp11/symmetric/Jamfile.v2 new file mode 100644 index 0000000..df1e8be --- /dev/null +++ b/example/cpp11/symmetric/Jamfile.v2 @@ -0,0 +1,39 @@ +# Boost.Coroutine Library Examples Jamfile + +# Copyright Oliver Kowalke 2009. +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +# For more information, see http://www.boost.org/ + +import common ; +import feature ; +import indirect ; +import modules ; +import os ; +import toolset ; + +project boost/coroutine/example + : requirements + ../../../build//boost_coroutine + /boost/program_options//boost_program_options + /boost/random//boost_random + /boost/thread//boost_thread + gcc-4.7,on:-fsplit-stack + gcc-4.7,on:-DBOOST_USE_SEGMENTED_STACKS + gcc-4.8,on:-fsplit-stack + gcc-4.8,on:-DBOOST_USE_SEGMENTED_STACKS + gcc-4.9,on:-fsplit-stack + gcc-4.9,on:-DBOOST_USE_SEGMENTED_STACKS + static + multi + ; + +exe simple + : simple.cpp + ; + +exe merge_arrays + : merge_arrays.cpp + ; diff --git a/example/cpp11/symmetric/merge_arrays.cpp b/example/cpp11/symmetric/merge_arrays.cpp new file mode 100644 index 0000000..a586ec8 --- /dev/null +++ b/example/cpp11/symmetric/merge_arrays.cpp @@ -0,0 +1,77 @@ + +// Copyright Keld Helsgaun 2000, Oliver Kowalke 2014. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include +#include + +#include + +typedef boost::coroutines::symmetric_coroutine< void > coro_t; + +std::vector< int > merge( std::vector< int > const& a, std::vector< int > const& b) +{ + std::vector< int > c; + std::size_t idx_a = 0, idx_b = 0; + coro_t * other_a = 0, * other_b = 0; + + coro_t coro_a( + [&]( coro_t::self_type & self) { + while ( idx_a < a.size() ) + { + if ( b[idx_b] < a[idx_a]) + self( * other_b); + c.push_back(a[idx_a++]); + } + while ( c.size() < a.size() + b.size()) + c.push_back( b[idx_b]); + }); + + coro_t coro_b( + [&]( coro_t::self_type & self) { + while ( idx_b < b.size() ) + { + if ( a[idx_a] < b[idx_b]) + self( * other_a); + c.push_back(b[idx_b++]); + } + while ( c.size() < ( a.size() + b.size() ) ) + c.push_back( a[idx_a]); + }); + + + other_a = & coro_a; + other_b = & coro_b; + + coro_a(); + + return c; +} + +void print( std::string const& name, std::vector< int > const& v) +{ + std::cout << name << " : "; + for ( auto itm : v) + { std::cout << itm << " "; } + std::cout << "\n"; +} + +int main( int argc, char * argv[]) +{ + std::vector< int > a = { 1, 5, 6, 10 }; + print( "a", a); + + std::vector< int > b = { 2, 4, 7, 8, 9, 13 }; + print( "b", b); + + std::vector< int > c = merge( a, b); + print( "c", c); + + std::cout << "Done" << std::endl; + + return EXIT_SUCCESS; +} diff --git a/example/cpp11/symmetric/simple.cpp b/example/cpp11/symmetric/simple.cpp new file mode 100644 index 0000000..d944a9f --- /dev/null +++ b/example/cpp11/symmetric/simple.cpp @@ -0,0 +1,41 @@ + +// Copyright Oliver Kowalke 2009. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#include + +#include + +typedef boost::coroutines::symmetric_coroutine< void > coro_t; + +coro_t * c1 = 0; +coro_t * c2 = 0; + +int main( int argc, char * argv[]) +{ + coro_t coro1( + [&]( coro_t::self_type & self) { + std::cout << "foo1" << std::endl; + self( * c2); + std::cout << "foo2" << std::endl; + self( * c2); + std::cout << "foo3" << std::endl; + }); + coro_t coro2( + [&]( coro_t::self_type & self) { + std::cout << "bar1" << std::endl; + self( * c1); + std::cout << "bar2" << std::endl; + self( * c1); + std::cout << "bar3" << std::endl; + }); + c1 = & coro1; + c2 = & coro2; + coro1(); + std::cout << "Done" << std::endl; + + return EXIT_SUCCESS; +}