Ejemplo n.º 1
0
 public static Array makeFromValues(Array shape, Object[] values) {
   GeneralArray array = allocateArray(shape);
   int total = array.getSize();
   Object[] data = new Object[total];
   if (values != null && values.length > 0) {
     int j = 0;
     for (int i = 0; i < total; ++i) {
       data[i] = values[j];
       if (++j == values.length) j = 0;
     }
   }
   array.setBase(data);
   return array;
 }
Ejemplo n.º 2
0
 /* Create a view of a simple-array that is an affine index transform.
  */
 public static Array shareArray(Array array, Array shape, Procedure proc) throws Throwable {
   GeneralArray result = allocateArray(shape);
   int rank = result.rank();
   Object[] args = new Object[rank];
   int[] dimensions = result.getDimensions();
   int[] lowBounds = result.getLowBounds();
   boolean empty = result.getSize() == 0;
   for (int i = rank; --i >= 0; ) args[i] = Integer.valueOf(result.getLowBound(i));
   int arank = array.rank();
   int[] offsets = new int[rank];
   int offset0;
   if (empty) offset0 = 0;
   else {
     int[] work = new int[arank];
     offset0 = effectiveIndex(array, proc, args, work);
     for (int i = rank; --i >= 0; ) {
       int size = dimensions[i];
       int lo = lowBounds == null ? 0 : lowBounds[i];
       if (size <= 1) offsets[i] = 0;
       else {
         Object low = args[i];
         args[i] = IntNum.make(lo + 1);
         offsets[i] = (effectiveIndex(array, proc, args, work) - offset0);
         args[i] = low;
       }
     }
   }
   AVector base =
       array instanceof GeneralArray ? ((GeneralArray) array).getBase() : (AVector) array;
   result.setBase(base);
   result.setStrides(offsets, offset0);
   return result;
 }
Ejemplo n.º 3
0
 public static Array makeFromSimple(
     int[] dimensions, int[] lowBounds, Object buffer, PrimType elementType) {
   char sig1;
   if (elementType == null) sig1 = 'L';
   else {
     sig1 = elementType.getSignature().charAt(0);
     if (elementType.isUnsigned()) sig1 = Character.toLowerCase(sig1);
   }
   int rank = dimensions.length;
   SimpleVector base;
   switch (sig1) {
     case 'L':
       base = new FVector((Object[]) buffer);
       break;
     case 'B':
       base = new S8Vector((byte[]) buffer);
       break;
     case 'b':
       base = new U8Vector((byte[]) buffer);
       break;
     case 'I':
       base = new S32Vector((int[]) buffer);
       break;
     case 'i':
       base = new U32Vector((int[]) buffer);
       break;
     case 'J':
       base = new S64Vector((long[]) buffer);
       break;
     case 'j':
       base = new U64Vector((long[]) buffer);
       break;
     case 'S':
       base = new S16Vector((short[]) buffer);
       break;
     case 's':
       base = new U16Vector((short[]) buffer);
       break;
     case 'D':
       base = new F64Vector((double[]) buffer);
       break;
     case 'F':
       base = new F32Vector((float[]) buffer);
       break;
     default:
       throw new Error("bad type for makeFromSimple");
   }
   if (rank == 1 && (lowBounds == null || lowBounds[0] == 0)) return base;
   else return GeneralArray.makeSimple(lowBounds, dimensions, base);
 }
Ejemplo n.º 4
0
 /** Convenience method for resolving shape specifiers. */
 public static GeneralArray allocateArray(Array shape) {
   int srank = shape.rank();
   int rank = shape.getSize(0);
   if (srank != 1 && !(srank == 2 && shape.getSize(1) == 2))
     throw new RuntimeException("array shape must be a sequence or a rank*2 array");
   int[] dimensions = new int[rank];
   int[] lowBounds = null;
   for (int i = rank; --i >= 0; ) {
     int lo, hi;
     if (srank == 2) {
       lo = shape.getInt(i, 0);
       hi = shape.getInt(i, 1);
     } else if (shape instanceof IntSequence) {
       lo = 0;
       hi = ((IntSequence) shape).getInt(i);
     } else {
       Object dim = shape.get(i);
       Sequence sdim;
       if (dim instanceof Range.IntRange) {
         Range.IntRange range = (Range.IntRange) dim;
         if (range.getStepInt() != 1) ; // ERROR
         lo = range.getStartInt();
         hi = range.size() + lo;
       } else {
         lo = 0;
         hi = ((Number) dim).intValue();
       }
     }
     if (lo > hi) throw new RuntimeException("array dimension " + i + " has negative size");
     dimensions[i] = hi - lo;
     if (lo != 0) {
       if (lowBounds == null) lowBounds = new int[rank];
       lowBounds[i] = lo;
     }
   }
   return GeneralArray.make(null, dimensions, lowBounds, null, 0);
 }
Ejemplo n.º 5
0
 public static <E> Array<E> getTransformed(Array<E> base, Procedure transformer, Array shape) {
   GeneralArray ashape = allocateArray(shape);
   return new ProcTransformedArray(
       base, transformer, ashape.getDimensions(), ashape.getLowBounds());
 }
Ejemplo n.º 6
0
 public static <E> Array<E> getBuiltArray(Array shape, Procedure procedure) {
   GeneralArray ashape = allocateArray(shape);
   return new BuiltArray(procedure, ashape.getDimensions(), ashape.getLowBounds());
 }
Ejemplo n.º 7
0
 public static Array makeSimple(Array shape, SimpleVector base) {
   GeneralArray array = allocateArray(shape);
   array.setBase(base);
   return array;
 }