3 * $Date: 2005-11-10 09:52:44 -0600 (Thu, 10 Nov 2005) $
6 * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017
7 * for use in SwingJS via transpilation into JavaScript using Java2Script.
9 * Copyright (C) 2003-2005 The Jmol Development Team
11 * Contact: jmol-developers@lists.sf.net
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29 // 4/23/15 BH getComponentType fix
31 import java.lang.reflect.Array;
33 import java.util.Arrays;
34 import java.util.Hashtable;
38 final public class AU {
41 * Very important that this not be used with Int32Array or Float32Array,
42 * because it is not initialize to all zeros in MSIE 9.
45 * @param minimumLength
48 public static Object ensureLength(Object array, int minimumLength) {
49 return (array != null && getLength(array) >= minimumLength ? array
50 : arrayCopyObject(array, minimumLength));
53 public static String[] ensureLengthS(String[] array, int minimumLength) {
54 return (array != null && array.length >= minimumLength ? array
55 : arrayCopyS(array, minimumLength));
58 public static float[] ensureLengthA(float[] array, int minimumLength) {
59 return (array != null && array.length >= minimumLength ? array: arrayCopyF(array, minimumLength));
62 public static int[] ensureLengthI(int[] array, int minimumLength) {
63 return (array != null && array.length >= minimumLength ? array : arrayCopyI(array, minimumLength));
66 public static short[] ensureLengthShort(short[] array, int minimumLength) {
67 return (array != null && array.length >= minimumLength ? array : arrayCopyShort(array, minimumLength));
70 public static byte[] ensureLengthByte(byte[] array, int minimumLength) {
71 return (array != null && array.length >= minimumLength ? array : arrayCopyByte(array, minimumLength));
75 * Very important that this not be used with Int32Array or Float32Array,
76 * because it is not initialized to all zeros in MSIE 9.
81 public static Object doubleLength(Object array) {
82 return arrayCopyObject(array, (array == null ? 16 : 2 * getLength(array)));
85 public static String[] doubleLengthS(String[] array) {
86 return arrayCopyS(array, (array == null ? 16 : 2 * array.length));
89 public static float[] doubleLengthF(float[] array) {
90 return arrayCopyF(array, (array == null ? 16 : 2 * array.length));
93 public static int[] doubleLengthI(int[] array) {
94 return arrayCopyI(array, (array == null ? 16 : 2 * array.length));
97 public static short[] doubleLengthShort(short[] array) {
98 return arrayCopyShort(array, (array == null ? 16 : 2 * array.length));
101 public static byte[] doubleLengthByte(byte[] array) {
102 return arrayCopyByte(array, (array == null ? 16 : 2 * array.length));
105 public static boolean[] doubleLengthBool(boolean[] array) {
106 return arrayCopyBool(array, (array == null ? 16 : 2 * array.length));
109 public static Object deleteElements(Object array, int firstElement,
111 if (nElements == 0 || array == null)
113 int oldLength = getLength(array);
114 if (firstElement >= oldLength)
116 int n = oldLength - (firstElement + nElements);
119 Object t = newInstanceO(array, firstElement + n);
120 if (firstElement > 0)
121 System.arraycopy(array, 0, t, 0, firstElement);
123 System.arraycopy(array, firstElement + nElements, t, firstElement, n);
128 * note -- cannot copy if array is null! does not copy if length is unchanged
134 public static Object arrayCopyObject(Object array, int newLength) {
135 int oldLength = (array == null ? -1 : getLength(array));
136 if (newLength < 0) newLength = oldLength;
137 if (newLength == oldLength)
142 * if (newLength < oldLength) return Clazz.array(-1, array, 0, newLength);
145 Object t = newInstanceO(array, newLength);
147 System.arraycopy(array, 0, t, 0, oldLength < newLength ? oldLength
154 * Very important that this not be used with Int32Array or Float32Array,
155 * because those need to be initialized to all zeros in MSIE 9, and
156 * MSIE 9 cannot distinguish Int32Array or Float32Array from Array.
162 private static Object newInstanceO(Object array, int n) {
163 return Array.newInstance(array.getClass().getComponentType(), n);
166 public static int getLength(Object array) {
170 * return array.length
174 return Array.getLength(array);
178 public static String[] arrayCopyS(String[] array, int newLength) {
179 int oldLength = (array == null ? -1 : array.length);
180 if (newLength < 0) newLength = oldLength;
184 * if (newLength < oldLength) return Clazz.array(-1, array, 0, newLength);
187 String[] t = new String[newLength];
189 System.arraycopy(array, 0, t, 0, oldLength < newLength ? oldLength
195 public static int[][] arrayCopyII(int[][] array, int newLength) {
196 int[][] t = newInt2(newLength);
198 int oldLength = array.length;
199 System.arraycopy(array, 0, t, 0, oldLength < newLength ? oldLength
205 public static T3[] arrayCopyPt(T3[] array, int newLength) {
207 newLength = array.length;
208 T3[] t = new T3[newLength];
210 int oldLength = array.length;
211 System.arraycopy(array, 0, t, 0, oldLength < newLength ? oldLength
217 public static float[] arrayCopyF(float[] array, int newLength) {
218 int oldLength = (array == null ? -1 : array.length);
219 if (newLength < 0) newLength = oldLength;
223 * if (newLength < oldLength) return Clazz.array(-1, array, 0, newLength);
226 float[] t = new float[newLength];
228 System.arraycopy(array, 0, t, 0, oldLength < newLength ? oldLength
234 public static int[] arrayCopyI(int[] array, int newLength) {
235 int oldLength = (array == null ? -1 : array.length);
236 if (newLength < 0) newLength = oldLength;
240 * if (newLength < oldLength) return Clazz.array(-1, array, 0, newLength);
243 int[] t = new int[newLength];
245 System.arraycopy(array, 0, t, 0, oldLength < newLength ? oldLength
252 * a specialized method that allows copying from a starting point either
253 * to the end or to the middle (color schemes, especially)
257 * @return array or null
259 public static int[] arrayCopyRangeI(int[] array, int i0, int n) {
262 int oldLength = array.length;
263 if (n == -1) n = oldLength;
264 if (n == -2) n = oldLength / 2;
268 * return Clazz.array(-1, array, i0, n);
273 int[] t = new int[n];
274 System.arraycopy(array, i0, t, 0, n);
279 public static int[] arrayCopyRangeRevI(int[] array, int i0, int n) {
285 * return Clazz.array(-1, array, i0, n).reverse();
288 int[] t = arrayCopyRangeI(array, i0, n);
291 for (int i = n / 2; --i >= 0;)
292 swapInt(t, i, n - 1 - i);
297 public static short[] arrayCopyShort(short[] array, int newLength) {
298 int oldLength = (array == null ? -1 : array.length);
299 if (newLength < 0) newLength = oldLength;
303 * if (newLength < oldLength) return Clazz.array(-1, array, 0, newLength);
306 short[] t = new short[newLength];
308 System.arraycopy(array, 0, t, 0, oldLength < newLength ? oldLength
314 public static byte[] arrayCopyByte(byte[] array, int newLength) {
315 int oldLength = (array == null ? -1 : array.length);
316 if (newLength < 0) newLength = oldLength;
320 * if (newLength < oldLength) return Clazz.array(-1, array, 0, newLength);
323 byte[] t = new byte[newLength];
325 System.arraycopy(array, 0, t, 0, oldLength < newLength ? oldLength
331 public static boolean[] arrayCopyBool(boolean[] array, int newLength) {
332 int oldLength = (array == null ? -1 : array.length);
333 if (newLength < 0) newLength = oldLength;
337 * if (newLength < oldLength) return Clazz.array(-1, array, 0, newLength);
340 boolean[] t = new boolean[newLength];
342 System.arraycopy(array, 0, t, 0, oldLength < newLength ? oldLength
348 public static void swapInt(int[] array, int indexA, int indexB) {
349 int t = array[indexA];
350 array[indexA] = array[indexB];
355 public static void swap(short[] array, int indexA, int indexB) {
356 short t = array[indexA];
357 array[indexA] = array[indexB];
361 public static void swap(float[] array, int indexA, int indexB) {
362 float t = array[indexA];
363 array[indexA] = array[indexB];
368 public static String dumpArray(String msg, float[][] A, int x1, int x2, int y1, int y2) {
369 String s = "dumpArray: " + msg + "\n";
370 for (int x = x1; x <= x2; x++)
371 s += "\t*" + x + "*";
372 for (int y = y2; y >= y1; y--) {
373 s += "\n*" + y + "*";
374 for (int x = x1; x <= x2; x++)
375 s += "\t" + (x < A.length && y < A[x].length ? A[x][y] : Float.NaN);
380 public static String dumpIntArray(int[] A, int n) {
382 for (int i = 0; i < n; i++)
387 public static String sortedItem(Lst<String> v, int n) {
392 String[] keys = v.toArray(new String[v.size()]);
394 return keys[n % keys.length];
398 * Helper method for creating a List<Tx>[] without warnings.
400 * @param <type> Type of objects in the list.
401 * @param size Array size.
402 * @return Array of List<type>
404 @SuppressWarnings("unchecked")
405 public static <type> Lst<type>[] createArrayOfArrayList(int size) {
406 return new Lst[size];
410 * Helper method for creating a Map<K, V>[] without warnings.
412 * @param <K> Type of object for the keys in the map.
413 * @param <V> Type of object for the values in the map.
414 * @param size Array size.
415 * @return Array of Map<K, V>
417 @SuppressWarnings("unchecked")
418 public static <K, V> Map<K, V>[] createArrayOfHashtable(int size) {
419 return new Hashtable[size];
422 public static void swap(Object[] o, int i, int j) {
428 public static float[][] newFloat2(int n) {
429 return new float[n][];
432 public static boolean[][] newBool2(int n) {
433 return new boolean[n][];
436 public static int[][] newInt2(int n) {
440 public static int[][][] newInt3(int nx, int ny) {
441 return (ny < 0 ? new int[nx][][] : new int[nx][ny][]);
444 public static float[][][] newFloat3(int nx, int ny) {
445 return (ny < 0 ? new float[nx][][] : new float[nx][ny][]);
448 public static int[][][][] newInt4(int n) {
449 return new int[n][][][];
452 public static short[][] newShort2(int n) {
453 return new short[n][];
456 public static byte[][] newByte2(int n) {
457 return new byte[n][];
460 public static double[][] newDouble2(int n) {
461 return new double[n][];
465 * remove all keys from a map that start with given root
468 * @return number removed
470 public static int removeMapKeys(Map<String, ?> map, String root) {
471 Lst<String> list = new Lst<String>();
472 for (String key: map.keySet())
473 if (key.startsWith(root))
475 for (int i = list.size(); --i >= 0;)
476 map.remove(list.get(i));
480 public static boolean isAS(Object x) {
481 return x instanceof String[];
484 public static boolean isASS(Object x) {
485 return x instanceof String[][];
488 public static boolean isAP(Object x) {
489 return x instanceof T3[];
492 public static boolean isAF(Object x) {
493 return x instanceof float[];
496 public static boolean isAFloat(Object x) {
497 return x instanceof Float[];
500 public static boolean isAD(Object x) {
501 return x instanceof double[];
504 public static boolean isADD(Object x) {
505 return x instanceof double[][];
508 public static boolean isAB(Object x) {
509 return x instanceof byte[];
512 public static boolean isAI(Object x) {
513 return x instanceof int[];
516 public static boolean isAII(Object x) {
517 return (x instanceof int[][]);
520 public static boolean isAFF(Object x) {
521 return x instanceof float[][];
524 public static boolean isAFFF(Object x) {
525 return x instanceof float[][][];
529 * Ensure that we have signed and not unsigned bytes coming out of any
530 * process, but particularly out of file reading.
535 public static byte[] ensureSignedBytes(byte[] b) {
540 * for (var i = b.length; --i >= 0;) { var j = b[i] &
541 * 0xFF; if (j >= 0x80) j -= 0x100; b[i] = j; }