1 8 package org.lsmp.djep.vectorJep; 9 10 11 19 public class Dimensions 20 { 21 private int dims[]; 22 public static final Dimensions ONE = new Dimensions(1); 23 public static final Dimensions TWO = new Dimensions(2); 24 public static final Dimensions THREE = new Dimensions(3); 25 26 private Dimensions() {}; 27 28 private Dimensions(int d) { dims = new int[] {d}; } 29 30 private Dimensions(int d1,int d2) { dims = new int[] {d1,d2}; } 31 32 private Dimensions(int d[]) 33 { 34 dims = new int[d.length]; 35 System.arraycopy(d,0,dims,0,d.length); 36 } 37 41 public static Dimensions valueOf(int d) 42 { 43 switch(d) { 44 case 1: return Dimensions.ONE; 45 case 2: return Dimensions.TWO; 46 case 3: return Dimensions.THREE; 47 default: 48 return new Dimensions(d); 49 } 50 } 51 52 public static Dimensions valueOf(int rows,int cols) 53 { 54 return new Dimensions(rows,cols); 55 } 56 57 public static Dimensions valueOf(int d,Dimensions inDim) 58 { 59 Dimensions res = new Dimensions(); 60 res.dims = new int[inDim.rank()+1]; 61 res.dims[0]=d; 62 for(int i=0;i<inDim.rank();++i) 63 res.dims[i+1]=inDim.dims[i]; 64 return res; 65 } 66 67 68 public static Dimensions valueOf(Dimensions inDim,int d) 69 { 70 Dimensions res = new Dimensions(); 71 res.dims = new int[inDim.rank()+1]; 72 for(int i=0;i<inDim.rank();++i) 73 res.dims[i]=inDim.dims[i]; 74 res.dims[inDim.rank()+1]=d; 75 return res; 76 } 77 78 79 public static Dimensions valueOf(int dims[]) 80 { 81 if(dims.length == 1) return valueOf(dims[0]); 82 if(dims.length == 2) return valueOf(dims[0],dims[1]); 83 return new Dimensions(dims); 84 } 85 86 89 public int getFirstDim() { return dims[0]; } 90 94 public int getLastDim() { return dims[dims.length-1]; } 95 public int getIthDim(int i) { return dims[i]; } 96 97 98 public boolean is0D() { return dims.length == 1 && dims[0] == 1; } 99 100 public boolean is1D() { return dims.length == 1 && dims[0] != 1; } 101 102 public boolean is2D() { return dims.length == 2; } 103 107 public int numEles() 108 { 109 int res=1; 110 for(int i=0;i<dims.length;++i) res *= dims[i]; 111 return res; 112 } 113 114 public int rank() 115 { 116 if(is0D()) return 0; 117 return dims.length; 118 } 119 122 public String toString() 123 { 124 if(is0D()) return String.valueOf(dims[0]); 125 if(is1D()) return String.valueOf(dims[0]); 126 StringBuffer sb = new StringBuffer ("["+dims[0]); 127 for(int i=1;i<dims.length;++i) 128 sb.append(","+dims[i]); 129 sb.append("]"); 130 return sb.toString(); 131 } 132 133 134 public boolean equals(Dimensions dims2) 135 { 136 if(dims2 == null) return false; 137 if( dims.length != dims2.dims.length) return false; 138 for(int i=0;i<dims.length;++i) 139 { if(dims[i] != dims2.dims[i]) return false;} 140 return true; 141 } 142 143 146 public int hashcode() 147 { 148 int res =17; 149 for(int i=0;i<dims.length;++i) 150 res = 37*res + dims[i]; 151 return res; 152 } 153 } 154 | Popular Tags |