1. /*
  2. * @(#)Arrays.java 1.30 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util;
  8. /**
  9. * This class contains various methods for manipulating arrays (such as
  10. * sorting and searching). It also contains a static factory that allows
  11. * arrays to be viewed as lists.<p>
  12. *
  13. * The documentation for the sorting and searching methods contained in this
  14. * class includes briefs description of the <i>implementations</i>. Such
  15. * descriptions should be regarded as <i>implementation notes</i>, rather than
  16. * parts of the <i>specification</i>. Implementors should feel free to
  17. * substitute other algorithms, so long as the specification itself is adhered
  18. * to. (For example, the algorithm used by <tt>sort(Object[])</tt> does not
  19. * have to be a mergesort, but it does have to be <i>stable</i>.)
  20. *
  21. * @author Josh Bloch
  22. * @version 1.30 11/29/01
  23. * @see Comparable
  24. * @see Comparator
  25. * @since JDK1.2
  26. */
  27. public class Arrays {
  28. // Suppresses default constructor, ensuring non-instantiability.
  29. private Arrays() {
  30. }
  31. // Sorting
  32. /**
  33. * Sorts the specified array of longs into ascending numerical order.
  34. * The sorting algorithm is a tuned quicksort, adapted from Jon
  35. * L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function",
  36. * Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November
  37. * 1993). This algorithm offers n*log(n) performance on many data sets
  38. * that cause other quicksorts to degrade to quadratic performance.
  39. *
  40. * @param a the array to be sorted.
  41. */
  42. public static void sort(long[] a) {
  43. sort1(a, 0, a.length);
  44. }
  45. /**
  46. * Sorts the specified range of the specified array of longs into
  47. * ascending numerical order. The sorting algorithm is a tuned quicksort,
  48. * adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a
  49. * Sort Function", Software-Practice and Experience, Vol. 23(11)
  50. * P. 1249-1265 (November 1993). This algorithm offers n*log(n)
  51. * performance on many data sets that cause other quicksorts to degrade to
  52. * quadratic performance.
  53. *
  54. * @param a the array to be sorted.
  55. * @param fromIndex the index of the first element (inclusive) to be
  56. * sorted.
  57. * @param toIndex the index of the last element (exclusive) to be sorted.
  58. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  59. * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  60. * <tt>toIndex > a.length</tt>
  61. */
  62. public static void sort(long[] a, int fromIndex, int toIndex) {
  63. rangeCheck(a.length, fromIndex, toIndex);
  64. sort1(a, fromIndex, toIndex-fromIndex);
  65. }
  66. /**
  67. * Sorts the specified array of ints into ascending numerical order.
  68. * The sorting algorithm is a tuned quicksort, adapted from Jon
  69. * L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function",
  70. * Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November
  71. * 1993). This algorithm offers n*log(n) performance on many data sets
  72. * that cause other quicksorts to degrade to quadratic performance.
  73. *
  74. * @param a the array to be sorted.
  75. */
  76. public static void sort(int[] a) {
  77. sort1(a, 0, a.length);
  78. }
  79. /**
  80. * Sorts the specified range of the specified array of ints into
  81. * ascending numerical order. The sorting algorithm is a tuned quicksort,
  82. * adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a
  83. * Sort Function", Software-Practice and Experience, Vol. 23(11)
  84. * P. 1249-1265 (November 1993). This algorithm offers n*log(n)
  85. * performance on many data sets that cause other quicksorts to degrade to
  86. * quadratic performance.
  87. *
  88. * @param a the array to be sorted.
  89. * @param fromIndex the index of the first element (inclusive) to be
  90. * sorted.
  91. * @param toIndex the index of the last element (exclusive) to be sorted.
  92. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  93. * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  94. * <tt>toIndex > a.length</tt>
  95. */
  96. public static void sort(int[] a, int fromIndex, int toIndex) {
  97. rangeCheck(a.length, fromIndex, toIndex);
  98. sort1(a, fromIndex, toIndex-fromIndex);
  99. }
  100. /**
  101. * Sorts the specified array of shorts into ascending numerical order.
  102. * The sorting algorithm is a tuned quicksort, adapted from Jon
  103. * L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function",
  104. * Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November
  105. * 1993). This algorithm offers n*log(n) performance on many data sets
  106. * that cause other quicksorts to degrade to quadratic performance.
  107. *
  108. * @param a the array to be sorted.
  109. */
  110. public static void sort(short[] a) {
  111. sort1(a, 0, a.length);
  112. }
  113. /**
  114. * Sorts the specified range of the specified array of shorts into
  115. * ascending numerical order. The sorting algorithm is a tuned quicksort,
  116. * adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a
  117. * Sort Function", Software-Practice and Experience, Vol. 23(11)
  118. * P. 1249-1265 (November 1993). This algorithm offers n*log(n)
  119. * performance on many data sets that cause other quicksorts to degrade to
  120. * quadratic performance.
  121. *
  122. * @param a the array to be sorted.
  123. * @param fromIndex the index of the first element (inclusive) to be
  124. * sorted.
  125. * @param toIndex the index of the last element (exclusive) to be sorted.
  126. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  127. * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  128. * <tt>toIndex > a.length</tt>
  129. */
  130. public static void sort(short[] a, int fromIndex, int toIndex) {
  131. rangeCheck(a.length, fromIndex, toIndex);
  132. sort1(a, fromIndex, toIndex-fromIndex);
  133. }
  134. /**
  135. * Sorts the specified array of chars into ascending numerical order.
  136. * The sorting algorithm is a tuned quicksort, adapted from Jon
  137. * L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function",
  138. * Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November
  139. * 1993). This algorithm offers n*log(n) performance on many data sets
  140. * that cause other quicksorts to degrade to quadratic performance.
  141. *
  142. * @param a the array to be sorted.
  143. */
  144. public static void sort(char[] a) {
  145. sort1(a, 0, a.length);
  146. }
  147. /**
  148. * Sorts the specified range of the specified array of chars into
  149. * ascending numerical order. The sorting algorithm is a tuned quicksort,
  150. * adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a
  151. * Sort Function", Software-Practice and Experience, Vol. 23(11)
  152. * P. 1249-1265 (November 1993). This algorithm offers n*log(n)
  153. * performance on many data sets that cause other quicksorts to degrade to
  154. * quadratic performance.
  155. *
  156. * @param a the array to be sorted.
  157. * @param fromIndex the index of the first element (inclusive) to be
  158. * sorted.
  159. * @param toIndex the index of the last element (exclusive) to be sorted.
  160. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  161. * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  162. * <tt>toIndex > a.length</tt>
  163. */
  164. public static void sort(char[] a, int fromIndex, int toIndex) {
  165. rangeCheck(a.length, fromIndex, toIndex);
  166. sort1(a, fromIndex, toIndex-fromIndex);
  167. }
  168. /**
  169. * Sorts the specified array of bytes into ascending numerical order.
  170. * The sorting algorithm is a tuned quicksort, adapted from Jon
  171. * L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function",
  172. * Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November
  173. * 1993). This algorithm offers n*log(n) performance on many data sets
  174. * that cause other quicksorts to degrade to quadratic performance.
  175. *
  176. * @param a the array to be sorted.
  177. */
  178. public static void sort(byte[] a) {
  179. sort1(a, 0, a.length);
  180. }
  181. /**
  182. * Sorts the specified range of the specified array of bytes into
  183. * ascending numerical order. The sorting algorithm is a tuned quicksort,
  184. * adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a
  185. * Sort Function", Software-Practice and Experience, Vol. 23(11)
  186. * P. 1249-1265 (November 1993). This algorithm offers n*log(n)
  187. * performance on many data sets that cause other quicksorts to degrade to
  188. * quadratic performance.
  189. *
  190. * @param a the array to be sorted.
  191. * @param fromIndex the index of the first element (inclusive) to be
  192. * sorted.
  193. * @param toIndex the index of the last element (exclusive) to be sorted.
  194. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  195. * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  196. * <tt>toIndex > a.length</tt>
  197. */
  198. public static void sort(byte[] a, int fromIndex, int toIndex) {
  199. rangeCheck(a.length, fromIndex, toIndex);
  200. sort1(a, fromIndex, toIndex-fromIndex);
  201. }
  202. /**
  203. * Sorts the specified array of doubles into ascending numerical order.
  204. * The sorting algorithm is a tuned quicksort, adapted from Jon
  205. * L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function",
  206. * Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November
  207. * 1993). This algorithm offers n*log(n) performance on many data sets
  208. * that cause other quicksorts to degrade to quadratic performance.
  209. *
  210. * @param a the array to be sorted.
  211. */
  212. public static void sort(double[] a) {
  213. sort2(a, 0, a.length);
  214. }
  215. /**
  216. * Sorts the specified range of the specified array of doubles into
  217. * ascending numerical order. The sorting algorithm is a tuned quicksort,
  218. * adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a
  219. * Sort Function", Software-Practice and Experience, Vol. 23(11)
  220. * P. 1249-1265 (November 1993). This algorithm offers n*log(n)
  221. * performance on many data sets that cause other quicksorts to degrade to
  222. * quadratic performance.
  223. *
  224. * @param a the array to be sorted.
  225. * @param fromIndex the index of the first element (inclusive) to be
  226. * sorted.
  227. * @param toIndex the index of the last element (exclusive) to be sorted.
  228. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  229. * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  230. * <tt>toIndex > a.length</tt>
  231. */
  232. public static void sort(double[] a, int fromIndex, int toIndex) {
  233. rangeCheck(a.length, fromIndex, toIndex);
  234. sort2(a, fromIndex, toIndex);
  235. }
  236. /**
  237. * Sorts the specified array of floats into ascending numerical order.
  238. * The sorting algorithm is a tuned quicksort, adapted from Jon
  239. * L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function",
  240. * Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November
  241. * 1993). This algorithm offers n*log(n) performance on many data sets
  242. * that cause other quicksorts to degrade to quadratic performance.
  243. *
  244. * @param a the array to be sorted.
  245. */
  246. public static void sort(float[] a) {
  247. sort2(a, 0, a.length);
  248. }
  249. /**
  250. * Sorts the specified range of the specified array of floats into
  251. * ascending numerical order. The sorting algorithm is a tuned quicksort,
  252. * adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a
  253. * Sort Function", Software-Practice and Experience, Vol. 23(11)
  254. * P. 1249-1265 (November 1993). This algorithm offers n*log(n)
  255. * performance on many data sets that cause other quicksorts to degrade to
  256. * quadratic performance.
  257. *
  258. * @param a the array to be sorted.
  259. * @param fromIndex the index of the first element (inclusive) to be
  260. * sorted.
  261. * @param toIndex the index of the last element (exclusive) to be sorted.
  262. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  263. * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  264. * <tt>toIndex > a.length</tt>
  265. */
  266. public static void sort(float[] a, int fromIndex, int toIndex) {
  267. rangeCheck(a.length, fromIndex, toIndex);
  268. sort2(a, fromIndex, toIndex);
  269. }
  270. private static void sort2(double a[], int fromIndex, int toIndex) {
  271. final long NEG_ZERO_BITS = Double.doubleToLongBits(-0.0d);
  272. /*
  273. * The sort is done in three phases to avoid the expense of using
  274. * NaN and -0.0 aware comparisons during the main sort.
  275. */
  276. /*
  277. * Preprocessing phase: Move any NaN's to end of array, count the
  278. * number of -0.0's, and turn them into 0.0's.
  279. */
  280. int numNegZeros = 0;
  281. int i = fromIndex, n = toIndex;
  282. while(i < n) {
  283. if (a[i] != a[i]) {
  284. a[i] = a[--n];
  285. a[n] = Double.NaN;
  286. } else {
  287. if (a[i]==0 && Double.doubleToLongBits(a[i])==NEG_ZERO_BITS) {
  288. a[i] = 0.0d;
  289. numNegZeros++;
  290. }
  291. i++;
  292. }
  293. }
  294. // Main sort phase: quicksort everything but the NaN's
  295. sort1(a, fromIndex, n-fromIndex);
  296. // Postprocessing phase: change 0.0's to -0.0's as required
  297. if (numNegZeros != 0) {
  298. int j = binarySearch(a, 0.0d, fromIndex, n-1); // posn of ANY zero
  299. do {
  300. j--;
  301. } while (j>=0 && a[j]==0.0d);
  302. // j is now one less than the index of the FIRST zero
  303. for (int k=0; k<numNegZeros; k++)
  304. a[++j] = -0.0d;
  305. }
  306. }
  307. private static void sort2(float a[], int fromIndex, int toIndex) {
  308. final int NEG_ZERO_BITS = Float.floatToIntBits(-0.0f);
  309. /*
  310. * The sort is done in three phases to avoid the expense of using
  311. * NaN and -0.0 aware comparisons during the main sort.
  312. */
  313. /*
  314. * Preprocessing phase: Move any NaN's to end of array, count the
  315. * number of -0.0's, and turn them into 0.0's.
  316. */
  317. int numNegZeros = 0;
  318. int i = fromIndex, n = toIndex;
  319. while(i < n) {
  320. if (a[i] != a[i]) {
  321. a[i] = a[--n];
  322. a[n] = Float.NaN;
  323. } else {
  324. if (a[i]==0 && Float.floatToIntBits(a[i])==NEG_ZERO_BITS) {
  325. a[i] = 0.0f;
  326. numNegZeros++;
  327. }
  328. i++;
  329. }
  330. }
  331. // Main sort phase: quicksort everything but the NaN's
  332. sort1(a, fromIndex, n-fromIndex);
  333. // Postprocessing phase: change 0.0's to -0.0's as required
  334. if (numNegZeros != 0) {
  335. int j = binarySearch(a, 0.0f, fromIndex, n-1); // posn of ANY zero
  336. do {
  337. j--;
  338. } while (j>=0 && a[j]==0.0f);
  339. // j is now one less than the index of the FIRST zero
  340. for (int k=0; k<numNegZeros; k++)
  341. a[++j] = -0.0f;
  342. }
  343. }
  344. /*
  345. * The code for each of the seven primitive types is largely identical.
  346. * C'est la vie.
  347. */
  348. /**
  349. * Sorts the specified sub-array of longs into ascending order.
  350. */
  351. private static void sort1(long x[], int off, int len) {
  352. // Insertion sort on smallest arrays
  353. if (len < 7) {
  354. for (int i=off; i<len+off; i++)
  355. for (int j=i; j>off && x[j-1]>x[j]; j--)
  356. swap(x, j, j-1);
  357. return;
  358. }
  359. // Choose a partition element, v
  360. int m = off + len2; // Small arrays, middle element
  361. if (len > 7) {
  362. int l = off;
  363. int n = off + len - 1;
  364. if (len > 40) { // Big arrays, pseudomedian of 9
  365. int s = len8;
  366. l = med3(x, l, l+s, l+2*s);
  367. m = med3(x, m-s, m, m+s);
  368. n = med3(x, n-2*s, n-s, n);
  369. }
  370. m = med3(x, l, m, n); // Mid-size, med of 3
  371. }
  372. long v = x[m];
  373. // Establish Invariant: v* (<v)* (>v)* v*
  374. int a = off, b = a, c = off + len - 1, d = c;
  375. while(true) {
  376. while (b <= c && x[b] <= v) {
  377. if (x[b] == v)
  378. swap(x, a++, b);
  379. b++;
  380. }
  381. while (c >= b && x[c] >= v) {
  382. if (x[c] == v)
  383. swap(x, c, d--);
  384. c--;
  385. }
  386. if (b > c)
  387. break;
  388. swap(x, b++, c--);
  389. }
  390. // Swap partition elements back to middle
  391. int s, n = off + len;
  392. s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s);
  393. s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s);
  394. // Recursively sort non-partition-elements
  395. if ((s = b-a) > 1)
  396. sort1(x, off, s);
  397. if ((s = d-c) > 1)
  398. sort1(x, n-s, s);
  399. }
  400. /**
  401. * Swaps x[a] with x[b].
  402. */
  403. private static void swap(long x[], int a, int b) {
  404. long t = x[a];
  405. x[a] = x[b];
  406. x[b] = t;
  407. }
  408. /**
  409. * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].
  410. */
  411. private static void vecswap(long x[], int a, int b, int n) {
  412. for (int i=0; i<n; i++, a++, b++)
  413. swap(x, a, b);
  414. }
  415. /**
  416. * Returns the index of the median of the three indexed longs.
  417. */
  418. private static int med3(long x[], int a, int b, int c) {
  419. return (x[a] < x[b] ?
  420. (x[b] < x[c] ? b : x[a] < x[c] ? c : a) :
  421. (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
  422. }
  423. /**
  424. * Sorts the specified sub-array of integers into ascending order.
  425. */
  426. private static void sort1(int x[], int off, int len) {
  427. // Insertion sort on smallest arrays
  428. if (len < 7) {
  429. for (int i=off; i<len+off; i++)
  430. for (int j=i; j>off && x[j-1]>x[j]; j--)
  431. swap(x, j, j-1);
  432. return;
  433. }
  434. // Choose a partition element, v
  435. int m = off + len2; // Small arrays, middle element
  436. if (len > 7) {
  437. int l = off;
  438. int n = off + len - 1;
  439. if (len > 40) { // Big arrays, pseudomedian of 9
  440. int s = len8;
  441. l = med3(x, l, l+s, l+2*s);
  442. m = med3(x, m-s, m, m+s);
  443. n = med3(x, n-2*s, n-s, n);
  444. }
  445. m = med3(x, l, m, n); // Mid-size, med of 3
  446. }
  447. int v = x[m];
  448. // Establish Invariant: v* (<v)* (>v)* v*
  449. int a = off, b = a, c = off + len - 1, d = c;
  450. while(true) {
  451. while (b <= c && x[b] <= v) {
  452. if (x[b] == v)
  453. swap(x, a++, b);
  454. b++;
  455. }
  456. while (c >= b && x[c] >= v) {
  457. if (x[c] == v)
  458. swap(x, c, d--);
  459. c--;
  460. }
  461. if (b > c)
  462. break;
  463. swap(x, b++, c--);
  464. }
  465. // Swap partition elements back to middle
  466. int s, n = off + len;
  467. s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s);
  468. s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s);
  469. // Recursively sort non-partition-elements
  470. if ((s = b-a) > 1)
  471. sort1(x, off, s);
  472. if ((s = d-c) > 1)
  473. sort1(x, n-s, s);
  474. }
  475. /**
  476. * Swaps x[a] with x[b].
  477. */
  478. private static void swap(int x[], int a, int b) {
  479. int t = x[a];
  480. x[a] = x[b];
  481. x[b] = t;
  482. }
  483. /**
  484. * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].
  485. */
  486. private static void vecswap(int x[], int a, int b, int n) {
  487. for (int i=0; i<n; i++, a++, b++)
  488. swap(x, a, b);
  489. }
  490. /**
  491. * Returns the index of the median of the three indexed integers.
  492. */
  493. private static int med3(int x[], int a, int b, int c) {
  494. return (x[a] < x[b] ?
  495. (x[b] < x[c] ? b : x[a] < x[c] ? c : a) :
  496. (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
  497. }
  498. /**
  499. * Sorts the specified sub-array of shorts into ascending order.
  500. */
  501. private static void sort1(short x[], int off, int len) {
  502. // Insertion sort on smallest arrays
  503. if (len < 7) {
  504. for (int i=off; i<len+off; i++)
  505. for (int j=i; j>off && x[j-1]>x[j]; j--)
  506. swap(x, j, j-1);
  507. return;
  508. }
  509. // Choose a partition element, v
  510. int m = off + len2; // Small arrays, middle element
  511. if (len > 7) {
  512. int l = off;
  513. int n = off + len - 1;
  514. if (len > 40) { // Big arrays, pseudomedian of 9
  515. int s = len8;
  516. l = med3(x, l, l+s, l+2*s);
  517. m = med3(x, m-s, m, m+s);
  518. n = med3(x, n-2*s, n-s, n);
  519. }
  520. m = med3(x, l, m, n); // Mid-size, med of 3
  521. }
  522. short v = x[m];
  523. // Establish Invariant: v* (<v)* (>v)* v*
  524. int a = off, b = a, c = off + len - 1, d = c;
  525. while(true) {
  526. while (b <= c && x[b] <= v) {
  527. if (x[b] == v)
  528. swap(x, a++, b);
  529. b++;
  530. }
  531. while (c >= b && x[c] >= v) {
  532. if (x[c] == v)
  533. swap(x, c, d--);
  534. c--;
  535. }
  536. if (b > c)
  537. break;
  538. swap(x, b++, c--);
  539. }
  540. // Swap partition elements back to middle
  541. int s, n = off + len;
  542. s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s);
  543. s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s);
  544. // Recursively sort non-partition-elements
  545. if ((s = b-a) > 1)
  546. sort1(x, off, s);
  547. if ((s = d-c) > 1)
  548. sort1(x, n-s, s);
  549. }
  550. /**
  551. * Swaps x[a] with x[b].
  552. */
  553. private static void swap(short x[], int a, int b) {
  554. short t = x[a];
  555. x[a] = x[b];
  556. x[b] = t;
  557. }
  558. /**
  559. * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].
  560. */
  561. private static void vecswap(short x[], int a, int b, int n) {
  562. for (int i=0; i<n; i++, a++, b++)
  563. swap(x, a, b);
  564. }
  565. /**
  566. * Returns the index of the median of the three indexed shorts.
  567. */
  568. private static int med3(short x[], int a, int b, int c) {
  569. return (x[a] < x[b] ?
  570. (x[b] < x[c] ? b : x[a] < x[c] ? c : a) :
  571. (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
  572. }
  573. /**
  574. * Sorts the specified sub-array of chars into ascending order.
  575. */
  576. private static void sort1(char x[], int off, int len) {
  577. // Insertion sort on smallest arrays
  578. if (len < 7) {
  579. for (int i=off; i<len+off; i++)
  580. for (int j=i; j>off && x[j-1]>x[j]; j--)
  581. swap(x, j, j-1);
  582. return;
  583. }
  584. // Choose a partition element, v
  585. int m = off + len2; // Small arrays, middle element
  586. if (len > 7) {
  587. int l = off;
  588. int n = off + len - 1;
  589. if (len > 40) { // Big arrays, pseudomedian of 9
  590. int s = len8;
  591. l = med3(x, l, l+s, l+2*s);
  592. m = med3(x, m-s, m, m+s);
  593. n = med3(x, n-2*s, n-s, n);
  594. }
  595. m = med3(x, l, m, n); // Mid-size, med of 3
  596. }
  597. char v = x[m];
  598. // Establish Invariant: v* (<v)* (>v)* v*
  599. int a = off, b = a, c = off + len - 1, d = c;
  600. while(true) {
  601. while (b <= c && x[b] <= v) {
  602. if (x[b] == v)
  603. swap(x, a++, b);
  604. b++;
  605. }
  606. while (c >= b && x[c] >= v) {
  607. if (x[c] == v)
  608. swap(x, c, d--);
  609. c--;
  610. }
  611. if (b > c)
  612. break;
  613. swap(x, b++, c--);
  614. }
  615. // Swap partition elements back to middle
  616. int s, n = off + len;
  617. s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s);
  618. s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s);
  619. // Recursively sort non-partition-elements
  620. if ((s = b-a) > 1)
  621. sort1(x, off, s);
  622. if ((s = d-c) > 1)
  623. sort1(x, n-s, s);
  624. }
  625. /**
  626. * Swaps x[a] with x[b].
  627. */
  628. private static void swap(char x[], int a, int b) {
  629. char t = x[a];
  630. x[a] = x[b];
  631. x[b] = t;
  632. }
  633. /**
  634. * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].
  635. */
  636. private static void vecswap(char x[], int a, int b, int n) {
  637. for (int i=0; i<n; i++, a++, b++)
  638. swap(x, a, b);
  639. }
  640. /**
  641. * Returns the index of the median of the three indexed chars.
  642. */
  643. private static int med3(char x[], int a, int b, int c) {
  644. return (x[a] < x[b] ?
  645. (x[b] < x[c] ? b : x[a] < x[c] ? c : a) :
  646. (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
  647. }
  648. /**
  649. * Sorts the specified sub-array of bytes into ascending order.
  650. */
  651. private static void sort1(byte x[], int off, int len) {
  652. // Insertion sort on smallest arrays
  653. if (len < 7) {
  654. for (int i=off; i<len+off; i++)
  655. for (int j=i; j>off && x[j-1]>x[j]; j--)
  656. swap(x, j, j-1);
  657. return;
  658. }
  659. // Choose a partition element, v
  660. int m = off + len2; // Small arrays, middle element
  661. if (len > 7) {
  662. int l = off;
  663. int n = off + len - 1;
  664. if (len > 40) { // Big arrays, pseudomedian of 9
  665. int s = len8;
  666. l = med3(x, l, l+s, l+2*s);
  667. m = med3(x, m-s, m, m+s);
  668. n = med3(x, n-2*s, n-s, n);
  669. }
  670. m = med3(x, l, m, n); // Mid-size, med of 3
  671. }
  672. byte v = x[m];
  673. // Establish Invariant: v* (<v)* (>v)* v*
  674. int a = off, b = a, c = off + len - 1, d = c;
  675. while(true) {
  676. while (b <= c && x[b] <= v) {
  677. if (x[b] == v)
  678. swap(x, a++, b);
  679. b++;
  680. }
  681. while (c >= b && x[c] >= v) {
  682. if (x[c] == v)
  683. swap(x, c, d--);
  684. c--;
  685. }
  686. if (b > c)
  687. break;
  688. swap(x, b++, c--);
  689. }
  690. // Swap partition elements back to middle
  691. int s, n = off + len;
  692. s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s);
  693. s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s);
  694. // Recursively sort non-partition-elements
  695. if ((s = b-a) > 1)
  696. sort1(x, off, s);
  697. if ((s = d-c) > 1)
  698. sort1(x, n-s, s);
  699. }
  700. /**
  701. * Swaps x[a] with x[b].
  702. */
  703. private static void swap(byte x[], int a, int b) {
  704. byte t = x[a];
  705. x[a] = x[b];
  706. x[b] = t;
  707. }
  708. /**
  709. * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].
  710. */
  711. private static void vecswap(byte x[], int a, int b, int n) {
  712. for (int i=0; i<n; i++, a++, b++)
  713. swap(x, a, b);
  714. }
  715. /**
  716. * Returns the index of the median of the three indexed bytes.
  717. */
  718. private static int med3(byte x[], int a, int b, int c) {
  719. return (x[a] < x[b] ?
  720. (x[b] < x[c] ? b : x[a] < x[c] ? c : a) :
  721. (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
  722. }
  723. /**
  724. * Sorts the specified sub-array of doubles into ascending order.
  725. */
  726. private static void sort1(double x[], int off, int len) {
  727. // Insertion sort on smallest arrays
  728. if (len < 7) {
  729. for (int i=off; i<len+off; i++)
  730. for (int j=i; j>off && x[j-1]>x[j]; j--)
  731. swap(x, j, j-1);
  732. return;
  733. }
  734. // Choose a partition element, v
  735. int m = off + len2; // Small arrays, middle element
  736. if (len > 7) {
  737. int l = off;
  738. int n = off + len - 1;
  739. if (len > 40) { // Big arrays, pseudomedian of 9
  740. int s = len8;
  741. l = med3(x, l, l+s, l+2*s);
  742. m = med3(x, m-s, m, m+s);
  743. n = med3(x, n-2*s, n-s, n);
  744. }
  745. m = med3(x, l, m, n); // Mid-size, med of 3
  746. }
  747. double v = x[m];
  748. // Establish Invariant: v* (<v)* (>v)* v*
  749. 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. // Swap partition elements back to middle
  766. int s, n = off + len;
  767. s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s);
  768. s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s);
  769. // Recursively sort non-partition-elements
  770. if ((s = b-a) > 1)
  771. sort1(x, off, s);
  772. if ((s = d-c) > 1)
  773. sort1(x, n-s, s);
  774. }
  775. /**
  776. * Swaps x[a] with x[b].
  777. */
  778. private static void swap(double x[], int a, int b) {
  779. double t = x[a];
  780. x[a] = x[b];
  781. x[b] = t;
  782. }
  783. /**
  784. * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].
  785. */
  786. private static void vecswap(double x[], int a, int b, int n) {
  787. for (int i=0; i<n; i++, a++, b++)
  788. swap(x, a, b);
  789. }
  790. /**
  791. * Returns the index of the median of the three indexed doubles.
  792. */
  793. private static int med3(double x[], int a, int b, int c) {
  794. return (x[a] < x[b] ?
  795. (x[b] < x[c] ? b : x[a] < x[c] ? c : a) :
  796. (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
  797. }
  798. /**
  799. * Sorts the specified sub-array of floats into ascending order.
  800. */
  801. private static void sort1(float x[], int off, int len) {
  802. // Insertion sort on smallest arrays
  803. if (len < 7) {
  804. for (int i=off; i<len+off; i++)
  805. for (int j=i; j>off && x[j-1]>x[j]; j--)
  806. swap(x, j, j-1);
  807. return;
  808. }
  809. // Choose a partition element, v
  810. int m = off + len2; // Small arrays, middle element
  811. if (len > 7) {
  812. int l = off;
  813. int n = off + len - 1;
  814. if (len > 40) { // Big arrays, pseudomedian of 9
  815. int s = len8;
  816. l = med3(x, l, l+s, l+2*s);
  817. m = med3(x, m-s, m, m+s);
  818. n = med3(x, n-2*s, n-s, n);
  819. }
  820. m = med3(x, l, m, n); // Mid-size, med of 3
  821. }
  822. float v = x[m];
  823. // Establish Invariant: v* (<v)* (>v)* v*
  824. int a = off, b = a, c = off + len - 1, d = c;
  825. while(true) {
  826. while (b <= c && x[b] <= v) {
  827. if (x[b] == v)
  828. swap(x, a++, b);
  829. b++;
  830. }
  831. while (c >= b && x[c] >= v) {
  832. if (x[c] == v)
  833. swap(x, c, d--);
  834. c--;
  835. }
  836. if (b > c)
  837. break;
  838. swap(x, b++, c--);
  839. }
  840. // Swap partition elements back to middle
  841. int s, n = off + len;
  842. s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s);
  843. s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s);
  844. // Recursively sort non-partition-elements
  845. if ((s = b-a) > 1)
  846. sort1(x, off, s);
  847. if ((s = d-c) > 1)
  848. sort1(x, n-s, s);
  849. }
  850. /**
  851. * Swaps x[a] with x[b].
  852. */
  853. private static void swap(float x[], int a, int b) {
  854. float t = x[a];
  855. x[a] = x[b];
  856. x[b] = t;
  857. }
  858. /**
  859. * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].
  860. */
  861. private static void vecswap(float x[], int a, int b, int n) {
  862. for (int i=0; i<n; i++, a++, b++)
  863. swap(x, a, b);
  864. }
  865. /**
  866. * Returns the index of the median of the three indexed floats.
  867. */
  868. private static int med3(float x[], int a, int b, int c) {
  869. return (x[a] < x[b] ?
  870. (x[b] < x[c] ? b : x[a] < x[c] ? c : a) :
  871. (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
  872. }
  873. /**
  874. * Sorts the specified array of objects into ascending order, according to
  875. * the <i>natural ordering</i> of its elements. All elements in the array
  876. * must implement the <tt>Comparable</tt> interface. Furthermore, all
  877. * elements in the array must be <i>mutually comparable</i> (that is,
  878. * <tt>e1.compareTo(e2)</tt> must not throw a <tt>ClassCastException</tt>
  879. * for any elements <tt>e1</tt> and <tt>e2</tt> in the array).<p>
  880. *
  881. * This sort is guaranteed to be <i>stable</i>: equal elements will
  882. * not be reordered as a result of the sort.<p>
  883. *
  884. * The sorting algorithm is a modified mergesort (in which the merge is
  885. * omitted if the highest element in the low sublist is less than the
  886. * lowest element in the high sublist). This algorithm offers guaranteed
  887. * n*log(n) performance, and can approach linear performance on nearly
  888. * sorted lists.
  889. *
  890. * @param a the array to be sorted.
  891. * @throws ClassCastException if the array contains elements that are not
  892. * <i>mutually comparable</i> (for example, strings and integers).
  893. * @see Comparable
  894. */
  895. public static void sort(Object[] a) {
  896. Object aux[] = (Object[])a.clone();
  897. mergeSort(aux, a, 0, a.length);
  898. }
  899. /**
  900. * Sorts the specified range of the specified array of objects into
  901. * ascending order, according to the <i>natural ordering</i> of its
  902. * elements. All elements in this range must implement the
  903. * <tt>Comparable</tt> interface. Furthermore, all elements in this range
  904. * must be <i>mutually comparable</i> (that is, <tt>e1.compareTo(e2)</tt>
  905. * must not throw a <tt>ClassCastException</tt> for any elements
  906. * <tt>e1</tt> and <tt>e2</tt> in the array).<p>
  907. *
  908. * This sort is guaranteed to be <i>stable</i>: equal elements will
  909. * not be reordered as a result of the sort.<p>
  910. *
  911. * The sorting algorithm is a modified mergesort (in which the merge is
  912. * omitted if the highest element in the low sublist is less than the
  913. * lowest element in the high sublist). This algorithm offers guaranteed
  914. * n*log(n) performance, and can approach linear performance on nearly
  915. * sorted lists.
  916. *
  917. * @param a the array to be sorted.
  918. * @param fromIndex the index of the first element (inclusive) to be
  919. * sorted.
  920. * @param toIndex the index of the last element (exclusive) to be sorted.
  921. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  922. * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  923. * <tt>toIndex > a.length</tt>
  924. * @throws ClassCastException if the array contains elements that are
  925. * not <i>mutually comparable</i> (for example, strings and
  926. * integers).
  927. * @see Comparable
  928. */
  929. public static void sort(Object[] a, int fromIndex, int toIndex) {
  930. rangeCheck(a.length, fromIndex, toIndex);
  931. Object aux[] = (Object[])a.clone(); // Optimization opportunity
  932. mergeSort(aux, a, fromIndex, toIndex);
  933. }
  934. private static void mergeSort(Object src[], Object dest[],
  935. int low, int high) {
  936. int length = high - low;
  937. // Insertion sort on smallest arrays
  938. if (length < 7) {
  939. for (int i=low; i<high; i++)
  940. for (int j=i; j>low &&
  941. ((Comparable)dest[j-1]).compareTo((Comparable)dest[j])>0; j--)
  942. swap(dest, j, j-1);
  943. return;
  944. }
  945. // Recursively sort halves of dest into src
  946. int mid = (low + high)/2;
  947. mergeSort(dest, src, low, mid);
  948. mergeSort(dest, src, mid, high);
  949. // If list is already sorted, just copy from src to dest. This is an
  950. // optimization that results in faster sorts for nearly ordered lists.
  951. if (((Comparable)src[mid-1]).compareTo((Comparable)src[mid]) <= 0) {
  952. System.arraycopy(src, low, dest, low, length);
  953. return;
  954. }
  955. // Merge sorted halves (now in src) into dest
  956. for(int i = low, p = low, q = mid; i < high; i++) {
  957. if (q>=high || p<mid && ((Comparable)src[p]).compareTo(src[q])<=0)
  958. dest[i] = src[p++];
  959. else
  960. dest[i] = src[q++];
  961. }
  962. }
  963. /**
  964. * Swaps x[a] with x[b].
  965. */
  966. private static void swap(Object x[], int a, int b) {
  967. Object t = x[a];
  968. x[a] = x[b];
  969. x[b] = t;
  970. }
  971. /**
  972. * Sorts the specified array of objects according to the order induced by
  973. * the specified comparator. All elements in the array must be
  974. * <i>mutually comparable</i> by the specified comparator (that is,
  975. * <tt>c.compare(e1, e2)</tt> must not throw a <tt>ClassCastException</tt>
  976. * for any elements <tt>e1</tt> and <tt>e2</tt> in the array).<p>
  977. *
  978. * This sort is guaranteed to be <i>stable</i>: equal elements will
  979. * not be reordered as a result of the sort.<p>
  980. *
  981. * The sorting algorithm is a modified mergesort (in which the merge is
  982. * omitted if the highest element in the low sublist is less than the
  983. * lowest element in the high sublist). This algorithm offers guaranteed
  984. * n*log(n) performance, and can approach linear performance on nearly
  985. * sorted lists.
  986. *
  987. * @param a the array to be sorted.
  988. * @param c the comparator to determine the order of the array.
  989. * @throws ClassCastException if the array contains elements that are
  990. * not <i>mutually comparable</i> using the specified comparator.
  991. * @see Comparator
  992. */
  993. public static void sort(Object[] a, Comparator c) {
  994. Object aux[] = (Object[])a.clone();
  995. mergeSort(aux, a, 0, a.length, c);
  996. }
  997. /**
  998. * Sorts the specified range of the specified array of objects according
  999. * to the order induced by the specified comparator. All elements in the
  1000. * range must be <i>mutually comparable</i> by the specified comparator
  1001. * (that is, <tt>c.compare(e1, e2)</tt> must not throw a
  1002. * <tt>ClassCastException</tt> for any elements <tt>e1</tt> and
  1003. * <tt>e2</tt> in the range).<p>
  1004. *
  1005. * This sort is guaranteed to be <i>stable</i>: equal elements will
  1006. * not be reordered as a result of the sort.<p>
  1007. *
  1008. * The sorting algorithm is a modified mergesort (in which the merge is
  1009. * omitted if the highest element in the low sublist is less than the
  1010. * lowest element in the high sublist). This algorithm offers guaranteed
  1011. * n*log(n) performance, and can approach linear performance on nearly
  1012. * sorted lists.
  1013. *
  1014. * @param a the array to be sorted.
  1015. * @param fromIndex the index of the first element (inclusive) to be
  1016. * sorted.
  1017. * @param toIndex the index of the last element (exclusive) to be sorted.
  1018. * @param c the comparator to determine the order of the array.
  1019. * @throws ClassCastException if the array contains elements that are not
  1020. * <i>mutually comparable</i> using the specified comparator.
  1021. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  1022. * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  1023. * <tt>toIndex > a.length</tt>
  1024. * @see Comparator
  1025. */
  1026. public static void sort(Object[] a, int fromIndex, int toIndex,
  1027. Comparator c) {
  1028. rangeCheck(a.length, fromIndex, toIndex);
  1029. Object aux[] = (Object[])a.clone();
  1030. mergeSort(aux, a, fromIndex, toIndex, c);
  1031. }
  1032. private static void mergeSort(Object src[], Object dest[],
  1033. int low, int high, Comparator c) {
  1034. int length = high - low;
  1035. // Insertion sort on smallest arrays
  1036. if (length < 7) {
  1037. for (int i=low; i<high; i++)
  1038. for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--)
  1039. swap(dest, j, j-1);
  1040. return;
  1041. }
  1042. // Recursively sort halves of dest into src
  1043. int mid = (low + high)/2;
  1044. mergeSort(dest, src, low, mid, c);
  1045. mergeSort(dest, src, mid, high, c);
  1046. // If list is already sorted, just copy from src to dest. This is an
  1047. // optimization that results in faster sorts for nearly ordered lists.
  1048. if (c.compare(src[mid-1], src[mid]) <= 0) {
  1049. System.arraycopy(src, low, dest, low, length);
  1050. return;
  1051. }
  1052. // Merge sorted halves (now in src) into dest
  1053. for(int i = low, p = low, q = mid; i < high; i++) {
  1054. if (q>=high || p<mid && c.compare(src[p], src[q]) <= 0)
  1055. dest[i] = src[p++];
  1056. else
  1057. dest[i] = src[q++];
  1058. }
  1059. }
  1060. /**
  1061. * Check that fromIndex and toIndex are in range, and throw an
  1062. * appropriate exception if they aren't.
  1063. */
  1064. private static void rangeCheck(int arrayLen, int fromIndex, int toIndex) {
  1065. if (fromIndex > toIndex)
  1066. throw new IllegalArgumentException("fromIndex(" + fromIndex +
  1067. ") > toIndex(" + toIndex+")");
  1068. if (fromIndex < 0)
  1069. throw new ArrayIndexOutOfBoundsException(fromIndex);
  1070. if (toIndex > arrayLen)
  1071. throw new ArrayIndexOutOfBoundsException(toIndex);
  1072. }
  1073. // Searching
  1074. /**
  1075. * Searches the specified array of longs for the specified value using the
  1076. * binary search algorithm. The array <strong>must</strong> be sorted (as
  1077. * by the <tt>sort</tt> method, above) prior to making this call. If it
  1078. * is not sorted, the results are undefined. If the array contains
  1079. * multiple elements with the specified value, there is no guarantee which
  1080. * one will be found.
  1081. *
  1082. * @param a the array to be searched.
  1083. * @param key the value to be searched for.
  1084. * @return index of the search key, if it is contained in the list;
  1085. * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
  1086. * <i>insertion point</i> is defined as the point at which the
  1087. * key would be inserted into the list: the index of the first
  1088. * element greater than the key, or <tt>list.size()</tt>, if all
  1089. * elements in the list are less than the specified key. Note
  1090. * that this guarantees that the return value will be >= 0 if
  1091. * and only if the key is found.
  1092. * @see #sort(long[])
  1093. */
  1094. public static int binarySearch(long[] a, long key) {
  1095. int low = 0;
  1096. int high = a.length-1;
  1097. while (low <= high) {
  1098. int mid =(low + high)/2;
  1099. long midVal = a[mid];
  1100. if (midVal < key)
  1101. low = mid + 1;
  1102. else if (midVal > key)
  1103. high = mid - 1;
  1104. else
  1105. return mid; // key found
  1106. }
  1107. return -(low + 1); // key not found.
  1108. }
  1109. /**
  1110. * Searches the specified array of ints for the specified value using the
  1111. * binary search algorithm. The array <strong>must</strong> be sorted (as
  1112. * by the <tt>sort</tt> method, above) prior to making this call. If it
  1113. * is not sorted, the results are undefined. If the array contains
  1114. * multiple elements with the specified value, there is no guarantee which
  1115. * one will be found.
  1116. *
  1117. * @param a the array to be searched.
  1118. * @param key the value to be searched for.
  1119. * @return index of the search key, if it is contained in the list;
  1120. * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
  1121. * <i>insertion point</i> is defined as the point at which the
  1122. * key would be inserted into the list: the index of the first
  1123. * element greater than the key, or <tt>list.size()</tt>, if all
  1124. * elements in the list are less than the specified key. Note
  1125. * that this guarantees that the return value will be >= 0 if
  1126. * and only if the key is found.
  1127. * @see #sort(int[])
  1128. */
  1129. public static int binarySearch(int[] a, int key) {
  1130. int low = 0;
  1131. int high = a.length-1;
  1132. while (low <= high) {
  1133. int mid =(low + high)/2;
  1134. int midVal = a[mid];
  1135. if (midVal < key)
  1136. low = mid + 1;
  1137. else if (midVal > key)
  1138. high = mid - 1;
  1139. else
  1140. return mid; // key found
  1141. }
  1142. return -(low + 1); // key not found.
  1143. }
  1144. /**
  1145. * Searches the specified array of shorts for the specified value using
  1146. * the binary search algorithm. The array <strong>must</strong> be sorted
  1147. * (as by the <tt>sort</tt> method, above) prior to making this call. If
  1148. * it is not sorted, the results are undefined. If the array contains
  1149. * multiple elements with the specified value, there is no guarantee which
  1150. * one will be found.
  1151. *
  1152. * @param a the array to be searched.
  1153. * @param key the value to be searched for.
  1154. * @return index of the search key, if it is contained in the list;
  1155. * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
  1156. * <i>insertion point</i> is defined as the point at which the
  1157. * key would be inserted into the list: the index of the first
  1158. * element greater than the key, or <tt>list.size()</tt>, if all
  1159. * elements in the list are less than the specified key. Note
  1160. * that this guarantees that the return value will be >= 0 if
  1161. * and only if the key is found.
  1162. * @see #sort(short[])
  1163. */
  1164. public static int binarySearch(short[] a, short key) {
  1165. int low = 0;
  1166. int high = a.length-1;
  1167. while (low <= high) {
  1168. int mid =(low + high)/2;
  1169. short midVal = a[mid];
  1170. if (midVal < key)
  1171. low = mid + 1;
  1172. else if (midVal > key)
  1173. high = mid - 1;
  1174. else
  1175. return mid; // key found
  1176. }
  1177. return -(low + 1); // key not found.
  1178. }
  1179. /**
  1180. * Searches the specified array of chars for the specified value using the
  1181. * binary search algorithm. The array <strong>must</strong> be sorted (as
  1182. * by the <tt>sort</tt> method, above) prior to making this call. If it
  1183. * is not sorted, the results are undefined. If the array contains
  1184. * multiple elements with the specified value, there is no guarantee which
  1185. * one will be found.
  1186. *
  1187. * @param a the array to be searched.
  1188. * @param key the value to be searched for.
  1189. * @return index of the search key, if it is contained in the list;
  1190. * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
  1191. * <i>insertion point</i> is defined as the point at which the
  1192. * key would be inserted into the list: the index of the first
  1193. * element greater than the key, or <tt>list.size()</tt>, if all
  1194. * elements in the list are less than the specified key. Note
  1195. * that this guarantees that the return value will be >= 0 if
  1196. * and only if the key is found.
  1197. * @see #sort(char[])
  1198. */
  1199. public static int binarySearch(char[] a, char key) {
  1200. int low = 0;
  1201. int high = a.length-1;
  1202. while (low <= high) {
  1203. int mid =(low + high)/2;
  1204. char midVal = a[mid];
  1205. if (midVal < key)
  1206. low = mid + 1;
  1207. else if (midVal > key)
  1208. high = mid - 1;
  1209. else
  1210. return mid; // key found
  1211. }
  1212. return -(low + 1); // key not found.
  1213. }
  1214. /**
  1215. * Searches the specified array of bytes for the specified value using the
  1216. * binary search algorithm. The array <strong>must</strong> be sorted (as
  1217. * by the <tt>sort</tt> method, above) prior to making this call. If it
  1218. * is not sorted, the results are undefined. If the array contains
  1219. * multiple elements with the specified value, there is no guarantee which
  1220. * one will be found.
  1221. *
  1222. * @param a the array to be searched.
  1223. * @param key the value to be searched for.
  1224. * @return index of the search key, if it is contained in the list;
  1225. * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
  1226. * <i>insertion point</i> is defined as the point at which the
  1227. * key would be inserted into the list: the index of the first
  1228. * element greater than the key, or <tt>list.size()</tt>, if all
  1229. * elements in the list are less than the specified key. Note
  1230. * that this guarantees that the return value will be >= 0 if
  1231. * and only if the key is found.
  1232. * @see #sort(byte[])
  1233. */
  1234. public static int binarySearch(byte[] a, byte key) {
  1235. int low = 0;
  1236. int high = a.length-1;
  1237. while (low <= high) {
  1238. int mid =(low + high)/2;
  1239. byte midVal = a[mid];
  1240. if (midVal < key)
  1241. low = mid + 1;
  1242. else if (midVal > key)
  1243. high = mid - 1;
  1244. else
  1245. return mid; // key found
  1246. }
  1247. return -(low + 1); // key not found.
  1248. }
  1249. /**
  1250. * Searches the specified array of doubles for the specified value using
  1251. * the binary search algorithm. The array <strong>must</strong> be sorted
  1252. * (as by the <tt>sort</tt> method, above) prior to making this call. If
  1253. * it is not sorted, the results are undefined. If the array contains
  1254. * multiple elements with the specified value, there is no guarantee which
  1255. * one will be found.
  1256. *
  1257. * @param a the array to be searched.
  1258. * @param key the value to be searched for.
  1259. * @return index of the search key, if it is contained in the list;
  1260. * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
  1261. * <i>insertion point</i> is defined as the point at which the
  1262. * key would be inserted into the list: the index of the first
  1263. * element greater than the key, or <tt>list.size()</tt>, if all
  1264. * elements in the list are less than the specified key. Note
  1265. * that this guarantees that the return value will be >= 0 if
  1266. * and only if the key is found.
  1267. * @see #sort(double[])
  1268. */
  1269. public static int binarySearch(double[] a, double key) {
  1270. return binarySearch(a, key, 0, a.length-1);
  1271. }
  1272. private static int binarySearch(double[] a, double key, int low,int high) {
  1273. while (low <= high) {
  1274. int mid =(low + high)/2;
  1275. double midVal = a[mid];
  1276. int cmp;
  1277. if (midVal < key) {
  1278. cmp = -1; // Neither val is NaN, thisVal is smaller
  1279. } else if (midVal > key) {
  1280. cmp = 1; // Neither val is NaN, thisVal is larger
  1281. } else {
  1282. long midBits = Double.doubleToLongBits(midVal);
  1283. long keyBits = Double.doubleToLongBits(key);
  1284. cmp = (midBits == keyBits ? 0 : // Values are equal
  1285. (midBits < keyBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
  1286. 1)); // (0.0, -0.0) or (NaN, !NaN)
  1287. }
  1288. if (cmp < 0)
  1289. low = mid + 1;
  1290. else if (cmp > 0)
  1291. high = mid - 1;
  1292. else
  1293. return mid; // key found
  1294. }
  1295. return -(low + 1); // key not found.
  1296. }
  1297. /**
  1298. * Searches the specified array of floats for the specified value using
  1299. * the binary search algorithm. The array <strong>must</strong> be sorted
  1300. * (as by the <tt>sort</tt> method, above) prior to making this call. If
  1301. * it is not sorted, the results are undefined. If the array contains
  1302. * multiple elements with the specified value, there is no guarantee which
  1303. * one will be found.
  1304. *
  1305. * @param a the array to be searched.
  1306. * @param key the value to be searched for.
  1307. * @return index of the search key, if it is contained in the list;
  1308. * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
  1309. * <i>insertion point</i> is defined as the point at which the
  1310. * key would be inserted into the list: the index of the first
  1311. * element greater than the key, or <tt>list.size()</tt>, if all
  1312. * elements in the list are less than the specified key. Note
  1313. * that this guarantees that the return value will be >= 0 if
  1314. * and only if the key is found.
  1315. * @see #sort(float[])
  1316. */
  1317. public static int binarySearch(float[] a, float key) {
  1318. return binarySearch(a, key, 0, a.length-1);
  1319. }
  1320. private static int binarySearch(float[] a, float key, int low,int high) {
  1321. while (low <= high) {
  1322. int mid =(low + high)/2;
  1323. float midVal = a[mid];
  1324. int cmp;
  1325. if (midVal < key) {
  1326. cmp = -1; // Neither val is NaN, thisVal is smaller
  1327. } else if (midVal > key) {
  1328. cmp = 1; // Neither val is NaN, thisVal is larger
  1329. } else {
  1330. int midBits = Float.floatToIntBits(midVal);
  1331. int keyBits = Float.floatToIntBits(key);
  1332. cmp = (midBits == keyBits ? 0 : // Values are equal
  1333. (midBits < keyBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
  1334. 1)); // (0.0, -0.0) or (NaN, !NaN)
  1335. }
  1336. if (cmp < 0)
  1337. low = mid + 1;
  1338. else if (cmp > 0)
  1339. high = mid - 1;
  1340. else
  1341. return mid; // key found
  1342. }
  1343. return -(low + 1); // key not found.
  1344. }
  1345. /**
  1346. * Searches the specified array for the specified object using the binary
  1347. * search algorithm. The array must be sorted into ascending order
  1348. * according to the <i>natural ordering</i> of its elements (as by
  1349. * <tt>Sort(Object[]</tt>), above) prior to making this call. If it is
  1350. * not sorted, the results are undefined. If the array contains multiple
  1351. * elements equal to the specified object, there is no guarantee which
  1352. * one will be found.
  1353. *
  1354. * @param a the array to be searched.
  1355. * @param key the value to be searched for.
  1356. * @return index of the search key, if it is contained in the list;
  1357. * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
  1358. * <i>insertion point</i> is defined as the point at which the
  1359. * key would be inserted into the list: the index of the first
  1360. * element greater than the key, or <tt>list.size()</tt>, if all
  1361. * elements in the list are less than the specified key. Note
  1362. * that this guarantees that the return value will be >= 0 if
  1363. * and only if the key is found.
  1364. * @throws ClassCastException if the array contains elements that are not
  1365. * <i>mutually comparable</i> (for example, strings and integers),
  1366. * or the search key in not mutually comparable with the elements
  1367. * of the array.
  1368. * @see Comparable
  1369. * @see #sort(Object[])
  1370. */
  1371. public static int binarySearch(Object[] a, Object key) {
  1372. int low = 0;
  1373. int high = a.length-1;
  1374. while (low <= high) {
  1375. int mid =(low + high)/2;
  1376. Object midVal = a[mid];
  1377. int cmp = ((Comparable)midVal).compareTo(key);
  1378. if (cmp < 0)
  1379. low = mid + 1;
  1380. else if (cmp > 0)
  1381. high = mid - 1;
  1382. else
  1383. return mid; // key found
  1384. }
  1385. return -(low + 1); // key not found.
  1386. }
  1387. /**
  1388. * Searches the specified array for the specified object using the binary
  1389. * search algorithm. The array must be sorted into ascending order
  1390. * according to the specified comparator (as by the <tt>Sort(Object[],
  1391. * Comparator)</tt> method, above), prior to making this call. If it is
  1392. * not sorted, the results are undefined. If the array contains multiple
  1393. * elements equal to the specified object, there is no guarantee which one
  1394. * will be found.
  1395. *
  1396. * @param a the array to be searched.
  1397. * @param key the value to be searched for.
  1398. * @param c the comparator by which the array is ordered.
  1399. * @return index of the search key, if it is contained in the list;
  1400. * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
  1401. * <i>insertion point</i> is defined as the point at which the
  1402. * key would be inserted into the list: the index of the first
  1403. * element greater than the key, or <tt>list.size()</tt>, if all
  1404. * elements in the list are less than the specified key. Note
  1405. * that this guarantees that the return value will be >= 0 if
  1406. * and only if the key is found.
  1407. * @throws ClassCastException if the array contains elements that are not
  1408. * <i>mutually comparable</i> using the specified comparator,
  1409. * or the search key in not mutually comparable with the
  1410. * elements of the array using this comparator.
  1411. * @see Comparable
  1412. * @see #sort(Object[], Comparator)
  1413. */
  1414. public static int binarySearch(Object[] a, Object key, Comparator c) {
  1415. int low = 0;
  1416. int high = a.length-1;
  1417. while (low <= high) {
  1418. int mid =(low + high)/2;
  1419. Object midVal = a[mid];
  1420. int cmp = c.compare(midVal, key);
  1421. if (cmp < 0)
  1422. low = mid + 1;
  1423. else if (cmp > 0)
  1424. high = mid - 1;
  1425. else
  1426. return mid; // key found
  1427. }
  1428. return -(low + 1); // key not found.
  1429. }
  1430. // Equality Testing
  1431. /**
  1432. * Returns <tt>true</tt> if the two specified arrays of longs are
  1433. * <i>equal</i> to one another. Two arrays are considered equal if both
  1434. * arrays contain the same number of elements, and all corresponding pairs
  1435. * of elements in the two arrays are equal. In other words, two arrays
  1436. * are equal if they contain the same elements in the same order. Also,
  1437. * two array references are considered equal if both are <tt>null</tt>.<p>
  1438. *
  1439. * @param a one array to be tested for equality.
  1440. * @param a2 the other array to be tested for equality.
  1441. * @return <tt>true</tt> if the two arrays are equal.
  1442. */
  1443. public static boolean equals(long[] a, long[] a2) {
  1444. if (a==a2)
  1445. return true;
  1446. if (a==null || a2==null)
  1447. return false;
  1448. int length = a.length;
  1449. if (a2.length != length)
  1450. return false;
  1451. for (int i=0; i<length; i++)
  1452. if (a[i] != a2[i])
  1453. return false;
  1454. return true;
  1455. }
  1456. /**
  1457. * Returns <tt>true</tt> if the two specified arrays of ints are
  1458. * <i>equal</i> to one another. Two arrays are considered equal if both
  1459. * arrays contain the same number of elements, and all corresponding pairs
  1460. * of elements in the two arrays are equal. In other words, two arrays
  1461. * are equal if they contain the same elements in the same order. Also,
  1462. * two array references are considered equal if both are <tt>null</tt>.<p>
  1463. *
  1464. * @param a one array to be tested for equality.
  1465. * @param a2 the other array to be tested for equality.
  1466. * @return <tt>true</tt> if the two arrays are equal.
  1467. */
  1468. public static boolean equals(int[] a, int[] a2) {
  1469. if (a==a2)
  1470. return true;
  1471. if (a==null || a2==null)
  1472. return false;
  1473. int length = a.length;
  1474. if (a2.length != length)
  1475. return false;
  1476. for (int i=0; i<length; i++)
  1477. if (a[i] != a2[i])
  1478. return false;
  1479. return true;
  1480. }
  1481. /**
  1482. * Returns <tt>true</tt> if the two specified arrays of shorts are
  1483. * <i>equal</i> to one another. Two arrays are considered equal if both
  1484. * arrays contain the same number of elements, and all corresponding pairs
  1485. * of elements in the two arrays are equal. In other words, two arrays
  1486. * are equal if they contain the same elements in the same order. Also,
  1487. * two array references are considered equal if both are <tt>null</tt>.<p>
  1488. *
  1489. * @param a one array to be tested for equality.
  1490. * @param a2 the other array to be tested for equality.
  1491. * @return <tt>true</tt> if the two arrays are equal.
  1492. */
  1493. public static boolean equals(short[] a, short a2[]) {
  1494. if (a==a2)
  1495. return true;
  1496. if (a==null || a2==null)
  1497. return false;
  1498. int length = a.length;
  1499. if (a2.length != length)
  1500. return false;
  1501. for (int i=0; i<length; i++)
  1502. if (a[i] != a2[i])
  1503. return false;
  1504. return true;
  1505. }
  1506. /**
  1507. * Returns <tt>true</tt> if the two specified arrays of chars are
  1508. * <i>equal</i> to one another. Two arrays are considered equal if both
  1509. * arrays contain the same number of elements, and all corresponding pairs
  1510. * of elements in the two arrays are equal. In other words, two arrays
  1511. * are equal if they contain the same elements in the same order. Also,
  1512. * two array references are considered equal if both are <tt>null</tt>.<p>
  1513. *
  1514. * @param a one array to be tested for equality.
  1515. * @param a2 the other array to be tested for equality.
  1516. * @return <tt>true</tt> if the two arrays are equal.
  1517. */
  1518. public static boolean equals(char[] a, char[] a2) {
  1519. if (a==a2)
  1520. return true;
  1521. if (a==null || a2==null)
  1522. return false;
  1523. int length = a.length;
  1524. if (a2.length != length)
  1525. return false;
  1526. for (int i=0; i<length; i++)
  1527. if (a[i] != a2[i])
  1528. return false;
  1529. return true;
  1530. }
  1531. /**
  1532. * Returns <tt>true</tt> if the two specified arrays of bytes are
  1533. * <i>equal</i> to one another. Two arrays are considered equal if both
  1534. * arrays contain the same number of elements, and all corresponding pairs
  1535. * of elements in the two arrays are equal. In other words, two arrays
  1536. * are equal if they contain the same elements in the same order. Also,
  1537. * two array references are considered equal if both are <tt>null</tt>.<p>
  1538. *
  1539. * @param a one array to be tested for equality.
  1540. * @param a2 the other array to be tested for equality.
  1541. * @return <tt>true</tt> if the two arrays are equal.
  1542. */
  1543. public static boolean equals(byte[] a, byte[] a2) {
  1544. if (a==a2)
  1545. return true;
  1546. if (a==null || a2==null)
  1547. return false;
  1548. int length = a.length;
  1549. if (a2.length != length)
  1550. return false;
  1551. for (int i=0; i<length; i++)
  1552. if (a[i] != a2[i])
  1553. return false;
  1554. return true;
  1555. }
  1556. /**
  1557. * Returns <tt>true</tt> if the two specified arrays of equals are
  1558. * <i>equal</i> to one another. Two arrays are considered equal if both
  1559. * arrays contain the same number of elements, and all corresponding pairs
  1560. * of elements in the two arrays are equal. In other words, two arrays
  1561. * are equal if they contain the same elements in the same order. Also,
  1562. * two array references are considered equal if both are <tt>null</tt>.<p>
  1563. *
  1564. * @param a one array to be tested for equality.
  1565. * @param a2 the other array to be tested for equality.
  1566. * @return <tt>true</tt> if the two arrays are equal.
  1567. */
  1568. public static boolean equals(boolean[] a, boolean[] a2) {
  1569. if (a==a2)
  1570. return true;
  1571. if (a==null || a2==null)
  1572. return false;
  1573. int length = a.length;
  1574. if (a2.length != length)
  1575. return false;
  1576. for (int i=0; i<length; i++)
  1577. if (a[i] != a2[i])
  1578. return false;
  1579. return true;
  1580. }
  1581. /**
  1582. * Returns <tt>true</tt> if the two specified arrays of doubles are
  1583. * <i>equal</i> to one another. Two arrays are considered equal if both
  1584. * arrays contain the same number of elements, and all corresponding pairs
  1585. * of elements in the two arrays are equal. In other words, two arrays
  1586. * are equal if they contain the same elements in the same order. Also,
  1587. * two array references are considered equal if both are <tt>null</tt>.<p>
  1588. *
  1589. * Two doubles <tt>d1</tt> and <tt>d2</tt> are considered equal if:
  1590. * <pre> <tt>new Double(d1).equals(new Double(d2))</tt></pre>
  1591. * (Unlike the <tt>==</tt> operator, this method considers
  1592. * <tt>NaN</tt> equals to itself, and 0.0d unequal to -0.0d.)
  1593. *
  1594. * @param a one array to be tested for equality.
  1595. * @param a2 the other array to be tested for equality.
  1596. * @return <tt>true</tt> if the two arrays are equal.
  1597. * @see Double#equals(Double)
  1598. */
  1599. public static boolean equals(double[] a, double[] a2) {
  1600. if (a==a2)
  1601. return true;
  1602. if (a==null || a2==null)
  1603. return false;
  1604. int length = a.length;
  1605. if (a2.length != length)
  1606. return false;
  1607. for (int i=0; i<length; i++)
  1608. if (Double.doubleToLongBits(a[i])!=Double.doubleToLongBits(a2[i]))
  1609. return false;
  1610. return true;
  1611. }
  1612. /**
  1613. * Returns <tt>true</tt> if the two specified arrays of floats are
  1614. * <i>equal</i> to one another. Two arrays are considered equal if both
  1615. * arrays contain the same number of elements, and all corresponding pairs
  1616. * of elements in the two arrays are equal. In other words, two arrays
  1617. * are equal if they contain the same elements in the same order. Also,
  1618. * two array references are considered equal if both are <tt>null</tt>.<p>
  1619. *
  1620. * Two doubles <tt>d1</tt> and <tt>d2</tt> are considered equal if:
  1621. * <pre> <tt>new Double(d1).equals(new Double(d2))</tt></pre>
  1622. * (Unlike the <tt>==</tt> operator, this method considers
  1623. * <tt>NaN</tt> equals to itself, and 0.0d unequal to -0.0d.)
  1624. *
  1625. * @param a one array to be tested for equality.
  1626. * @param a2 the other array to be tested for equality.
  1627. * @return <tt>true</tt> if the two arrays are equal.
  1628. * @see Double#equals(Double)
  1629. */
  1630. public static boolean equals(float[] a, float[] a2) {
  1631. if (a==a2)
  1632. return true;
  1633. if (a==null || a2==null)
  1634. return false;
  1635. int length = a.length;
  1636. if (a2.length != length)
  1637. return false;
  1638. for (int i=0; i<length; i++)
  1639. if (Float.floatToIntBits(a[i])!=Float.floatToIntBits(a2[i]))
  1640. return false;
  1641. return true;
  1642. }
  1643. /**
  1644. * Returns <tt>true</tt> if the two specified arrays of Objects are
  1645. * <i>equal</i> to one another. The two arrays are considered equal if
  1646. * both arrays contain the same number of elements, and all corresponding
  1647. * pairs of elements in the two arrays are equal. Two objects <tt>e1</tt>
  1648. * and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null
  1649. * : e1.equals(e2))</tt>. In other words, the two arrays are equal if
  1650. * they contain the same elements in the same order. Also, two array
  1651. * references are considered equal if both are <tt>null</tt>.<p>
  1652. *
  1653. * @param a one array to be tested for equality.
  1654. * @param a2 the other array to be tested for equality.
  1655. * @return <tt>true</tt> if the two arrays are equal.
  1656. */
  1657. public static boolean equals(Object[] a, Object[] a2) {
  1658. if (a==a2)
  1659. return true;
  1660. if (a==null || a2==null)
  1661. return false;
  1662. int length = a.length;
  1663. if (a2.length != length)
  1664. return false;
  1665. for (int i=0; i<length; i++) {
  1666. Object o1 = a[i];
  1667. Object o2 = a2[i];
  1668. if (!(o1==null ? o2==null : o1.equals(o2)))
  1669. return false;
  1670. }
  1671. return true;
  1672. }
  1673. // Filling
  1674. /**
  1675. * Assigns the specified long value to each element of the specified array
  1676. * of longs.
  1677. *
  1678. * @param a the array to be filled.
  1679. * @param val the value to be stored in all elements of the array.
  1680. */
  1681. public static void fill(long[] a, long val) {
  1682. fill(a, 0, a.length, val);
  1683. }
  1684. /**
  1685. * Assigns the specified long value to each element of the specified
  1686. * range of the specified array of longs.
  1687. *
  1688. * @param a the array to be filled.
  1689. * @param fromIndex the index of the first element (inclusive) to be
  1690. * filled with the specified value.
  1691. * @param toIndex the index of the last element (exclusive) to be
  1692. * filled with the specified value.
  1693. * @param val the value to be stored in all elements of the array.
  1694. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  1695. * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  1696. * <tt>toIndex > a.length</tt>
  1697. */
  1698. public static void fill(long[] a, int fromIndex, int toIndex, long val) {
  1699. rangeCheck(a.length, fromIndex, toIndex);
  1700. for (int i=fromIndex; i<toIndex; i++)
  1701. a[i] = val;
  1702. }
  1703. /**
  1704. * Assigns the specified int value to each element of the specified array
  1705. * of ints.
  1706. *
  1707. * @param a the array to be filled.
  1708. * @param val the value to be stored in all elements of the array.
  1709. */
  1710. public static void fill(int[] a, int val) {
  1711. fill(a, 0, a.length, val);
  1712. }
  1713. /**
  1714. * Assigns the specified int value to each element of the specified
  1715. * range of the specified array of ints.
  1716. *
  1717. * @param a the array to be filled.
  1718. * @param fromIndex the index of the first element (inclusive) to be
  1719. * filled with the specified value.
  1720. * @param toIndex the index of the last element (exclusive) to be
  1721. * filled with the specified value.
  1722. * @param val the value to be stored in all elements of the array.
  1723. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  1724. * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  1725. * <tt>toIndex > a.length</tt>
  1726. */
  1727. public static void fill(int[] a, int fromIndex, int toIndex, int val) {
  1728. rangeCheck(a.length, fromIndex, toIndex);
  1729. for (int i=fromIndex; i<toIndex; i++)
  1730. a[i] = val;
  1731. }
  1732. /**
  1733. * Assigns the specified short value to each element of the specified array
  1734. * of shorts.
  1735. *
  1736. * @param a the array to be filled.
  1737. * @param val the value to be stored in all elements of the array.
  1738. */
  1739. public static void fill(short[] a, short val) {
  1740. fill(a, 0, a.length, val);
  1741. }
  1742. /**
  1743. * Assigns the specified short value to each element of the specified
  1744. * range of the specified array of shorts.
  1745. *
  1746. * @param a the array to be filled.
  1747. * @param fromIndex the index of the first element (inclusive) to be
  1748. * filled with the specified value.
  1749. * @param toIndex the index of the last element (exclusive) to be
  1750. * filled with the specified value.
  1751. * @param val the value to be stored in all elements of the array.
  1752. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  1753. * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  1754. * <tt>toIndex > a.length</tt>
  1755. */
  1756. public static void fill(short[] a, int fromIndex, int toIndex, short val) {
  1757. rangeCheck(a.length, fromIndex, toIndex);
  1758. for (int i=fromIndex; i<toIndex; i++)
  1759. a[i] = val;
  1760. }
  1761. /**
  1762. * Assigns the specified char value to each element of the specified array
  1763. * of chars.
  1764. *
  1765. * @param a the array to be filled.
  1766. * @param val the value to be stored in all elements of the array.
  1767. */
  1768. public static void fill(char[] a, char val) {
  1769. fill(a, 0, a.length, val);
  1770. }
  1771. /**
  1772. * Assigns the specified char value to each element of the specified
  1773. * range of the specified array of chars.
  1774. *
  1775. * @param a the array to be filled.
  1776. * @param fromIndex the index of the first element (inclusive) to be
  1777. * filled with the specified value.
  1778. * @param toIndex the index of the last element (exclusive) to be
  1779. * filled with the specified value.
  1780. * @param val the value to be stored in all elements of the array.
  1781. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  1782. * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  1783. * <tt>toIndex > a.length</tt>
  1784. */
  1785. public static void fill(char[] a, int fromIndex, int toIndex, char val) {
  1786. rangeCheck(a.length, fromIndex, toIndex);
  1787. for (int i=fromIndex; i<toIndex; i++)
  1788. a[i] = val;
  1789. }
  1790. /**
  1791. * Assigns the specified byte value to each element of the specified array
  1792. * of bytes.
  1793. *
  1794. * @param a the array to be filled.
  1795. * @param val the value to be stored in all elements of the array.
  1796. */
  1797. public static void fill(byte[] a, byte val) {
  1798. fill(a, 0, a.length, val);
  1799. }
  1800. /**
  1801. * Assigns the specified byte value to each element of the specified
  1802. * range of the specified array of bytes.
  1803. *
  1804. * @param a the array to be filled.
  1805. * @param fromIndex the index of the first element (inclusive) to be
  1806. * filled with the specified value.
  1807. * @param toIndex the index of the last element (exclusive) to be
  1808. * filled with the specified value.
  1809. * @param val the value to be stored in all elements of the array.
  1810. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  1811. * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  1812. * <tt>toIndex > a.length</tt>
  1813. */
  1814. public static void fill(byte[] a, int fromIndex, int toIndex, byte val) {
  1815. rangeCheck(a.length, fromIndex, toIndex);
  1816. for (int i=fromIndex; i<toIndex; i++)
  1817. a[i] = val;
  1818. }
  1819. /**
  1820. * Assigns the specified boolean value to each element of the specified
  1821. * array of booleans.
  1822. *
  1823. * @param a the array to be filled.
  1824. * @param val the value to be stored in all elements of the array.
  1825. */
  1826. public static void fill(boolean[] a, boolean val) {
  1827. fill(a, 0, a.length, val);
  1828. }
  1829. /**
  1830. * Assigns the specified boolean value to each element of the specified
  1831. * range of the specified array of booleans.
  1832. *
  1833. * @param a the array to be filled.
  1834. * @param fromIndex the index of the first element (inclusive) to be
  1835. * filled with the specified value.
  1836. * @param toIndex the index of the last element (exclusive) to be
  1837. * filled with the specified value.
  1838. * @param val the value to be stored in all elements of the array.
  1839. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  1840. * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  1841. * <tt>toIndex > a.length</tt>
  1842. */
  1843. public static void fill(boolean[] a, int fromIndex, int toIndex,
  1844. boolean val) {
  1845. rangeCheck(a.length, fromIndex, toIndex);
  1846. for (int i=fromIndex; i<toIndex; i++)
  1847. a[i] = val;
  1848. }
  1849. /**
  1850. * Assigns the specified double value to each element of the specified
  1851. * array of doubles.
  1852. *
  1853. * @param a the array to be filled.
  1854. * @param val the value to be stored in all elements of the array.
  1855. */
  1856. public static void fill(double[] a, double val) {
  1857. fill(a, 0, a.length, val);
  1858. }
  1859. /**
  1860. * Assigns the specified double value to each element of the specified
  1861. * range of the specified array of doubles.
  1862. *
  1863. * @param a the array to be filled.
  1864. * @param fromIndex the index of the first element (inclusive) to be
  1865. * filled with the specified value.
  1866. * @param toIndex the index of the last element (exclusive) to be
  1867. * filled with the specified value.
  1868. * @param val the value to be stored in all elements of the array.
  1869. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  1870. * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  1871. * <tt>toIndex > a.length</tt>
  1872. */
  1873. public static void fill(double[] a, int fromIndex, int toIndex,double val){
  1874. rangeCheck(a.length, fromIndex, toIndex);
  1875. for (int i=fromIndex; i<toIndex; i++)
  1876. a[i] = val;
  1877. }
  1878. /**
  1879. * Assigns the specified float value to each element of the specified array
  1880. * of floats.
  1881. *
  1882. * @param a the array to be filled.
  1883. * @param val the value to be stored in all elements of the array.
  1884. */
  1885. public static void fill(float[] a, float val) {
  1886. fill(a, 0, a.length, val);
  1887. }
  1888. /**
  1889. * Assigns the specified float value to each element of the specified
  1890. * range of the specified array of floats.
  1891. *
  1892. * @param a the array to be filled.
  1893. * @param fromIndex the index of the first element (inclusive) to be
  1894. * filled with the specified value.
  1895. * @param toIndex the index of the last element (exclusive) to be
  1896. * filled with the specified value.
  1897. * @param val the value to be stored in all elements of the array.
  1898. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  1899. * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  1900. * <tt>toIndex > a.length</tt>
  1901. */
  1902. public static void fill(float[] a, int fromIndex, int toIndex, float val) {
  1903. rangeCheck(a.length, fromIndex, toIndex);
  1904. for (int i=fromIndex; i<toIndex; i++)
  1905. a[i] = val;
  1906. }
  1907. /**
  1908. * Assigns the specified Object reference to each element of the specified
  1909. * array of Objects.
  1910. *
  1911. * @param a the array to be filled.
  1912. * @param val the value to be stored in all elements of the array.
  1913. */
  1914. public static void fill(Object[] a, Object val) {
  1915. fill(a, 0, a.length, val);
  1916. }
  1917. /**
  1918. * Assigns the specified Object reference to each element of the specified
  1919. * range of the specified array of Objects.
  1920. *
  1921. * @param a the array to be filled.
  1922. * @param fromIndex the index of the first element (inclusive) to be
  1923. * filled with the specified value.
  1924. * @param toIndex the index of the last element (exclusive) to be
  1925. * filled with the specified value.
  1926. * @param val the value to be stored in all elements of the array.
  1927. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
  1928. * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
  1929. * <tt>toIndex > a.length</tt>
  1930. */
  1931. public static void fill(Object[] a, int fromIndex, int toIndex,Object val){
  1932. rangeCheck(a.length, fromIndex, toIndex);
  1933. for (int i=fromIndex; i<toIndex; i++)
  1934. a[i] = val;
  1935. }
  1936. // Misc
  1937. /**
  1938. * Returns a fixed-size list backed by the specified array. (Changes to
  1939. * the returned list "write through" to the array.) This method acts
  1940. * as bridge between array-based and collection-based APIs, in
  1941. * combination with <tt>Collection.toArray</tt>. The returned list is
  1942. * serializable.
  1943. *
  1944. * @param a the array by which the list will be backed.
  1945. * @return a list view of the specified array.
  1946. * @see Collection#toArray()
  1947. */
  1948. public static List asList(Object[] a) {
  1949. return new ArrayList(a);
  1950. }
  1951. private static class ArrayList extends AbstractList
  1952. implements java.io.Serializable
  1953. {
  1954. private static final long serialVersionUID = -2764017481108945198L;
  1955. private Object[] a;
  1956. ArrayList(Object[] array) {
  1957. a = array;
  1958. }
  1959. public int size() {
  1960. return a.length;
  1961. }
  1962. public Object[] toArray() {
  1963. return (Object[]) a.clone();
  1964. }
  1965. public Object get(int index) {
  1966. return a[index];
  1967. }
  1968. public Object set(int index, Object element) {
  1969. Object oldValue = a[index];
  1970. a[index] = element;
  1971. return oldValue;
  1972. }
  1973. public int indexOf(Object o) {
  1974. if (o==null) {
  1975. for (int i=0; i<a.length; i++)
  1976. if (a[i]==null)
  1977. return i;
  1978. } else {
  1979. for (int i=0; i<a.length; i++)
  1980. if (o.equals(a[i]))
  1981. return i;
  1982. }
  1983. return -1;
  1984. }
  1985. public boolean contains(Object o) {
  1986. return indexOf(o) != -1;
  1987. }
  1988. }
  1989. }