bitsetv.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* Bitset vectors.
  2. Copyright (C) 2001-2002, 2004-2006, 2009-2013 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 <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. #include "bitsetv.h"
  16. #include <stdlib.h>
  17. /* Create a vector of N_VECS bitsets, each of N_BITS, and of
  18. type TYPE. */
  19. bitset *
  20. bitsetv_alloc (bitset_bindex n_vecs, bitset_bindex n_bits,
  21. enum bitset_type type)
  22. {
  23. size_t vector_bytes;
  24. size_t bytes;
  25. bitset *bsetv;
  26. bitset_bindex i;
  27. /* Determine number of bytes for each set. */
  28. bytes = bitset_bytes (type, n_bits);
  29. /* If size calculation overflows, memory is exhausted. */
  30. if (BITSET_SIZE_MAX / (sizeof (bitset) + bytes) <= n_vecs)
  31. xalloc_die ();
  32. /* Allocate vector table at head of bitset array. */
  33. vector_bytes = (n_vecs + 1) * sizeof (bitset) + bytes - 1;
  34. vector_bytes -= vector_bytes % bytes;
  35. bsetv = xcalloc (1, vector_bytes + bytes * n_vecs);
  36. for (i = 0; i < n_vecs; i++)
  37. {
  38. bsetv[i] = (bitset) (void *) ((char *) bsetv + vector_bytes + i * bytes);
  39. bitset_init (bsetv[i], n_bits, type);
  40. }
  41. /* Null terminate table. */
  42. bsetv[i] = 0;
  43. return bsetv;
  44. }
  45. /* Create a vector of N_VECS bitsets, each of N_BITS, and with
  46. attribute hints specified by ATTR. */
  47. bitset *
  48. bitsetv_create (bitset_bindex n_vecs, bitset_bindex n_bits, unsigned int attr)
  49. {
  50. enum bitset_type type;
  51. type = bitset_type_choose (n_bits, attr);
  52. return bitsetv_alloc (n_vecs, n_bits, type);
  53. }
  54. /* Free bitset vector BSETV. */
  55. void
  56. bitsetv_free (bitsetv bsetv)
  57. {
  58. bitset_bindex i;
  59. for (i = 0; bsetv[i]; i++)
  60. BITSET_FREE_ (bsetv[i]);
  61. free (bsetv);
  62. }
  63. /* Zero a vector of bitsets. */
  64. void
  65. bitsetv_zero (bitsetv bsetv)
  66. {
  67. bitset_bindex i;
  68. for (i = 0; bsetv[i]; i++)
  69. bitset_zero (bsetv[i]);
  70. }
  71. /* Set a vector of bitsets to ones. */
  72. void
  73. bitsetv_ones (bitsetv bsetv)
  74. {
  75. bitset_bindex i;
  76. for (i = 0; bsetv[i]; i++)
  77. bitset_ones (bsetv[i]);
  78. }
  79. /* Given a vector BSETV of N bitsets of size N, modify its contents to
  80. be the transitive closure of what was given. */
  81. void
  82. bitsetv_transitive_closure (bitsetv bsetv)
  83. {
  84. bitset_bindex i;
  85. bitset_bindex j;
  86. for (i = 0; bsetv[i]; i++)
  87. for (j = 0; bsetv[j]; j++)
  88. if (bitset_test (bsetv[j], i))
  89. bitset_or (bsetv[j], bsetv[j], bsetv[i]);
  90. }
  91. /* Given a vector BSETV of N bitsets of size N, modify its contents to
  92. be the reflexive transitive closure of what was given. This is
  93. the same as transitive closure but with all bits on the diagonal
  94. of the bit matrix set. */
  95. void
  96. bitsetv_reflexive_transitive_closure (bitsetv bsetv)
  97. {
  98. bitset_bindex i;
  99. bitsetv_transitive_closure (bsetv);
  100. for (i = 0; bsetv[i]; i++)
  101. bitset_set (bsetv[i], i);
  102. }
  103. /* Dump the contents of a bitset vector BSETV with N_VECS elements to
  104. FILE. */
  105. void
  106. bitsetv_dump (FILE *file, char const *title, char const *subtitle,
  107. bitsetv bsetv)
  108. {
  109. bitset_windex i;
  110. fprintf (file, "%s\n", title);
  111. for (i = 0; bsetv[i]; i++)
  112. {
  113. fprintf (file, "%s %lu\n", subtitle, (unsigned long int) i);
  114. bitset_dump (file, bsetv[i]);
  115. }
  116. fprintf (file, "\n");
  117. }
  118. void
  119. debug_bitsetv (bitsetv bsetv)
  120. {
  121. bitset_windex i;
  122. for (i = 0; bsetv[i]; i++)
  123. {
  124. fprintf (stderr, "%lu: ", (unsigned long int) i);
  125. debug_bitset (bsetv[i]);
  126. }
  127. fprintf (stderr, "\n");
  128. }