bitset.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* Generic bitsets.
  2. Copyright (C) 2002-2004, 2009-2013 Free Software Foundation, Inc.
  3. Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef _BITSET_H
  15. #define _BITSET_H
  16. /* This file is the public interface to the bitset abstract data type.
  17. Only use the functions and macros defined in this file. */
  18. #include "bbitset.h"
  19. #include "obstack.h"
  20. #include <stdio.h>
  21. #if USE_UNLOCKED_IO
  22. # include "unlocked-io.h"
  23. #endif
  24. /* Attributes used to select a bitset implementation. */
  25. enum bitset_attr {BITSET_FIXED = 1, /* Bitset size fixed. */
  26. BITSET_VARIABLE = 2, /* Bitset size variable. */
  27. BITSET_DENSE = 4, /* Bitset dense. */
  28. BITSET_SPARSE = 8, /* Bitset sparse. */
  29. BITSET_FRUGAL = 16, /* Prefer most compact. */
  30. BITSET_GREEDY = 32}; /* Prefer fastest at memory expense. */
  31. typedef unsigned int bitset_attrs;
  32. /* The contents of the union should be considered to be private.
  33. While I would like to make this union opaque, it needs to be
  34. visible for the inline bit set/test functions, and for delegation
  35. to the proper implementation. */
  36. union bitset_union
  37. {
  38. /* This must be the first member of every other structure that is a
  39. member of this union. */
  40. struct bbitset_struct b; /* Base bitset data. */
  41. struct abitset_struct
  42. {
  43. struct bbitset_struct b;
  44. bitset_word words[1]; /* The array of bits. */
  45. } a;
  46. struct ebitset_struct
  47. {
  48. struct bbitset_struct b;
  49. bitset_windex size; /* Number of elements. */
  50. struct ebitset_elt_struct **elts; /* Expanding array of ptrs to elts. */
  51. } e;
  52. struct lbitset_struct
  53. {
  54. struct bbitset_struct b;
  55. struct lbitset_elt_struct *head; /* First element in linked list. */
  56. struct lbitset_elt_struct *tail; /* Last element in linked list. */
  57. } l;
  58. struct bitset_stats_struct
  59. {
  60. struct bbitset_struct b;
  61. bitset bset;
  62. } s;
  63. struct vbitset_struct
  64. {
  65. struct bbitset_struct b;
  66. bitset_windex size; /* Allocated size of array. */
  67. } v;
  68. };
  69. /* The contents of this structure should be considered private.
  70. It is used for iterating over set bits. */
  71. typedef struct
  72. {
  73. bitset_bindex list[BITSET_LIST_SIZE];
  74. bitset_bindex next;
  75. bitset_bindex num;
  76. bitset_bindex i;
  77. } bitset_iterator;
  78. /* Return bytes required for bitset of desired type and size. */
  79. extern size_t bitset_bytes (enum bitset_type, bitset_bindex);
  80. /* Initialise a bitset with desired type and size. */
  81. extern bitset bitset_init (bitset, bitset_bindex, enum bitset_type);
  82. /* Select an implementation type based on the desired bitset size
  83. and attributes. */
  84. extern enum bitset_type bitset_type_choose (bitset_bindex, bitset_attrs);
  85. /* Create a bitset of desired type and size. The bitset is zeroed. */
  86. extern bitset bitset_alloc (bitset_bindex, enum bitset_type);
  87. /* Free bitset. */
  88. extern void bitset_free (bitset);
  89. /* Create a bitset of desired type and size using an obstack. The
  90. bitset is zeroed. */
  91. extern bitset bitset_obstack_alloc (struct obstack *bobstack,
  92. bitset_bindex, enum bitset_type);
  93. /* Free bitset allocated on obstack. */
  94. extern void bitset_obstack_free (bitset);
  95. /* Create a bitset of desired size and attributes. The bitset is zeroed. */
  96. extern bitset bitset_create (bitset_bindex, bitset_attrs);
  97. /* Return bitset type. */
  98. extern enum bitset_type bitset_type_get (bitset);
  99. /* Return bitset type name. */
  100. extern const char *bitset_type_name_get (bitset);
  101. /* Set bit BITNO in bitset BSET. */
  102. static inline void
  103. bitset_set (bitset bset, bitset_bindex bitno)
  104. {
  105. bitset_windex windex = bitno / BITSET_WORD_BITS;
  106. bitset_windex offset = windex - bset->b.cindex;
  107. if (offset < bset->b.csize)
  108. bset->b.cdata[offset] |= ((bitset_word) 1 << (bitno % BITSET_WORD_BITS));
  109. else
  110. BITSET_SET_ (bset, bitno);
  111. }
  112. /* Reset bit BITNO in bitset BSET. */
  113. static inline void
  114. bitset_reset (bitset bset, bitset_bindex bitno)
  115. {
  116. bitset_windex windex = bitno / BITSET_WORD_BITS;
  117. bitset_windex offset = windex - bset->b.cindex;
  118. if (offset < bset->b.csize)
  119. bset->b.cdata[offset] &= ~((bitset_word) 1 << (bitno % BITSET_WORD_BITS));
  120. else
  121. BITSET_RESET_ (bset, bitno);
  122. }
  123. /* Test bit BITNO in bitset BSET. */
  124. static inline bool
  125. bitset_test (bitset bset, bitset_bindex bitno)
  126. {
  127. bitset_windex windex = bitno / BITSET_WORD_BITS;
  128. bitset_windex offset = windex - bset->b.cindex;
  129. if (offset < bset->b.csize)
  130. return (bset->b.cdata[offset] >> (bitno % BITSET_WORD_BITS)) & 1;
  131. else
  132. return BITSET_TEST_ (bset, bitno);
  133. }
  134. /* Toggle bit BITNO in bitset BSET and return non-zero if now set. */
  135. #define bitset_toggle(bset, bitno) BITSET_TOGGLE_ (bset, bitno)
  136. /* Return size in bits of bitset SRC. */
  137. #define bitset_size(SRC) BITSET_SIZE_ (SRC)
  138. /* Change size of bitset. */
  139. extern void bitset_resize (bitset, bitset_bindex);
  140. /* Return number of bits set in bitset SRC. */
  141. #define bitset_count(SRC) BITSET_COUNT_ (SRC)
  142. /* Return SRC == 0. */
  143. #define bitset_empty_p(SRC) BITSET_EMPTY_P_ (SRC)
  144. /* DST = ~0. */
  145. #define bitset_ones(DST) BITSET_ONES_ (DST)
  146. /* DST = 0. */
  147. #define bitset_zero(DST) BITSET_ZERO_ (DST)
  148. /* DST = SRC. */
  149. #define bitset_copy(DST, SRC) BITSET_COPY_ (DST, SRC)
  150. /* Return DST & SRC == 0. */
  151. #define bitset_disjoint_p(DST, SRC) BITSET_DISJOINT_P_ (DST, SRC)
  152. /* Return DST == SRC. */
  153. #define bitset_equal_p(DST, SRC) BITSET_EQUAL_P_ (DST, SRC)
  154. /* DST = ~SRC. */
  155. #define bitset_not(DST, SRC) BITSET_NOT_ (DST, SRC)
  156. /* Return DST == DST | SRC. */
  157. #define bitset_subset_p(DST, SRC) BITSET_SUBSET_P_ (DST, SRC)
  158. /* DST = SRC1 & SRC2. */
  159. #define bitset_and(DST, SRC1, SRC2) BITSET_AND_ (DST, SRC1, SRC2)
  160. /* DST = SRC1 & SRC2. Return non-zero if DST != SRC1 & SRC2. */
  161. #define bitset_and_cmp(DST, SRC1, SRC2) BITSET_AND_CMP_ (DST, SRC1, SRC2)
  162. /* DST = SRC1 & ~SRC2. */
  163. #define bitset_andn(DST, SRC1, SRC2) BITSET_ANDN_ (DST, SRC1, SRC2)
  164. /* DST = SRC1 & ~SRC2. Return non-zero if DST != SRC1 & ~SRC2. */
  165. #define bitset_andn_cmp(DST, SRC1, SRC2) BITSET_ANDN_CMP_ (DST, SRC1, SRC2)
  166. /* DST = SRC1 | SRC2. */
  167. #define bitset_or(DST, SRC1, SRC2) BITSET_OR_ (DST, SRC1, SRC2)
  168. /* DST = SRC1 | SRC2. Return non-zero if DST != SRC1 | SRC2. */
  169. #define bitset_or_cmp(DST, SRC1, SRC2) BITSET_OR_CMP_ (DST, SRC1, SRC2)
  170. /* DST = SRC1 ^ SRC2. */
  171. #define bitset_xor(DST, SRC1, SRC2) BITSET_XOR_ (DST, SRC1, SRC2)
  172. /* DST = SRC1 ^ SRC2. Return non-zero if DST != SRC1 ^ SRC2. */
  173. #define bitset_xor_cmp(DST, SRC1, SRC2) BITSET_XOR_CMP_ (DST, SRC1, SRC2)
  174. /* DST = (SRC1 & SRC2) | SRC3. */
  175. #define bitset_and_or(DST, SRC1, SRC2, SRC3) \
  176. BITSET_AND_OR_ (DST, SRC1, SRC2, SRC3)
  177. /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
  178. DST != (SRC1 & SRC2) | SRC3. */
  179. #define bitset_and_or_cmp(DST, SRC1, SRC2, SRC3) \
  180. BITSET_AND_OR_CMP_ (DST, SRC1, SRC2, SRC3)
  181. /* DST = (SRC1 & ~SRC2) | SRC3. */
  182. #define bitset_andn_or(DST, SRC1, SRC2, SRC3) \
  183. BITSET_ANDN_OR_ (DST, SRC1, SRC2, SRC3)
  184. /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
  185. DST != (SRC1 & ~SRC2) | SRC3. */
  186. #define bitset_andn_or_cmp(DST, SRC1, SRC2, SRC3) \
  187. BITSET_ANDN_OR_CMP_ (DST, SRC1, SRC2, SRC3)
  188. /* DST = (SRC1 | SRC2) & SRC3. */
  189. #define bitset_or_and(DST, SRC1, SRC2, SRC3)\
  190. BITSET_OR_AND_ (DST, SRC1, SRC2, SRC3)
  191. /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
  192. DST != (SRC1 | SRC2) & SRC3. */
  193. #define bitset_or_and_cmp(DST, SRC1, SRC2, SRC3)\
  194. BITSET_OR_AND_CMP_ (DST, SRC1, SRC2, SRC3)
  195. /* Find list of up to NUM bits set in BSET starting from and including
  196. *NEXT. Return with actual number of bits found and with *NEXT
  197. indicating where search stopped. */
  198. #define bitset_list(BSET, LIST, NUM, NEXT) \
  199. BITSET_LIST_ (BSET, LIST, NUM, NEXT)
  200. /* Find reverse list of up to NUM bits set in BSET starting from and
  201. including NEXT. Return with actual number of bits found and with
  202. *NEXT indicating where search stopped. */
  203. #define bitset_list_reverse(BSET, LIST, NUM, NEXT) \
  204. BITSET_LIST_REVERSE_ (BSET, LIST, NUM, NEXT)
  205. /* Return true if both bitsets are of the same type and size. */
  206. extern bool bitset_compatible_p (bitset bset1, bitset bset2);
  207. /* Find next set bit from the given bit index. */
  208. extern bitset_bindex bitset_next (bitset, bitset_bindex);
  209. /* Find previous set bit from the given bit index. */
  210. extern bitset_bindex bitset_prev (bitset, bitset_bindex);
  211. /* Find first set bit. */
  212. extern bitset_bindex bitset_first (bitset);
  213. /* Find last set bit. */
  214. extern bitset_bindex bitset_last (bitset);
  215. /* Return nonzero if this is the only set bit. */
  216. extern bool bitset_only_set_p (bitset, bitset_bindex);
  217. /* Dump bitset. */
  218. extern void bitset_dump (FILE *, bitset);
  219. /* Loop over all elements of BSET, starting with MIN, setting INDEX
  220. to the index of each set bit. For example, the following will print
  221. the bits set in a bitset:
  222. bitset_bindex i;
  223. bitset_iterator iter;
  224. BITSET_FOR_EACH (iter, src, i, 0)
  225. {
  226. printf ("%lu ", (unsigned long int) i);
  227. };
  228. */
  229. #define BITSET_FOR_EACH(ITER, BSET, INDEX, MIN) \
  230. for (ITER.next = (MIN), ITER.num = BITSET_LIST_SIZE; \
  231. (ITER.num == BITSET_LIST_SIZE) \
  232. && (ITER.num = bitset_list (BSET, ITER.list, \
  233. BITSET_LIST_SIZE, &ITER.next));) \
  234. for (ITER.i = 0; \
  235. ITER.i < ITER.num && ((INDEX) = ITER.list[ITER.i], 1); \
  236. ITER.i++)
  237. /* Loop over all elements of BSET, in reverse order starting with
  238. MIN, setting INDEX to the index of each set bit. For example, the
  239. following will print the bits set in a bitset in reverse order:
  240. bitset_bindex i;
  241. bitset_iterator iter;
  242. BITSET_FOR_EACH_REVERSE (iter, src, i, 0)
  243. {
  244. printf ("%lu ", (unsigned long int) i);
  245. };
  246. */
  247. #define BITSET_FOR_EACH_REVERSE(ITER, BSET, INDEX, MIN) \
  248. for (ITER.next = (MIN), ITER.num = BITSET_LIST_SIZE; \
  249. (ITER.num == BITSET_LIST_SIZE) \
  250. && (ITER.num = bitset_list_reverse (BSET, ITER.list, \
  251. BITSET_LIST_SIZE, &ITER.next));) \
  252. for (ITER.i = 0; \
  253. ITER.i < ITER.num && ((INDEX) = ITER.list[ITER.i], 1); \
  254. ITER.i++)
  255. /* Define set operations in terms of logical operations. */
  256. #define bitset_diff(DST, SRC1, SRC2) bitset_andn (DST, SRC1, SRC2)
  257. #define bitset_diff_cmp(DST, SRC1, SRC2) bitset_andn_cmp (DST, SRC1, SRC2)
  258. #define bitset_intersection(DST, SRC1, SRC2) bitset_and (DST, SRC1, SRC2)
  259. #define bitset_intersection_cmp(DST, SRC1, SRC2) bitset_and_cmp (DST, SRC1, SRC2)
  260. #define bitset_union(DST, SRC1, SRC2) bitset_or (DST, SRC1, SRC2)
  261. #define bitset_union_cmp(DST, SRC1, SRC2) bitset_or_cmp (DST, SRC1, SRC2)
  262. /* Symmetrical difference. */
  263. #define bitset_symdiff(DST, SRC1, SRC2) bitset_xor (DST, SRC1, SRC2)
  264. #define bitset_symdiff_cmp(DST, SRC1, SRC2) bitset_xor_cmp (DST, SRC1, SRC2)
  265. /* Union of difference. */
  266. #define bitset_diff_union(DST, SRC1, SRC2, SRC3) \
  267. bitset_andn_or (DST, SRC1, SRC2, SRC3)
  268. #define bitset_diff_union_cmp(DST, SRC1, SRC2, SRC3) \
  269. bitset_andn_or_cmp (DST, SRC1, SRC2, SRC3)
  270. /* Release any memory tied up with bitsets. */
  271. extern void bitset_release_memory (void);
  272. /* Enable bitset stats gathering. */
  273. extern void bitset_stats_enable (void);
  274. /* Disable bitset stats gathering. */
  275. extern void bitset_stats_disable (void);
  276. /* Read bitset stats file of accummulated stats. */
  277. void bitset_stats_read (const char *file_name);
  278. /* Write bitset stats file of accummulated stats. */
  279. void bitset_stats_write (const char *file_name);
  280. /* Dump bitset stats. */
  281. extern void bitset_stats_dump (FILE *);
  282. /* Function to debug bitset from debugger. */
  283. extern void debug_bitset (bitset);
  284. /* Function to debug bitset stats from debugger. */
  285. extern void debug_bitset_stats (void);
  286. #endif /* _BITSET_H */