base.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* Base bitset stuff.
  2. Copyright (C) 2002-2004, 2006, 2009-2015, 2018-2020 Free Software
  3. Foundation, Inc.
  4. Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
  5. This program 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 3 of the License, or
  8. (at your option) any later version.
  9. This program 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 this program. If not, see <https://www.gnu.org/licenses/>. */
  15. #ifndef _BITSET_BASE_H
  16. #define _BITSET_BASE_H
  17. #include <limits.h>
  18. #include <stdbool.h>
  19. #include <stddef.h>
  20. #include "attribute.h"
  21. #include "xalloc.h"
  22. /* Currently we support five flavours of bitsets:
  23. BITSET_ARRAY: Array of bits (fixed size, fast for dense bitsets).
  24. Memory for bit array and bitset structure allocated
  25. contiguously.
  26. BITSET_LIST: Linked list of arrays of bits (variable size, least storage
  27. for large very sparse sets).
  28. BITSET_TABLE: Expandable table of pointers to arrays of bits
  29. (variable size, less storage for large sparse sets).
  30. Faster than BITSET_LIST for random access.
  31. BITSET_VECTOR: Variable array of bits (variable size, fast for
  32. dense bitsets).
  33. BITSET_STATS: Wrapper bitset for internal use only. Used for gathering
  34. statistics and/or better run-time checking.
  35. */
  36. enum bitset_type {BITSET_ARRAY, BITSET_LIST, BITSET_TABLE, BITSET_VECTOR,
  37. BITSET_TYPE_NUM, BITSET_STATS};
  38. #define BITSET_TYPE_NAMES {"abitset", "lbitset", "tbitset", "vbitset"}
  39. extern const char * const bitset_type_names[];
  40. /* Data type used to store a word of bits. */
  41. typedef unsigned long bitset_word;
  42. #define BITSET_WORD_BITS ((unsigned) (CHAR_BIT * sizeof (bitset_word)))
  43. /* Bit index. In theory we might need a type wider than size_t, but
  44. in practice we lose at most a factor of CHAR_BIT by going with
  45. size_t, and that is good enough. If this type is changed to be
  46. wider than size_t, the code needs to be modified to check for
  47. overflow when converting bit counts to byte or word counts.
  48. The bit and word index types must be unsigned. */
  49. typedef size_t bitset_bindex;
  50. /* Word index. */
  51. typedef size_t bitset_windex;
  52. /* Maximum values for commonly-used unsigned types. BITSET_SIZE_MAX
  53. always equals SIZE_MAX, but some older systems lack SIZE_MAX. */
  54. #define BITSET_BINDEX_MAX ((bitset_bindex) -1)
  55. /* Limit max word index to the maximum value of a signed integer
  56. to simplify cache disabling. */
  57. #define BITSET_WINDEX_MAX (((bitset_windex) -1) >> 1)
  58. #define BITSET_SIZE_MAX ((size_t) -1)
  59. #define BITSET_MSB ((bitset_word) 1 << (BITSET_WORD_BITS - 1))
  60. #define BITSET_LIST_SIZE 1024
  61. enum bitset_ops {BITSET_OP_ZERO, BITSET_OP_ONES,
  62. BITSET_OP_COPY, BITSET_OP_NOT,
  63. BITSET_OP_EMPTY_P, BITSET_OP_EQUAL_P,
  64. BITSET_OP_SUBSET_P, BITSET_OP_DISJOINT_P,
  65. BITSET_OP_AND, BITSET_OP_OR, BITSET_OP_XOR, BITSET_OP_ANDN,
  66. BITSET_OP_OR_AND, BITSET_OP_AND_OR, BITSET_OP_ANDN_OR};
  67. struct bbitset_struct
  68. {
  69. const struct bitset_vtable *vtable;
  70. bitset_windex cindex; /* Cache word index. */
  71. bitset_windex csize; /* Cache size in words. */
  72. bitset_word *cdata; /* Cache data pointer. */
  73. bitset_bindex n_bits; /* Number of bits. */
  74. /* Perhaps we could sacrifice another word to indicate
  75. that the bitset is known to be zero, that a bit has been set
  76. in the cache, and that a bit has been cleared in the cache.
  77. This would speed up some of the searches but slightly slow down
  78. bit set/reset operations of cached bits. */
  79. };
  80. typedef union bitset_union *bitset;
  81. /* Private accessor macros to bitset structure. */
  82. #define BITSET_VTABLE_(SRC) (SRC)->b.vtable
  83. #define BITSET_CINDEX_(SRC) (SRC)->b.cindex
  84. #define BITSET_CDATA_(SRC) (SRC)->b.cdata
  85. #define BITSET_CSIZE_(SRC) (SRC)->b.csize
  86. #define BITSET_NBITS_(SRC) (SRC)->b.n_bits
  87. /* The contents of this structure should be considered private. */
  88. struct bitset_vtable
  89. {
  90. void (*set) (bitset, bitset_bindex);
  91. void (*reset) (bitset, bitset_bindex);
  92. bool (*toggle) (bitset, bitset_bindex);
  93. bool (*test) (bitset, bitset_bindex);
  94. bitset_bindex (*resize) (bitset, bitset_bindex);
  95. bitset_bindex (*size) (bitset);
  96. bitset_bindex (*count) (bitset);
  97. bool (*empty_p) (bitset);
  98. void (*ones) (bitset);
  99. void (*zero) (bitset);
  100. void (*copy) (bitset, bitset);
  101. bool (*disjoint_p) (bitset, bitset);
  102. bool (*equal_p) (bitset, bitset);
  103. void (*not_) (bitset, bitset);
  104. bool (*subset_p) (bitset, bitset);
  105. void (*and_) (bitset, bitset, bitset);
  106. bool (*and_cmp) (bitset, bitset, bitset);
  107. void (*andn) (bitset, bitset, bitset);
  108. bool (*andn_cmp) (bitset, bitset, bitset);
  109. void (*or_) (bitset, bitset, bitset);
  110. bool (*or_cmp) (bitset, bitset, bitset);
  111. void (*xor_) (bitset, bitset, bitset);
  112. bool (*xor_cmp) (bitset, bitset, bitset);
  113. void (*and_or) (bitset, bitset, bitset, bitset);
  114. bool (*and_or_cmp) (bitset, bitset, bitset, bitset);
  115. void (*andn_or) (bitset, bitset, bitset, bitset);
  116. bool (*andn_or_cmp) (bitset, bitset, bitset, bitset);
  117. void (*or_and) (bitset, bitset, bitset, bitset);
  118. bool (*or_and_cmp) (bitset, bitset, bitset, bitset);
  119. bitset_bindex (*list) (bitset, bitset_bindex *, bitset_bindex,
  120. bitset_bindex *);
  121. bitset_bindex (*list_reverse) (bitset, bitset_bindex *, bitset_bindex,
  122. bitset_bindex *);
  123. void (*free) (bitset);
  124. enum bitset_type type;
  125. };
  126. #define BITSET_COMPATIBLE_(BSET1, BSET2) \
  127. ((BSET1)->b.vtable == (BSET2)->b.vtable)
  128. #define BITSET_CHECK2_(DST, SRC) \
  129. if (!BITSET_COMPATIBLE_ (DST, SRC)) abort ();
  130. #define BITSET_CHECK3_(DST, SRC1, SRC2) \
  131. if (!BITSET_COMPATIBLE_ (DST, SRC1) \
  132. || !BITSET_COMPATIBLE_ (DST, SRC2)) abort ();
  133. #define BITSET_CHECK4_(DST, SRC1, SRC2, SRC3) \
  134. if (!BITSET_COMPATIBLE_ (DST, SRC1) || !BITSET_COMPATIBLE_ (DST, SRC2) \
  135. || !BITSET_COMPATIBLE_ (DST, SRC3)) abort ();
  136. /* Redefine number of bits in bitset DST. */
  137. #define BITSET_RESIZE_(DST, SIZE) (DST)->b.vtable->resize (DST, SIZE)
  138. /* Return size in bits of bitset SRC. */
  139. #define BITSET_SIZE_(SRC) (SRC)->b.vtable->size (SRC)
  140. /* Return number of bits set in bitset SRC. */
  141. #define BITSET_COUNT_(SRC) (SRC)->b.vtable->count (SRC)
  142. /* Return type of bitset SRC. */
  143. #define BITSET_TYPE_(DST) (DST)->b.vtable->type
  144. /* Set bit BITNO in bitset DST. */
  145. #define BITSET_SET_(DST, BITNO) (DST)->b.vtable->set (DST, BITNO)
  146. /* Reset bit BITNO in bitset DST. */
  147. #define BITSET_RESET_(DST, BITNO) (DST)->b.vtable->reset (DST, BITNO)
  148. /* Toggle bit BITNO in bitset DST. */
  149. #define BITSET_TOGGLE_(DST, BITNO) (DST)->b.vtable->toggle (DST, BITNO)
  150. /* Return non-zero if bit BITNO in bitset SRC is set. */
  151. #define BITSET_TEST_(SRC, BITNO) (SRC)->b.vtable->test (SRC, BITNO)
  152. /* Free bitset SRC. */
  153. #define BITSET_FREE_(SRC)\
  154. ((SRC)->b.vtable->free ? (SRC)->b.vtable->free (SRC) :(void)0)
  155. /* Return SRC == 0. */
  156. #define BITSET_EMPTY_P_(SRC) (SRC)->b.vtable->empty_p (SRC)
  157. /* DST = ~0. */
  158. #define BITSET_ONES_(DST) (DST)->b.vtable->ones (DST)
  159. /* DST = 0. */
  160. #define BITSET_ZERO_(DST) (DST)->b.vtable->zero (DST)
  161. /* DST = SRC. */
  162. #define BITSET_COPY_(DST, SRC) (SRC)->b.vtable->copy (DST, SRC)
  163. /* Return DST & SRC == 0. */
  164. #define BITSET_DISJOINT_P_(DST, SRC) (SRC)->b.vtable->disjoint_p (DST, SRC)
  165. /* Return DST == SRC. */
  166. #define BITSET_EQUAL_P_(DST, SRC) (SRC)->b.vtable->equal_p (DST, SRC)
  167. /* DST = ~SRC. */
  168. #define BITSET_NOT_(DST, SRC) (SRC)->b.vtable->not_ (DST, SRC)
  169. /* Return DST == DST | SRC. */
  170. #define BITSET_SUBSET_P_(DST, SRC) (SRC)->b.vtable->subset_p (DST, SRC)
  171. /* DST = SRC1 & SRC2. */
  172. #define BITSET_AND_(DST, SRC1, SRC2) (SRC1)->b.vtable->and_ (DST, SRC1, SRC2)
  173. #define BITSET_AND_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->and_cmp (DST, SRC1, SRC2)
  174. /* DST = SRC1 & ~SRC2. */
  175. #define BITSET_ANDN_(DST, SRC1, SRC2) (SRC1)->b.vtable->andn (DST, SRC1, SRC2)
  176. #define BITSET_ANDN_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->andn_cmp (DST, SRC1, SRC2)
  177. /* DST = SRC1 | SRC2. */
  178. #define BITSET_OR_(DST, SRC1, SRC2) (SRC1)->b.vtable->or_ (DST, SRC1, SRC2)
  179. #define BITSET_OR_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->or_cmp (DST, SRC1, SRC2)
  180. /* DST = SRC1 ^ SRC2. */
  181. #define BITSET_XOR_(DST, SRC1, SRC2) (SRC1)->b.vtable->xor_ (DST, SRC1, SRC2)
  182. #define BITSET_XOR_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->xor_cmp (DST, SRC1, SRC2)
  183. /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
  184. DST != (SRC1 & SRC2) | SRC3. */
  185. #define BITSET_AND_OR_(DST, SRC1, SRC2, SRC3) \
  186. (SRC1)->b.vtable->and_or (DST, SRC1, SRC2, SRC3)
  187. #define BITSET_AND_OR_CMP_(DST, SRC1, SRC2, SRC3) \
  188. (SRC1)->b.vtable->and_or_cmp (DST, SRC1, SRC2, SRC3)
  189. /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
  190. DST != (SRC1 & ~SRC2) | SRC3. */
  191. #define BITSET_ANDN_OR_(DST, SRC1, SRC2, SRC3) \
  192. (SRC1)->b.vtable->andn_or (DST, SRC1, SRC2, SRC3)
  193. #define BITSET_ANDN_OR_CMP_(DST, SRC1, SRC2, SRC3) \
  194. (SRC1)->b.vtable->andn_or_cmp (DST, SRC1, SRC2, SRC3)
  195. /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
  196. DST != (SRC1 | SRC2) & SRC3. */
  197. #define BITSET_OR_AND_(DST, SRC1, SRC2, SRC3) \
  198. (SRC1)->b.vtable->or_and (DST, SRC1, SRC2, SRC3)
  199. #define BITSET_OR_AND_CMP_(DST, SRC1, SRC2, SRC3) \
  200. (SRC1)->b.vtable->or_and_cmp (DST, SRC1, SRC2, SRC3)
  201. /* Find list of up to NUM bits set in BSET starting from and including
  202. *NEXT. Return with actual number of bits found and with *NEXT
  203. indicating where search stopped. */
  204. #define BITSET_LIST_(BSET, LIST, NUM, NEXT) \
  205. (BSET)->b.vtable->list (BSET, LIST, NUM, NEXT)
  206. /* Find reverse list of up to NUM bits set in BSET starting from and
  207. including NEXT. Return with actual number of bits found and with
  208. *NEXT indicating where search stopped. */
  209. #define BITSET_LIST_REVERSE_(BSET, LIST, NUM, NEXT) \
  210. (BSET)->b.vtable->list_reverse (BSET, LIST, NUM, NEXT)
  211. /* Private functions for bitset implementations. */
  212. bool bitset_toggle_ (bitset, bitset_bindex);
  213. bitset_bindex bitset_count_ (bitset);
  214. bitset_bindex bitset_size_ (bitset);
  215. bool bitset_copy_ (bitset, bitset);
  216. void bitset_and_or_ (bitset, bitset, bitset, bitset);
  217. bool bitset_and_or_cmp_ (bitset, bitset, bitset, bitset);
  218. void bitset_andn_or_ (bitset, bitset, bitset, bitset);
  219. bool bitset_andn_or_cmp_ (bitset, bitset, bitset, bitset);
  220. void bitset_or_and_ (bitset, bitset, bitset, bitset);
  221. bool bitset_or_and_cmp_ (bitset, bitset, bitset, bitset);
  222. #endif /* _BBITSET_H */