00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifdef HAVE_CONFIG_H
00023 #include "config.h"
00024 #endif
00025
00026 #ifdef HAVE_STDLIB_H
00027 #include <stdlib.h>
00028 #endif
00029
00030 #ifdef HAVE_STRING_H
00031 #include <string.h>
00032 #endif
00033
00034 #include "libiberty.h"
00035 #include "partition.h"
00036
00037 static int elem_compare PARAMS ((const void *, const void *));
00038
00039
00040
00041
00042 partition
00043 partition_new (num_elements)
00044 int num_elements;
00045 {
00046 int e;
00047
00048 partition part = (partition)
00049 xmalloc (sizeof (struct partition_def) +
00050 (num_elements - 1) * sizeof (struct partition_elem));
00051 part->num_elements = num_elements;
00052 for (e = 0; e < num_elements; ++e)
00053 {
00054 part->elements[e].class_element = e;
00055 part->elements[e].next = &(part->elements[e]);
00056 part->elements[e].class_count = 1;
00057 }
00058
00059 return part;
00060 }
00061
00062
00063
00064 void
00065 partition_delete (part)
00066 partition part;
00067 {
00068 free (part);
00069 }
00070
00071
00072
00073
00074
00075
00076 int
00077 partition_union (part, elem1, elem2)
00078 partition part;
00079 int elem1;
00080 int elem2;
00081 {
00082 struct partition_elem *elements = part->elements;
00083 struct partition_elem *e1;
00084 struct partition_elem *e2;
00085 struct partition_elem *p;
00086 struct partition_elem *old_next;
00087
00088 int class_element = elements[elem1].class_element;
00089
00090
00091 if (class_element == elements[elem2].class_element)
00092 return class_element;
00093
00094
00095
00096 if (elements[elem1].class_count < elements[elem2].class_count)
00097 {
00098 int temp = elem1;
00099 elem1 = elem2;
00100 elem2 = temp;
00101 class_element = elements[elem1].class_element;
00102 }
00103
00104 e1 = &(elements[elem1]);
00105 e2 = &(elements[elem2]);
00106
00107
00108 elements[class_element].class_count
00109 += elements[e2->class_element].class_count;
00110
00111
00112 e2->class_element = class_element;
00113 for (p = e2->next; p != e2; p = p->next)
00114 p->class_element = class_element;
00115
00116
00117
00118 old_next = e1->next;
00119 e1->next = e2->next;
00120 e2->next = old_next;
00121
00122 return class_element;
00123 }
00124
00125
00126
00127
00128 static int
00129 elem_compare (elem1, elem2)
00130 const void *elem1;
00131 const void *elem2;
00132 {
00133 int e1 = * (const int *) elem1;
00134 int e2 = * (const int *) elem2;
00135 if (e1 < e2)
00136 return -1;
00137 else if (e1 > e2)
00138 return 1;
00139 else
00140 return 0;
00141 }
00142
00143
00144
00145
00146 void
00147 partition_print (part, fp)
00148 partition part;
00149 FILE *fp;
00150 {
00151 char *done;
00152 int num_elements = part->num_elements;
00153 struct partition_elem *elements = part->elements;
00154 int *class_elements;
00155 int e;
00156
00157
00158 done = (char *) xmalloc (num_elements);
00159 memset (done, 0, num_elements);
00160
00161
00162 class_elements = (int *) xmalloc (num_elements * sizeof (int));
00163
00164 fputc ('[', fp);
00165 for (e = 0; e < num_elements; ++e)
00166
00167 if (! done[e])
00168 {
00169 int c = e;
00170 int count = elements[elements[e].class_element].class_count;
00171 int i;
00172
00173
00174 for (i = 0; i < count; ++i) {
00175 class_elements[i] = c;
00176 done[c] = 1;
00177 c = elements[c].next - elements;
00178 }
00179
00180 qsort ((void *) class_elements, count, sizeof (int), elem_compare);
00181
00182 fputc ('(', fp);
00183 for (i = 0; i < count; ++i)
00184 fprintf (fp, i == 0 ? "%d" : " %d", class_elements[i]);
00185 fputc (')', fp);
00186 }
00187 fputc (']', fp);
00188
00189 free (done);
00190 }
00191