## ucar.multiarray
Class MultiArrayImpl

    java.lang.Object
      ucar.multiarray.MultiArrayImpl

All Implemented Interfaces:
  Accessor, Cloneable, MultiArray, MultiArrayInfo, Serializable

public class MultiArrayImpl
extends Object
implements MultiArray, Cloneable, Serializable

A concrete, space efficent implementation of the MultiArray interface.

See Also:
  `MultiArray`, Serialized Form

Field Summary
`Object` `storage` The actual storage.

Constructor Summary
`MultiArrayImpl(Class componentType, int[] dimensions)` Create a new MultiArray of the given componentType and shape.
`MultiArrayImpl(int[] lengths, int[] products, Object storage)` Create a new MultiArrayImple of the given shape accessing externally created storage.
`MultiArrayImpl(int[] lengths, Object storage)` Create a new MultiArrayImpl of the given shape accessing externally created storage.
`MultiArrayImpl(MultiArray ma)` A copy constructor.

Method Summary
`Object` `clone()`
`void` `copyin(int[] origin, MultiArray data)` Aggregate write access.
`void` `copyin(int[] origin, MultiArrayImpl src)` Version `copyin` specialized and optimized for MultiArrayImpl.
`MultiArray` `copyout(int[] origin, int[] shape)` Aggregate read access.
`static Object` `fixDest(Object dst, int lengthNeeded, Class defaultComponentType)`
`Object` `get(int[] index)` Get (read) the array element at index.
`boolean` `getBoolean(int[] index)` Get the array element at index, as a boolean.
`byte` `getByte(int[] index)` Get the array element at index, as a byte.
`char` `getChar(int[] index)` Get the array element at index, as a char.
`Class` `getComponentType()` Returns the Class object representing the component type of the array.
`double` `getDouble(int[] index)` Get the array element at index, as a double.
`float` `getFloat(int[] index)` Get the array element at index, as a float.
`int` `getInt(int[] index)` Get the array element at index, as an int.
`int[]` `getLengths()` Discover the dimensions of this MultiArray.
`long` `getLong(int[] index)` Get the array element at index, as a long.
`int` `getRank()` Returns the number of dimensions of the array.
`short` `getShort(int[] index)` Get the array element at index, as a short.
`Object` `getStorage()`
`int` `indexMap(int[] index)` Convert index vector into integer index into storage.
`boolean` `isScalar()` Convenience interface; return `true` if and only if the rank is zero.
`boolean` `isUnlimited()` Returns `true` if and only if the effective dimension lengths can change.
`static void` `main(String[] args)`
`static int` `numberOfElements(int[] dimensions)` Used to figure out how storage is required for a given shape.
`static int` `numberOfElements(int[] dimensions, int[] products)` Used to figure out how storage is required for a given shape, retaining intermediate products.
`void` `set(int[] index, Object value)` Set (modify, write) the array element at index to the specified value.
`void` `setBoolean(int[] index, boolean value)` Set the array element at index to the specified boolean value.
`void` `setByte(int[] index, byte value)` Set the array element at index to the specified byte value.
`void` `setChar(int[] index, char value)` Set the array element at index to the specified char value.
`void` `setDouble(int[] index, double value)` Set the array element at index to the specified double value.
`void` `setFloat(int[] index, float value)` Set the array element at index to the specified float value.
`void` `setInt(int[] index, int value)` Set the array element at index to the specified int value.
`void` `setLong(int[] index, long value)` Set the array element at index to the specified long value.
`void` `setShort(int[] index, short value)` Set the array element at index to the specified short value.
`Object` `toArray()` Returns a new array containing all of the elements in this MultiArray.
`Object` `toArray(Object dst, int[] origin, int[] shape)` Returns an array containing elements of this MultiArray specified by origin and shape, possibly converting the component type.

