bitset.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /* Generic bitsets.
  2. Copyright (C) 2002-2004, 2009-2015, 2018-2020 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 <https://www.gnu.org/licenses/>. */
  14. #ifndef _GL_BITSET_H
  15. #define _GL_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 <stdio.h>
  19. #if USE_UNLOCKED_IO
  20. # include "unlocked-io.h"
  21. #endif
  22. #include "bitset/base.h"
  23. #include "obstack.h"
  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 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 tbitset_struct
  47. {
  48. struct bbitset_struct b;
  49. bitset_windex size; /* Number of elements. */
  50. struct tbitset_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. size_t bitset_bytes (enum bitset_type, bitset_bindex);
  80. /* Initialise a bitset with desired type and size. */
  81. bitset bitset_init (bitset, bitset_bindex, enum bitset_type);
  82. /* Select an implementation type based on the desired bitset size
  83. and attributes. */
  84. enum bitset_type bitset_type_choose (bitset_bindex, bitset_attrs);
  85. /* Create a bitset of desired type and size. The bitset is zeroed. */
  86. bitset bitset_alloc (bitset_bindex, enum bitset_type);
  87. /* Free bitset. Do nothing if NULL. */
  88. void bitset_free (bitset);
  89. /* Create a bitset of desired type and size using an obstack. The
  90. bitset is zeroed. */
  91. bitset bitset_obstack_alloc (struct obstack *bobstack,
  92. bitset_bindex, enum bitset_type);
  93. /* Free bitset allocated on obstack. Do nothing if NULL. */
  94. void bitset_obstack_free (bitset);
  95. /* Create a bitset of desired size and attributes. The bitset is zeroed. */
  96. bitset bitset_create (bitset_bindex, bitset_attrs);
  97. /* Return bitset type. */
  98. enum bitset_type bitset_type_get (bitset);
  99. /* Return bitset type name. */
  100. 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 in bits of bitset. New bits are zeroed. Return
  139. SIZE. */
  140. #define bitset_resize(DST, SIZE) BITSET_RESIZE_ (DST, SIZE)
  141. /* Return number of bits set in bitset SRC. */
  142. #define bitset_count(SRC) BITSET_COUNT_ (SRC)
  143. /* Return SRC == 0. */
  144. #define bitset_empty_p(SRC) BITSET_EMPTY_P_ (SRC)
  145. /* DST = ~0. */
  146. #define bitset_ones(DST) BITSET_ONES_ (DST)
  147. /* DST = 0. */
  148. #define bitset_zero(DST) BITSET_ZERO_ (DST)
  149. /* DST = SRC. */
  150. #define bitset_copy(DST, SRC) BITSET_COPY_ (DST, SRC)
  151. /* Return DST & SRC == 0. */
  152. #define bitset_disjoint_p(DST, SRC) BITSET_DISJOINT_P_ (DST, SRC)
  153. /* Return DST == SRC. */
  154. #define bitset_equal_p(DST, SRC) BITSET_EQUAL_P_ (DST, SRC)
  155. /* DST = ~SRC. */
  156. #define bitset_not(DST, SRC) BITSET_NOT_ (DST, SRC)
  157. /* Return DST == DST | SRC. */
  158. #define bitset_subset_p(DST, SRC) BITSET_SUBSET_P_ (DST, SRC)
  159. /* DST = SRC1 & SRC2. */
  160. #define bitset_and(DST, SRC1, SRC2) BITSET_AND_ (DST, SRC1, SRC2)
  161. /* DST = SRC1 & SRC2. Return non-zero if DST != SRC1 & SRC2. */
  162. #define bitset_and_cmp(DST, SRC1, SRC2) BITSET_AND_CMP_ (DST, SRC1, SRC2)
  163. /* DST = SRC1 & ~SRC2. */
  164. #define bitset_andn(DST, SRC1, SRC2) BITSET_ANDN_ (DST, SRC1, SRC2)
  165. /* DST = SRC1 & ~SRC2. Return non-zero if DST != SRC1 & ~SRC2. */
  166. #define bitset_andn_cmp(DST, SRC1, SRC2) BITSET_ANDN_CMP_ (DST, SRC1, SRC2)
  167. /* DST = SRC1 | SRC2. */
  168. #define bitset_or(DST, SRC1, SRC2) BITSET_OR_ (DST, SRC1, SRC2)
  169. /* DST = SRC1 | SRC2. Return non-zero if DST != SRC1 | SRC2. */
  170. #define bitset_or_cmp(DST, SRC1, SRC2) BITSET_OR_CMP_ (DST, SRC1, SRC2)
  171. /* DST = SRC1 ^ SRC2. */
  172. #define bitset_xor(DST, SRC1, SRC2) BITSET_XOR_ (DST, SRC1, SRC2)
  173. /* DST = SRC1 ^ SRC2. Return non-zero if DST != SRC1 ^ SRC2. */
  174. #define bitset_xor_cmp(DST, SRC1, SRC2) BITSET_XOR_CMP_ (DST, SRC1, SRC2)
  175. /* DST = (SRC1 & SRC2) | SRC3. */
  176. #define bitset_and_or(DST, SRC1, SRC2, SRC3) \
  177. BITSET_AND_OR_ (DST, SRC1, SRC2, SRC3)
  178. /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
  179. DST != (SRC1 & SRC2) | SRC3. */
  180. #define bitset_and_or_cmp(DST, SRC1, SRC2, SRC3) \
  181. BITSET_AND_OR_CMP_ (DST, SRC1, SRC2, SRC3)
  182. /* DST = (SRC1 & ~SRC2) | SRC3. */
  183. #define bitset_andn_or(DST, SRC1, SRC2, SRC3) \
  184. BITSET_ANDN_OR_ (DST, SRC1, SRC2, SRC3)
  185. /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
  186. DST != (SRC1 & ~SRC2) | SRC3. */
  187. #define bitset_andn_or_cmp(DST, SRC1, SRC2, SRC3) \
  188. BITSET_ANDN_OR_CMP_ (DST, SRC1, SRC2, SRC3)
  189. /* DST = (SRC1 | SRC2) & SRC3. */
  190. #define bitset_or_and(DST, SRC1, SRC2, SRC3)\
  191. BITSET_OR_AND_ (DST, SRC1, SRC2, SRC3)
  192. /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
  193. DST != (SRC1 | SRC2) & SRC3. */
  194. #define bitset_or_and_cmp(DST, SRC1, SRC2, SRC3)\
  195. BITSET_OR_AND_CMP_ (DST, SRC1, SRC2, SRC3)
  196. /* Find list of up to NUM bits set in BSET starting from and including
  197. *NEXT. Return with actual number of bits found and with *NEXT
  198. indicating where search stopped. */
  199. #define bitset_list(BSET, LIST, NUM, NEXT) \
  200. BITSET_LIST_ (BSET, LIST, NUM, NEXT)
  201. /* Find reverse list of up to NUM bits set in BSET starting from and
  202. including NEXT. Return with actual number of bits found and with
  203. *NEXT indicating where search stopped. */
  204. #define bitset_list_reverse(BSET, LIST, NUM, NEXT) \
  205. BITSET_LIST_REVERSE_ (BSET, LIST, NUM, NEXT)
  206. /* Return true if both bitsets are of the same type and size. */
  207. bool bitset_compatible_p (bitset bset1, bitset bset2);
  208. /* Find next set bit from the given bit index. */
  209. bitset_bindex bitset_next (bitset, bitset_bindex);
  210. /* Find previous set bit from the given bit index. */
  211. bitset_bindex bitset_prev (bitset, bitset_bindex);
  212. /* Find first set bit. */
  213. bitset_bindex bitset_first (bitset);
  214. /* Find last set bit. */
  215. bitset_bindex bitset_last (bitset);
  216. /* Return nonzero if this is the only set bit. */
  217. bool bitset_only_set_p (bitset, bitset_bindex);
  218. /* Dump bitset. */
  219. void bitset_dump (FILE *, bitset);
  220. /* Loop over all elements of BSET, starting with MIN, setting INDEX
  221. to the index of each set bit. For example, the following will print
  222. the bits set in a bitset:
  223. bitset_bindex i;
  224. bitset_iterator iter;
  225. BITSET_FOR_EACH (iter, src, i, 0)
  226. printf ("%lu ", (unsigned long) i);
  227. */
  228. #define BITSET_FOR_EACH(ITER, BSET, INDEX, MIN) \
  229. for (ITER.next = (MIN), ITER.num = BITSET_LIST_SIZE; \
  230. (ITER.num == BITSET_LIST_SIZE) \
  231. && (ITER.num = bitset_list (BSET, ITER.list, \
  232. BITSET_LIST_SIZE, &ITER.next));) \
  233. for (ITER.i = 0; \
  234. ITER.i < ITER.num && ((INDEX) = ITER.list[ITER.i], 1); \
  235. ITER.i++)
  236. /* Loop over all elements of BSET, in reverse order starting with
  237. MIN, setting INDEX to the index of each set bit. For example, the
  238. following will print the bits set in a bitset in reverse order:
  239. bitset_bindex i;
  240. bitset_iterator iter;
  241. BITSET_FOR_EACH_REVERSE (iter, src, i, 0)
  242. printf ("%lu ", (unsigned long) i);
  243. */
  244. #define BITSET_FOR_EACH_REVERSE(ITER, BSET, INDEX, MIN) \
  245. for (ITER.next = (MIN), ITER.num = BITSET_LIST_SIZE; \
  246. (ITER.num == BITSET_LIST_SIZE) \
  247. && (ITER.num = bitset_list_reverse (BSET, ITER.list, \
  248. BITSET_LIST_SIZE, &ITER.next));) \
  249. for (ITER.i = 0; \
  250. ITER.i < ITER.num && ((INDEX) = ITER.list[ITER.i], 1); \
  251. ITER.i++)
  252. /* Define set operations in terms of logical operations. */
  253. #define bitset_diff(DST, SRC1, SRC2) bitset_andn (DST, SRC1, SRC2)
  254. #define bitset_diff_cmp(DST, SRC1, SRC2) bitset_andn_cmp (DST, SRC1, SRC2)
  255. #define bitset_intersection(DST, SRC1, SRC2) bitset_and (DST, SRC1, SRC2)
  256. #define bitset_intersection_cmp(DST, SRC1, SRC2) bitset_and_cmp (DST, SRC1, SRC2)
  257. #define bitset_union(DST, SRC1, SRC2) bitset_or (DST, SRC1, SRC2)
  258. #define bitset_union_cmp(DST, SRC1, SRC2) bitset_or_cmp (DST, SRC1, SRC2)
  259. /* Symmetrical difference. */
  260. #define bitset_symdiff(DST, SRC1, SRC2) bitset_xor (DST, SRC1, SRC2)
  261. #define bitset_symdiff_cmp(DST, SRC1, SRC2) bitset_xor_cmp (DST, SRC1, SRC2)
  262. /* Union of difference. */
  263. #define bitset_diff_union(DST, SRC1, SRC2, SRC3) \
  264. bitset_andn_or (DST, SRC1, SRC2, SRC3)
  265. #define bitset_diff_union_cmp(DST, SRC1, SRC2, SRC3) \
  266. bitset_andn_or_cmp (DST, SRC1, SRC2, SRC3)
  267. /* Release any memory tied up with bitsets. */
  268. void bitset_release_memory (void);
  269. /* Enable bitset stats gathering. */
  270. void bitset_stats_enable (void);
  271. /* Disable bitset stats gathering. */
  272. void bitset_stats_disable (void);
  273. /* Read bitset stats file of accummulated stats. */
  274. void bitset_stats_read (const char *file_name);
  275. /* Write bitset stats file of accummulated stats. */
  276. void bitset_stats_write (const char *file_name);
  277. /* Dump bitset stats. */
  278. void bitset_stats_dump (FILE *);
  279. /* Function to debug bitset from debugger. */
  280. void debug_bitset (bitset);
  281. /* Function to debug bitset stats from debugger. */
  282. void debug_bitset_stats (void);
  283. #endif /* _GL_BITSET_H */