This is an automated email from the ASF dual-hosted git repository. aherbert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
The following commit(s) were added to refs/heads/master by this push: new 2c819fa3 Sonar fix: remove code duplication (redundant method) 2c819fa3 is described below commit 2c819fa383e7397f306710251cb91d7a41fa75cb Author: Alex Herbert <aherb...@apache.org> AuthorDate: Mon Jul 1 17:50:44 2024 +0100 Sonar fix: remove code duplication (redundant method) --- .../numbers/arrays/KeyUpdatingInterval.java | 74 ---------------------- .../numbers/arrays/KeyUpdatingIntervalTest.java | 4 -- 2 files changed, 78 deletions(-) diff --git a/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/KeyUpdatingInterval.java b/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/KeyUpdatingInterval.java index 123c4ba4..3ade5cb7 100644 --- a/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/KeyUpdatingInterval.java +++ b/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/KeyUpdatingInterval.java @@ -202,78 +202,4 @@ final class KeyUpdatingInterval implements UpdatingInterval { // r is always moved downward when a middle index value is too high return r; } - - /** - * Search the data for the smallest index {@code i} where {@code a[i]} is - * greater-than-or-equal to the {@code key}; else return {@code right + 1}. - * <pre> - * a[i] >= k : left <= i <= right, or (right + 1) - * </pre> - * - * <p>The data is assumed to be in ascending order, otherwise the behaviour is undefined. - * If the range contains multiple elements with the {@code key} value, the result index - * may be any that match. - * - * <p>This is similar to using {@link java.util.Arrays#binarySearch(int[], int, int, int) - * Arrays.binarySearch}. The method differs in: - * <ul> - * <li>use of an inclusive upper bound; - * <li>returning the closest index with a value above {@code key} if no match was not found; - * <li>performing no range checks: it is assumed {@code left <= right} and they are valid - * indices into the array. - * </ul> - * - * <p>An equivalent use of binary search is: - * <pre>{@code - * int i = Arrays.binarySearch(a, left, right + 1, k); - * if (i < 0) { - * i = ~i; - * } - * }</pre> - * - * <p>This specialisation avoids the caller checking the binary search result for the use - * case when the presence or absence of a key is not important; only that the returned - * index for an absence of a key is the smallest index. When used on unique keys this - * method can be used to update a lower index so all keys are known to be above a key: - * - * <pre>{@code - * int[] keys = ... - * // [i0, i1] contains all keys - * int i0 = 0; - * int i1 = keys.length - 1; - * // Update: [i0, i1] contains all keys >= k - * i0 = searchGreaterOrEqual(keys, i0, i1, k); - * }</pre> - * - * @param a Data. - * @param left Lower bound (inclusive). - * @param right Upper bound (inclusive). - * @param k Key. - * @return largest index {@code i} such that {@code a[i] >= k}, or {@code right + 1} if no - * such index exists - */ - static int searchGreaterOrEqual(int[] a, int left, int right, int k) { - int l = left; - int r = right; - while (l <= r) { - // Middle value - final int m = (l + r) >>> 1; - final int v = a[m]; - // Test: - // l------m------r - // v k update left - // k v update right - if (v < k) { - l = m + 1; - } else if (v > k) { - r = m - 1; - } else { - // Equal - return m; - } - } - // Smallest known value above - // l is always moved upward when a middle index value is too low - return l; - } } diff --git a/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/KeyUpdatingIntervalTest.java b/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/KeyUpdatingIntervalTest.java index 1a7abcef..222bfa88 100644 --- a/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/KeyUpdatingIntervalTest.java +++ b/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/KeyUpdatingIntervalTest.java @@ -38,13 +38,10 @@ class KeyUpdatingIntervalTest { final int k = keys[i]; // Unspecified index when key is present Assertions.assertEquals(k, keys[KeyUpdatingInterval.searchLessOrEqual(keys, l, r, k)], "leq"); - Assertions.assertEquals(k, keys[KeyUpdatingInterval.searchGreaterOrEqual(keys, l, r, k)], "geq"); } // Search above/below keys Assertions.assertEquals(l - 1, KeyUpdatingInterval.searchLessOrEqual(keys, l, r, keys[l] - 44), "leq below"); Assertions.assertEquals(r, KeyUpdatingInterval.searchLessOrEqual(keys, l, r, keys[r] + 44), "leq above"); - Assertions.assertEquals(l, KeyUpdatingInterval.searchGreaterOrEqual(keys, l, r, keys[l] - 44), "geq below"); - Assertions.assertEquals(r + 1, KeyUpdatingInterval.searchGreaterOrEqual(keys, l, r, keys[r] + 44), "geq above"); // Search between neighbour keys for (int i = l + 1; i <= r; i++) { // Bound: keys[i-1] < k < keys[i] @@ -52,7 +49,6 @@ class KeyUpdatingIntervalTest { final int k2 = keys[i]; for (int k = k1 + 1; k < k2; k++) { Assertions.assertEquals(i - 1, KeyUpdatingInterval.searchLessOrEqual(keys, l, r, k), "leq between"); - Assertions.assertEquals(i, KeyUpdatingInterval.searchGreaterOrEqual(keys, l, r, k), "geq between"); } } }