//
// 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.c -- 1-state finite-state machine: precomputed first two states returning f(n - 2)
//
// Author           : Peter A. Buhr
// Created On       : Thu Apr 26 23:20:08 2018
// Last Modified By : Peter A. Buhr
// Last Modified On : Sat Aug 18 11:21:02 2018
// Update Count     : 13
// 

#include <fstream.hfa>
#include <coroutine.hfa>

coroutine Fibonacci { int ret; };						// used for communication

void main( Fibonacci & fib ) with( fib ) {				// called on first resume
	int fn, fn1 = 1, fn2 = 0;							// precompute first two states
	for () {
		ret = fn2;
		fn = fn1 + fn2;  fn2 = fn1;  fn1 = fn;			// general case
		suspend();										// restart last resume
	} // for
}

int next( Fibonacci & fib ) with( fib ) {
	resume( fib );										// restart last suspend
	return ret;
}

int main() {
	Fibonacci f1, f2;
	for ( 10 ) {										// print N Fibonacci values
		sout | next( f1 ) | next( f2 ) | endl;
	} // for
}

// Local Variables: //
// tab-width: 4 //
// compile-command: "cfa fibonacci_1.c" //
// End: //
