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