]> git.saurik.com Git - bison.git/blame - src/relation.c
(allocate_itemsets): Use xnmalloc, not xcalloc,
[bison.git] / src / relation.c
CommitLineData
0e4d5753 1/* Binary relations.
779e7ceb 2 Copyright (C) 2002, 2004 Free Software Foundation, Inc.
0e4d5753
AD
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
6 Bison 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 2, or (at your option)
9 any later version.
10
11 Bison 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.
15
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21#include "system.h"
17ee7397
PE
22
23#include <bitsetv.h>
24
0e4d5753 25#include "getargs.h"
17ee7397 26#include "relation.h"
0e4d5753
AD
27
28void
17ee7397 29relation_print (relation r, size_t size, FILE *out)
0e4d5753 30{
779e7ceb
PE
31 unsigned int i;
32 unsigned int j;
0e4d5753
AD
33
34 for (i = 0; i < size; ++i)
35 {
36 fprintf (out, "%3d: ", i);
17ee7397 37 if (r[i])
0859e438 38 for (j = 0; r[i][j] != END_NODE; ++j)
17ee7397 39 fprintf (out, "%3d ", r[i][j]);
0e4d5753
AD
40 fputc ('\n', out);
41 }
42 fputc ('\n', out);
43}
44
45
46/*---------------------------------------------------------------.
47| digraph & traverse. |
48| |
49| The following variables are used as common storage between the |
50| two. |
51`---------------------------------------------------------------*/
52
17ee7397
PE
53static relation R;
54static relation_nodes INDEX;
55static relation_nodes VERTICES;
0e4d5753
AD
56static int top;
57static int infinity;
58static bitsetv F;
59
60static void
61traverse (int i)
62{
63 int j;
64 int height;
65
66 VERTICES[++top] = i;
67 INDEX[i] = height = top;
68
69 if (R[i])
0859e438 70 for (j = 0; R[i][j] != END_NODE; ++j)
0e4d5753
AD
71 {
72 if (INDEX[R[i][j]] == 0)
73 traverse (R[i][j]);
74
75 if (INDEX[i] > INDEX[R[i][j]])
76 INDEX[i] = INDEX[R[i][j]];
77
78 bitset_or (F[i], F[i], F[R[i][j]]);
79 }
80
81 if (INDEX[i] == height)
82 for (;;)
83 {
84 j = VERTICES[top--];
85 INDEX[j] = infinity;
86
87 if (i == j)
88 break;
89
90 bitset_copy (F[j], F[i]);
91 }
92}
93
94
95void
17ee7397 96relation_digraph (relation r, size_t size, bitsetv *function)
0e4d5753 97{
779e7ceb 98 unsigned int i;
0e4d5753
AD
99
100 infinity = size + 2;
f2a655b6
PE
101 CALLOC (INDEX, size + 1);
102 CALLOC (VERTICES, size + 1);
0e4d5753
AD
103 top = 0;
104
17ee7397 105 R = r;
0e4d5753
AD
106 F = *function;
107
108 for (i = 0; i < size; i++)
109 INDEX[i] = 0;
110
111 for (i = 0; i < size; i++)
112 if (INDEX[i] == 0 && R[i])
113 traverse (i);
114
afbb696d
PE
115 free (INDEX);
116 free (VERTICES);
0e4d5753
AD
117
118 *function = F;
119}
120
121
122/*-------------------------------------------.
123| Destructively transpose R_ARG, of size N. |
124`-------------------------------------------*/
125
126void
17ee7397 127relation_transpose (relation *R_arg, int n)
0e4d5753
AD
128{
129 /* The result. */
f2a655b6 130 relation new_R = CALLOC (new_R, n);
0e4d5753 131 /* END_R[I] -- next entry of NEW_R[I]. */
f2a655b6 132 relation end_R = CALLOC (end_R, n);
0e4d5753 133 /* NEDGES[I] -- total size of NEW_R[I]. */
f2a655b6 134 int *nedges = CALLOC (nedges, n);
0e4d5753
AD
135 int i, j;
136
273a74fa 137 if (trace_flag & trace_sets)
0e4d5753 138 {
427c0dda 139 fputs ("relation_transpose: input\n", stderr);
0e4d5753
AD
140 relation_print (*R_arg, n, stderr);
141 }
142
143 /* Count. */
144 for (i = 0; i < n; i++)
145 if ((*R_arg)[i])
0859e438 146 for (j = 0; (*R_arg)[i][j] != END_NODE; ++j)
0e4d5753
AD
147 ++nedges[(*R_arg)[i][j]];
148
149 /* Allocate. */
150 for (i = 0; i < n; i++)
151 if (nedges[i] > 0)
152 {
f2a655b6 153 relation_node *sp = CALLOC (sp, nedges[i] + 1);
0859e438 154 sp[nedges[i]] = END_NODE;
0e4d5753
AD
155 new_R[i] = sp;
156 end_R[i] = sp;
157 }
158
159 /* Store. */
160 for (i = 0; i < n; i++)
161 if ((*R_arg)[i])
0859e438 162 for (j = 0; (*R_arg)[i][j] != END_NODE; ++j)
0e4d5753
AD
163 {
164 *end_R[(*R_arg)[i][j]] = i;
165 ++end_R[(*R_arg)[i][j]];
166 }
167
168 free (nedges);
169 free (end_R);
170
171 /* Free the input: it is replaced with the result. */
172 for (i = 0; i < n; i++)
afbb696d 173 free ((*R_arg)[i]);
0e4d5753
AD
174 free (*R_arg);
175
273a74fa 176 if (trace_flag & trace_sets)
0e4d5753 177 {
427c0dda 178 fputs ("relation_transpose: output\n", stderr);
0e4d5753
AD
179 relation_print (new_R, n, stderr);
180 }
181
182 *R_arg = new_R;
183}