]> git.saurik.com Git - bison.git/blame - lib/bitsetv.c
Use yy* consistently.
[bison.git] / lib / bitsetv.c
CommitLineData
7086e707 1/* Bitset vectors.
02650b7f 2 Copyright (C) 2001, 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
7086e707 3
f16b0819 4 This program is free software: you can redistribute it and/or modify
02650b7f 5 it under the terms of the GNU General Public License as published by
f16b0819 6 the Free Software Foundation, either version 3 of the License, or
02650b7f 7 (at your option) any later version.
7086e707 8
02650b7f
PE
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.
7086e707 13
02650b7f 14 You should have received a copy of the GNU General Public License
f16b0819 15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
7086e707 16
231ed89a 17#include <config.h>
7086e707 18
7086e707
AD
19#include "bitsetv.h"
20
231ed89a
PE
21#include <stdlib.h>
22
7086e707
AD
23
24/* Create a vector of N_VECS bitsets, each of N_BITS, and of
25 type TYPE. */
26bitset *
a182371d
PE
27bitsetv_alloc (bitset_bindex n_vecs, bitset_bindex n_bits,
28 enum bitset_type type)
7086e707 29{
32e218da
PE
30 size_t vector_bytes;
31 size_t bytes;
ef017502 32 bitset *bsetv;
32e218da 33 bitset_bindex i;
a182371d 34
ef017502
AD
35 /* Determine number of bytes for each set. */
36 bytes = bitset_bytes (type, n_bits);
32e218da
PE
37
38 /* If size calculation overflows, memory is exhausted. */
39 if (BITSET_SIZE_MAX / (sizeof (bitset) + bytes) <= n_vecs)
40 xalloc_die ();
a182371d 41
ef017502 42 /* Allocate vector table at head of bitset array. */
f5e211a1
PE
43 vector_bytes = (n_vecs + 1) * sizeof (bitset) + bytes - 1;
44 vector_bytes -= vector_bytes % bytes;
89519adf 45 bsetv = xcalloc (1, vector_bytes + bytes * n_vecs);
a182371d 46
ef017502 47 for (i = 0; i < n_vecs; i++)
7086e707 48 {
f5e211a1 49 bsetv[i] = (bitset) (void *) ((char *) bsetv + vector_bytes + i * bytes);
a182371d 50
7086e707
AD
51 bitset_init (bsetv[i], n_bits, type);
52 }
a182371d 53
ef017502
AD
54 /* Null terminate table. */
55 bsetv[i] = 0;
56 return bsetv;
7086e707
AD
57}
58
59
60/* Create a vector of N_VECS bitsets, each of N_BITS, and with
61 attribute hints specified by ATTR. */
62bitset *
a182371d 63bitsetv_create (bitset_bindex n_vecs, bitset_bindex n_bits, unsigned int attr)
7086e707 64{
ef017502 65 enum bitset_type type;
a182371d 66
ef017502
AD
67 type = bitset_type_choose (n_bits, attr);
68 return bitsetv_alloc (n_vecs, n_bits, type);
7086e707
AD
69}
70
71
72/* Free bitset vector BSETV. */
73void
a182371d 74bitsetv_free (bitsetv bsetv)
7086e707 75{
32e218da 76 bitset_bindex i;
ef017502
AD
77
78 for (i = 0; bsetv[i]; i++)
79 BITSET_FREE_ (bsetv[i]);
80 free (bsetv);
7086e707
AD
81}
82
83
ef017502 84/* Zero a vector of bitsets. */
7086e707 85void
a182371d 86bitsetv_zero (bitsetv bsetv)
7086e707 87{
32e218da 88 bitset_bindex i;
a182371d 89
ef017502
AD
90 for (i = 0; bsetv[i]; i++)
91 bitset_zero (bsetv[i]);
7086e707
AD
92}
93
94
ef017502 95/* Set a vector of bitsets to ones. */
7086e707 96void
a182371d 97bitsetv_ones (bitsetv bsetv)
7086e707 98{
32e218da 99 bitset_bindex i;
a182371d 100
ef017502 101 for (i = 0; bsetv[i]; i++)
7086e707
AD
102 bitset_ones (bsetv[i]);
103}
104
105
345cea78
AD
106/* Given a vector BSETV of N bitsets of size N, modify its contents to
107 be the transitive closure of what was given. */
108void
a182371d 109bitsetv_transitive_closure (bitsetv bsetv)
345cea78 110{
32e218da
PE
111 bitset_bindex i;
112 bitset_bindex j;
345cea78
AD
113
114 for (i = 0; bsetv[i]; i++)
115 for (j = 0; bsetv[j]; j++)
116 if (bitset_test (bsetv[j], i))
a182371d 117 bitset_or (bsetv[j], bsetv[j], bsetv[i]);
345cea78
AD
118}
119
120
121/* Given a vector BSETV of N bitsets of size N, modify its contents to
a182371d 122 be the reflexive transitive closure of what was given. This is
345cea78
AD
123 the same as transitive closure but with all bits on the diagonal
124 of the bit matrix set. */
125void
126bitsetv_reflexive_transitive_closure (bitsetv bsetv)
127{
32e218da 128 bitset_bindex i;
345cea78
AD
129
130 bitsetv_transitive_closure (bsetv);
131 for (i = 0; bsetv[i]; i++)
132 bitset_set (bsetv[i], i);
133}
134
135
7086e707
AD
136/* Dump the contents of a bitset vector BSETV with N_VECS elements to
137 FILE. */
138void
a182371d
PE
139bitsetv_dump (FILE *file, char const *title, char const *subtitle,
140 bitsetv bsetv)
7086e707 141{
32e218da 142 bitset_windex i;
a182371d 143
7086e707 144 fprintf (file, "%s\n", title);
ef017502 145 for (i = 0; bsetv[i]; i++)
7086e707 146 {
779e7ceb 147 fprintf (file, "%s %lu\n", subtitle, (unsigned long int) i);
7086e707
AD
148 bitset_dump (file, bsetv[i]);
149 }
a182371d 150
2f4f028d 151 fprintf (file, "\n");
7086e707 152}
ef017502
AD
153
154
155void
a182371d 156debug_bitsetv (bitsetv bsetv)
ef017502 157{
32e218da 158 bitset_windex i;
a182371d 159
ef017502
AD
160 for (i = 0; bsetv[i]; i++)
161 {
779e7ceb 162 fprintf (stderr, "%lu: ", (unsigned long int) i);
ef017502
AD
163 debug_bitset (bsetv[i]);
164 }
a182371d 165
2f4f028d 166 fprintf (stderr, "\n");
ef017502 167}