bitset.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /* General bitsets.
  2. Copyright (C) 2002-2006, 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. #include <config.h>
  15. #include "bitset.h"
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "abitset.h"
  19. #include "lbitset.h"
  20. #include "ebitset.h"
  21. #include "vbitset.h"
  22. #include "bitset_stats.h"
  23. #include "obstack.h"
  24. const char * const bitset_type_names[] = BITSET_TYPE_NAMES;
  25. /* Return number of bytes required to create a N_BIT bitset
  26. of TYPE. The bitset may grow to require more bytes than this. */
  27. size_t
  28. bitset_bytes (enum bitset_type type, bitset_bindex n_bits)
  29. {
  30. size_t bytes;
  31. if (bitset_stats_enabled)
  32. return bitset_stats_bytes ();
  33. switch (type)
  34. {
  35. default:
  36. abort ();
  37. case BITSET_ARRAY:
  38. bytes = abitset_bytes (n_bits);
  39. break;
  40. case BITSET_LIST:
  41. bytes = lbitset_bytes (n_bits);
  42. break;
  43. case BITSET_TABLE:
  44. bytes = ebitset_bytes (n_bits);
  45. break;
  46. case BITSET_VARRAY:
  47. bytes = vbitset_bytes (n_bits);
  48. break;
  49. }
  50. return bytes;
  51. }
  52. /* Initialise bitset BSET of TYPE for N_BITS. */
  53. bitset
  54. bitset_init (bitset bset, bitset_bindex n_bits, enum bitset_type type)
  55. {
  56. if (bitset_stats_enabled)
  57. return bitset_stats_init (bset, n_bits, type);
  58. switch (type)
  59. {
  60. default:
  61. abort ();
  62. case BITSET_ARRAY:
  63. return abitset_init (bset, n_bits);
  64. case BITSET_LIST:
  65. return lbitset_init (bset, n_bits);
  66. case BITSET_TABLE:
  67. return ebitset_init (bset, n_bits);
  68. case BITSET_VARRAY:
  69. return vbitset_init (bset, n_bits);
  70. }
  71. }
  72. /* Select a bitset type for a set of N_BITS and with attribute hints
  73. specified by ATTR. For variable size bitsets, N_BITS is only a
  74. hint and may be zero. */
  75. enum bitset_type
  76. bitset_type_choose (bitset_bindex n_bits ATTRIBUTE_UNUSED, unsigned int attr)
  77. {
  78. /* Check attributes. */
  79. if (attr & BITSET_FIXED && attr & BITSET_VARIABLE)
  80. abort ();
  81. if (attr & BITSET_SPARSE && attr & BITSET_DENSE)
  82. abort ();
  83. /* Choose the type of bitset. Note that sometimes we will be asked
  84. for a zero length fixed size bitset. */
  85. /* If no attributes selected, choose a good compromise. */
  86. if (!attr)
  87. return BITSET_VARRAY;
  88. if (attr & BITSET_SPARSE)
  89. return BITSET_LIST;
  90. if (attr & BITSET_FIXED)
  91. return BITSET_ARRAY;
  92. if (attr & BITSET_GREEDY)
  93. return BITSET_TABLE;
  94. return BITSET_VARRAY;
  95. }
  96. /* Create a bitset of N_BITS of type TYPE. */
  97. bitset
  98. bitset_alloc (bitset_bindex n_bits, enum bitset_type type)
  99. {
  100. size_t bytes;
  101. bitset bset;
  102. bytes = bitset_bytes (type, n_bits);
  103. bset = xcalloc (1, bytes);
  104. /* The cache is disabled until some elements are allocated. If we
  105. have variable length arrays, then we may need to allocate a dummy
  106. element. */
  107. return bitset_init (bset, n_bits, type);
  108. }
  109. /* Create a bitset of N_BITS of type TYPE. */
  110. bitset
  111. bitset_obstack_alloc (struct obstack *bobstack,
  112. bitset_bindex n_bits, enum bitset_type type)
  113. {
  114. size_t bytes;
  115. bitset bset;
  116. bytes = bitset_bytes (type, n_bits);
  117. bset = obstack_alloc (bobstack, bytes);
  118. memset (bset, 0, bytes);
  119. return bitset_init (bset, n_bits, type);
  120. }
  121. /* Create a bitset of N_BITS and with attribute hints specified by
  122. ATTR. */
  123. bitset
  124. bitset_create (bitset_bindex n_bits, unsigned int attr)
  125. {
  126. enum bitset_type type;
  127. type = bitset_type_choose (n_bits, attr);
  128. return bitset_alloc (n_bits, type);
  129. }
  130. /* Free bitset BSET. */
  131. void
  132. bitset_free (bitset bset)
  133. {
  134. BITSET_FREE_ (bset);
  135. free (bset);
  136. }
  137. /* Free bitset BSET allocated on obstack. */
  138. void
  139. bitset_obstack_free (bitset bset)
  140. {
  141. BITSET_FREE_ (bset);
  142. }
  143. /* Return bitset type. */
  144. enum bitset_type
  145. bitset_type_get (bitset bset)
  146. {
  147. enum bitset_type type;
  148. type = BITSET_TYPE_ (bset);
  149. if (type != BITSET_STATS)
  150. return type;
  151. return bitset_stats_type_get (bset);
  152. }
  153. /* Return name of bitset type. */
  154. const char *
  155. bitset_type_name_get (bitset bset)
  156. {
  157. enum bitset_type type;
  158. type = bitset_type_get (bset);
  159. return bitset_type_names[type];
  160. }
  161. /* Find next bit set in SRC starting from and including BITNO.
  162. Return BITSET_BINDEX_MAX if SRC empty. */
  163. bitset_bindex
  164. bitset_next (bitset src, bitset_bindex bitno)
  165. {
  166. bitset_bindex val;
  167. bitset_bindex next = bitno;
  168. if (!bitset_list (src, &val, 1, &next))
  169. return BITSET_BINDEX_MAX;
  170. return val;
  171. }
  172. /* Return true if both bitsets are of the same type and size. */
  173. extern bool
  174. bitset_compatible_p (bitset bset1, bitset bset2)
  175. {
  176. return BITSET_COMPATIBLE_ (bset1, bset2);
  177. }
  178. /* Find previous bit set in SRC starting from and including BITNO.
  179. Return BITSET_BINDEX_MAX if SRC empty. */
  180. bitset_bindex
  181. bitset_prev (bitset src, bitset_bindex bitno)
  182. {
  183. bitset_bindex val;
  184. bitset_bindex next = bitno;
  185. if (!bitset_list_reverse (src, &val, 1, &next))
  186. return BITSET_BINDEX_MAX;
  187. return val;
  188. }
  189. /* Find first set bit. */
  190. bitset_bindex
  191. bitset_first (bitset src)
  192. {
  193. return bitset_next (src, 0);
  194. }
  195. /* Find last set bit. */
  196. bitset_bindex
  197. bitset_last (bitset src)
  198. {
  199. return bitset_prev (src, 0);
  200. }
  201. /* Is BITNO in SRC the only set bit? */
  202. bool
  203. bitset_only_set_p (bitset src, bitset_bindex bitno)
  204. {
  205. bitset_bindex val[2];
  206. bitset_bindex next = 0;
  207. if (bitset_list (src, val, 2, &next) != 1)
  208. return false;
  209. return val[0] == bitno;
  210. }
  211. /* Print contents of bitset BSET to FILE. */
  212. static void
  213. bitset_print (FILE *file, bitset bset, bool verbose)
  214. {
  215. unsigned int pos;
  216. bitset_bindex i;
  217. bitset_iterator iter;
  218. if (verbose)
  219. fprintf (file, "n_bits = %lu, set = {",
  220. (unsigned long int) bitset_size (bset));
  221. pos = 30;
  222. BITSET_FOR_EACH (iter, bset, i, 0)
  223. {
  224. if (pos > 70)
  225. {
  226. fprintf (file, "\n");
  227. pos = 0;
  228. }
  229. fprintf (file, "%lu ", (unsigned long int) i);
  230. pos += 1 + (i >= 10) + (i >= 100);
  231. };
  232. if (verbose)
  233. fprintf (file, "}\n");
  234. }
  235. /* Dump bitset BSET to FILE. */
  236. void
  237. bitset_dump (FILE *file, bitset bset)
  238. {
  239. bitset_print (file, bset, false);
  240. }
  241. /* Release memory associated with bitsets. */
  242. void
  243. bitset_release_memory (void)
  244. {
  245. lbitset_release_memory ();
  246. ebitset_release_memory ();
  247. }
  248. /* Toggle bit BITNO in bitset BSET and the new value of the bit. */
  249. bool
  250. bitset_toggle_ (bitset bset, bitset_bindex bitno)
  251. {
  252. /* This routine is for completeness. It could be optimized if
  253. required. */
  254. if (bitset_test (bset, bitno))
  255. {
  256. bitset_reset (bset, bitno);
  257. return false;
  258. }
  259. else
  260. {
  261. bitset_set (bset, bitno);
  262. return true;
  263. }
  264. }
  265. /* Return number of bits in bitset SRC. */
  266. bitset_bindex
  267. bitset_size_ (bitset src)
  268. {
  269. return BITSET_NBITS_ (src);
  270. }
  271. /* Return number of bits set in bitset SRC. */
  272. bitset_bindex
  273. bitset_count_ (bitset src)
  274. {
  275. bitset_bindex list[BITSET_LIST_SIZE];
  276. bitset_bindex next;
  277. bitset_bindex num;
  278. bitset_bindex count;
  279. /* This could be greatly sped up by adding a count method for each
  280. bitset implementation that uses a direct technique (based on
  281. masks) for counting the number of bits set in a word. */
  282. next = 0;
  283. for (count = 0; (num = bitset_list (src, list, BITSET_LIST_SIZE, &next));
  284. count += num)
  285. continue;
  286. return count;
  287. }
  288. /* DST = SRC. Return true if DST != SRC.
  289. This is a fallback for the case where SRC and DST are different
  290. bitset types. */
  291. bool
  292. bitset_copy_ (bitset dst, bitset src)
  293. {
  294. bitset_bindex i;
  295. bitset_iterator iter;
  296. /* Convert bitset types. We assume that the DST bitset
  297. is large enough to hold the SRC bitset. */
  298. bitset_zero (dst);
  299. BITSET_FOR_EACH (iter, src, i, 0)
  300. {
  301. bitset_set (dst, i);
  302. };
  303. return true;
  304. }
  305. /* This is a fallback for implementations that do not support
  306. four operand operations. */
  307. static inline bool
  308. bitset_op4_cmp (bitset dst, bitset src1, bitset src2, bitset src3,
  309. enum bitset_ops op)
  310. {
  311. bool changed = false;
  312. bool stats_enabled_save;
  313. bitset tmp;
  314. /* Create temporary bitset. */
  315. stats_enabled_save = bitset_stats_enabled;
  316. bitset_stats_enabled = false;
  317. tmp = bitset_alloc (0, bitset_type_get (dst));
  318. bitset_stats_enabled = stats_enabled_save;
  319. switch (op)
  320. {
  321. default:
  322. abort ();
  323. case BITSET_OP_OR_AND:
  324. bitset_or (tmp, src1, src2);
  325. changed = bitset_and_cmp (dst, src3, tmp);
  326. break;
  327. case BITSET_OP_AND_OR:
  328. bitset_and (tmp, src1, src2);
  329. changed = bitset_or_cmp (dst, src3, tmp);
  330. break;
  331. case BITSET_OP_ANDN_OR:
  332. bitset_andn (tmp, src1, src2);
  333. changed = bitset_or_cmp (dst, src3, tmp);
  334. break;
  335. }
  336. bitset_free (tmp);
  337. return changed;
  338. }
  339. /* DST = (SRC1 & SRC2) | SRC3. */
  340. void
  341. bitset_and_or_ (bitset dst, bitset src1, bitset src2, bitset src3)
  342. {
  343. bitset_and_or_cmp_ (dst, src1, src2, src3);
  344. }
  345. /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
  346. DST != (SRC1 & SRC2) | SRC3. */
  347. bool
  348. bitset_and_or_cmp_ (bitset dst, bitset src1, bitset src2, bitset src3)
  349. {
  350. return bitset_op4_cmp (dst, src1, src2, src3, BITSET_OP_AND_OR);
  351. }
  352. /* DST = (SRC1 & ~SRC2) | SRC3. */
  353. void
  354. bitset_andn_or_ (bitset dst, bitset src1, bitset src2, bitset src3)
  355. {
  356. bitset_andn_or_cmp_ (dst, src1, src2, src3);
  357. }
  358. /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
  359. DST != (SRC1 & ~SRC2) | SRC3. */
  360. bool
  361. bitset_andn_or_cmp_ (bitset dst, bitset src1, bitset src2, bitset src3)
  362. {
  363. return bitset_op4_cmp (dst, src1, src2, src3, BITSET_OP_ANDN_OR);
  364. }
  365. /* DST = (SRC1 | SRC2) & SRC3. */
  366. void
  367. bitset_or_and_ (bitset dst, bitset src1, bitset src2, bitset src3)
  368. {
  369. bitset_or_and_cmp_ (dst, src1, src2, src3);
  370. }
  371. /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
  372. DST != (SRC1 | SRC2) & SRC3. */
  373. bool
  374. bitset_or_and_cmp_ (bitset dst, bitset src1, bitset src2, bitset src3)
  375. {
  376. return bitset_op4_cmp (dst, src1, src2, src3, BITSET_OP_OR_AND);
  377. }
  378. /* Function to be called from debugger to print bitset. */
  379. void
  380. debug_bitset (bitset bset)
  381. {
  382. if (bset)
  383. bitset_print (stderr, bset, true);
  384. }