]>
git.saurik.com Git - bison.git/blob - lib/bitsetv.c
2 Copyright (C) 2001, 2002, 2004, 2005, 2006, 2009 Free Software
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.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 /* Create a vector of N_VECS bitsets, each of N_BITS, and of
28 bitsetv_alloc (bitset_bindex n_vecs
, bitset_bindex n_bits
,
29 enum bitset_type type
)
36 /* Determine number of bytes for each set. */
37 bytes
= bitset_bytes (type
, n_bits
);
39 /* If size calculation overflows, memory is exhausted. */
40 if (BITSET_SIZE_MAX
/ (sizeof (bitset
) + bytes
) <= n_vecs
)
43 /* Allocate vector table at head of bitset array. */
44 vector_bytes
= (n_vecs
+ 1) * sizeof (bitset
) + bytes
- 1;
45 vector_bytes
-= vector_bytes
% bytes
;
46 bsetv
= xcalloc (1, vector_bytes
+ bytes
* n_vecs
);
48 for (i
= 0; i
< n_vecs
; i
++)
50 bsetv
[i
] = (bitset
) (void *) ((char *) bsetv
+ vector_bytes
+ i
* bytes
);
52 bitset_init (bsetv
[i
], n_bits
, type
);
55 /* Null terminate table. */
61 /* Create a vector of N_VECS bitsets, each of N_BITS, and with
62 attribute hints specified by ATTR. */
64 bitsetv_create (bitset_bindex n_vecs
, bitset_bindex n_bits
, unsigned int attr
)
66 enum bitset_type type
;
68 type
= bitset_type_choose (n_bits
, attr
);
69 return bitsetv_alloc (n_vecs
, n_bits
, type
);
73 /* Free bitset vector BSETV. */
75 bitsetv_free (bitsetv bsetv
)
79 for (i
= 0; bsetv
[i
]; i
++)
80 BITSET_FREE_ (bsetv
[i
]);
85 /* Zero a vector of bitsets. */
87 bitsetv_zero (bitsetv bsetv
)
91 for (i
= 0; bsetv
[i
]; i
++)
92 bitset_zero (bsetv
[i
]);
96 /* Set a vector of bitsets to ones. */
98 bitsetv_ones (bitsetv bsetv
)
102 for (i
= 0; bsetv
[i
]; i
++)
103 bitset_ones (bsetv
[i
]);
107 /* Given a vector BSETV of N bitsets of size N, modify its contents to
108 be the transitive closure of what was given. */
110 bitsetv_transitive_closure (bitsetv bsetv
)
115 for (i
= 0; bsetv
[i
]; i
++)
116 for (j
= 0; bsetv
[j
]; j
++)
117 if (bitset_test (bsetv
[j
], i
))
118 bitset_or (bsetv
[j
], bsetv
[j
], bsetv
[i
]);
122 /* Given a vector BSETV of N bitsets of size N, modify its contents to
123 be the reflexive transitive closure of what was given. This is
124 the same as transitive closure but with all bits on the diagonal
125 of the bit matrix set. */
127 bitsetv_reflexive_transitive_closure (bitsetv bsetv
)
131 bitsetv_transitive_closure (bsetv
);
132 for (i
= 0; bsetv
[i
]; i
++)
133 bitset_set (bsetv
[i
], i
);
137 /* Dump the contents of a bitset vector BSETV with N_VECS elements to
140 bitsetv_dump (FILE *file
, char const *title
, char const *subtitle
,
145 fprintf (file
, "%s\n", title
);
146 for (i
= 0; bsetv
[i
]; i
++)
148 fprintf (file
, "%s %lu\n", subtitle
, (unsigned long int) i
);
149 bitset_dump (file
, bsetv
[i
]);
152 fprintf (file
, "\n");
157 debug_bitsetv (bitsetv bsetv
)
161 for (i
= 0; bsetv
[i
]; i
++)
163 fprintf (stderr
, "%lu: ", (unsigned long int) i
);
164 debug_bitset (bsetv
[i
]);
167 fprintf (stderr
, "\n");