use of org.jgrapht.graph.DefaultEdge in project jop by jop-devel.
the class TypeGraph method getJoinNodes.
/** Compute the set of join nodes<br/>
*
* See <quote>Vitek, J., Horspool, R. N., and Krall, A. 1997. Efficient type inclusion tests.
* SIGPLAN Not. 32, 10 (Oct. 1997), 142-157.</quote>
* @return a map from types to join nodes
*/
public Set<ClassInfo> getJoinNodes() {
Set<ClassInfo> joinNodes = new HashSet<ClassInfo>();
Set<ClassInfo> miNodes = new HashSet<ClassInfo>();
for (ClassInfo v : bottomUpTraversal()) {
boolean hasJoinSub = false;
for (DefaultEdge descEdge : outgoingEdgesOf(v)) {
if (miNodes.contains(getEdgeTarget(descEdge))) {
hasJoinSub = true;
}
}
boolean isMiNode = hasJoinSub;
if (inDegreeOf(v) > 1 && !hasJoinSub) {
joinNodes.add(v);
isMiNode = true;
}
if (isMiNode) {
miNodes.add(v);
}
}
return joinNodes;
}
use of org.jgrapht.graph.DefaultEdge in project jop by jop-devel.
the class TypeGraph method computeInterfaceBuckets.
/**
* Bucket assignment for interfaces.
* Two interfaces may not share a bucket if they have a common subtype.
* Uses a simple highest-degree first graph coloring heuristic
* @param maxBucketEntries
* @return
*/
public Map<ClassInfo, Integer> computeInterfaceBuckets(int maxBucketEntries) {
Map<ClassInfo, Integer> bucketMap = new HashMap<ClassInfo, Integer>();
SimpleGraph<ClassInfo, DefaultEdge> conflictGraph = getInterfaceConflictGraph();
List<ClassInfo> ifaces = new LinkedList<ClassInfo>();
for (ClassInfo v : vertexSet()) {
if (!v.isInterface())
continue;
ifaces.add(v);
}
Collections.sort(ifaces, new ReverseSubtypeCountComparator(getSubtypeSets()));
BitSet fullBuckets = new BitSet();
int[] count = new int[ifaces.size()];
for (ClassInfo iface : ifaces) {
BitSet used = (BitSet) fullBuckets.clone();
for (DefaultEdge conflictEdge : conflictGraph.edgesOf(iface)) {
ClassInfo conflictNode = conflictGraph.getEdgeSource(conflictEdge);
// FIXME: ugly JGraphT idiom ?
if (conflictNode == iface)
conflictNode = conflictGraph.getEdgeTarget(conflictEdge);
if (bucketMap.containsKey(conflictNode)) {
used.set(bucketMap.get(conflictNode));
}
}
int color = used.nextClearBit(0);
count[color]++;
if (count[color] == maxBucketEntries) {
fullBuckets.set(color);
}
bucketMap.put(iface, color);
}
return bucketMap;
}
use of org.jgrapht.graph.DefaultEdge in project jop by jop-devel.
the class TypeGraph method getLevels.
/**
* Compute a the maximum level of each class
* If the longest subtype chain
* {@code Object <: T1 <: T2 <: ... <: T @} has length l+1,
* T has a maximum level of l.
* @return
*/
public Map<ClassInfo, Integer> getLevels() {
Map<ClassInfo, Integer> levels = new HashMap<ClassInfo, Integer>();
for (ClassInfo v : topDownTraversal()) {
int level = 0;
for (DefaultEdge parentEdge : incomingEdgesOf(v)) {
level = Math.max(level, 1 + levels.get(getEdgeSource(parentEdge)));
}
levels.put(v, level);
}
return levels;
}
use of org.jgrapht.graph.DefaultEdge in project jop by jop-devel.
the class LoopColoring method getLoopAncestor.
/**
* @param hol the loop of which to compute the ancestor
* @param dist the distance n
* @return the n-th outer loop
* <p>Given all predecessors of the loop hol in the loop nest forest, we want the one
* where {@code |successors \cap loopcolor(hol)| = dist}.</p>
* <p>Example: Assume hol = loop 4, and the following loop nest DAG<pre>
* 1->2,3,4,5,6 for(1: )
* 2->3,4,5,6 for(2: )
* 3->4,5,6 for(3: )
* 4->5 for(4: )
* for(5: )
* colors(4) = {1,2,4,5} for(6:)
* </pre>
* For 3, we have {@code |{4,5,6} \cap {1,2,3,4}| = 1}, so 3 is the ancestor with distance 1
* For 1, we have {@code |{2,3,4,5,6} \cap {1,2,3,4}| = 3}, so 1 is the ancestor with distance 3 */
public V getLoopAncestor(V hol, int dist) {
if (dist == 0)
return hol;
V ancestor = null;
Set<V> holColors = loopColors.get(hol);
SimpleDirectedGraph<V, DefaultEdge> loopNestForest = getLoopNestDAG();
for (DefaultEdge incoming : loopNestForest.incomingEdgesOf(hol)) {
V pred = loopNestForest.getEdgeSource(incoming);
loopNestForest.outgoingEdgesOf(pred);
int intersectSize = 0;
for (DefaultEdge predOutgoing : loopNestForest.outgoingEdgesOf(pred)) {
V predSucc = loopNestForest.getEdgeTarget(predOutgoing);
if (holColors.contains(predSucc))
intersectSize += 1;
}
if (intersectSize == dist) {
if (ancestor != null) {
throw new AssertionError("malformed loop nest DAG: more than one direct ancestor");
} else {
ancestor = pred;
}
}
}
return ancestor;
}
use of org.jgrapht.graph.DefaultEdge in project jop by jop-devel.
the class TypeGraph method getSubtypeSets.
/**
* Compute, for each type, the set of subtypes, using a single
* bottom-up traversal.
* @return
*/
public Map<ClassInfo, Set<ClassInfo>> getSubtypeSets() {
Map<ClassInfo, Set<ClassInfo>> subtypeMap = new HashMap<ClassInfo, Set<ClassInfo>>();
for (ClassInfo v : bottomUpTraversal()) {
Set<ClassInfo> subtypes = new HashSet<ClassInfo>();
for (DefaultEdge kidEdge : outgoingEdgesOf(v)) {
ClassInfo subtype = getEdgeTarget(kidEdge);
subtypes.addAll(subtypeMap.get(subtype));
subtypes.add(subtype);
}
subtypeMap.put(v, subtypes);
}
return subtypeMap;
}
Aggregations