]>
git.saurik.com Git - bison.git/blob - src/relation.c
2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 relation_print (relation r
, relation_node size
, FILE *out
)
33 for (i
= 0; i
< size
; ++i
)
35 fprintf (out
, "%3lu: ", (unsigned long int) i
);
37 for (j
= 0; r
[i
][j
] != END_NODE
; ++j
)
38 fprintf (out
, "%3lu ", (unsigned long int) r
[i
][j
]);
45 /*---------------------------------------------------------------.
46 | digraph & traverse. |
48 | The following variables are used as common storage between the |
50 `---------------------------------------------------------------*/
53 static relation_nodes INDEX
;
54 static relation_nodes VERTICES
;
55 static relation_node top
;
56 static relation_node infinity
;
60 traverse (relation_node i
)
66 INDEX
[i
] = height
= top
;
69 for (j
= 0; R
[i
][j
] != END_NODE
; ++j
)
71 if (INDEX
[R
[i
][j
]] == 0)
74 if (INDEX
[i
] > INDEX
[R
[i
][j
]])
75 INDEX
[i
] = INDEX
[R
[i
][j
]];
77 bitset_or (F
[i
], F
[i
], F
[R
[i
][j
]]);
80 if (INDEX
[i
] == height
)
89 bitset_copy (F
[j
], F
[i
]);
95 relation_digraph (relation r
, relation_node size
, bitsetv
*function
)
100 INDEX
= xcalloc (size
+ 1, sizeof *INDEX
);
101 VERTICES
= xnmalloc (size
+ 1, sizeof *VERTICES
);
107 for (i
= 0; i
< size
; i
++)
108 if (INDEX
[i
] == 0 && R
[i
])
118 /*-------------------------------------------.
119 | Destructively transpose R_ARG, of size N. |
120 `-------------------------------------------*/
123 relation_transpose (relation
*R_arg
, relation_node n
)
127 relation new_R
= xnmalloc (n
, sizeof *new_R
);
128 /* END_R[I] -- next entry of NEW_R[I]. */
129 relation end_R
= xnmalloc (n
, sizeof *end_R
);
130 /* NEDGES[I] -- total size of NEW_R[I]. */
131 size_t *nedges
= xcalloc (n
, sizeof *nedges
);
135 if (trace_flag
& trace_sets
)
137 fputs ("relation_transpose: input\n", stderr
);
138 relation_print (r
, n
, stderr
);
142 for (i
= 0; i
< n
; i
++)
144 for (j
= 0; r
[i
][j
] != END_NODE
; ++j
)
148 for (i
= 0; i
< n
; i
++)
150 relation_node
*sp
= NULL
;
153 sp
= xnmalloc (nedges
[i
] + 1, sizeof *sp
);
154 sp
[nedges
[i
]] = END_NODE
;
161 for (i
= 0; i
< n
; i
++)
163 for (j
= 0; r
[i
][j
] != END_NODE
; ++j
)
164 *end_R
[r
[i
][j
]]++ = i
;
169 /* Free the input: it is replaced with the result. */
170 for (i
= 0; i
< n
; i
++)
174 if (trace_flag
& trace_sets
)
176 fputs ("relation_transpose: output\n", stderr
);
177 relation_print (new_R
, n
, stderr
);