package: codespell
[bison.git] / src / relation.c
blob23f1c1b7c208e7c7d11a35534a1d442f8b557989
1 /* Binary relations.
3 Copyright (C) 2002, 2004-2005, 2009-2015, 2018-2021 Free Software
4 Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include "system.h"
24 #include <bitsetv.h>
26 #include "getargs.h"
27 #include "relation.h"
29 void
30 relation_print (const char *title,
31 relation r, relation_node size,
32 relation_node_print print, FILE *out)
34 if (title)
35 fprintf (out, "%s:\n", title);
36 for (relation_node i = 0; i < size; ++i)
37 if (r[i])
39 fputs (" ", out);
40 if (print)
41 print (i, out);
42 else
43 fprintf (out, "%3ld", (long) i);
44 fputc (':', out);
45 for (relation_node j = 0; r[i][j] != END_NODE; ++j)
47 fputc (' ', out);
48 if (print)
49 print (r[i][j], out);
50 else
51 fprintf (out, "%3ld", (long) r[i][j]);
53 fputc ('\n', out);
55 fputc ('\n', out);
59 /*---------------------------------------------------------------.
60 | digraph & traverse. |
61 | |
62 | The following variables are used as common storage between the |
63 | two. |
64 `---------------------------------------------------------------*/
66 static relation R;
67 static relation_nodes indexes;
68 static relation_nodes vertices;
69 static relation_node top;
70 static relation_node infinity;
71 static bitsetv F;
73 static void
74 traverse (relation_node i)
76 vertices[++top] = i;
77 relation_node height = indexes[i] = top;
79 if (R[i])
80 for (relation_node j = 0; R[i][j] != END_NODE; ++j)
82 if (indexes[R[i][j]] == 0)
83 traverse (R[i][j]);
85 if (indexes[i] > indexes[R[i][j]])
86 indexes[i] = indexes[R[i][j]];
88 bitset_or (F[i], F[i], F[R[i][j]]);
91 if (indexes[i] == height)
92 for (;;)
94 relation_node j = vertices[top--];
95 indexes[j] = infinity;
97 if (i == j)
98 break;
100 bitset_copy (F[j], F[i]);
105 void
106 relation_digraph (relation r, relation_node size, bitsetv function)
108 infinity = size + 2;
109 indexes = xcalloc (size + 1, sizeof *indexes);
110 vertices = xnmalloc (size + 1, sizeof *vertices);
111 top = 0;
113 R = r;
114 F = function;
116 for (relation_node i = 0; i < size; i++)
117 if (indexes[i] == 0 && R[i])
118 traverse (i);
120 free (indexes);
121 free (vertices);
123 function = F;
127 /*-------------------------------------------.
128 | Destructively transpose R_ARG, of size N. |
129 `-------------------------------------------*/
131 void
132 relation_transpose (relation *R_arg, relation_node size)
134 relation r = *R_arg;
136 if (trace_flag & trace_sets)
137 relation_print ("relation_transpose", r, size, NULL, stderr);
139 /* Count. */
140 /* NEDGES[I] -- total size of NEW_R[I]. */
141 size_t *nedges = xcalloc (size, sizeof *nedges);
142 for (relation_node i = 0; i < size; i++)
143 if (r[i])
144 for (relation_node j = 0; r[i][j] != END_NODE; ++j)
145 ++nedges[r[i][j]];
147 /* Allocate. */
148 /* The result. */
149 relation new_R = xnmalloc (size, sizeof *new_R);
150 /* END_R[I] -- next entry of NEW_R[I]. */
151 relation end_R = xnmalloc (size, sizeof *end_R);
152 for (relation_node i = 0; i < size; i++)
154 relation_node *sp = NULL;
155 if (nedges[i] > 0)
157 sp = xnmalloc (nedges[i] + 1, sizeof *sp);
158 sp[nedges[i]] = END_NODE;
160 new_R[i] = sp;
161 end_R[i] = sp;
164 /* Store. */
165 for (relation_node i = 0; i < size; i++)
166 if (r[i])
167 for (relation_node j = 0; r[i][j] != END_NODE; ++j)
168 *end_R[r[i][j]]++ = i;
170 free (nedges);
171 free (end_R);
173 /* Free the input: it is replaced with the result. */
174 for (relation_node i = 0; i < size; i++)
175 free (r[i]);
176 free (r);
178 if (trace_flag & trace_sets)
179 relation_print ("relation_transpose: output", new_R, size, NULL, stderr);
181 *R_arg = new_R;