You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2021/12/03 09:34:29 UTC
svn commit: r1895540 - /poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/LookupUtils.java
Author: fanningpj
Date: Fri Dec 3 09:34:29 2021
New Revision: 1895540
URL: http://svn.apache.org/viewvc?rev=1895540&view=rev
Log:
refactor LookupUtils
Modified:
poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/LookupUtils.java
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/LookupUtils.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/LookupUtils.java?rev=1895540&r1=1895539&r2=1895540&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/LookupUtils.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/LookupUtils.java Fri Dec 3 09:34:29 2021
@@ -18,6 +18,7 @@
package org.apache.poi.ss.formula.functions;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -106,9 +107,38 @@ public final class LookupUtils {
public interface ValueVector {
ValueEval getItem(int index);
int getSize();
+ default Iterator<Integer> indexIterator() {
+ return new Iterator<Integer>() {
+ int pos = 0;
+
+ @Override
+ public boolean hasNext() {
+ return pos < getSize();
+ }
+
+ @Override
+ public Integer next() {
+ return pos++;
+ }
+ };
+ }
+ default Iterator<Integer> reverseIndexIterator() {
+ return new Iterator<Integer>() {
+ int pos = getSize() - 1;
+
+ @Override
+ public boolean hasNext() {
+ return pos >= 0;
+ }
+
+ @Override
+ public Integer next() {
+ return pos--;
+ }
+ };
+ }
}
-
private static final class RowVector implements ValueVector {
private final TwoDEval _tableArray;
@@ -134,6 +164,7 @@ public final class LookupUtils {
}
return _tableArray.getValue(_rowIndex, index);
}
+
@Override
public int getSize() {
return _size;
@@ -614,49 +645,7 @@ public final class LookupUtils {
*/
private static int lookupFirstIndexOfValue(LookupValueComparer lookupComparer, ValueVector vector,
MatchMode matchMode) {
-
- // find first occurrence of lookup value
- int size = vector.getSize();
- int bestMatchIdx = -1;
- ValueEval bestMatchEval = null;
- for (int i = 0; i < size; i++) {
- ValueEval valueEval = vector.getItem(i);
- CompareResult result = lookupComparer.compareTo(valueEval);
- if(result.isEqual()) {
- return i;
- }
- switch (matchMode) {
- case ExactMatchFallbackToLargerValue:
- if (result.isLessThan()) {
- if (bestMatchEval == null) {
- bestMatchIdx = i;
- bestMatchEval = valueEval;
- } else {
- LookupValueComparer matchComparer = createTolerantLookupComparer(valueEval, true, true);
- if (matchComparer.compareTo(bestMatchEval).isLessThan()) {
- bestMatchIdx = i;
- bestMatchEval = valueEval;
- }
- }
- }
- break;
- case ExactMatchFallbackToSmallerValue:
- if (result.isGreaterThan()) {
- if (bestMatchEval == null) {
- bestMatchIdx = i;
- bestMatchEval = valueEval;
- } else {
- LookupValueComparer matchComparer = createTolerantLookupComparer(valueEval, true, true);
- if (matchComparer.compareTo(bestMatchEval).isGreaterThan()) {
- bestMatchIdx = i;
- bestMatchEval = valueEval;
- }
- }
- }
- break;
- }
- }
- return bestMatchIdx;
+ return lookupIndexOfValue(lookupComparer, vector, matchMode, false);
}
/**
@@ -669,12 +658,16 @@ public final class LookupUtils {
*/
private static int lookupLastIndexOfValue(LookupValueComparer lookupComparer, ValueVector vector,
MatchMode matchMode) {
+ return lookupIndexOfValue(lookupComparer, vector, matchMode, true);
+ }
- // find last occurrence of lookup value
- int size = vector.getSize();
+ private static int lookupIndexOfValue(LookupValueComparer lookupComparer, ValueVector vector,
+ MatchMode matchMode, boolean reverse) {
int bestMatchIdx = -1;
ValueEval bestMatchEval = null;
- for (int i = size - 1; i >= 0; i--) {
+ Iterator<Integer> idxIter = reverse ? vector.reverseIndexIterator() : vector.indexIterator();
+ while (idxIter.hasNext()) {
+ int i = idxIter.next();
ValueEval valueEval = vector.getItem(i);
CompareResult result = lookupComparer.compareTo(valueEval);
if (result.isEqual()) {
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org