// // Cforall Version 1.0.0 Copyright (C) 2018 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // fibonacci_1.cfa -- 1-state finite-state machine: precomputed first two states returning f(n - 1) // // Author : Peter A. Buhr // Created On : Thu Apr 26 23:20:08 2018 // Last Modified By : Peter A. Buhr // Last Modified On : Thu Mar 21 08:10:45 2019 // Update Count : 25 // #include #include coroutine Fibonacci { int fn1; }; // used for communication void main( Fibonacci & fib ) with( fib ) { // called on first resume int fn; [fn1, fn] = [0, 1]; // precompute first two states for () { suspend; // restart last resume [fn1, fn] = [fn, fn1 + fn]; // general case } // for } int next( Fibonacci & fib ) with( fib ) { resume( fib ); // restart last suspend return fn1; } int main() { Fibonacci f1, f2; for ( 10 ) { // print N Fibonacci values sout | next( f1 ) | next( f2 ); } // for } // Local Variables: // // tab-width: 4 // // compile-command: "cfa fibonacci_1.cfa" // // End: //