/**
 * Subnet.java Represention of channel routing subnets.
 * The vertical constraint graph (G) of any channel routing
 * state is represented by the collection of arrays containing
 * the Subnets below each Subnet.
 *
 * Created 6/7/2002
 * @author David Hettlinger
 */

public class Subnet implements Cloneable {
    public int name;  //The name of the subnet.
    public int net;  //The net to which it belongs.
    public int begin;  //The column that it begins in.
    public int end;  //The column it ends in.
    public Subnet[] subnetsBelow;  //The Subnets vertically below it.
    public int numSubsBelow; //Number of Subnets below this one
    public int vertex; //The vertex that this Subnet is in.

    /**
     * Constructs a Subnet object.
     * @param sName the name of this Subnet
     * @param sNet the net of this Subnet
     * @param sBegin the beginning column of this Subnet
     * @param sEnd the ending column of this Subnet
     * @param listSize the max possible number of Subnets that could be
     * in subnetsBelow.
     */
    public Subnet(int sName, int sNet, int sBegin, int sEnd, int listSize){
	name = sName;
	net = sNet;
	numSubsBelow = 0;
	begin = sBegin;
	end = sEnd;
	subnetsBelow = new Subnet[listSize];
    }

    /**
     * Sets the vertex number of this Subnet.
     * @param vertNum the verex number
     */
    public void setVertex(int vertNum) {
	
	vertex = vertNum;
    }

    /**
     * Adds a Subnet to the subnetsBelow array and increments numSubsBelow.
     * @param newSub the Subnet to be added.
     */
    public void addSubBelow(Subnet newSub){
	subnetsBelow[numSubsBelow] = newSub;
	numSubsBelow++;
    }

    /**
     * Sets the subnetsBelow list of this Subnet.
     * @param subArray an array of Subnet references.
     * @param originalList the original subnetsBelow list
     */
    public void setSubsBelowList(Subnet[] subArray, Subnet[] originalList){
	
	for (int i = 0; i < numSubsBelow; i++) 
	    for (int j = 0; j < subArray.length && subnetsBelow[i] == null; j++)
		if (originalList[i].name == subArray[j].name)
		    subnetsBelow[i] = subArray[j];
    }


    /**
     * Returns a String representing this Subnet object.
     * @return returnString the String representation of this Subnet.
     */
    public String toString(){
	String returnString = "" + name + ", " + begin + ", " + end;
	return returnString;
    }
    
    /**
     * Makes a clone of the current Subnet object.
     * @return duplicate a duplicate a this Subnet object.
     */
    public Object clone(){
	
	try{
	    Subnet duplicate = (Subnet) super.clone();
	    duplicate.subnetsBelow = new Subnet[this.subnetsBelow.length];
	    return duplicate;
	}
	catch (CloneNotSupportedException e){
	    System.out.println("CloneNotSupportedException in Subnet.java");
	    return null;
	}
    }
}
