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

changeset 1562
2154ed9ff6c8
child 2000
4a6acc42c3a1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/util/GraphUtils.java	Tue Feb 12 19:25:09 2013 +0000
     1.3 @@ -0,0 +1,145 @@
     1.4 +/*
     1.5 + * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javac.util;
    1.30 +
    1.31 +/** <p><b>This is NOT part of any supported API.
    1.32 + *  If you write code that depends on this, you do so at your own risk.
    1.33 + *  This code and its internal interfaces are subject to change or
    1.34 + *  deletion without notice.</b>
    1.35 + */
    1.36 +public class GraphUtils {
    1.37 +
    1.38 +    /**
    1.39 +     * This class is a basic abstract class for representing a node.
    1.40 +     * A node is associated with a given data.
    1.41 +     */
    1.42 +    public static abstract class Node<D> {
    1.43 +        public final D data;
    1.44 +
    1.45 +        public Node(D data) {
    1.46 +            this.data = data;
    1.47 +        }
    1.48 +
    1.49 +        public abstract Iterable<? extends Node<D>> getDependencies();
    1.50 +
    1.51 +        public abstract String printDependency(Node<D> to);
    1.52 +
    1.53 +        @Override
    1.54 +        public String toString() {
    1.55 +            return data.toString();
    1.56 +        }
    1.57 +    }
    1.58 +
    1.59 +    /**
    1.60 +     * This class specialized Node, by adding elements that are required in order
    1.61 +     * to perform Tarjan computation of strongly connected components.
    1.62 +     */
    1.63 +    public static abstract class TarjanNode<D> extends Node<D> implements Comparable<TarjanNode<D>> {
    1.64 +        int index = -1;
    1.65 +        int lowlink;
    1.66 +        boolean active;
    1.67 +
    1.68 +        public TarjanNode(D data) {
    1.69 +            super(data);
    1.70 +        }
    1.71 +
    1.72 +        public abstract Iterable<? extends TarjanNode<D>> getDependencies();
    1.73 +
    1.74 +        public int compareTo(TarjanNode<D> o) {
    1.75 +            return (index < o.index) ? -1 : (index == o.index) ? 0 : 1;
    1.76 +        }
    1.77 +    }
    1.78 +
    1.79 +    /**
    1.80 +     * Tarjan's algorithm to determine strongly connected components of a
    1.81 +     * directed graph in linear time. Works on TarjanNode.
    1.82 +     */
    1.83 +    public static <D, N extends TarjanNode<D>> List<? extends List<? extends N>> tarjan(Iterable<? extends N> nodes) {
    1.84 +        ListBuffer<List<N>> cycles = ListBuffer.lb();
    1.85 +        ListBuffer<N> stack = ListBuffer.lb();
    1.86 +        int index = 0;
    1.87 +        for (N node: nodes) {
    1.88 +            if (node.index == -1) {
    1.89 +                index += tarjan(node, index, stack, cycles);
    1.90 +            }
    1.91 +        }
    1.92 +        return cycles.toList();
    1.93 +    }
    1.94 +
    1.95 +    private static <D, N extends TarjanNode<D>> int tarjan(N v, int index, ListBuffer<N> stack, ListBuffer<List<N>> cycles) {
    1.96 +        v.index = index;
    1.97 +        v.lowlink = index;
    1.98 +        index++;
    1.99 +        stack.prepend(v);
   1.100 +        v.active = true;
   1.101 +        for (TarjanNode<D> nd: v.getDependencies()) {
   1.102 +            @SuppressWarnings("unchecked")
   1.103 +            N n = (N)nd;
   1.104 +            if (n.index == -1) {
   1.105 +                tarjan(n, index, stack, cycles);
   1.106 +                v.lowlink = Math.min(v.lowlink, n.lowlink);
   1.107 +            } else if (stack.contains(n)) {
   1.108 +                v.lowlink = Math.min(v.lowlink, n.index);
   1.109 +            }
   1.110 +        }
   1.111 +        if (v.lowlink == v.index) {
   1.112 +            N n;
   1.113 +            ListBuffer<N> cycle = ListBuffer.lb();
   1.114 +            do {
   1.115 +                n = stack.remove();
   1.116 +                n.active = false;
   1.117 +                cycle.add(n);
   1.118 +            } while (n != v);
   1.119 +            cycles.add(cycle.toList());
   1.120 +        }
   1.121 +        return index;
   1.122 +    }
   1.123 +
   1.124 +    /**
   1.125 +     * Debugging: dot representation of a set of connected nodes. The resulting
   1.126 +     * dot representation will use {@code Node.toString} to display node labels
   1.127 +     * and {@code Node.printDependency} to display edge labels. The resulting
   1.128 +     * representation is also customizable with a graph name and a header.
   1.129 +     */
   1.130 +    public static <D> String toDot(Iterable<? extends TarjanNode<D>> nodes, String name, String header) {
   1.131 +        StringBuilder buf = new StringBuilder();
   1.132 +        buf.append(String.format("digraph %s {\n", name));
   1.133 +        buf.append(String.format("label = \"%s\";\n", header));
   1.134 +        //dump nodes
   1.135 +        for (TarjanNode<D> n : nodes) {
   1.136 +            buf.append(String.format("%s [label = \"%s\"];\n", n.hashCode(), n.toString()));
   1.137 +        }
   1.138 +        //dump arcs
   1.139 +        for (TarjanNode<D> from : nodes) {
   1.140 +            for (TarjanNode<D> to : from.getDependencies()) {
   1.141 +                buf.append(String.format("%s -> %s [label = \" %s \"];\n",
   1.142 +                        from.hashCode(), to.hashCode(), from.printDependency(to)));
   1.143 +            }
   1.144 +        }
   1.145 +        buf.append("}\n");
   1.146 +        return buf.toString();
   1.147 +    }
   1.148 +}

mercurial