sparseset.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* SparseSet implementation.
  2. Copyright (C) 2007-2015 Free Software Foundation, Inc.
  3. Contributed by Peter Bergner <bergner@vnet.ibm.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "sparseset.h"
  19. /* Allocate and clear a n_elms SparseSet. */
  20. sparseset
  21. sparseset_alloc (SPARSESET_ELT_TYPE n_elms)
  22. {
  23. unsigned int n_bytes = sizeof (struct sparseset_def)
  24. + ((n_elms - 1) * 2 * sizeof (SPARSESET_ELT_TYPE));
  25. sparseset set = XNEWVAR (struct sparseset_def, n_bytes);
  26. /* Mark the sparseset as defined to silence some valgrind uninitialized
  27. read errors when accessing set->sparse[n] when "n" is not, and never has
  28. been, in the set. These uninitialized reads are expected, by design and
  29. harmless. */
  30. VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (set, n_bytes));
  31. set->dense = &(set->elms[0]);
  32. set->sparse = &(set->elms[n_elms]);
  33. set->size = n_elms;
  34. sparseset_clear (set);
  35. return set;
  36. }
  37. /* Low level routine not meant for use outside of sparseset.[ch].
  38. Assumes idx1 < s->members and idx2 < s->members. */
  39. static inline void
  40. sparseset_swap (sparseset s, SPARSESET_ELT_TYPE idx1, SPARSESET_ELT_TYPE idx2)
  41. {
  42. SPARSESET_ELT_TYPE tmp = s->dense[idx2];
  43. sparseset_insert_bit (s, s->dense[idx1], idx2);
  44. sparseset_insert_bit (s, tmp, idx1);
  45. }
  46. /* Operation: S = S - {e}
  47. Delete e from the set S if it is a member of S. */
  48. void
  49. sparseset_clear_bit (sparseset s, SPARSESET_ELT_TYPE e)
  50. {
  51. if (sparseset_bit_p (s, e))
  52. {
  53. SPARSESET_ELT_TYPE idx = s->sparse[e];
  54. SPARSESET_ELT_TYPE iter = s->iter;
  55. SPARSESET_ELT_TYPE mem = s->members - 1;
  56. /* If we are iterating over this set and we want to delete a
  57. member we've already visited, then we swap the element we
  58. want to delete with the element at the current iteration
  59. index so that it plays well together with the code below
  60. that actually removes the element. */
  61. if (s->iterating && idx <= iter)
  62. {
  63. if (idx < iter)
  64. {
  65. sparseset_swap (s, idx, iter);
  66. idx = iter;
  67. }
  68. s->iter_inc = 0;
  69. }
  70. /* Replace the element we want to delete with the last element
  71. in the dense array and then decrement s->members, effectively
  72. removing the element we want to delete. */
  73. sparseset_insert_bit (s, s->dense[mem], idx);
  74. s->members = mem;
  75. }
  76. }
  77. /* Operation: D = S
  78. Restrictions: none. */
  79. void
  80. sparseset_copy (sparseset d, sparseset s)
  81. {
  82. SPARSESET_ELT_TYPE i;
  83. if (d == s)
  84. return;
  85. sparseset_clear (d);
  86. for (i = 0; i < s->members; i++)
  87. sparseset_insert_bit (d, s->dense[i], i);
  88. d->members = s->members;
  89. }
  90. /* Operation: D = A & B.
  91. Restrictions: none. */
  92. void
  93. sparseset_and (sparseset d, sparseset a, sparseset b)
  94. {
  95. SPARSESET_ELT_TYPE e;
  96. if (a == b)
  97. {
  98. if (d != a)
  99. sparseset_copy (d, a);
  100. return;
  101. }
  102. if (d == a || d == b)
  103. {
  104. sparseset s = (d == a) ? b : a;
  105. EXECUTE_IF_SET_IN_SPARSESET (d, e)
  106. if (!sparseset_bit_p (s, e))
  107. sparseset_clear_bit (d, e);
  108. }
  109. else
  110. {
  111. sparseset sml, lrg;
  112. if (sparseset_cardinality (a) < sparseset_cardinality (b))
  113. {
  114. sml = a;
  115. lrg = b;
  116. }
  117. else
  118. {
  119. sml = b;
  120. lrg = a;
  121. }
  122. sparseset_clear (d);
  123. EXECUTE_IF_SET_IN_SPARSESET (sml, e)
  124. if (sparseset_bit_p (lrg, e))
  125. sparseset_set_bit (d, e);
  126. }
  127. }
  128. /* Operation: D = A & ~B.
  129. Restrictions: D != B, unless D == A == B. */
  130. void
  131. sparseset_and_compl (sparseset d, sparseset a, sparseset b)
  132. {
  133. SPARSESET_ELT_TYPE e;
  134. if (a == b)
  135. {
  136. sparseset_clear (d);
  137. return;
  138. }
  139. gcc_assert (d != b);
  140. if (d == a)
  141. {
  142. if (sparseset_cardinality (d) < sparseset_cardinality (b))
  143. {
  144. EXECUTE_IF_SET_IN_SPARSESET (d, e)
  145. if (sparseset_bit_p (b, e))
  146. sparseset_clear_bit (d, e);
  147. }
  148. else
  149. {
  150. EXECUTE_IF_SET_IN_SPARSESET (b, e)
  151. sparseset_clear_bit (d, e);
  152. }
  153. }
  154. else
  155. {
  156. sparseset_clear (d);
  157. EXECUTE_IF_SET_IN_SPARSESET (a, e)
  158. if (!sparseset_bit_p (b, e))
  159. sparseset_set_bit (d, e);
  160. }
  161. }
  162. /* Operation: D = A | B.
  163. Restrictions: none. */
  164. void
  165. sparseset_ior (sparseset d, sparseset a, sparseset b)
  166. {
  167. SPARSESET_ELT_TYPE e;
  168. if (a == b)
  169. sparseset_copy (d, a);
  170. else if (d == b)
  171. {
  172. EXECUTE_IF_SET_IN_SPARSESET (a, e)
  173. sparseset_set_bit (d, e);
  174. }
  175. else
  176. {
  177. if (d != a)
  178. sparseset_copy (d, a);
  179. EXECUTE_IF_SET_IN_SPARSESET (b, e)
  180. sparseset_set_bit (d, e);
  181. }
  182. }
  183. /* Operation: A == B
  184. Restrictions: none. */
  185. bool
  186. sparseset_equal_p (sparseset a, sparseset b)
  187. {
  188. SPARSESET_ELT_TYPE e;
  189. if (a == b)
  190. return true;
  191. if (sparseset_cardinality (a) != sparseset_cardinality (b))
  192. return false;
  193. EXECUTE_IF_SET_IN_SPARSESET (a, e)
  194. if (!sparseset_bit_p (b, e))
  195. return false;
  196. return true;
  197. }