sparseset.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #ifndef GCC_SPARSESET_H
  17. #define GCC_SPARSESET_H
  18. /* Implementation of the Briggs and Torczon sparse set representation.
  19. The sparse set representation was first published in:
  20. "An Efficient Representation for Sparse Sets",
  21. ACM LOPLAS, Vol. 2, Nos. 1-4, March-December 1993, Pages 59-69.
  22. The sparse set representation is suitable for integer sets with a
  23. fixed-size universe. Two vectors are used to store the members of
  24. the set. If an element I is in the set, then sparse[I] is the
  25. index of I in the dense vector, and dense[sparse[I]] == I. The dense
  26. vector works like a stack. The size of the stack is the cardinality
  27. of the set.
  28. The following operations can be performed in O(1) time:
  29. * clear : sparseset_clear
  30. * cardinality : sparseset_cardinality
  31. * set_size : sparseset_size
  32. * member_p : sparseset_bit_p
  33. * add_member : sparseset_set_bit
  34. * remove_member : sparseset_clear_bit
  35. * choose_one : sparseset_pop
  36. Additionally, the sparse set representation supports enumeration of
  37. the members in O(N) time, where n is the number of members in the set.
  38. The members of the set are stored cache-friendly in the dense vector.
  39. This makes it a competitive choice for iterating over relatively sparse
  40. sets requiring operations:
  41. * forall : EXECUTE_IF_SET_IN_SPARSESET
  42. * set_copy : sparseset_copy
  43. * set_intersection : sparseset_and
  44. * set_union : sparseset_ior
  45. * set_difference : sparseset_and_compl
  46. * set_disjuction : (not implemented)
  47. * set_compare : sparseset_equal_p
  48. NB: It is OK to use remove_member during EXECUTE_IF_SET_IN_SPARSESET.
  49. The iterator is updated for it.
  50. Based on the efficiency of these operations, this representation of
  51. sparse sets will often be superior to alternatives such as simple
  52. bitmaps, linked-list bitmaps, array bitmaps, balanced binary trees,
  53. hash tables, linked lists, etc., if the set is sufficiently sparse.
  54. In the LOPLAS paper the cut-off point where sparse sets became faster
  55. than simple bitmaps (see sbitmap.h) when N / U < 64 (where U is the
  56. size of the universe of the set).
  57. Because the set universe is fixed, the set cannot be resized. For
  58. sparse sets with initially unknown size, linked-list bitmaps are a
  59. better choice, see bitmap.h.
  60. Sparse sets storage requirements are relatively large: O(U) with a
  61. larger constant than sbitmaps (if the storage requirement for an
  62. sbitmap with universe U is S, then the storage required for a sparse
  63. set for the same universe are 2*HOST_BITS_PER_WIDEST_FAST_INT * S).
  64. Accessing the sparse vector is not very cache-friendly, but iterating
  65. over the members in the set is cache-friendly because only the dense
  66. vector is used. */
  67. /* Data Structure used for the SparseSet representation. */
  68. #define SPARSESET_ELT_BITS ((unsigned) HOST_BITS_PER_WIDEST_FAST_INT)
  69. #define SPARSESET_ELT_TYPE unsigned HOST_WIDEST_FAST_INT
  70. typedef struct sparseset_def
  71. {
  72. SPARSESET_ELT_TYPE *dense; /* Dense array. */
  73. SPARSESET_ELT_TYPE *sparse; /* Sparse array. */
  74. SPARSESET_ELT_TYPE members; /* Number of elements. */
  75. SPARSESET_ELT_TYPE size; /* Maximum number of elements. */
  76. SPARSESET_ELT_TYPE iter; /* Iterator index. */
  77. unsigned char iter_inc; /* Iteration increment amount. */
  78. bool iterating;
  79. SPARSESET_ELT_TYPE elms[2]; /* Combined dense and sparse arrays. */
  80. } *sparseset;
  81. #define sparseset_free(MAP) free(MAP)
  82. extern sparseset sparseset_alloc (SPARSESET_ELT_TYPE n_elms);
  83. extern void sparseset_clear_bit (sparseset, SPARSESET_ELT_TYPE);
  84. extern void sparseset_copy (sparseset, sparseset);
  85. extern void sparseset_and (sparseset, sparseset, sparseset);
  86. extern void sparseset_and_compl (sparseset, sparseset, sparseset);
  87. extern void sparseset_ior (sparseset, sparseset, sparseset);
  88. extern bool sparseset_equal_p (sparseset, sparseset);
  89. /* Operation: S = {}
  90. Clear the set of all elements. */
  91. static inline void
  92. sparseset_clear (sparseset s)
  93. {
  94. s->members = 0;
  95. s->iterating = false;
  96. }
  97. /* Return the number of elements currently in the set. */
  98. static inline SPARSESET_ELT_TYPE
  99. sparseset_cardinality (sparseset s)
  100. {
  101. return s->members;
  102. }
  103. /* Return the maximum number of elements this set can hold. */
  104. static inline SPARSESET_ELT_TYPE
  105. sparseset_size (sparseset s)
  106. {
  107. return s->size;
  108. }
  109. /* Return true if e is a member of the set S, otherwise return false. */
  110. static inline bool
  111. sparseset_bit_p (sparseset s, SPARSESET_ELT_TYPE e)
  112. {
  113. SPARSESET_ELT_TYPE idx;
  114. gcc_checking_assert (e < s->size);
  115. idx = s->sparse[e];
  116. return idx < s->members && s->dense[idx] == e;
  117. }
  118. /* Low level insertion routine not meant for use outside of sparseset.[ch].
  119. Assumes E is valid and not already a member of the set S. */
  120. static inline void
  121. sparseset_insert_bit (sparseset s, SPARSESET_ELT_TYPE e, SPARSESET_ELT_TYPE idx)
  122. {
  123. s->sparse[e] = idx;
  124. s->dense[idx] = e;
  125. }
  126. /* Operation: S = S + {e}
  127. Insert E into the set S, if it isn't already a member. */
  128. static inline void
  129. sparseset_set_bit (sparseset s, SPARSESET_ELT_TYPE e)
  130. {
  131. if (!sparseset_bit_p (s, e))
  132. sparseset_insert_bit (s, e, s->members++);
  133. }
  134. /* Return and remove the last member added to the set S. */
  135. static inline SPARSESET_ELT_TYPE
  136. sparseset_pop (sparseset s)
  137. {
  138. SPARSESET_ELT_TYPE mem = s->members;
  139. gcc_checking_assert (mem != 0);
  140. s->members = mem - 1;
  141. return s->dense[s->members];
  142. }
  143. static inline void
  144. sparseset_iter_init (sparseset s)
  145. {
  146. s->iter = 0;
  147. s->iter_inc = 1;
  148. s->iterating = true;
  149. }
  150. static inline bool
  151. sparseset_iter_p (sparseset s)
  152. {
  153. if (s->iterating && s->iter < s->members)
  154. return true;
  155. else
  156. return s->iterating = false;
  157. }
  158. static inline SPARSESET_ELT_TYPE
  159. sparseset_iter_elm (sparseset s)
  160. {
  161. return s->dense[s->iter];
  162. }
  163. static inline void
  164. sparseset_iter_next (sparseset s)
  165. {
  166. s->iter += s->iter_inc;
  167. s->iter_inc = 1;
  168. }
  169. #define EXECUTE_IF_SET_IN_SPARSESET(SPARSESET, ITER) \
  170. for (sparseset_iter_init (SPARSESET); \
  171. sparseset_iter_p (SPARSESET) \
  172. && (((ITER) = sparseset_iter_elm (SPARSESET)) || 1); \
  173. sparseset_iter_next (SPARSESET))
  174. #endif /* GCC_SPARSESET_H */