|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.commons.math.util.FixedDoubleArray
Provides an implementation of the DoubleArray with a maximum number of elements. Creating an array implementation with an upper limit on the number of elements allows us to support a more efficient "rolling" mechanism to support addElementRoling(double). Please note that this implementation will not preserve the order of the values supplied to this array, calling getValues() will return an array of indeterminate order.
Values are added to this array by calling addElement(double) or addElementRolling(double). If addElement(double) is called on an array that already contains the maximum number of elements, an ArrayIndexOutOfBoundsException will be thrown to reflect an attempt to add a value beyond the boundaries of the fixed length array - in this respect a FixedDoubleArray can be considered "full". Calling addElementRolling(double) on an array which contains the maximum number of elements will cause the array to overwrite the "oldest" value in the array.
This class is called FixedDoubleArray not because it is of a fixed size. The name is appropriate because the internal storage array remains "fixed" in memory, this implementation will never allocate, or copy the internal storage array to a new array instance.
Constructor Summary | |
FixedDoubleArray(double[] array)
Create a fixed array backed by the provided double[] implementation. |
|
FixedDoubleArray(int maxElements)
Create a fixed array for double primitives which can hold up to maxElements doubles. |
Method Summary | |
void |
addElement(double value)
Add an element to the current array, testing to see if this array has already met or exceeded the maximum number of elements |
double |
addElementRolling(double value)
Adds an element by "rolling" the new value into the current array while discarding the element which was added maxElement
add operations ago. |
void |
clear()
Clear the array - drop all the data and start with a blank internal array. |
double |
getElement(int index)
Returns the element value at the specified index. |
double[] |
getElements()
Provides an array of double[] which contain the number of elements added to this array. |
int |
getNumElements()
Retrieves the current size of the array. |
double[] |
getValues()
Returns the internal storage array |
void |
setElement(int index,
double value)
Sets the element at the specified index to the value supplied. |
int |
start()
The starting index in the InternalArray. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public FixedDoubleArray(int maxElements)
maxElements doubles. This implementation of
DoubleArray was created to provide a more "performance-oriented"
in-place rolling mechanism for calculations which need to
operate on a rolling window of values.
- Parameters:
maxElements
- the maximum number of elements this
FixeddoubleArray may contain.
public FixedDoubleArray(double[] array)
array
- the backing arrayMethod Detail |
public int getNumElements()
getNumElements
in interface DoubleArray
DoubleArray.getNumElements()
public double getElement(int index)
getElement
in interface DoubleArray
index
- index to fetch a value from
DoubleArray.getElement(int)
public void setElement(int index, double value)
Sets the element at the specified index to the value supplied.
Implementation Notes:
setElement
in interface DoubleArray
index
- index to store a value invalue
- value to store at the specified indexDoubleArray.setElement(int, double)
public void addElement(double value)
addElement
in interface DoubleArray
value
- to be added to end of arrayDoubleArray.addElement(double)
public double addElementRolling(double value)
Adds an element by "rolling" the new value into the current array
while discarding the element which was added maxElement
add operations ago. The value replaced is returned from this
method. Until an array contains the maximum number of element, this
method has the same result as the addElement(double) operation. Once
the maximum number of elements has been reached this implementation
inserts the new values starting at index 0 of the internal storage
array. This allows for efficient rolling, but prevents us from
preserving the order of the added values.
Note: This function will return Double.NaN
if
no value has been discarded in this roll. This can happen when
the array has not met the size limitation introduced in the
constructor.
addElementRolling
in interface DoubleArray
value
- the value to be added to the array
Double.NaN
value.DoubleArray.addElementRolling(double)
public double[] getElements()
getElements
in interface DoubleArray
DoubleArray.getElements()
public double[] getValues()
public int start()
public void clear()
clear
in interface DoubleArray
DoubleArray.clear()
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |