org.apache.commons.math.util
Class ExpandableDoubleArray

java.lang.Object
  extended byorg.apache.commons.math.util.ExpandableDoubleArray
All Implemented Interfaces:
DoubleArray, Serializable
Direct Known Subclasses:
ContractableDoubleArray

public class ExpandableDoubleArray
extends Object
implements Serializable, DoubleArray

A DoubleArray implementation which automatically expands an internal double array to handle an array of arbitrary length. This implementation of DoubleArray is provided to support scenarios in which the ultimate length of an array is unknown, and the developer can control the expansion process through the initialCapacity and expansionFactor parameters.

This implementation also allows a developer to:

The initialCapacity parameter sets the capacity of the initial storage array, and the expansionFactor is the factor by which the current storage capacity is multiplied each time the internal array needs to be expanded. Please note that the length of the internal storage array has nothing to do with the number of elements currently stored in this array. If one creates an instance of this class with an initialCapacity of "2" and an expansion factor of "2", and then adds 3546 elements to the array, this implementation will need to expand the array 10 times - first from 2 -> 4. then 4 -> 8, 8 -> 16, and so on until we reach 4096 which is sufficient to hold 3546 elements.

Version:
$Revision: 1.11 $ $Date: 2003/11/19 03:28:24 $
See Also:
Serialized Form

Field Summary
protected  float expansionFactor
          The expand factor of the array.
protected  int initialCapacity
          The initial capacity of the array.
protected  double[] internalArray
          This is the internal storage array.
protected  int numElements
          Number of elements in the array
protected  int startIndex
          Keeps track of a starting index
 
Constructor Summary
ExpandableDoubleArray()
          Create an expandable double array with the default initial capactiy of 16 and an expansion factor of 2.00
ExpandableDoubleArray(int initialCapacity)
          Create an expandable double array with the specified initial capacity and the defult expansion factor of 2.00
ExpandableDoubleArray(int initialCapacity, float expansionFactor)
          Create an expandable double array with the specificed initial capacity and expand factor.
 
Method Summary
 void addElement(double value)
          Adds an element to the end of this expandable array
 double addElementRolling(double value)
          Adds an element and moves the window of elements up one.
 void clear()
          Clear the array, reset the size to the initialCapacity and the number of elements to zero.
 void discardFrontElements(int i)
          Discards values from the front of the list.
protected  void expand()
          Expands the internal storage array using the expansion factor
 double getElement(int index)
          Returns the element at the specified index
 double[] getElements()
          Returns a double[] of elements
 float getExpansionFactor()
          The expansion factor controls the size of a new aray when an array needs to be expanded.
 int getNumElements()
          Returns the number of elements currently in the array.
 double[] getValues()
          Returns the internal storage array
 void setElement(int index, double value)
          Sets the element at the specified index.
 void setExpansionFactor(float expansionFactor)
          Sets the expansion factor for this expandable double array.
 void setInitialCapacity(int initialCapacity)
          Sets the initial capacity
 void setNumElements(int i)
          This function allows you to control the number of elements contained in this array, and can be used to "throw" out the last n values in an array.
 int start()
          Returns the starting index of the internal array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

internalArray

protected double[] internalArray
This is the internal storage array.


numElements

protected int numElements
Number of elements in the array


startIndex

protected int startIndex
Keeps track of a starting index


initialCapacity

protected int initialCapacity
The initial capacity of the array. Initial capacity is not exposed as a property as it is only meaningful when passed to a constructor.


expansionFactor

protected float expansionFactor
The expand factor of the array. When the array need to be expanded, the new array size will be internalArray.length * expandFactor

Constructor Detail

ExpandableDoubleArray

public ExpandableDoubleArray()
Create an expandable double array with the default initial capactiy of 16 and an expansion factor of 2.00


ExpandableDoubleArray

public ExpandableDoubleArray(int initialCapacity)
Create an expandable double array with the specified initial capacity and the defult expansion factor of 2.00

Parameters:
initialCapacity - The initial size of the internal storage array

ExpandableDoubleArray

public ExpandableDoubleArray(int initialCapacity,
                             float expansionFactor)
Create an expandable double array with the specificed initial capacity and expand factor.

Parameters:
initialCapacity - The initial size of the internal storage array
expansionFactor - the array will be expanded based on this parameter
Method Detail

getExpansionFactor

public float getExpansionFactor()
The expansion factor controls the size of a new aray when an array needs to be expanded. When a value is inserted into a full array, the new array size is calculated as the current array size times this expansion factor. The default expansion factor is 2.0

Returns:
the expansion factor of this expandable double array

setExpansionFactor

public void setExpansionFactor(float expansionFactor)
Sets the expansion factor for this expandable double array. The expansion factor will affect the next expansion of this array.

Parameters:
expansionFactor - the expansion factor of this array

setInitialCapacity

public void setInitialCapacity(int initialCapacity)
Sets the initial capacity

Parameters:
initialCapacity - of the array

getValues

public double[] getValues()
Returns the internal storage array

Returns:
the internal storage array used by this object

start

public int start()
Returns the starting index of the internal array.

Returns:
starting index

getNumElements

public int getNumElements()
Returns the number of elements currently in the array. Please note that this is different from the length of the internal storage array.

Specified by:
getNumElements in interface DoubleArray
Returns:
number of elements

setNumElements

public void setNumElements(int i)
This function allows you to control the number of elements contained in this array, and can be used to "throw" out the last n values in an array. This feature is mainly targetted at the subclasses of this array class. Note that this function will also expand the internal array as needed.

Parameters:
i - a new number of elements

getElement

public double getElement(int index)
Returns the element at the specified index

Specified by:
getElement in interface DoubleArray
Parameters:
index - index to fetch a value from
Returns:
value stored at the specified index

setElement

public void setElement(int index,
                       double value)
Sets the element at the specified index. This method will expand the internal storage array to accomodate the insertion of a value at an index beyond the current capacity.

Specified by:
setElement in interface DoubleArray
Parameters:
index - index to store a value in
value - value to store at the specified index

expand

protected void expand()
Expands the internal storage array using the expansion factor


addElement

public void addElement(double value)
Adds an element to the end of this expandable array

Specified by:
addElement in interface DoubleArray
Parameters:
value - value to be added to end of array

addElementRolling

public double addElementRolling(double value)
Adds an element and moves the window of elements up one. This has the effect of a FIFO. when you "roll" the array an element is removed from the array. The return value of this function is the discarded double.

Specified by:
addElementRolling in interface DoubleArray
Parameters:
value - the value to add
Returns:
the value which has been discarded or "pushed" out of the array by this rolling insert.

clear

public void clear()
Clear the array, reset the size to the initialCapacity and the number of elements to zero.

Specified by:
clear in interface DoubleArray

discardFrontElements

public void discardFrontElements(int i)
Discards values from the front of the list. This function removes n elements from the front of the array.

Parameters:
i - number of elements to discard from the front of the array.

getElements

public double[] getElements()
Description copied from interface: DoubleArray
Returns a double[] of elements

Specified by:
getElements in interface DoubleArray
Returns:
all elements added to the array
See Also:
DoubleArray.getElements()


Copyright © 2003-2004 Apache Software Foundation. All Rights Reserved.