@Test public void simpleLookup() throws Exception { Integer[] breaks = { -10, -5, 0, 5, 10 }; Integer[] values = { -99, -1, 0, 1, 2, 99 }; RangeLookupTable<Integer, Integer> table = createTableFromBreaks(breaks, values); final int N = breaks.length; final int startVal = breaks[0] - 1; final int endVal = breaks[N-1] + 1; int k = 0; int expected = values[0]; LookupItem<Integer, Integer> match; for (int val = startVal; val <= endVal; val++) { if (val >= breaks[k]) { expected = values[k+1]; if (k < N-1) k++ ; } match = table.getLookupItem(val); assertNotNull(match); assertEquals(expected, match.getValue().intValue()); } }
public LookupItem<T, U> getLookupItem(T srcValue) { if (items.isEmpty()) { return null; } else { int lo = 0; int hi = items.size() - 1; while (hi >= lo) { int mid = lo + (hi - lo) / 2; LookupItem<T, U> item = items.get(mid); Range<T> r = item.getRange(); if (r.contains(srcValue)) { return item; } else if (!r.isMinNegInf() && NumberOperations.compare(srcValue, r.getMin()) <= 0) { hi = mid - 1; } else { lo = mid + 1; } } return null; } }
RangeLookupTable { public LookupItem<T, U> getLookupItem(T srcValue) { if (items.isEmpty()) { return null; } else { int lo = 0; int hi = items.size() - 1; while (hi >= lo) { int mid = lo + (hi - lo) / 2; LookupItem<T, U> item = items.get(mid); Range<T> r = item.getRange(); if (r.contains(srcValue)) { return item; } else if (!r.isMinNegInf() && NumberOperations.compare(srcValue, r.getMin()) <= 0) { hi = mid - 1; } else { lo = mid + 1; } } return null; } } }
RangeLookupTable { public LookupItem<T, U> getLookupItem(T srcValue) { if (items.isEmpty()) { return null; } else { int lo = 0; int hi = items.size() - 1; while (hi >= lo) { int mid = lo + (hi - lo) / 2; LookupItem<T, U> item = items.get(mid); Range<T> r = item.getRange(); if (r.contains(srcValue)) { return item; } else if (!r.isMinNegInf() && NumberOperations.compare(srcValue, r.getMin()) <= 0) { hi = mid - 1; } else { lo = mid + 1; } } return null; } } private  RangeLookupTable(Builder builder); }
RangeLookupTable { public LookupItem<T, U> getLookupItem(T srcValue) { if (items.isEmpty()) { return null; } else { int lo = 0; int hi = items.size() - 1; while (hi >= lo) { int mid = lo + (hi - lo) / 2; LookupItem<T, U> item = items.get(mid); Range<T> r = item.getRange(); if (r.contains(srcValue)) { return item; } else if (!r.isMinNegInf() && NumberOperations.compare(srcValue, r.getMin()) <= 0) { hi = mid - 1; } else { lo = mid + 1; } } return null; } } private  RangeLookupTable(Builder builder); LookupItem<T, U> getLookupItem(T srcValue); @Override String toString(); }
RangeLookupTable { public LookupItem<T, U> getLookupItem(T srcValue) { if (items.isEmpty()) { return null; } else { int lo = 0; int hi = items.size() - 1; while (hi >= lo) { int mid = lo + (hi - lo) / 2; LookupItem<T, U> item = items.get(mid); Range<T> r = item.getRange(); if (r.contains(srcValue)) { return item; } else if (!r.isMinNegInf() && NumberOperations.compare(srcValue, r.getMin()) <= 0) { hi = mid - 1; } else { lo = mid + 1; } } return null; } } private  RangeLookupTable(Builder builder); LookupItem<T, U> getLookupItem(T srcValue); @Override String toString();  }