Class ArrayBuilder

java.lang.Object
org.flag4j.util.ArrayBuilder

public final class ArrayBuilder extends Object

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 Type
    Method
    Description
    static 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
    Fills an array of complex numbers with zeros.
    static double[]
    getOrCreateArray(double[] arr, int size)
    Checks if an array is null and constructs a new array with the specified size if so.
    static int[]
    getOrCreateArray(int[] arr, int size)
    Checks if an array is null and constructs a new array with the specified size if so.
    static <T> T[]
    getOrCreateArray(T[] array, Supplier<T[]> initializer)
    Checks if an array is null and constructs a new array using the specified initializer if so.
    static int[]
    intRange(int start, int end)
    Gets an array filled with integers from start (inclusive) to end (exclusive)
    static int[]
    intRange(int start, int end, int stride)
    Gets an array filled with integers from start (inclusive) to end (exclusive) where each int is repeated stride times.
    static double[]
    range(int start, int end)
    Gets an array filled with integers from start (inclusive) to end (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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getOrCreateArray

      public static int[] getOrCreateArray(int[] arr, int size)
      Checks if an array is null and constructs a new array with the specified size if so.
      Parameters:
      arr - Array of interest.
      size - Size of the array to construct and return in the event that arr == null.
      Returns:
      If arr == null then a new array with length size is created and returned. Otherwise, if arr != null then a reference to arr is returned.
    • getOrCreateArray

      public static double[] getOrCreateArray(double[] arr, int size)
      Checks if an array is null and constructs a new array with the specified size if so.
      Parameters:
      arr - Array of interest.
      size - Size of the array to construct and return in the event that arr == null.
      Returns:
      If arr == null then a new array with length size is created and returned. Otherwise, if arr != null then a reference to arr is returned.
    • getOrCreateArray

      public static <T> T[] getOrCreateArray(T[] array, Supplier<T[]> initializer)
      Checks if an array is null and constructs a new array using the specified initializer if so.
      Parameters:
      initializer - Supplier which constructs a new array of desired size in the case arr==null.
      arr - Array of interest.
      Returns:
      If arr == null then a new array is created by initializer and returned. Otherwise, if arr != null then a reference to arr is returned.
    • fillZeros

      public static void fillZeros(Complex128[][] dest)
      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, and dest={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

      public static void stridedFillZeros(Complex128[] dest, int start, int stride)

      Fills an array with zeros separated by the given stride.

      If stride=3, start=1, and dest={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

      public 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. 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, and dest={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 - If stride or n is less than one.
      IllegalArgumentException - If start 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, and dest={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

      public static void fill(Complex128[] dest, double fillValue)
      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 by dest[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

      public static void fill(Complex128[] dest, double fillValue, int from, int to)
      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 specified value.
      Throws:
      NegativeArraySizeException - If is negative.
    • range

      public static double[] range(int start, int end)
      Gets an array filled with integers from start (inclusive) to end (exclusive)
      Parameters:
      start - Staring value (inclusive).
      end - Stopping value (exclusive).
      Returns:
      An array containing the integer range [start, end).
      Throws:
      IllegalArgumentException - If end < start.
    • intRange

      public static int[] intRange(int start, int end)
      Gets an array filled with integers from start (inclusive) to end (exclusive)
      Parameters:
      start - Staring value (inclusive).
      end - Stopping value (exclusive).
      Returns:
      An array containing the integer range [start, end).
      Throws:
      IllegalArgumentException - If end < start.
    • intRange

      public static int[] intRange(int start, int end, int stride)
      Gets an array filled with integers from start (inclusive) to end (exclusive) where each int is repeated stride 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 repeated stride times.
      Throws:
      NegativeArraySizeException - If stride is negative.
      IllegalArgumentException - If start is not in [0, end)