| 1 4 package gnu.kawa.functions; 5 import gnu.lists.*; 6 import gnu.mapping.*; 7 import gnu.math.IntNum; 8 9 10 11 public class Arrays 12 { 13 static final int[] shapeStrides = { 2, 1} ; 14 static final int[] zeros2 = new int[2]; 15 16 public static Array shape (Object [] vals) 17 { 18 int len = vals.length; 19 if ((len & 1) != 0) 20 throw new RuntimeException ("shape: not an even number of arguments"); 21 int d = len >> 1; 22 int[] dims = { d, 2 }; 23 return new FVector(vals).transpose(zeros2, dims, 0, shapeStrides); 24 } 25 26 public static Array make(Array shape, Object value) 27 { 28 int rank = shape.getSize(0); 29 int[] dimensions = new int[rank]; 30 int[] lowBounds = null; 31 int total = 1; 32 for (int i = rank; --i >= 0; ) 33 { 34 int lo = ((Number ) shape.getRowMajor(2*i)).intValue(); 35 int hi = ((Number ) shape.getRowMajor(2*i+1)).intValue(); 36 int size = hi - lo; 37 dimensions[i] = size; 38 if (lo != 0) 39 { 40 if (lowBounds == null) 41 lowBounds = new int[rank]; 42 lowBounds[i] = lo; 43 } 44 total *= size; 45 } 46 return GeneralArray.makeSimple(lowBounds, dimensions, new FVector(total, value)); 47 } 48 49 public static Array makeSimple(Array shape, SimpleVector base) 50 { 51 int rank = shape.getSize(0); 52 int[] dimensions = new int[rank]; 53 int[] lowBounds = null; 54 for (int i = rank; --i >= 0; ) 55 { 56 int lo = ((Number ) shape.getRowMajor(2*i)).intValue(); 57 int hi = ((Number ) shape.getRowMajor(2*i+1)).intValue(); 58 dimensions[i] = hi - lo; 59 if (lo != 0) 60 { 61 if (lowBounds == null) 62 lowBounds = new int[rank]; 63 lowBounds[i] = lo; 64 } 65 } 66 return GeneralArray.makeSimple(lowBounds, dimensions, base); 67 } 68 69 public static int effectiveIndex (Array array, Procedure proc, 70 Object [] args, int[] work) 71 throws Throwable  72 { 73 Object mapval = proc.applyN(args); 74 if (mapval instanceof Values) 75 { 76 Values mapvals = (Values) mapval; 77 for (int i = 0, j = 0; (i = mapvals.nextPos(i)) != 0; j++) 78 { 79 work[j] = ((Number ) mapvals.getPosPrevious(i)).intValue(); 80 } 81 } 82 else 83 work[0] = ((Number ) mapval).intValue(); 84 return array.getEffectiveIndex(work); 85 } 86 87 public static Array shareArray(Array array, Array shape, 88 Procedure proc) 89 throws Throwable  90 { 91 int rank = shape.getSize(0); 92 Object [] args = new Object [rank]; 93 int[] dimensions = new int[rank]; 94 int[] lowBounds = new int[rank]; boolean empty = false; 96 for (int i = rank; --i >= 0; ) 97 { 98 Object low = shape.getRowMajor(2*i); 99 args[i] = low; 100 int lo = ((Number ) low).intValue(); 101 lowBounds[i] = lo; 102 int hi = ((Number ) shape.getRowMajor(2*i+1)).intValue(); 103 int size = hi - lo; 104 dimensions[i] = size; 105 if (size <= 0) 106 empty = true; 107 } 108 int arank = array.rank(); 109 int[] offsets = new int[rank]; 110 int offset0; 111 if (empty) 112 offset0 = 0; 113 else 114 { 115 int[] work = new int[arank]; 116 offset0 = effectiveIndex (array, proc, args, work); 117 for (int i = rank; --i >= 0; ) 118 { 119 int size = dimensions[i]; 120 int lo = lowBounds[i]; 121 if (size <= 1) 122 offsets[i] = 0; 123 else 124 { 125 Object low = args[i]; 126 args[i] = IntNum.make(lo + 1); 127 offsets[i] = (effectiveIndex (array, proc, args, work) 128 - offset0); 129 args[i] = low; 130 } 131 } 132 } 133 return array.transpose(lowBounds, dimensions, offset0, offsets); 134 } 135 } 136 | Popular Tags |