1 /*
   2  * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 package org.graalvm.compiler.graph;
  26 
  27 /**
  28  * Union-find data structure for {@link Node Nodes}.
  29  *
  30  * All operations have an accumulated worst-case complexity of O(a(n)), where a(n) is the inverse of
  31  * the Ackermann function A(n,n).
  32  */
  33 public class NodeUnionFind extends NodeIdAccessor {
  34 
  35     private int[] rank;
  36     private int[] parent;
  37 
  38     /**
  39      * Create a new union-find data structure for a {@link Graph}. Initially, all nodes are in their
  40      * own equivalence set.
  41      */
  42     public NodeUnionFind(Graph graph) {
  43         super(graph);
  44         rank = new int[graph.nodeIdCount()];
  45         parent = new int[graph.nodeIdCount()];
  46         for (int i = 0; i < parent.length; i++) {
  47             parent[i] = i;
  48         }
  49     }
  50 
  51     /**
  52      * Merge the equivalence sets of two nodes.
  53      *
  54      * After calling this function, find(a) == find(b).
  55      */
  56     public void union(Node a, Node b) {
  57         union(getNodeId(a), getNodeId(b));
  58     }
  59 
  60     /**
  61      * Get a representative element of the equivalence set of a node.
  62      *
  63      * This function returns the same representative element for all members of the same equivalence
  64      * set, i.e., find(a) == find(b) if and only if a and b are in the same set.
  65      */
  66     public Node find(Node a) {
  67         int id = find(getNodeId(a));
  68         return graph.getNode(id);
  69     }
  70 
  71     public boolean equiv(Node a, Node b) {
  72         return find(getNodeId(a)) == find(getNodeId(b));
  73     }
  74 
  75     private void union(int a, int b) {
  76         int aRoot = find(a);
  77         int bRoot = find(b);
  78         if (aRoot != bRoot) {
  79             if (rank[aRoot] < rank[bRoot]) {
  80                 parent[aRoot] = bRoot;
  81             } else {
  82                 parent[bRoot] = aRoot;
  83                 if (rank[aRoot] == rank[bRoot]) {
  84                     rank[aRoot]++;
  85                 }
  86             }
  87         }
  88     }
  89 
  90     private int find(int a) {
  91         int ret = a;
  92         while (ret != parent[ret]) {
  93             parent[ret] = parent[parent[ret]];
  94             ret = parent[ret];
  95         }
  96         return ret;
  97     }
  98 }