//! -------------------------------------------------------
//! I need to hack this to make it possible for recomb vertices to send out 0 edges

class EdgeIterator
{
	private int v;
	private int iter[];
	private int root[];
	private int status[];
	private int flip[];
	private int edgesPerIteration;
	private int edgeVec[][];
	private static final int TAIL = 0;
	private static final int HEAD = 1;

	public EdgeIterator( int numVer )
	{
		//! The first vertex is a root, the second is a split, the last is a recomb

		v = numVer;

		iter = new int[v-1];
		root = new int[v-1];

		status = new int[v-1];

		flip = new int[v-1];

		edgesPerIteration = 1;	//! The first arc is always for free

		AddIterator( 0, 1 );
		AddIterator( 1, 2 );		
		//! No iterator for the last vertex because has no edges leaving it...
	}

	public void AddIterator( int vnum, int numArc )
	{
		//! System.out.println("Adding iterator for "+vnum);
		iter[vnum] = numArc;
		edgesPerIteration += numArc;
		if( numArc == 1 )
		{
			//! e.g. v=7, vnum=0, 6 arrows are possible numbered 0 to 5
			//! but we allow an extra possibility: linking to yourself! i.e. no edge

			flip[vnum] = v - vnum;	//! used to be -1
		}
		else
			if( numArc == 2 )
			{
				int temp = v - vnum - 1;
				root[vnum] = temp;
				flip[vnum] = temp*temp;
			}
	}

	public void armIterator()
	{
		edgeVec = new int[edgesPerIteration][2];		
	}

	public boolean isFinished()
	{
		//! It's finished if all counters are maxed out

		for( int x=0; x<status.length; x++ )
		{
			if( status[x] != (flip[x]-1) ) return false;
		}
		return true;
	}


	public void increment()
	{

		boolean finished = false;
		int at = status.length - 1;

		while( ! finished )
		{
			if( status[at] != (flip[at]-1) )
			{
				status[at]++;
				finished = true;
			}
			else
			{
				status[at] = 0;
				at--;
			}
		}

	}

	public int[][] getEdgeVec()
	{
		//! That should be enough...

		//! Now, let's turn that thing into a vector...

		edgeVec[0][TAIL] = 0;
		edgeVec[0][HEAD] = 1;	//! The first edge, for free...

		int fill = 1;
		int scan = 0;

		while( fill != edgesPerIteration )
		{
			int val = status[scan];

			if( iter[scan] == 1 )
			{
				if( val != flip[scan]-1 )
				{
					edgeVec[fill][TAIL] = scan;
					edgeVec[fill][HEAD] = scan + val + 1;
					fill++;
				}
				else
				{
					//! it's a non-edge
					edgeVec[fill][TAIL] = -1;
					edgeVec[fill][HEAD] = -1;
					fill++;
				}
			}
			else
				if( iter[scan] == 2 )
				{
					int r = root[scan];

					int one = val / r;
					int two = val % r;

					edgeVec[fill][TAIL] = scan;
					edgeVec[fill][HEAD] = scan + one + 1;

					fill++;

					edgeVec[fill][TAIL] = scan;
					edgeVec[fill][HEAD] = scan + two + 1;

					fill++;

				}
				else	{
					System.out.println("Something went wrong...");
				}

			scan++;

		}
		return edgeVec;

	}


}

