properly mark symbols in the public API to be exported. This allows us to use -fvisib...
[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 #include "../begin.h"
37
38 /**
39  * Call this to initialize an array of @p count elements to be used by the
40  * union find functions.
41  *
42  * @param data    The array (you have to allocate it yourself)
43  * @param n_elems number of elements handled by the datastructure
44  */
45 static inline void uf_init(int* data, size_t n_elems)
46 {
47         size_t i;
48         for(i = 0; i < n_elems; ++i) {
49                 data[i] = -1;
50         }
51 }
52
53 /**
54  * Merge 2 sets (union operation). Note that you have to pass the
55  * representatives of the sets and not just random elements
56  *
57  * @param data  The union find data
58  * @param set1  Representative of set1
59  * @param set2  Representative of set2
60  * @return      the new representative of the set (which is set1 or set2)
61  */
62 static inline int uf_union(int* data, int set1, int set2)
63 {
64         int d1;
65         int d2;
66         int newcount;
67
68         if(set1 == set2)
69                 return set1;
70
71         /* need 2 set representatives */
72         d1 = data[set1];
73         d2 = data[set2];
74         assert(d1 < 0 && d2 < 0);
75
76         newcount = d1 + d2;
77         if(d1 > d2) {
78                 data[set1] = set2;
79                 data[set2] = newcount;
80                 return set2;
81         } else {
82                 data[set2] = set1;
83                 data[set1] = newcount;
84                 return set1;
85         }
86 }
87
88 /**
89  * Finds the representative for the set with member @p e.
90  * The representative of a set is unique, so if the find operations finds
91  * the same/different representatives, then the elements are in the
92  * the same/different sets.
93  *
94  * @param data  The union find data
95  * @param e             The element
96  * @return              The representative of the set that contains @p e
97  */
98 static inline int uf_find(int* data, int e)
99 {
100         /* go through list to find representative */
101         int repr = e;
102         while(data[repr] >= 0) {
103                 repr = data[repr];
104         }
105
106         /* update list to point to new representative (path compression) */
107         while(e != repr) {
108                 int next = data[e];
109                 data[e] = repr;
110                 e = next;
111         }
112
113         return repr;
114 }
115
116 #include "../end.h"
117
118 #endif