/* 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; }
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; }