/** * If a symbolic register resides in a scratch register at an instruction numbered n, then return * the scratch register. Else, return null. */ Register getScratch(Register r, int n) { ArrayList<Interval> v = map.get(r); if (v == null) return null; for (Interval i : v) { if (i.contains(n)) return i.scratch; } return null; }
/** * If a physical register is being used as a scratch register at instruction n, return true; else, * return false; */ boolean isScratch(Register r, int n) { ArrayList<Interval> v = map.get(r); if (v == null) return false; for (final Interval interval : v) { if (interval.contains(n)) return true; } return false; }
public boolean contains(Object value) { if (intervals.contains(value)) { return true; } for (Interval interval : intervals) { if (interval.contains(value)) { return true; } } return false; }
public Partition<T> getAppendPartition(long timestamp) throws JournalException { int sz = partitions.size(); if (sz > 0) { Partition<T> par = partitions.getQuick(sz - 1); Interval interval = par.getInterval(); if (interval == null || interval.contains(timestamp)) { return par.open().access(); } else if (interval.isBefore(timestamp)) { return createPartition(new Interval(timestamp, getMetadata().getPartitionType()), sz); } else { throw new JournalException("%s cannot be appended to %s", Dates.toString(timestamp), this); } } else { return createPartition(new Interval(timestamp, getMetadata().getPartitionType()), 0); } }
// test client public static void main(String[] args) { int N = Integer.parseInt(args[0]); Interval<Integer> a = new Interval<Integer>(5, 17); Interval<Integer> b = new Interval<Integer>(5, 17); Interval<Integer> c = new Interval<Integer>(5, 18); System.out.println(a.equals(b)); System.out.println(!a.equals(c)); System.out.println(!b.equals(c)); // generate N random points in [-1, 2] and compute // fraction that lies in [0, 1] Interval<Double> interval = new Interval<Double>(0.0, 1.0); int cnt = 0; for (int i = 0; i < N; i++) { Double x = 3 * Math.random() - 1.0; if (interval.contains(x)) cnt++; } System.out.println("fraction = " + (1.0 * cnt / N)); }
/** * Returns whether this interval and other overlap. Two intervals overlap if there exists a point * contained in both. * * @param other the interval tested to see if it overlaps with this interval. * @return true if this interval and other overlap. */ public boolean overlaps(Interval other) { if (contains(other.high) || contains(other.low) || other.contains(low)) return true; // it is not necessary to check whether other contains high return false; }
private void denyIntervalContainsAny(Interval interval, int[] expectedValues) { for (int value : expectedValues) { Assert.assertFalse(interval.contains(value)); } }
private void assertIntervalContainsAll(Interval interval, int[] expectedValues) { for (int value : expectedValues) { Assert.assertTrue(interval.contains(value)); } }