#ifndef __CELLULARAUTOMATA_H
#define __CELLULARAUTOMATA_H

/////////////////////////////////////////////////////////////////////////
//
// Name:	CellularAutomata.h
//
// Purpose:	Abstract base class for n-dimensional
//		cellular automata models.
//
/////////////////////////////////////////////////////////////////////////

#include "Status.h"
#include "Iteration.h"
#include "LatticeIndex.h"

class CellularAutomata {
public:

	//
	// Default constructor
	// 
	CellularAutomata(const Iteration_t& i=DEFAULT_ITERATIONS) :
		maxIters(i), iters(0) {}

	//
	// Copy constructor
	//
	CellularAutomata(const CellularAutomata& C) :
		maxIters(C.maxIters),
		iters(C.iters) {}

	//
	// Overloaded assignment operator for CellularAutomata
	// objects
	//
	CellularAutomata& operator=(const CellularAutomata& C)
		{
			if (this != &C) {
				delete this;
				maxIters = C.maxIters;
				iters = C.iters;
			}
			return *this;
		}


	//
	// Destructor
	//
	~CellularAutomata() {}


	//
	// Performs one iteration of the particular CA rules
	//
	Status_t Iterate()
		{
			Status_t status = SUCCESS;
			if (iters++<maxIters) {
				status = ApplyRules();
			}
			return status;
		}


	//
	// When defined, this shall uniquely specify and apply
	// the rules for the CA model.
	//
	virtual Status_t ApplyRules()=0;


	//
	// Finishes all remaining iterations in the simulation
	//
	Status_t RunToCompletion()
		{
			Status_t status=SUCCESS;
			while(iters<maxIters && (status=Iterate())!=FAILURE) ;
			return status;
		}


	//
	// Access functions
	//
	Iteration_t	MaxIterations()	const	{ return maxIters;	}
	Iteration_t	Iterations()	const	{ return iters;		}


protected:

	Iteration_t		maxIters,	// total iterations to perform
					iters;		// iterations completed thus far

	LatticeIndex_t	curr,		// Indexes the working lattice for
								// current iteration.
					prev;		// Indexes the lattice calculated
								// during previous iteration.

	void SwapLattices()
	// This should be executed either before or after each iteration
	// of the simulation. Effectively, the current lattice shall
	// become the previous lattice, and the previous lattice shall
	// be the current lattice, recalculated in the current iteration
		{
			LatticeIndex_t	temp = curr;
			curr = prev;
			prev = temp;
		}

private:

};

#endif // __CELLULARAUTOMATA_H
