Class ArrayUtil

java.lang.Object
toxi.util.datatypes.ArrayUtil

public class ArrayUtil extends Object
A collection of array utilities.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> void
    addArrayToCollection(T[] array, Collection<T> collection)
    Adds all array elements to the given collection of the same type.
    static <T> ArrayList<T>
    arrayToList(T[] array)
    Converts the generic array into an ArrayList of the same type.
    static float[]
    getAsNormalizedFloatArray(int[] source, int bits, int mask, int peak, float target)
    Creates a normalized version of the values of the given int[] array.
    static int
    indexInArray(float needle, float[] stack, int maxLen)
    Returns the index of the element where the given value is found in the array.
    static int
    indexInArray(int needle, int[] stack, int maxLen)
    Returns the index of the element where the given value is found in the array.
    static int
    indexInArray(Object needle, Object[] stack, int maxLen)
    Returns the index of the element where the given value is found in the array.
    static float[]
    normalizeFloatArray(float[] buffer, float peak, float target)
    Normalizes the values in the given array to the new absolute target value.
    static void
    reverse(byte[] array)
    Reverses the item order of the supplied byte array.
    static void
    reverse(char[] array)
    Reverses the item order of the supplied char array.
    static void
    reverse(float[] array)
    Reverses the item order of the supplied float array.
    static void
    reverse(int[] array)
    Reverses the item order of the supplied int array.
    static void
    reverse(short[] array)
    Reverses the item order of the supplied short array.
    static <T> void
    reverse(T[] array)
    Reverses the item order of the supplied array (generic types).
    static <T> void
    shuffle(T[] array)
    Rearranges the array items in random order using the default java.util.Random generator.
    static <T> void
    shuffle(T[] array, Random rnd)
    Rearranges the array items in random order using the given RNG.
    static String
    toString(byte[] array)
     
    static String
    toString(double[] array)
     
    static String
    toString(float[] array)
     
    static String
    toString(int[] array)
     
    static <T> String
    toString(T[] array)
     

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ArrayUtil

      public ArrayUtil()
  • Method Details

    • addArrayToCollection

      public static <T> void addArrayToCollection(T[] array, Collection<T> collection)
      Adds all array elements to the given collection of the same type.
      Type Parameters:
      T -
      Parameters:
      array - array
      collection - existing collection or null (to create a new ArrayList automatically)
    • arrayToList

      public static <T> ArrayList<T> arrayToList(T[] array)
      Converts the generic array into an ArrayList of the same type.
      Parameters:
      array -
      Returns:
      array list version
    • getAsNormalizedFloatArray

      public static float[] getAsNormalizedFloatArray(int[] source, int bits, int mask, int peak, float target)
      Creates a normalized version of the values of the given int[] array. Supports packed integers (e.g. ARGB data) by allowing to specify a bitshift amount & bitmask, e.g. do this to get the normalized representation of the red channel of an ARGB array:
       
       // use 16 bits as shift offset for accessing red channel
       float[] red = ArrayUtil.getAsNormalizedFloatArray(argbPixels, 16, 255, 255);
       
      Parameters:
      source - source data
      bits - number of bits to right shift each value
      mask - bitmask to apply after bitshifting
      peak - peak value (in the source domain) to normalize against
      target - peak of the normalized values
      Returns:
      normalized values
    • indexInArray

      public static int indexInArray(float needle, float[] stack, int maxLen)
      Returns the index of the element where the given value is found in the array.
      Parameters:
      needle - number to find
      stack - array to search
      maxLen - number of elements to search
      Returns:
      array index or -1 if value couldn't be found in array
    • indexInArray

      public static int indexInArray(int needle, int[] stack, int maxLen)
      Returns the index of the element where the given value is found in the array.
      Parameters:
      needle - number to find
      stack - array to search
      maxLen - number of elements to search
      Returns:
      array index or -1 if value couldn't be found in array
    • indexInArray

      public static int indexInArray(Object needle, Object[] stack, int maxLen)
      Returns the index of the element where the given value is found in the array. The comparison uses Object.equals(Object).
      Parameters:
      needle - number to find
      stack - array to search
      maxLen - number of elements to search
      Returns:
      array index or -1 if value couldn't be found in array
    • normalizeFloatArray

      public static float[] normalizeFloatArray(float[] buffer, float peak, float target)
      Normalizes the values in the given array to the new absolute target value. The original values are overridden.
      Parameters:
      buffer - array
      peak - current peak in the source domain
      target - new peak in the target domain
      Returns:
      normalized array
    • reverse

      public static void reverse(byte[] array)
      Reverses the item order of the supplied byte array.
      Parameters:
      array -
    • reverse

      public static void reverse(char[] array)
      Reverses the item order of the supplied char array.
      Parameters:
      array -
    • reverse

      public static void reverse(float[] array)
      Reverses the item order of the supplied float array.
      Parameters:
      array -
    • reverse

      public static void reverse(int[] array)
      Reverses the item order of the supplied int array.
      Parameters:
      array -
    • reverse

      public static void reverse(short[] array)
      Reverses the item order of the supplied short array.
      Parameters:
      array -
    • reverse

      public static <T> void reverse(T[] array)
      Reverses the item order of the supplied array (generic types).
      Parameters:
      array -
    • shuffle

      public static <T> void shuffle(T[] array)
      Rearranges the array items in random order using the default java.util.Random generator. Operation is in-place, no copy is created.
      Parameters:
      array -
    • shuffle

      public static <T> void shuffle(T[] array, Random rnd)
      Rearranges the array items in random order using the given RNG. Operation is in-place, no copy is created.
      Parameters:
      array -
      rnd -
    • toString

      public static String toString(byte[] array)
    • toString

      public static String toString(double[] array)
    • toString

      public static String toString(float[] array)
    • toString

      public static String toString(int[] array)
    • toString

      public static <T> String toString(T[] array)