/*
  Copyright 2006 by Sean Luke
  Licensed under the Academic Free License version 3.0
  See the file "LICENSE" for more information
*/


package ec.gp;
import ec.*;
import ec.util.*;
import java.util.Hashtable;
import java.util.Enumeration;

/* 
 * GPSetType.java
 * 
 * Created: Fri Aug 27 20:55:42 1999
 * By: Sean Luke
 */

/**
 * A GPSetType is a GPType which contains GPAtomicTypes in a set, and is used
 * as a generic GP type.  For more information, see GPType
 *
 * GPSetTypes implement their set using both a hash table and an array.
 * if the size of the set is "significantly big", then the hash table
 * is used to look up membership in the set (O(1), but with a big constant).
 * If the size is small, then the array is used (O(n)).  The dividing line
 * is determined by the constant START_USING_HASH_BEYOND, which you might
 * play with to optimize for your system.
 *
 * @see ec.gp.GPType
 * @author Sean Luke
 * @version 1.0 
 */

public final class GPSetType extends GPType
    {
    public static final String P_MEMBER = "member";
    public static final String P_SIZE = "size";

    /** A packed, sorted array of atomic types in the set */
    public int[] types_packed;

    /** A sparse array of atomic types in the set */
    public boolean[] types_sparse;

    /** The hashtable of types in the set */
    public Hashtable types_h;

    /** You should not construct new types. */
    public GPSetType() { }


    /** Sets up the packed and sparse arrays based on the hashtable */
    public void postProcessSetType(int totalAtomicTypes)
        {
        // load the hashtable into the arrays
        int x=0;
        types_packed = new int[types_h.size()];
        types_sparse = new boolean[totalAtomicTypes];
        Enumeration e = types_h.elements();
        while(e.hasMoreElements())
            {
            GPAtomicType t = (GPAtomicType)(e.nextElement());
            types_packed[x++] = t.type;
            types_sparse[t.type] = true;
            }

        // Sort the packed array
        java.util.Arrays.sort(types_packed);
        }


    public void setup(final EvolutionState state, Parameter base)
        {
        super.setup(state,base);
        
        // Make my Hashtable
        types_h = new Hashtable();

        // How many atomic types do I have?
        int len = state.parameters.getInt(base.push(P_SIZE),null,1);
        if (len<=0) 
            state.output.fatal("The number of atomic types in the GPSetType " +
                name + " must be >= 1.",base.push(P_SIZE));

        // Load the GPAtomicTypes
        for(int x=0;x 

Ads help maintain this website.