1 30 31 32 package org.hsqldb.lib; 33 34 import java.lang.reflect.Array ; 35 36 43 public class ArrayUtil { 44 45 public static final int CLASS_CODE_BYTE = 'B'; 46 public static final int CLASS_CODE_CHAR = 'C'; 47 public static final int CLASS_CODE_DOUBLE = 'D'; 48 public static final int CLASS_CODE_FLOAT = 'F'; 49 public static final int CLASS_CODE_INT = 'I'; 50 public static final int CLASS_CODE_LONG = 'J'; 51 public static final int CLASS_CODE_OBJECT = 'L'; 52 public static final int CLASS_CODE_SHORT = 'S'; 53 public static final int CLASS_CODE_BOOLEAN = 'Z'; 54 private static IntValueHashMap classCodeMap = new IntValueHashMap(); 55 56 static { 57 classCodeMap.put(byte.class, ArrayUtil.CLASS_CODE_BYTE); 58 classCodeMap.put(char.class, ArrayUtil.CLASS_CODE_SHORT); 59 classCodeMap.put(short.class, ArrayUtil.CLASS_CODE_SHORT); 60 classCodeMap.put(int.class, ArrayUtil.CLASS_CODE_INT); 61 classCodeMap.put(long.class, ArrayUtil.CLASS_CODE_LONG); 62 classCodeMap.put(float.class, ArrayUtil.CLASS_CODE_FLOAT); 63 classCodeMap.put(double.class, ArrayUtil.CLASS_CODE_DOUBLE); 64 classCodeMap.put(boolean.class, ArrayUtil.CLASS_CODE_BOOLEAN); 65 classCodeMap.put(Object .class, ArrayUtil.CLASS_CODE_OBJECT); 66 } 67 68 71 static int getClassCode(Class cla) { 72 73 if (!cla.isPrimitive()) { 74 return ArrayUtil.CLASS_CODE_OBJECT; 75 } 76 77 return classCodeMap.get(cla, -1); 78 } 79 80 83 public static void clearArray(int type, Object data, int from, int to) { 84 85 switch (type) { 86 87 case ArrayUtil.CLASS_CODE_BYTE : { 88 byte[] array = (byte[]) data; 89 90 while (--to >= from) { 91 array[to] = 0; 92 } 93 94 return; 95 } 96 case ArrayUtil.CLASS_CODE_CHAR : { 97 byte[] array = (byte[]) data; 98 99 while (--to >= from) { 100 array[to] = 0; 101 } 102 103 return; 104 } 105 case ArrayUtil.CLASS_CODE_SHORT : { 106 short[] array = (short[]) data; 107 108 while (--to >= from) { 109 array[to] = 0; 110 } 111 112 return; 113 } 114 case ArrayUtil.CLASS_CODE_INT : { 115 int[] array = (int[]) data; 116 117 while (--to >= from) { 118 array[to] = 0; 119 } 120 121 return; 122 } 123 case ArrayUtil.CLASS_CODE_LONG : { 124 long[] array = (long[]) data; 125 126 while (--to >= from) { 127 array[to] = 0; 128 } 129 130 return; 131 } 132 case ArrayUtil.CLASS_CODE_FLOAT : { 133 float[] array = (float[]) data; 134 135 while (--to >= from) { 136 array[to] = 0; 137 } 138 139 return; 140 } 141 case ArrayUtil.CLASS_CODE_DOUBLE : { 142 double[] array = (double[]) data; 143 144 while (--to >= from) { 145 array[to] = 0; 146 } 147 148 return; 149 } 150 case ArrayUtil.CLASS_CODE_BOOLEAN : { 151 boolean[] array = (boolean[]) data; 152 153 while (--to >= from) { 154 array[to] = false; 155 } 156 157 return; 158 } 159 default : { 160 Object [] array = (Object []) data; 161 162 while (--to >= from) { 163 array[to] = null; 164 } 165 166 return; 167 } 168 } 169 } 170 171 181 public static void adjustArray(int type, Object array, int usedElements, 182 int index, int count) { 183 184 if (index >= usedElements) { 185 return; 186 } 187 188 int newCount = usedElements + count; 189 int source; 190 int target; 191 int size; 192 193 if (count >= 0) { 194 source = index; 195 target = index + count; 196 size = usedElements - index; 197 } else { 198 source = index - count; 199 target = index; 200 size = usedElements - index + count; 201 } 202 203 if (size > 0) { 204 System.arraycopy(array, source, array, target, size); 205 } 206 207 if (count < 0) { 208 clearArray(type, array, newCount, usedElements); 209 } 210 } 211 212 215 public static void sortArray(int[] array) { 216 217 boolean swapped; 218 219 do { 220 swapped = false; 221 222 for (int i = 0; i < array.length - 1; i++) { 223 if (array[i] > array[i + 1]) { 224 int temp = array[i + 1]; 225 226 array[i + 1] = array[i]; 227 array[i] = temp; 228 swapped = true; 229 } 230 } 231 } while (swapped); 232 } 233 234 237 public static int find(Object [] array, Object object) { 238 239 for (int i = 0; i < array.length; i++) { 240 if (array[i] == object) { 241 242 return i; 244 } 245 246 if (object != null && object.equals(array[i])) { 247 return i; 248 } 249 } 250 251 return -1; 252 } 253 254 257 public static int find(int[] array, int value) { 258 259 for (int i = 0; i < array.length; i++) { 260 if (array[i] == value) { 261 return i; 262 } 263 } 264 265 return -1; 266 } 267 268 271 public static int findNot(int[] array, int value) { 272 273 for (int i = 0; i < array.length; i++) { 274 if (array[i] != value) { 275 return i; 276 } 277 } 278 279 return -1; 280 } 281 282 287 public static boolean areEqualSets(int[] arra, int[] arrb) { 288 return arra.length == arrb.length 289 && ArrayUtil.haveEqualSets(arra, arrb, arra.length); 290 } 291 292 302 public static boolean areEqual(int[] arra, int[] arrb, int count, 303 boolean full) { 304 305 if (ArrayUtil.haveEqualArrays(arra, arrb, count)) { 306 if (full) { 307 return arra.length == arrb.length && count == arra.length; 308 } 309 310 return true; 311 } 312 313 return false; 314 } 315 316 321 public static boolean haveEqualSets(int[] arra, int[] arrb, int count) { 322 323 if (count > arra.length || count > arrb.length) { 324 return false; 325 } 326 327 if (count == 1) { 328 return arra[0] == arrb[0]; 329 } 330 331 int[] tempa = (int[]) resizeArray(arra, count); 332 int[] tempb = (int[]) resizeArray(arrb, count); 333 334 sortArray(tempa); 335 sortArray(tempb); 336 337 for (int j = 0; j < count; j++) { 338 if (tempa[j] != tempb[j]) { 339 return false; 340 } 341 } 342 343 return true; 344 } 345 346 351 public static boolean haveEqualArrays(int[] arra, int[] arrb, int count) { 352 353 if (count > arra.length || count > arrb.length) { 354 return false; 355 } 356 357 for (int j = 0; j < count; j++) { 358 if (arra[j] != arrb[j]) { 359 return false; 360 } 361 } 362 363 return true; 364 } 365 366 371 public static boolean haveEqualArrays(Object [] arra, Object [] arrb, 372 int count) { 373 374 if (count > arra.length || count > arrb.length) { 375 return false; 376 } 377 378 for (int j = 0; j < count; j++) { 379 if (arra[j] != arrb[j]) { 380 if (arra[j] == null ||!arra[j].equals(arrb[j])) { 381 return false; 382 } 383 } 384 } 385 386 return true; 387 } 388 389 395 public static boolean haveCommonElement(int[] arra, int[] arrb, 396 int bcount) { 397 398 for (int i = 0; i < arra.length; i++) { 399 int c = arra[i]; 400 401 for (int j = 0; j < bcount; j++) { 402 if (c == arrb[j]) { 403 return true; 404 } 405 } 406 } 407 408 return false; 409 } 410 411 439 public static int[] commonElements(int[] arra, int[] arrb) { 440 441 int[] c = null; 442 int n = countCommonElements(arra, arrb); 443 444 if (n > 0) { 445 c = new int[n]; 446 447 int k = 0; 448 449 for (int i = 0; i < arra.length; i++) { 450 for (int j = 0; j < arrb.length; j++) { 451 if (arra[i] == arrb[j]) { 452 c[k++] = arra[i]; 453 } 454 } 455 } 456 } 457 458 return c; 459 } 460 461 477 public static int countCommonElements(int[] arra, int[] arrb) { 478 479 int k = 0; 480 481 for (int i = 0; i < arra.length; i++) { 482 for (int j = 0; j < arrb.length; j++) { 483 if (arra[i] == arrb[j]) { 484 k++; 485 } 486 } 487 } 488 489 return k; 490 } 491 492 496 public static int countSameElements(byte[] arra, int start, byte[] arrb) { 497 498 int k = 0; 499 int limit = arra.length - start; 500 501 if (limit > arrb.length) { 502 limit = arrb.length; 503 } 504 505 for (int i = 0; i < limit; i++) { 506 if (arra[i + start] == arrb[i]) { 507 k++; 508 } else { 509 break; 510 } 511 } 512 513 return k; 514 } 515 516 519 public static int find(byte[] arra, int start, int limit, byte[] arrb) { 520 521 int k = 0; 522 523 limit = limit - arrb.length + 1; 524 525 int value = arrb[0]; 526 527 for (; k < limit; k++) { 528 if (arra[k] == value) { 529 if (arrb.length == 1) { 530 return k; 531 } 532 533 if (containsAt(arra, k, arrb)) { 534 return k; 535 } 536 } 537 } 538 539 return -1; 540 } 541 542 546 public static int findNotIn(byte[] arra, int start, int limit, 547 byte[] charset) { 548 549 int k = 0; 550 551 for (; k < limit; k++) { 552 for (int i = 0; i < charset.length; i++) { 553 if (arra[k] == charset[i]) { 554 continue; 555 } 556 } 557 558 return k; 559 } 560 561 return -1; 562 } 563 564 568 public static int findIn(byte[] arra, int start, int limit, 569 byte[] charset) { 570 571 int k = 0; 572 573 for (; k < limit; k++) { 574 for (int i = 0; i < charset.length; i++) { 575 if (arra[k] == charset[i]) { 576 return k; 577 } 578 } 579 } 580 581 return -1; 582 } 583 584 587 public static int find(byte[] arra, int start, int limit, int b, int c) { 588 589 int k = 0; 590 591 for (; k < limit; k++) { 592 if (arra[k] == b || arra[k] == c) { 593 return k; 594 } 595 } 596 597 return -1; 598 } 599 600 603 public static void intIndexesToBooleanArray(int[] arra, boolean[] arrb) { 604 605 int k = 0; 606 607 for (int i = 0; i < arra.length; i++) { 608 if (arra[i] < arrb.length) { 609 arrb[arra[i]] = true; 610 } 611 } 612 } 613 614 618 public static boolean containsAllTrueElements(boolean[] arra, 619 boolean[] arrb) { 620 621 for (int i = 0; i < arra.length; i++) { 622 if (arrb[i] &&!arra[i]) { 623 return false; 624 } 625 } 626 627 return true; 628 } 629 630 634 public static boolean containsAt(byte[] arra, int start, byte[] arrb) { 635 return countSameElements(arra, start, arrb) == arrb.length; 636 } 637 638 642 public static int countStartElementsAt(byte[] arra, int start, 643 byte[] arrb) { 644 645 int k = 0; 646 647 mainloop: 648 for (int i = start; i < arra.length; i++) { 649 for (int j = 0; j < arrb.length; j++) { 650 if (arra[i] == arrb[j]) { 651 k++; 652 653 continue mainloop; 654 } 655 } 656 657 break; 658 } 659 660 return k; 661 } 662 663 668 public static int countNonStartElementsAt(byte[] arra, int start, 669 byte[] arrb) { 670 671 int k = 0; 672 673 mainloop: 674 for (int i = start; i < arra.length; i++) { 675 for (int j = 0; j < arrb.length; j++) { 676 if (arra[i] == arrb[j]) { 677 break mainloop; 678 } 679 } 680 681 k++; 682 } 683 684 return k; 685 } 686 687 690 public static void copyArray(Object source, Object dest, int count) { 691 System.arraycopy(source, 0, dest, 0, count); 692 } 693 694 697 public static int[] arraySlice(int[] source, int start, int count) { 698 699 int[] slice = new int[count]; 700 701 System.arraycopy(source, start, slice, 0, count); 702 703 return slice; 704 } 705 706 709 public static void fillArray(Object [] array, Object value) { 710 711 int to = array.length; 712 713 while (--to >= 0) { 714 array[to] = value; 715 } 716 } 717 718 721 public static void fillArray(int[] array, int value) { 722 723 int to = array.length; 724 725 while (--to >= 0) { 726 array[to] = value; 727 } 728 } 729 730 733 public static Object duplicateArray(Object source) { 734 735 int size = Array.getLength(source); 736 Object newarray = 737 Array.newInstance(source.getClass().getComponentType(), size); 738 739 System.arraycopy(source, 0, newarray, 0, size); 740 741 return newarray; 742 } 743 744 749 public static Object resizeArrayIfDifferent(Object source, int newsize) { 750 751 int oldsize = Array.getLength(source); 752 753 if (oldsize == newsize) { 754 return source; 755 } 756 757 Object newarray = 758 Array.newInstance(source.getClass().getComponentType(), newsize); 759 760 if (oldsize < newsize) { 761 newsize = oldsize; 762 } 763 764 System.arraycopy(source, 0, newarray, 0, newsize); 765 766 return newarray; 767 } 768 769 774 public static Object resizeArray(Object source, int newsize) { 775 776 Object newarray = 777 Array.newInstance(source.getClass().getComponentType(), newsize); 778 int oldsize = Array.getLength(source); 779 780 if (oldsize < newsize) { 781 newsize = oldsize; 782 } 783 784 System.arraycopy(source, 0, newarray, 0, newsize); 785 786 return newarray; 787 } 788 789 796 public static Object toAdjustedArray(Object source, Object addition, 797 int colindex, int adjust) { 798 799 int newsize = Array.getLength(source) + adjust; 800 Object newarray = 801 Array.newInstance(source.getClass().getComponentType(), newsize); 802 803 copyAdjustArray(source, newarray, addition, colindex, adjust); 804 805 return newarray; 806 } 807 808 818 public static void copyAdjustArray(Object source, Object dest, 819 Object addition, int colindex, 820 int adjust) { 821 822 int length = Array.getLength(source); 823 824 if (colindex < 0) { 825 System.arraycopy(source, 0, dest, 0, length); 826 827 return; 828 } 829 830 System.arraycopy(source, 0, dest, 0, colindex); 831 832 if (adjust == 0) { 833 int endcount = length - colindex - 1; 834 835 Array.set(dest, colindex, addition); 836 837 if (endcount > 0) { 838 System.arraycopy(source, colindex + 1, dest, colindex + 1, 839 endcount); 840 } 841 } else if (adjust < 0) { 842 int endcount = length - colindex - 1; 843 844 if (endcount > 0) { 845 System.arraycopy(source, colindex + 1, dest, colindex, 846 endcount); 847 } 848 } else { 849 int endcount = length - colindex; 850 851 Array.set(dest, colindex, addition); 852 853 if (endcount > 0) { 854 System.arraycopy(source, colindex, dest, colindex + 1, 855 endcount); 856 } 857 } 858 } 859 860 880 public static int[] toAdjustedColumnArray(int[] colarr, int colindex, 881 int adjust) { 882 883 if (colarr == null) { 884 return null; 885 } 886 887 int[] intarr = new int[colarr.length]; 888 int j = 0; 889 890 for (int i = 0; i < colarr.length; i++) { 891 if (colarr[i] > colindex) { 892 intarr[j] = colarr[i] + adjust; 893 894 j++; 895 } else if (colarr[i] == colindex) { 896 if (adjust < 0) { 897 898 } else { 900 intarr[j] = colarr[i] + adjust; 901 902 j++; 903 } 904 } else { 905 intarr[j] = colarr[i]; 906 907 j++; 908 } 909 } 910 911 if (colarr.length != j) { 912 int[] newarr = new int[j]; 913 914 copyArray(intarr, newarr, j); 915 916 return newarr; 917 } 918 919 return intarr; 920 } 921 922 933 public static void copyColumnValues(Object [] row, int[] colindex, 934 Object [] colobject) { 935 936 for (int i = 0; i < colindex.length; i++) { 937 colobject[i] = row[colindex[i]]; 938 } 939 } 940 941 public static void copyColumnValues(int[] row, int[] colindex, 942 int[] colobject) { 943 944 for (int i = 0; i < colindex.length; i++) { 945 colobject[i] = row[colindex[i]]; 946 } 947 } 948 949 public static void fillSequence(int[] colindex) { 950 951 for (int i = 0; i < colindex.length; i++) { 952 colindex[i] = i; 953 } 954 } 955 984 } 985 | Popular Tags |