1 /* List implentation of a partition of consecutive integers.
2 Copyright (C) 2000 Free Software Foundation, Inc.
3 Contributed by CodeSourcery, LLC.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
34 #include "libiberty.h"
35 #include "partition.h"
37 /* Creates a partition of NUM_ELEMENTS elements. Initially each
38 element is in a class by itself. */
41 partition_new (num_elements)
46 partition part = (partition)
47 xmalloc (sizeof (struct partition_def) +
48 (num_elements - 1) * sizeof (struct partition_elem));
49 part->num_elements = num_elements;
50 for (e = 0; e < num_elements; ++e)
52 part->elements[e].class_element = e;
53 part->elements[e].next = &(part->elements[e]);
54 part->elements[e].class_count = 1;
60 /* Freeds a partition. */
63 partition_delete (part)
69 /* Unites the classes containing ELEM1 and ELEM2 into a single class
70 of partition PART. If ELEM1 and ELEM2 are already in the same
71 class, does nothing. Returns the canonical element of the
72 resulting union class. */
75 partition_union (part, elem1, elem2)
80 struct partition_elem *elements = part->elements;
81 struct partition_elem *e1;
82 struct partition_elem *e2;
83 struct partition_elem *p;
84 struct partition_elem *old_next;
85 /* The canonical element of the resulting union class. */
86 int class_element = elements[elem1].class_element;
88 /* If they're already in the same class, do nothing. */
89 if (class_element == elements[elem2].class_element)
92 /* Make sure ELEM1 is in the larger class of the two. If not, swap
93 them. This way we always scan the shorter list. */
94 if (elements[elem1].class_count < elements[elem2].class_count)
99 class_element = elements[elem1].class_element;
102 e1 = &(elements[elem1]);
103 e2 = &(elements[elem2]);
105 /* Keep a count of the number of elements in the list. */
106 elements[class_element].class_count
107 += elements[e2->class_element].class_count;
109 /* Update the class fields in elem2's class list. */
110 e2->class_element = class_element;
111 for (p = e2->next; p != e2; p = p->next)
112 p->class_element = class_element;
114 /* Splice ELEM2's class list into ELEM1's. These are circular
120 return class_element;
123 /* Compare elements ELEM1 and ELEM2 from array of integers, given a
124 pointer to each. Used to qsort such an array. */
127 elem_compare (elem1, elem2)
131 int e1 = * (int *) elem1;
132 int e2 = * (int *) elem2;
141 /* Prints PART to the file pointer FP. The elements of each
145 partition_print (part, fp)
150 int num_elements = part->num_elements;
151 struct partition_elem *elements = part->elements;
155 /* Flag the elements we've already printed. */
156 done = (char *) xmalloc (num_elements);
157 memset (done, 0, num_elements);
159 /* A buffer used to sort elements in a class. */
160 class_elements = (int *) xmalloc (num_elements * sizeof (int));
163 for (e = 0; e < num_elements; ++e)
164 /* If we haven't printed this element, print its entire class. */
168 int count = elements[elements[e].class_element].class_count;
171 /* Collect the elements in this class. */
172 for (i = 0; i < count; ++i) {
173 class_elements[i] = c;
175 c = elements[c].next - elements;
178 qsort ((void *) class_elements, count, sizeof (int), &elem_compare);
181 for (i = 0; i < count; ++i)
182 fprintf (fp, i == 0 ? "%d" : " %d", class_elements[i]);