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