public class ResizableDoubleArray extends Object implements DoubleArray, Serializable
A variable length DoubleArray
implementation that automatically
handles expanding and contracting its internal storage array as elements
are added and removed.
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.
Modifier and Type | Class and Description |
---|---|
static class |
ResizableDoubleArray.ExpansionMode
Specification of expansion algorithm.
|
Modifier and Type | Field and Description |
---|---|
static int |
ADDITIVE_MODE
Deprecated.
As of 3.1. Please use
ResizableDoubleArray.ExpansionMode.ADDITIVE instead. |
static int |
MULTIPLICATIVE_MODE
Deprecated.
As of 3.1. Please use
ResizableDoubleArray.ExpansionMode.MULTIPLICATIVE instead. |
Constructor and Description |
---|
ResizableDoubleArray()
Creates an instance with default properties.
|
ResizableDoubleArray(double[] initialArray)
Creates an instance from an existing
double[] with the
initial capacity and numElements corresponding to the size of
the supplied double[] 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. Please use
ResizableDoubleArray(int,double) instead. |
ResizableDoubleArray(int initialCapacity,
float expansionFactor,
float contractionCriteria)
Deprecated.
As of 3.1. Please use
ResizableDoubleArray(int,double,double) instead. |
ResizableDoubleArray(int initialCapacity,
float expansionFactor,
float contractionCriteria,
int expansionMode)
Deprecated.
As of 3.1. Please use
ResizableDoubleArray(int,double,double,ExpansionMode,double[])
instead. |
ResizableDoubleArray(ResizableDoubleArray original)
Copy constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
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. Please use
checkContractExpand(double,double) instead. |
void |
clear()
Clear the array contents, resetting the number of elements to zero.
|
double |
compute(MathArrays.Function f)
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.
|
ResizableDoubleArray |
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 the
i initial elements of the array. |
void |
discardMostRecentElements(int i)
Discards the
i last elements of the array. |
boolean |
equals(Object object)
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[] |
getArrayRef()
Provides direct access to the internal storage array.
|
int |
getCapacity()
Gets the currently allocated size of the internal data structure used
for storing elements.
|
float |
getContractionCriteria()
Deprecated.
As of 3.1. Please use
getContractionCriterion()
instead. |
double |
getContractionCriterion()
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 index
|
double[] |
getElements()
Returns a double array containing the elements of this
ResizableArray . |
float |
getExpansionFactor()
Deprecated.
As of 3.1. Return type will be changed to "double" in 4.0.
|
int |
getExpansionMode()
Deprecated.
As of 3.1. Return value to be changed to
ResizableDoubleArray.ExpansionMode in 4.0. |
double[] |
getInternalValues()
Deprecated.
As of 3.1.
|
int |
getNumElements()
Returns the number of elements currently in the array.
|
protected int |
getStartIndex()
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. Please use
setExpansionMode(ExpansionMode) instead. |
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)
Substitutes
value for the most recently added value. |
@Deprecated public static final int ADDITIVE_MODE
ResizableDoubleArray.ExpansionMode.ADDITIVE
instead.@Deprecated public static final int MULTIPLICATIVE_MODE
ResizableDoubleArray.ExpansionMode.MULTIPLICATIVE
instead.public ResizableDoubleArray()
initialCapacity = 16
expansionMode = MULTIPLICATIVE
expansionFactor = 2.0
contractionCriterion = 2.5
public ResizableDoubleArray(int initialCapacity) throws MathIllegalArgumentException
expansionMode = MULTIPLICATIVE
expansionFactor = 2.0
contractionCriterion = 2.5
initialCapacity
- Initial size of the internal storage array.MathIllegalArgumentException
- if initialCapacity <= 0
.public ResizableDoubleArray(double[] initialArray)
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 = 16
expansionMode = MULTIPLICATIVE
expansionFactor = 2.0
contractionCriterion = 2.5
initialArray
- initial array@Deprecated public ResizableDoubleArray(int initialCapacity, float expansionFactor) throws MathIllegalArgumentException
ResizableDoubleArray(int,double)
instead.expansionMode = MULTIPLICATIVE
contractionCriterion = 0.5 + expansionFactor
initialCapacity > 0
expansionFactor > 1
initialCapacity
- Initial size of the internal storage array.expansionFactor
- The array will be expanded based on this
parameter.MathIllegalArgumentException
- if parameters are not valid.public ResizableDoubleArray(int initialCapacity, double expansionFactor) throws MathIllegalArgumentException
expansionMode = MULTIPLICATIVE
contractionCriterion = 0.5 + expansionFactor
initialCapacity > 0
expansionFactor > 1
initialCapacity
- Initial size of the internal storage array.expansionFactor
- The array will be expanded based on this
parameter.MathIllegalArgumentException
- if parameters are not valid.@Deprecated public ResizableDoubleArray(int initialCapacity, float expansionFactor, float contractionCriteria) throws MathIllegalArgumentException
ResizableDoubleArray(int,double,double)
instead.MULTIPLICATIVE
.
initialCapacity > 0
expansionFactor > 1
contractionCriterion >= expansionFactor
initialCapacity
- Initial size of the internal storage array..expansionFactor
- The array will be expanded based on this
parameter.contractionCriteria
- Contraction criteria.MathIllegalArgumentException
- if parameters are not valid.public ResizableDoubleArray(int initialCapacity, double expansionFactor, double contractionCriterion) throws MathIllegalArgumentException
MULTIPLICATIVE
.
initialCapacity > 0
expansionFactor > 1
contractionCriterion >= expansionFactor
initialCapacity
- Initial size of the internal storage array..expansionFactor
- The array will be expanded based on this
parameter.contractionCriterion
- Contraction criterion.MathIllegalArgumentException
- if the parameters are not valid.@Deprecated public ResizableDoubleArray(int initialCapacity, float expansionFactor, float contractionCriteria, int expansionMode) throws MathIllegalArgumentException
ResizableDoubleArray(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}
initialCapacity
- the initial size of the internal storage arrayexpansionFactor
- the array will be expanded based on this
parametercontractionCriteria
- the contraction CriteriaexpansionMode
- the expansion modeMathIllegalArgumentException
- if parameters are not validpublic ResizableDoubleArray(int initialCapacity, double expansionFactor, double contractionCriterion, ResizableDoubleArray.ExpansionMode expansionMode, double... data) throws MathIllegalArgumentException
initialCapacity > 0
expansionFactor > 1
contractionCriterion >= expansionFactor
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.MathIllegalArgumentException
- if the parameters are not valid.public ResizableDoubleArray(ResizableDoubleArray original) throws NullArgumentException
NullArgumentException
is thrown.original
- array to copyNullArgumentException
- if original is nullpublic void addElement(double value)
addElement
in interface DoubleArray
value
- Value to be added to end of array.public void addElements(double[] values)
addElements
in interface DoubleArray
values
- Values to be added to end of array.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.
addElementRolling
in interface DoubleArray
value
- Value to be added to the array.public double substituteMostRecentElement(double value) throws MathIllegalStateException
value
for the most recently added value.
Returns the value that has been replaced. If the array is empty (i.e.
if numElements
is zero), an IllegalStateException is thrown.value
- New value to substitute for the most recently added valueMathIllegalStateException
- if the array is empty@Deprecated protected void checkContractExpand(float contraction, float expansion) throws MathIllegalArgumentException
checkContractExpand(double,double)
instead.expansion
- factor to be checkedcontraction
- criteria to be checkedMathIllegalArgumentException
- if the contractionCriteria is less than
the expansionCriteria.protected void checkContractExpand(double contraction, double expansion) throws NumberIsTooSmallException
contraction
- Criterion to be checked.expansion
- Factor to be checked.NumberIsTooSmallException
- if contraction < expansion
.NumberIsTooSmallException
- if contraction <= 1
.NumberIsTooSmallException
- if expansion <= 1
.public void clear()
clear
in interface DoubleArray
public void contract()
public void discardFrontElements(int i) throws MathIllegalArgumentException
i
initial 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. Throws illegalArgumentException
if i exceeds numElements.i
- the number of elements to discard from the front of the arrayMathIllegalArgumentException
- if i is greater than numElements.public void discardMostRecentElements(int i) throws MathIllegalArgumentException
i
last 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. Throws illegalArgumentException
if i exceeds numElements.i
- the number of elements to discard from the end of the arrayMathIllegalArgumentException
- if i is greater than numElements.protected void expand()
if expansionMode
is set to MULTIPLICATIVE_MODE,
the new array size will be internalArray.length * expansionFactor.
If expansionMode
is set to ADDITIVE_MODE, the length
after expansion will be internalArray.length + expansionFactor
@Deprecated public float getContractionCriteria()
getContractionCriterion()
instead.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.
public double getContractionCriterion()
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.
public double getElement(int index)
getElement
in interface DoubleArray
index
- index to fetch a value fromArrayIndexOutOfBoundsException
- if index
is less than
zero or is greater than getNumElements() - 1
.public double[] getElements()
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.
getElements
in interface DoubleArray
@Deprecated public float getExpansionFactor()
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.@Deprecated public int getExpansionMode()
ResizableDoubleArray.ExpansionMode
in 4.0.public int getCapacity()
the number of
elements actually stored
.public int getNumElements()
getNumElements
in interface DoubleArray
@Deprecated public double[] getInternalValues()
startIndex
is
required (available via the start()
method). This method should
only be used in cases where copying the internal array is not practical.
The getElements()
method should be used in all other cases.protected double[] getArrayRef()
getStartIndex
method.
getElements()
method has no such limitation since it
returns a copy of this array's addressable elements.protected int getStartIndex()
getStartIndex()
,
getStartIndex()
+ getNumElements()
- 1].@Deprecated public void setContractionCriteria(float contractionCriteria) throws MathIllegalArgumentException
contractionCriteria
- contraction criteriaMathIllegalArgumentException
- if the contractionCriteria is less than
the expansionCriteria.public double compute(MathArrays.Function f)
f
- Function to be applied on this array.public void setElement(int index, double value)
getNumElements() - 1
, the numElements
property
is increased to index +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).setElement
in interface DoubleArray
index
- index to store a value invalue
- value to store at the specified indexArrayIndexOutOfBoundsException
- if index < 0
.@Deprecated public void setExpansionFactor(float expansionFactor) throws MathIllegalArgumentException
expansionFactor > 1
contractionFactor >= expansionFactor
expansionFactor
- the new expansion factor value.MathIllegalArgumentException
- if expansionFactor is <= 1 or greater
than contractionFactor@Deprecated public void setExpansionMode(int expansionMode) throws MathIllegalArgumentException
setExpansionMode(ExpansionMode)
instead.expansionMode
. The specified value must be one of
ADDITIVE_MODE, MULTIPLICATIVE_MODE.expansionMode
- The expansionMode to set.MathIllegalArgumentException
- if the specified mode value is not valid.@Deprecated public void setExpansionMode(ResizableDoubleArray.ExpansionMode expansionMode)
expansion mode
.expansionMode
- Expansion mode to use for resizing the array.@Deprecated protected void setInitialCapacity(int initialCapacity) throws MathIllegalArgumentException
initialCapacity
- of the arrayMathIllegalArgumentException
- if initialCapacity
is not
positive.public void setNumElements(int i) throws MathIllegalArgumentException
i
- a new number of elementsMathIllegalArgumentException
- if i
is negative.@Deprecated public int start()
internalArray[startIndex],...,internalArray[startIndex + numElements -1]
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
source
- ResizableDoubleArray to copydest
- ResizableArray to replace with a copy of the source arrayNullArgumentException
- if either source or dest is nullpublic ResizableDoubleArray copy()
public boolean equals(Object object)
Copyright © 2003–2016 The Apache Software Foundation. All rights reserved.