diff --git a/platform/util/src/com/intellij/util/graph/CachingSemiGraph.java b/platform/util/src/com/intellij/util/graph/CachingSemiGraph.java index 3264699131c3..a52f36094dda 100644 --- a/platform/util/src/com/intellij/util/graph/CachingSemiGraph.java +++ b/platform/util/src/com/intellij/util/graph/CachingSemiGraph.java @@ -23,10 +23,14 @@ import java.util.*; * @author dsl */ public class CachingSemiGraph implements GraphGenerator.SemiGraph { + public static InboundSemiGraph cache(InboundSemiGraph original) { + return new CachingSemiGraph(original); + } + private final Set myNodes; private final Map> myIn; - public CachingSemiGraph(GraphGenerator.SemiGraph original) { + private CachingSemiGraph(InboundSemiGraph original) { myNodes = ContainerUtil.newLinkedHashSet(original.getNodes()); myIn = new LinkedHashMap>(); for (Node node : myNodes) { @@ -36,10 +40,6 @@ public class CachingSemiGraph implements GraphGenerator.SemiGraph { } } - public static CachingSemiGraph create(GraphGenerator.SemiGraph original) { - return new CachingSemiGraph(original); - } - @Override public Collection getNodes() { return myNodes; @@ -49,4 +49,14 @@ public class CachingSemiGraph implements GraphGenerator.SemiGraph { public Iterator getIn(Node n) { return myIn.get(n).iterator(); } + + // + public static CachingSemiGraph create(GraphGenerator.SemiGraph original) { + return new CachingSemiGraph((InboundSemiGraph)original); + } + + public CachingSemiGraph(GraphGenerator.SemiGraph original) { + this((InboundSemiGraph)original); + } + // } \ No newline at end of file diff --git a/platform/util/src/com/intellij/util/graph/DFSTBuilder.java b/platform/util/src/com/intellij/util/graph/DFSTBuilder.java index afad35943393..1b3b22d7e277 100644 --- a/platform/util/src/com/intellij/util/graph/DFSTBuilder.java +++ b/platform/util/src/com/intellij/util/graph/DFSTBuilder.java @@ -30,7 +30,7 @@ import java.util.*; * @author dsl, ven */ public class DFSTBuilder { - private final Graph myGraph; + private final OutboundSemiGraph myGraph; private final TObjectIntHashMap myNodeToNNumber; // node -> node number in topological order [0..size). Independent nodes are in reversed loading order (loading order is the graph.getNodes() order) private final Node[] myInvN; // node number in topological order [0..size) -> node private Couple myBackEdge; @@ -42,8 +42,12 @@ public class DFSTBuilder { private final Node[] myInvT; // number in (enumerate all nodes scc by scc) order -> node private final Node[] myAllNodes; - @SuppressWarnings("unchecked") public DFSTBuilder(@NotNull Graph graph) { + this((OutboundSemiGraph)graph); + } + + @SuppressWarnings("unchecked") + public DFSTBuilder(@NotNull OutboundSemiGraph graph) { myAllNodes = (Node[])graph.getNodes().toArray(); myGraph = graph; int size = graph.getNodes().size(); diff --git a/platform/util/src/com/intellij/util/graph/Graph.java b/platform/util/src/com/intellij/util/graph/Graph.java index a192cfa7e238..aab996994600 100644 --- a/platform/util/src/com/intellij/util/graph/Graph.java +++ b/platform/util/src/com/intellij/util/graph/Graph.java @@ -21,7 +21,7 @@ import java.util.Iterator; /** * @author dsl */ -public interface Graph { +public interface Graph extends InboundSemiGraph, OutboundSemiGraph { Collection getNodes(); Iterator getIn(Node n); diff --git a/platform/util/src/com/intellij/util/graph/GraphGenerator.java b/platform/util/src/com/intellij/util/graph/GraphGenerator.java index d7a583e14188..73a50dd29b96 100644 --- a/platform/util/src/com/intellij/util/graph/GraphGenerator.java +++ b/platform/util/src/com/intellij/util/graph/GraphGenerator.java @@ -21,25 +21,19 @@ import java.util.*; * @author dsl */ public class GraphGenerator implements Graph { - public interface SemiGraph { - Collection getNodes(); - - Iterator getIn(Node n); + public static Graph generate(InboundSemiGraph graph) { + return new GraphGenerator(graph); } - private final SemiGraph myGraph; + private final InboundSemiGraph myGraph; private final Map> myOuts; - public GraphGenerator(SemiGraph graph) { + private GraphGenerator(InboundSemiGraph graph) { myGraph = graph; myOuts = new LinkedHashMap>(); buildOuts(); } - public static GraphGenerator create(SemiGraph graph) { - return new GraphGenerator(graph); - } - private void buildOuts() { Collection nodes = myGraph.getNodes(); for (Node node : nodes) { @@ -73,4 +67,20 @@ public class GraphGenerator implements Graph { public Iterator getOut(Node n) { return myOuts.get(n).iterator(); } + + // + public interface SemiGraph extends InboundSemiGraph { + Collection getNodes(); + + Iterator getIn(Node n); + } + + public GraphGenerator(SemiGraph graph) { + this((InboundSemiGraph)graph); + } + + public static GraphGenerator create(SemiGraph graph) { + return new GraphGenerator((InboundSemiGraph)graph); + } + // } \ No newline at end of file diff --git a/platform/util/src/com/intellij/util/graph/InboundSemiGraph.java b/platform/util/src/com/intellij/util/graph/InboundSemiGraph.java new file mode 100644 index 000000000000..108c6dc5e448 --- /dev/null +++ b/platform/util/src/com/intellij/util/graph/InboundSemiGraph.java @@ -0,0 +1,25 @@ +/* + * Copyright 2000-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.util.graph; + +import java.util.Collection; +import java.util.Iterator; + +public interface InboundSemiGraph { + Collection getNodes(); + + Iterator getIn(Node n); +} \ No newline at end of file diff --git a/platform/util/src/com/intellij/util/graph/OutboundSemiGraph.java b/platform/util/src/com/intellij/util/graph/OutboundSemiGraph.java new file mode 100644 index 000000000000..6a4688dc07cc --- /dev/null +++ b/platform/util/src/com/intellij/util/graph/OutboundSemiGraph.java @@ -0,0 +1,25 @@ +/* + * Copyright 2000-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.util.graph; + +import java.util.Collection; +import java.util.Iterator; + +public interface OutboundSemiGraph { + Collection getNodes(); + + Iterator getOut(Node n); +} \ No newline at end of file