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 static int elem_compare
PARAMS ((const void *, const void *));
39 /* Creates a partition of NUM_ELEMENTS elements. Initially each
40 element is in a class by itself. */
43 partition_new (num_elements
)
48 partition part
= (partition
)
49 xmalloc (sizeof (struct partition_def
) +
50 (num_elements
- 1) * sizeof (struct partition_elem
));
51 part
->num_elements
= num_elements
;
52 for (e
= 0; e
< num_elements
; ++e
)
54 part
->elements
[e
].class_element
= e
;
55 part
->elements
[e
].next
= &(part
->elements
[e
]);
56 part
->elements
[e
].class_count
= 1;
62 /* Freeds a partition. */
65 partition_delete (part
)
71 /* Unites the classes containing ELEM1 and ELEM2 into a single class
72 of partition PART. If ELEM1 and ELEM2 are already in the same
73 class, does nothing. Returns the canonical element of the
74 resulting union class. */
77 partition_union (part
, elem1
, elem2
)
82 struct partition_elem
*elements
= part
->elements
;
83 struct partition_elem
*e1
;
84 struct partition_elem
*e2
;
85 struct partition_elem
*p
;
86 struct partition_elem
*old_next
;
87 /* The canonical element of the resulting union class. */
88 int class_element
= elements
[elem1
].class_element
;
90 /* If they're already in the same class, do nothing. */
91 if (class_element
== elements
[elem2
].class_element
)
94 /* Make sure ELEM1 is in the larger class of the two. If not, swap
95 them. This way we always scan the shorter list. */
96 if (elements
[elem1
].class_count
< elements
[elem2
].class_count
)
101 class_element
= elements
[elem1
].class_element
;
104 e1
= &(elements
[elem1
]);
105 e2
= &(elements
[elem2
]);
107 /* Keep a count of the number of elements in the list. */
108 elements
[class_element
].class_count
109 += elements
[e2
->class_element
].class_count
;
111 /* Update the class fields in elem2's class list. */
112 e2
->class_element
= class_element
;
113 for (p
= e2
->next
; p
!= e2
; p
= p
->next
)
114 p
->class_element
= class_element
;
116 /* Splice ELEM2's class list into ELEM1's. These are circular
122 return class_element
;
125 /* Compare elements ELEM1 and ELEM2 from array of integers, given a
126 pointer to each. Used to qsort such an array. */
129 elem_compare (elem1
, elem2
)
133 int e1
= * (const int *) elem1
;
134 int e2
= * (const int *) elem2
;
143 /* Prints PART to the file pointer FP. The elements of each
147 partition_print (part
, fp
)
152 int num_elements
= part
->num_elements
;
153 struct partition_elem
*elements
= part
->elements
;
157 /* Flag the elements we've already printed. */
158 done
= (char *) xmalloc (num_elements
);
159 memset (done
, 0, num_elements
);
161 /* A buffer used to sort elements in a class. */
162 class_elements
= (int *) xmalloc (num_elements
* sizeof (int));
165 for (e
= 0; e
< num_elements
; ++e
)
166 /* If we haven't printed this element, print its entire class. */
170 int count
= elements
[elements
[e
].class_element
].class_count
;
173 /* Collect the elements in this class. */
174 for (i
= 0; i
< count
; ++i
) {
175 class_elements
[i
] = c
;
177 c
= elements
[c
].next
- elements
;
180 qsort ((void *) class_elements
, count
, sizeof (int), elem_compare
);
183 for (i
= 0; i
< count
; ++i
)
184 fprintf (fp
, i
== 0 ? "%d" : " %d", class_elements
[i
]);