
public class CodeRack {
    static ARRandom random = new ARRandom();
    Codelet[] codelets;
    int topIndex;

    public CodeRack( int size ) {
	codelets = new Codelet[ size ];
	topIndex = 0;
    }

    protected void finalize() throws Throwable {
	codelets = null;
    }

    public void addCodelet( Codelet c ) {
	if ( c != null && c.getScore() != Integer.MIN_VALUE ) {
	    if ( topIndex < codelets.length ) {
		codelets[ topIndex ] = c;
		++topIndex;
	    } else {
		int minScore = Integer.MAX_VALUE;
		int minIndex = 0;

		for ( int ii=0; ii < topIndex; ++ii ) {
		    int score = codelets[ ii ].getScore();
		    if ( score < minScore ) {
			minScore = score;
			minIndex = ii;
		    }
		}

		if ( minScore < c.getScore() ) {
		    codelets[ minIndex ] = c;
		}
	    }
	}
    }

    public Codelet[] getCodelets() {
	return codelets;
    }

    public void mingleSomeFolks( int count ) {

	    /*
	    ** mingle some folks.
	    */
	for ( int ii=0; ii < count; ++ii ) {
	    Codelet a = codelets[ random.nextInt( topIndex ) ];
	    Codelet b = codelets[ random.nextInt( topIndex ) ];

	    if ( a != null && b != null ) {
		Codelet c = a.combineWith( b );
		addCodelet( c );
	    }
	}
    }
}
