* config/s390/s390-protos.h (preferred_la_operand_p): Adapt prototype.
[official-gcc.git] / libbanshee / engine / ufind.c
blob545171812bb89ed08c39cade9a7718f68e6b668f
1 /*
2 * Copyright (c) 2000-2001
3 * The Regents of the University of California. All rights reserved.
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.
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.
31 #include <stdio.h>
32 #include "ufind.h"
33 #include "assert.h"
36 enum uf_type {uf_ecr,uf_link};
37 typedef enum uf_type uf_type;
39 struct uf_element
41 uf_type type;
42 int rank;
43 void *info;
44 struct uf_element *link;
47 struct uf_element *new_uf_element(region r, void *info)
49 struct uf_element *result;
51 result = ralloc(r, struct uf_element);
53 result->type = uf_ecr;
54 result->rank = 0;
55 result->info = info;
56 result->link = NULL;
58 return result;
61 static struct uf_element *find(struct uf_element *e)
64 if (e->type == uf_ecr)
65 return e;
67 else if (e->link->type == uf_link)
69 struct uf_element *temp = e->link;
71 e->link = e->link->link;
73 return find(temp);
76 else
77 return e->link;
80 bool uf_union(struct uf_element *a, struct uf_element *b)
82 struct uf_element *e1 = find(a);
83 struct uf_element *e2 = find(b);
85 if ( e1 == e2 )
86 return FALSE;
88 else if (e1->rank < e2->rank)
90 e1->type = uf_link;
91 e1->link = e2;
93 return TRUE;
96 else if (e1->rank > e2->rank)
98 e2->type = uf_link;
99 e2->link = e1;
101 return TRUE;
104 else
106 e2->rank++;
108 e1->type = uf_link;
109 e1->link = e2;
111 return TRUE;
116 bool uf_unify(combine_fn_ptr combine,
117 struct uf_element *a, struct uf_element *b)
119 struct uf_element *e1 = find(a);
120 struct uf_element *e2 = find(b);
122 if ( e1 == e2 )
123 return FALSE;
125 else if (e1->rank < e2->rank)
127 e2->info = combine(e2->info,e1->info);
128 e1->type = uf_link;
129 e1->link = e2;
131 return TRUE;
134 else if (e1->rank > e2->rank)
136 e1->info = combine(e1->info,e2->info);
137 e2->type = uf_link;
138 e2->link = e1;
140 return TRUE;
143 else
145 e2->info = combine(e2->info, e1->info);
147 e2->rank++;
148 e1->type = uf_link;
149 e1->link = e2;
151 return TRUE;
157 void *uf_get_info(struct uf_element *e)
159 return find(e)->info;
163 bool uf_eq(struct uf_element *e1,struct uf_element *e2)
165 return (find(e1) == find(e2));
168 void uf_update(struct uf_element *e,uf_info i)
170 find(e)->info = i;