All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ucar.ma2.ArrayLong 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.ByteOrder;
import java.nio.LongBuffer;

/**
 * Concrete implementation of Array specialized for longs.
 * Data storage is with 1D java array of longs.
 *
 * issues: what should we do if a conversion loses accuracy? nothing ? Exception ?
 *
 * @see Array
 * @author caron
 */
public class ArrayLong extends Array {

  /** package private. use Array.factory() */
  static ArrayLong factory(Index index, boolean isUnsigned) {
    return ArrayLong.factory(index, isUnsigned, null);
  }

  /* create new ArrayLong with given indexImpl and backing store.
   * @param index use this Index
   * @param stor. use this storage. if null, allocate.
   * @return. new ArrayLong.D or ArrayLong object.
   */
  static ArrayLong factory( Index index, boolean isUnsigned, long [] storage) {
      if (index instanceof Index0D) {
          return new ArrayLong.D0(index, isUnsigned, storage);
      } else if (index instanceof Index1D) {
          return new ArrayLong.D1(index, isUnsigned, storage);
      } else if (index instanceof Index2D) {
          return new ArrayLong.D2(index, isUnsigned, storage);
      } else if (index instanceof Index3D) {
          return new ArrayLong.D3(index, isUnsigned, storage);
      } else if (index instanceof Index4D) {
          return new ArrayLong.D4(index, isUnsigned, storage);
      } else if (index instanceof Index5D) {
          return new ArrayLong.D5(index, isUnsigned, storage);
      } else if (index instanceof Index6D) {
          return new ArrayLong.D6(index, isUnsigned, storage);
      } else if (index instanceof Index7D) {
          return new ArrayLong.D7(index, isUnsigned, storage);
      } else {
          return new ArrayLong(index, isUnsigned, storage);
      }
  }

  //////////////////////////////////////////////////////
  protected long[] storage;

  /**
  * Create a new Array of type long and the given shape.
  * dimensions.length determines the rank of the new Array.
  * @param dimensions the shape of the Array.
  */
  public ArrayLong(int [] dimensions, boolean isUnsigned) {
    super(isUnsigned ? DataType.ULONG : DataType.LONG, dimensions);
    storage = new long[(int)indexCalc.getSize()];
  }

  /**
  * Create a new Array using the given IndexArray and backing store.
  * used for sections. Trusted package private.
  * @param ima use this IndexArray as the index
  * @param data use this as the backing store
  */
  ArrayLong(Index ima, boolean isUnsigned, long [] data) {
    super(isUnsigned ? DataType.ULONG : DataType.LONG, ima);
    /* replace by something better
    if (ima.getSize() != data.length)
      throw new IllegalArgumentException("bad data length"); */
    if (data != null)
      storage = data;
    else
      storage = new long[(int)ima.getSize()];
  }

  /** create new Array with given indexImpl and same backing store */
  protected Array createView( Index index) {
    return ArrayLong.factory( index, isUnsigned(), storage);
  }

  /* Get underlying primitive array storage. CAUTION! You may invalidate your warrentee! */
  public Object getStorage() { return storage; }

      // copy from javaArray to storage using the iterator: used by factory( Object);
  protected void copyFrom1DJavaArray(IndexIterator iter, Object javaArray) {
    long[] ja = (long []) javaArray;
    for (long aJa : ja) iter.setLongNext(aJa);
  }

  // copy to javaArray from storage using the iterator: used by copyToNDJavaArray;
  protected void copyTo1DJavaArray(IndexIterator iter, Object javaArray) {
    long[] ja = (long []) javaArray;
    for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy