Package nom.tam.util
Class ArrayFuncs
java.lang.Object
nom.tam.util.ArrayFuncs
(for internal use) Varioys static functions for handling arrays. Generally these routines attempt to complete
without throwing errors by ignoring data they cannot understand.
-
Method Summary
Modifier and TypeMethodDescriptionstatic Stringstatic int[]checkRegularArray(Object o, boolean allowNulls) Checks that an array has a regular structure, with a consistent shape and element types, and returns the regular array size or else throws an exeption.static ObjectcomplexToDecimals(Object o, Class<?> decimalType) Converts complex value(s) count tofloat[2]ordouble[2]or arrays thereof, which maintain the shape of the original input array (if applicable).static longDeprecated.static intDeprecated.UseFitsEncoder.computeSize(Object)instead.static ObjectconvertArray(Object array, Class<?> newType) Converts a numerical array to a specified element type.static ObjectconvertArray(Object array, Class<?> newType, boolean reuse) Converts a numerical array to a specified element type, returning the original if type conversion is not needed.static ObjectconvertArray(Object array, Class<?> newType, Quantizer quant) Converts a numerical array to a specified element type, returning the original if type conversion is not needed.static voidPerform an array copy with an API similar to System.arraycopy(), specifying the number of values to jump to the next read.static voidDeprecated.(for internal use)static voidCopy an array into an array of a different type.static longstatic ObjectCurl an input array up into a multi-dimensional array.static ObjectdecimalsToComplex(Object array) Converts real-valued arrays of even element count to aComplexValueor arrays thereof.static ObjectdecimalsToComplex(Object re, Object im) Converts separate but matched real valued arrays, containing the real an imaginary parts respectively, toComplexValueor arrays thereof.static ObjectReturns a deep clone of an array (in one or more dimensions) or a standard clone of a scalar.static ObjectGiven an array of arbitrary dimensionality .static ObjectClone an Object if possible.static ObjectThis routine returns the base array of a multi-dimensional array.static Class<?> This routine returns the base class of an object.static intThis routine returns the size of the base element of an array.static int[]Find the dimensions of an object.static int[]getReversed(int... index) Retuens a copy of the input array with the order of elements reversed.static booleanConvenience method to check a generic Array object.static ObjectmimicArray(Object array, Class<?> newType) Create an array of a type given by new type with the dimensionality given in array.static intDeprecated.May silently underestimate size if number is > 2 G.static ObjectnewInstance(Class<?> cl, int... dims) Allocate an array dynamically.static longnLElements(Object o) Deprecated.Use the more aptly namedcountElements(Object)instead.static ObjectobjectToArray(Object o, boolean booleanAsObject) Converts objects to arrays.static int[]reverseIndices(int... indices) Reverse an integer array.static ObjectObtains a sparse sampling of from an array of one or more dimensions.static ObjectObtains a sparse sampling of from an array of one or more dimensions.static ObjectObtains a sparse sampling from an array of one or more dimensions.static ObjectObtains a slice (subarray) from an array of one or more dimensions.static ObjectObtains a slice (subarray) from an array of one or more dimensions.
-
Method Details
-
getReversed
public static int[] getReversed(int... index) Retuens a copy of the input array with the order of elements reversed.- Parameters:
index- the input array- Returns:
- a copy of the input array in reversed order
-
copy
Perform an array copy with an API similar to System.arraycopy(), specifying the number of values to jump to the next read.- Parameters:
src- The source array.srcPos- Starting position in the source array.dest- The destination array.destPos- Starting position in the destination data.length- The number of array elements to be read.step- The number of jumps to the next read.- Since:
- 1.18
-
arrayDescription
- Parameters:
o- The array to be described.- Returns:
- a description of an array (presumed rectangular).
-
computeLSize
Deprecated.UseFitsEncoder.computeSize(Object)instead.- Parameters:
o- the object- Returns:
- the number of bytes in the FITS binary representation of the object or 0 if the object has no FITS representation. (Also elements not known to FITS will count as 0 sized).
-
computeSize
Deprecated.UseFitsEncoder.computeSize(Object)instead.- Parameters:
o- the object- Returns:
- the number of bytes in the FITS binary representation of the object or 0 if the object has no FITS representation. (Also elements not known to FITS will count as 0 sized).
-
convertArray
Converts a numerical array to a specified element type. This method supports conversions only among the primitive numeric types, andComplexValuetypes (as of version 1.20). When converting primitive arrays to complex values, the trailing dimension must be 2, corresponding to the real and imaginary components of the complex values stored.- Parameters:
array- a numerical array of one or more dimensionsnewType- the desired output type. This should be one of the class descriptors for primitive numeric data, e.g.,double.class, or else aComplexValuetype (also supported as of 1.20).- Returns:
- a new array with the requested element type, which may also be composed of
ComplexValuetypes as of version 1.20. - See Also:
-
convertArray
Converts a numerical array to a specified element type, returning the original if type conversion is not needed. This method supports conversions only among the primitive numeric types originally. Support forComplexValuetypes was added as of version 1.20. When converting primitive arrays to complex values, the trailing dimension must be 2, corresponding to the real and imaginary components of the complex values stored.- Parameters:
array- a numerical array of one or more dimensionsnewType- the desired output type. This should be one of the class descriptors for primitive numeric data, e.g.,double.classr else aComplexValuetype (also supported as of 1.20).reuse- If the original (rather than a copy) should be returned when possible for the same type.- Returns:
- a new array with the requested element type, or possibly the original array if it readily matches
the type and
reuseis enabled. - See Also:
-
copyArray
@Deprecated public static void copyArray(Object original, Object copy) throws IllegalArgumentException Deprecated.(for internal use)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.- Throws:
IllegalArgumentException- if the two arrays do not match in type or size.
-
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.
- Throws:
IllegalStateException- if the size of the input does not match the specified dimensions.
-
deepClone
Returns a deep clone of an array (in one or more dimensions) 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, such as aList, then only a shallow copy of that object is made. I.e., deep refers only to arrays.- Parameters:
o- The object (usually an array) to be copied.- Returns:
- a new object, with a copy of the original.
-
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
-
isEmpty
Convenience method to check a generic Array object.- Parameters:
o- The Array to check.- Returns:
- True if it's empty, False otherwise.
- Since:
- 1.18
-
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 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.Use the more aptly namedcountElements(Object)instead.- Parameters:
o- the array to count elements in- Returns:
- Count the number of elements in an array.
-
countElements
- Parameters:
o- the array to count elements in- Returns:
- Count the number of elements in an array.
- Since:
- 1.18
-
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.
-
checkRegularArray
public static int[] checkRegularArray(Object o, boolean allowNulls) throws NullPointerException, IllegalArgumentException, ClassCastException Checks that an array has a regular structure, with a consistent shape and element types, and returns the regular array size or else throws an exeption. Optionally, it will also throw an exception if any or all all elements arenull.- Parameters:
o- An array objectallowNulls- If we should toleratenullentries.- Returns:
- the regular shape of the array with sizes along each array dimension.
- Throws:
NullPointerException- if the argument isnull.IllegalArgumentException- if the array contains mismatched elements in size, or containsnullvalues.ClassCastException- if the array contain a heterogeneous collection of different element types.- Since:
- 1.18
-
complexToDecimals
Converts complex value(s) count tofloat[2]ordouble[2]or arrays thereof, which maintain the shape of the original input array (if applicable).- Parameters:
o- one of more complex valuesdecimalType-float.classordouble.class(all other values default to as ifdouble.classwas used.- Returns:
- an array of
float[2]ordouble[2], or arrays thereof. - Throws:
IllegalArgumentException- if the argument is not suitable for conversion to complex values.- Since:
- 1.18
- See Also:
-
decimalsToComplex
Converts real-valued arrays of even element count to aComplexValueor arrays thereof. The size and shape is otherwise maintained, apart from coalescing pairs of real values intoComplexValueobjects.- Parameters:
array- an array offloatordoubleelements containing an even number of elements at the last dimension- Returns:
- one of more complex values
- Throws:
IllegalArgumentException- if the argument is not suitable for conversion to complex values.- Since:
- 1.18
- See Also:
-
decimalsToComplex
Converts separate but matched real valued arrays, containing the real an imaginary parts respectively, toComplexValueor arrays thereof. The size and shape of the matching input arrays is otherwise maintained, apart from coalescing pairs of real values intoComplexValueobjects.- Parameters:
re- an array offloatordoubleelements containing the real parts of the complex values.im- a matching array of the same type and shape as the real parts which contains the imaginary parts of the complex values.- Returns:
- one of more complex values
- Throws:
IllegalArgumentException- if the argument is not suitable for conversion to complex values.- Since:
- 1.20
- See Also:
-
convertArray
public static Object convertArray(Object array, Class<?> newType, Quantizer quant) throws IllegalArgumentException Converts a numerical array to a specified element type, returning the original if type conversion is not needed. If the conversion is from decimal to integer type, or vice-versa, an optional quantization may be supplied to to perform the integer-decimal conversion of the elements. This method supports conversions only among the primitive numeric types and alsoComplexValuetype.- Parameters:
array- a numerical array of one or more dimensionsnewType- the desired output type. This should be one of the class descriptors for primitive numeric data, e.g.,double.class, or else aComplexValueorComplexValue.Float.quant- optional qunatizer for integer-decimal conversion, ornullto use simply rounding.- Returns:
- a new array with the requested element type, or possibly the original array if
it readily matches the type and
reuseis enabled. - Throws:
IllegalArgumentException- if the input is not an array, or its elements are not a supported type or if the new type is not supported.- Since:
- 1.20
- See Also:
-
sample
Obtains a sparse sampling of from an array of one or more dimensions.- Parameters:
orig- the original arraystep- the sampling step size along all dimensions for a subsampled slice. A negative value indicates that the sampling should proceed in the reverse direction along every axis.- Returns:
- the requested sampling from the original. The returned array may share data with the original, and so modifications to either may affect the other. The orginal object is returned if it is not an array.
- Throws:
IndexOutOfBoundsException- if any of the indices for the requested slice are out of bounds for the original. That is if the original does not fully contain the requested slice. Or, if the from and size arguments have differing lengths.- Since:
- 1.20
- See Also:
-
sample
Obtains a sparse sampling of from an array of one or more dimensions.- Parameters:
orig- the original arraystep- the sampling step size along each dimension for a subsampled slice. Negative values indicate that the sampling should proceed in the reverse direction along the given axis.- Returns:
- the requested sampling from the original. The returned array may share data with the original, and so modifications to either may affect the other. The orginal object is returned if it is not an array.
- Throws:
IndexOutOfBoundsException- if any of the indices for the requested slice are out of bounds for the original. That is if the original does not fully contain the requested slice. Or, if the from and size arguments have differing lengths.- Since:
- 1.20
- See Also:
-
slice
Obtains a slice (subarray) from an array of one or more dimensions.- Parameters:
orig- the original arrayfrom- the starting indices for the slice in the original array. It should have at most as many elements as there are array dimensions, but it can also have fewer.- Returns:
- the requested slice from the original. The returned array may share data with the original, and so modifications to either may affect the other. The orginal object is returned if it is not an array.
- Throws:
IndexOutOfBoundsException- if any of the indices for the requested slice are out of bounds for the original. That is if the original does not fully contain the requested slice. Or, if the from and size arguments have differing lengths.- Since:
- 1.20
- See Also:
-
slice
Obtains a slice (subarray) from an array of one or more dimensions.- Parameters:
orig- the original arrayfrom- the starting indices for the slice in the original array. It should have at most as many elements as there are array dimensions, but it can also have fewer.size- the size of the slice. Negative values can indicate moving backwards in the original array (but forward in the slice -- resulting in a flipped axis). Anullsize argument can be used to sample the full original. The slice will end at indexfrom[k] + size[k]in dimensionkin the original (not including the ending index). It should have the same number of elements as thefromargument.- Returns:
- the requested slice from the original. The returned array may share data with the original, and so modifications to either may affect the other. The orginal object is returned if it is not an array.
- Throws:
IndexOutOfBoundsException- if any of the indices for the requested slice are out of bounds for the original. That is if the original does not fully contain the requested slice. Or, if the from and size arguments have differing lengths.- Since:
- 1.20
- See Also:
-
sample
public static Object sample(Object orig, int[] from, int[] size, int[] step) throws IndexOutOfBoundsException Obtains a sparse sampling from an array of one or more dimensions.- Parameters:
orig- the original arrayfrom- the starting indices in the original array at which to start sampling. It should have at most as many elements as there are array dimensions, but it can also have fewer. Anullargument can be used to sample from the start or end of the array (depending on the direction).size- the size of the sampled area in the original along each dimension. The signature of the values is irrelevant as the direction of sampling is determined by the step argument. Zero entries can be used to indicate that the full array should be sampled along the given dimension, while anullargument will sample the full array in all dimensions.step- the sampling step size along each dimension for a subsampled slice. Negative values indicate sampling the original in the reverse direction along the given dimension. 0 values are are automatically bumped to 1 (full sampling), and anullargument is understood to mean full sampling along all dimensions.- Returns:
- the requested sampling from the original. The returned array may share data with the original, and so modifications to either may affect the other. The orginal object is returned if it is not an array.
- Throws:
IndexOutOfBoundsException- if any of the indices for the requested slice are out of bounds for the original. That is if the original does not fully contain the requested slice. Or, if the from and size arguments have differing lengths.- Since:
- 1.20
- See Also:
-
objectToArray
Converts objects to arrays. If the object is already an array it is returned unchanged. Boxed primitives are returned as primitive arrays of 1. All other objects are wrapped into an array of 1 of the same type.Booleanvalues are somewhat special and are handled according to the second argument, either to produce aboolean[1]or else aBoolean[1].- Parameters:
o- The objectbooleanAsObject- WhetherBooleanvalues should be convertedBoolean[1]instead ofboolean[1].- Returns:
- The input object, wrapped into an array as appropriate.
- Since:
- 1.21
-
FitsEncoder.computeSize(Object)instead.