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