Class ArrayBuilder
The ArrayBuilder
class provides a collection of static utility methods to construct, initialize,
and manipulate arrays in various ways. It is designed to simplify array handling tasks,
such as creating arrays with default values, filling arrays with specific values, and
generating ranges of numbers.
This class supports multiple array types, including primitive types (int[]
, double[]
) as well as
Complex128
and Complex64
.
Example Usage:
// Ensure an array exists, if not create an array of size 10.
int[] myArray = ArrayBuilder.getOrCreateArray(null, 10);
// Create a range of integers.
int[] range = ArrayBuilder.intRange(0, 10);
// Fill an array with a specific value.
int[] filledArray = ArrayBuilder.filledArray(5, 2);
// Perform a strided fill of zeros values.
double[] stridedArray = new double[10];
ArrayBuilder.stridedFillZeros(stridedArray, 2, 3);
Restrictions:
- This class is not designed for jagged (non-rectangular) arrays. All methods dealing with multidimensional arrays assume, without an explicit check, that all arrays are rectangular.
Note: This class is a utility class and cannot be instantiated.
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
fill
(double[][] dest, double fillValue) Fills an array with the specified value;static void
fill
(Complex128[] dest, double fillValue) Fills an array with specified value.static void
fill
(Complex128[] dest, double fillValue, int from, int to) Fills range of an array with specified value.static <T> void
fill
(T[][] dest, T fillValue) Fills an array with specified value.static int[]
filledArray
(int size, int value) Constructs an integer array filled with a specific value.static void
fillZeros
(Complex128[][] dest) Fills an array of complex numbers with zeros.static double[]
getOrCreateArray
(double[] arr, int size) Checks if an array isnull
and constructs a new array with the specifiedsize
if so.static int[]
getOrCreateArray
(int[] arr, int size) Checks if an array isnull
and constructs a new array with the specifiedsize
if so.static <T> T[]
getOrCreateArray
(T[] array, Supplier<T[]> initializer) Checks if an array isnull
and constructs a new array using the specifiedinitializer
if so.static int[]
intRange
(int start, int end) Gets an array filled with integers fromstart
(inclusive) toend
(exclusive)static int[]
intRange
(int start, int end, int stride) Gets an array filled with integers fromstart
(inclusive) toend
(exclusive) where each int is repeatedstride
times.static double[]
range
(int start, int end) Gets an array filled with integers fromstart
(inclusive) toend
(exclusive)static void
stridedFillZeros
(double[] dest, int start, int stride) Fills an array with zeros separated by the given stride.static void
stridedFillZeros
(double[] dest, int start, int length, int stride) Fills an array with a range of zeros, each separated by the given stride.static void
stridedFillZeros
(Complex128[] dest, int start, int stride) Fills an array with zeros separated by the given stride.static void
stridedFillZeros
(Complex128[] dest, int start, int n, int stride) Fills an array with a range of zeros, each separated by the given stride.
-
Method Details
-
getOrCreateArray
public static int[] getOrCreateArray(int[] arr, int size) Checks if an array isnull
and constructs a new array with the specifiedsize
if so.- Parameters:
arr
- Array of interest.size
- Size of the array to construct and return in the event thatarr == null
.- Returns:
- If
arr == null
then a new array with lengthsize
is created and returned. Otherwise, ifarr != null
then a reference toarr
is returned.
-
getOrCreateArray
public static double[] getOrCreateArray(double[] arr, int size) Checks if an array isnull
and constructs a new array with the specifiedsize
if so.- Parameters:
arr
- Array of interest.size
- Size of the array to construct and return in the event thatarr == null
.- Returns:
- If
arr == null
then a new array with lengthsize
is created and returned. Otherwise, ifarr != null
then a reference toarr
is returned.
-
getOrCreateArray
Checks if an array isnull
and constructs a new array using the specifiedinitializer
if so.- Parameters:
initializer
- Supplier which constructs a new array of desired size in the casearr==null
.arr
- Array of interest.- Returns:
- If
arr == null
then a new array is created byinitializer
and returned. Otherwise, ifarr != null
then a reference toarr
is returned.
-
fillZeros
Fills an array of complex numbers with zeros.- Parameters:
dest
- Array to fill with zeros.
-
stridedFillZeros
public static void stridedFillZeros(double[] dest, int start, int stride) Fills an array with zeros separated by the given stride.
If
stride=3
,start=1
, anddest={1, 2, 3, 4, 5, 6, 7, 8, 9}
then the result will be{1, 0, 3, 4, 0, 6, 7, 0, 9}
.- Parameters:
dest
- Array to fill with strided zeros.start
- Staring point in array to apply strided zero fill.stride
- Number of elements between each value to set to zero within the destination array.- Throws:
IllegalArgumentException
- If stride is less than 1.IllegalArgumentException
- If start is less than 0.
-
stridedFillZeros
Fills an array with zeros separated by the given stride.
If
stride=3
,start=1
, anddest={1, 2, 3, 4, 5, 6, 7, 8, 9}
then the result will be{1, 0, 3, 4, 0, 6, 7, 0, 9}
.- Parameters:
dest
- Array to fill with strided zeros.start
- Staring point in array to apply strided zero fill.stride
- Number of elements between each value to set to zero within the destination array.- Throws:
IllegalArgumentException
- If stride is less than 1.IllegalArgumentException
- If start is less than 0.
-
stridedFillZeros
Fills an array with a range of zeros, each separated by the given stride. Specifically, the destination array will be filled with several sequential ranges of zeros of specified n. Each range of zeros will be separated by the stride.
If
stride=2
,n=3
,start=1
, anddest={1, 2, 3, 4, 5, 6, 7, 8, 9}
then the result will be {1, 0, 0, 0, 5, 0, 0, 0, 9}.- Parameters:
dest
- Array to fill with strided zeros.start
- Starting point to apply strided zero fill.n
- Number of sequential zeros to fill per stride.stride
- Number of elements between each value to set to zero within the destination array.- Throws:
IllegalArgumentException
- Ifstride
orn
is less than one.IllegalArgumentException
- Ifstart
is less than zero.
-
stridedFillZeros
public static void stridedFillZeros(double[] dest, int start, int length, int stride) Fills an array with a range of zeros, each separated by the given stride. Specifically, the destination array will be filled with several sequential ranges of zeros of specified length. Each range of zeros will be separated by the stride.
If
stride=2
,length=3
,start=1
, anddest={1, 2, 3, 4, 5, 6, 7, 8, 9}
then the result will be {1, 0, 0, 0, 5, 0, 0, 0, 9}.- Parameters:
dest
- Array to fill with strided zeros.start
- Starting point to apply strided zero fill.length
- Number of sequential zeros to fill per stride.stride
- Number of elements between each value to set to zero within the destination array.- Throws:
IllegalArgumentException
- If stride or length is less than one.IllegalArgumentException
- If start is less than zero.
-
fill
Fills an array with specified value.- Parameters:
dest
- Array to fill.fillValue
- Value to fill array with. This will be converted to a member of the field as if bydest[0].getZero().add(fillValue)
-
fill
public static <T> void fill(T[][] dest, T fillValue) Fills an array with specified value.- Parameters:
dest
- Array to fill.fillValue
- Value to fill array with.
-
fill
Fills range of an array with specified value.- Parameters:
dest
- Array to fill.fillValue
- Value to fill array with.from
- Staring index of range (inclusive).to
- Ending index of range (exclusive).
-
fill
public static void fill(double[][] dest, double fillValue) Fills an array with the specified value;- Parameters:
dest
- Array to fill.fillValue
- Value to fill array with.
-
filledArray
public static int[] filledArray(int size, int value) Constructs an integer array filled with a specific value.- Parameters:
size
- Size of the array.value
- Value to set each index of the array.- Returns:
- An array of specified
size
filled with the specifiedvalue
. - Throws:
NegativeArraySizeException
- If is negative.
-
range
public static double[] range(int start, int end) Gets an array filled with integers fromstart
(inclusive) toend
(exclusive)- Parameters:
start
- Staring value (inclusive).end
- Stopping value (exclusive).- Returns:
- An array containing the integer range
[start, end)
. - Throws:
IllegalArgumentException
- Ifend < start
.
-
intRange
public static int[] intRange(int start, int end) Gets an array filled with integers fromstart
(inclusive) toend
(exclusive)- Parameters:
start
- Staring value (inclusive).end
- Stopping value (exclusive).- Returns:
- An array containing the integer range
[start, end)
. - Throws:
IllegalArgumentException
- Ifend < start
.
-
intRange
public static int[] intRange(int start, int end, int stride) Gets an array filled with integers fromstart
(inclusive) toend
(exclusive) where each int is repeatedstride
times.- Parameters:
start
- Staring value (inclusive).end
- Stopping value (exclusive).stride
- Number of times to repeat each integer.- Returns:
- An array containing the integer range
[start, end)
and each integer is repeatedstride
times. - Throws:
NegativeArraySizeException
- Ifstride
is negative.IllegalArgumentException
- Ifstart
is not in[0, end)
-