mcimadamore@1562: /* mcimadamore@1562: * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. mcimadamore@1562: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mcimadamore@1562: * mcimadamore@1562: * This code is free software; you can redistribute it and/or modify it mcimadamore@1562: * under the terms of the GNU General Public License version 2 only, as mcimadamore@1562: * published by the Free Software Foundation. Oracle designates this mcimadamore@1562: * particular file as subject to the "Classpath" exception as provided mcimadamore@1562: * by Oracle in the LICENSE file that accompanied this code. mcimadamore@1562: * mcimadamore@1562: * This code is distributed in the hope that it will be useful, but WITHOUT mcimadamore@1562: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mcimadamore@1562: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mcimadamore@1562: * version 2 for more details (a copy is included in the LICENSE file that mcimadamore@1562: * accompanied this code). mcimadamore@1562: * mcimadamore@1562: * You should have received a copy of the GNU General Public License version mcimadamore@1562: * 2 along with this work; if not, write to the Free Software Foundation, mcimadamore@1562: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mcimadamore@1562: * mcimadamore@1562: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA mcimadamore@1562: * or visit www.oracle.com if you need additional information or have any mcimadamore@1562: * questions. mcimadamore@1562: */ mcimadamore@1562: mcimadamore@1562: package com.sun.tools.javac.util; mcimadamore@1562: mcimadamore@1562: /**

This is NOT part of any supported API. mcimadamore@1562: * If you write code that depends on this, you do so at your own risk. mcimadamore@1562: * This code and its internal interfaces are subject to change or mcimadamore@1562: * deletion without notice. mcimadamore@1562: */ mcimadamore@1562: public class GraphUtils { mcimadamore@1562: mcimadamore@1562: /** mcimadamore@1562: * This class is a basic abstract class for representing a node. mcimadamore@1562: * A node is associated with a given data. mcimadamore@1562: */ mcimadamore@1562: public static abstract class Node { mcimadamore@1562: public final D data; mcimadamore@1562: mcimadamore@1562: public Node(D data) { mcimadamore@1562: this.data = data; mcimadamore@1562: } mcimadamore@1562: mcimadamore@1562: public abstract Iterable> getDependencies(); mcimadamore@1562: mcimadamore@1562: public abstract String printDependency(Node to); mcimadamore@1562: mcimadamore@1562: @Override mcimadamore@1562: public String toString() { mcimadamore@1562: return data.toString(); mcimadamore@1562: } mcimadamore@1562: } mcimadamore@1562: mcimadamore@1562: /** mcimadamore@1562: * This class specialized Node, by adding elements that are required in order mcimadamore@1562: * to perform Tarjan computation of strongly connected components. mcimadamore@1562: */ mcimadamore@1562: public static abstract class TarjanNode extends Node implements Comparable> { mcimadamore@1562: int index = -1; mcimadamore@1562: int lowlink; mcimadamore@1562: boolean active; mcimadamore@1562: mcimadamore@1562: public TarjanNode(D data) { mcimadamore@1562: super(data); mcimadamore@1562: } mcimadamore@1562: mcimadamore@1562: public abstract Iterable> getDependencies(); mcimadamore@1562: mcimadamore@1562: public int compareTo(TarjanNode o) { mcimadamore@1562: return (index < o.index) ? -1 : (index == o.index) ? 0 : 1; mcimadamore@1562: } mcimadamore@1562: } mcimadamore@1562: mcimadamore@1562: /** mcimadamore@1562: * Tarjan's algorithm to determine strongly connected components of a mcimadamore@1562: * directed graph in linear time. Works on TarjanNode. mcimadamore@1562: */ mcimadamore@1562: public static > List> tarjan(Iterable nodes) { mcimadamore@1562: ListBuffer> cycles = ListBuffer.lb(); mcimadamore@1562: ListBuffer stack = ListBuffer.lb(); mcimadamore@1562: int index = 0; mcimadamore@1562: for (N node: nodes) { mcimadamore@1562: if (node.index == -1) { mcimadamore@1562: index += tarjan(node, index, stack, cycles); mcimadamore@1562: } mcimadamore@1562: } mcimadamore@1562: return cycles.toList(); mcimadamore@1562: } mcimadamore@1562: mcimadamore@1562: private static > int tarjan(N v, int index, ListBuffer stack, ListBuffer> cycles) { mcimadamore@1562: v.index = index; mcimadamore@1562: v.lowlink = index; mcimadamore@1562: index++; mcimadamore@1562: stack.prepend(v); mcimadamore@1562: v.active = true; mcimadamore@1562: for (TarjanNode nd: v.getDependencies()) { mcimadamore@1562: @SuppressWarnings("unchecked") mcimadamore@1562: N n = (N)nd; mcimadamore@1562: if (n.index == -1) { mcimadamore@1562: tarjan(n, index, stack, cycles); mcimadamore@1562: v.lowlink = Math.min(v.lowlink, n.lowlink); mcimadamore@1562: } else if (stack.contains(n)) { mcimadamore@1562: v.lowlink = Math.min(v.lowlink, n.index); mcimadamore@1562: } mcimadamore@1562: } mcimadamore@1562: if (v.lowlink == v.index) { mcimadamore@1562: N n; mcimadamore@1562: ListBuffer cycle = ListBuffer.lb(); mcimadamore@1562: do { mcimadamore@1562: n = stack.remove(); mcimadamore@1562: n.active = false; mcimadamore@1562: cycle.add(n); mcimadamore@1562: } while (n != v); mcimadamore@1562: cycles.add(cycle.toList()); mcimadamore@1562: } mcimadamore@1562: return index; mcimadamore@1562: } mcimadamore@1562: mcimadamore@1562: /** mcimadamore@1562: * Debugging: dot representation of a set of connected nodes. The resulting mcimadamore@1562: * dot representation will use {@code Node.toString} to display node labels mcimadamore@1562: * and {@code Node.printDependency} to display edge labels. The resulting mcimadamore@1562: * representation is also customizable with a graph name and a header. mcimadamore@1562: */ mcimadamore@1562: public static String toDot(Iterable> nodes, String name, String header) { mcimadamore@1562: StringBuilder buf = new StringBuilder(); mcimadamore@1562: buf.append(String.format("digraph %s {\n", name)); mcimadamore@1562: buf.append(String.format("label = \"%s\";\n", header)); mcimadamore@1562: //dump nodes mcimadamore@1562: for (TarjanNode n : nodes) { mcimadamore@1562: buf.append(String.format("%s [label = \"%s\"];\n", n.hashCode(), n.toString())); mcimadamore@1562: } mcimadamore@1562: //dump arcs mcimadamore@1562: for (TarjanNode from : nodes) { mcimadamore@1562: for (TarjanNode to : from.getDependencies()) { mcimadamore@1562: buf.append(String.format("%s -> %s [label = \" %s \"];\n", mcimadamore@1562: from.hashCode(), to.hashCode(), from.printDependency(to))); mcimadamore@1562: } mcimadamore@1562: } mcimadamore@1562: buf.append("}\n"); mcimadamore@1562: return buf.toString(); mcimadamore@1562: } mcimadamore@1562: }