Class ResizableDoubleArray
- All Implemented Interfaces:
Serializable,DoubleArray
A variable length DoubleArray 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
initialCapacity property, which can be set by the constructor.
The default initial capacity is 16. Adding elements using
addElement(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 the
expansionMode and expansionFactor properties.
The expansionMode determines whether the size of the array is
multiplied by the expansionFactor (MULTIPLICATIVE_MODE) or if
the expansion is additive (ADDITIVE_MODE -- expansionFactor
storage locations added). The default expansionMode is
MULTIPLICATIVE_MODE and the default expansionFactor
is 2.0.
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 of discardFrontElements(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 (the numElements property) and if the difference
is too large, the internal array is contracted to size
numElements + 1. The determination of when the internal
storage array is "too large" depends on the expansionMode and
contractionFactor properties. If the expansionMode
is MULTIPLICATIVE_MODE, contraction is triggered when the
ratio between storage array length and numElements exceeds
contractionFactor. If the expansionMode
is ADDITIVE_MODE, the number of excess storage locations
is compared to contractionFactor.
To avoid cycles of expansions and contractions, the
expansionFactor must not exceed the
contractionFactor. Constructors and mutators for both of these
properties enforce this requirement, throwing IllegalArgumentException if it
is violated.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intadditive expansion modestatic final intmultiplicative expansion mode -
Constructor Summary
ConstructorsConstructorDescriptionCreate a ResizableArray with default properties.ResizableDoubleArray(double[] initialArray) Create a ResizableArray from an existing double[] with the initial capacity and numElements corresponding to the size of the supplied double[] array.ResizableDoubleArray(int initialCapacity) Create a ResizableArray with the specified initial capacity.ResizableDoubleArray(int initialCapacity, float expansionFactor) Create a ResizableArray with the specified initial capacity and expansion factor.ResizableDoubleArray(int initialCapacity, float expansionFactor, float contractionCriteria) Create a ResizableArray with the specified initialCapacity, expansionFactor, and contractionCriteria.ResizableDoubleArray(int initialCapacity, float expansionFactor, float contractionCriteria, int expansionMode) Create a ResizableArray with the specified properties.ResizableDoubleArray(ResizableDoubleArray original) Copy constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddElement(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.voidclear()Clear the array, reset the size to the initialCapacity and the number of elements to zero.voidcontract()Contracts the storage array to the (size of the element set) + 1 - to avoid a zero length array.copy()Returns a copy of the ResizableDoubleArray.static voidcopy(ResizableDoubleArray source, ResizableDoubleArray dest) Copies source to dest, copying the underlying data, so dest is a new, independent copy of source.voiddiscardFrontElements(int i) Discards theiinitial elements of the array.voiddiscardMostRecentElements(int i) Discards theilast elements of the array.booleanReturns true iff object is a ResizableDoubleArray with the same properties as this and an identical internal storage array.floatThe contraction criteria 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 indexdouble[]Returns a double array containing the elements of thisResizableArray.floatThe expansion factor controls the size of a new array when an array needs to be expanded.intTheexpansionModedetermines whether the internal storage array grows additively (ADDITIVE_MODE) or multiplicatively (MULTIPLICATIVE_MODE) when it is expanded.double[]Returns the internal storage array.intReturns the number of elements currently in the array.double[]Deprecated.inthashCode()Returns a hash code consistent with equals.voidsetContractionCriteria(float contractionCriteria) Sets the contraction criteria for this ExpandContractDoubleArray.voidsetElement(int index, double value) Sets the element at the specified index.voidsetExpansionFactor(float expansionFactor) Sets the expansionFactor.voidsetExpansionMode(int expansionMode) Sets theexpansionMode.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.intstart()Returns the starting index of the internal array.doublesubstituteMostRecentElement(double value) Substitutesvaluefor the most recently added value.
-
Field Details
-
ADDITIVE_MODE
public static final int ADDITIVE_MODEadditive expansion mode- See Also:
-
MULTIPLICATIVE_MODE
public static final int MULTIPLICATIVE_MODEmultiplicative expansion mode- See Also:
-
-
Constructor Details
-
ResizableDoubleArray
public ResizableDoubleArray()Create a ResizableArray with default properties.initialCapacity = 16expansionMode = MULTIPLICATIVE_MODEexpansionFactor = 2.5contractionFactor = 2.0
-
ResizableDoubleArray
public ResizableDoubleArray(int initialCapacity) Create a ResizableArray with the specified initial capacity. Other properties take default values:expansionMode = MULTIPLICATIVE_MODEexpansionFactor = 2.5contractionFactor = 2.0
- Parameters:
initialCapacity- The initial size of the internal storage array- Throws:
IllegalArgumentException- if initialCapacity is not > 0
-
ResizableDoubleArray
public ResizableDoubleArray(double[] initialArray) Create a ResizableArray from an existing double[] with the initial capacity and numElements corresponding to the size of the supplied double[] 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:initialCapacity = 16expansionMode = MULTIPLICATIVE_MODEexpansionFactor = 2.5contractionFactor = 2.0
- Parameters:
initialArray- initial array- Since:
- 2.2
-
ResizableDoubleArray
public ResizableDoubleArray(int initialCapacity, float expansionFactor) Create a ResizableArray with the specified initial capacity and expansion factor. The remaining properties take default values:
expansionMode = MULTIPLICATIVE_MODEcontractionFactor = 0.5 + expansionFactor
Throws IllegalArgumentException if the following conditions are not met:
initialCapacity > 0expansionFactor > 1
- Parameters:
initialCapacity- The initial size of the internal storage arrayexpansionFactor- the array will be expanded based on this parameter- Throws:
IllegalArgumentException- if parameters are not valid
-
ResizableDoubleArray
public ResizableDoubleArray(int initialCapacity, float expansionFactor, float contractionCriteria) Create a ResizableArray with the specified initialCapacity, expansionFactor, and contractionCriteria. The
expansionModewill default toMULTIPLICATIVE_MODE.Throws IllegalArgumentException if the following conditions are not met:
initialCapacity > 0expansionFactor > 1contractionFactor >= expansionFactor
- Parameters:
initialCapacity- The initial size of the internal storage arrayexpansionFactor- the array will be expanded based on this parametercontractionCriteria- The contraction Criteria.- Throws:
IllegalArgumentException- if parameters are not valid
-
ResizableDoubleArray
public ResizableDoubleArray(int initialCapacity, float expansionFactor, float contractionCriteria, int expansionMode) Create a ResizableArray with the specified properties.
Throws IllegalArgumentException if the following conditions are not met:
initialCapacity > 0expansionFactor > 1contractionFactor >= expansionFactorexpansionMode in {MULTIPLICATIVE_MODE, ADDITIVE_MODE}
- Parameters:
initialCapacity- the initial size of the internal storage arrayexpansionFactor- the array will be expanded based on this parametercontractionCriteria- the contraction CriteriaexpansionMode- the expansion mode- Throws:
IllegalArgumentException- if parameters are not valid
-
ResizableDoubleArray
Copy constructor. Creates a new ResizableDoubleArray that is a deep, fresh copy of the original. Needs to acquire synchronization lock on original. Original may not be null; otherwise a NullPointerException is thrown.- Parameters:
original- array to copy- Since:
- 2.0
-
-
Method Details
-
addElement
public void addElement(double value) Adds an element to the end of this expandable array.- Specified by:
addElementin interfaceDoubleArray- Parameters:
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- to be added to end of array- Since:
- 2.2
-
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.
- Specified by:
addElementRollingin interfaceDoubleArray- Parameters:
value- the 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) Substitutesvaluefor the most recently added value. Returns the value that has been replaced. If the array is empty (i.e. ifnumElementsis zero), a MathRuntimeException is thrown.- Parameters:
value- new value to substitute for the most recently added value- Returns:
- value that has been replaced in the array
- Since:
- 2.0
-
clear
public void clear()Clear the array, reset the size to the initialCapacity and the number of elements to zero.- Specified by:
clearin interfaceDoubleArray
-
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) Discards theiinitial elements of the array. For example, if the array contains the elements 1,2,3,4, invokingdiscardFrontElements(2)will cause the first two elements to be discarded, leaving 3,4 in the array. Throws illegalArgumentException if i exceeds numElements.- Parameters:
i- the number of elements to discard from the front of the array- Throws:
IllegalArgumentException- if i is greater than numElements.- Since:
- 2.0
-
discardMostRecentElements
public void discardMostRecentElements(int i) Discards theilast elements of the array. For example, if the array contains the elements 1,2,3,4, invokingdiscardMostRecentElements(2)will cause the last two elements to be discarded, leaving 1,2 in the array. Throws illegalArgumentException if i exceeds numElements.- Parameters:
i- the number of elements to discard from the end of the array- Throws:
IllegalArgumentException- if i is greater than numElements.- Since:
- 2.0
-
getContractionCriteria
public float getContractionCriteria()The contraction criteria defines when the internal array will contract to store only the number of elements in the element array. If theexpansionModeisMULTIPLICATIVE_MODE, contraction is triggered when the ratio between storage array length andnumElementsexceedscontractionFactor. If theexpansionModeisADDITIVE_MODE, the number of excess storage locations is compared tocontractionFactor.- Returns:
- the contraction criteria used to reclaim memory.
-
getElement
public double getElement(int index) Returns the element at the specified index- Specified by:
getElementin interfaceDoubleArray- 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 thisResizableArray. This method returns a copy, not a reference to the underlying array, so that changes made to the returned array have no effect on thisResizableArray.- Specified by:
getElementsin interfaceDoubleArray- Returns:
- the double array.
-
getExpansionFactor
public float getExpansionFactor()The expansion factor controls the size of a new array when an array needs to be expanded. TheexpansionModedetermines whether the size of the array is multiplied by theexpansionFactor(MULTIPLICATIVE_MODE) or if the expansion is additive (ADDITIVE_MODE --expansionFactorstorage locations added). The defaultexpansionModeis MULTIPLICATIVE_MODE and the defaultexpansionFactoris 2.0.- Returns:
- the expansion factor of this expandable double array
-
getExpansionMode
public int getExpansionMode()TheexpansionModedetermines whether the internal storage array grows additively (ADDITIVE_MODE) or multiplicatively (MULTIPLICATIVE_MODE) when it is expanded.- Returns:
- Returns the expansionMode.
-
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:
getNumElementsin interfaceDoubleArray- Returns:
- number of elements
-
getValues
Deprecated.replaced bygetInternalValues()as of 2.0Returns the internal storage array. Note that this method returns a reference to the internal storage array, not a copy, and to correctly address elements of the array, thestartIndexis required (available via thestart()method). This method should only be used in cases where copying the internal array is not practical. ThegetElements()method should be used in all other cases.- Returns:
- the internal storage array used by this object
-
getInternalValues
public double[] getInternalValues()Returns the internal storage array. Note that this method returns a reference to the internal storage array, not a copy, and to correctly address elements of the array, thestartIndexis required (available via thestart()method). This method should only be used in cases where copying the internal array is not practical. ThegetElements()method should be used in all other cases.- Returns:
- the internal storage array used by this object
- Since:
- 2.0
-
setContractionCriteria
public void setContractionCriteria(float contractionCriteria) Sets the contraction criteria for this ExpandContractDoubleArray.- Parameters:
contractionCriteria- contraction criteria
-
setElement
public void setElement(int index, double value) Sets the element at the specified index. If the specified index is greater thangetNumElements() - 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).- Specified by:
setElementin interfaceDoubleArray- Parameters:
index- index to store a value invalue- value to store at the specified index- Throws:
ArrayIndexOutOfBoundsException- ifindexis less than zero.
-
setExpansionFactor
public void setExpansionFactor(float expansionFactor) Sets the expansionFactor. Throws IllegalArgumentException if the the following conditions are not met:expansionFactor > 1contractionFactor >= expansionFactor
- Parameters:
expansionFactor- the new expansion factor value.- Throws:
IllegalArgumentException- if expansionFactor is invalid input: '<'= 1 or greater than contractionFactor
-
setExpansionMode
public void setExpansionMode(int expansionMode) Sets theexpansionMode. The specified value must be one of ADDITIVE_MODE, MULTIPLICATIVE_MODE.- Parameters:
expansionMode- The expansionMode to set.- Throws:
IllegalArgumentException- if the specified mode value is not valid
-
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 function will also expand the internal array as needed.- Parameters:
i- a new number of elements- Throws:
IllegalArgumentException- ifiis negative.
-
start
public int start()Returns the starting index of the internal array. The starting index is the position of the first addressable element in the internal storage array. The addressable elements in the array areinternalArray[startIndex],...,internalArray[startIndex + numElements -1]- Returns:
- starting index
-
copy
Copies source to dest, copying the underlying data, so dest is a new, independent copy of source. Does not contract before the copy.
Obtains synchronization locks on both source and dest (in that order) before performing the copy.
Neither source nor dest may be null; otherwise a NullPointerException is thrown
- Parameters:
source- ResizableDoubleArray to copydest- ResizableArray to replace with a copy of the source array- Since:
- 2.0
-
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
- Since:
- 2.0
-
equals
Returns true iff object is a ResizableDoubleArray with the same properties as this and an identical internal storage array. -
hashCode
public int hashCode()Returns a hash code consistent with equals.
-
getInternalValues()as of 2.0