Class ResizableDoubleArray
- java.lang.Object
-
- org.hipparchus.util.ResizableDoubleArray
-
- All Implemented Interfaces:
Serializable
public class ResizableDoubleArray extends Object implements Serializable
A variable length primitive double array implementation that automatically handles expanding and contracting its internal storage array as elements are added and removed.The internal storage array starts with capacity determined by the
initialCapacityproperty, which can be set by the constructor. The default initial capacity is 16. Adding elements usingaddElement(double)appends elements to the end of the array. When there are no open entries at the end of the internal storage array, the array is expanded. The size of the expanded array depends on theexpansionModeandexpansionFactorproperties. TheexpansionModedetermines whether the size of the array is multiplied by theexpansionFactor(ResizableDoubleArray.ExpansionMode.MULTIPLICATIVE) or if the expansion is additive (ResizableDoubleArray.ExpansionMode.ADDITIVE--expansionFactorstorage locations added). The defaultexpansionModeisMULTIPLICATIVEand the defaultexpansionFactoris 2.The
addElementRolling(double)method adds a new element to the end of the internal storage array and adjusts the "usable window" of the internal array forward by one position (effectively making what was the second element the first, and so on). Repeated activations of this method (or activation ofdiscardFrontElements(int)) will effectively orphan the storage locations at the beginning of the internal storage array. To reclaim this storage, each time one of these methods is activated, the size of the internal storage array is compared to the number of addressable elements (thenumElementsproperty) and if the difference is too large, the internal array is contracted to sizenumElements + 1. The determination of when the internal storage array is "too large" depends on theexpansionModeandcontractionFactorproperties. If theexpansionModeisMULTIPLICATIVE, contraction is triggered when the ratio between storage array length andnumElementsexceedscontractionFactor.If theexpansionModeisADDITIVE, the number of excess storage locations is compared tocontractionFactor.To avoid cycles of expansions and contractions, the
expansionFactormust not exceed thecontractionFactor. Constructors and mutators for both of these properties enforce this requirement, throwing aMathIllegalArgumentExceptionif it is violated.Note: this class is NOT thread-safe.
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classResizableDoubleArray.ExpansionModeSpecification of expansion algorithm.
-
Constructor Summary
Constructors Constructor Description ResizableDoubleArray()Creates an instance with default properties.ResizableDoubleArray(double[] initialArray)Creates an instance from an existingdouble[]with the initial capacity and numElements corresponding to the size of the supplieddouble[]array.ResizableDoubleArray(int initialCapacity)Creates an instance with the specified initial capacity.ResizableDoubleArray(int initialCapacity, double expansionFactor)Creates an instance with the specified initial capacity and expansion factor.ResizableDoubleArray(int initialCapacity, double expansionFactor, double contractionCriterion)Creates an instance with the specified initial capacity, expansion factor, and contraction criteria.ResizableDoubleArray(int initialCapacity, double expansionFactor, double contractionCriterion, ResizableDoubleArray.ExpansionMode expansionMode, double... data)Creates an instance with the specified properties.ResizableDoubleArray(ResizableDoubleArray original)Copy constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddElement(double value)Adds an element to the end of this expandable array.doubleaddElementRolling(double value)Adds an element to the end of the array and removes the first element in the array.voidaddElements(double[] values)Adds several element to the end of this expandable array.protected voidcheckContractExpand(double contraction, double expansion)Checks the expansion factor and the contraction criterion and raises an exception if the contraction criterion is smaller than the expansion criterion.voidclear()Clear the array contents, resetting the number of elements to zero.doublecompute(MathArrays.Function f)Performs an operation on the addressable elements of the array.voidcontract()Contracts the storage array to the (size of the element set) + 1 - to avoid a zero length array.ResizableDoubleArraycopy()Returns a copy of the ResizableDoubleArray.voiddiscardFrontElements(int i)Discards theiinitial elements of the array.voiddiscardMostRecentElements(int i)Discards theilast elements of the array.booleanequals(Object object)Returns true iff object is a ResizableDoubleArray with the same properties as this and an identical internal storage array.protected voidexpand()Expands the internal storage array using the expansion factor.protected double[]getArrayRef()Provides direct access to the internal storage array.intgetCapacity()Gets the currently allocated size of the internal data structure used for storing elements.doublegetContractionCriterion()The contraction criterion defines when the internal array will contract to store only the number of elements in the element array.doublegetElement(int index)Returns the element at the specified index.double[]getElements()Returns a double array containing the elements of this ResizableArray.doublegetExpansionFactor()The expansion factor controls the size of a new array when an array needs to be expanded.ResizableDoubleArray.ExpansionModegetExpansionMode()The expansion mode determines whether the internal storage array grows additively or multiplicatively when it is expanded.intgetNumElements()Returns the number of elements currently in the array.protected intgetStartIndex()Returns the "start index" of the internal array.inthashCode()Returns a hash code consistent with equals.voidsetElement(int index, double value)Sets the element at the specified index.voidsetNumElements(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.doublesubstituteMostRecentElement(double value)Substitutesvaluefor the most recently added value.
-
-
-
Constructor Detail
-
ResizableDoubleArray
public ResizableDoubleArray()
Creates an instance with default properties.initialCapacity = 16expansionMode = MULTIPLICATIVEexpansionFactor = 2.0contractionCriterion = 2.5
-
ResizableDoubleArray
public ResizableDoubleArray(int initialCapacity) throws MathIllegalArgumentExceptionCreates an instance with the specified initial capacity.Other properties take default values:
expansionMode = MULTIPLICATIVEexpansionFactor = 2.0contractionCriterion = 2.5
- Parameters:
initialCapacity- Initial size of the internal storage array.- Throws:
MathIllegalArgumentException- ifinitialCapacity <= 0.
-
ResizableDoubleArray
public ResizableDoubleArray(double[] initialArray)
Creates an instance from an existingdouble[]with the initial capacity and numElements corresponding to the size of the supplieddouble[]array.If the supplied array is null, a new empty array with the default initial capacity will be created. The input array is copied, not referenced. Other properties take default values:
expansionMode = MULTIPLICATIVEexpansionFactor = 2.0contractionCriterion = 2.5
- Parameters:
initialArray- initial array
-
ResizableDoubleArray
public ResizableDoubleArray(int initialCapacity, double expansionFactor) throws MathIllegalArgumentExceptionCreates an instance with the specified initial capacity and expansion factor.The remaining properties take default values:
expansionMode = MULTIPLICATIVEcontractionCriterion = 0.5 + expansionFactor
Throws MathIllegalArgumentException if the following conditions are not met:
initialCapacity > 0expansionFactor > 1
- Parameters:
initialCapacity- Initial size of the internal storage array.expansionFactor- The array will be expanded based on this parameter.- Throws:
MathIllegalArgumentException- if parameters are not valid.
-
ResizableDoubleArray
public ResizableDoubleArray(int initialCapacity, double expansionFactor, double contractionCriterion) throws MathIllegalArgumentExceptionCreates an instance with the specified initial capacity, expansion factor, and contraction criteria.The expansion mode will default to
MULTIPLICATIVE.Throws MathIllegalArgumentException if the following conditions are not met:
initialCapacity > 0expansionFactor > 1contractionCriterion >= expansionFactor
- Parameters:
initialCapacity- Initial size of the internal storage array.expansionFactor- The array will be expanded based on this parameter.contractionCriterion- Contraction criterion.- Throws:
MathIllegalArgumentException- if the parameters are not valid.
-
ResizableDoubleArray
public ResizableDoubleArray(int initialCapacity, double expansionFactor, double contractionCriterion, ResizableDoubleArray.ExpansionMode expansionMode, double... data) throws MathIllegalArgumentExceptionCreates an instance with the specified properties.
Throws MathIllegalArgumentException if the following conditions are not met:initialCapacity > 0expansionFactor > 1contractionCriterion >= expansionFactor
- Parameters:
initialCapacity- Initial size of the internal storage array.expansionFactor- The array will be expanded based on this parameter.contractionCriterion- Contraction criteria.expansionMode- Expansion mode.data- Initial contents of the array.- Throws:
MathIllegalArgumentException- if the parameters are not valid.NullArgumentException- if expansionMode is null
-
ResizableDoubleArray
public ResizableDoubleArray(ResizableDoubleArray original) throws NullArgumentException
Copy constructor.Creates a new ResizableDoubleArray that is a deep, fresh copy of the original. Original may not be null; otherwise a
NullArgumentExceptionis thrown.- Parameters:
original- array to copy- Throws:
NullArgumentException- if original is null
-
-
Method Detail
-
addElement
public void addElement(double value)
Adds an element to the end of this expandable array.- Parameters:
value- Value to be added to end of array.
-
addElements
public void addElements(double[] values)
Adds several element to the end of this expandable array.- Parameters:
values- Values to be added to end of array.
-
addElementRolling
public double addElementRolling(double value)
Adds an element to the end of the array and removes the first element in the array. Returns the discarded first element.The effect is similar to a push operation in a FIFO queue.
Example: If the array contains the elements 1, 2, 3, 4 (in that order) and addElementRolling(5) is invoked, the result is an array containing the entries 2, 3, 4, 5 and the value returned is 1.
- Parameters:
value- Value to be added to the array.- Returns:
- the value which has been discarded or "pushed" out of the array by this rolling insert.
-
substituteMostRecentElement
public double substituteMostRecentElement(double value) throws MathIllegalStateExceptionSubstitutesvaluefor the most recently added value.Returns the value that has been replaced. If the array is empty (i.e. if
numElementsis zero), an MathIllegalStateException is thrown.- Parameters:
value- New value to substitute for the most recently added value- Returns:
- the value that has been replaced in the array.
- Throws:
MathIllegalStateException- if the array is empty
-
checkContractExpand
protected void checkContractExpand(double contraction, double expansion) throws MathIllegalArgumentExceptionChecks the expansion factor and the contraction criterion and raises an exception if the contraction criterion is smaller than the expansion criterion.- Parameters:
contraction- Criterion to be checked.expansion- Factor to be checked.- Throws:
MathIllegalArgumentException- ifcontraction < expansion.MathIllegalArgumentException- ifcontraction <= 1.MathIllegalArgumentException- ifexpansion <= 1.
-
clear
public void clear()
Clear the array contents, resetting the number of elements to zero.
-
contract
public void contract()
Contracts the storage array to the (size of the element set) + 1 - to avoid a zero length array. This function also resets the startIndex to zero.
-
discardFrontElements
public void discardFrontElements(int i) throws MathIllegalArgumentExceptionDiscards theiinitial elements of the array.For example, if the array contains the elements 1,2,3,4, invoking
discardFrontElements(2)will cause the first two elements to be discarded, leaving 3,4 in the array.- Parameters:
i- the number of elements to discard from the front of the array- Throws:
MathIllegalArgumentException- if i is greater than numElements.
-
discardMostRecentElements
public void discardMostRecentElements(int i) throws MathIllegalArgumentExceptionDiscards theilast elements of the array.For example, if the array contains the elements 1,2,3,4, invoking
discardMostRecentElements(2)will cause the last two elements to be discarded, leaving 1,2 in the array.- Parameters:
i- the number of elements to discard from the end of the array- Throws:
MathIllegalArgumentException- if i is greater than numElements.
-
expand
protected void expand()
Expands the internal storage array using the expansion factor.If
expansionModeis set to MULTIPLICATIVE, the new array size will beinternalArray.length * expansionFactor. IfexpansionModeis set to ADDITIVE, the length after expansion will beinternalArray.length + expansionFactor.
-
getContractionCriterion
public double getContractionCriterion()
The contraction criterion defines when the internal array will contract to store only the number of elements in the element array.If the
expansionModeisMULTIPLICATIVE, contraction is triggered when the ratio between storage array length andnumElementsexceedscontractionFactor. If theexpansionModeisADDITIVE, the number of excess storage locations is compared tocontractionFactor.- Returns:
- the contraction criterion used to reclaim memory.
-
getElement
public double getElement(int index)
Returns the element at the specified index.- Parameters:
index- index to fetch a value from- Returns:
- value stored at the specified index
- Throws:
ArrayIndexOutOfBoundsException- ifindexis less than zero or is greater thangetNumElements() - 1.
-
getElements
public double[] getElements()
Returns a double array containing the elements of this ResizableArray.This method returns a copy, not a reference to the underlying array, so that changes made to the returned array have no effect on this ResizableArray.
- Returns:
- the double array.
-
getExpansionFactor
public double getExpansionFactor()
The expansion factor controls the size of a new array when an array needs to be expanded.The
expansionModedetermines whether the size of the array is multiplied by theexpansionFactor(MULTIPLICATIVE) or if the expansion is additive (ADDITIVE --expansionFactorstorage locations added). The defaultexpansionModeis MULTIPLICATIVE and the defaultexpansionFactoris 2.0.- Returns:
- the expansion factor of this expandable double array
-
getExpansionMode
public ResizableDoubleArray.ExpansionMode getExpansionMode()
The expansion mode determines whether the internal storage array grows additively or multiplicatively when it is expanded.- Returns:
- the expansion mode.
-
getCapacity
public int getCapacity()
Gets the currently allocated size of the internal data structure used for storing elements. This is not to be confused withthe number of elements actually stored.- Returns:
- the length of the internal array.
-
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.- Returns:
- the number of elements.
-
getArrayRef
protected double[] getArrayRef()
Provides direct access to the internal storage array. Please note that this method returns a reference to this object's storage array, not a copy.To correctly address elements of the array, the "start index" is required (available via the
getStartIndexmethod.This method should only be used to avoid copying the internal array. The returned value must be used for reading only; other uses could lead to this object becoming inconsistent.
The
getElements()method has no such limitation since it returns a copy of this array's addressable elements.- Returns:
- the internal storage array used by this object.
-
getStartIndex
protected int getStartIndex()
Returns the "start index" of the internal array. This index is the position of the first addressable element in the internal storage array.The addressable elements in the array are at indices contained in the interval [
getStartIndex(),getStartIndex()+getNumElements()- 1].- Returns:
- the start index.
-
compute
public double compute(MathArrays.Function f)
Performs an operation on the addressable elements of the array.- Parameters:
f- Function to be applied on this array.- Returns:
- the result.
-
setElement
public void setElement(int index, double value)Sets the element at the specified index.If the specified index is greater than
getNumElements() - 1, thenumElementsproperty is increased toindex +1and additional storage is allocated (if necessary) for the new element and all (uninitialized) elements between the new element and the previous end of the array).- Parameters:
index- index to store a value invalue- value to store at the specified index- Throws:
ArrayIndexOutOfBoundsException- ifindex < 0.
-
setNumElements
public void setNumElements(int i) throws MathIllegalArgumentExceptionThis 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 function will also expand the internal array as needed.- Parameters:
i- a new number of elements- Throws:
MathIllegalArgumentException- ifiis negative.
-
copy
public ResizableDoubleArray copy()
Returns a copy of the ResizableDoubleArray. Does not contract before the copy, so the returned object is an exact copy of this.- Returns:
- a new ResizableDoubleArray with the same data and configuration properties as this
-
equals
public boolean equals(Object object)
Returns true iff object is a ResizableDoubleArray with the same properties as this and an identical internal storage array.
-
-