kvr000 commented on code in PR #1270:
URL: https://github.com/apache/commons-lang/pull/1270#discussion_r1742875196
##########
src/main/java/org/apache/commons/lang3/SortedListUtils.java:
##########
@@ -0,0 +1,89 @@
+package org.apache.commons.lang3;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.function.Function;
+
+
+/**
+ * Operations on sorted {@link List}.
+ *
+ * @author Zbynek Vyskovsky
+ */
+public class SortedListUtils {
+ /**
+ * Finds element in sorted list.
+ *
+ * @param a
+ * list sorted by key field
+ * @param key
+ * key to search for
+ * @param keyExtractor
+ * function to extract key from element
+ * @param c
+ * comparator for keys
+ *
+ * @return
+ * index of the search key, if it is contained in the array within
the specified range; otherwise, (-(insertion
+ * point) - 1). The insertion point is defined as the point at which
the key would be inserted into the array:
+ * the index of the first element in the range greater than the key,
or toIndex if all elements in the range
+ * 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.
+ *
+ * @param <T>
+ * type of array element
+ * @param <K>
+ * type of key
+ */
+ public static <K, T> int binarySearch(List<T> a, K key,
+ Function<T, K> keyExtractor,
Comparator<? super K> c) {
+ return binarySearch(a, 0, a.size(), key, keyExtractor, c);
+ }
+
+ /**
+ * Finds element in sorted list, within range fromIndex - toIndex
(inclusive - exclusive).
+ *
+ * @param a
+ * list sorted by key field
+ * @param fromIndex
+ * start index
+ * @param toIndex
+ * end index (exclusive)
+ * @param key
+ * key to search for
+ * @param keyExtractor
+ * function to extract key from element
+ * @param c
+ * comparator for keys
+ *
+ * @return
+ * index of the search key, if it is contained in the array within
the specified range; otherwise, (-(insertion
+ * point) - 1). The insertion point is defined as the point at which
the key would be inserted into the array:
+ * the index of the first element in the range greater than the key,
or toIndex if all elements in the range
+ * 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.
+ *
+ * @param <T>
+ * type of array element
+ * @param <K>
+ * type of key
+ */
+ public static <T, K> int binarySearch(List<T> a, int fromIndex, int
toIndex, K key,
Review Comment:
Well, the `binarySearch()` is so common that everybody writes it the same
way as we learnt in school 😁. I do understand your comment though.
I wrote the code with different names of variables, added explaining
comments and different calculation of not-found result. There is little to do
about the algorithm itself but the same algorithms appear everywhere, including
Wiki, so Oracle definitely cannot claim license on this.
Hope this helps.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]