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