001/*
002 * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.graph;
024
025/**
026 * Union-find data structure for {@link Node Nodes}.
027 *
028 * All operations have an accumulated worst-case complexity of O(a(n)), where a(n) is the inverse of
029 * the Ackermann function A(n,n).
030 */
031public class NodeUnionFind extends NodeIdAccessor {
032
033    private int[] rank;
034    private int[] parent;
035
036    /**
037     * Create a new union-find data structure for a {@link Graph}. Initially, all nodes are in their
038     * own equivalence set.
039     */
040    public NodeUnionFind(Graph graph) {
041        super(graph);
042        rank = new int[graph.nodeIdCount()];
043        parent = new int[graph.nodeIdCount()];
044        for (int i = 0; i < parent.length; i++) {
045            parent[i] = i;
046        }
047    }
048
049    /**
050     * Merge the equivalence sets of two nodes.
051     *
052     * After calling this function, find(a) == find(b).
053     */
054    public void union(Node a, Node b) {
055        union(getNodeId(a), getNodeId(b));
056    }
057
058    /**
059     * Get a representative element of the equivalence set of a node.
060     *
061     * This function returns the same representative element for all members of the same equivalence
062     * set, i.e., find(a) == find(b) if and only if a and b are in the same set.
063     */
064    public Node find(Node a) {
065        int id = find(getNodeId(a));
066        return graph.getNode(id);
067    }
068
069    public boolean equiv(Node a, Node b) {
070        return find(getNodeId(a)) == find(getNodeId(b));
071    }
072
073    private void union(int a, int b) {
074        int aRoot = find(a);
075        int bRoot = find(b);
076        if (aRoot != bRoot) {
077            if (rank[aRoot] < rank[bRoot]) {
078                parent[aRoot] = bRoot;
079            } else {
080                parent[bRoot] = aRoot;
081                if (rank[aRoot] == rank[bRoot]) {
082                    rank[aRoot]++;
083                }
084            }
085        }
086    }
087
088    private int find(int a) {
089        int ret = a;
090        while (ret != parent[ret]) {
091            parent[ret] = parent[parent[ret]];
092            ret = parent[ret];
093        }
094        return ret;
095    }
096}