Class gpjpp.GPContainer
All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class gpjpp.GPContainer

java.lang.Object
   |
   +----gpjpp.GPObject
           |
           +----gpjpp.GPContainer

public class GPContainer
extends GPObject
The superclass of most GP objects, holding a fixed-size array of references to GPObject subclasses. For example, GPGene is a container holding its argument nodes. GP is a container holding its result-producing and ADF branches (themselves GPGene trees). GPPopulation is a container holding all the individual GPs in the population, etc.

GPContainer provides a convenient location to centralize the streaming operations for its subclasses.

GPContainer is generally not instantiated directly.

Several of the GPContainer methods are marked "final" so that an optimizing compiler can inline these simple methods, causing a substantial improvement to gpjpp performance. Except for this desire for optimization, these methods don't need to be final.

No explicit array index checking or memory allocation checking is required because Java performs these checks automatically and throws exceptions if errors are detected.

Version:
1.0

Variable Index

 o container
An array that is allocated once during the lifetime of the container.

Constructor Index

 o GPContainer()
Public null constructor used during stream loading only.
 o GPContainer(GPContainer)
Constructor called to clone a deep copy of another container.
 o GPContainer(int)
The constructor called by many GPContainer subclasses to allocate a container of specified size.

Method Index

 o clear()
Sets all container elements to null to ensure that no references remain to objects currently held by this container.
 o clone()
Implements the Cloneable interface.
 o containerSize()
Returns the number of elements the container can hold.
 o get(int)
Returns the object reference from the specified location in the container.
 o isA()
Returns a code identifying the class in a stream file.
 o load(DataInputStream)
Loads a GPContainer from the specified stream.
 o printOn(PrintStream, GPVariables)
Writes a GPContainer in text format to a PrintStream.
 o put(int, GPObject)
Stores an object reference at the specified location in the container.
 o reserveSpace(int)
Reserves space for the specified number of object references.
 o save(DataOutputStream)
Saves a GPContainer to the specified stream.

Variables

 o container
  protected GPObject container[]
An array that is allocated once during the lifetime of the container. It doesn't need to be resized. Use the get() and put() methods to access elements of the array.
See Also:
get, put

Constructors

 o GPContainer
  public GPContainer()
Public null constructor used during stream loading only.
 o GPContainer
  public GPContainer(int numObjects)
The constructor called by many GPContainer subclasses to allocate a container of specified size.
Parameters:
numObjects - the number of references the container can hold.
 o GPContainer
  public GPContainer(GPContainer gpc)
Constructor called to clone a deep copy of another container. This is used during the genetic evolution process to make unique copies of selected individuals.
Parameters:
gpc - the container to copy.

Methods

 o clone
  protected synchronized Object clone()
Implements the Cloneable interface. This clones any GPContainer subclass that doesn't have its own data fields.
Returns:
the cloned object.
Overrides:
clone in class GPObject
 o isA
  public byte isA()
Returns a code identifying the class in a stream file.
Returns:
the ID code CONTAINERID.
Overrides:
isA in class GPObject
 o reserveSpace
  public final void reserveSpace(int num)
Reserves space for the specified number of object references. Note that Java allows allocating a zero-size array and that the array is filled with nulls after allocation.
Parameters:
num - the number of places to reserve
 o containerSize
  public final int containerSize()
Returns the number of elements the container can hold.
Returns:
the container capacity.
 o put
  public final void put(int n,
                        GPObject gpo)
Stores an object reference at the specified location in the container.
Parameters:
n - the container location. Must be in the range from 0 to containerSize()-1.
gpo - the object reference to store. null is ok.
 o get
  public final GPObject get(int n)
Returns the object reference from the specified location in the container.
Parameters:
n - the container location. Must be in the range from 0 to containerSize()-1.
Returns:
the object reference at that location. May be null.
 o clear
  public void clear()
Sets all container elements to null to ensure that no references remain to objects currently held by this container. This was used during testing of garbage collection but is not normally called by gpjpp.
 o load
  protected synchronized void load(DataInputStream is) throws ClassNotFoundException, IOException, InstantiationException, IllegalAccessException
Loads a GPContainer from the specified stream. Calls createRegisteredClassObject to construct the objects contained within the container.
Throws: ClassNotFoundException
if the class indicated by the stream's ID code is not registered with GPObject.
Throws: InstantiationException
if an error occurs while calling new or the null constructor of the specified class.
Throws: IllegalAccessException
if the specified class or its null constructor is not public.
Throws: IOException
if an error occurs while reading the stream.
Throws: RuntimeException
if the class ID code found next in the stream does not match the isA() code for the calling object.
Overrides:
load in class GPObject
 o save
  protected void save(DataOutputStream os) throws IOException
Saves a GPContainer to the specified stream. It saves the container by recursively calling the save method of each object contained within the container. Any null elements in the container are represented by the NULLID constant alone.
Parameters:
os - a formatted output stream.
Throws: RuntimeException
if a container other than GPPopulation has more than 255 elements. This allows save() to store most container sizes in a single byte, leading to a reduction in overall stream size of almost 4x.
Overrides:
save in class GPObject
 o printOn
  public void printOn(PrintStream os,
                      GPVariables cfg)
Writes a GPContainer in text format to a PrintStream. Every supplied GPContainer subclass overrides this method to write the container in a non-generic format.
Overrides:
printOn in class GPObject

All Packages  Class Hierarchy  This Package  Previous  Next  Index