You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LookupUtils.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.formula.functions;
  16. import org.apache.poi.ss.formula.eval.BlankEval;
  17. import org.apache.poi.ss.formula.eval.BoolEval;
  18. import org.apache.poi.ss.formula.eval.ErrorEval;
  19. import org.apache.poi.ss.formula.eval.EvaluationException;
  20. import org.apache.poi.ss.formula.eval.NumberEval;
  21. import org.apache.poi.ss.formula.eval.NumericValueEval;
  22. import org.apache.poi.ss.formula.eval.OperandResolver;
  23. import org.apache.poi.ss.formula.eval.RefEval;
  24. import org.apache.poi.ss.formula.eval.StringEval;
  25. import org.apache.poi.ss.formula.eval.ValueEval;
  26. import org.apache.poi.ss.formula.TwoDEval;
  27. import java.util.regex.Matcher;
  28. import java.util.regex.Pattern;
  29. /**
  30. * Common functionality used by VLOOKUP, HLOOKUP, LOOKUP and MATCH
  31. *
  32. * @author Josh Micich
  33. * @author Cedric Walter at innoveo.com
  34. */
  35. final class LookupUtils {
  36. /**
  37. * Represents a single row or column within an <tt>AreaEval</tt>.
  38. */
  39. public interface ValueVector {
  40. ValueEval getItem(int index);
  41. int getSize();
  42. }
  43. private static final class RowVector implements ValueVector {
  44. private final TwoDEval _tableArray;
  45. private final int _size;
  46. private final int _rowIndex;
  47. public RowVector(TwoDEval tableArray, int rowIndex) {
  48. _rowIndex = rowIndex;
  49. int lastRowIx = tableArray.getHeight() - 1;
  50. if(rowIndex < 0 || rowIndex > lastRowIx) {
  51. throw new IllegalArgumentException("Specified row index (" + rowIndex
  52. + ") is outside the allowed range (0.." + lastRowIx + ")");
  53. }
  54. _tableArray = tableArray;
  55. _size = tableArray.getWidth();
  56. }
  57. public ValueEval getItem(int index) {
  58. if(index > _size) {
  59. throw new ArrayIndexOutOfBoundsException("Specified index (" + index
  60. + ") is outside the allowed range (0.." + (_size-1) + ")");
  61. }
  62. return _tableArray.getValue(_rowIndex, index);
  63. }
  64. public int getSize() {
  65. return _size;
  66. }
  67. }
  68. private static final class ColumnVector implements ValueVector {
  69. private final TwoDEval _tableArray;
  70. private final int _size;
  71. private final int _columnIndex;
  72. public ColumnVector(TwoDEval tableArray, int columnIndex) {
  73. _columnIndex = columnIndex;
  74. int lastColIx = tableArray.getWidth()-1;
  75. if(columnIndex < 0 || columnIndex > lastColIx) {
  76. throw new IllegalArgumentException("Specified column index (" + columnIndex
  77. + ") is outside the allowed range (0.." + lastColIx + ")");
  78. }
  79. _tableArray = tableArray;
  80. _size = _tableArray.getHeight();
  81. }
  82. public ValueEval getItem(int index) {
  83. if(index > _size) {
  84. throw new ArrayIndexOutOfBoundsException("Specified index (" + index
  85. + ") is outside the allowed range (0.." + (_size-1) + ")");
  86. }
  87. return _tableArray.getValue(index, _columnIndex);
  88. }
  89. public int getSize() {
  90. return _size;
  91. }
  92. }
  93. public static ValueVector createRowVector(TwoDEval tableArray, int relativeRowIndex) {
  94. return new RowVector(tableArray, relativeRowIndex);
  95. }
  96. public static ValueVector createColumnVector(TwoDEval tableArray, int relativeColumnIndex) {
  97. return new ColumnVector(tableArray, relativeColumnIndex);
  98. }
  99. /**
  100. * @return <code>null</code> if the supplied area is neither a single row nor a single colum
  101. */
  102. public static ValueVector createVector(TwoDEval ae) {
  103. if (ae.isColumn()) {
  104. return createColumnVector(ae, 0);
  105. }
  106. if (ae.isRow()) {
  107. return createRowVector(ae, 0);
  108. }
  109. return null;
  110. }
  111. /**
  112. * Enumeration to support <b>4</b> valued comparison results.<p/>
  113. * Excel lookup functions have complex behaviour in the case where the lookup array has mixed
  114. * types, and/or is unordered. Contrary to suggestions in some Excel documentation, there
  115. * does not appear to be a universal ordering across types. The binary search algorithm used
  116. * changes behaviour when the evaluated 'mid' value has a different type to the lookup value.<p/>
  117. *
  118. * A simple int might have done the same job, but there is risk in confusion with the well
  119. * known <tt>Comparable.compareTo()</tt> and <tt>Comparator.compare()</tt> which both use
  120. * a ubiquitous 3 value result encoding.
  121. */
  122. public static final class CompareResult {
  123. private final boolean _isTypeMismatch;
  124. private final boolean _isLessThan;
  125. private final boolean _isEqual;
  126. private final boolean _isGreaterThan;
  127. private CompareResult(boolean isTypeMismatch, int simpleCompareResult) {
  128. if(isTypeMismatch) {
  129. _isTypeMismatch = true;
  130. _isLessThan = false;
  131. _isEqual = false;
  132. _isGreaterThan = false;
  133. } else {
  134. _isTypeMismatch = false;
  135. _isLessThan = simpleCompareResult < 0;
  136. _isEqual = simpleCompareResult == 0;
  137. _isGreaterThan = simpleCompareResult > 0;
  138. }
  139. }
  140. public static final CompareResult TYPE_MISMATCH = new CompareResult(true, 0);
  141. public static final CompareResult LESS_THAN = new CompareResult(false, -1);
  142. public static final CompareResult EQUAL = new CompareResult(false, 0);
  143. public static final CompareResult GREATER_THAN = new CompareResult(false, +1);
  144. public static final CompareResult valueOf(int simpleCompareResult) {
  145. if(simpleCompareResult < 0) {
  146. return LESS_THAN;
  147. }
  148. if(simpleCompareResult > 0) {
  149. return GREATER_THAN;
  150. }
  151. return EQUAL;
  152. }
  153. public static final CompareResult valueOf(boolean matches) {
  154. if(matches) {
  155. return EQUAL ;
  156. }
  157. return LESS_THAN;
  158. }
  159. public boolean isTypeMismatch() {
  160. return _isTypeMismatch;
  161. }
  162. public boolean isLessThan() {
  163. return _isLessThan;
  164. }
  165. public boolean isEqual() {
  166. return _isEqual;
  167. }
  168. public boolean isGreaterThan() {
  169. return _isGreaterThan;
  170. }
  171. public String toString() {
  172. StringBuffer sb = new StringBuffer(64);
  173. sb.append(getClass().getName()).append(" [");
  174. sb.append(formatAsString());
  175. sb.append("]");
  176. return sb.toString();
  177. }
  178. private String formatAsString() {
  179. if(_isTypeMismatch) {
  180. return "TYPE_MISMATCH";
  181. }
  182. if(_isLessThan) {
  183. return "LESS_THAN";
  184. }
  185. if(_isEqual) {
  186. return "EQUAL";
  187. }
  188. if(_isGreaterThan) {
  189. return "GREATER_THAN";
  190. }
  191. // toString must be reliable
  192. return "??error??";
  193. }
  194. }
  195. public interface LookupValueComparer {
  196. /**
  197. * @return one of 4 instances or <tt>CompareResult</tt>: <tt>LESS_THAN</tt>, <tt>EQUAL</tt>,
  198. * <tt>GREATER_THAN</tt> or <tt>TYPE_MISMATCH</tt>
  199. */
  200. CompareResult compareTo(ValueEval other);
  201. }
  202. private static abstract class LookupValueComparerBase implements LookupValueComparer {
  203. private final Class<? extends ValueEval> _targetClass;
  204. protected LookupValueComparerBase(ValueEval targetValue) {
  205. if(targetValue == null) {
  206. throw new RuntimeException("targetValue cannot be null");
  207. }
  208. _targetClass = targetValue.getClass();
  209. }
  210. public final CompareResult compareTo(ValueEval other) {
  211. if (other == null) {
  212. throw new RuntimeException("compare to value cannot be null");
  213. }
  214. if (_targetClass != other.getClass()) {
  215. return CompareResult.TYPE_MISMATCH;
  216. }
  217. return compareSameType(other);
  218. }
  219. public String toString() {
  220. StringBuffer sb = new StringBuffer(64);
  221. sb.append(getClass().getName()).append(" [");
  222. sb.append(getValueAsString());
  223. sb.append("]");
  224. return sb.toString();
  225. }
  226. protected abstract CompareResult compareSameType(ValueEval other);
  227. /** used only for debug purposes */
  228. protected abstract String getValueAsString();
  229. }
  230. private static final class StringLookupComparer extends LookupValueComparerBase {
  231. private String _value;
  232. private final Pattern _wildCardPattern;
  233. private boolean _matchExact;
  234. private boolean _isMatchFunction;
  235. protected StringLookupComparer(StringEval se, boolean matchExact, boolean isMatchFunction) {
  236. super(se);
  237. _value = se.getStringValue();
  238. _wildCardPattern = Countif.StringMatcher.getWildCardPattern(_value);
  239. _matchExact = matchExact;
  240. _isMatchFunction = isMatchFunction;
  241. }
  242. protected CompareResult compareSameType(ValueEval other) {
  243. StringEval se = (StringEval) other;
  244. String stringValue = se.getStringValue();
  245. if (_wildCardPattern != null) {
  246. Matcher matcher = _wildCardPattern.matcher(stringValue);
  247. boolean matches = matcher.matches();
  248. if (_isMatchFunction ||
  249. !_matchExact) {
  250. return CompareResult.valueOf(matches);
  251. }
  252. }
  253. return CompareResult.valueOf(_value.compareToIgnoreCase(stringValue));
  254. }
  255. protected String getValueAsString() {
  256. return _value;
  257. }
  258. }
  259. private static final class NumberLookupComparer extends LookupValueComparerBase {
  260. private double _value;
  261. protected NumberLookupComparer(NumberEval ne) {
  262. super(ne);
  263. _value = ne.getNumberValue();
  264. }
  265. protected CompareResult compareSameType(ValueEval other) {
  266. NumberEval ne = (NumberEval) other;
  267. return CompareResult.valueOf(Double.compare(_value, ne.getNumberValue()));
  268. }
  269. protected String getValueAsString() {
  270. return String.valueOf(_value);
  271. }
  272. }
  273. private static final class BooleanLookupComparer extends LookupValueComparerBase {
  274. private boolean _value;
  275. protected BooleanLookupComparer(BoolEval be) {
  276. super(be);
  277. _value = be.getBooleanValue();
  278. }
  279. protected CompareResult compareSameType(ValueEval other) {
  280. BoolEval be = (BoolEval) other;
  281. boolean otherVal = be.getBooleanValue();
  282. if(_value == otherVal) {
  283. return CompareResult.EQUAL;
  284. }
  285. // TRUE > FALSE
  286. if(_value) {
  287. return CompareResult.GREATER_THAN;
  288. }
  289. return CompareResult.LESS_THAN;
  290. }
  291. protected String getValueAsString() {
  292. return String.valueOf(_value);
  293. }
  294. }
  295. /**
  296. * Processes the third argument to VLOOKUP, or HLOOKUP (<b>col_index_num</b>
  297. * or <b>row_index_num</b> respectively).<br>
  298. * Sample behaviour:
  299. * <table border="0" cellpadding="1" cellspacing="2" summary="Sample behaviour">
  300. * <tr><th>Input&nbsp;&nbsp;&nbsp;Return</th><th>Value&nbsp;&nbsp;</th><th>Thrown Error</th></tr>
  301. * <tr><td>5</td><td>4</td><td>&nbsp;</td></tr>
  302. * <tr><td>2.9</td><td>2</td><td>&nbsp;</td></tr>
  303. * <tr><td>"5"</td><td>4</td><td>&nbsp;</td></tr>
  304. * <tr><td>"2.18e1"</td><td>21</td><td>&nbsp;</td></tr>
  305. * <tr><td>"-$2"</td><td>-3</td><td>*</td></tr>
  306. * <tr><td>FALSE</td><td>-1</td><td>*</td></tr>
  307. * <tr><td>TRUE</td><td>0</td><td>&nbsp;</td></tr>
  308. * <tr><td>"TRUE"</td><td>&nbsp;</td><td>#REF!</td></tr>
  309. * <tr><td>"abc"</td><td>&nbsp;</td><td>#REF!</td></tr>
  310. * <tr><td>""</td><td>&nbsp;</td><td>#REF!</td></tr>
  311. * <tr><td>&lt;blank&gt;</td><td>&nbsp;</td><td>#VALUE!</td></tr>
  312. * </table><br/>
  313. *
  314. * Note - out of range errors (result index too high) are handled by the caller.
  315. * @return column or row index as a zero-based value, never negative.
  316. * @throws EvaluationException when the specified arg cannot be coerced to a non-negative integer
  317. */
  318. public static int resolveRowOrColIndexArg(ValueEval rowColIndexArg, int srcCellRow, int srcCellCol) throws EvaluationException {
  319. if(rowColIndexArg == null) {
  320. throw new IllegalArgumentException("argument must not be null");
  321. }
  322. ValueEval veRowColIndexArg;
  323. try {
  324. veRowColIndexArg = OperandResolver.getSingleValue(rowColIndexArg, srcCellRow, (short)srcCellCol);
  325. } catch (EvaluationException e) {
  326. // All errors get translated to #REF!
  327. throw EvaluationException.invalidRef();
  328. }
  329. int oneBasedIndex;
  330. if(veRowColIndexArg instanceof StringEval) {
  331. StringEval se = (StringEval) veRowColIndexArg;
  332. String strVal = se.getStringValue();
  333. Double dVal = OperandResolver.parseDouble(strVal);
  334. if(dVal == null) {
  335. // String does not resolve to a number. Raise #REF! error.
  336. throw EvaluationException.invalidRef();
  337. // This includes text booleans "TRUE" and "FALSE". They are not valid.
  338. }
  339. // else - numeric value parses OK
  340. }
  341. // actual BoolEval values get interpreted as FALSE->0 and TRUE->1
  342. oneBasedIndex = OperandResolver.coerceValueToInt(veRowColIndexArg);
  343. if (oneBasedIndex < 1) {
  344. // note this is asymmetric with the errors when the index is too large (#REF!)
  345. throw EvaluationException.invalidValue();
  346. }
  347. return oneBasedIndex - 1; // convert to zero based
  348. }
  349. /**
  350. * The second argument (table_array) should be an area ref, but can actually be a cell ref, in
  351. * which case it is interpreted as a 1x1 area ref. Other scalar values cause #VALUE! error.
  352. */
  353. public static TwoDEval resolveTableArrayArg(ValueEval eval) throws EvaluationException {
  354. if (eval instanceof TwoDEval) {
  355. return (TwoDEval) eval;
  356. }
  357. if(eval instanceof RefEval) {
  358. RefEval refEval = (RefEval) eval;
  359. // Make this cell ref look like a 1x1 area ref.
  360. // It doesn't matter if eval is a 2D or 3D ref, because that detail is never asked of AreaEval.
  361. return refEval.offset(0, 0, 0, 0);
  362. }
  363. throw EvaluationException.invalidValue();
  364. }
  365. /**
  366. * Resolves the last (optional) parameter (<b>range_lookup</b>) to the VLOOKUP and HLOOKUP functions.
  367. * @param rangeLookupArg must not be <code>null</code>
  368. */
  369. public static boolean resolveRangeLookupArg(ValueEval rangeLookupArg, int srcCellRow, int srcCellCol) throws EvaluationException {
  370. ValueEval valEval = OperandResolver.getSingleValue(rangeLookupArg, srcCellRow, srcCellCol);
  371. if(valEval instanceof BlankEval) {
  372. // Tricky:
  373. // fourth arg supplied but evaluates to blank
  374. // this does not get the default value
  375. return false;
  376. }
  377. if(valEval instanceof BoolEval) {
  378. // Happy day flow
  379. BoolEval boolEval = (BoolEval) valEval;
  380. return boolEval.getBooleanValue();
  381. }
  382. if (valEval instanceof StringEval) {
  383. String stringValue = ((StringEval) valEval).getStringValue();
  384. if(stringValue.length() < 1) {
  385. // More trickiness:
  386. // Empty string is not the same as BlankEval. It causes #VALUE! error
  387. throw EvaluationException.invalidValue();
  388. }
  389. // TODO move parseBoolean to OperandResolver
  390. Boolean b = Countif.parseBoolean(stringValue);
  391. if(b != null) {
  392. // string converted to boolean OK
  393. return b.booleanValue();
  394. }
  395. // Even more trickiness:
  396. // Note - even if the StringEval represents a number value (for example "1"),
  397. // Excel does not resolve it to a boolean.
  398. throw EvaluationException.invalidValue();
  399. // This is in contrast to the code below,, where NumberEvals values (for
  400. // example 0.01) *do* resolve to equivalent boolean values.
  401. }
  402. if (valEval instanceof NumericValueEval) {
  403. NumericValueEval nve = (NumericValueEval) valEval;
  404. // zero is FALSE, everything else is TRUE
  405. return 0.0 != nve.getNumberValue();
  406. }
  407. throw new RuntimeException("Unexpected eval type (" + valEval.getClass().getName() + ")");
  408. }
  409. public static int lookupIndexOfValue(ValueEval lookupValue, ValueVector vector, boolean isRangeLookup) throws EvaluationException {
  410. LookupValueComparer lookupComparer = createLookupComparer(lookupValue, isRangeLookup, false);
  411. int result;
  412. if(isRangeLookup) {
  413. result = performBinarySearch(vector, lookupComparer);
  414. } else {
  415. result = lookupIndexOfExactValue(lookupComparer, vector);
  416. }
  417. if(result < 0) {
  418. throw new EvaluationException(ErrorEval.NA);
  419. }
  420. return result;
  421. }
  422. /**
  423. * Finds first (lowest index) exact occurrence of specified value.
  424. * @param lookupComparer the value to be found in column or row vector
  425. * @param vector the values to be searched. For VLOOKUP this is the first column of the
  426. * tableArray. For HLOOKUP this is the first row of the tableArray.
  427. * @return zero based index into the vector, -1 if value cannot be found
  428. */
  429. private static int lookupIndexOfExactValue(LookupValueComparer lookupComparer, ValueVector vector) {
  430. // find first occurrence of lookup value
  431. int size = vector.getSize();
  432. for (int i = 0; i < size; i++) {
  433. if(lookupComparer.compareTo(vector.getItem(i)).isEqual()) {
  434. return i;
  435. }
  436. }
  437. return -1;
  438. }
  439. /**
  440. * Encapsulates some standard binary search functionality so the unusual Excel behaviour can
  441. * be clearly distinguished.
  442. */
  443. private static final class BinarySearchIndexes {
  444. private int _lowIx;
  445. private int _highIx;
  446. public BinarySearchIndexes(int highIx) {
  447. _lowIx = -1;
  448. _highIx = highIx;
  449. }
  450. /**
  451. * @return -1 if the search range is empty
  452. */
  453. public int getMidIx() {
  454. int ixDiff = _highIx - _lowIx;
  455. if(ixDiff < 2) {
  456. return -1;
  457. }
  458. return _lowIx + (ixDiff / 2);
  459. }
  460. public int getLowIx() {
  461. return _lowIx;
  462. }
  463. public int getHighIx() {
  464. return _highIx;
  465. }
  466. public void narrowSearch(int midIx, boolean isLessThan) {
  467. if(isLessThan) {
  468. _highIx = midIx;
  469. } else {
  470. _lowIx = midIx;
  471. }
  472. }
  473. }
  474. /**
  475. * Excel has funny behaviour when the some elements in the search vector are the wrong type.
  476. *
  477. */
  478. private static int performBinarySearch(ValueVector vector, LookupValueComparer lookupComparer) {
  479. // both low and high indexes point to values assumed too low and too high.
  480. BinarySearchIndexes bsi = new BinarySearchIndexes(vector.getSize());
  481. while(true) {
  482. int midIx = bsi.getMidIx();
  483. if(midIx < 0) {
  484. return bsi.getLowIx();
  485. }
  486. CompareResult cr = lookupComparer.compareTo(vector.getItem(midIx));
  487. if(cr.isTypeMismatch()) {
  488. int newMidIx = handleMidValueTypeMismatch(lookupComparer, vector, bsi, midIx);
  489. if(newMidIx < 0) {
  490. continue;
  491. }
  492. midIx = newMidIx;
  493. cr = lookupComparer.compareTo(vector.getItem(midIx));
  494. }
  495. if(cr.isEqual()) {
  496. return findLastIndexInRunOfEqualValues(lookupComparer, vector, midIx, bsi.getHighIx());
  497. }
  498. bsi.narrowSearch(midIx, cr.isLessThan());
  499. }
  500. }
  501. /**
  502. * Excel seems to handle mismatched types initially by just stepping 'mid' ix forward to the
  503. * first compatible value.
  504. * @param midIx 'mid' index (value which has the wrong type)
  505. * @return usually -1, signifying that the BinarySearchIndex has been narrowed to the new mid
  506. * index. Zero or greater signifies that an exact match for the lookup value was found
  507. */
  508. private static int handleMidValueTypeMismatch(LookupValueComparer lookupComparer, ValueVector vector,
  509. BinarySearchIndexes bsi, int midIx) {
  510. int newMid = midIx;
  511. int highIx = bsi.getHighIx();
  512. while(true) {
  513. newMid++;
  514. if(newMid == highIx) {
  515. // every element from midIx to highIx was the wrong type
  516. // move highIx down to the low end of the mid values
  517. bsi.narrowSearch(midIx, true);
  518. return -1;
  519. }
  520. CompareResult cr = lookupComparer.compareTo(vector.getItem(newMid));
  521. if(cr.isLessThan() && newMid == highIx-1) {
  522. // move highIx down to the low end of the mid values
  523. bsi.narrowSearch(midIx, true);
  524. return -1;
  525. // but only when "newMid == highIx-1"? slightly weird.
  526. // It would seem more efficient to always do this.
  527. }
  528. if(cr.isTypeMismatch()) {
  529. // keep stepping over values until the right type is found
  530. continue;
  531. }
  532. if(cr.isEqual()) {
  533. return newMid;
  534. }
  535. // Note - if moving highIx down (due to lookup<vector[newMid]),
  536. // this execution path only moves highIx it down as far as newMid, not midIx,
  537. // which would be more efficient.
  538. bsi.narrowSearch(newMid, cr.isLessThan());
  539. return -1;
  540. }
  541. }
  542. /**
  543. * Once the binary search has found a single match, (V/H)LOOKUP steps one by one over subsequent
  544. * values to choose the last matching item.
  545. */
  546. private static int findLastIndexInRunOfEqualValues(LookupValueComparer lookupComparer, ValueVector vector,
  547. int firstFoundIndex, int maxIx) {
  548. for(int i=firstFoundIndex+1; i<maxIx; i++) {
  549. if(!lookupComparer.compareTo(vector.getItem(i)).isEqual()) {
  550. return i-1;
  551. }
  552. }
  553. return maxIx - 1;
  554. }
  555. public static LookupValueComparer createLookupComparer(ValueEval lookupValue, boolean matchExact, boolean isMatchFunction) {
  556. if (lookupValue == BlankEval.instance) {
  557. // blank eval translates to zero
  558. // Note - a blank eval in the lookup column/row never matches anything
  559. // empty string in the lookup column/row can only be matched by explicit empty string
  560. return new NumberLookupComparer(NumberEval.ZERO);
  561. }
  562. if (lookupValue instanceof StringEval) {
  563. //TODO eventually here return a WildcardStringLookupComparer
  564. return new StringLookupComparer((StringEval) lookupValue, matchExact, isMatchFunction);
  565. }
  566. if (lookupValue instanceof NumberEval) {
  567. return new NumberLookupComparer((NumberEval) lookupValue);
  568. }
  569. if (lookupValue instanceof BoolEval) {
  570. return new BooleanLookupComparer((BoolEval) lookupValue);
  571. }
  572. throw new IllegalArgumentException("Bad lookup value type (" + lookupValue.getClass().getName() + ")");
  573. }
  574. }