src/share/classes/com/sun/tools/javac/util/GraphUtils.java

Sun, 17 Feb 2013 16:44:55 -0500

author
dholmes
date
Sun, 17 Feb 2013 16:44:55 -0500
changeset 1571
af8417e590f4
parent 1562
2154ed9ff6c8
child 2000
4a6acc42c3a1
permissions
-rw-r--r--

Merge

mcimadamore@1562 1 /*
mcimadamore@1562 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
mcimadamore@1562 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@1562 4 *
mcimadamore@1562 5 * This code is free software; you can redistribute it and/or modify it
mcimadamore@1562 6 * under the terms of the GNU General Public License version 2 only, as
mcimadamore@1562 7 * published by the Free Software Foundation. Oracle designates this
mcimadamore@1562 8 * particular file as subject to the "Classpath" exception as provided
mcimadamore@1562 9 * by Oracle in the LICENSE file that accompanied this code.
mcimadamore@1562 10 *
mcimadamore@1562 11 * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@1562 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@1562 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@1562 14 * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@1562 15 * accompanied this code).
mcimadamore@1562 16 *
mcimadamore@1562 17 * You should have received a copy of the GNU General Public License version
mcimadamore@1562 18 * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@1562 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@1562 20 *
mcimadamore@1562 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
mcimadamore@1562 22 * or visit www.oracle.com if you need additional information or have any
mcimadamore@1562 23 * questions.
mcimadamore@1562 24 */
mcimadamore@1562 25
mcimadamore@1562 26 package com.sun.tools.javac.util;
mcimadamore@1562 27
mcimadamore@1562 28 /** <p><b>This is NOT part of any supported API.
mcimadamore@1562 29 * If you write code that depends on this, you do so at your own risk.
mcimadamore@1562 30 * This code and its internal interfaces are subject to change or
mcimadamore@1562 31 * deletion without notice.</b>
mcimadamore@1562 32 */
mcimadamore@1562 33 public class GraphUtils {
mcimadamore@1562 34
mcimadamore@1562 35 /**
mcimadamore@1562 36 * This class is a basic abstract class for representing a node.
mcimadamore@1562 37 * A node is associated with a given data.
mcimadamore@1562 38 */
mcimadamore@1562 39 public static abstract class Node<D> {
mcimadamore@1562 40 public final D data;
mcimadamore@1562 41
mcimadamore@1562 42 public Node(D data) {
mcimadamore@1562 43 this.data = data;
mcimadamore@1562 44 }
mcimadamore@1562 45
mcimadamore@1562 46 public abstract Iterable<? extends Node<D>> getDependencies();
mcimadamore@1562 47
mcimadamore@1562 48 public abstract String printDependency(Node<D> to);
mcimadamore@1562 49
mcimadamore@1562 50 @Override
mcimadamore@1562 51 public String toString() {
mcimadamore@1562 52 return data.toString();
mcimadamore@1562 53 }
mcimadamore@1562 54 }
mcimadamore@1562 55
mcimadamore@1562 56 /**
mcimadamore@1562 57 * This class specialized Node, by adding elements that are required in order
mcimadamore@1562 58 * to perform Tarjan computation of strongly connected components.
mcimadamore@1562 59 */
mcimadamore@1562 60 public static abstract class TarjanNode<D> extends Node<D> implements Comparable<TarjanNode<D>> {
mcimadamore@1562 61 int index = -1;
mcimadamore@1562 62 int lowlink;
mcimadamore@1562 63 boolean active;
mcimadamore@1562 64
mcimadamore@1562 65 public TarjanNode(D data) {
mcimadamore@1562 66 super(data);
mcimadamore@1562 67 }
mcimadamore@1562 68
mcimadamore@1562 69 public abstract Iterable<? extends TarjanNode<D>> getDependencies();
mcimadamore@1562 70
mcimadamore@1562 71 public int compareTo(TarjanNode<D> o) {
mcimadamore@1562 72 return (index < o.index) ? -1 : (index == o.index) ? 0 : 1;
mcimadamore@1562 73 }
mcimadamore@1562 74 }
mcimadamore@1562 75
mcimadamore@1562 76 /**
mcimadamore@1562 77 * Tarjan's algorithm to determine strongly connected components of a
mcimadamore@1562 78 * directed graph in linear time. Works on TarjanNode.
mcimadamore@1562 79 */
mcimadamore@1562 80 public static <D, N extends TarjanNode<D>> List<? extends List<? extends N>> tarjan(Iterable<? extends N> nodes) {
mcimadamore@1562 81 ListBuffer<List<N>> cycles = ListBuffer.lb();
mcimadamore@1562 82 ListBuffer<N> stack = ListBuffer.lb();
mcimadamore@1562 83 int index = 0;
mcimadamore@1562 84 for (N node: nodes) {
mcimadamore@1562 85 if (node.index == -1) {
mcimadamore@1562 86 index += tarjan(node, index, stack, cycles);
mcimadamore@1562 87 }
mcimadamore@1562 88 }
mcimadamore@1562 89 return cycles.toList();
mcimadamore@1562 90 }
mcimadamore@1562 91
mcimadamore@1562 92 private static <D, N extends TarjanNode<D>> int tarjan(N v, int index, ListBuffer<N> stack, ListBuffer<List<N>> cycles) {
mcimadamore@1562 93 v.index = index;
mcimadamore@1562 94 v.lowlink = index;
mcimadamore@1562 95 index++;
mcimadamore@1562 96 stack.prepend(v);
mcimadamore@1562 97 v.active = true;
mcimadamore@1562 98 for (TarjanNode<D> nd: v.getDependencies()) {
mcimadamore@1562 99 @SuppressWarnings("unchecked")
mcimadamore@1562 100 N n = (N)nd;
mcimadamore@1562 101 if (n.index == -1) {
mcimadamore@1562 102 tarjan(n, index, stack, cycles);
mcimadamore@1562 103 v.lowlink = Math.min(v.lowlink, n.lowlink);
mcimadamore@1562 104 } else if (stack.contains(n)) {
mcimadamore@1562 105 v.lowlink = Math.min(v.lowlink, n.index);
mcimadamore@1562 106 }
mcimadamore@1562 107 }
mcimadamore@1562 108 if (v.lowlink == v.index) {
mcimadamore@1562 109 N n;
mcimadamore@1562 110 ListBuffer<N> cycle = ListBuffer.lb();
mcimadamore@1562 111 do {
mcimadamore@1562 112 n = stack.remove();
mcimadamore@1562 113 n.active = false;
mcimadamore@1562 114 cycle.add(n);
mcimadamore@1562 115 } while (n != v);
mcimadamore@1562 116 cycles.add(cycle.toList());
mcimadamore@1562 117 }
mcimadamore@1562 118 return index;
mcimadamore@1562 119 }
mcimadamore@1562 120
mcimadamore@1562 121 /**
mcimadamore@1562 122 * Debugging: dot representation of a set of connected nodes. The resulting
mcimadamore@1562 123 * dot representation will use {@code Node.toString} to display node labels
mcimadamore@1562 124 * and {@code Node.printDependency} to display edge labels. The resulting
mcimadamore@1562 125 * representation is also customizable with a graph name and a header.
mcimadamore@1562 126 */
mcimadamore@1562 127 public static <D> String toDot(Iterable<? extends TarjanNode<D>> nodes, String name, String header) {
mcimadamore@1562 128 StringBuilder buf = new StringBuilder();
mcimadamore@1562 129 buf.append(String.format("digraph %s {\n", name));
mcimadamore@1562 130 buf.append(String.format("label = \"%s\";\n", header));
mcimadamore@1562 131 //dump nodes
mcimadamore@1562 132 for (TarjanNode<D> n : nodes) {
mcimadamore@1562 133 buf.append(String.format("%s [label = \"%s\"];\n", n.hashCode(), n.toString()));
mcimadamore@1562 134 }
mcimadamore@1562 135 //dump arcs
mcimadamore@1562 136 for (TarjanNode<D> from : nodes) {
mcimadamore@1562 137 for (TarjanNode<D> to : from.getDependencies()) {
mcimadamore@1562 138 buf.append(String.format("%s -> %s [label = \" %s \"];\n",
mcimadamore@1562 139 from.hashCode(), to.hashCode(), from.printDependency(to)));
mcimadamore@1562 140 }
mcimadamore@1562 141 }
mcimadamore@1562 142 buf.append("}\n");
mcimadamore@1562 143 return buf.toString();
mcimadamore@1562 144 }
mcimadamore@1562 145 }

mercurial