/** * Returns <tt>true</tt> if the two specified regions of longs are <i>equal</i> to one another. * Two regions are considered equal if both regions contain the same number of elements, and all * corresponding pairs of elements in the two regions are equal. In other words, two regions are * equal if they contain the same elements in the same order. Also, two region references are * considered equal if both are <tt>null</tt>. * * <p> * * @param a one region to be tested for equality. * @param a2 the other region to be tested for equality. * @return <tt>true</tt> if the two regions are equal. */ public static boolean equals(LongRegion a, LongRegion a2) { if (a == a2) return true; if (a == null || a2 == null) return false; long[] arr1 = a.baseArray(); long[] arr2 = a2.baseArray(); if (arr1 == arr2) return true; int length = a.length(); if (a2.length() != length) return false; for (int i = a.offset(), j = a2.offset(); length-- > 0; i++, j++) if (arr1[i] != arr2[i]) return false; return true; }
/** * Searches the specified region of longs for the specified value using the binary search * algorithm. The region <strong>must</strong> be sorted (as by the {@link #sort} method) prior to * making this call. If it is not sorted, the results are undefined. If the region contains * multiple elements with the specified value, there is no guarantee which one will be found. * * @param a the region to be searched. * @param key the value to be searched for. * @return index of the search key, if it is contained in the region; otherwise, * <tt>(-(<i>insertion point</i>) - 1)</tt>. The <i>insertion point</i> is defined as the * point at which the key would be inserted into the region: the index of the first element * greater than the key, or <tt>region.length()</tt>, if all elements in the region are less * than the specified key. Note that this guarantees that the return value will be >= 0 if * and only if the key is found. * @see #sort(LongRegion) */ public static int binarySearch(LongRegion a, long key) { long[] arr = a.baseArray(); int low = a.offset(); int high = a.length() - 1; while (low <= high) { int mid = (low + high) / 2; long midVal = arr[mid]; if (midVal < key) low = mid + 1; else if (midVal > key) high = mid - 1; else return mid; // key found } return -(low + 1); // key not found. }