2c2316d801c1805f4d38f64eb00ccd53805c3484
[libfirm] / include / libfirm / adt / unionfind.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief      Union-Find datastructure
23  * @author     Matthias Braun
24  * @version    $Id$
25  * @brief
26  *  Union-Find datastructure
27  *
28  *  This implementation uses weighted sets and path compression which results
29  *  in (nearly) O(n) complexity for n find and union operations
30  */
31 #ifndef FIRM_ADT_UNIONFIND_H
32 #define FIRM_ADT_UNIONFIND_H
33
34 #include <assert.h>
35
36 /**
37  * Call this to initialize an array of @p count elements to be used by the
38  * union find functions.
39  *
40  * @param data    The array (you have to allocate it yourself)
41  * @param n_elems number of elements handled by the datastructure
42  */
43 static inline void uf_init(int* data, size_t n_elems)
44 {
45         size_t i;
46         for(i = 0; i < n_elems; ++i) {
47                 data[i] = -1;
48         }
49 }
50
51 /**
52  * Merge 2 sets (union operation). Note that you have to pass the
53  * representatives of the sets and not just random elements
54  *
55  * @param data  The union find data
56  * @param set1  Representative of set1
57  * @param set2  Representative of set2
58  * @return      the new representative of the set (which is set1 or set2)
59  */
60 static inline int uf_union(int* data, int set1, int set2)
61 {
62         int d1;
63         int d2;
64         int newcount;
65
66         if(set1 == set2)
67                 return set1;
68
69         /* need 2 set representatives */
70         d1 = data[set1];
71         d2 = data[set2];
72         assert(d1 < 0 && d2 < 0);
73
74         newcount = d1 + d2;
75         if(d1 > d2) {
76                 data[set1] = set2;
77                 data[set2] = newcount;
78                 return set2;
79         } else {
80                 data[set2] = set1;
81                 data[set1] = newcount;
82                 return set1;
83         }
84 }
85
86 /**
87  * Finds the representative for the set with member @p e.
88  * The representative of a set is unique, so if the find operations finds
89  * the same/different representatives, then the elements are in the
90  * the same/different sets.
91  *
92  * @param data  The union find data
93  * @param e             The element
94  * @return              The representative of the set that contains @p e
95  */
96 static inline int uf_find(int* data, int e)
97 {
98         /* go through list to find representative */
99         int repr = e;
100         while(data[repr] >= 0) {
101                 repr = data[repr];
102         }
103
104         /* update list to point to new representative (path compression) */
105         while(e != repr) {
106                 int next = data[e];
107                 data[e] = repr;
108                 e = next;
109         }
110
111         return repr;
112 }
113
114 #endif