source:
src/tests/coroutine/fibonacci.c@
33f5b57
| Last change on this file since 33f5b57 was af1ed1ad, checked in by , 8 years ago | |
|---|---|
|
|
| File size: 1.3 KB | |
| Rev | Line | |
|---|---|---|
| [242a902] | 1 | // |
| [ec95d11] | 2 | // Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo |
| 3 | // | |
| 4 | // The contents of this file are covered under the licence agreement in the | |
| 5 | // file "LICENCE" distributed with Cforall. | |
| [242a902] | 6 | // |
| 7 | // fibonacci.c -- | |
| 8 | // | |
| [ec95d11] | 9 | // Author : Thierry Delisle |
| 10 | // Created On : Thu Jun 8 07:29:37 2017 | |
| 11 | // Last Modified By : Peter A. Buhr | |
| [af1ed1ad] | 12 | // Last Modified On : Thu Mar 22 22:45:44 2018 |
| 13 | // Update Count : 15 | |
| [242a902] | 14 | // |
| [ec95d11] | 15 | |
| [c15b805] | 16 | #include <fstream> |
| [4a3334cf] | 17 | #include <coroutine> |
| [c15b805] | 18 | |
| [971d9f2] | 19 | coroutine Fibonacci { int fn; }; // used for communication |
| [c15b805] | 20 | |
| [971d9f2] | 21 | void ?{}( Fibonacci & fib ) with( fib ) { fn = 0; } |
| [80d9e49] | 22 | |
| [af1ed1ad] | 23 | // main automatically called on first resume |
| [971d9f2] | 24 | void main( Fibonacci & fib ) with( fib ) { |
| [f802e46] | 25 | int fn1, fn2; // retained between resumes |
| [af1ed1ad] | 26 | fn = 0; fn1 = fn; // 1st case |
| [f802e46] | 27 | suspend(); // restart last resume |
| [af1ed1ad] | 28 | fn = 1; fn2 = fn1; fn1 = fn; // 2nd case |
| [f802e46] | 29 | suspend(); // restart last resume |
| [971d9f2] | 30 | for ( ;; ) { |
| 31 | fn = fn1 + fn2; fn2 = fn1; fn1 = fn; // general case | |
| [f802e46] | 32 | suspend(); // restart last resume |
| [ec95d11] | 33 | } // for |
| [c15b805] | 34 | } |
| 35 | ||
| [971d9f2] | 36 | int next( Fibonacci & fib ) with( fib ) { |
| 37 | resume( fib ); // restart last suspend | |
| 38 | return fn; | |
| [c15b805] | 39 | } |
| 40 | ||
| [9129a84] | 41 | int main() { |
| [ec95d11] | 42 | Fibonacci f1, f2; |
| 43 | for ( int i = 1; i <= 10; i += 1 ) { | |
| [f802e46] | 44 | sout | next( f1 ) | next( f2 ) | endl; |
| [ec95d11] | 45 | } // for |
| [0e76cf4f] | 46 | } |
| [ec95d11] | 47 | |
| 48 | // Local Variables: // | |
| 49 | // tab-width: 4 // | |
| 50 | // compile-command: "cfa fibonacci.c" // | |
| 51 | // End: // |
Note:
See TracBrowser
for help on using the repository browser.