OSDN Git Service

Merge tree-ssa-20020619-branch into mainline.
[pf3gnuchains/gcc-fork.git] / libbanshee / engine / ufind.c
1 /*
2  * Copyright (c) 2000-2001
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30
31 #include <stdio.h>
32 #include "ufind.h"
33 #include "assert.h"
34
35
36 enum uf_type {uf_ecr,uf_link};
37 typedef enum uf_type uf_type;
38
39 struct uf_element
40 {
41   uf_type type;
42   int rank;
43   void *info;
44   struct uf_element *link;
45 };
46
47 struct uf_element *new_uf_element(region r, void *info)
48 {
49   struct uf_element *result;
50
51   result = ralloc(r, struct uf_element);
52
53   result->type = uf_ecr;
54   result->rank = 0;
55   result->info = info;
56   result->link = NULL;
57
58   return result;
59 }
60
61 static struct uf_element *find(struct uf_element *e)
62 {
63
64   if (e->type == uf_ecr)
65     return e;
66   
67   else if (e->link->type == uf_link)
68     {
69       struct uf_element *temp = e->link;
70         
71       e->link = e->link->link;
72
73       return find(temp);
74     }
75
76   else
77     return e->link;
78 }
79
80 bool uf_union(struct uf_element *a, struct uf_element *b)
81 {
82   struct uf_element *e1 = find(a);
83   struct uf_element *e2 = find(b);
84
85   if ( e1 == e2 )
86     return FALSE;
87
88  else if (e1->rank < e2->rank)
89     {
90       e1->type = uf_link;
91       e1->link = e2;
92
93       return TRUE;
94     }
95
96   else if (e1->rank > e2->rank)
97     {
98       e2->type = uf_link;
99       e2->link = e1;
100
101       return TRUE;
102     }
103   
104   else 
105     {
106       e2->rank++;
107       
108       e1->type = uf_link;
109       e1->link = e2;
110
111       return TRUE;
112     }
113
114 }
115
116 bool uf_unify(combine_fn_ptr combine,
117               struct uf_element *a, struct uf_element *b)
118 {
119   struct uf_element *e1 = find(a);
120   struct uf_element *e2 = find(b);
121
122   if ( e1 == e2 )
123     return FALSE;
124
125   else if (e1->rank < e2->rank)
126     {
127       e2->info = combine(e2->info,e1->info);
128       e1->type = uf_link;
129       e1->link = e2;
130      
131       return TRUE;
132     }
133
134   else if (e1->rank > e2->rank)
135     {
136       e1->info = combine(e1->info,e2->info);
137       e2->type = uf_link;
138       e2->link = e1;
139
140       return TRUE;
141     }
142   
143   else 
144     {
145       e2->info = combine(e2->info, e1->info);
146
147       e2->rank++;
148       e1->type = uf_link;
149       e1->link = e2;
150
151       return TRUE;
152     }
153 }
154
155
156
157 void *uf_get_info(struct uf_element *e)
158 {
159   return find(e)->info;
160 }
161
162
163 bool uf_eq(struct uf_element *e1,struct uf_element *e2)
164 {
165   return (find(e1) == find(e2));
166 }
167
168 void uf_update(struct uf_element *e,uf_info i)
169 {
170   find(e)->info = i;
171 }
172
173
174
175
176
177