]> git.saurik.com Git - bison.git/blame - lib/bitsetv.c
Update copyright date.
[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 *
a182371d
PE
32bitsetv_alloc (bitset_bindex n_vecs, bitset_bindex n_bits,
33 enum bitset_type type)
7086e707 34{
32e218da
PE
35 size_t vector_bytes;
36 size_t bytes;
ef017502 37 bitset *bsetv;
32e218da 38 bitset_bindex i;
a182371d 39
ef017502
AD
40 /* Determine number of bytes for each set. */
41 bytes = bitset_bytes (type, n_bits);
32e218da
PE
42
43 /* If size calculation overflows, memory is exhausted. */
44 if (BITSET_SIZE_MAX / (sizeof (bitset) + bytes) <= n_vecs)
45 xalloc_die ();
a182371d 46
ef017502 47 /* Allocate vector table at head of bitset array. */
f5e211a1
PE
48 vector_bytes = (n_vecs + 1) * sizeof (bitset) + bytes - 1;
49 vector_bytes -= vector_bytes % bytes;
ef017502 50 bsetv = (bitset *) xcalloc (1, vector_bytes + bytes * n_vecs);
a182371d 51
ef017502 52 for (i = 0; i < n_vecs; i++)
7086e707 53 {
f5e211a1 54 bsetv[i] = (bitset) (void *) ((char *) bsetv + vector_bytes + i * bytes);
a182371d 55
7086e707
AD
56 bitset_init (bsetv[i], n_bits, type);
57 }
a182371d 58
ef017502
AD
59 /* Null terminate table. */
60 bsetv[i] = 0;
61 return bsetv;
7086e707
AD
62}
63
64
65/* Create a vector of N_VECS bitsets, each of N_BITS, and with
66 attribute hints specified by ATTR. */
67bitset *
a182371d 68bitsetv_create (bitset_bindex n_vecs, bitset_bindex n_bits, unsigned int attr)
7086e707 69{
ef017502 70 enum bitset_type type;
a182371d 71
ef017502
AD
72 type = bitset_type_choose (n_bits, attr);
73 return bitsetv_alloc (n_vecs, n_bits, type);
7086e707
AD
74}
75
76
77/* Free bitset vector BSETV. */
78void
a182371d 79bitsetv_free (bitsetv bsetv)
7086e707 80{
32e218da 81 bitset_bindex i;
ef017502
AD
82
83 for (i = 0; bsetv[i]; i++)
84 BITSET_FREE_ (bsetv[i]);
85 free (bsetv);
7086e707
AD
86}
87
88
ef017502 89/* Zero a vector of bitsets. */
7086e707 90void
a182371d 91bitsetv_zero (bitsetv bsetv)
7086e707 92{
32e218da 93 bitset_bindex i;
a182371d 94
ef017502
AD
95 for (i = 0; bsetv[i]; i++)
96 bitset_zero (bsetv[i]);
7086e707
AD
97}
98
99
ef017502 100/* Set a vector of bitsets to ones. */
7086e707 101void
a182371d 102bitsetv_ones (bitsetv bsetv)
7086e707 103{
32e218da 104 bitset_bindex i;
a182371d 105
ef017502 106 for (i = 0; bsetv[i]; i++)
7086e707
AD
107 bitset_ones (bsetv[i]);
108}
109
110
345cea78
AD
111/* Given a vector BSETV of N bitsets of size N, modify its contents to
112 be the transitive closure of what was given. */
113void
a182371d 114bitsetv_transitive_closure (bitsetv bsetv)
345cea78 115{
32e218da
PE
116 bitset_bindex i;
117 bitset_bindex j;
345cea78
AD
118
119 for (i = 0; bsetv[i]; i++)
120 for (j = 0; bsetv[j]; j++)
121 if (bitset_test (bsetv[j], i))
a182371d 122 bitset_or (bsetv[j], bsetv[j], bsetv[i]);
345cea78
AD
123}
124
125
126/* Given a vector BSETV of N bitsets of size N, modify its contents to
a182371d 127 be the reflexive transitive closure of what was given. This is
345cea78
AD
128 the same as transitive closure but with all bits on the diagonal
129 of the bit matrix set. */
130void
131bitsetv_reflexive_transitive_closure (bitsetv bsetv)
132{
32e218da 133 bitset_bindex i;
345cea78
AD
134
135 bitsetv_transitive_closure (bsetv);
136 for (i = 0; bsetv[i]; i++)
137 bitset_set (bsetv[i], i);
138}
139
140
7086e707
AD
141/* Dump the contents of a bitset vector BSETV with N_VECS elements to
142 FILE. */
143void
a182371d
PE
144bitsetv_dump (FILE *file, char const *title, char const *subtitle,
145 bitsetv bsetv)
7086e707 146{
32e218da 147 bitset_windex i;
a182371d 148
7086e707 149 fprintf (file, "%s\n", title);
ef017502 150 for (i = 0; bsetv[i]; i++)
7086e707 151 {
32e218da 152 fprintf (file, "%s %lu\n", subtitle, (unsigned long) i);
7086e707
AD
153 bitset_dump (file, bsetv[i]);
154 }
a182371d 155
7086e707
AD
156 fprintf (file, "\n");
157}
ef017502
AD
158
159
160void
a182371d 161debug_bitsetv (bitsetv bsetv)
ef017502 162{
32e218da 163 bitset_windex i;
a182371d 164
ef017502
AD
165 for (i = 0; bsetv[i]; i++)
166 {
32e218da 167 fprintf (stderr, "%lu: ", (unsigned long) i);
ef017502
AD
168 debug_bitset (bsetv[i]);
169 }
a182371d 170
ef017502
AD
171 fprintf (stderr, "\n");
172}