ucar.ma2.ArrayDouble Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 1998-2018 University Corporation for Atmospheric Research/Unidata
* See LICENSE for license information.
*/
package ucar.ma2;
import java.nio.ByteBuffer;
import java.nio.DoubleBuffer;
/**
* Concrete implementation of Array specialized for doubles.
* Data storage is with 1D java array of doubles.
*
* @see Array
* @author caron
*/
public class ArrayDouble extends Array {
/** package private. use Array.factory() */
static ArrayDouble factory(Index index) {
return ArrayDouble.factory(index, null);
}
/* Create new ArrayDouble with given indexImpl and backing store.
* Should be private.
* @param index use this Index
* @param stor. use this storage. if null, allocate.
* @return. new ArrayDouble.D or ArrayDouble object.
*/
static ArrayDouble factory( Index index, double [] storage) {
if (index instanceof Index0D) {
return new ArrayDouble.D0(index, storage);
} else if (index instanceof Index1D) {
return new ArrayDouble.D1(index, storage);
} else if (index instanceof Index2D) {
return new ArrayDouble.D2(index, storage);
} else if (index instanceof Index3D) {
return new ArrayDouble.D3(index, storage);
} else if (index instanceof Index4D) {
return new ArrayDouble.D4(index, storage);
} else if (index instanceof Index5D) {
return new ArrayDouble.D5(index, storage);
} else if (index instanceof Index6D) {
return new ArrayDouble.D6(index, storage);
} else if (index instanceof Index7D) {
return new ArrayDouble.D7(index, storage);
} else {
return new ArrayDouble(index, storage);
}
}
///////////////////////////////////////////////////////////////////////////////
protected double[] storageD;
/**
* Create a new Array of type double and the given shape.
* dimensions.length determines the rank of the new Array.
* @param shape the shape of the Array.
*/
public ArrayDouble(int [] shape) {
super(DataType.DOUBLE, shape);
storageD = new double[(int) indexCalc.getSize()];
}
/** create new Array with given indexImpl and the same backing store */
protected Array createView( Index index) {
return ArrayDouble.factory(index, storageD);
}
/**
* Create a new Array using the given IndexArray and backing store.
* used for sections, and factory. Trusted package private.
* @param ima use this IndexArray as the index
* @param data use this as the backing store. if null, allocate
*/
ArrayDouble(Index ima, double [] data) {
super(DataType.DOUBLE, ima);
if (data != null) {
storageD = data;
} else {
storageD = new double[(int) indexCalc.getSize()];
}
}
/* Get underlying primitive array storage. CAUTION! You may invalidate your warrentee! */
public Object getStorage() { return storageD; }
// copy from javaArray to storage using the iterator: used by factory( Object);
protected void copyFrom1DJavaArray(IndexIterator iter, Object javaArray) {
double[] ja = (double []) javaArray;
for (double aJa : ja) iter.setDoubleNext(aJa);
}
// copy to javaArray from storage using the iterator: used by copyToNDJavaArray;
protected void copyTo1DJavaArray(IndexIterator iter, Object javaArray) {
double[] ja = (double []) javaArray;
for (int i=0; i