1 package nom.tam.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 import java.lang.reflect.Array;
35 import java.util.Arrays;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
38
39 import nom.tam.util.array.MultiArrayCopier;
40 import nom.tam.util.type.ElementType;
41
42
43
44
45
46 public final class ArrayFuncs {
47
48 private static final Logger LOG = Logger.getLogger(ArrayFuncs.class.getName());
49
50 private ArrayFuncs() {
51 }
52
53
54
55
56
57
58
59
60 public static int[] getReversed(int... index) {
61 int[] rev = new int[index.length];
62 int nm1 = index.length - 1;
63 for (int i = index.length; --i >= 0;) {
64 rev[i] = index[nm1 - i];
65 }
66 return rev;
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 public static void copy(Object src, int srcPos, Object dest, int destPos, int length, int step) {
83 if (!dest.getClass().equals(src.getClass())) {
84 throw new IllegalArgumentException("Mismatched types: src " + src.getClass() + ", dst " + dest.getClass());
85 }
86
87 if (src instanceof Object[]) {
88 final Object[] from = (Object[]) src;
89 final Object[] to = (Object[]) dest;
90 int toIndex = 0;
91 for (int index = srcPos; index < srcPos + length; index += step) {
92 ArrayFuncs.copy(from[index], srcPos, to[toIndex++], destPos, length, step);
93 }
94 } else if (step == 1) {
95 System.arraycopy(src, srcPos, dest, destPos, length);
96 } else {
97 final int srcLength = Array.getLength(src);
98 final int loopLength = Math.min(srcLength, srcPos + length);
99 for (int i = srcPos; i < loopLength; i += step) {
100 Array.set(dest, destPos++, Array.get(src, i));
101 }
102 }
103 }
104
105
106
107
108
109
110 public static String arrayDescription(Object o) {
111 Class<?> base = getBaseClass(o);
112 if (base == Void.TYPE) {
113 return "NULL";
114 }
115 return base.getSimpleName() + Arrays.toString(getDimensions(o));
116 }
117
118
119
120
121
122
123
124
125
126 @Deprecated
127 public static long computeLSize(Object o) {
128 return FitsEncoder.computeSize(o);
129 }
130
131
132
133
134
135
136
137
138
139 @Deprecated
140 public static int computeSize(Object o) {
141 return (int) computeLSize(o);
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160 public static Object convertArray(Object array, Class<?> newType) {
161 if (newType.equals(getBaseClass(array))) {
162
163 Object copy = Array.newInstance(newType, getDimensions(array));
164 copyArray(array, copy);
165 return copy;
166 }
167 return convertArray(array, newType, null);
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188 public static Object convertArray(Object array, Class<?> newType, boolean reuse) {
189 if (reuse) {
190 return convertArray(array, newType, null);
191 }
192 return convertArray(array, newType);
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206 @Deprecated
207 public static void copyArray(Object original, Object copy) throws IllegalArgumentException {
208 Class<? extends Object> cl = original.getClass();
209 if (!cl.isArray()) {
210 throw new IllegalArgumentException("original is not an array");
211 }
212
213 if (!copy.getClass().equals(cl)) {
214 throw new IllegalArgumentException("mismatch of types: " + cl.getName() + " vs " + copy.getClass().getName());
215 }
216
217 int length = Array.getLength(original);
218
219 if (Array.getLength(copy) != length) {
220 throw new IllegalArgumentException("mismatch of sizes: " + length + " vs " + Array.getLength(copy));
221 }
222
223 if (original instanceof Object[]) {
224 Object[] from = (Object[]) original;
225 Object[] to = (Object[]) copy;
226 for (int index = 0; index < length; index++) {
227 copyArray(from[index], to[index]);
228 }
229 } else {
230 System.arraycopy(original, 0, copy, 0, length);
231 }
232 }
233
234
235
236
237
238
239
240
241 public static void copyInto(Object array, Object mimic) {
242 MultiArrayCopier.copyInto(array, mimic);
243 }
244
245
246
247
248
249
250
251
252
253
254
255 public static Object curl(Object input, int... dimens) throws IllegalStateException {
256 if (input == null) {
257 return null;
258 }
259 if (!input.getClass().isArray()) {
260 throw new IllegalArgumentException("Attempt to curl a non-array: " + input.getClass());
261 }
262 if (input.getClass().getComponentType().isArray()) {
263 throw new IllegalArgumentException("Attempt to curl non-1D array: " + input.getClass());
264 }
265 int size = Array.getLength(input);
266 int test = 1;
267 for (int dimen : dimens) {
268 test *= dimen;
269 }
270 if (test != size) {
271 throw new IllegalStateException("Curled array does not fit desired dimensions: " + size + ", expected " + test
272 + " (" + getBaseClass(input) + ")");
273 }
274 Object newArray = ArrayFuncs.newInstance(getBaseClass(input), dimens);
275 MultiArrayCopier.copyInto(input, newArray);
276 return newArray;
277
278 }
279
280
281
282
283
284
285
286
287
288
289
290 public static Object deepClone(Object o) {
291 if (o == null) {
292 return null;
293 }
294
295 if (!o.getClass().isArray()) {
296 return genericClone(o);
297 }
298
299 if (o instanceof Object[]) {
300 Object[] array = (Object[]) o;
301 Object[] copy = (Object[]) ArrayFuncs.newInstance(array.getClass().getComponentType(), array.length);
302
303 for (int i = 0; i < array.length; i++) {
304 copy[i] = deepClone(array[i]);
305 }
306 return copy;
307 }
308
309
310
311 int length = Array.getLength(o);
312 Object copy = Array.newInstance(o.getClass().getComponentType(), length);
313 System.arraycopy(o, 0, copy, 0, length);
314 return copy;
315 }
316
317
318
319
320
321
322
323
324 public static Object flatten(Object input) {
325 if (input == null) {
326 return null;
327 }
328
329 if (!input.getClass().isArray()) {
330 return input;
331 }
332
333 int[] dimens = getDimensions(input);
334 if (dimens.length <= 1) {
335 return input;
336 }
337 int size = 1;
338 for (int dimen : dimens) {
339 size *= dimen;
340 }
341 Object flat = ArrayFuncs.newInstance(getBaseClass(input), size);
342 MultiArrayCopier.copyInto(input, flat);
343 return flat;
344 }
345
346
347
348
349
350
351
352
353
354
355
356
357
358 public static Object genericClone(Object o) {
359 if (o.getClass().isArray()) {
360 return deepClone(o);
361 }
362 if (!(o instanceof Cloneable)) {
363 LOG.log(Level.SEVERE, "generic clone called on a non clonable type");
364 return null;
365 }
366 try {
367 return o.getClass().getMethod("clone").invoke(o);
368 } catch (Exception e) {
369 LOG.log(Level.WARNING, "Implements cloneable, but does not apparently make clone public.", e);
370 return null;
371 }
372 }
373
374
375
376
377
378
379
380
381
382 public static Object getBaseArray(Object o) {
383 if (o instanceof Object[]) {
384 return getBaseArray(((Object[]) o)[0]);
385 }
386 return o;
387 }
388
389
390
391
392
393
394
395
396 public static Class<?> getBaseClass(Object o) {
397 if (o == null) {
398 return Void.TYPE;
399 }
400 Class<?> clazz = o.getClass();
401 while (clazz.isArray()) {
402 clazz = clazz.getComponentType();
403 }
404 return clazz;
405 }
406
407
408
409
410
411
412
413
414 public static int getBaseLength(Object o) {
415 if (o == null) {
416 return 0;
417 }
418 ElementType<?> type = ElementType.forClass(getBaseClass(o));
419 return type.size();
420 }
421
422
423
424
425
426
427
428
429
430
431
432 public static int[] getDimensions(Object o) {
433 if (o == null) {
434 return null;
435 }
436 Object object = o;
437 Class<?> clazz = o.getClass();
438 int ndim = 0;
439 while (clazz.isArray()) {
440 clazz = clazz.getComponentType();
441 ndim++;
442 }
443 clazz = o.getClass();
444 int[] dimens = new int[ndim];
445 ndim = 0;
446 while (clazz.isArray()) {
447 dimens[ndim] = -1;
448 if (object != null) {
449 int length = Array.getLength(object);
450 if (length > 0) {
451 dimens[ndim] = length;
452 object = Array.get(object, 0);
453 } else {
454 dimens[ndim] = 0;
455 object = null;
456 }
457 }
458 clazz = clazz.getComponentType();
459 ndim++;
460 }
461 return dimens;
462 }
463
464
465
466
467
468
469
470
471
472
473 public static Object mimicArray(Object array, Class<?> newType) {
474 int dims = 0;
475 Class<?> arrayClass = array.getClass();
476 while (arrayClass.isArray()) {
477 arrayClass = arrayClass.getComponentType();
478 dims++;
479 }
480
481 if (dims <= 1) {
482 return ArrayFuncs.newInstance(newType, Array.getLength(array));
483 }
484
485 Object[] xarray = (Object[]) array;
486 int[] dimens = new int[dims];
487 dimens[0] = xarray.length;
488
489 Object mimic = ArrayFuncs.newInstance(newType, dimens);
490
491 for (int i = 0; i < xarray.length; i++) {
492 Object temp = mimicArray(xarray[i], newType);
493 ((Object[]) mimic)[i] = temp;
494 }
495
496 return mimic;
497 }
498
499
500
501
502
503
504
505
506
507
508 public static boolean isEmpty(final Object o) {
509 return (o == null || Array.getLength(o) == 0);
510 }
511
512
513
514
515
516
517
518
519 @Deprecated
520 public static int nElements(Object o) {
521 return (int) nLElements(o);
522 }
523
524
525
526
527
528
529
530
531
532
533 public static Object newInstance(Class<?> cl, int... dims) {
534 if (dims.length == 0) {
535
536 dims = new int[] {1};
537 }
538 return Array.newInstance(cl, dims);
539 }
540
541
542
543
544
545
546
547
548 public static long nLElements(Object o) {
549 return countElements(o);
550 }
551
552
553
554
555
556
557
558
559 public static long countElements(Object o) {
560 if (o == null) {
561 return 0;
562 }
563
564 if (o instanceof Object[]) {
565 long count = 0;
566 for (Object e : (Object[]) o) {
567 count += nLElements(e);
568 }
569 return count;
570 }
571
572 if (o.getClass().isArray()) {
573 return Array.getLength(o);
574 }
575
576 return 1;
577 }
578
579
580
581
582
583
584
585
586
587 public static int[] reverseIndices(int... indices) {
588 int[] result = new int[indices.length];
589 int len = indices.length;
590 for (int i = 0; i < indices.length; i++) {
591 result[len - i - 1] = indices[i];
592 }
593 return result;
594 }
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613 public static int[] checkRegularArray(Object o, boolean allowNulls)
614 throws NullPointerException, IllegalArgumentException, ClassCastException {
615 if (!o.getClass().isArray()) {
616 throw new IllegalArgumentException("Not an array: " + o.getClass());
617 }
618
619 if (o.getClass().getComponentType().isPrimitive()) {
620 return new int[] {Array.getLength(o)};
621 }
622
623 int[] dim = getDimensions(o);
624 if (dim[0] == 0) {
625 return dim;
626 }
627
628 Class<?> type = null;
629 Object[] array = (Object[]) o;
630
631 for (int i = 0; i < dim[0]; i++) {
632 Object e = array[i];
633
634 if (e == null) {
635 if (allowNulls) {
636 continue;
637 }
638 throw new IllegalArgumentException("Entry at index " + i + " is null");
639 }
640
641 if (type == null) {
642 type = e.getClass();
643 } else if (!e.getClass().equals(type)) {
644 throw new ClassCastException(
645 "Mismatched component type at index " + i + ": " + e.getClass() + ", expected " + type);
646 }
647
648 if (e.getClass().isArray()) {
649 int[] sub = checkRegularArray(e, allowNulls);
650
651 if (sub.length + 1 != dim.length) {
652 throw new IllegalArgumentException("Mismatched component dimension at index " + i + ": " + sub.length
653 + ", expected " + (dim.length - 1));
654 }
655
656 if (sub[0] != dim[1]) {
657 throw new IllegalArgumentException(
658 "Mismatched component size at index " + i + ": " + sub[0] + ", expected " + dim[0]);
659 }
660 }
661 }
662
663 return dim;
664 }
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682 public static Object complexToDecimals(Object o, Class<?> decimalType) {
683
684 if (o instanceof ComplexValue) {
685 ComplexValue z = (ComplexValue) o;
686 if (float.class.equals(decimalType)) {
687 return new float[] {(float) z.re(), (float) z.im()};
688 }
689 return new double[] {z.re(), z.im()};
690 }
691
692 if (o instanceof ComplexValue[]) {
693 ComplexValue[] z = (ComplexValue[]) o;
694
695 if (float.class.equals(decimalType)) {
696 float[][] f = new float[z.length][];
697 for (int i = 0; i < z.length; i++) {
698 f[i] = (float[]) complexToDecimals(z[i], decimalType);
699 }
700 return f;
701 }
702
703 double[][] d = new double[z.length][];
704 for (int i = 0; i < z.length; i++) {
705 d[i] = (double[]) complexToDecimals(z[i], decimalType);
706 }
707 return d;
708 }
709
710 if (o instanceof Object[]) {
711 Object[] array = (Object[]) o;
712 Object[] z = null;
713
714 for (int i = 0; i < array.length; i++) {
715 Object e = complexToDecimals(array[i], decimalType);
716 if (z == null) {
717 z = (Object[]) Array.newInstance(e.getClass(), array.length);
718 }
719 z[i] = e;
720 }
721
722 return z;
723 }
724
725 throw new IllegalArgumentException("Cannot convert to complex values: " + o.getClass().getName());
726 }
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744 public static Object decimalsToComplex(Object array) throws IllegalArgumentException {
745 if (array instanceof float[]) {
746 float[] f = (float[]) array;
747 if (f.length == 2) {
748 return new ComplexValue.Float(f[0], f[1]);
749 }
750 if (f.length % 2 == 1) {
751 throw new IllegalArgumentException("Odd number floats for complex conversion: " + f.length);
752 }
753 ComplexValue[] z = new ComplexValue[f.length >>> 1];
754 for (int i = 0; i < f.length; i += 2) {
755 z[i >> 1] = new ComplexValue(f[i], f[i + 1]);
756 }
757 return z;
758 }
759
760 if (array instanceof double[]) {
761 double[] d = (double[]) array;
762 if (d.length == 2) {
763 return new ComplexValue(d[0], d[1]);
764 }
765 if (d.length % 2 == 1) {
766 throw new IllegalArgumentException("Odd number floats for complex conversion: " + d.length);
767 }
768 ComplexValue[] z = new ComplexValue[d.length >>> 1];
769 for (int i = 0; i < d.length; i += 2) {
770 z[i >> 1] = new ComplexValue(d[i], d[i + 1]);
771 }
772 return z;
773 }
774
775 if (array instanceof Object[]) {
776 Object[] o = (Object[]) array;
777 Object[] z = null;
778
779 for (int i = 0; i < o.length; i++) {
780 Object e = decimalsToComplex(o[i]);
781 if (z == null) {
782 z = (Object[]) Array.newInstance(e.getClass(), o.length);
783 }
784 z[i] = e;
785 }
786
787 return z;
788 }
789
790 throw new IllegalArgumentException("Cannot convert to complex values: " + array.getClass().getName());
791 }
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811 public static Object decimalsToComplex(Object re, Object im) {
812 if (!re.getClass().equals(im.getClass())) {
813 throw new IllegalArgumentException(
814 "Mismatched components: " + re.getClass().getName() + " vs " + re.getClass().getName());
815 }
816
817 if (re instanceof float[]) {
818 float[] fre = (float[]) re;
819 float[] fim = (float[]) im;
820 ComplexValue.Float[] z = new ComplexValue.Float[fre.length];
821 for (int i = fre.length; --i >= 0;) {
822 z[i] = new ComplexValue.Float(fre[i], fim[i]);
823 }
824 return z;
825 }
826 if (re instanceof double[]) {
827 double[] dre = (double[]) re;
828 double[] dim = (double[]) im;
829 ComplexValue[] z = new ComplexValue[dre.length];
830 for (int i = dre.length; --i >= 0;) {
831 z[i] = new ComplexValue(dre[i], dim[i]);
832 }
833 return z;
834 }
835 if (re instanceof Object[]) {
836 Object[] ore = (Object[]) re;
837 Object[] oim = (Object[]) im;
838 Object[] z = null;
839 for (int i = ore.length; --i >= 0;) {
840 Object e = decimalsToComplex(ore[i], oim[i]);
841 if (z == null) {
842 z = (Object[]) Array.newInstance(e.getClass(), ore.length);
843 }
844 z[i] = e;
845 }
846 return z;
847 }
848 throw new IllegalArgumentException("Cannot convert components to complex: " + re.getClass().getName());
849 }
850
851 private static void decimalToInteger(Object from, Object to, Quantizer q) {
852 if (from instanceof Object[]) {
853 Object[] a = (Object[]) from;
854 Object[] b = (Object[]) to;
855 for (int i = 0; i < a.length; i++) {
856 decimalToInteger(a[i], b[i], q);
857 }
858 } else {
859 for (int i = Array.getLength(from); --i >= 0;) {
860 long l = q.toLong(from instanceof double[] ? ((double[]) from)[i] : ((float[]) from)[i]);
861
862 if (to instanceof byte[]) {
863 ((byte[]) to)[i] = (byte) l;
864 } else if (to instanceof short[]) {
865 ((short[]) to)[i] = (short) l;
866 } else if (to instanceof int[]) {
867 ((int[]) to)[i] = (int) l;
868 } else {
869 ((long[]) to)[i] = l;
870 }
871 }
872 }
873 }
874
875 private static void integerToDecimal(Object from, Object to, Quantizer q) {
876 if (from instanceof Object[]) {
877 Object[] a = (Object[]) from;
878 Object[] b = (Object[]) to;
879 for (int i = 0; i < a.length; i++) {
880 integerToDecimal(a[i], b[i], q);
881 }
882 } else {
883 for (int i = Array.getLength(from); --i >= 0;) {
884 double d = q.toDouble(Array.getLong(from, i));
885
886 if (to instanceof float[]) {
887 ((float[]) to)[i] = (float) d;
888 } else {
889 ((double[]) to)[i] = d;
890 }
891 }
892 }
893 }
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919 public static Object convertArray(Object array, Class<?> newType, Quantizer quant) throws IllegalArgumentException {
920
921 if (!array.getClass().isArray()) {
922 throw new IllegalArgumentException("Not an array: " + array.getClass().getName());
923 }
924
925 Class<?> fromType = getBaseClass(array);
926
927 if (newType.isAssignableFrom(fromType)) {
928 return array;
929 }
930
931 boolean toComplex = ComplexValue.class.isAssignableFrom(newType);
932 if (toComplex) {
933 newType = (ComplexValue.Float.class.isAssignableFrom(newType) ? float.class : double.class);
934 }
935
936 if (!newType.isPrimitive() || newType == boolean.class || newType == char.class) {
937 throw new IllegalArgumentException("Not a supported numerical type: " + newType.getName());
938 }
939
940 boolean toInteger = (newType != float.class && newType != double.class);
941
942 if (ComplexValue.class.isAssignableFrom(fromType)) {
943 fromType = (ComplexValue.Float.class.isAssignableFrom(fromType) ? float.class : double.class);
944 array = complexToDecimals(array, fromType);
945 }
946
947 boolean fromInteger = (fromType != float.class && fromType != double.class);
948
949 Object t = Array.newInstance(newType, getDimensions(array));
950
951 if (quant != null) {
952 if (fromInteger && !toInteger) {
953 integerToDecimal(array, t, quant);
954 } else if (!fromInteger && toInteger) {
955 decimalToInteger(array, t, quant);
956 } else {
957 MultiArrayCopier.copyInto(array, t);
958 }
959 } else {
960 MultiArrayCopier.copyInto(array, t);
961 }
962
963 if (toComplex) {
964 t = decimalsToComplex(t);
965 }
966
967 return t;
968 }
969
970 }