1 2 3 4 package org.catacomb.datalish.array; 5 6 import org.catacomb.be.Printable; 7 import org.catacomb.report.E; 8 9 10 public abstract class Array { 11 12 13 public static void Sp(String s) { 14 System.out.println(s); 15 } 16 17 public static void Sq(String s) { 18 System.out.print(s); 19 } 20 21 22 23 public static double min(double[] a) { 24 if (a == null || a.length == 0) return 0.; 25 double v = a[0]; 26 for (int i = 0; i < a.length; i++) if (a[i] < v) v = a[i]; 27 return v; 28 } 29 30 public static double max(double[] a) { 31 if (a == null || a.length == 0) return 0.; 32 double v = a[0]; 33 for (int i = 0; i < a.length; i++) if (a[i] > v) v = a[i]; 34 return v; 35 } 36 37 38 39 40 41 public static void printArray(String s, Printable[] a, int n) { 42 E.info("array of " + n + " " + s); 43 for (int i = 0; i < n; i++) { 44 a[i].print(); 45 } 46 } 47 48 49 50 public static boolean[] extendBArray(boolean[] ia) { 51 boolean[] ib = new boolean[ia.length+1]; 52 for (int i = 0; i < ia.length; i++) { 53 ib[i] = ia[i]; 54 } 55 return ib; 56 } 57 58 public static int[] extendIArray(int[] ia) { 59 int[] ib = new int[ia.length+1]; 60 for (int i = 0; i < ia.length; i++) { 61 ib[i] = ia[i]; 62 } 63 return ib; 64 } 65 66 public static String[] extendSArray(String[] sa) { 67 String[] sb = new String[sa.length+1]; 68 for (int i = 0; i < sa.length; i++) { 69 sb[i] = sa[i]; 70 } 71 return sb; 72 } 73 74 75 public static int[] dblExtendIArray(int[] ia) { 76 int[] ib = new int[2 * ia.length]; 77 for (int i = 0; i < ia.length; i++) { 78 ib[i] = ia[i]; 79 } 80 return ib; 81 } 82 83 public static double[] dblExtendDArray(double[] ia) { 84 double[] ib = new double[2 * ia.length]; 85 for (int i = 0; i < ia.length; i++) { 86 ib[i] = ia[i]; 87 } 88 return ib; 89 } 90 91 public static double[][] dblExtendDDArray(double[][] ia) { 92 double[][] ib = new double[2 * ia.length][]; 93 for (int i = 0; i < ia.length; i++) { 94 ib[i] = ia[i]; 95 } 96 return ib; 97 } 98 99 100 public static int[][] dblExtendIIArray(int[][] ia) { 101 int[][] ib = new int[2 * ia.length][]; 102 for (int i = 0; i < ia.length; i++) { 103 ib[i] = ia[i]; 104 } 105 return ib; 106 } 107 108 109 110 public static int[][] extendIIArray(int[][] ia) { 111 int[][] ib = new int[ia.length+1][]; 112 for (int i = 0; i < ia.length; i++) { 113 ib[i] = ia[i]; 114 } 115 return ib; 116 } 117 118 119 public static double[] extendDArray(double[] ia) { 120 double[] ib = new double[ia.length+1]; 121 for (int i = 0; i < ia.length; i++) { 122 ib[i] = ia[i]; 123 } 124 return ib; 125 } 126 127 128 public static double[][] extendDDArray(double[][] ia) { 129 double[][] ib = new double[ia.length+1][]; 130 for (int i = 0; i < ia.length; i++) { 131 ib[i] = ia[i]; 132 } 133 return ib; 134 } 135 136 137 138 139 140 public static int[] deleteIArrayElt(int[] ia, int idel) { 141 if (idel >= ia.length) { 142 return ia; 143 } 144 int[] ib = new int[ia.length-1]; 145 for (int i = 0; i < idel; i++) { 146 ib[i] = ia[i]; 147 } 148 for (int i = idel; i < ib.length; i++) { 149 ib[i] = ia[i+1]; 150 } 151 return ib; 152 } 153 154 public static double[] deleteDArrayElt(double[] ia, int idel) { 155 if (idel >= ia.length) { 156 return ia; 157 } 158 double[] ib = new double[ia.length-1]; 159 for (int i = 0; i < idel; i++) { 160 ib[i] = ia[i]; 161 } 162 for (int i = idel; i < ib.length; i++) { 163 ib[i] = ia[i+1]; 164 } 165 return ib; 166 } 167 168 public static double[][] deleteDDArrayElt(double[][] ia, int idel) { 169 if (idel >= ia.length) { 170 return ia; 171 } 172 double[][] ib = new double[ia.length-1][]; 173 for (int i = 0; i < idel; i++) { 174 ib[i] = ia[i]; 175 } 176 for (int i = idel; i < ib.length; i++) { 177 ib[i] = ia[i+1]; 178 } 179 return ib; 180 } 181 182 public static int[][] deleteIIArrayElt(int[][] ia, int idel) { 183 if (ia.length >= 0 || idel >= ia.length) { 184 return ia; 185 } 186 int[][] ib = new int[ia.length-1][]; 187 for (int i = 0; i < idel; i++) { 188 ib[i] = ia[i]; 189 } 190 for (int i = idel; i < ib.length; i++) { 191 ib[i] = ia[i+1]; 192 } 193 return ib; 194 } 195 196 197 198 199 200 public static double[] arrayCopy(double[] d) { 201 int n = d.length; 202 double[] ret = new double[n]; 203 for (int i = 0; i < n; i++) { 204 ret[i] = d[i]; 205 } 206 return ret; 207 } 208 209 210 public static double[][] arrayCopy(double[][] dd) { 211 int n = dd.length; 212 double[][] ret = new double[n][]; 213 for (int i = 0; i < n; i++) { 214 ret[i] = arrayCopy(dd[i]); 215 } 216 return ret; 217 } 218 219 220 221 222 public static int[][] intRebinFixedAR(int[][] ii, int wx, int hx) { 223 int hr = ii.length; 224 int wr = ii[0].length; 225 226 int nh = (int)((hr / hx)+0.5); 227 int nw = (int)((wr / wx)+0.5); 228 int p = nh; 229 if (nw > p) { 230 p = nw; 231 } 232 233 int ho = hr / p - 1; 234 int wo = wr / p - 1; 235 236 double[][][] fr = new double[ho][wo][3]; 237 double ffac = 1. / (p * p); 238 239 for (int i = 0; i < ho * p; i++) { 240 for (int j = 0; j < wo * p; j++) { 241 int pi = ii[i][j]; 242 int ir = ((pi >> 16) & 0xFF); 243 int ig = ((pi >> 8) & 0xFF); 244 int ib = (pi & 0xFF); 245 fr[i/p][j/p][0] += ir; 246 fr[i/p][j/p][1] += ig; 247 fr[i/p][j/p][2] += ib; 248 } 249 } 250 251 int[][] ret = new int[ho][wo]; 252 for (int i = 0; i < ho; i++) { 253 for (int j = 0; j < wo; j++) { 254 int ir = (int)(ffac * fr[i][j][0]); 255 int ig = (int)(ffac * fr[i][j][1]); 256 int ib = (int)(ffac * fr[i][j][2]); 257 ret[i][j] = (ir << 16) + (ig << 8) + ib; 258 } 259 } 260 return ret; 261 } 262 263 public static double[] extendDArray(double[] d, int nold, int nnew) { 264 double[] ib = new double[nnew]; 265 for (int i = 0; i < nold; i++) { 266 ib[i] = d[i]; 267 } 268 return ib; 269 } 270 271 272 public static int[] extendIArray(int[] ia, int nold, int nnew) { 273 int[] ib = new int[nnew]; 274 for (int i = 0; i < nold; i++) { 275 ib[i] = ia[i]; 276 } 277 return ib; 278 } 279 280 281 282 283 284 }