public Vector put(Range holder, Object value) { int pos = VectorUtil.binarySearch(ranges, holder, rangeComparator, true); if (pos == -1) { pos = 0; } Vector output = new Vector(); int posHigh = VectorUtil.binarySearch( ranges, new RangeHolder(holder.getHigh(), holder.getHigh()), rangeComparator, true); Vector purged = new Vector(); boolean positionRemoved = false; for (int i = (pos - 1 >= 0 ? pos - 1 : 0); i < posHigh && i < ranges.size(); i++) { Range clobberMe = (Range) ranges.elementAt(i); if (overlaps(clobberMe, holder)) { if (i == pos - 1) positionRemoved = true; output.addElement(data.remove(clobberMe)); purged.addElement(clobberMe); } } for (int i = 0; i < purged.size(); i++) ranges.removeElement(purged.elementAt(i)); if (positionRemoved) pos = pos - 1; ranges.insertElementAt(holder, pos); data.put(holder, value); return output; }
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 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 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 boolean equals(Range a, Range b) { return a.getLow() == b.getLow() && a.getHigh() == b.getHigh(); }