1 7 8 package java.util; 9 10 import java.lang.reflect.*; 11 12 39 40 public class Arrays { 41 private Arrays() { 43 } 44 45 47 57 public static void sort(long[] a) { 58 sort1(a, 0, a.length); 59 } 60 61 81 public static void sort(long[] a, int fromIndex, int toIndex) { 82 rangeCheck(a.length, fromIndex, toIndex); 83 sort1(a, fromIndex, toIndex-fromIndex); 84 } 85 86 96 public static void sort(int[] a) { 97 sort1(a, 0, a.length); 98 } 99 100 120 public static void sort(int[] a, int fromIndex, int toIndex) { 121 rangeCheck(a.length, fromIndex, toIndex); 122 sort1(a, fromIndex, toIndex-fromIndex); 123 } 124 125 135 public static void sort(short[] a) { 136 sort1(a, 0, a.length); 137 } 138 139 159 public static void sort(short[] a, int fromIndex, int toIndex) { 160 rangeCheck(a.length, fromIndex, toIndex); 161 sort1(a, fromIndex, toIndex-fromIndex); 162 } 163 164 174 public static void sort(char[] a) { 175 sort1(a, 0, a.length); 176 } 177 178 198 public static void sort(char[] a, int fromIndex, int toIndex) { 199 rangeCheck(a.length, fromIndex, toIndex); 200 sort1(a, fromIndex, toIndex-fromIndex); 201 } 202 203 213 public static void sort(byte[] a) { 214 sort1(a, 0, a.length); 215 } 216 217 237 public static void sort(byte[] a, int fromIndex, int toIndex) { 238 rangeCheck(a.length, fromIndex, toIndex); 239 sort1(a, fromIndex, toIndex-fromIndex); 240 } 241 242 267 public static void sort(double[] a) { 268 sort2(a, 0, a.length); 269 } 270 271 305 public static void sort(double[] a, int fromIndex, int toIndex) { 306 rangeCheck(a.length, fromIndex, toIndex); 307 sort2(a, fromIndex, toIndex); 308 } 309 310 335 public static void sort(float[] a) { 336 sort2(a, 0, a.length); 337 } 338 339 373 public static void sort(float[] a, int fromIndex, int toIndex) { 374 rangeCheck(a.length, fromIndex, toIndex); 375 sort2(a, fromIndex, toIndex); 376 } 377 378 private static void sort2(double a[], int fromIndex, int toIndex) { 379 final long NEG_ZERO_BITS = Double.doubleToLongBits(-0.0d); 380 384 385 389 int numNegZeros = 0; 390 int i = fromIndex, n = toIndex; 391 while(i < n) { 392 if (a[i] != a[i]) { 393 double swap = a[i]; 394 a[i] = a[--n]; 395 a[n] = swap; 396 } else { 397 if (a[i]==0 && Double.doubleToLongBits(a[i])==NEG_ZERO_BITS) { 398 a[i] = 0.0d; 399 numNegZeros++; 400 } 401 i++; 402 } 403 } 404 405 sort1(a, fromIndex, n-fromIndex); 407 408 if (numNegZeros != 0) { 410 int j = binarySearch(a, 0.0d, fromIndex, n-1); do { 412 j--; 413 } while (j>=0 && a[j]==0.0d); 414 415 for (int k=0; k<numNegZeros; k++) 417 a[++j] = -0.0d; 418 } 419 } 420 421 422 private static void sort2(float a[], int fromIndex, int toIndex) { 423 final int NEG_ZERO_BITS = Float.floatToIntBits(-0.0f); 424 428 429 433 int numNegZeros = 0; 434 int i = fromIndex, n = toIndex; 435 while(i < n) { 436 if (a[i] != a[i]) { 437 float swap = a[i]; 438 a[i] = a[--n]; 439 a[n] = swap; 440 } else { 441 if (a[i]==0 && Float.floatToIntBits(a[i])==NEG_ZERO_BITS) { 442 a[i] = 0.0f; 443 numNegZeros++; 444 } 445 i++; 446 } 447 } 448 449 sort1(a, fromIndex, n-fromIndex); 451 452 if (numNegZeros != 0) { 454 int j = binarySearch(a, 0.0f, fromIndex, n-1); do { 456 j--; 457 } while (j>=0 && a[j]==0.0f); 458 459 for (int k=0; k<numNegZeros; k++) 461 a[++j] = -0.0f; 462 } 463 } 464 465 466 470 471 474 private static void sort1(long x[], int off, int len) { 475 if (len < 7) { 477 for (int i=off; i<len+off; i++) 478 for (int j=i; j>off && x[j-1]>x[j]; j--) 479 swap(x, j, j-1); 480 return; 481 } 482 483 int m = off + (len >> 1); if (len > 7) { 486 int l = off; 487 int n = off + len - 1; 488 if (len > 40) { int s = len/8; 490 l = med3(x, l, l+s, l+2*s); 491 m = med3(x, m-s, m, m+s); 492 n = med3(x, n-2*s, n-s, n); 493 } 494 m = med3(x, l, m, n); } 496 long v = x[m]; 497 498 int a = off, b = a, c = off + len - 1, d = c; 500 while(true) { 501 while (b <= c && x[b] <= v) { 502 if (x[b] == v) 503 swap(x, a++, b); 504 b++; 505 } 506 while (c >= b && x[c] >= v) { 507 if (x[c] == v) 508 swap(x, c, d--); 509 c--; 510 } 511 if (b > c) 512 break; 513 swap(x, b++, c--); 514 } 515 516 int s, n = off + len; 518 s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s); 519 s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s); 520 521 if ((s = b-a) > 1) 523 sort1(x, off, s); 524 if ((s = d-c) > 1) 525 sort1(x, n-s, s); 526 } 527 528 531 private static void swap(long x[], int a, int b) { 532 long t = x[a]; 533 x[a] = x[b]; 534 x[b] = t; 535 } 536 537 540 private static void vecswap(long x[], int a, int b, int n) { 541 for (int i=0; i<n; i++, a++, b++) 542 swap(x, a, b); 543 } 544 545 548 private static int med3(long x[], int a, int b, int c) { 549 return (x[a] < x[b] ? 550 (x[b] < x[c] ? b : x[a] < x[c] ? c : a) : 551 (x[b] > x[c] ? b : x[a] > x[c] ? c : a)); 552 } 553 554 557 private static void sort1(int x[], int off, int len) { 558 if (len < 7) { 560 for (int i=off; i<len+off; i++) 561 for (int j=i; j>off && x[j-1]>x[j]; j--) 562 swap(x, j, j-1); 563 return; 564 } 565 566 int m = off + (len >> 1); if (len > 7) { 569 int l = off; 570 int n = off + len - 1; 571 if (len > 40) { int s = len/8; 573 l = med3(x, l, l+s, l+2*s); 574 m = med3(x, m-s, m, m+s); 575 n = med3(x, n-2*s, n-s, n); 576 } 577 m = med3(x, l, m, n); } 579 int v = x[m]; 580 581 int a = off, b = a, c = off + len - 1, d = c; 583 while(true) { 584 while (b <= c && x[b] <= v) { 585 if (x[b] == v) 586 swap(x, a++, b); 587 b++; 588 } 589 while (c >= b && x[c] >= v) { 590 if (x[c] == v) 591 swap(x, c, d--); 592 c--; 593 } 594 if (b > c) 595 break; 596 swap(x, b++, c--); 597 } 598 599 int s, n = off + len; 601 s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s); 602 s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s); 603 604 if ((s = b-a) > 1) 606 sort1(x, off, s); 607 if ((s = d-c) > 1) 608 sort1(x, n-s, s); 609 } 610 611 614 private static void swap(int x[], int a, int b) { 615 int t = x[a]; 616 x[a] = x[b]; 617 x[b] = t; 618 } 619 620 623 private static void vecswap(int x[], int a, int b, int n) { 624 for (int i=0; i<n; i++, a++, b++) 625 swap(x, a, b); 626 } 627 628 631 private static int med3(int x[], int a, int b, int c) { 632 return (x[a] < x[b] ? 633 (x[b] < x[c] ? b : x[a] < x[c] ? c : a) : 634 (x[b] > x[c] ? b : x[a] > x[c] ? c : a)); 635 } 636 637 640 private static void sort1(short x[], int off, int len) { 641 if (len < 7) { 643 for (int i=off; i<len+off; i++) 644 for (int j=i; j>off && x[j-1]>x[j]; j--) 645 swap(x, j, j-1); 646 return; 647 } 648 649 int m = off + (len >> 1); if (len > 7) { 652 int l = off; 653 int n = off + len - 1; 654 if (len > 40) { int s = len/8; 656 l = med3(x, l, l+s, l+2*s); 657 m = med3(x, m-s, m, m+s); 658 n = med3(x, n-2*s, n-s, n); 659 } 660 m = med3(x, l, m, n); } 662 short v = x[m]; 663 664 int a = off, b = a, c = off + len - 1, d = c; 666 while(true) { 667 while (b <= c && x[b] <= v) { 668 if (x[b] == v) 669 swap(x, a++, b); 670 b++; 671 } 672 while (c >= b && x[c] >= v) { 673 if (x[c] == v) 674 swap(x, c, d--); 675 c--; 676 } 677 if (b > c) 678 break; 679 swap(x, b++, c--); 680 } 681 682 int s, n = off + len; 684 s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s); 685 s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s); 686 687 if ((s = b-a) > 1) 689 sort1(x, off, s); 690 if ((s = d-c) > 1) 691 sort1(x, n-s, s); 692 } 693 694 697 private static void swap(short x[], int a, int b) { 698 short t = x[a]; 699 x[a] = x[b]; 700 x[b] = t; 701 } 702 703 706 private static void vecswap(short x[], int a, int b, int n) { 707 for (int i=0; i<n; i++, a++, b++) 708 swap(x, a, b); 709 } 710 711 714 private static int med3(short x[], int a, int b, int c) { 715 return (x[a] < x[b] ? 716 (x[b] < x[c] ? b : x[a] < x[c] ? c : a) : 717 (x[b] > x[c] ? b : x[a] > x[c] ? c : a)); 718 } 719 720 721 724 private static void sort1(char x[], int off, int len) { 725 if (len < 7) { 727 for (int i=off; i<len+off; i++) 728 for (int j=i; j>off && x[j-1]>x[j]; j--) 729 swap(x, j, j-1); 730 return; 731 } 732 733 int m = off + (len >> 1); if (len > 7) { 736 int l = off; 737 int n = off + len - 1; 738 if (len > 40) { int s = len/8; 740 l = med3(x, l, l+s, l+2*s); 741 m = med3(x, m-s, m, m+s); 742 n = med3(x, n-2*s, n-s, n); 743 } 744 m = med3(x, l, m, n); } 746 char v = x[m]; 747 748 int a = off, b = a, c = off + len - 1, d = c; 750 while(true) { 751 while (b <= c && x[b] <= v) { 752 if (x[b] == v) 753 swap(x, a++, b); 754 b++; 755 } 756 while (c >= b && x[c] >= v) { 757 if (x[c] == v) 758 swap(x, c, d--); 759 c--; 760 } 761 if (b > c) 762 break; 763 swap(x, b++, c--); 764 } 765 766 int s, n = off + len; 768 s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s); 769 s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s); 770 771 if ((s = b-a) > 1) 773 sort1(x, off, s); 774 if ((s = d-c) > 1) 775 sort1(x, n-s, s); 776 } 777 778 781 private static void swap(char x[], int a, int b) { 782 char t = x[a]; 783 x[a] = x[b]; 784 x[b] = t; 785 } 786 787 790 private static void vecswap(char x[], int a, int b, int n) { 791 for (int i=0; i<n; i++, a++, b++) 792 swap(x, a, b); 793 } 794 795 798 private static int med3(char x[], int a, int b, int c) { 799 return (x[a] < x[b] ? 800 (x[b] < x[c] ? b : x[a] < x[c] ? c : a) : 801 (x[b] > x[c] ? b : x[a] > x[c] ? c : a)); 802 } 803 804 805 808 private static void sort1(byte x[], int off, int len) { 809 if (len < 7) { 811 for (int i=off; i<len+off; i++) 812 for (int j=i; j>off && x[j-1]>x[j]; j--) 813 swap(x, j, j-1); 814 return; 815 } 816 817 int m = off + (len >> 1); if (len > 7) { 820 int l = off; 821 int n = off + len - 1; 822 if (len > 40) { int s = len/8; 824 l = med3(x, l, l+s, l+2*s); 825 m = med3(x, m-s, m, m+s); 826 n = med3(x, n-2*s, n-s, n); 827 } 828 m = med3(x, l, m, n); } 830 byte v = x[m]; 831 832 int a = off, b = a, c = off + len - 1, d = c; 834 while(true) { 835 while (b <= c && x[b] <= v) { 836 if (x[b] == v) 837 swap(x, a++, b); 838 b++; 839 } 840 while (c >= b && x[c] >= v) { 841 if (x[c] == v) 842 swap(x, c, d--); 843 c--; 844 } 845 if (b > c) 846 break; 847 swap(x, b++, c--); 848 } 849 850 int s, n = off + len; 852 s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s); 853 s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s); 854 855 if ((s = b-a) > 1) 857 sort1(x, off, s); 858 if ((s = d-c) > 1) 859 sort1(x, n-s, s); 860 } 861 862 865 private static void swap(byte x[], int a, int b) { 866 byte t = x[a]; 867 x[a] = x[b]; 868 x[b] = t; 869 } 870 871 874 private static void vecswap(byte x[], int a, int b, int n) { 875 for (int i=0; i<n; i++, a++, b++) 876 swap(x, a, b); 877 } 878 879 882 private static int med3(byte x[], int a, int b, int c) { 883 return (x[a] < x[b] ? 884 (x[b] < x[c] ? b : x[a] < x[c] ? c : a) : 885 (x[b] > x[c] ? b : x[a] > x[c] ? c : a)); 886 } 887 888 889 892 private static void sort1(double x[], int off, int len) { 893 if (len < 7) { 895 for (int i=off; i<len+off; i++) 896 for (int j=i; j>off && x[j-1]>x[j]; j--) 897 swap(x, j, j-1); 898 return; 899 } 900 901 int m = off + (len >> 1); if (len > 7) { 904 int l = off; 905 int n = off + len - 1; 906 if (len > 40) { int s = len/8; 908 l = med3(x, l, l+s, l+2*s); 909 m = med3(x, m-s, m, m+s); 910 n = med3(x, n-2*s, n-s, n); 911 } 912 m = med3(x, l, m, n); } 914 double v = x[m]; 915 916 int a = off, b = a, c = off + len - 1, d = c; 918 while(true) { 919 while (b <= c && x[b] <= v) { 920 if (x[b] == v) 921 swap(x, a++, b); 922 b++; 923 } 924 while (c >= b && x[c] >= v) { 925 if (x[c] == v) 926 swap(x, c, d--); 927 c--; 928 } 929 if (b > c) 930 break; 931 swap(x, b++, c--); 932 } 933 934 int s, n = off + len; 936 s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s); 937 s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s); 938 939 if ((s = b-a) > 1) 941 sort1(x, off, s); 942 if ((s = d-c) > 1) 943 sort1(x, n-s, s); 944 } 945 946 949 private static void swap(double x[], int a, int b) { 950 double t = x[a]; 951 x[a] = x[b]; 952 x[b] = t; 953 } 954 955 958 private static void vecswap(double x[], int a, int b, int n) { 959 for (int i=0; i<n; i++, a++, b++) 960 swap(x, a, b); 961 } 962 963 966 private static int med3(double x[], int a, int b, int c) { 967 return (x[a] < x[b] ? 968 (x[b] < x[c] ? b : x[a] < x[c] ? c : a) : 969 (x[b] > x[c] ? b : x[a] > x[c] ? c : a)); 970 } 971 972 973 976 private static void sort1(float x[], int off, int len) { 977 if (len < 7) { 979 for (int i=off; i<len+off; i++) 980 for (int j=i; j>off && x[j-1]>x[j]; j--) 981 swap(x, j, j-1); 982 return; 983 } 984 985 int m = off + (len >> 1); if (len > 7) { 988 int l = off; 989 int n = off + len - 1; 990 if (len > 40) { int s = len/8; 992 l = med3(x, l, l+s, l+2*s); 993 m = med3(x, m-s, m, m+s); 994 n = med3(x, n-2*s, n-s, n); 995 } 996 m = med3(x, l, m, n); } 998 float v = x[m]; 999 1000 int a = off, b = a, c = off + len - 1, d = c; 1002 while(true) { 1003 while (b <= c && x[b] <= v) { 1004 if (x[b] == v) 1005 swap(x, a++, b); 1006 b++; 1007 } 1008 while (c >= b && x[c] >= v) { 1009 if (x[c] == v) 1010 swap(x, c, d--); 1011 c--; 1012 } 1013 if (b > c) 1014 break; 1015 swap(x, b++, c--); 1016 } 1017 1018 int s, n = off + len; 1020 s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s); 1021 s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s); 1022 1023 if ((s = b-a) > 1) 1025 sort1(x, off, s); 1026 if ((s = d-c) > 1) 1027 sort1(x, n-s, s); 1028 } 1029 1030 1033 private static void swap(float x[], int a, int b) { 1034 float t = x[a]; 1035 x[a] = x[b]; 1036 x[b] = t; 1037 } 1038 1039 1042 private static void vecswap(float x[], int a, int b, int n) { 1043 for (int i=0; i<n; i++, a++, b++) 1044 swap(x, a, b); 1045 } 1046 1047 1050 private static int med3(float x[], int a, int b, int c) { 1051 return (x[a] < x[b] ? 1052 (x[b] < x[c] ? b : x[a] < x[c] ? c : a) : 1053 (x[b] > x[c] ? b : x[a] > x[c] ? c : a)); 1054 } 1055 1056 1057 1078 public static void sort(Object [] a) { 1079 Object [] aux = (Object [])a.clone(); 1080 mergeSort(aux, a, 0, a.length, 0); 1081 } 1082 1083 1115 public static void sort(Object [] a, int fromIndex, int toIndex) { 1116 rangeCheck(a.length, fromIndex, toIndex); 1117 Object [] aux = cloneSubarray(a, fromIndex, toIndex); 1118 mergeSort(aux, a, fromIndex, toIndex, -fromIndex); 1119 } 1120 1121 1125 private static final int INSERTIONSORT_THRESHOLD = 7; 1126 1127 1131 private static <T> T[] cloneSubarray(T[] a, int from, int to) { 1132 int n = to - from; 1133 T[] result = (T[])Array.newInstance(a.getClass().getComponentType(), n); 1134 System.arraycopy(a, from, result, 0, n); 1135 return result; 1136 } 1137 1138 1145 private static void mergeSort(Object [] src, 1146 Object [] dest, 1147 int low, 1148 int high, 1149 int off) { 1150 int length = high - low; 1151 1152 if (length < INSERTIONSORT_THRESHOLD) { 1154 for (int i=low; i<high; i++) 1155 for (int j=i; j>low && 1156 ((Comparable ) dest[j-1]).compareTo(dest[j])>0; j--) 1157 swap(dest, j, j-1); 1158 return; 1159 } 1160 1161 int destLow = low; 1163 int destHigh = high; 1164 low += off; 1165 high += off; 1166 int mid = (low + high) >> 1; 1167 mergeSort(dest, src, low, mid, -off); 1168 mergeSort(dest, src, mid, high, -off); 1169 1170 if (((Comparable )src[mid-1]).compareTo(src[mid]) <= 0) { 1173 System.arraycopy(src, low, dest, destLow, length); 1174 return; 1175 } 1176 1177 for(int i = destLow, p = low, q = mid; i < destHigh; i++) { 1179 if (q >= high || p < mid && ((Comparable )src[p]).compareTo(src[q])<=0) 1180 dest[i] = src[p++]; 1181 else 1182 dest[i] = src[q++]; 1183 } 1184 } 1185 1186 1189 private static void swap(Object [] x, int a, int b) { 1190 Object t = x[a]; 1191 x[a] = x[b]; 1192 x[b] = t; 1193 } 1194 1195 1218 public static <T> void sort(T[] a, Comparator <? super T> c) { 1219 T[] aux = (T[])a.clone(); 1220 if (c==null) 1221 mergeSort(aux, a, 0, a.length, 0); 1222 else 1223 mergeSort(aux, a, 0, a.length, 0, c); 1224 } 1225 1226 1258 public static <T> void sort(T[] a, int fromIndex, int toIndex, 1259 Comparator <? super T> c) { 1260 rangeCheck(a.length, fromIndex, toIndex); 1261 T[] aux = (T[])cloneSubarray(a, fromIndex, toIndex); 1262 if (c==null) 1263 mergeSort(aux, a, fromIndex, toIndex, -fromIndex); 1264 else 1265 mergeSort(aux, a, fromIndex, toIndex, -fromIndex, c); 1266 } 1267 1268 1275 private static void mergeSort(Object [] src, 1276 Object [] dest, 1277 int low, int high, int off, 1278 Comparator c) { 1279 int length = high - low; 1280 1281 if (length < INSERTIONSORT_THRESHOLD) { 1283 for (int i=low; i<high; i++) 1284 for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--) 1285 swap(dest, j, j-1); 1286 return; 1287 } 1288 1289 int destLow = low; 1291 int destHigh = high; 1292 low += off; 1293 high += off; 1294 int mid = (low + high) >> 1; 1295 mergeSort(dest, src, low, mid, -off, c); 1296 mergeSort(dest, src, mid, high, -off, c); 1297 1298 if (c.compare(src[mid-1], src[mid]) <= 0) { 1301 System.arraycopy(src, low, dest, destLow, length); 1302 return; 1303 } 1304 1305 for(int i = destLow, p = low, q = mid; i < destHigh; i++) { 1307 if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0) 1308 dest[i] = src[p++]; 1309 else 1310 dest[i] = src[q++]; 1311 } 1312 } 1313 1314 1318 private static void rangeCheck(int arrayLen, int fromIndex, int toIndex) { 1319 if (fromIndex > toIndex) 1320 throw new IllegalArgumentException ("fromIndex(" + fromIndex + 1321 ") > toIndex(" + toIndex+")"); 1322 if (fromIndex < 0) 1323 throw new ArrayIndexOutOfBoundsException (fromIndex); 1324 if (toIndex > arrayLen) 1325 throw new ArrayIndexOutOfBoundsException (toIndex); 1326 } 1327 1328 1330 1350 public static int binarySearch(long[] a, long key) { 1351 int low = 0; 1352 int high = a.length-1; 1353 1354 while (low <= high) { 1355 int mid = (low + high) >> 1; 1356 long midVal = a[mid]; 1357 1358 if (midVal < key) 1359 low = mid + 1; 1360 else if (midVal > key) 1361 high = mid - 1; 1362 else 1363 return mid; } 1365 return -(low + 1); } 1367 1368 1369 1389 public static int binarySearch(int[] a, int key) { 1390 int low = 0; 1391 int high = a.length-1; 1392 1393 while (low <= high) { 1394 int mid = (low + high) >> 1; 1395 int midVal = a[mid]; 1396 1397 if (midVal < key) 1398 low = mid + 1; 1399 else if (midVal > key) 1400 high = mid - 1; 1401 else 1402 return mid; } 1404 return -(low + 1); } 1406 1407 1427 public static int binarySearch(short[] a, short key) { 1428 int low = 0; 1429 int high = a.length-1; 1430 1431 while (low <= high) { 1432 int mid = (low + high) >> 1; 1433 short midVal = a[mid]; 1434 1435 if (midVal < key) 1436 low = mid + 1; 1437 else if (midVal > key) 1438 high = mid - 1; 1439 else 1440 return mid; } 1442 return -(low + 1); } 1444 1445 1465 public static int binarySearch(char[] a, char key) { 1466 int low = 0; 1467 int high = a.length-1; 1468 1469 while (low <= high) { 1470 int mid = (low + high) >> 1; 1471 char midVal = a[mid]; 1472 1473 if (midVal < key) 1474 low = mid + 1; 1475 else if (midVal > key) 1476 high = mid - 1; 1477 else 1478 return mid; } 1480 return -(low + 1); } 1482 1483 1503 public static int binarySearch(byte[] a, byte key) { 1504 int low = 0; 1505 int high = a.length-1; 1506 1507 while (low <= high) { 1508 int mid = (low + high) >> 1; 1509 byte midVal = a[mid]; 1510 1511 if (midVal < key) 1512 low = mid + 1; 1513 else if (midVal > key) 1514 high = mid - 1; 1515 else 1516 return mid; } 1518 return -(low + 1); } 1520 1521 1542 public static int binarySearch(double[] a, double key) { 1543 return binarySearch(a, key, 0, a.length-1); 1544 } 1545 1546 private static int binarySearch(double[] a, double key, int low,int high) { 1547 while (low <= high) { 1548 int mid = (low + high) >> 1; 1549 double midVal = a[mid]; 1550 1551 int cmp; 1552 if (midVal < key) { 1553 cmp = -1; } else if (midVal > key) { 1555 cmp = 1; } else { 1557 long midBits = Double.doubleToLongBits(midVal); 1558 long keyBits = Double.doubleToLongBits(key); 1559 cmp = (midBits == keyBits ? 0 : (midBits < keyBits ? -1 : 1)); } 1563 1564 if (cmp < 0) 1565 low = mid + 1; 1566 else if (cmp > 0) 1567 high = mid - 1; 1568 else 1569 return mid; } 1571 return -(low + 1); } 1573 1574 1595 public static int binarySearch(float[] a, float key) { 1596 return binarySearch(a, key, 0, a.length-1); 1597 } 1598 1599 private static int binarySearch(float[] a, float key, int low,int high) { 1600 while (low <= high) { 1601 int mid = (low + high) >> 1; 1602 float midVal = a[mid]; 1603 1604 int cmp; 1605 if (midVal < key) { 1606 cmp = -1; } else if (midVal > key) { 1608 cmp = 1; } else { 1610 int midBits = Float.floatToIntBits(midVal); 1611 int keyBits = Float.floatToIntBits(key); 1612 cmp = (midBits == keyBits ? 0 : (midBits < keyBits ? -1 : 1)); } 1616 1617 if (cmp < 0) 1618 low = mid + 1; 1619 else if (cmp > 0) 1620 high = mid - 1; 1621 else 1622 return mid; } 1624 return -(low + 1); } 1626 1627 1628 1656 public static int binarySearch(Object [] a, Object key) { 1657 int low = 0; 1658 int high = a.length-1; 1659 1660 while (low <= high) { 1661 int mid = (low + high) >> 1; 1662 Comparable midVal = (Comparable )a[mid]; 1663 int cmp = midVal.compareTo(key); 1664 1665 if (cmp < 0) 1666 low = mid + 1; 1667 else if (cmp > 0) 1668 high = mid - 1; 1669 else 1670 return mid; } 1672 return -(low + 1); } 1674 1675 1705 public static <T> int binarySearch(T[] a, T key, Comparator <? super T> c) { 1706 if (c==null) { 1707 return binarySearch(a, key); 1708 } 1709 1710 int low = 0; 1711 int high = a.length-1; 1712 1713 while (low <= high) { 1714 int mid = (low + high) >> 1; 1715 T midVal = a[mid]; 1716 int cmp = c.compare(midVal, key); 1717 1718 if (cmp < 0) 1719 low = mid + 1; 1720 else if (cmp > 0) 1721 high = mid - 1; 1722 else 1723 return mid; } 1725 return -(low + 1); } 1727 1728 1729 1731 1743 public static boolean equals(long[] a, long[] a2) { 1744 if (a==a2) 1745 return true; 1746 if (a==null || a2==null) 1747 return false; 1748 1749 int length = a.length; 1750 if (a2.length != length) 1751 return false; 1752 1753 for (int i=0; i<length; i++) 1754 if (a[i] != a2[i]) 1755 return false; 1756 1757 return true; 1758 } 1759 1760 1772 public static boolean equals(int[] a, int[] a2) { 1773 if (a==a2) 1774 return true; 1775 if (a==null || a2==null) 1776 return false; 1777 1778 int length = a.length; 1779 if (a2.length != length) 1780 return false; 1781 1782 for (int i=0; i<length; i++) 1783 if (a[i] != a2[i]) 1784 return false; 1785 1786 return true; 1787 } 1788 1789 1801 public static boolean equals(short[] a, short a2[]) { 1802 if (a==a2) 1803 return true; 1804 if (a==null || a2==null) 1805 return false; 1806 1807 int length = a.length; 1808 if (a2.length != length) 1809 return false; 1810 1811 for (int i=0; i<length; i++) 1812 if (a[i] != a2[i]) 1813 return false; 1814 1815 return true; 1816 } 1817 1818 1830 public static boolean equals(char[] a, char[] a2) { 1831 if (a==a2) 1832 return true; 1833 if (a==null || a2==null) 1834 return false; 1835 1836 int length = a.length; 1837 if (a2.length != length) 1838 return false; 1839 1840 for (int i=0; i<length; i++) 1841 if (a[i] != a2[i]) 1842 return false; 1843 1844 return true; 1845 } 1846 1847 1859 public static boolean equals(byte[] a, byte[] a2) { 1860 if (a==a2) 1861 return true; 1862 if (a==null || a2==null) 1863 return false; 1864 1865 int length = a.length; 1866 if (a2.length != length) 1867 return false; 1868 1869 for (int i=0; i<length; i++) 1870 if (a[i] != a2[i]) 1871 return false; 1872 1873 return true; 1874 } 1875 1876 1888 public static boolean equals(boolean[] a, boolean[] a2) { 1889 if (a==a2) 1890 return true; 1891 if (a==null || a2==null) 1892 return false; 1893 1894 int length = a.length; 1895 if (a2.length != length) 1896 return false; 1897 1898 for (int i=0; i<length; i++) 1899 if (a[i] != a2[i]) 1900 return false; 1901 1902 return true; 1903 } 1904 1905 1923 public static boolean equals(double[] a, double[] a2) { 1924 if (a==a2) 1925 return true; 1926 if (a==null || a2==null) 1927 return false; 1928 1929 int length = a.length; 1930 if (a2.length != length) 1931 return false; 1932 1933 for (int i=0; i<length; i++) 1934 if (Double.doubleToLongBits(a[i])!=Double.doubleToLongBits(a2[i])) 1935 return false; 1936 1937 return true; 1938 } 1939 1940 1958 public static boolean equals(float[] a, float[] a2) { 1959 if (a==a2) 1960 return true; 1961 if (a==null || a2==null) 1962 return false; 1963 1964 int length = a.length; 1965 if (a2.length != length) 1966 return false; 1967 1968 for (int i=0; i<length; i++) 1969 if (Float.floatToIntBits(a[i])!=Float.floatToIntBits(a2[i])) 1970 return false; 1971 1972 return true; 1973 } 1974 1975 1976 1990 public static boolean equals(Object [] a, Object [] a2) { 1991 if (a==a2) 1992 return true; 1993 if (a==null || a2==null) 1994 return false; 1995 1996 int length = a.length; 1997 if (a2.length != length) 1998 return false; 1999 2000 for (int i=0; i<length; i++) { 2001 Object o1 = a[i]; 2002 Object o2 = a2[i]; 2003 if (!(o1==null ? o2==null : o1.equals(o2))) 2004 return false; 2005 } 2006 2007 return true; 2008 } 2009 2010 2011 2013 2020 public static void fill(long[] a, long val) { 2021 fill(a, 0, a.length, val); 2022 } 2023 2024 2041 public static void fill(long[] a, int fromIndex, int toIndex, long val) { 2042 rangeCheck(a.length, fromIndex, toIndex); 2043 for (int i=fromIndex; i<toIndex; i++) 2044 a[i] = val; 2045 } 2046 2047 2054 public static void fill(int[] a, int val) { 2055 fill(a, 0, a.length, val); 2056 } 2057 2058 2075 public static void fill(int[] a, int fromIndex, int toIndex, int val) { 2076 rangeCheck(a.length, fromIndex, toIndex); 2077 for (int i=fromIndex; i<toIndex; i++) 2078 a[i] = val; 2079 } 2080 2081 2088 public static void fill(short[] a, short val) { 2089 fill(a, 0, a.length, val); 2090 } 2091 2092 2109 public static void fill(short[] a, int fromIndex, int toIndex, short val) { 2110 rangeCheck(a.length, fromIndex, toIndex); 2111 for (int i=fromIndex; i<toIndex; i++) 2112 a[i] = val; 2113 } 2114 2115 2122 public static void fill(char[] a, char val) { 2123 fill(a, 0, a.length, val); 2124 } 2125 2126 2143 public static void fill(char[] a, int fromIndex, int toIndex, char val) { 2144 rangeCheck(a.length, fromIndex, toIndex); 2145 for (int i=fromIndex; i<toIndex; i++) 2146 a[i] = val; 2147 } 2148 2149 2156 public static void fill(byte[] a, byte val) { 2157 fill(a, 0, a.length, val); 2158 } 2159 2160 2177 public static void fill(byte[] a, int fromIndex, int toIndex, byte val) { 2178 rangeCheck(a.length, fromIndex, toIndex); 2179 for (int i=fromIndex; i<toIndex; i++) 2180 a[i] = val; 2181 } 2182 2183 2190 public static void fill(boolean[] a, boolean val) { 2191 fill(a, 0, a.length, val); 2192 } 2193 2194 2211 public static void fill(boolean[] a, int fromIndex, int toIndex, 2212 boolean val) { 2213 rangeCheck(a.length, fromIndex, toIndex); 2214 for (int i=fromIndex; i<toIndex; i++) 2215 a[i] = val; 2216 } 2217 2218 2225 public static void fill(double[] a, double val) { 2226 fill(a, 0, a.length, val); 2227 } 2228 2229 2246 public static void fill(double[] a, int fromIndex, int toIndex,double val){ 2247 rangeCheck(a.length, fromIndex, toIndex); 2248 for (int i=fromIndex; i<toIndex; i++) 2249 a[i] = val; 2250 } 2251 2252 2259 public static void fill(float[] a, float val) { 2260 fill(a, 0, a.length, val); 2261 } 2262 2263 2280 public static void fill(float[] a, int fromIndex, int toIndex, float val) { 2281 rangeCheck(a.length, fromIndex, toIndex); 2282 for (int i=fromIndex; i<toIndex; i++) 2283 a[i] = val; 2284 } 2285 2286 2293 public static void fill(Object [] a, Object val) { 2294 Arrays.fill(a, 0, a.length, val); 2295 } 2296 2297 2314 public static void fill(Object [] a, int fromIndex, int toIndex, Object val) { 2315 rangeCheck(a.length, fromIndex, toIndex); 2316 for (int i=fromIndex; i<toIndex; i++) 2317 a[i] = val; 2318 } 2319 2320 2321 2323 2340 public static <T> List <T> asList(T... a) { 2341 return new ArrayList <T>(a); 2342 } 2343 2344 2347 private static class ArrayList<E> extends AbstractList <E> 2348 implements RandomAccess , java.io.Serializable 2349 { 2350 private static final long serialVersionUID = -2764017481108945198L; 2351 private Object [] a; 2352 2353 ArrayList(E[] array) { 2354 if (array==null) 2355 throw new NullPointerException (); 2356 a = array; 2357 } 2358 2359 public int size() { 2360 return a.length; 2361 } 2362 2363 public Object [] toArray() { 2364 return (Object [])a.clone(); 2365 } 2366 2367 public E get(int index) { 2368 return (E)a[index]; 2369 } 2370 2371 public E set(int index, E element) { 2372 Object oldValue = a[index]; 2373 a[index] = element; 2374 return (E)oldValue; 2375 } 2376 2377 public int indexOf(Object o) { 2378 if (o==null) { 2379 for (int i=0; i<a.length; i++) 2380 if (a[i]==null) 2381 return i; 2382 } else { 2383 for (int i=0; i<a.length; i++) 2384 if (o.equals(a[i])) 2385 return i; 2386 } 2387 return -1; 2388 } 2389 2390 public boolean contains(Object o) { 2391 return indexOf(o) != -1; 2392 } 2393 } 2394 2395 2411 public static int hashCode(long a[]) { 2412 if (a == null) 2413 return 0; 2414 2415 int result = 1; 2416 for (long element : a) { 2417 int elementHash = (int)(element ^ (element >>> 32)); 2418 result = 31 * result + elementHash; 2419 } 2420 2421 return result; 2422 } 2423 2424 2440 public static int hashCode(int a[]) { 2441 if (a == null) 2442 return 0; 2443 2444 int result = 1; 2445 for (int element : a) 2446 result = 31 * result + element; 2447 2448 return result; 2449 } 2450 2451 2467 public static int hashCode(short a[]) { 2468 if (a == null) 2469 return 0; 2470 2471 int result = 1; 2472 for (short element : a) 2473 result = 31 * result + element; 2474 2475 return result; 2476 } 2477 2478 2494 public static int hashCode(char a[]) { 2495 if (a == null) 2496 return 0; 2497 2498 int result = 1; 2499 for (char element : a) 2500 result = 31 * result + element; 2501 2502 return result; 2503 } 2504 2505 2521 public static int hashCode(byte a[]) { 2522 if (a == null) 2523 return 0; 2524 2525 int result = 1; 2526 for (byte element : a) 2527 result = 31 * result + element; 2528 2529 return result; 2530 } 2531 2532 2548 public static int hashCode(boolean a[]) { 2549 if (a == null) 2550 return 0; 2551 2552 int result = 1; 2553 for (boolean element : a) 2554 result = 31 * result + (element ? 1231 : 1237); 2555 2556 return result; 2557 } 2558 2559 2575 public static int hashCode(float a[]) { 2576 if (a == null) 2577 return 0; 2578 2579 int result = 1; 2580 for (float element : a) 2581 result = 31 * result + Float.floatToIntBits(element); 2582 2583 return result; 2584 } 2585 2586 2602 public static int hashCode(double a[]) { 2603 if (a == null) 2604 return 0; 2605 2606 int result = 1; 2607 for (double element : a) { 2608 long bits = Double.doubleToLongBits(element); 2609 result = 31 * result + (int)(bits ^ (bits >>> 32)); 2610 } 2611 return result; 2612 } 2613 2614 2635 public static int hashCode(Object a[]) { 2636 if (a == null) 2637 return 0; 2638 2639 int result = 1; 2640 2641 for (Object element : a) 2642 result = 31 * result + (element == null ? 0 : element.hashCode()); 2643 2644 return result; 2645 } 2646 2647 2676 public static int deepHashCode(Object a[]) { 2677 if (a == null) 2678 return 0; 2679 2680 int result = 1; 2681 2682 for (Object element : a) { 2683 int elementHash = 0; 2684 if (element instanceof Object []) 2685 elementHash = deepHashCode((Object []) element); 2686 else if (element instanceof byte[]) 2687 elementHash = hashCode((byte[]) element); 2688 else if (element instanceof short[]) 2689 elementHash = hashCode((short[]) element); 2690 else if (element instanceof int[]) 2691 elementHash = hashCode((int[]) element); 2692 else if (element instanceof long[]) 2693 elementHash = hashCode((long[]) element); 2694 else if (element instanceof char[]) 2695 elementHash = hashCode((char[]) element); 2696 else if (element instanceof float[]) 2697 elementHash = hashCode((float[]) element); 2698 else if (element instanceof double[]) 2699 elementHash = hashCode((double[]) element); 2700 else if (element instanceof boolean[]) 2701 elementHash = hashCode((boolean[]) element); 2702 else if (element != null) 2703 elementHash = element.hashCode(); 2704 2705 result = 31 * result + elementHash; 2706 } 2707 2708 return result; 2709 } 2710 2711 2745 public static boolean deepEquals(Object [] a1, Object [] a2) { 2746 if (a1 == a2) 2747 return true; 2748 if (a1 == null || a2==null) 2749 return false; 2750 int length = a1.length; 2751 if (a2.length != length) 2752 return false; 2753 2754 for (int i = 0; i < length; i++) { 2755 Object e1 = a1[i]; 2756 Object e2 = a2[i]; 2757 2758 if (e1 == e2) 2759 continue; 2760 if (e1 == null) 2761 return false; 2762 2763 boolean eq; 2765 if (e1 instanceof Object [] && e2 instanceof Object []) 2766 eq = deepEquals ((Object []) e1, (Object []) e2); 2767 else if (e1 instanceof byte[] && e2 instanceof byte[]) 2768 eq = equals((byte[]) e1, (byte[]) e2); 2769 else if (e1 instanceof short[] && e2 instanceof short[]) 2770 eq = equals((short[]) e1, (short[]) e2); 2771 else if (e1 instanceof int[] && e2 instanceof int[]) 2772 eq = equals((int[]) e1, (int[]) e2); 2773 else if (e1 instanceof long[] && e2 instanceof long[]) 2774 eq = equals((long[]) e1, (long[]) e2); 2775 else if (e1 instanceof char[] && e2 instanceof char[]) 2776 eq = equals((char[]) e1, (char[]) e2); 2777 else if (e1 instanceof float[] && e2 instanceof float[]) 2778 eq = equals((float[]) e1, (float[]) e2); 2779 else if (e1 instanceof double[] && e2 instanceof double[]) 2780 eq = equals((double[]) e1, (double[]) e2); 2781 else if (e1 instanceof boolean[] && e2 instanceof boolean[]) 2782 eq = equals((boolean[]) e1, (boolean[]) e2); 2783 else 2784 eq = e1.equals(e2); 2785 2786 if (!eq) 2787 return false; 2788 } 2789 return true; 2790 } 2791 2792 2805 public static String toString(long[] a) { 2806 if (a == null) 2807 return "null"; 2808 if (a.length == 0) 2809 return "[]"; 2810 2811 StringBuilder buf = new StringBuilder (); 2812 buf.append('['); 2813 buf.append(a[0]); 2814 2815 for (int i = 1; i < a.length; i++) { 2816 buf.append(", "); 2817 buf.append(a[i]); 2818 } 2819 2820 buf.append("]"); 2821 return buf.toString(); 2822 } 2823 2824 2837 public static String toString(int[] a) { 2838 if (a == null) 2839 return "null"; 2840 if (a.length == 0) 2841 return "[]"; 2842 2843 StringBuilder buf = new StringBuilder (); 2844 buf.append('['); 2845 buf.append(a[0]); 2846 2847 for (int i = 1; i < a.length; i++) { 2848 buf.append(", "); 2849 buf.append(a[i]); 2850 } 2851 2852 buf.append("]"); 2853 return buf.toString(); 2854 } 2855 2856 2869 public static String toString(short[] a) { 2870 if (a == null) 2871 return "null"; 2872 if (a.length == 0) 2873 return "[]"; 2874 2875 StringBuilder buf = new StringBuilder (); 2876 buf.append('['); 2877 buf.append(a[0]); 2878 2879 for (int i = 1; i < a.length; i++) { 2880 buf.append(", "); 2881 buf.append(a[i]); 2882 } 2883 2884 buf.append("]"); 2885 return buf.toString(); 2886 } 2887 2888 2901 public static String toString(char[] a) { 2902 if (a == null) 2903 return "null"; 2904 if (a.length == 0) 2905 return "[]"; 2906 2907 StringBuilder buf = new StringBuilder (); 2908 buf.append('['); 2909 buf.append(a[0]); 2910 2911 for (int i = 1; i < a.length; i++) { 2912 buf.append(", "); 2913 buf.append(a[i]); 2914 } 2915 2916 buf.append("]"); 2917 return buf.toString(); 2918 } 2919 2920 2933 public static String toString(byte[] a) { 2934 if (a == null) 2935 return "null"; 2936 if (a.length == 0) 2937 return "[]"; 2938 2939 StringBuilder buf = new StringBuilder (); 2940 buf.append('['); 2941 buf.append(a[0]); 2942 2943 for (int i = 1; i < a.length; i++) { 2944 buf.append(", "); 2945 buf.append(a[i]); 2946 } 2947 2948 buf.append("]"); 2949 return buf.toString(); 2950 } 2951 2952 2965 public static String toString(boolean[] a) { 2966 if (a == null) 2967 return "null"; 2968 if (a.length == 0) 2969 return "[]"; 2970 2971 StringBuilder buf = new StringBuilder (); 2972 buf.append('['); 2973 buf.append(a[0]); 2974 2975 for (int i = 1; i < a.length; i++) { 2976 buf.append(", "); 2977 buf.append(a[i]); 2978 } 2979 2980 buf.append("]"); 2981 return buf.toString(); 2982 } 2983 2984 2997 public static String toString(float[] a) { 2998 if (a == null) 2999 return "null"; 3000 if (a.length == 0) 3001 return "[]"; 3002 3003 StringBuilder buf = new StringBuilder (); 3004 buf.append('['); 3005 buf.append(a[0]); 3006 3007 for (int i = 1; i < a.length; i++) { 3008 buf.append(", "); 3009 buf.append(a[i]); 3010 } 3011 3012 buf.append("]"); 3013 return buf.toString(); 3014 } 3015 3016 3029 public static String toString(double[] a) { 3030 if (a == null) 3031 return "null"; 3032 if (a.length == 0) 3033 return "[]"; 3034 3035 StringBuilder buf = new StringBuilder (); 3036 buf.append('['); 3037 buf.append(a[0]); 3038 3039 for (int i = 1; i < a.length; i++) { 3040 buf.append(", "); 3041 buf.append(a[i]); 3042 } 3043 3044 buf.append("]"); 3045 return buf.toString(); 3046 } 3047 3048 3064 public static String toString(Object [] a) { 3065 if (a == null) 3066 return "null"; 3067 if (a.length == 0) 3068 return "[]"; 3069 3070 StringBuilder buf = new StringBuilder (); 3071 3072 for (int i = 0; i < a.length; i++) { 3073 if (i == 0) 3074 buf.append('['); 3075 else 3076 buf.append(", "); 3077 3078 buf.append(String.valueOf(a[i])); 3079 } 3080 3081 buf.append("]"); 3082 return buf.toString(); 3083 } 3084 3085 3118 public static String deepToString(Object [] a) { 3119 if (a == null) 3120 return "null"; 3121 3122 int bufLen = 20 * a.length; 3123 if (a.length != 0 && bufLen <= 0) 3124 bufLen = Integer.MAX_VALUE; 3125 StringBuilder buf = new StringBuilder (bufLen); 3126 deepToString(a, buf, new HashSet ()); 3127 return buf.toString(); 3128 } 3129 3130 private static void deepToString(Object [] a, StringBuilder buf, 3131 Set <Object []> dejaVu) { 3132 if (a == null) { 3133 buf.append("null"); 3134 return; 3135 } 3136 dejaVu.add(a); 3137 buf.append('['); 3138 for (int i = 0; i < a.length; i++) { 3139 if (i != 0) 3140 buf.append(", "); 3141 3142 Object element = a[i]; 3143 if (element == null) { 3144 buf.append("null"); 3145 } else { 3146 Class eClass = element.getClass(); 3147 3148 if (eClass.isArray()) { 3149 if (eClass == byte[].class) 3150 buf.append(toString((byte[]) element)); 3151 else if (eClass == short[].class) 3152 buf.append(toString((short[]) element)); 3153 else if (eClass == int[].class) 3154 buf.append(toString((int[]) element)); 3155 else if (eClass == long[].class) 3156 buf.append(toString((long[]) element)); 3157 else if (eClass == char[].class) 3158 buf.append(toString((char[]) element)); 3159 else if (eClass == float[].class) 3160 buf.append(toString((float[]) element)); 3161 else if (eClass == double[].class) 3162 buf.append(toString((double[]) element)); 3163 else if (eClass == boolean[].class) 3164 buf.append(toString((boolean[]) element)); 3165 else { if (dejaVu.contains(element)) 3167 buf.append("[...]"); 3168 else 3169 deepToString((Object [])element, buf, dejaVu); 3170 } 3171 } else { buf.append(element.toString()); 3173 } 3174 } 3175 } 3176 buf.append("]"); 3177 dejaVu.remove(a); 3178 } 3179} 3180 | Popular Tags |