public int[] getDefinedInterval(int key) { RangeHolder holder = new RangeHolder(key, key); int pos = VectorUtil.binarySearch(ranges, holder, rangeComparator, true) - 1; if (pos < 0 || pos >= ranges.size()) return null; Range r = (Range) ranges.elementAt(pos); if (r.getLow() <= key && r.getHigh() >= key) { int[] out = new int[2]; out[0] = r.getLow(); out[1] = r.getHigh(); return out; } else return null; }
public double[] getInterval(int key) { double[] output = new double[2]; RangeHolder holder = new RangeHolder(key, key + 1); int pos = VectorUtil.binarySearch(ranges, holder, rangeComparator, true) - 1; // System.err.println("ranges = "+ranges); if (ranges.size() == 0) { output[0] = Double.NEGATIVE_INFINITY; output[1] = Double.POSITIVE_INFINITY; return output; } if (pos < 0) { output[0] = Double.NEGATIVE_INFINITY; output[1] = (double) ((Range) ranges.elementAt(0)).getLow() - 1; return output; } if (pos >= ranges.size()) { output[0] = (double) ((Range) ranges.elementAt(ranges.size() - 1)).getHigh() + 1; output[1] = Double.POSITIVE_INFINITY; return output; } Range rh = (Range) ranges.elementAt(pos); if (contains(rh, key)) { output[0] = (double) rh.getLow(); output[1] = (double) rh.getHigh(); } else { output[0] = (double) rh.getHigh() + 1; if (pos < ranges.size() - 1) output[1] = ((Range) ranges.elementAt(pos + 1)).getLow() - 1; else output[1] = Double.POSITIVE_INFINITY; } return output; }
public String toString() { StringBuffer out = new StringBuffer("("); for (int i = 0; i < ranges.size(); i++) { Range r = (Range) ranges.elementAt(i); Object o = data.get(r); if (i > 0) out.append(", "); out.append("{[" + r.getLow() + ", " + r.getHigh() + "] = " + o.toString() + "}"); } out.append(")"); return out.toString(); }
protected static boolean overlaps(Range a, Range b) { return (contains(a, b.getLow()) || contains(a, b.getHigh()) || contains(b, a.getLow()) || contains(b, a.getHigh())); }
protected static boolean contains(Range r, int value) { return value >= r.getLow() && value <= r.getHigh(); }
protected static int compare(Range a, Range b) { if (a.getLow() < b.getLow()) return -1; else if (a.getLow() > b.getLow()) return 1; else return 0; }
protected static boolean equals(Range a, Range b) { return a.getLow() == b.getLow() && a.getHigh() == b.getHigh(); }