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.
Important note: Usage should not assume that this class is thread-safe
even though some of the methods are synchronized
.
This qualifier will be dropped in the next major release (4.0).
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
(ResizableDoubleArray.ExpansionMode.MULTIPLICATIVE
) or if the expansion is additive
(ResizableDoubleArray.ExpansionMode.ADDITIVE
-- expansionFactor
storage
locations added).
The default expansionMode
is MULTIPLICATIVE
and the default
expansionFactor
is 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 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
, contraction is triggered when the
ratio between storage array length and numElements
exceeds
contractionFactor.
If the expansionMode
is ADDITIVE
, 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 a MathIllegalArgumentException
if it is
violated.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
Specification of expansion algorithm. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Deprecated.As of 3.1.static final int
Deprecated.As of 3.1. -
Constructor Summary
ConstructorsConstructorDescriptionCreates 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
(int initialCapacity, float expansionFactor) Deprecated.As of 3.1.ResizableDoubleArray
(int initialCapacity, float expansionFactor, float contractionCriteria) Deprecated.As of 3.1.ResizableDoubleArray
(int initialCapacity, float expansionFactor, float contractionCriteria, int expansionMode) Deprecated.As of 3.1.ResizableDoubleArray
(ResizableDoubleArray original) Copy constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addElement
(double value) Adds an element to the end of this expandable array.double
addElementRolling
(double value) Adds an element to the end of the array and removes the first element in the array.void
addElements
(double[] values) Adds several element to the end of this expandable array.protected void
checkContractExpand
(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.protected void
checkContractExpand
(float contraction, float expansion) Deprecated.As of 3.1.void
clear()
Clear the array contents, resetting the number of elements to zero.double
Performs an operation on the addressable elements of the array.void
contract()
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 void
copy
(ResizableDoubleArray source, ResizableDoubleArray dest) Copies source to dest, copying the underlying data, so dest is a new, independent copy of source.void
discardFrontElements
(int i) Discards thei
initial elements of the array.void
discardMostRecentElements
(int i) Discards thei
last elements of the array.boolean
Returns true iff object is a ResizableDoubleArray with the same properties as this and an identical internal storage array.protected void
expand()
Expands the internal storage array using the expansion factor.protected double[]
Provides direct access to the internal storage array.int
Gets the currently allocated size of the internal data structure used for storing elements.float
Deprecated.As of 3.1.double
The contraction criterion defines when the internal array will contract to store only the number of elements in the element array.double
getElement
(int index) Returns the element at the specified indexdouble[]
Returns a double array containing the elements of thisResizableArray
.float
Deprecated.As of 3.1.int
Deprecated.As of 3.1.double[]
Deprecated.As of 3.1.int
Returns the number of elements currently in the array.protected int
Returns the "start index" of the internal array.int
hashCode()
Returns a hash code consistent with equals.void
setContractionCriteria
(float contractionCriteria) Deprecated.As of 3.1 (to be removed in 4.0 as field will become "final").void
setElement
(int index, double value) Sets the element at the specified index.void
setExpansionFactor
(float expansionFactor) Deprecated.As of 3.1 (to be removed in 4.0 as field will become "final").void
setExpansionMode
(int expansionMode) Deprecated.As of 3.1.void
setExpansionMode
(ResizableDoubleArray.ExpansionMode expansionMode) Deprecated.As of 3.1 (to be removed in 4.0 as field will become "final").protected void
setInitialCapacity
(int initialCapacity) Deprecated.As of 3.1, this is a no-op.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()
Deprecated.As of 3.1.double
substituteMostRecentElement
(double value) Substitutesvalue
for the most recently added value.
-
Field Details
-
ADDITIVE_MODE
Deprecated.As of 3.1. Please useResizableDoubleArray.ExpansionMode.ADDITIVE
instead.Additive expansion mode.- See Also:
-
MULTIPLICATIVE_MODE
Deprecated.As of 3.1. Please useResizableDoubleArray.ExpansionMode.MULTIPLICATIVE
instead.Multiplicative expansion mode.- See Also:
-
-
Constructor Details
-
ResizableDoubleArray
public ResizableDoubleArray()Creates an instance with default properties.initialCapacity = 16
expansionMode = MULTIPLICATIVE
expansionFactor = 2.0
contractionCriterion = 2.5
-
ResizableDoubleArray
Creates an instance with the specified initial capacity. Other properties take default values:expansionMode = MULTIPLICATIVE
expansionFactor = 2.0
contractionCriterion = 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:initialCapacity = 16
expansionMode = MULTIPLICATIVE
expansionFactor = 2.0
contractionCriterion = 2.5
- Parameters:
initialArray
- initial array- Since:
- 2.2
-
ResizableDoubleArray
@Deprecated public ResizableDoubleArray(int initialCapacity, float expansionFactor) throws MathIllegalArgumentException Deprecated.As of 3.1. Please useResizableDoubleArray(int,double)
instead.Creates an instance with the specified initial capacity and expansion factor. The remaining properties take default values:expansionMode = MULTIPLICATIVE
contractionCriterion = 0.5 + expansionFactor
Throws IllegalArgumentException if the following conditions are not met:initialCapacity > 0
expansionFactor > 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) throws MathIllegalArgumentException Creates an instance with the specified initial capacity and expansion factor. The remaining properties take default values:expansionMode = MULTIPLICATIVE
contractionCriterion = 0.5 + expansionFactor
Throws IllegalArgumentException if the following conditions are not met:initialCapacity > 0
expansionFactor > 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.- Since:
- 3.1
-
ResizableDoubleArray
@Deprecated public ResizableDoubleArray(int initialCapacity, float expansionFactor, float contractionCriteria) throws MathIllegalArgumentException Deprecated.As of 3.1. Please useResizableDoubleArray(int,double,double)
instead.Creates an instance with the specified initialCapacity, expansionFactor, and contractionCriterion. The expansion mode will default toMULTIPLICATIVE
.
Throws IllegalArgumentException if the following conditions are not met:initialCapacity > 0
expansionFactor > 1
contractionCriterion >= expansionFactor
- Parameters:
initialCapacity
- Initial size of the internal storage array..expansionFactor
- The array will be expanded based on this parameter.contractionCriteria
- Contraction criteria.- Throws:
MathIllegalArgumentException
- if parameters are not valid.
-
ResizableDoubleArray
public ResizableDoubleArray(int initialCapacity, double expansionFactor, double contractionCriterion) throws MathIllegalArgumentException Creates an instance with the specified initial capacity, expansion factor, and contraction criteria. The expansion mode will default toMULTIPLICATIVE
.
Throws IllegalArgumentException if the following conditions are not met:initialCapacity > 0
expansionFactor > 1
contractionCriterion >= 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.- Since:
- 3.1
-
ResizableDoubleArray
@Deprecated public ResizableDoubleArray(int initialCapacity, float expansionFactor, float contractionCriteria, int expansionMode) throws MathIllegalArgumentException Deprecated.As of 3.1. Please useResizableDoubleArray(int,double,double,ExpansionMode,double[])
instead.Create a ResizableArray with the specified properties.
Throws IllegalArgumentException if the following conditions are not met:
initialCapacity > 0
expansionFactor > 1
contractionFactor >= expansionFactor
expansionMode 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:
MathIllegalArgumentException
- if parameters are not valid
-
ResizableDoubleArray
public ResizableDoubleArray(int initialCapacity, double expansionFactor, double contractionCriterion, ResizableDoubleArray.ExpansionMode expansionMode, double... data) throws MathIllegalArgumentException Creates an instance with the specified properties.
Throws MathIllegalArgumentException if the following conditions are not met:initialCapacity > 0
expansionFactor > 1
contractionCriterion >= 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.
-
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 aNullArgumentException
is thrown.- Parameters:
original
- array to copy- Throws:
NullArgumentException
- if original is null- Since:
- 2.0
-
-
Method Details
-
addElement
public void addElement(double value) Adds an element to the end of this expandable array.- Specified by:
addElement
in interfaceDoubleArray
- 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.- Specified by:
addElements
in interfaceDoubleArray
- Parameters:
values
- 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:
addElementRolling
in interfaceDoubleArray
- 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
Substitutesvalue
for the most recently added value. Returns the value that has been replaced. If the array is empty (i.e. ifnumElements
is zero), an IllegalStateException 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- Since:
- 2.0
-
checkContractExpand
@Deprecated protected void checkContractExpand(float contraction, float expansion) throws MathIllegalArgumentException Deprecated.As of 3.1. Please usecheckContractExpand(double,double)
instead.Checks the expansion factor and the contraction criterion and throws an IllegalArgumentException if the contractionCriteria is less than the expansionCriteria- Parameters:
contraction
- criteria to be checkedexpansion
- factor to be checked- Throws:
MathIllegalArgumentException
- if the contractionCriteria is less than the expansionCriteria.
-
checkContractExpand
protected void checkContractExpand(double contraction, double expansion) throws NumberIsTooSmallException Checks 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:
NumberIsTooSmallException
- ifcontraction < expansion
.NumberIsTooSmallException
- ifcontraction <= 1
.NumberIsTooSmallException
- ifexpansion <= 1
.- Since:
- 3.1
-
clear
public void clear()Clear the array contents, resetting the number of elements to zero.- Specified by:
clear
in 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
Discards thei
initial 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:
MathIllegalArgumentException
- if i is greater than numElements.- Since:
- 2.0
-
discardMostRecentElements
Discards thei
last 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:
MathIllegalArgumentException
- if i is greater than numElements.- Since:
- 2.0
-
expand
protected void expand()Expands the internal storage array using the expansion factor.if
expansionMode
is set to MULTIPLICATIVE_MODE, the new array size will beinternalArray.length * expansionFactor.
IfexpansionMode
is set to ADDITIVE_MODE, the length after expansion will beinternalArray.length + expansionFactor
-
getContractionCriteria
Deprecated.As of 3.1. Please usegetContractionCriterion()
instead.The contraction criteria defines when the internal array will contract to store only the number of elements in the element array. If theexpansionMode
isMULTIPLICATIVE_MODE
, contraction is triggered when the ratio between storage array length andnumElements
exceedscontractionFactor
. If theexpansionMode
isADDITIVE_MODE
, the number of excess storage locations is compared tocontractionFactor.
- Returns:
- the contraction criteria used to reclaim memory.
-
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 theexpansionMode
isMULTIPLICATIVE_MODE
, contraction is triggered when the ratio between storage array length andnumElements
exceedscontractionFactor
. If theexpansionMode
isADDITIVE_MODE
, the number of excess storage locations is compared tocontractionFactor.
- Returns:
- the contraction criterion used to reclaim memory.
- Since:
- 3.1
-
getElement
public double getElement(int index) Returns the element at the specified index- Specified by:
getElement
in interfaceDoubleArray
- Parameters:
index
- index to fetch a value from- Returns:
- value stored at the specified index
- Throws:
ArrayIndexOutOfBoundsException
- ifindex
is 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:
getElements
in interfaceDoubleArray
- Returns:
- the double array.
-
getExpansionFactor
Deprecated.As of 3.1. Return type will be changed to "double" in 4.0.The expansion factor controls the size of a new array when an array needs to be expanded. TheexpansionMode
determines whether the size of the array is multiplied by theexpansionFactor
(MULTIPLICATIVE_MODE) or if the expansion is additive (ADDITIVE_MODE --expansionFactor
storage locations added). The defaultexpansionMode
is MULTIPLICATIVE_MODE and the defaultexpansionFactor
is 2.0.- Returns:
- the expansion factor of this expandable double array
-
getExpansionMode
Deprecated.As of 3.1. Return value to be changed toResizableDoubleArray.ExpansionMode
in 4.0.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.
- Since:
- 3.1
-
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 interfaceDoubleArray
- Returns:
- the number of elements.
-
getInternalValues
Deprecated.As of 3.1.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, thestartIndex
is 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
-
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 thegetStartIndex
method.
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.
ThegetElements()
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.
- Since:
- 3.1
-
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.
- Since:
- 3.1
-
setContractionCriteria
@Deprecated public void setContractionCriteria(float contractionCriteria) throws MathIllegalArgumentException Deprecated.As of 3.1 (to be removed in 4.0 as field will become "final").Sets the contraction criteria.- Parameters:
contractionCriteria
- contraction criteria- Throws:
MathIllegalArgumentException
- if the contractionCriteria is less than the expansionCriteria.
-
compute
Performs an operation on the addressable elements of the array.- Parameters:
f
- Function to be applied on this array.- Returns:
- the result.
- Since:
- 3.1
-
setElement
public void setElement(int index, double value) Sets the element at the specified index. If the specified index is greater thangetNumElements() - 1
, thenumElements
property is increased toindex +1
and 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:
setElement
in interfaceDoubleArray
- Parameters:
index
- index to store a value invalue
- value to store at the specified index- Throws:
ArrayIndexOutOfBoundsException
- ifindex < 0
.
-
setExpansionFactor
@Deprecated public void setExpansionFactor(float expansionFactor) throws MathIllegalArgumentException Deprecated.As of 3.1 (to be removed in 4.0 as field will become "final").Sets the expansionFactor. Throws IllegalArgumentException if the the following conditions are not met:expansionFactor > 1
contractionFactor >= expansionFactor
- Parameters:
expansionFactor
- the new expansion factor value.- Throws:
MathIllegalArgumentException
- if expansionFactor is invalid input: '<'= 1 or greater than contractionFactor
-
setExpansionMode
Deprecated.As of 3.1. Please usesetExpansionMode(ExpansionMode)
instead.Sets theexpansionMode
. The specified value must be one of ADDITIVE_MODE, MULTIPLICATIVE_MODE.- Parameters:
expansionMode
- The expansionMode to set.- Throws:
MathIllegalArgumentException
- if the specified mode value is not valid.
-
setExpansionMode
Deprecated.As of 3.1 (to be removed in 4.0 as field will become "final").Sets theexpansion mode
.- Parameters:
expansionMode
- Expansion mode to use for resizing the array.
-
setInitialCapacity
@Deprecated protected void setInitialCapacity(int initialCapacity) throws MathIllegalArgumentException Deprecated.As of 3.1, this is a no-op.Sets the initial capacity. Should only be invoked by constructors.- Parameters:
initialCapacity
- of the array- Throws:
MathIllegalArgumentException
- ifinitialCapacity
is not positive.
-
setNumElements
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:
MathIllegalArgumentException
- ifi
is negative.
-
start
Deprecated.As of 3.1.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:
- the starting index.
-
copy
public static void copy(ResizableDoubleArray source, ResizableDoubleArray dest) throws NullArgumentException 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
NullArgumentException
is thrown- Parameters:
source
- ResizableDoubleArray to copydest
- ResizableArray to replace with a copy of the source array- Throws:
NullArgumentException
- if either source or dest is null- 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.
-