Methods inherited from class java.lang.Object
`equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### storage

    public final Object storage

  The actual storage. An array of componentType. This member is exposed so that System.arraycopy(), etc can be used directly on the storage.

Constructor Detail

### MultiArrayImpl

    public MultiArrayImpl(Class componentType,
                          int[] dimensions)

  Create a new MultiArray of the given componentType and shape. Storage for the values is allocated and owned by this with default initialization.

Parameters:
  `componentType` - Class of the primitives or objects to be contained.
  `dimensions` - the shape of the MultiArray. dimensions.length determines the rank of the new MultiArray.

### MultiArrayImpl

    public MultiArrayImpl(MultiArray ma)
                   throws IOException

  A copy constructor.

  Create a new MultiArray with the same componentType and shape as the argument Storage for values is allocated and owned by this, and the values are initialized to the values of the argument.

Parameters:
  `ma` - the MultiArray to copy.

### MultiArrayImpl

    public MultiArrayImpl(int[] lengths,
                          Object storage)

  Create a new MultiArrayImpl of the given shape accessing externally created storage. It is up to the client to to mitigate conflicting access to the external storage.

Parameters:
  `lengths` - the shape of the MultiArray.
  `storage` - array Object which is storage

### MultiArrayImpl

    public MultiArrayImpl(int[] lengths,
                          int[] products,
                          Object storage)

  Create a new MultiArrayImple of the given shape accessing externally created storage. It is up to the client to to mitigate conflicting access to the external storage. Should be a protected constructor?

Parameters:
  `lengths` - describing the shape of the MultiArray.
  `products` - right-to-left accumulated sizes.
  `storage` - array Object which is storage

Method Detail

### numberOfElements

    public static int numberOfElements(int[] dimensions)

  Used to figure out how storage is required for a given shape. Compute the right to left multiplicative product of the argument.

  Parameters:
    `dimensions` - the shape.
  Returns:
    int product of the dimensions.

### numberOfElements

    public static int numberOfElements(int[] dimensions,
                                       int[] products)

  Used to figure out how storage is required for a given shape, retaining intermediate products. Compute the right to left multiplicative product of the first argument, modifying the second argument so that it contains the intermediate products.

  Parameters:
    `dimensions` - the shape.
    `products` - modified upon return to contain the intermediate products
  Returns:
    int product of the dimensions.

### getComponentType

    public Class getComponentType()

  Description copied from interface: `MultiArrayInfo`
  Returns the Class object representing the component type of the array.

  Specified by:
    `getComponentType` in interface `MultiArrayInfo`
  Returns:
    Class the component type
  See Also:
    `MultiArrayInfo.getComponentType()`

### getRank

    public int getRank()

  Description copied from interface: `MultiArrayInfo`
  Returns the number of dimensions of the array.

  Specified by:
    `getRank` in interface `MultiArrayInfo`
  Returns:
    int number of dimensions of the array
  See Also:
    `MultiArrayInfo.getRank()`

### getLengths

    public int[] getLengths()

  Description copied from interface: `MultiArrayInfo`
  Discover the dimensions of this MultiArray.

  Specified by:
    `getLengths` in interface `MultiArrayInfo`
  Returns:
    int array whose length is the rank of this MultiArray and whose elements represent the length of each of it's dimensions
  See Also:
    `MultiArrayInfo.getLengths()`

### isUnlimited

    public boolean isUnlimited()

  Description copied from interface: `MultiArrayInfo`
  Returns `true` if and only if the effective dimension lengths can change. For example, if this were implemented by a java.util.Vector.

  Specified by:
    `isUnlimited` in interface `MultiArrayInfo`
  Returns:
    boolean `true` iff this can grow
  See Also:
    `MultiArrayInfo.isUnlimited()`

### isScalar

    public boolean isScalar()

  Description copied from interface: `MultiArrayInfo`
  Convenience interface; return `true` if and only if the rank is zero.

  Specified by:
    `isScalar` in interface `MultiArrayInfo`
  Returns:
    boolean `true` iff rank == 0
  See Also:
    `MultiArrayInfo.isScalar()`

### get

    public Object get(int[] index)

  Description copied from interface: `Accessor`
  Get (read) the array element at index. The returned value is wrapped in an object if it has a primitive type. Length of index must be greater than or equal to the rank of this. Values of index components must be less than corresponding values from getLengths().

  Specified by:
    `get` in interface `Accessor`
  Parameters:
    `index` - MultiArray index
  Returns:
    Object value at `index`
  See Also:
    `Accessor.get(int[])`

### getBoolean

    public boolean getBoolean(int[] index)

  Description copied from interface: `Accessor`
  Get the array element at index, as a boolean.

  Specified by:
    `getBoolean` in interface `Accessor`
  See Also:
    `Accessor.getBoolean(int[])`

### getChar

    public char getChar(int[] index)

  Description copied from interface: `Accessor`
  Get the array element at index, as a char.

  Specified by:
    `getChar` in interface `Accessor`
  See Also:
    `Accessor.getChar(int[])`

### getByte

    public byte getByte(int[] index)

  Description copied from interface: `Accessor`
  Get the array element at index, as a byte.

  Specified by:
    `getByte` in interface `Accessor`
  See Also:
    `Accessor.getByte(int[])`

### getShort

    public short getShort(int[] index)

  Description copied from interface: `Accessor`
  Get the array element at index, as a short.

  Specified by:
    `getShort` in interface `Accessor`
  See Also:
    `Accessor.getShort(int[])`

### getInt

    public int getInt(int[] index)

  Description copied from interface: `Accessor`
  Get the array element at index, as an int.

  Specified by:
    `getInt` in interface `Accessor`
  See Also:
    `Accessor.getInt(int[])`

### getLong

    public long getLong(int[] index)

  Description copied from interface: `Accessor`
  Get the array element at index, as a long.

  Specified by:
    `getLong` in interface `Accessor`
  See Also:
    `Accessor.getLong(int[])`

### getFloat

    public float getFloat(int[] index)

  Description copied from interface: `Accessor`
  Get the array element at index, as a float.

  Specified by:
    `getFloat` in interface `Accessor`
  See Also:
    `Accessor.getFloat(int[])`

### getDouble

    public double getDouble(int[] index)

  Description copied from interface: `Accessor`
  Get the array element at index, as a double.

  Specified by:
    `getDouble` in interface `Accessor`
  See Also:
    `Accessor.getDouble(int[])`

### set

    public void set(int[] index,
                    Object value)

  Description copied from interface: `Accessor`
  Set (modify, write) the array element at index to the specified value. If the array has a primitive component type, the value may be unwrapped. Values of index components must be less than corresponding values from getLengths().

  Specified by:
    `set` in interface `Accessor`
  Parameters:
    `index` - MultiArray index
    `value` - the new value.
  See Also:
    `Accessor.set(int[], java.lang.Object)`

### setBoolean

    public void setBoolean(int[] index,
                           boolean value)

  Description copied from interface: `Accessor`
  Set the array element at index to the specified boolean value.

  Specified by:
    `setBoolean` in interface `Accessor`
  See Also:
    `Accessor.setBoolean(int[], boolean)`

### setChar

    public void setChar(int[] index,
                        char value)

  Description copied from interface: `Accessor`
  Set the array element at index to the specified char value.

  Specified by:
    `setChar` in interface `Accessor`
  See Also:
    `Accessor.setChar(int[], char)`

### setByte

    public void setByte(int[] index,
                        byte value)

  Description copied from interface: `Accessor`
  Set the array element at index to the specified byte value.

  Specified by:
    `setByte` in interface `Accessor`
  See Also:
    `Accessor.setByte(int[], byte)`

### setShort

    public void setShort(int[] index,
                         short value)

  Description copied from interface: `Accessor`
  Set the array element at index to the specified short value.

  Specified by:
    `setShort` in interface `Accessor`
  See Also:
    `Accessor.setShort(int[], short)`

### setInt

    public void setInt(int[] index,
                       int value)

  Description copied from interface: `Accessor`
  Set the array element at index to the specified int value.

  Specified by:
    `setInt` in interface `Accessor`
  See Also:
    `Accessor.setInt(int[], int)`

### setLong

    public void setLong(int[] index,
                        long value)

  Description copied from interface: `Accessor`
  Set the array element at index to the specified long value.

  Specified by:
    `setLong` in interface `Accessor`
  See Also:
    `Accessor.setLong(int[], long)`

### setFloat

    public void setFloat(int[] index,
                         float value)

  Description copied from interface: `Accessor`
  Set the array element at index to the specified float value.

  Specified by:
    `setFloat` in interface `Accessor`
  See Also:
    `Accessor.setFloat(int[], float)`

### setDouble

    public void setDouble(int[] index,
                          double value)

  Description copied from interface: `Accessor`
  Set the array element at index to the specified double value.

  Specified by:
    `setDouble` in interface `Accessor`
  See Also:
    `Accessor.setDouble(int[], double)`

### copyout

    public MultiArray copyout(int[] origin,
                              int[] shape)

  Description copied from interface: `Accessor`
  Aggregate read access. Return a new MultiArray of the same componentType as this, and with shape as specified, which is initialized to the values of this, as clipped to (origin, origin + shape).

  It is easier to implement than to specify :-).

  The main reason to implement this instead of using the equivalent proxy is for remote or file access.

  `assert(origin[ii] + shape[ii] <= lengths[ii]);`

  Specified by:
    `copyout` in interface `Accessor`
  Parameters:
    `origin` - int array specifying the starting index.
    `shape` - int array specifying the extents in each dimension. This becomes the shape of the return.
  Returns:
    the MultiArray with the specified shape
  See Also:
    `Accessor.copyout(int[], int[])`

### copyin

    public void copyin(int[] origin,
                       MultiArrayImpl src)

  Version `copyin` specialized and optimized for MultiArrayImpl.

  See Also:
    `Accessor.copyin(int[], ucar.multiarray.MultiArray)`

### copyin

    public void copyin(int[] origin,
                       MultiArray data)
                throws IOException

  Description copied from interface: `Accessor`
  Aggregate write access. Given a MultiArray, copy it into this at the specified starting index. TODO: clearer specification.

  Hopefully this member can be optimized in various situations.

  `assert(origin[ii] + (source.getLengths())[ii] <= (getLengths())[ii]);`

  Specified by:
    `copyin` in interface `Accessor`
  Parameters:
    `origin` - int array specifying the starting index.
    `data` - MultiArray with the same componentType as this and shape smaller than `this.getLengths() - origin`
  Throws:
    `IOException`
  See Also:
    `Accessor.copyin(int[], ucar.multiarray.MultiArray)`

### fixDest

    public static Object fixDest(Object dst,
                                 int lengthNeeded,
                                 Class defaultComponentType)

### getStorage

    public Object getStorage()

  Specified by:
    `getStorage` in interface `MultiArray`
  Returns:
    a the original one dimensional Array containing all the elements in this MultiArray
  See Also:
    `Accessor#getStorage`

