/*
 * History
 * Created on Sep 23, 2004 by kofke
 */
package etomica.potential;

import etomica.nbr.NeighborCriterion;
import etomica.util.Arrays;

/**
 * Instances of this class are created by AtomType classes to
 * store the list of potentials and criteria that apply to 
 * the atoms of that type.
 */
public class PotentialArray implements java.io.Serializable {

	/**
	 * 
	 */
	public PotentialArray() {
		super();
	}

    /**
     * Returns the index that this type has assigned to the given
     * potential.  If potential has not been previously identified
     * to this instance, a new index will be returned.
     * @param potential The potential of interest
     * @return index associated with potential
     */
    public int getPotentialIndex(Potential potential) {
        if(mostRecentIndex > -1 && potentials[mostRecentIndex]==potential) return mostRecentIndex;

        mostRecentIndex = -1;
        while(++mostRecentIndex < potentials.length) {
            if(potentials[mostRecentIndex]==potential) {
                return mostRecentIndex; 
            }
        }
        throw new IllegalArgumentException("Potential "+potential+" is unknown");
    }
    
    /**
     * Identifies the given potential to this type instance and returns
     * the index assigned to it.  If potential was previously added,
     * no action is taken other than to return the previously assigned
     * index for the potential (acts same as getPotentialIndex in this 
     * case).
     * @param newPotential the potential being added
     * @return the new or previously assigned index for the potential
     */
    public int addPotential(Potential newPotential) {
    	for(mostRecentIndex=0; mostRecentIndex<potentials.length; mostRecentIndex++) {
    		if(potentials[mostRecentIndex] == newPotential) return mostRecentIndex;
    	}
        potentials = (Potential[])Arrays.addObject(potentials, newPotential);
        // make room for a criterion to be added via setCriterion
        criteria = (NeighborCriterion[])Arrays.resizeArray(criteria, potentials.length);
    	return potentials.length-1;
    }
    
    /**
     * Sets the criterion associated with the given potential
     */
    public void setCriterion(Potential potential, NeighborCriterion criterion) {
        criteria[getPotentialIndex(potential)] = criterion;
    }

    /**
     * Removes the given potential from the list held by this type instance,
     * and returns the index that was assigned to it.  If the potential was
     * not previously added to this instance, returns -1.
     */
    public int removePotential(Potential potential) {
        for (int i=0; i<potentials.length; i++) {
    		if (potentials[i] == potential) {
    	    	Potential[] newPotentials = new Potential[potentials.length-1];
    	    	System.arraycopy(potentials,0,newPotentials,0,i);
    	    	System.arraycopy(potentials,i+1,newPotentials,i,potentials.length-i-1);
    	    	potentials = newPotentials;
    	    	return i;
    		}
    	}
    	return -1;
    }
    
    public final Potential[] getPotentials() {
    	return potentials;
    }
    
    public final NeighborCriterion[] getCriteria() {
        return criteria;
    }
    
    private Potential[] potentials = new Potential[0];
    private NeighborCriterion[] criteria = new NeighborCriterion[0];
    private int mostRecentIndex = -1;

}

@@ -366,23 +366,8 @@
  %7B%0A%0A
-%09/**%0A%09 * %0A%09 */%0A
 %09pub
@@ -2367,16 +2367,45 @@
 tential 
+and the associated criterion 
 from the
@@ -2410,16 +2410,24 @@
 he list 
+%0A     * 
 held by 
@@ -2445,23 +2445,16 @@
 nstance,
-%0A     *
  and ret
@@ -2481,32 +2481,40 @@
 was assigned to 
+%0A     * 
 it.  If the pote
@@ -2522,23 +2522,16 @@
 tial was
-%0A     *
  not pre
@@ -2569,16 +2569,23 @@
  returns
+%0A     *
  -1.%0A   
@@ -2982,24 +2982,301 @@
 wPotentials;
+%0A%0A                NeighborCriterion%5B%5D newCriteria = new NeighborCriterion%5Bcriteria.length-1%5D;%0A                System.arraycopy(criteria,0,newCriteria,0,i);%0A                System.arraycopy(criteria,i+1,newCriteria,i,criteria.length-i-1);%0A                criteria = newCriteria;
 %0A    %09    %09r
@@ -3488,29 +3488,82 @@
 eria;%0A    %7D%0A
-
     %0A
+    private static final long serialVersionUID = 1L;%0A
     private 

remove associated NeighborCriterion when removing a Potential