From e8f5b0d42de176d9a9696d9134a661031649d5fa Mon Sep 17 00:00:00 2001 From: msridhar1 Date: Thu, 4 Jun 2009 20:55:06 +0000 Subject: [PATCH] create class Topological and move corresponding method from DefaultFixedPointSystem git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@3638 f5eafffb-2e1d-0410-98e4-8ec43c5233c4 --- .../impl/DefaultFixedPointSystem.java | 37 +------------ .../propagation/PropagationGraph.java | 4 +- .../wala/util/graph/traverse/Topological.java | 55 +++++++++++++++++++ 3 files changed, 59 insertions(+), 37 deletions(-) create mode 100644 com.ibm.wala.core/src/com/ibm/wala/util/graph/traverse/Topological.java diff --git a/com.ibm.wala.core/src/com/ibm/wala/fixedpoint/impl/DefaultFixedPointSystem.java b/com.ibm.wala.core/src/com/ibm/wala/fixedpoint/impl/DefaultFixedPointSystem.java index 07779604a..c0015a39b 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/fixedpoint/impl/DefaultFixedPointSystem.java +++ b/com.ibm.wala.core/src/com/ibm/wala/fixedpoint/impl/DefaultFixedPointSystem.java @@ -20,16 +20,13 @@ import com.ibm.wala.util.collections.EmptyIterator; import com.ibm.wala.util.collections.Filter; import com.ibm.wala.util.collections.FilterIterator; import com.ibm.wala.util.collections.HashSetFactory; -import com.ibm.wala.util.collections.ReverseIterator; import com.ibm.wala.util.debug.Assertions; import com.ibm.wala.util.debug.UnimplementedError; -import com.ibm.wala.util.graph.Graph; import com.ibm.wala.util.graph.GraphIntegrity; import com.ibm.wala.util.graph.INodeWithNumber; import com.ibm.wala.util.graph.NumberedGraph; -import com.ibm.wala.util.graph.impl.GraphInverter; import com.ibm.wala.util.graph.impl.SparseNumberedGraph; -import com.ibm.wala.util.graph.traverse.DFS; +import com.ibm.wala.util.graph.traverse.Topological; /** * @@ -205,7 +202,7 @@ public class DefaultFixedPointSystem implements IFixedPoint checkGraph(); } - Iterator order = makeSCCTopOrder(graph); + Iterator order = Topological.makeTopologicalIter(graph); int number = 0; while (order.hasNext()) { Object elt = order.next(); @@ -216,36 +213,6 @@ public class DefaultFixedPointSystem implements IFixedPoint } } - /** - * Build an Iterator over all the nodes in the graph, in an order - * such that SCCs are visited in topological order. - * @throws IllegalArgumentException if graph == null - */ - public static Iterator makeSCCTopOrder(Graph graph) throws IllegalArgumentException { - // the following code ensures a topological order over SCCs. - // note that the first two lines of the following give a topological - // order for dags, but that can get screwed up by cycles. so - // instead, we use Tarjan's SCC algorithm, which happens to - // visit nodes in an order consistent with a top. order over SCCs. - - if (graph == null) { - throw new IllegalArgumentException("graph == null"); - } - // finish time is post-order - // note that if you pay attention only to the first representative - // of each SCC discovered, we have a top. order of these SCC - // representatives - Iterator finishTime = DFS.iterateFinishTime(graph); - // reverse postorder is usual topological sort. - Iterator rev = ReverseIterator.reverse(finishTime); - // the following statement helps out the GC; note that finishTime holds - // on to a large array - finishTime = null; - Graph G_T = GraphInverter.invert(graph); - Iterator order = DFS.iterateFinishTime(G_T, rev); - return order; - } - /** * check that this graph is well-formed */ diff --git a/com.ibm.wala.core/src/com/ibm/wala/ipa/callgraph/propagation/PropagationGraph.java b/com.ibm.wala.core/src/com/ibm/wala/ipa/callgraph/propagation/PropagationGraph.java index b5dd706a5..250410b95 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/ipa/callgraph/propagation/PropagationGraph.java +++ b/com.ibm.wala.core/src/com/ibm/wala/ipa/callgraph/propagation/PropagationGraph.java @@ -17,7 +17,6 @@ import java.util.Map; import com.ibm.wala.fixedpoint.impl.AbstractOperator; import com.ibm.wala.fixedpoint.impl.AbstractStatement; -import com.ibm.wala.fixedpoint.impl.DefaultFixedPointSystem; import com.ibm.wala.fixedpoint.impl.GeneralStatement; import com.ibm.wala.fixedpoint.impl.UnaryOperator; import com.ibm.wala.fixedpoint.impl.UnaryStatement; @@ -41,6 +40,7 @@ import com.ibm.wala.util.graph.NumberedGraph; import com.ibm.wala.util.graph.NumberedNodeManager; import com.ibm.wala.util.graph.impl.DelegatingNumberedNodeManager; import com.ibm.wala.util.graph.impl.SparseNumberedEdgeManager; +import com.ibm.wala.util.graph.traverse.Topological; import com.ibm.wala.util.heapTrace.HeapTracer; import com.ibm.wala.util.intset.BasicNaturalRelation; import com.ibm.wala.util.intset.IBinaryNaturalRelation; @@ -429,7 +429,7 @@ public class PropagationGraph implements IFixedPointSystem public void reorder() { VariableGraphView graph = new VariableGraphView(); - Iterator order = DefaultFixedPointSystem.makeSCCTopOrder(graph); + Iterator order = Topological.makeTopologicalIter(graph); int number = 0; while (order.hasNext()) { diff --git a/com.ibm.wala.core/src/com/ibm/wala/util/graph/traverse/Topological.java b/com.ibm.wala.core/src/com/ibm/wala/util/graph/traverse/Topological.java new file mode 100644 index 000000000..9b4717caf --- /dev/null +++ b/com.ibm.wala.core/src/com/ibm/wala/util/graph/traverse/Topological.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (c) 2007 IBM Corporation. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package com.ibm.wala.util.graph.traverse; + +import java.util.Iterator; + +import com.ibm.wala.util.collections.ReverseIterator; +import com.ibm.wala.util.graph.Graph; +import com.ibm.wala.util.graph.impl.GraphInverter; + +/** + * Utilities for iterating over graphs in topological order. + * + */ +public class Topological { + + /** + * Build an Iterator over all the nodes in the graph, in an order such that SCCs are visited in topological order. + * + * @throws IllegalArgumentException if graph == null + */ + public static Iterator makeTopologicalIter(Graph graph) throws IllegalArgumentException { + // the following code ensures a topological order over SCCs. + // note that the first two lines of the following give a topological + // order for dags, but that can get screwed up by cycles. so + // instead, we use Tarjan's SCC algorithm, which happens to + // visit nodes in an order consistent with a top. order over SCCs. + + if (graph == null) { + throw new IllegalArgumentException("graph == null"); + } + // finish time is post-order + // note that if you pay attention only to the first representative + // of each SCC discovered, we have a top. order of these SCC + // representatives + Iterator finishTime = DFS.iterateFinishTime(graph); + // reverse postorder is usual topological sort. + Iterator rev = ReverseIterator.reverse(finishTime); + // the following statement helps out the GC; note that finishTime holds + // on to a large array + finishTime = null; + Graph G_T = GraphInverter.invert(graph); + Iterator order = DFS.iterateFinishTime(G_T, rev); + return order; + } + +}