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 package org.graalvm.compiler.graph;
  24 
  25 /**
  26  * Union-find data structure for {@link Node Nodes}.
  27  *
  28  * All operations have an accumulated worst-case complexity of O(a(n)), where a(n) is the inverse of
  29  * the Ackermann function A(n,n).
  30  */
  31 public class NodeUnionFind extends NodeIdAccessor {
  32 
  33     private int[] rank;
  34     private int[] parent;
  35 
  36     /**
  37      * Create a new union-find data structure for a {@link Graph}. Initially, all nodes are in their
  38      * own equivalence set.
  39      */
  40     public NodeUnionFind(Graph graph) {
  41         super(graph);
  42         rank = new int[graph.nodeIdCount()];
  43         parent = new int[graph.nodeIdCount()];
  44         for (int i = 0; i < parent.length; i++) {
  45             parent[i] = i;
  46         }
  47     }
  48 
  49     /**
  50      * Merge the equivalence sets of two nodes.
  51      *
  52      * After calling this function, find(a) == find(b).
  53      */
  54     public void union(Node a, Node b) {
  55         union(getNodeId(a), getNodeId(b));
  56     }
  57 
  58     /**
  59      * Get a representative element of the equivalence set of a node.
  60      *
  61      * This function returns the same representative element for all members of the same equivalence
  62      * set, i.e., find(a) == find(b) if and only if a and b are in the same set.
  63      */
  64     public Node find(Node a) {
  65         int id = find(getNodeId(a));
  66         return graph.getNode(id);
  67     }
  68 
  69     public boolean equiv(Node a, Node b) {
  70         return find(getNodeId(a)) == find(getNodeId(b));
  71     }
  72 
  73     private void union(int a, int b) {
  74         int aRoot = find(a);
  75         int bRoot = find(b);
  76         if (aRoot != bRoot) {
  77             if (rank[aRoot] < rank[bRoot]) {
  78                 parent[aRoot] = bRoot;
  79             } else {
  80                 parent[bRoot] = aRoot;
  81                 if (rank[aRoot] == rank[bRoot]) {
  82                     rank[aRoot]++;
  83                 }
  84             }
  85         }
  86     }
  87 
  88     private int find(int a) {
  89         int ret = a;
  90         while (ret != parent[ret]) {
  91             parent[ret] = parent[parent[ret]];
  92             ret = parent[ret];
  93         }
  94         return ret;
  95     }
  96 }