Class ArrayLib

java.lang.Object
org.opensourcephysics.numerics.ArrayLib

public abstract class ArrayLib extends Object
Library of supplementary array routines not supported by the java.util.Arrays class.
Author:
jeffrey heer
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static int
     
    static int
     
    static final int
    Arrays with lengths beneath this value will be insertion sorted.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static final int
    binarySearch(int[] a, int key)
    Perform a binary search over a sorted array for the given key.
    static final int
    binarySearch(int[] a, int key, int length)
    Perform a binary search over a sorted range of an array for the given key.
    static final int
    binarySearch(int[] a, int key, int begin, int end)
    Perform a binary search over a sorted range of an array for the given key.
    static final int
    Perform a binary search over a sorted array for the given key.
    static final int
    binarySearch(Object[] a, Object key, int length)
    Perform a binary search over a sorted range of an array for the given key.
    static final int
    binarySearch(Object[] a, Object key, int begin, int end)
    Perform a binary search over a sorted range of an array for the given key.
    static final int
    Perform a binary search over a sorted array for the given key.
    static final int
    binarySearch(Object[] a, Object key, Comparator<Object> cp, int length)
    Perform a binary search over a sorted range of an array for the given key.
    static final int
    binarySearch(Object[] a, Object key, Comparator<Object> cp, int begin, int end)
    Perform a binary search over a sorted range of an array for the given key.
    static final int
    find(int[] a, int key)
    Linearly search an array for a given key value.
    static final int
    find(int[] a, int key, int length)
    Linearly search an array range for a given key value.
    static final int
    find(int[] a, int key, int begin, int end)
    Linearly search an array range for a given key value.
    static final int
     
    static final int
     
    static int[]
    getIntArray(String filename)
    Read in a text file as an array of integers.
    protected static final void
    insertionsort(double[] a, int[] b, int p, int r)
     
    protected static final void
    insertionsort(float[] a, int[] b, int p, int r)
     
    protected static final void
    insertionsort(int[] a, double[] b, int p, int r)
     
    protected static final void
    insertionsort(int[] a, int[] b, int p, int r)
     
    protected static final void
    insertionsort(int[] a, Object[] b, int p, int r)
     
    protected static final void
    insertionsort(Object[] a, int[] b, int p, int r, Comparator<Object> cmp)
     
    static final double
    max(double[] a)
    Find the maximum value in an array.
    protected static final void
    merge(double[] a, int[] b, int p, int q, int r)
     
    protected static final void
    merge(float[] a, int[] b, int p, int q, int r)
     
    protected static final void
    merge(int[] a, double[] b, int p, int q, int r)
     
    protected static final void
    merge(int[] a, int[] b, int p, int q, int r)
     
    protected static final void
    merge(int[] a, Object[] b, int p, int q, int r)
     
    protected static final void
    merge(Object[] a, int[] b, int p, int q, int r, Comparator<Object> cmp)
     
    protected static final void
    mergesort(double[] a, int[] b, int p, int r)
     
    protected static final void
    mergesort(float[] a, int[] b, int p, int r)
     
    protected static final void
    mergesort(int[] a, double[] b, int p, int r)
     
    protected static final void
    mergesort(int[] a, int[] b, int p, int r)
     
    protected static void
    mergesort(int[] ks, int[] kd, Object[] vs, Object[] vd, int lo, int hi, int off)
     
    protected static final void
    mergesort(Object[] a, int[] b, int p, int r, Comparator<Object> cmp)
     
    static final double
    min(double[] a)
    Find the minimum value in an array.
    static final double[]
    resize(double[] a, int size)
    Resize the given array as needed to meet a target size.
    static final float[]
    resize(float[] a, int size)
    Resize the given array as needed to meet a target size.
    static final int[]
    resize(int[] a, int size)
    Resize the given array as needed to meet a target size.
    static final Object[]
    resize(Object[] a, int size)
    Resize the given array as needed to meet a target size.
    static final void
    sort(double[] a, int[] b)
    Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
    static final void
    sort(double[] a, int[] b, int length)
    Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
    static final void
    sort(double[] a, int[] b, int begin, int end)
    Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
    static final void
    sort(float[] a, int[] b)
    Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
    static final void
    sort(float[] a, int[] b, int length)
    Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
    static final void
    sort(float[] a, int[] b, int begin, int end)
    Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
    static final void
    sort(int[] a, double[] b)
    Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
    static final void
    sort(int[] a, double[] b, int length)
    Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
    static final void
    sort(int[] a, double[] b, int begin, int end)
    Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
    static final void
    sort(int[] a, int[] b)
    Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
    static final void
    sort(int[] a, int[] b, int length)
    Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
    static final void
    sort(int[] a, int[] b, int begin, int end)
    Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
    static final void
    sort(int[] a, Object[] b, int[] abuf, Object[] bbuf, int begin, int end)
    Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
    static final void
    sort(int[] a, Object[] b, int begin, int end)
    Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
    static final void
    sort(Object[] a, int[] b, int begin, int end, Comparator<Object> cmp)
    Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
    static final void
    sort(Object[] a, int[] b, int length, Comparator<Object> cmp)
    Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
    static final void
    sort(Object[] a, int[] b, Comparator<Object> cmp)
    Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
    static final double
    sum(double[] a)
    Compute the sum of the values in an array.
    static final double[]
    trim(double[] a, int size)
    Trims an array to be exactly the target a size.
    static final float[]
    trim(float[] a, int size)
    Trims an array to be exactly the target a size.
    static final int[]
    trim(int[] a, int size)
    Trims an array to be exactly the target a size.
    static final Object[]
    trim(Object[] a, int size)
    Trims an array to be exactly the target a size.

    Methods inherited from class java.lang.Object

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

    • IndexMax

      public static int IndexMax
    • IndexMin

      public static int IndexMin
    • SORT_THRESHOLD

      public static final int SORT_THRESHOLD
      Arrays with lengths beneath this value will be insertion sorted.
      See Also:
  • Constructor Details

    • ArrayLib

      public ArrayLib()
  • Method Details

    • max

      public static final double max(double[] a)
      Find the maximum value in an array.
      Parameters:
      a - the array
      indMax - the array minimum index
      Returns:
      the maximum value in the array
    • min

      public static final double min(double[] a)
      Find the minimum value in an array.
      Parameters:
      a - the array
      indMin - the array minimum index
      Returns:
      the minimum value in the array
    • getIndexMax

      public static final int getIndexMax()
      Parameters:
      IndexMax - the array minimum index
      Returns:
      IndexMax
    • getIndexMin

      public static final int getIndexMin()
      Parameters:
      IndexMin - the array minimum index
      Returns:
      IndexMin
    • sum

      public static final double sum(double[] a)
      Compute the sum of the values in an array.
      Parameters:
      a - the array
      Returns:
      the sum of the values in the array
    • binarySearch

      public static final int binarySearch(int[] a, int key)
      Perform a binary search over a sorted array for the given key.
      Parameters:
      a - the array to search
      key - the key to search for
      Returns:
      the index of the given key if it exists in the array, otherwise -1 times the index value at the insertion point that would be used if the key were added to the array.
    • binarySearch

      public static final int binarySearch(int[] a, int key, int length)
      Perform a binary search over a sorted range of an array for the given key. The range is assumed to start at index 0.
      Parameters:
      a - the array to search
      key - the key to search for
      length - the the length of the range to search over.
      Returns:
      the index of the given key if it exists in the array, otherwise -1 times the index value at the insertion point that would be used if the key were added to the array.
    • binarySearch

      public static final int binarySearch(int[] a, int key, int begin, int end)
      Perform a binary search over a sorted range of an array for the given key.
      Parameters:
      a - the array to search
      key - the key to search for
      begin - the starting index of the range
      end - the ending index of the range, exclusive
      Returns:
      the index of the given key if it exists in the array, otherwise -1 times the index value at the insertion point that would be used if the key were added to the array.
    • binarySearch

      public static final int binarySearch(Object[] a, Object key)
      Perform a binary search over a sorted array for the given key.
      Parameters:
      a - the array to search
      key - the key to search for
      Returns:
      the index of the given key if it exists in the array, otherwise -1 times the index value at the insertion point that would be used if the key were added to the array.
    • binarySearch

      public static final int binarySearch(Object[] a, Object key, int length)
      Perform a binary search over a sorted range of an array for the given key. The range is assumed to start at index 0.
      Parameters:
      a - the array to search
      key - the key to search for
      length - the the length of the range to search over.
      Returns:
      the index of the given key if it exists in the array, otherwise -1 times the index value at the insertion point that would be used if the key were added to the array.
    • binarySearch

      public static final int binarySearch(Object[] a, Object key, int begin, int end)
      Perform a binary search over a sorted range of an array for the given key.
      Parameters:
      a - the array to search
      key - the key to search for
      begin - the starting index of the range
      end - the ending index of the range, exclusive
      Returns:
      the index of the given key if it exists in the array, otherwise -1 times the index value at the insertion point that would be used if the key were added to the array.
    • binarySearch

      public static final int binarySearch(Object[] a, Object key, Comparator<Object> cp)
      Perform a binary search over a sorted array for the given key.
      Parameters:
      a - the array to search
      key - the key to search for
      cp - the comparator to use to compare key values
      Returns:
      the index of the given key if it exists in the array, otherwise -1 times the index value at the insertion point that would be used if the key were added to the array.
    • binarySearch

      public static final int binarySearch(Object[] a, Object key, Comparator<Object> cp, int length)
      Perform a binary search over a sorted range of an array for the given key. The range is assumed to start at index 0.
      Parameters:
      a - the array to search
      key - the key to search for
      cp - the comparator to use to compare key values
      length - the the length of the range to search over.
      Returns:
      the index of the given key if it exists in the array, otherwise -1 times the index value at the insertion point that would be used if the key were added to the array.
    • binarySearch

      public static final int binarySearch(Object[] a, Object key, Comparator<Object> cp, int begin, int end)
      Perform a binary search over a sorted range of an array for the given key.
      Parameters:
      a - the array to search
      key - the key to search for
      cp - the comparator to use to compare key values
      begin - the starting index of the range
      end - the ending index of the range, exclusive
      Returns:
      the index of the given key if it exists in the array, otherwise -1 times the index value at the insertion point that would be used if the key were added to the array.
    • find

      public static final int find(int[] a, int key)
      Linearly search an array for a given key value.
      Parameters:
      a - the array to search
      key - the key to search for
      Returns:
      the index of the first occurrence of the key in the array, of -1 if the key is not found.
    • find

      public static final int find(int[] a, int key, int length)
      Linearly search an array range for a given key value. Assumes that the range begins at index 0.
      Parameters:
      a - the array to search
      key - the key to search for
      length - the length of the range to search over
      Returns:
      the index of the first occurrence of the key in the array, of -1 if the key is not found.
    • find

      public static final int find(int[] a, int key, int begin, int end)
      Linearly search an array range for a given key value.
      Parameters:
      a - the array to search
      key - the key to search for
      begin - the starting index of the range
      end - the ending index of the range, exclusive
      Returns:
      the index of the first occurrence of the key in the array, of -1 if the key is not found.
    • resize

      public static final int[] resize(int[] a, int size)
      Resize the given array as needed to meet a target size.
      Parameters:
      a - the array to potentially resize
      size - the minimum size of the target array
      Returns:
      the resized array, if the original array meets the size requirement, it is simply return, otherwise a new array is allocated and the contents of the original array are copied over.
    • resize

      public static final float[] resize(float[] a, int size)
      Resize the given array as needed to meet a target size.
      Parameters:
      a - the array to potentially resize
      size - the minimum size of the target array
      Returns:
      the resized array, if the original array meets the size requirement, it is simply return, otherwise a new array is allocated and the contents of the original array are copied over.
    • resize

      public static final double[] resize(double[] a, int size)
      Resize the given array as needed to meet a target size.
      Parameters:
      a - the array to potentially resize
      size - the minimum size of the target array
      Returns:
      the resized array, if the original array meets the size requirement, it is simply return, otherwise a new array is allocated and the contents of the original array are copied over.
    • resize

      public static final Object[] resize(Object[] a, int size)
      Resize the given array as needed to meet a target size.
      Parameters:
      a - the array to potentially resize
      size - the minimum size of the target array
      Returns:
      the resized array, if the original array meets the size requirement, it is simply return, otherwise a new array is allocated and the contents of the original array are copied over.
    • trim

      public static final int[] trim(int[] a, int size)
      Trims an array to be exactly the target a size.
      Parameters:
      a - the array to trim
      size - the desired size of the array. This value must be lesser than or equal to the size of the input array.
      Returns:
      a trimmed array instance
    • trim

      public static final float[] trim(float[] a, int size)
      Trims an array to be exactly the target a size.
      Parameters:
      a - the array to trim
      size - the desired size of the array. This value must be lesser than or equal to the size of the input array.
      Returns:
      a trimmed array instance
    • trim

      public static final double[] trim(double[] a, int size)
      Trims an array to be exactly the target a size.
      Parameters:
      a - the array to trim
      size - the desired size of the array. This value must be lesser than or equal to the size of the input array.
      Returns:
      a trimmed array instance
    • trim

      public static final Object[] trim(Object[] a, int size)
      Trims an array to be exactly the target a size.
      Parameters:
      a - the array to trim
      size - the desired size of the array. This value must be lesser than or equal to the size of the input array.
      Returns:
      a trimmed array instance
    • sort

      public static final void sort(int[] a, double[] b)
      Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
      Parameters:
      a - the array to sort by
      b - the array to re-arrange based on the sort order of the first array.
    • sort

      public static final void sort(int[] a, double[] b, int length)
      Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
      Parameters:
      a - the array to sort by
      b - the array to re-arrange based on the sort order of the first array.
      length - the array range length to sort over
    • sort

      public static final void sort(int[] a, double[] b, int begin, int end)
      Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
      Parameters:
      a - the array to sort by
      b - the array to re-arrange based on the sort order of the first array.
      begin - the start index of the range to sort
      end - the end index, exclusive, of the range to sort
    • insertionsort

      protected static final void insertionsort(int[] a, double[] b, int p, int r)
    • mergesort

      protected static final void mergesort(int[] a, double[] b, int p, int r)
    • merge

      protected static final void merge(int[] a, double[] b, int p, int q, int r)
    • sort

      public static final void sort(int[] a, int[] b)
      Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
      Parameters:
      a - the array to sort by
      b - the array to re-arrange based on the sort order of the first array.
    • sort

      public static final void sort(int[] a, int[] b, int length)
      Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
      Parameters:
      a - the array to sort by
      b - the array to re-arrange based on the sort order of the first array.
      length - the array range length to sort over
    • sort

      public static final void sort(int[] a, int[] b, int begin, int end)
      Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
      Parameters:
      a - the array to sort by
      b - the array to re-arrange based on the sort order of the first array.
      begin - the start index of the range to sort
      end - the end index, exclusive, of the range to sort
    • insertionsort

      protected static final void insertionsort(int[] a, int[] b, int p, int r)
    • mergesort

      protected static final void mergesort(int[] a, int[] b, int p, int r)
    • merge

      protected static final void merge(int[] a, int[] b, int p, int q, int r)
    • sort

      public static final void sort(int[] a, Object[] b, int begin, int end)
      Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
      Parameters:
      a - the array to sort by
      b - the array to re-arrange based on the sort order of the first array.
      begin - the start index of the range to sort
      end - the end index, exclusive, of the range to sort
    • sort

      public static final void sort(int[] a, Object[] b, int[] abuf, Object[] bbuf, int begin, int end)
      Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
      Parameters:
      a - the array to sort by
      b - the array to re-arrange based on the sort order of the first array.
      abuf - a buffer array to perform the sorting without allocating any additional memory
      bbuf - a buffer array to perform the sorting without allocating any additional memory
      begin - the start index of the range to sort
      end - the end index, exclusive, of the range to sort
    • insertionsort

      protected static final void insertionsort(int[] a, Object[] b, int p, int r)
    • mergesort

      protected static void mergesort(int[] ks, int[] kd, Object[] vs, Object[] vd, int lo, int hi, int off)
    • merge

      protected static final void merge(int[] a, Object[] b, int p, int q, int r)
    • sort

      public static final void sort(double[] a, int[] b)
      Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
      Parameters:
      a - the array to sort by
      b - the array to re-arrange based on the sort order of the first array.
    • sort

      public static final void sort(double[] a, int[] b, int length)
      Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
      Parameters:
      a - the array to sort by
      b - the array to re-arrange based on the sort order of the first array.
      length - the length of the range to be sorted
    • sort

      public static final void sort(double[] a, int[] b, int begin, int end)
      Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
      Parameters:
      a - the array to sort by
      b - the array to re-arrange based on the sort order of the first array.
      begin - the start index of the range to sort
      end - the end index, exclusive, of the range to sort
    • insertionsort

      protected static final void insertionsort(double[] a, int[] b, int p, int r)
    • mergesort

      protected static final void mergesort(double[] a, int[] b, int p, int r)
    • merge

      protected static final void merge(double[] a, int[] b, int p, int q, int r)
    • sort

      public static final void sort(float[] a, int[] b)
      Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
      Parameters:
      a - the array to sort by
      b - the array to re-arrange based on the sort order of the first array.
    • sort

      public static final void sort(float[] a, int[] b, int length)
      Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
      Parameters:
      a - the array to sort by
      b - the array to re-arrange based on the sort order of the first array.
      length - the length of the range to be sorted
    • sort

      public static final void sort(float[] a, int[] b, int begin, int end)
      Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
      Parameters:
      a - the array to sort by
      b - the array to re-arrange based on the sort order of the first array.
      begin - the start index of the range to sort
      end - the end index, exclusive, of the range to sort
    • insertionsort

      protected static final void insertionsort(float[] a, int[] b, int p, int r)
    • mergesort

      protected static final void mergesort(float[] a, int[] b, int p, int r)
    • merge

      protected static final void merge(float[] a, int[] b, int p, int q, int r)
    • sort

      public static final void sort(Object[] a, int[] b, Comparator<Object> cmp)
      Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
      Parameters:
      a - the array to sort by
      b - the array to re-arrange based on the sort order of the first array.
      cmp - the comparator to use to compare key values
    • sort

      public static final void sort(Object[] a, int[] b, int length, Comparator<Object> cmp)
      Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
      Parameters:
      a - the array to sort by
      b - the array to re-arrange based on the sort order of the first array.
      length - the length of the range to be sorted
      cmp - the comparator to use to compare key values
    • sort

      public static final void sort(Object[] a, int[] b, int begin, int end, Comparator<Object> cmp)
      Sort two arrays simultaneously, using the sort order of the values in the first array to determine the sort order for both arrays.
      Parameters:
      a - the array to sort by
      b - the array to re-arrange based on the sort order of the first array.
      begin - the start index of the range to sort
      end - the end index, exclusive, of the range to sort
      cmp - the comparator to use to compare key values
    • insertionsort

      protected static final void insertionsort(Object[] a, int[] b, int p, int r, Comparator<Object> cmp)
    • mergesort

      protected static final void mergesort(Object[] a, int[] b, int p, int r, Comparator<Object> cmp)
    • merge

      protected static final void merge(Object[] a, int[] b, int p, int q, int r, Comparator<Object> cmp)
    • getIntArray

      public static int[] getIntArray(String filename)
      Read in a text file as an array of integers. Uses the default java StringTokenizer to segment the text file. Additionally, tokens beginning with the '#' character are ignored.
      Parameters:
      filename - the name of the file to read in
      Returns:
      an array of integers parsed from the file