bitsetv.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* Bitset vectors.
  2. Copyright (C) 2001-2002, 2004-2006, 2009-2015, 2018-2020 Free Software
  3. Foundation, Inc.
  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. #include <config.h>
  15. #include "bitsetv.h"
  16. #include <stdlib.h>
  17. #include "xalloc.h"
  18. /* Create a vector of N_VECS bitsets, each of N_BITS, and of
  19. type TYPE. */
  20. bitset *
  21. bitsetv_alloc (bitset_bindex n_vecs, bitset_bindex n_bits,
  22. enum bitset_type type)
  23. {
  24. /* Determine number of bytes for each set. */
  25. size_t bytes = bitset_bytes (type, n_bits);
  26. /* If size calculation overflows, memory is exhausted. */
  27. if (BITSET_SIZE_MAX / (sizeof (bitset) + bytes) <= n_vecs)
  28. xalloc_die ();
  29. /* Allocate vector table at head of bitset array. */
  30. size_t vector_bytes = (n_vecs + 1) * sizeof (bitset) + bytes - 1;
  31. vector_bytes -= vector_bytes % bytes;
  32. bitset *bsetv = xzalloc (vector_bytes + bytes * n_vecs);
  33. bitset_bindex i = 0;
  34. for (i = 0; i < n_vecs; i++)
  35. {
  36. bsetv[i] = (bitset) (void *) ((char *) bsetv + vector_bytes + i * bytes);
  37. bitset_init (bsetv[i], n_bits, type);
  38. }
  39. /* Null terminate table. */
  40. bsetv[i] = 0;
  41. return bsetv;
  42. }
  43. /* Create a vector of N_VECS bitsets, each of N_BITS, and with
  44. attribute hints specified by ATTR. */
  45. bitset *
  46. bitsetv_create (bitset_bindex n_vecs, bitset_bindex n_bits, unsigned attr)
  47. {
  48. enum bitset_type type = bitset_type_choose (n_bits, attr);
  49. return bitsetv_alloc (n_vecs, n_bits, type);
  50. }
  51. /* Free bitset vector BSETV. */
  52. void
  53. bitsetv_free (bitsetv bsetv)
  54. {
  55. if (bsetv)
  56. {
  57. for (bitset_bindex i = 0; bsetv[i]; i++)
  58. BITSET_FREE_ (bsetv[i]);
  59. free (bsetv);
  60. }
  61. }
  62. /* Zero a vector of bitsets. */
  63. void
  64. bitsetv_zero (bitsetv bsetv)
  65. {
  66. for (bitset_bindex i = 0; bsetv[i]; i++)
  67. bitset_zero (bsetv[i]);
  68. }
  69. /* Set a vector of bitsets to ones. */
  70. void
  71. bitsetv_ones (bitsetv bsetv)
  72. {
  73. for (bitset_bindex i = 0; bsetv[i]; i++)
  74. bitset_ones (bsetv[i]);
  75. }
  76. /* Given a vector BSETV of N bitsets of size N, modify its contents to
  77. be the transitive closure of what was given. */
  78. void
  79. bitsetv_transitive_closure (bitsetv bsetv)
  80. {
  81. for (bitset_bindex i = 0; bsetv[i]; i++)
  82. for (bitset_bindex j = 0; bsetv[j]; j++)
  83. if (bitset_test (bsetv[j], i))
  84. bitset_or (bsetv[j], bsetv[j], bsetv[i]);
  85. }
  86. /* Given a vector BSETV of N bitsets of size N, modify its contents to
  87. be the reflexive transitive closure of what was given. This is
  88. the same as transitive closure but with all bits on the diagonal
  89. of the bit matrix set. */
  90. void
  91. bitsetv_reflexive_transitive_closure (bitsetv bsetv)
  92. {
  93. bitsetv_transitive_closure (bsetv);
  94. for (bitset_bindex i = 0; bsetv[i]; i++)
  95. bitset_set (bsetv[i], i);
  96. }
  97. /* Dump the contents of a bitset vector BSETV with N_VECS elements to
  98. FILE. */
  99. void
  100. bitsetv_dump (FILE *file, char const *title, char const *subtitle,
  101. bitsetv bsetv)
  102. {
  103. fprintf (file, "%s\n", title);
  104. for (bitset_windex i = 0; bsetv[i]; i++)
  105. {
  106. fprintf (file, "%s %lu\n", subtitle, (unsigned long) i);
  107. bitset_dump (file, bsetv[i]);
  108. }
  109. fprintf (file, "\n");
  110. }
  111. void
  112. debug_bitsetv (bitsetv bsetv)
  113. {
  114. for (bitset_windex i = 0; bsetv[i]; i++)
  115. {
  116. fprintf (stderr, "%lu: ", (unsigned long) i);
  117. debug_bitset (bsetv[i]);
  118. }
  119. fprintf (stderr, "\n");
  120. }
  121. /*--------------------------------------------------------.
  122. | Display the MATRIX array of SIZE bitsets of size SIZE. |
  123. `--------------------------------------------------------*/
  124. void
  125. bitsetv_matrix_dump (FILE *out, const char *title, bitsetv bset)
  126. {
  127. bitset_bindex hsize = bitset_size (bset[0]);
  128. /* Title. */
  129. fprintf (out, "%s BEGIN\n", title);
  130. /* Column numbers. */
  131. fputs (" ", out);
  132. for (bitset_bindex i = 0; i < hsize; ++i)
  133. putc (i / 10 ? '0' + i / 10 : ' ', out);
  134. putc ('\n', out);
  135. fputs (" ", out);
  136. for (bitset_bindex i = 0; i < hsize; ++i)
  137. fprintf (out, "%d", (int) (i % 10));
  138. putc ('\n', out);
  139. /* Bar. */
  140. fputs (" .", out);
  141. for (bitset_bindex i = 0; i < hsize; ++i)
  142. putc ('-', out);
  143. fputs (".\n", out);
  144. /* Contents. */
  145. for (bitset_bindex i = 0; bset[i]; ++i)
  146. {
  147. fprintf (out, "%2lu|", (unsigned long) i);
  148. for (bitset_bindex j = 0; j < hsize; ++j)
  149. fputs (bitset_test (bset[i], j) ? "1" : " ", out);
  150. fputs ("|\n", out);
  151. }
  152. /* Bar. */
  153. fputs (" `", out);
  154. for (bitset_bindex i = 0; i < hsize; ++i)
  155. putc ('-', out);
  156. fputs ("'\n", out);
  157. /* End title. */
  158. fprintf (out, "%s END\n\n", title);
  159. }