partition.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* List implementation of a partition of consecutive integers.
  2. Copyright (C) 2000, 2001 Free Software Foundation, Inc.
  3. Contributed by CodeSourcery, LLC.
  4. This file is part of GNU CC.
  5. GNU CC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU CC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU CC; see the file COPYING. If not, write to
  15. the Free Software Foundation, 51 Franklin Street - Fifth Floor,
  16. Boston, MA 02110-1301, USA. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #ifdef HAVE_STDLIB_H
  21. #include <stdlib.h>
  22. #endif
  23. #ifdef HAVE_STRING_H
  24. #include <string.h>
  25. #endif
  26. #include "libiberty.h"
  27. #include "partition.h"
  28. static int elem_compare (const void *, const void *);
  29. /* Creates a partition of NUM_ELEMENTS elements. Initially each
  30. element is in a class by itself. */
  31. partition
  32. partition_new (int num_elements)
  33. {
  34. int e;
  35. partition part = (partition)
  36. xmalloc (sizeof (struct partition_def) +
  37. (num_elements - 1) * sizeof (struct partition_elem));
  38. part->num_elements = num_elements;
  39. for (e = 0; e < num_elements; ++e)
  40. {
  41. part->elements[e].class_element = e;
  42. part->elements[e].next = &(part->elements[e]);
  43. part->elements[e].class_count = 1;
  44. }
  45. return part;
  46. }
  47. /* Freeds a partition. */
  48. void
  49. partition_delete (partition part)
  50. {
  51. free (part);
  52. }
  53. /* Unites the classes containing ELEM1 and ELEM2 into a single class
  54. of partition PART. If ELEM1 and ELEM2 are already in the same
  55. class, does nothing. Returns the canonical element of the
  56. resulting union class. */
  57. int
  58. partition_union (partition part, int elem1, int elem2)
  59. {
  60. struct partition_elem *elements = part->elements;
  61. struct partition_elem *e1;
  62. struct partition_elem *e2;
  63. struct partition_elem *p;
  64. struct partition_elem *old_next;
  65. /* The canonical element of the resulting union class. */
  66. int class_element = elements[elem1].class_element;
  67. /* If they're already in the same class, do nothing. */
  68. if (class_element == elements[elem2].class_element)
  69. return class_element;
  70. /* Make sure ELEM1 is in the larger class of the two. If not, swap
  71. them. This way we always scan the shorter list. */
  72. if (elements[elem1].class_count < elements[elem2].class_count)
  73. {
  74. int temp = elem1;
  75. elem1 = elem2;
  76. elem2 = temp;
  77. class_element = elements[elem1].class_element;
  78. }
  79. e1 = &(elements[elem1]);
  80. e2 = &(elements[elem2]);
  81. /* Keep a count of the number of elements in the list. */
  82. elements[class_element].class_count
  83. += elements[e2->class_element].class_count;
  84. /* Update the class fields in elem2's class list. */
  85. e2->class_element = class_element;
  86. for (p = e2->next; p != e2; p = p->next)
  87. p->class_element = class_element;
  88. /* Splice ELEM2's class list into ELEM1's. These are circular
  89. lists. */
  90. old_next = e1->next;
  91. e1->next = e2->next;
  92. e2->next = old_next;
  93. return class_element;
  94. }
  95. /* Compare elements ELEM1 and ELEM2 from array of integers, given a
  96. pointer to each. Used to qsort such an array. */
  97. static int
  98. elem_compare (const void *elem1, const void *elem2)
  99. {
  100. int e1 = * (const int *) elem1;
  101. int e2 = * (const int *) elem2;
  102. if (e1 < e2)
  103. return -1;
  104. else if (e1 > e2)
  105. return 1;
  106. else
  107. return 0;
  108. }
  109. /* Prints PART to the file pointer FP. The elements of each
  110. class are sorted. */
  111. void
  112. partition_print (partition part, FILE *fp)
  113. {
  114. char *done;
  115. int num_elements = part->num_elements;
  116. struct partition_elem *elements = part->elements;
  117. int *class_elements;
  118. int e;
  119. /* Flag the elements we've already printed. */
  120. done = (char *) xmalloc (num_elements);
  121. memset (done, 0, num_elements);
  122. /* A buffer used to sort elements in a class. */
  123. class_elements = (int *) xmalloc (num_elements * sizeof (int));
  124. fputc ('[', fp);
  125. for (e = 0; e < num_elements; ++e)
  126. /* If we haven't printed this element, print its entire class. */
  127. if (! done[e])
  128. {
  129. int c = e;
  130. int count = elements[elements[e].class_element].class_count;
  131. int i;
  132. /* Collect the elements in this class. */
  133. for (i = 0; i < count; ++i) {
  134. class_elements[i] = c;
  135. done[c] = 1;
  136. c = elements[c].next - elements;
  137. }
  138. /* Sort them. */
  139. qsort ((void *) class_elements, count, sizeof (int), elem_compare);
  140. /* Print them. */
  141. fputc ('(', fp);
  142. for (i = 0; i < count; ++i)
  143. fprintf (fp, i == 0 ? "%d" : " %d", class_elements[i]);
  144. fputc (')', fp);
  145. }
  146. fputc (']', fp);
  147. free (class_elements);
  148. free (done);
  149. }