Package ucar.ma2
Class Array
java.lang.Object
ucar.ma2.Array
- Direct Known Subclasses:
ArrayBoolean,ArrayByte,ArrayChar,ArrayDouble,ArrayFloat,ArrayInt,ArrayLong,ArrayObject,ArrayRagged,ArrayScalar,ArrayShort,ArrayString,ArrayStructure
Superclass for implementations of multidimensional arrays. An Array has a classType which gives
the Class of its elements, and a shape which describes the number of elements in each index.
The rank is the number of indices. A scalar Array has rank = 0. An
Array may have arbitrary rank. The Array size is the total number of elements, which must be less than
2^31 (about 2x10^9).
Actual data storage is done with Java 1D arrays and stride index calculations.
This makes our Arrays rectangular, i.e. no "ragged arrays" where different elements
can have different lengths as in Java multidimensional arrays, which are arrays of arrays.
Each primitive Java type (boolean, byte, char, short, int, long, float, double) has a corresponding concrete
implementation, e.g. ArrayBoolean, ArrayDouble. Reference types are all implemented using the ArrayObject class,
with the exceptions of the reference types that correspond to the primitive types, eg Double.class is mapped to
double.class.
For efficiency, each Array type implementation has concrete subclasses for ranks 0-7, eg ArrayDouble.D0 is a double
array of rank 0, ArrayDouble.D1 is a double array of rank 1, etc. These type and rank specific classes are convenient
to work with when you know the type and rank of the Array. Ranks greater than 7 are handled by the type-specific
superclass e.g. ArrayDouble. The Array class itself is used for fully general handling of any type and rank array.
Use the Array.factory() methods to create Arrays in a general way.
The stride index calculations allow logical views to be efficiently implemented, eg subset, transpose, slice,
etc.
These views use the same data storage as the original Array they are derived from. The index stride calculations are
equally efficient for any composition of logical views.
The type, shape and backing storage of an Array are immutable.
The data itself is read or written using an Index or an IndexIterator, which stores any needed state information
for efficient traversal. This makes use of Arrays thread-safe (as long as you don't share the Index or IndexIterator)
except for the possibility of non-atomic read/write on long/doubles. If this is the case, you should probably
synchronize your calls. Presumably 64-bit CPUs will make those operations atomic also.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidCover for System.arraycopy().copy()Create a copy of this Array, copying the data so that physical order is the same as logical orderCopy this array to a 1D Java primitive array of type getElementType(), with the physical order of the result the same as logical order.Copy this array to an n-Dimensional Java primitive array of type getElementType() and rank getRank().static ArrayGenerate new Array with given dataType and shape and zeroed storage.static ArrayGenerate new Array with given dataType, shape, storage.static Arrayfactory(DataType dtype, int[] shape, ByteBuffer bb) Create an Array from a ByteBufferstatic Arraystatic ArrayfactoryConstant(DataType dtype, int[] shape, Object storage) Generate new Array with given type and shape and an Index that always return 0.static ArrayfactoryCopy(DataType dataType, int[] shape, List<Array> arrays) Combine list of Arrays by copying the underlying Arrays into a single primitive arrayflip(int dim) Create a new Array using same backing store as this Array, by flipping the index so that it runs from shape[index]-1 to 0.get1DJavaArray(Class wantType) Deprecated.use get1DJavaArray(DataType wantType)get1DJavaArray(DataType wantType) This gets the equivalent java array of the wanted type, in correct order.abstract booleangetBoolean(int elem) abstract booleangetBoolean(Index ima) Get the array element at the current element of ima, as a boolean.abstract bytegetByte(int elem) abstract byteGet the array element at the current element of ima, as a byte.abstract chargetChar(int elem) abstract charGet the array element at the current element of ima, as a char.This gets the data as a ByteBuffer, in correct order.getDataAsByteBuffer(int capacity, ByteOrder order) getDataAsByteBuffer(ByteOrder order) Return the computed datatype for this arrayabstract doublegetDouble(int elem) abstract doubleGet the array element at the current element of ima, as a double.abstract ClassGet the element class type of this Arrayabstract floatgetFloat(int elem) abstract floatGet the array element at the current element of ima, as a float.getIndex()Get an Index object used for indexed access of this Array.Get an index iterator for traversing the array in canonical order.abstract intgetInt(int elem) abstract intGet the array element at the current element of ima, as a int.abstract longgetLong(int elem) abstract longGet the array element at the current element of ima, as a long.abstract ObjectgetObject(int elem) abstract ObjectGet the array element at index as an Object.getRangeIterator(List<Range> ranges) Get an index iterator for traversing a section of the array in canonical order.intgetRank()Get the number of dimensions of the array.int[]getShape()Get the shape: length of array in each dimension.abstract shortgetShort(int elem) abstract shortGet the array element at the current element of ima, as a short.longgetSize()Get the total number of elements in the array.longGet the total number of bytes in the array.abstract ObjectGet underlying primitive array storage.booleanhasNext()Check if more elements in the local iterator.booleanIf this is a constant arraybooleanFind whether the underlying data should be interpreted as unsigned.booleanisVlen()static ArrayMake a 1D array from a start and incr.static ArrayMake an 1D array from an array of strings.static ArrayMake an 1D array from a list of strings.static ArrayAdd extra outermost dimension with len = 1.static ArraymakeFromJavaArray(Object javaArray) static ArraymakeFromJavaArray(Object javaArray, boolean isUnsigned) Generate a new Array from a java array of any rank and type.static ArraymakeObjectArray(DataType dtype, Class classType, int[] shape, Object storage) Generate new Array with given type, shape, storage.static ArraymakeVlenArray(int[] shape, Array[] storage) Make a vlen arraynext()Return the next object in the local iterator.booleanReturn the next boolean in the local iterator.bytenextByte()Return the next byte in the local iterator.charnextChar()Return the next char in the local iterator.doubleReturn the next double in the local iterator.floatReturn the next float in the local iterator.intnextInt()Return the next int in the local iterator.longnextLong()Return the next long in the local iterator.shortReturn the next short in the local iterator.permute(int[] dims) Create a new Array using same backing store as this Array, by permuting the indices.reduce()Create a new Array using same backing store as this Array, by eliminating any dimensions with length one.reduce(int dim) Create a new Array using same backing store as this Array, by eliminating the specified dimension.voidReset the local iterator.reshape(int[] shape) Create a new Array by copying this Array to a new one with given shapereshapeNoCopy(int[] shape) Reshape this array without copying datasection(int[] origin, int[] shape) Create a new Array as a subsection of this Array, with rank reduction.section(int[] origin, int[] shape, int[] stride) Create a new Array as a subsection of this Array, with rank reduction.Create a new Array as a subsection of this Array, with rank reduction.sectionNoReduce(int[] origin, int[] shape, int[] stride) Create a new Array as a subsection of this Array, without rank reduction.sectionNoReduce(List<Range> ranges) Create a new Array as a subsection of this Array, without rank reduction.abstract voidsetBoolean(int elem, boolean value) abstract voidsetBoolean(Index ima, boolean value) Set the array element at the current element of ima.abstract voidsetByte(int elem, byte value) abstract voidSet the array element at the current element of ima.abstract voidsetChar(int elem, char value) abstract voidSet the array element at the current element of ima.abstract voidsetDouble(int elem, double val) abstract voidSet the array element at the current element of ima.abstract voidsetFloat(int elem, float val) abstract voidSet the array element at the current element of ima.abstract voidsetInt(int elem, int value) abstract voidSet the array element at the current element of ima.abstract voidsetLong(int elem, long value) abstract voidSet the array element at the current element of ima.abstract voidabstract voidSet the array element at index to the specified value.abstract voidsetShort(int elem, short value) abstract voidSet the array element at the current element of ima.Create a string representation of the shape of this Array.slice(int dim, int value) Create a new Array using same backing store as this Array, by fixing the specified dimension at the specified index value.toString()transpose(int dim1, int dim2) Create a new Array using same backing store as this Array, by transposing two of the indices.
-
Method Details
-
factory
Generate new Array with given dataType and shape and zeroed storage.- Parameters:
dataType- instance of DataType.shape- shape of the array.- Returns:
- new Array
or Array .D if 0 <= rank <= 7.
-
factory
Generate new Array with given dataType, shape, storage.- Parameters:
dataType- DataType, eg DataType.DOUBLE.shape- shape of the array.storage- primitive array of correct type- Returns:
- new Array
or Array .D if 0 <= rank <= 7. - Throws:
ClassCastException- wrong storage type
-
factory
-
makeVlenArray
Make a vlen array- Parameters:
shape- the outer shape, ie excluding the vlen dimensionstorage- must be an Array type. must not be null- Returns:
- ArrayObject
-
makeObjectArray
Generate new Array with given type, shape, storage. This should be package private, but is exposed for efficiency. Normally use factory( Class classType, int [] shape) instead. storage must be 1D array of type classType. storage.length must equal product of shapes storage data needs to be in canonical order- Parameters:
classType- element class type, eg double.class. Corresponding Object types like Double.class are mapped to double.class. Any reference types use ArrayObject.shape- array shapestorage- 1D java array of type classType, except object types like Double.class are mapped to their corresponding primitive type, eg double.class.- Returns:
- Array of given type, shape and storage
- Throws:
IllegalArgumentException- storage.length != product of shapesClassCastException- wrong storage type
-
factoryConstant
Generate new Array with given type and shape and an Index that always return 0.- Parameters:
dtype- data typeshape- shape of the array.storage- primitive array of correct type of length 1- Returns:
- new Array
or Array .D if 0 <= rank <= 7.
-
makeFromJavaArray
-
makeFromJavaArray
Generate a new Array from a java array of any rank and type. This makes a COPY of the data values of javaArray of primitive type LOOK: not sure this works for reference types.- Parameters:
javaArray- scalar Object or a java array of any rank and type- Returns:
- Array of the appropriate rank and type, with the data copied from javaArray.
-
arraycopy
Cover for System.arraycopy(). Works with the underlying data arrays. ArraySrc and ArrayDst must be the same primitive type. Exposed for efficiency; use at your own risk.- Parameters:
arraySrc- copy from here : if not in canonical order, an extra copy will be donesrcPos- starting atarrayDst- copy to here : must be in canonical orderdstPos- starting atlen- number of elements to copy
-
makeArray
Make a 1D array from a start and incr.- Parameters:
dtype- data type of result. must be convertible to double.npts- number of pointsstart- starting valuesincr- increment- Returns:
- 1D array
-
makeArray
public static Array makeArray(DataType dtype, List<String> stringValues) throws NumberFormatException Make an 1D array from a list of strings.- Parameters:
dtype- data type of the array.stringValues- list of strings.- Returns:
- resulting 1D array.
- Throws:
NumberFormatException- if string values not parseable to specified data type
-
makeArray
Make an 1D array from an array of strings.- Parameters:
dtype- data type of the array. Assumed unsignedstringValues- list of strings.- Returns:
- resulting 1D array.
- Throws:
NumberFormatException- if string values not parseable to specified data type
-
makeArrayRankPlusOne
Add extra outermost dimension with len = 1.- Parameters:
org- original array- Returns:
- rank1 array of rank + 1
-
factoryCopy
Combine list of Arrays by copying the underlying Arrays into a single primitive array- Parameters:
dataType- the DataTypeshape- the shape of the combined arrayarrays- non-empty list of arrays of the same dataType to combine- Returns:
- a new Array containing data from the arrays
- Throws:
IllegalArgumentException- if arrays is empty or if it contains ArrayStructures with different Members
-
getDataType
Return the computed datatype for this array- Returns:
- the data type
-
getIndex
Get an Index object used for indexed access of this Array.- Returns:
- an Index for this Array
- See Also:
-
getIndexIterator
Get an index iterator for traversing the array in canonical order.- Returns:
- an IndexIterator for this Array
- See Also:
-
getRank
public int getRank()Get the number of dimensions of the array.- Returns:
- number of dimensions of the array
-
getShape
public int[] getShape()Get the shape: length of array in each dimension.- Returns:
- array whose length is the rank of this Array and whose elements represent the length of each of its indices.
-
getSize
public long getSize()Get the total number of elements in the array.- Returns:
- total number of elements in the array
-
getSizeBytes
public long getSizeBytes()Get the total number of bytes in the array.- Returns:
- total number of bytes in the array
-
getRangeIterator
Get an index iterator for traversing a section of the array in canonical order. This is equivalent to Array.section(ranges).getIterator();- Parameters:
ranges- list of Ranges that specify the array subset. Must be same rank as original Array. A particular Range: 1) may be a subset, or 2) may be null, meaning use entire Range.- Returns:
- an IndexIterator over the named range.
- Throws:
InvalidRangeException- if ranges is invalid
-
getElementType
Get the element class type of this Array- Returns:
- the class of the element
-
getStorage
Get underlying primitive array storage. Exposed for efficiency, use at your own risk.- Returns:
- underlying primitive array storage
-
section
Create a new Array as a subsection of this Array, with rank reduction. No data is moved, so the new Array references the same backing store as the original.- Parameters:
ranges- list of Ranges that specify the array subset. Must be same rank as original Array. A particular Range: 1) may be a subset, or 2) may be null, meaning use entire Range. If Range[dim].length == 1, then the rank of the resulting Array is reduced at that dimension.- Returns:
- the new Array
- Throws:
InvalidRangeException- if ranges is invalid
-
section
Create a new Array as a subsection of this Array, with rank reduction. No data is moved, so the new Array references the same backing store as the original.- Parameters:
origin- int array specifying the starting index. Must be same rank as original Array.shape- int array specifying the extents in each dimension. This becomes the shape of the returned Array. Must be same rank as original Array. If shape[dim] == 1, then the rank of the resulting Array is reduced at that dimension.- Returns:
- the new Array
- Throws:
InvalidRangeException- if ranges is invalid
-
section
Create a new Array as a subsection of this Array, with rank reduction. No data is moved, so the new Array references the same backing store as the original.- Parameters:
origin- int array specifying the starting index. Must be same rank as original Array.shape- int array specifying the extents in each dimension. This becomes the shape of the returned Array. Must be same rank as original Array. If shape[dim] == 1, then the rank of the resulting Array is reduced at that dimension.stride- int array specifying the strides in each dimension. If null, assume all ones.- Returns:
- the new Array
- Throws:
InvalidRangeException- if ranges is invalid
-
sectionNoReduce
Create a new Array as a subsection of this Array, without rank reduction. No data is moved, so the new Array references the same backing store as the original. Vlen is transferred over unchanged.- Parameters:
ranges- list of Ranges that specify the array subset. Must be same rank as original Array. A particular Range: 1) may be a subset, or 2) may be null, meaning use entire Range.- Returns:
- the new Array
- Throws:
InvalidRangeException- if ranges is invalid
-
sectionNoReduce
Create a new Array as a subsection of this Array, without rank reduction. No data is moved, so the new Array references the same backing store as the original.- Parameters:
origin- int array specifying the starting index. Must be same rank as original Array.shape- int array specifying the extents in each dimension. This becomes the shape of the returned Array. Must be same rank as original Array.stride- int array specifying the strides in each dimension. If null, assume all ones.- Returns:
- the new Array
- Throws:
InvalidRangeException- if ranges is invalid
-
slice
Create a new Array using same backing store as this Array, by fixing the specified dimension at the specified index value. This reduces rank by 1.- Parameters:
dim- which dimension to fixvalue- at what index value- Returns:
- a new Array
-
copy
Create a copy of this Array, copying the data so that physical order is the same as logical order- Returns:
- the new Array
-
get1DJavaArray
This gets the equivalent java array of the wanted type, in correct order. It avoids copying if possible.- Parameters:
wantType- returned object will be an array of this type. This must be convertible to it.- Returns:
- java array of type want
-
get1DJavaArray
Deprecated.use get1DJavaArray(DataType wantType) -
getDataAsByteBuffer
This gets the data as a ByteBuffer, in correct order. It avoids copying if possible. Only for numeric types (byte, short, int, long, double, float)- Returns:
- equivalent data in a ByteBuffer
-
getDataAsByteBuffer
-
getDataAsByteBuffer
-
factory
Create an Array from a ByteBuffer- Parameters:
dtype- type of datashape- shape of data; if null, then use int[]{bb.limit()}bb- data is in here- Returns:
- equivilent Array
-
copyTo1DJavaArray
Copy this array to a 1D Java primitive array of type getElementType(), with the physical order of the result the same as logical order.- Returns:
- a Java 1D array of type getElementType().
-
copyToNDJavaArray
Copy this array to an n-Dimensional Java primitive array of type getElementType() and rank getRank(). Makes a copy of the data.- Returns:
- a Java ND array of type getElementType().
-
flip
Create a new Array using same backing store as this Array, by flipping the index so that it runs from shape[index]-1 to 0.- Parameters:
dim- dimension to flip- Returns:
- the new Array
-
transpose
Create a new Array using same backing store as this Array, by transposing two of the indices.- Parameters:
dim1- transpose these two indicesdim2- transpose these two indices- Returns:
- the new Array
-
permute
Create a new Array using same backing store as this Array, by permuting the indices.- Parameters:
dims- the old index dims[k] becomes the new kth index.- Returns:
- the new Array
-
reshape
Create a new Array by copying this Array to a new one with given shape- Parameters:
shape- the new shape- Returns:
- the new Array
- Throws:
IllegalArgumentException- new shape is not conformable
-
reshapeNoCopy
Reshape this array without copying data- Parameters:
shape- the new shape- Returns:
- the new Array, using same backing object
- Throws:
IllegalArgumentException- new shape is not conformable
-
reduce
Create a new Array using same backing store as this Array, by eliminating any dimensions with length one.- Returns:
- the new Array, or the same array if no reduction was done
-
reduce
Create a new Array using same backing store as this Array, by eliminating the specified dimension.- Parameters:
dim- dimension to eliminate: must be of length one, else IllegalArgumentException- Returns:
- the new Array
-
isUnsigned
public boolean isUnsigned()Find whether the underlying data should be interpreted as unsigned. Only affects byte, short, int, and long. When true, conversions to wider types are handled correctly.- Returns:
- true if the data is an unsigned integer type.
-
isConstant
public boolean isConstant()If this is a constant array- Returns:
- If this is a constant array
-
isVlen
public boolean isVlen() -
getDouble
Get the array element at the current element of ima, as a double.- Parameters:
ima- Index with current element set- Returns:
- value at
indexcast to double if necessary.
-
setDouble
Set the array element at the current element of ima.- Parameters:
ima- Index with current element setvalue- the new value; cast to underlying data type if necessary.
-
getFloat
Get the array element at the current element of ima, as a float.- Parameters:
ima- Index with current element set- Returns:
- value at
indexcast to float if necessary.
-
setFloat
Set the array element at the current element of ima.- Parameters:
ima- Index with current element setvalue- the new value; cast to underlying data type if necessary.
-
getLong
Get the array element at the current element of ima, as a long.- Parameters:
ima- Index with current element set- Returns:
- value at
indexcast to long if necessary.
-
setLong
Set the array element at the current element of ima.- Parameters:
ima- Index with current element setvalue- the new value; cast to underlying data type if necessary.
-
getInt
Get the array element at the current element of ima, as a int.- Parameters:
ima- Index with current element set- Returns:
- value at
indexcast to int if necessary.
-
setInt
Set the array element at the current element of ima.- Parameters:
ima- Index with current element setvalue- the new value; cast to underlying data type if necessary.
-
getShort
Get the array element at the current element of ima, as a short.- Parameters:
ima- Index with current element set- Returns:
- value at
indexcast to short if necessary.
-
setShort
Set the array element at the current element of ima.- Parameters:
ima- Index with current element setvalue- the new value; cast to underlying data type if necessary.
-
getByte
Get the array element at the current element of ima, as a byte.- Parameters:
ima- Index with current element set- Returns:
- value at
indexcast to float if necessary.
-
setByte
Set the array element at the current element of ima.- Parameters:
ima- Index with current element setvalue- the new value; cast to underlying data type if necessary.
-
getChar
Get the array element at the current element of ima, as a char.- Parameters:
ima- Index with current element set- Returns:
- value at
indexcast to char if necessary.
-
setChar
Set the array element at the current element of ima.- Parameters:
ima- Index with current element setvalue- the new value; cast to underlying data type if necessary.
-
getBoolean
Get the array element at the current element of ima, as a boolean.- Parameters:
ima- Index with current element set- Returns:
- value at
indexcast to boolean if necessary. - Throws:
ForbiddenConversionException- if underlying array not boolean
-
setBoolean
Set the array element at the current element of ima.- Parameters:
ima- Index with current element setvalue- the new value; cast to underlying data type if necessary.- Throws:
ForbiddenConversionException- if underlying array not boolean
-
getObject
Get the array element at index as an Object. The returned value is wrapped in an object, eg Double for double- Parameters:
ima- element Index- Returns:
- Object value at
index - Throws:
ArrayIndexOutOfBoundsException- if index incorrect rank or out of bounds
-
setObject
Set the array element at index to the specified value. the value must be passed wrapped in the appropriate Object (eg Double for double)- Parameters:
ima- Index with current element setvalue- the new value.- Throws:
ArrayIndexOutOfBoundsException- if index incorrect rank or out of boundsClassCastException- if Object is incorrect type
-
getDouble
public abstract double getDouble(int elem) -
setDouble
public abstract void setDouble(int elem, double val) -
getFloat
public abstract float getFloat(int elem) -
setFloat
public abstract void setFloat(int elem, float val) -
getLong
public abstract long getLong(int elem) -
setLong
public abstract void setLong(int elem, long value) -
getInt
public abstract int getInt(int elem) -
setInt
public abstract void setInt(int elem, int value) -
getShort
public abstract short getShort(int elem) -
setShort
public abstract void setShort(int elem, short value) -
getByte
public abstract byte getByte(int elem) -
setByte
public abstract void setByte(int elem, byte value) -
getChar
public abstract char getChar(int elem) -
setChar
public abstract void setChar(int elem, char value) -
getBoolean
public abstract boolean getBoolean(int elem) -
setBoolean
public abstract void setBoolean(int elem, boolean value) -
getObject
-
setObject
-
toString
-
shapeToString
Create a string representation of the shape of this Array.- Returns:
- string representation of the shape
-
hasNext
public boolean hasNext()Check if more elements in the local iterator. Uses the local iterator, which is not thread-safe. Use getIndexIterator if you need thread-safety. You cannot call any of the array.nextXXX() methods without calling hasNext() first. If you are not sure of the state of the iterator, you must reset it before use. Example:arr.resetLocalIterator(); while (arr.hasNext()) { double val = mdata.nextDouble(); .. }- Returns:
- true if there are more elements in the iteration
-
next
Return the next object in the local iterator. Uses the local iterator, which is not thread-safe. Use getIndexIterator if you need thread-safety.- Returns:
- next element as an Object, same as IndexIterator.getObjectNext().
-
nextDouble
public double nextDouble()Return the next double in the local iterator. Uses the local iterator, which is not thread-safe. Use getIndexIterator if you need thread-safety.- Returns:
- next element as a double, same as IndexIterator.getDoubleNext().
-
nextFloat
public float nextFloat()Return the next float in the local iterator. Uses the local iterator, which is not thread-safe. Use getIndexIterator if you need thread-safety.- Returns:
- next element as a float, same as IndexIterator.getFloatNext().
-
nextByte
public byte nextByte()Return the next byte in the local iterator. Uses the local iterator, which is not thread-safe. Use getIndexIterator if you need thread-safety.- Returns:
- next element as a byte, same as IndexIterator.getByteNext().
-
nextShort
public short nextShort()Return the next short in the local iterator. Uses the local iterator, which is not thread-safe. Use getIndexIterator if you need thread-safety.- Returns:
- next element as a short, same as IndexIterator.getShortNext().
-
nextInt
public int nextInt()Return the next int in the local iterator. Uses the local iterator, which is not thread-safe. Use getIndexIterator if you need thread-safety.- Returns:
- next element as an int, same as IndexIterator.getIntNext().
-
nextLong
public long nextLong()Return the next long in the local iterator. Uses the local iterator, which is not thread-safe. Use getIndexIterator if you need thread-safety.- Returns:
- next element as a long, same as IndexIterator.getLongNext().
-
nextChar
public char nextChar()Return the next char in the local iterator. Uses the local iterator, which is not thread-safe. Use getIndexIterator if you need thread-safety.- Returns:
- next element as a char, same as IndexIterator.getCharNext().
-
nextBoolean
public boolean nextBoolean()Return the next boolean in the local iterator. Uses the local iterator, which is not thread-safe. Use getIndexIterator if you need thread-safety.- Returns:
- next element as a boolean, same as IndexIterator.getBooleanNext().
-
resetLocalIterator
public void resetLocalIterator()Reset the local iterator. Uses the local iterator, which is not thread-safe. Use getIndexIterator if you need thread-safety.
-