package org.nevec.rjm ;
import java.math.* ;
import java.util.Vector ;
/*!***************************
* The indices and powers of a cycle index.
* @since 2017-08-13
* @author Richard J. Mathar
*/
class CycIndxMono implements Comparable<CycIndxMono>
{
	/** The cyle monomial t[i]^j[i] for i=1,2,3.. (group order divisors)
	* A single pairs[0..1] indicates that the index pairs[0] is having exponent paris[1].
	* The vector is a squence of such pairs[] with 2 integer elements.
	*/
	Vector<int[]> pairs ;

	/** ctor given the indices (cycle length) and exponents (frequencies)	
	* @param unsrt The not necessarily sorted pairs of index and exponent
	*/
	CycIndxMono(Vector<int[]> unsrt)
	{
		/* start with an empty vector
		*/
		pairs = new Vector<int[]>() ;

		/* put all the unsrt[0...] pairs of integers
		* into the vector such that they're sorted along the first
		* element in the pair.
		*/
		for(int[] u : unsrt)
		{
			/* insert at a place such that pair[0] is increasing...
			*/
			boolean insrtd = false;
			for(int i=0 ; i < pairs.size() && ! insrtd; i++)
			{
				if (pairs.elementAt(i)[0] >= u[0])
				{
					pairs.insertElementAt(u,i) ;
					insrtd = true ;
				}
			}
			if( ! insrtd)
				pairs.add(u) ;
		}
	} /* ctor */

	/** Compare two monomials with respect to index and exponent.
	* In a sort of random decision monomials are considered larger
	*  if the number of terms is larger. If the number of terms is the
	*  same we assume (see ctor) that the terms are sorted and we compare
	*  primarily with respect to the first occurrence of the larger index
	*  scanning the internal vector upwards, and secondarily with respect
	*  to the larger exponent.
	* @param oth the second monimial to be compared with this.
	* @return 1, 0 or -1 if this is considered larger, equal to or smaller than oth.
	*/
	public int compareTo(final CycIndxMono oth)
	{
		if ( pairs.size() > oth.pairs.size())
			return 1; 
		else if ( pairs.size() < oth.pairs.size())
			return -1; 
		else
		{
			for(int i=0 ; i < pairs.size() ; i++)
			{
				if ( pairs.elementAt(i)[0] > oth.pairs.elementAt(i)[0] )
					return 1;
				else if ( pairs.elementAt(i)[0] < oth.pairs.elementAt(i)[0] )
					return -1;
				else if ( pairs.elementAt(i)[1] > oth.pairs.elementAt(i)[1] )
					return 1;
				else if ( pairs.elementAt(i)[1] < oth.pairs.elementAt(i)[1] )
					return -1;
			}
			return 0 ;
		}
	}

} /* CycIndxMono */
