Package net.algart.arrays
Class DoubleJArrayHolder
java.lang.Object
net.algart.arrays.DoubleJArrayHolder
A simple class allowing to reuse Java double[] array many times.
It can be useful, when the algorithm usually allocates double[] array with the same size
many times (changing the size is a rare event). In this case, you can replace "new" operator
(that spends time for zero-initialing new array) with
quickNew(int)
method,
that probably will work very quickly.
The previously allocated array is stored inside the object in a SoftReference.
This class is thread-safe: you may use the same instance of this class in several threads.
- Author:
- Daniel Alievsky
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptiondouble[]
quickClone
(double[] array) Quick analog of array.clone().double[]
quickNew
(int newArrayLength) Quick analog of new double[newArrayLength]}.double[]
quickNew
(long newArrayLength) Equivalent ofquickNew(int)
method, but in addition it checks that newArrayLength is actually 32-bit value (newArrayLength==(int)newArrayLength) and, if not, throwsTooLargeArrayException
.double[]
-
Constructor Details
-
DoubleJArrayHolder
public DoubleJArrayHolder()
-
-
Method Details
-
quickNew
- Parameters:
matrix
- some AlgART matrix.- Returns:
- newly created "new double[newArrayLength]" or previously allocated array, if it exists and has identical length.
- Throws:
NullPointerException
- if the argument is null.TooLargeArrayException
- if matrix.size() > Integer.MAX_VALUE.
-
quickNew
public double[] quickNew(long newArrayLength) Equivalent ofquickNew(int)
method, but in addition it checks that newArrayLength is actually 32-bit value (newArrayLength==(int)newArrayLength) and, if not, throwsTooLargeArrayException
.- Parameters:
newArrayLength
- required array length.- Returns:
- newly created "new double[newArrayLength]" or previously allocated array, if it exists and has identical length.
- Throws:
IllegalArgumentException
- if newArrayLength < 0.TooLargeArrayException
- if newArrayLength > Integer.MAX_VALUE.
-
quickNew
public double[] quickNew(int newArrayLength) Quick analog of new double[newArrayLength]}. If this method is called several times for allocating data with the same size, this method returns previously allocated array. (Previous array is stored in SoftReference and, if there is not enough memory, can be utilized by garbage collector; in this case, this method will just use "new" operator.)Please remember: unlike standard new operator, the returned array is usually not filled by zeros.
- Parameters:
newArrayLength
- required array length.- Returns:
- newly created "new double[newArrayLength]" or previously allocated array, if it exists and has identical length.
- Throws:
IllegalArgumentException
- if newArrayLength < 0
-
quickClone
public double[] quickClone(double[] array) Quick analog of array.clone(). Equivalent to the following operators:double[] result =
quickNew(int)
quickNew}(array.length); System.arraycopy(array, 0, result, 0, array.length); (return result)- Parameters:
array
- some array to clone.- Returns:
- exact copy of the source array.
- Throws:
NullPointerException
- if the passed array is null.
-