org.metasyntactic.utilities
Class ArrayUtilities

java.lang.Object
  |
  +--org.metasyntactic.utilities.ArrayUtilities

public class ArrayUtilities
extends java.lang.Object

Contains useful functions for array manipulations. Most methods can hanlde both Object arrays, and primitive by using reflection. Note! For all methods arrays containing arrays are supported, however, internal arrays with pointers to an external array can cause problems such as inifite loops and should not be used!


Method Summary
static boolean equals(java.lang.Object a1, java.lang.Object a2)
          Returns true if all the elements in the first array equals all the elements in the second array.
static void fill(java.lang.Object[] elements, java.lang.Object object)
          Uses a O(log(n)) algortihm to quickly fill the passed in array with references to the passed in object
static void fill(java.lang.Object array, java.lang.Object wrappedPrimitive)
          Fills each space in a primitive array with the primitive valued wrapped in this object
static int hashCode(java.lang.Object array)
          Returns a hashcode for the array.
static void main(java.lang.String[] args)
           
static void quicksort(java.lang.Object array)
          Quicksorts the elements of this array in place according to the natural ordering of it's elements.
static void quicksort(java.lang.Object[] array)
          Quicksorts the elements of this array in pace accroding to the natural ordering of it's elements
static void quicksort(java.lang.Object[] array, java.util.Comparator comparator)
           
static void quicksort(java.lang.Object[] A, java.util.Comparator comparator, int p, int r)
           
static void quicksort(java.lang.Object A, java.util.Comparator comparator)
          Quicksorts the elements of this array using the comparator provided
static void quicksort(java.lang.Object A, java.util.Comparator comparator, int p, int r)
          Quicksorts the elements of this array (in the range [p,r)) using the comparator provided
static void randomizedQuicksort(java.lang.Object array)
           
static void randomizedQuicksort(java.lang.Object array, java.util.Comparator comparator)
           
static void randomizedQuicksort(java.lang.Object A, java.util.Comparator comparator, int p, int r)
           
static java.lang.Object reverse(java.lang.Object array)
          Reverses a primitive array in place
static java.lang.Object[] reverse(java.lang.Object[] objs)
          Reverses the contents of this array in place and returns a reference to the array passed in
static java.lang.Object[] reverse(java.lang.Object[] objs, boolean newArray)
          Reverses the contents of this array.
static java.lang.Object reverse(java.lang.Object primArray, boolean newArray)
          Reverses the contents of this primitive array.
static java.util.Iterator toIterator(java.lang.Object array)
           
static java.util.Iterator toIterator(java.lang.Object[] array)
           
static java.util.List toList(java.lang.Object array)
          Returns a new List BACKED by this array! This means that additions will fail! If you don't want the addition to fail, create a new Collection from the collection returned.
static java.lang.String toString(java.lang.Object array)
          Returns a string representation of this Array.
static void traverse(java.lang.Object array, Operator node)
          Traverses an array (and all sub arrays) and applies the operator 'node' to every non array Object contained within.
static void traverse(java.lang.Object array, Operator node, Operator startArray, Operator endArray, Operator alreadySeen)
          Traverses this array, applying 'node' to every non array Object contained within.
static java.lang.Object[] wrap(java.lang.Object primArray)
          Takes in a primitive array (such as an int[]) and returns an Object[], where each element in the new array is the wrapped object value of the primitive element in the original array.
static java.lang.Object[] wrap(java.lang.Object primArray, int offset, int length)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

wrap

public static java.lang.Object[] wrap(java.lang.Object primArray)
Takes in a primitive array (such as an int[]) and returns an Object[], where each element in the new array is the wrapped object value of the primitive element in the original array. Note, the current implementation is slow :-)


wrap

public static java.lang.Object[] wrap(java.lang.Object primArray,
                                      int offset,
                                      int length)

toList

public static java.util.List toList(java.lang.Object array)
Returns a new List BACKED by this array! This means that additions will fail! If you don't want the addition to fail, create a new Collection from the collection returned.


traverse

public static void traverse(java.lang.Object array,
                            Operator node)
Traverses an array (and all sub arrays) and applies the operator 'node' to every non array Object contained within. Note! Cycles will not be retraversed.

Parameters:
array -
node -

traverse

public static void traverse(java.lang.Object array,
                            Operator node,
                            Operator startArray,
                            Operator endArray,
                            Operator alreadySeen)
