use of org.opentripplanner.common.DisjointSet in project OpenTripPlanner by opentripplanner.
the class AreaGroup method groupAreas.
public static List<AreaGroup> groupAreas(Map<Area, OSMLevel> areasLevels) {
DisjointSet<Area> groups = new DisjointSet<Area>();
Multimap<OSMNode, Area> areasForNode = LinkedListMultimap.create();
for (Area area : areasLevels.keySet()) {
for (Ring ring : area.outermostRings) {
for (Ring inner : ring.holes) {
for (OSMNode node : inner.nodes) {
areasForNode.put(node, area);
}
}
for (OSMNode node : ring.nodes) {
areasForNode.put(node, area);
}
}
}
// areas that can be joined must share nodes and levels
for (OSMNode osmNode : areasForNode.keySet()) {
for (Area area1 : areasForNode.get(osmNode)) {
OSMLevel level1 = areasLevels.get(area1);
for (Area area2 : areasForNode.get(osmNode)) {
OSMLevel level2 = areasLevels.get(area2);
if ((level1 == null && level2 == null) || (level1 != null && level1.equals(level2))) {
groups.union(area1, area2);
}
}
}
}
List<AreaGroup> out = new ArrayList<AreaGroup>();
for (Set<Area> areaSet : groups.sets()) {
try {
out.add(new AreaGroup(areaSet));
} catch (RingConstructionException e) {
for (Area area : areaSet) {
LOG.debug("Failed to create merged area for " + area + ". This area might not be at fault; it might be one of the other areas in this list.");
out.add(new AreaGroup(Arrays.asList(area)));
}
}
}
return out;
}
use of org.opentripplanner.common.DisjointSet in project OpenTripPlanner by opentripplanner.
the class TestDisjointSet method testRandom.
public void testRandom() {
DisjointSet<Integer> set = new DisjointSet<Integer>();
Random random = new Random(1);
for (int i = 0; i < 150; ++i) {
set.union(Math.abs(random.nextInt() % 700), Math.abs(random.nextInt() % 700));
}
HashMap<Integer, Integer> seen = new HashMap<Integer, Integer>();
int sizeSum = 0;
for (int i = 0; i < 700; ++i) {
int key = set.find(i);
int size = set.size(key);
Integer lastSize = seen.get(key);
assertTrue(lastSize == null || size == lastSize);
if (lastSize == null) {
seen.put(key, size);
sizeSum += size;
}
assertTrue(size >= 1 && size <= 150);
}
assertEquals(700, sizeSum);
}
Aggregations