/*
 * Created on Feb 12, 2004
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package pipe.dataLayer.calculations;


import java.util.*;

/**
 * @author Matthew
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class StateList {
		private ArrayList list = new ArrayList();
		//String id;
		//This only adds unique elements
		public void add(int[] m) { 
			list.trimToSize();
			int size = list.size();
			//int[] candidate = m.getMarking();
			
			if (size == 0) {
				Marking marking = new Marking (m, "M0");
				list.add(marking);
				//id = "M0";
				//print(m);
				return;
			}
			else if (size > 0 )
				for (int i = 0;i < size; i++){
					Marking comparator = (Marking)list.get(i);
					int[] compareInts = comparator.getMarking();
					if (compareMarking(m, compareInts)== true)
					{	
						return;
					}
				}
			 
			Marking marking = new Marking (m,"M" + size);
			list.add(marking);
			//id = "M"+list.size();
			return;
			
		}
		
		//This is for adding states with an existing ID to another StateList
		public void add(int[] marking, String idInput) {
			Marking m = new Marking(marking, idInput);
			list.add(m);
		}
		
		public void getID(){
			}
		
		public StateList() {
			
		}
	
		
		public void output(int i) {
			Marking mark = (Marking)list.get(i);
			int[] row = mark.getMarking();
			int size = row.length;
			for (int j = 0; j <size; j++){
				System.out.print(row[j] + " ");
			}
		}
		
		public void print(){
			int size = list.size();
			for (int i= 0; i < size; i++) {
				output(i);
				System.out.println();
			}
		}
		
		public int[] get(int index) {
			Marking record = (Marking)list.get(index);
			return record.getMarking();
		}
		
		public String getID (int index){
			Marking record = (Marking)list.get(index);
			return record.getID();
		}
		
		public int size() { 
			return list.size(); 
			}
		

		public boolean compareMarking(int[] mark1, int[] mark2) {
			int m1 = mark1.length;
			int m2 = mark2.length;
			//System.out.println("m1 = " + m1 + " m2 = " + m2);
			if (m1 != m2)
				return false;
			else
				for (int i = 0; i < m1; i++) {
					if ((int)(mark1[i]) !=  (int)(mark2[i])) {
						//System.out.println (mark1[i] +" ");
						return false;
					}		
				}
			return true;
		}
		
	
	public void print (int[] marking) {
		int length = marking.length;
		for (int i = 0; i < length; i++) {
			System.out.print (marking [i] + " ");
		}
		System.out.println("");
	}
	

}
