public int getRepresentative(int i) { return uf.find(i); }
public int getRepresentative(int i) { return uf.find(i); }
/** * @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); }
private static int countEquivalenceClasses(IntegerUnionFind uf) { HashSet<Integer> s = HashSetFactory.make(); for (int i = 0; i < uf.size(); i++) { s.add(Integer.valueOf(uf.find(i))); } return s.size(); }
/** * 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; }
@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); }