Traverses this array, applying 'node' to every non array Object contained within. Every time an array is reached for the first time startArray is invoked with the array as it's argument, and when the array is done with endArray is called (Again with the array as it's argument). If an array is reached again alreadySeen is called (with the array as it's argument)

Parameters:
array -
node -
startArray -
endArray -
alreadySeen -

toString

public static java.lang.String toString(java.lang.Object array)
Returns a string representation of this Array. Arrays start and end with "{" and "}" and it's elements are seperated by ", ". If the array contains internal arrays they will also be printed with this method. However, if there are references to arrays already printed, then to prevent infinite recursion the array is not traversed and only a reference to it is added to the string.

Parameters:
array - The array to create a string from
Returns:
A string representation of this array

fill

public static void fill(java.lang.Object[] elements,
                        java.lang.Object object)
Uses a O(log(n)) algortihm to quickly fill the passed in array with references to the passed in object

Parameters:
elements - The array you want to fill
object - The object that will be copied into every position in the array

fill

public static void fill(java.lang.Object array,
                        java.lang.Object wrappedPrimitive)
Fills each space in a primitive array with the primitive valued wrapped in this object

Parameters:
array -
wrappedPrimitive - The value to place in the arrat

reverse

public static java.lang.Object[] reverse(java.lang.Object[] objs,
                                         boolean newArray)
Reverses the contents of this array. If newArray is true the contents are copied into a new array, otherwise they are reversed 'in place'

Parameters:
objs - The array with the contents reversed
newArray - If true returns a new array with the contents reversed (the original array is unnaffected). If false, the array passed in has it's elements rearranged
Returns:
The same array passed, but with the contents reversed

reverse

public static java.lang.Object[] reverse(java.lang.Object[] objs)
Reverses the contents of this array in place and returns a reference to the array passed in

Parameters:
objs - The array to reverse
Returns:
A reference the array passed in whose elements have been reversed

reverse

public static java.lang.Object reverse(java.lang.Object array)
Reverses a primitive array in place

Parameters:
array - The primitive array to reverse in place
Returns:
A reference to the array passed in
Throws:
InvalidArgumentException - when obj is not an array

reverse

public static java.lang.Object reverse(java.lang.Object primArray,
                                       boolean newArray)
Reverses the contents of this primitive array. If primArray is true the contents are copied into a new array, otherwise they are reversed 'in place'

Parameters:
primArray - The array with the content to revers
newArray - If true returns a new array with the contents reversed (the original array is unnaffected). If false, the array passed in has it's elements rearranged
Returns:
A reference to an array with elements in the reverse order of the array passed in

hashCode

public static int hashCode(java.lang.Object array)
Returns a hashcode for the array. The hashcode is equals to the sum of all the non array elements contained in this array, plus the sum of the lengths of all the internal arrays

Parameters:
array -
Returns:

equals

public static boolean equals(java.lang.Object a1,
                             java.lang.Object a2)
Returns true if all the elements in the first array equals all the elements in the second array. Note, subarrays are handled in the same manner, however, arrays with cyclic graphs (ie, inner arrays that point to the outer array) can cause infinite loops and are not currently supported.

Parameters:
a1 -
a2 -
Returns:

toIterator

public static java.util.Iterator toIterator(java.lang.Object array)

toIterator

public static java.util.Iterator toIterator(java.lang.Object[] array)

randomizedQuicksort

public static void randomizedQuicksort(java.lang.Object array)

randomizedQuicksort

public static void randomizedQuicksort(java.lang.Object array,
                                       java.util.Comparator comparator)

randomizedQuicksort

public static void randomizedQuicksort(java.lang.Object A,
                                       java.util.Comparator comparator,
                                       int p,
                                       int r)

quicksort

public static void quicksort(java.lang.Object array)
Quicksorts the elements of this array in place according to the natural ordering of it's elements.

Parameters:
array -

quicksort

public static void quicksort(java.lang.Object[] array)
Quicksorts the elements of this array in pace accroding to the natural ordering of it's elements


quicksort

public static void quicksort(java.lang.Object A,
                             java.util.Comparator comparator)
Quicksorts the elements of this array using the comparator provided


quicksort

public static void quicksort(java.lang.Object A,
                             java.util.Comparator comparator,
                             int p,
                             int r)
Quicksorts the elements of this array (in the range [p,r)) using the comparator provided


quicksort

public static void quicksort(java.lang.Object[] array,
                             java.util.Comparator comparator)

quicksort

public static void quicksort(java.lang.Object[] A,
                             java.util.Comparator comparator,
                             int p,
                             int r)

main

public static void main(java.lang.String[] args)