### toArray

    public Object toArray()

  Description copied from interface: `Accessor`
  Returns a new array containing all of the elements in this MultiArray. The returned array is one dimensional. The order of the elements in the result is natural, as if we used an IndexIterator to step through the elements of this MultiArray. The component type of the result is the same as this.

  This method acts as bridge between array-based and MultiArray-based APIs.

  This method is functionally equivalent to

                      Object anArray = Array.newInstance(getComponentType(), 1);
                      int [] origin = new int[getRank()]
                      int [] shape = getDimensions();
                      return toArray(anArray, origin, shape);

  Specified by:
    `toArray` in interface `Accessor`
  Returns:
    a one dimensional Array containing all the elements in this MultiArray
  See Also:
    `Accessor.toArray()`

### toArray

    public Object toArray(Object dst,
                          int[] origin,
                          int[] shape)

  Description copied from interface: `Accessor`
  Returns an array containing elements of this MultiArray specified by origin and shape, possibly converting the component type. The returned array is one dimensional. The order of the elements in the result is natural, as if we used an IndexIterator to step through the elements of this MultiArray.

  The anArray argument should be an array. If it is large enough to contain the output, it is used and no new storage is allocated. Otherwise, new storage is allocated with the same component type as the argument, and the data is copied into it.

  This method acts as bridge between array-based and MultiArray-based APIs.

  This method is similar to copyout(origin, shape).toArray(), but avoids a copy operation and (potentially) an allocation.

  NOTE: Implementation of type conversion is deferred until JDK 1.2. Currently, the componentType of `anArray` must be the same as `this`

  Specified by:
    `toArray` in interface `Accessor`
  Returns:
    a one dimensional Array containing the specified elements
  See Also:
    `Accessor.toArray()`

### clone

    public Object clone()

  See Also:
    `Object.clone()`

### indexMap

    public int indexMap(int[] index)

  Convert index vector into integer index into storage.

### main

    public static void main(String[] args)