fibonacci.cpp 1017 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright Oliver Kowalke 2009.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/coroutine/all.hpp>
  6. #include <cstdlib>
  7. #include <iostream>
  8. #include <boost/range.hpp>
  9. void fibonacci( boost::coroutines::asymmetric_coroutine< int >::push_type & sink)
  10. {
  11. int first = 1, second = 1;
  12. sink( first);
  13. sink( second);
  14. while ( true)
  15. {
  16. int third = first + second;
  17. first = second;
  18. second = third;
  19. sink( third);
  20. }
  21. }
  22. int main()
  23. {
  24. boost::coroutines::asymmetric_coroutine< int >::pull_type source( fibonacci);
  25. boost::range_iterator<
  26. boost::coroutines::asymmetric_coroutine< int >::pull_type
  27. >::type it( boost::begin( source) );
  28. for ( int i = 0; i < 10; ++i)
  29. {
  30. std::cout << * it << " ";
  31. ++it;
  32. }
  33. std::cout << "\nDone" << std::endl;
  34. return EXIT_SUCCESS;
  35. }