1 |
/* Generate transitive closure of a matrix, |
/* Generate transitive closure of a matrix, |
2 |
Copyright 1984, 1989, 2000, 2001 Free Software Foundation, Inc. |
Copyright 1984, 1989, 2000, 2001, 2002 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of Bison, the GNU Compiler Compiler. |
This file is part of Bison, the GNU Compiler Compiler. |
5 |
|
|
21 |
|
|
22 |
#include "system.h" |
#include "system.h" |
23 |
#include "getargs.h" |
#include "getargs.h" |
24 |
|
#include "bitset.h" |
25 |
#include "warshall.h" |
#include "warshall.h" |
26 |
|
|
27 |
/*-------------------------------------------------------------. |
/*--------------------------------------------------------. |
28 |
| Given n by n matrix of bits R, modify its contents to be the | |
| Display the MATRIX array of SIZE bitsets of size SIZE. | |
29 |
| transive closure of what was given. | |
`--------------------------------------------------------*/ |
|
`-------------------------------------------------------------*/ |
|
30 |
|
|
31 |
static void |
static void |
32 |
bitmatrix_print (const char *title, unsigned *matrix, size_t size) |
bitmatrix_print (const char *title, bitset *matrix, size_t size) |
33 |
{ |
{ |
34 |
size_t i, j; |
size_t i, j; |
|
size_t rowsize = WORDSIZE (size) * sizeof (unsigned); |
|
|
#define ROW(Num) ((unsigned *) ((char *) matrix + ((Num) * rowsize))) |
|
35 |
|
|
36 |
/* Title. */ |
/* Title. */ |
37 |
fprintf (stderr, "%s BEGIN\n", title); |
fprintf (stderr, "%s BEGIN\n", title); |
57 |
{ |
{ |
58 |
fprintf (stderr, "%2d|", i); |
fprintf (stderr, "%2d|", i); |
59 |
for (j = 0; j < size; ++j) |
for (j = 0; j < size; ++j) |
60 |
fputs (BITISSET (ROW (i), j) ? "1" : " ", stderr); |
fputs (bitset_test (matrix[i], j) ? "1" : " ", stderr); |
61 |
fputs ("|\n", stderr); |
fputs ("|\n", stderr); |
62 |
} |
} |
63 |
|
|
71 |
fprintf (stderr, "%s END\n\n", title); |
fprintf (stderr, "%s END\n\n", title); |
72 |
} |
} |
73 |
|
|
74 |
#define R(Num) (unsigned *) ((char *) R + ((Num) * rowsize)) |
/*-------------------------------------------------------------------. |
75 |
|
| Given the MATRIX array of N bitsets of size N, modify its contents | |
76 |
|
| to be the transive closure of what was given. | |
77 |
|
`-------------------------------------------------------------------*/ |
78 |
|
|
79 |
static void |
static void |
80 |
TC (unsigned *R, int n) |
TC (bitset *matrix, int n) |
81 |
{ |
{ |
|
int rowsize = WORDSIZE (n) * sizeof (unsigned); |
|
82 |
int i, j, k; |
int i, j, k; |
83 |
|
|
84 |
if (trace_flag) |
if (trace_flag) |
85 |
bitmatrix_print ("TC: Input", R, n); |
bitmatrix_print ("TC: Input", matrix, n); |
86 |
|
|
87 |
/* R (J, I) && R (I, K) => R (J, K). |
/* R (J, I) && R (I, K) => R (J, K). |
88 |
I *must* be the outter loop. */ |
I *must* be the outter loop. */ |
89 |
|
|
90 |
for (i = 0; i < n; ++i) |
for (i = 0; i < n; ++i) |
91 |
for (j = 0; j < n; ++j) |
for (j = 0; j < n; ++j) |
92 |
if (BITISSET (R (j), i)) |
if (bitset_test (matrix[j], i)) |
93 |
for (k = 0; k < n; ++k) |
for (k = 0; k < n; ++k) |
94 |
if (BITISSET (R (i), k)) |
if (bitset_test (matrix[i], k)) |
95 |
SETBIT (R (j), k); |
bitset_set (matrix[j], k); |
96 |
|
|
97 |
if (trace_flag) |
if (trace_flag) |
98 |
bitmatrix_print ("TC: Output", R, n); |
bitmatrix_print ("TC: Output", matrix, n); |
99 |
} |
} |
100 |
|
|
101 |
|
|
102 |
/*---------------------------------------------------------------. |
/*---------------------------------------------------------------. |
103 |
| Reflexive Transitive Closure. Same as TC and then set all the | |
| Reflexive Transitive Closure. Same as TC and then set all the | |
104 |
| bits on the diagonal of R. | |
| bits on the diagonal of MATRIX. | |
105 |
`---------------------------------------------------------------*/ |
`---------------------------------------------------------------*/ |
106 |
|
|
107 |
void |
void |
108 |
RTC (unsigned *R, int n) |
RTC (bitset *matrix, int n) |
109 |
{ |
{ |
|
int rowsize = WORDSIZE (n) * sizeof (unsigned); |
|
110 |
int i; |
int i; |
111 |
|
|
112 |
TC (R, n); |
TC (matrix, n); |
113 |
for (i = 0; i < n; ++i) |
for (i = 0; i < n; ++i) |
114 |
SETBIT (R (i), i); |
bitset_set (matrix[i], i); |
115 |
} |
} |