]>
git.saurik.com Git - bison.git/blob - lib/bitsetv.c
2 Copyright (C) 2001, 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 /* Create a vector of N_VECS bitsets, each of N_BITS, and of
27 bitsetv_alloc (bitset_bindex n_vecs
, bitset_bindex n_bits
,
28 enum bitset_type type
)
35 /* Determine number of bytes for each set. */
36 bytes
= bitset_bytes (type
, n_bits
);
38 /* If size calculation overflows, memory is exhausted. */
39 if (BITSET_SIZE_MAX
/ (sizeof (bitset
) + bytes
) <= n_vecs
)
42 /* Allocate vector table at head of bitset array. */
43 vector_bytes
= (n_vecs
+ 1) * sizeof (bitset
) + bytes
- 1;
44 vector_bytes
-= vector_bytes
% bytes
;
45 bsetv
= xcalloc (1, vector_bytes
+ bytes
* n_vecs
);
47 for (i
= 0; i
< n_vecs
; i
++)
49 bsetv
[i
] = (bitset
) (void *) ((char *) bsetv
+ vector_bytes
+ i
* bytes
);
51 bitset_init (bsetv
[i
], n_bits
, type
);
54 /* Null terminate table. */
60 /* Create a vector of N_VECS bitsets, each of N_BITS, and with
61 attribute hints specified by ATTR. */
63 bitsetv_create (bitset_bindex n_vecs
, bitset_bindex n_bits
, unsigned int attr
)
65 enum bitset_type type
;
67 type
= bitset_type_choose (n_bits
, attr
);
68 return bitsetv_alloc (n_vecs
, n_bits
, type
);
72 /* Free bitset vector BSETV. */
74 bitsetv_free (bitsetv bsetv
)
78 for (i
= 0; bsetv
[i
]; i
++)
79 BITSET_FREE_ (bsetv
[i
]);
84 /* Zero a vector of bitsets. */
86 bitsetv_zero (bitsetv bsetv
)
90 for (i
= 0; bsetv
[i
]; i
++)
91 bitset_zero (bsetv
[i
]);
95 /* Set a vector of bitsets to ones. */
97 bitsetv_ones (bitsetv bsetv
)
101 for (i
= 0; bsetv
[i
]; i
++)
102 bitset_ones (bsetv
[i
]);
106 /* Given a vector BSETV of N bitsets of size N, modify its contents to
107 be the transitive closure of what was given. */
109 bitsetv_transitive_closure (bitsetv bsetv
)
114 for (i
= 0; bsetv
[i
]; i
++)
115 for (j
= 0; bsetv
[j
]; j
++)
116 if (bitset_test (bsetv
[j
], i
))
117 bitset_or (bsetv
[j
], bsetv
[j
], bsetv
[i
]);
121 /* Given a vector BSETV of N bitsets of size N, modify its contents to
122 be the reflexive transitive closure of what was given. This is
123 the same as transitive closure but with all bits on the diagonal
124 of the bit matrix set. */
126 bitsetv_reflexive_transitive_closure (bitsetv bsetv
)
130 bitsetv_transitive_closure (bsetv
);
131 for (i
= 0; bsetv
[i
]; i
++)
132 bitset_set (bsetv
[i
], i
);
136 /* Dump the contents of a bitset vector BSETV with N_VECS elements to
139 bitsetv_dump (FILE *file
, char const *title
, char const *subtitle
,
144 fprintf (file
, "%s\n", title
);
145 for (i
= 0; bsetv
[i
]; i
++)
147 fprintf (file
, "%s %lu\n", subtitle
, (unsigned long int) i
);
148 bitset_dump (file
, bsetv
[i
]);
151 fprintf (file
, "\n");
156 debug_bitsetv (bitsetv bsetv
)
160 for (i
= 0; bsetv
[i
]; i
++)
162 fprintf (stderr
, "%lu: ", (unsigned long int) i
);
163 debug_bitset (bsetv
[i
]);
166 fprintf (stderr
, "\n");