Package nom.tam.util
Class ArrayFuncs
java.lang.Object
nom.tam.util.ArrayFuncs
This is a package of static functions which perform computations on arrays.
Generally these routines attempt to complete without throwing errors by
ignoring data they cannot understand.
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
static long
static int
Deprecated.May silently underestimate the size if the size > 2 GB.static Object
convertArray
(Object array, Class<?> newType) static Object
convertArray
(Object array, Class<?> newType, boolean reuse) static void
Copy one array into another.static void
Copy an array into an array of a different type.static Object
Curl an input array up into a multi-dimensional array.static Object
static Object
Given an array of arbitrary dimensionality .static Object
Clone an Object if possible.static Object
This routine returns the base array of a multi-dimensional array.static Class<?>
This routine returns the base class of an object.static int
This routine returns the size of the base element of an array.static int[]
Find the dimensions of an object.static Object
mimicArray
(Object array, Class<?> newType) Create an array of a type given by new type with the dimensionality given in array.static int
Deprecated.May silently underestimate size if number is > 2 G.static Object
newInstance
(Class<?> cl, int dim) Allocate an array dynamically.static Object
newInstance
(Class<?> cl, int[] dims) Allocate an array dynamically.static long
nLElements
(Object o) Deprecated.May silently underestimate size if number is > 2 G.static int[]
reverseIndices
(int[] indices) Reverse an integer array.
-
Method Details
-
arrayDescription
- Parameters:
o
- The array to be described.- Returns:
- a description of an array (presumed rectangular).
-
computeLSize
-
computeSize
Deprecated.May silently underestimate the size if the size > 2 GB.- Parameters:
o
- The object whose size is desired.- Returns:
- Compute the size of an object. Note that this only handles arrays or scalars of the primitive objects and Strings. It returns 0 for any object array element it does not understand.
-
convertArray
- Parameters:
array
- A possibly multidimensional array to be converted.newType
- The desired output type. This should be one of the class descriptors for primitive numeric data, e.g., double.type.- Returns:
- Convert an array to a specified type. This method supports conversions only among the primitive numeric types.
-
convertArray
- Parameters:
array
- A possibly multidimensional array to be converted.newType
- The desired output type. This should be one of the class descriptors for primitive numeric data, e.g., double.type.reuse
- If set, and the requested type is the same as the original, then the original is returned.- Returns:
- Convert an array to a specified type. This method supports conversions only among the primitive numeric types.
-
copyArray
Copy one array into another. This function copies the contents of one array into a previously allocated array. The arrays must agree in type and size.- Parameters:
original
- The array to be copied.copy
- The array to be copied into. This array must already be fully allocated.
-
copyInto
Copy an array into an array of a different type. The dimensions and dimensionalities of the two arrays should be the same.- Parameters:
array
- The original array.mimic
- The array mimicking the original.
-
curl
Curl an input array up into a multi-dimensional array.- Parameters:
input
- The one dimensional array to be curled.dimens
- The desired dimensions- Returns:
- The curled array.
-
deepClone
- Parameters:
o
- The object to be copied.- Returns:
- a deep clone of an Array or a standard clone of a scalar. The object may comprise arrays of any primitive type or any Object type which implements Cloneable. However, if the Object is some kind of collection, e.g., a Vector then only a shallow copy of that object is made. I.e., deep refers only to arrays.
-
flatten
Given an array of arbitrary dimensionality .- Parameters:
input
- The input array.- Returns:
- the array flattened into a single dimension.
-
genericClone
Clone an Object if possible. This method returns an Object which is a clone of the input object. It checks if the method implements the Cloneable interface and then uses reflection to invoke the clone method. This can't be done directly since as far as the compiler is concerned the clone method for Object is protected and someone could implement Cloneable but leave the clone method protected. The cloning can fail in a variety of ways which are trapped so that it returns null instead. This method will generally create a shallow clone. If you wish a deep copy of an array the method deepClone should be used.- Parameters:
o
- The object to be cloned.- Returns:
- the clone
-
getBaseArray
This routine returns the base array of a multi-dimensional array. I.e., a one-d array of whatever the array is composed of. Note that arrays are not guaranteed to be rectangular, so this returns o[0][0]....- Parameters:
o
- the multi-dimensional array- Returns:
- base array of a multi-dimensional array.
-
getBaseClass
This routine returns the base class of an object. This is just the class of the object for non-arrays.- Parameters:
o
- array to get the base class from- Returns:
- the base class of an array
-
getBaseLength
This routine returns the size of the base element of an array.- Parameters:
o
- The array object whose base length is desired.- Returns:
- the size of the object in bytes, 0 if null, or -1 if not a primitive array.
-
getDimensions
Find the dimensions of an object. This method returns an integer array with the dimensions of the object o which should usually be an array. It returns an array of dimension 0 for scalar objects and it returns -1 for dimension which have not been allocated, e.g.,int[][][] x = new int[100][][];
should return [100,-1,-1].- Parameters:
o
- The object to get the dimensions of.- Returns:
- the dimensions of an object
-
mimicArray
Create an array of a type given by new type with the dimensionality given in array.- Parameters:
array
- A possibly multidimensional array to be converted.newType
- The desired output type. This should be one of the class descriptors for primitive numeric data, e.g., double.type.- Returns:
- the new array with same dimensions
-
nElements
Deprecated.May silently underestimate size if number is > 2 G.- Parameters:
o
- the array to count the elements- Returns:
- Count the number of elements in an array.
-
newInstance
Allocate an array dynamically. The Array.newInstance method does not throw an error when there is insufficient memory and silently returns a null.throws an OutOfMemoryError if insufficient space is available.- Parameters:
cl
- The class of the array.dim
- The dimension of the array.- Returns:
- The allocated array.
-
newInstance
Allocate an array dynamically. The Array.newInstance method does not throw an error and silently returns a null.throws an OutOfMemoryError if insufficient space is available.- Parameters:
cl
- The class of the array.dims
- The dimensions of the array.- Returns:
- The allocated array.
-
nLElements
Deprecated.May silently underestimate size if number is > 2 G.- Parameters:
o
- the array to count elements in- Returns:
- Count the number of elements in an array.
-
reverseIndices
public static int[] reverseIndices(int[] indices) Reverse an integer array. This can be especially useful when dealing with an array of indices in FITS order that you wish to have in Java order.- Parameters:
indices
- the array to reverse- Returns:
- the reversed array.
-