|
|
@@ -1,143 +1,179 @@
|
|
|
-/*
|
|
|
- * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
- * @copyright Ranides Atterwim
|
|
|
- * @license WTFPL
|
|
|
- * @url http://ranides.net/projects/assira
|
|
|
- */
|
|
|
-package net.ranides.assira.collection.arrays;
|
|
|
-
|
|
|
-import java.util.Comparator;
|
|
|
-
|
|
|
-@SuppressWarnings({
|
|
|
- "PMD.AvoidReassigningParameters"
|
|
|
-})
|
|
|
-public final class ArraySearch {
|
|
|
-
|
|
|
- private ArraySearch() {
|
|
|
- // utility class
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Searches a range of the specified array for the specified value using the
|
|
|
- * binary search algorithm. The range must be sorted prior to making this
|
|
|
- * call. If it is not sorted, the results are undefined. If the range
|
|
|
- * contains multiple elements with the specified value, there is no
|
|
|
- * guarantee which one will be found.
|
|
|
- *
|
|
|
- * @param array the array to be searched.
|
|
|
- * @param begin the index of the first element (inclusive) to be searched.
|
|
|
- * @param end the index of the last element (exclusive) to be searched.
|
|
|
- * @param key the value to be searched for.
|
|
|
- * @return index of the search key, if it is contained in the array;
|
|
|
- * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
|
|
|
- * point</i> is defined as the the point at which the value would be
|
|
|
- * inserted into the array: the index of the first element greater than the
|
|
|
- * key, or the length of the array, if all elements in the array 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 java.util.Arrays
|
|
|
- */
|
|
|
- public static <K extends Comparable<K>> int binarySearch(K[] array, int begin, int end, K key) {
|
|
|
- K midVal;
|
|
|
- end--;
|
|
|
- while (begin <= end) {
|
|
|
- int mid = (begin + end) >>> 1;
|
|
|
- midVal = array[ mid];
|
|
|
- int cmp = midVal.compareTo(key);
|
|
|
- if (cmp < 0) {
|
|
|
- begin = mid + 1;
|
|
|
- } else if (cmp > 0) {
|
|
|
- end = mid - 1;
|
|
|
- } else {
|
|
|
- return mid;
|
|
|
- }
|
|
|
- }
|
|
|
- return -(begin + 1);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Searches an array for the specified value using the binary search
|
|
|
- * algorithm. The range must be sorted prior to making this call. If it is
|
|
|
- * not sorted, the results are undefined. If the range contains multiple
|
|
|
- * elements with the specified value, there is no guarantee which one will
|
|
|
- * be found.
|
|
|
- *
|
|
|
- * @param array the array to be searched.
|
|
|
- * @param key the value to be searched for.
|
|
|
- * @return index of the search key, if it is contained in the array;
|
|
|
- * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
|
|
|
- * point</i> is defined as the the point at which the value would be
|
|
|
- * inserted into the array: the index of the first element greater than the
|
|
|
- * key, or the length of the array, if all elements in the array 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 java.util.Arrays
|
|
|
- */
|
|
|
- public static <K extends Comparable<K>> int binarySearch(K[] array, K key) {
|
|
|
- return binarySearch(array, 0, array.length, key);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Searches a range of the specified array for the specified value using the
|
|
|
- * binary search algorithm and a specified comparator. The range must be
|
|
|
- * sorted following the comparator prior to making this call. If it is not
|
|
|
- * sorted, the results are undefined. If the range contains multiple
|
|
|
- * elements with the specified value, there is no guarantee which one will
|
|
|
- * be found.
|
|
|
- *
|
|
|
- * @param array the array to be searched.
|
|
|
- * @param begin the index of the first element (inclusive) to be searched.
|
|
|
- * @param end the index of the last element (exclusive) to be searched.
|
|
|
- * @param key the value to be searched for.
|
|
|
- * @param comp a comparator.
|
|
|
- * @return index of the search key, if it is contained in the array;
|
|
|
- * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
|
|
|
- * point</i> is defined as the the point at which the value would be
|
|
|
- * inserted into the array: the index of the first element greater than the
|
|
|
- * key, or the length of the array, if all elements in the array 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 java.util.Arrays
|
|
|
- */
|
|
|
- public static <K> int binarySearch(K[] array, int begin, int end, K key, Comparator<K> comp) {
|
|
|
- K midVal;
|
|
|
- end--;
|
|
|
- while (begin <= end) {
|
|
|
- int mid = (begin + end) >>> 1;
|
|
|
- midVal = array[ mid];
|
|
|
- int cmp = comp.compare(midVal, key);
|
|
|
- if (cmp < 0) {
|
|
|
- begin = mid + 1;
|
|
|
- } else if (cmp > 0) {
|
|
|
- end = mid - 1;
|
|
|
- } else {
|
|
|
- return mid; // key found
|
|
|
- }
|
|
|
- }
|
|
|
- return -(begin + 1);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Searches an array for the specified value using the binary search
|
|
|
- * algorithm and a specified comparator. The range must be sorted following
|
|
|
- * the comparator prior to making this call. If it is not sorted, the
|
|
|
- * results are undefined. If the range contains multiple elements with the
|
|
|
- * specified value, there is no guarantee which one will be found.
|
|
|
- *
|
|
|
- * @param array the array to be searched.
|
|
|
- * @param key the value to be searched for.
|
|
|
- * @param cmp a comparator.
|
|
|
- * @return index of the search key, if it is contained in the array;
|
|
|
- * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
|
|
|
- * point</i> is defined as the the point at which the value would be
|
|
|
- * inserted into the array: the index of the first element greater than the
|
|
|
- * key, or the length of the array, if all elements in the array 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 java.util.Arrays
|
|
|
- */
|
|
|
- public static <K> int binarySearch(K[] array, K key, Comparator<K> cmp) {
|
|
|
- return binarySearch(array, 0, array.length, key, cmp);
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
+/*
|
|
|
+ * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
+ * @copyright Ranides Atterwim
|
|
|
+ * @license WTFPL
|
|
|
+ * @url http://ranides.net/projects/assira
|
|
|
+ */
|
|
|
+package net.ranides.assira.collection.arrays;
|
|
|
+
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.List;
|
|
|
+import net.ranides.assira.generic.CompareUtils;
|
|
|
+
|
|
|
+@SuppressWarnings({
|
|
|
+ "PMD.AvoidReassigningParameters"
|
|
|
+})
|
|
|
+public final class ArraySearch {
|
|
|
+
|
|
|
+ private ArraySearch() {
|
|
|
+ // utility class
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Searches an array for the specified value using the binary search
|
|
|
+ * algorithm. The range must be sorted prior to making this call. If it is
|
|
|
+ * not sorted, the results are undefined. If the range contains multiple
|
|
|
+ * elements with the specified value, there is no guarantee which one will
|
|
|
+ * be found.
|
|
|
+ *
|
|
|
+ * @param array the array to be searched.
|
|
|
+ * @param key the value to be searched for.
|
|
|
+ * @return index of the search key, if it is contained in the array;
|
|
|
+ * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
|
|
|
+ * point</i> is defined as the the point at which the value would be
|
|
|
+ * inserted into the array: the index of the first element greater than the
|
|
|
+ * key, or the length of the array, if all elements in the array 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 java.util.Arrays
|
|
|
+ */
|
|
|
+ public static <T extends Comparable<T>> int binarySearch(T[] array, T key) {
|
|
|
+ return binarySearch(array, 0, array.length, key);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Searches a range of the specified array for the specified value using the
|
|
|
+ * binary search algorithm. The range must be sorted prior to making this
|
|
|
+ * call. If it is not sorted, the results are undefined. If the range
|
|
|
+ * contains multiple elements with the specified value, there is no
|
|
|
+ * guarantee which one will be found.
|
|
|
+ *
|
|
|
+ * @param array the array to be searched.
|
|
|
+ * @param begin the index of the first element (inclusive) to be searched.
|
|
|
+ * @param end the index of the last element (exclusive) to be searched.
|
|
|
+ * @param key the value to be searched for.
|
|
|
+ * @return index of the search key, if it is contained in the array;
|
|
|
+ * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
|
|
|
+ * point</i> is defined as the the point at which the value would be
|
|
|
+ * inserted into the array: the index of the first element greater than the
|
|
|
+ * key, or the length of the array, if all elements in the array 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 java.util.Arrays
|
|
|
+ */
|
|
|
+ public static <T extends Comparable<T>> int binarySearch(T[] array, int begin, int end, T key) {
|
|
|
+ T midVal;
|
|
|
+ end--;
|
|
|
+ while (begin <= end) {
|
|
|
+ int mid = (begin + end) >>> 1;
|
|
|
+ midVal = array[ mid];
|
|
|
+ int cmp = midVal.compareTo(key);
|
|
|
+ if (cmp < 0) {
|
|
|
+ begin = mid + 1;
|
|
|
+ } else if (cmp > 0) {
|
|
|
+ end = mid - 1;
|
|
|
+ } else {
|
|
|
+ return mid;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return -(begin + 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Searches an array for the specified value using the binary search
|
|
|
+ * algorithm and a specified comparator. The range must be sorted following
|
|
|
+ * the comparator prior to making this call. If it is not sorted, the
|
|
|
+ * results are undefined. If the range contains multiple elements with the
|
|
|
+ * specified value, there is no guarantee which one will be found.
|
|
|
+ *
|
|
|
+ * @param array the array to be searched.
|
|
|
+ * @param key the value to be searched for.
|
|
|
+ * @param cmp a comparator.
|
|
|
+ * @return index of the search key, if it is contained in the array;
|
|
|
+ * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
|
|
|
+ * point</i> is defined as the the point at which the value would be
|
|
|
+ * inserted into the array: the index of the first element greater than the
|
|
|
+ * key, or the length of the array, if all elements in the array 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 java.util.Arrays
|
|
|
+ */
|
|
|
+ public static <T> int binarySearch(T[] array, T key, Comparator<T> cmp) {
|
|
|
+ return binarySearch(array, 0, array.length, key, cmp);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Searches a range of the specified array for the specified value using the
|
|
|
+ * binary search algorithm and a specified comparator. The range must be
|
|
|
+ * sorted following the comparator prior to making this call. If it is not
|
|
|
+ * sorted, the results are undefined. If the range contains multiple
|
|
|
+ * elements with the specified value, there is no guarantee which one will
|
|
|
+ * be found.
|
|
|
+ *
|
|
|
+ * @param array the array to be searched.
|
|
|
+ * @param begin the index of the first element (inclusive) to be searched.
|
|
|
+ * @param end the index of the last element (exclusive) to be searched.
|
|
|
+ * @param key the value to be searched for.
|
|
|
+ * @param comp a comparator.
|
|
|
+ * @return index of the search key, if it is contained in the array;
|
|
|
+ * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
|
|
|
+ * point</i> is defined as the the point at which the value would be
|
|
|
+ * inserted into the array: the index of the first element greater than the
|
|
|
+ * key, or the length of the array, if all elements in the array 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 java.util.Arrays
|
|
|
+ */
|
|
|
+ public static <T> int binarySearch(T[] array, int begin, int end, T key, Comparator<T> comp) {
|
|
|
+ T midVal;
|
|
|
+ end--;
|
|
|
+ while (begin <= end) {
|
|
|
+ int mid = (begin + end) >>> 1;
|
|
|
+ midVal = array[ mid];
|
|
|
+ int cmp = comp.compare(midVal, key);
|
|
|
+ if (cmp < 0) {
|
|
|
+ begin = mid + 1;
|
|
|
+ } else if (cmp > 0) {
|
|
|
+ end = mid - 1;
|
|
|
+ } else {
|
|
|
+ return mid; // key found
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return -(begin + 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> int binarySearch(Object array, T key) {
|
|
|
+ return binarySearch(array, 0, ArrayUtils.size(array), key);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> int binarySearch(Object array, int begin, int end, T key) {
|
|
|
+ return binarySearchList(ArrayUtils.$wrap(array), begin, end, key, CompareUtils.comparator());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> int binarySearch(Object array, T key, Comparator<T> comp) {
|
|
|
+ return binarySearch(array, 0, ArrayUtils.size(array), key, comp);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> int binarySearch(Object array, int begin, int end, T key, Comparator<T> comp) {
|
|
|
+ return binarySearchList(ArrayUtils.$wrap(array), begin, end, key, comp);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static <T> int binarySearchList(List<T> array, int begin, int end, T key, Comparator<T> comp) {
|
|
|
+ T midVal;
|
|
|
+ end--;
|
|
|
+ while (begin <= end) {
|
|
|
+ int mid = (begin + end) >>> 1;
|
|
|
+ midVal = array.get(mid);
|
|
|
+ int cmp = comp.compare(midVal, key);
|
|
|
+ if (cmp < 0) {
|
|
|
+ begin = mid + 1;
|
|
|
+ } else if (cmp > 0) {
|
|
|
+ end = mid - 1;
|
|
|
+ } else {
|
|
|
+ return mid; // key found
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return -(begin + 1);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|