public int getRepresentative(int i) { return uf.find(i); }
throw new IllegalArgumentException("y is too big: " + y); if (x >= size() || y >= size()) { grow(2 * Math.max(x, y)); x = findInternal(x); y = findInternal(y); if (x != y) {
/** * @return representative of x's equivalence class */ public int find(int x) { if (x < 0) { throw new IllegalArgumentException("illegal x " + x); } if (x >= size()) { return x; } // shift by one to support sets including 0 return findInternal(x + 1) - 1; }
@Test public void testUnionFind() { int SIZE = 10000; IntegerUnionFind uf = new IntegerUnionFind(SIZE); int count = countEquivalenceClasses(uf); Assert.assertTrue("Got count " + count, count == SIZE); uf.union(3, 7); Assert.assertTrue(uf.find(3) == uf.find(7)); Assert.assertTrue("Got uf.find(3)=" + uf.find(3), uf.find(3) == 3 || uf.find(3) == 7); uf.union(7, SIZE - 1); Assert.assertTrue(uf.find(3) == uf.find(SIZE - 1)); Assert.assertTrue("Got uf.find(3)=" + uf.find(3), uf.find(3) == 3 || uf.find(3) == 7 || uf.find(3) == SIZE - 1); for (int i = 0; i < SIZE - 1; i++) { uf.union(i, i + 1); } count = countEquivalenceClasses(uf); Assert.assertTrue("Got count " + count, count == 1); uf = new IntegerUnionFind(SIZE); for (int i = 0; i < SIZE; i++) { if ((i % 2) == 0) { uf.union(i, 0); } else { uf.union(i, 1); } } count = countEquivalenceClasses(uf); Assert.assertTrue("Got count " + count, count == 2); }
int repI = uf.find(i); int repJ = uf.find(j); if (repI != repJ) { PointsToSetVariable pi = (PointsToSetVariable) pointsToSets.get(repI); throw new IllegalArgumentException("No PointsToSetVariable for j: " + j); uf.union(repI, repJ); int rep = uf.find(repI); PointsToSetVariable p = (PointsToSetVariable) pointsToSets.get(rep); if (pi.getValue() != null) {
/** * @return representative of x's equivalence class */ public int find(int x) { if (x < 0) { throw new IllegalArgumentException("illegal x " + x); } if (x >= size()) { return x; } // shift by one to support sets including 0 return findInternal(x + 1) - 1; }
int repI = uf.find(i); int repJ = uf.find(j); if (repI != repJ) { PointsToSetVariable pi = (PointsToSetVariable) pointsToSets.get(repI); throw new IllegalArgumentException("No PointsToSetVariable for j: " + j); uf.union(repI, repJ); int rep = uf.find(repI); PointsToSetVariable p = (PointsToSetVariable) pointsToSets.get(rep); if (pi.getValue() != null) {
throw new IllegalArgumentException("y is too big: " + y); if (x >= size() || y >= size()) { grow(2 * Math.max(x, y)); x = findInternal(x); y = findInternal(y); if (x != y) {
/** * @return representative of x's equivalence class */ public int find(int x) { if (x < 0) { throw new IllegalArgumentException("illegal x " + x); } if (x >= size()) { return x; } // shift by one to support sets including 0 return findInternal(x + 1) - 1; }
public int getRepresentative(int i) { return uf.find(i); }
throw new IllegalArgumentException("y is too big: " + y); if (x >= size() || y >= size()) { grow(2 * Math.max(x, y)); x = findInternal(x); y = findInternal(y); if (x != y) {
/** * @return the {@link PointsToSetVariable} recorded for a particular id */ public PointsToSetVariable getPointsToSet(int id) { int repI = uf.find(id); return (PointsToSetVariable) pointsToSets.get(repI); }
/** * @return the {@link PointsToSetVariable} recorded for a particular id */ public PointsToSetVariable getPointsToSet(int id) { int repI = uf.find(id); return (PointsToSetVariable) pointsToSets.get(repI); }
/** * If p is unified, returns the representative for p. */ public PointsToSetVariable getPointsToSet(PointerKey p) { if (p == null) { throw new IllegalArgumentException("null p"); } if (isImplicit(p)) { throw new IllegalArgumentException("unexpected: shouldn't ask a PointsToMap for an implicit points-to-set: " + p); } int i = pointerKeys.getMappedIndex(p); if (i == -1) { return null; } int repI = uf.find(i); PointsToSetVariable result = (PointsToSetVariable) pointsToSets.get(repI); if (result != null && p instanceof FilteredPointerKey && (!(result.getPointerKey() instanceof FilteredPointerKey))) { upgradeToFilter(result, ((FilteredPointerKey) p).getTypeFilter()); } return result; }
/** * If p is unified, returns the representative for p. */ public PointsToSetVariable getPointsToSet(PointerKey p) { if (p == null) { throw new IllegalArgumentException("null p"); } if (isImplicit(p)) { throw new IllegalArgumentException("unexpected: shouldn't ask a PointsToMap for an implicit points-to-set: " + p); } int i = pointerKeys.getMappedIndex(p); if (i == -1) { return null; } int repI = uf.find(i); PointsToSetVariable result = (PointsToSetVariable) pointsToSets.get(repI); if (result != null && p instanceof FilteredPointerKey && (!(result.getPointerKey() instanceof FilteredPointerKey))) { upgradeToFilter(result, ((FilteredPointerKey) p).getTypeFilter()); } return result; }