-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdag-io.cpp
306 lines (241 loc) · 7.1 KB
/
dag-io.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* PET
* Platform for Experimentation with efficient HPSG processing Techniques
* (C) 1999 - 2002 Ulrich Callmeier [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* i/o operations for simple typed dags */
#include "dumper.h"
#include "dag.h"
#include "types.h"
//
// readable output of dag in OSF format
//
void dag_mark_coreferences(struct dag_node *dag)
// recursively set `visit' field of dag nodes to number of occurences
// in this dag - any nodes with more than one occurence are `coreferenced'
{
dag = dag_deref(dag);
if(dag_set_visit(dag, dag_get_visit(dag) + 1) == 1)
{ // not yet visited
struct dag_arc *arc;
arc = dag->arcs;
while(arc)
{
dag_mark_coreferences(arc->val);
arc = arc->next;
}
}
}
/*
int coref_nr;
void dag_print_rec(FILE *f, struct dag_node *dag, int indent)
// recursively print dag. requires `visit' field to be set up by
// mark_coreferences. negative value in `visit' field means that node
// is coreferenced, and already printed
{
int coref;
struct dag_arc *arc;
dag = dag_deref(dag);
coref = dag_get_visit(dag) - 1;
if(coref < 0) // dag is coreferenced, already printed
{
fprintf(f, "#%d", -(coref+1));
return;
}
else if(coref > 0) // dag is coreferenced, not printed yet
{
coref = -dag_set_visit(dag, -(coref_nr++));
indent += fprintf(f, "#%d:", coref);
}
#ifdef PETDEBUG
fprintf(f, "%s (%x)", type_name(dag->type), (int) dag);
#else
fprintf(f, "%s", type_name(dag->type));
#endif
if((arc = dag->arcs) != 0)
{
struct dag_node **print_attrs = (struct dag_node **)
malloc(sizeof(struct dag_node *) * nattrs);
int maxlen = 0, i, maxatt = 0;
for(i = 0; i < nattrs; ++i)
print_attrs[i] = 0;
while(arc)
{
i = attrnamelen[arc->attr];
maxlen = maxlen > i ? maxlen : i;
print_attrs[arc->attr]=arc->val;
maxatt = arc->attr > maxatt ? arc->attr : maxatt;
arc=arc->next;
}
fprintf(f, "\n%*s[ ", indent, "");
bool first = true;
for(int j = 0; j <= maxatt; ++j) if(print_attrs[j])
{
i = attrnamelen[j];
if(!first)
fprintf(f, ",\n%*s",indent + 2,"");
else
first = false;
fprintf(f, "%s%*s", attrname[j], maxlen-i+1, "");
dag_print_rec(f, print_attrs[j], indent + 2 + maxlen + 1);
}
fprintf(f, " ]");
free(print_attrs);
}
}
void dag_print(FILE *f, struct dag_node *dag)
{
if(dag == 0)
{
fprintf(f, "%s", type_name(0));
return;
}
if(dag == FAIL)
{
fprintf(f, "fail");
return;
}
dag_mark_coreferences(dag);
coref_nr = 1;
dag_print_rec(f, dag, 0);
dag_invalidate_visited();
}
*/
//
// compact binary form output (dumping)
//
int dag_dump_grand_total_nodes = 0;
int dag_dump_grand_total_atomic = 0;
int dag_dump_grand_total_arcs = 0;
int dag_dump_total_nodes, dag_dump_total_arcs;
void dag_mark_dump_nodes(struct dag_node *dag)
{
dag = dag_deref(dag);
if(dag_get_visit(dag) == 0)
{
struct dag_arc *arc;
dag_set_visit(dag, 1); // value doesn't matter, but not zero
if(!dag->arcs)
dag_dump_grand_total_atomic++;
arc = dag->arcs;
while(arc)
{
dag_dump_total_arcs++;
dag_mark_dump_nodes(arc->val);
arc = arc->next;
}
dag_set_visit(dag, dag_dump_total_nodes++);
}
}
int dump_index = 0;
void dag_dump_rec(dumper *f, struct dag_node *dag)
{
int visit;
static struct dag_node_dump dump_n;
static struct dag_arc_dump dump_a;
dag = dag_deref(dag);
visit = dag_get_visit(dag);
if(visit > 0) // first time
{
struct dag_arc *arc;
int nr;
int type;
dag_set_visit(dag, -visit);
arc = dag->arcs;
while(arc)
{
dag_dump_rec(f, arc->val);
arc = arc->next;
}
arc = dag->arcs; nr = 0;
while(arc) {
++nr;
arc = arc->next;
}
type = dag->type;
#ifdef FLOP
// always dump with cheap type codes
type = flop2cheap[type];
#endif
dump_n.type = type;
dump_n.nattrs = (short int) nr;
dump_node(f, &dump_n);
++dump_index;
arc = dag->arcs;
while(arc)
{
dump_a.attr = (short) arc->attr;
dump_a.val = (short) (abs(dag_get_visit(dag_deref(arc->val))) - 1);
// this assertion fails on cyclic feature structures, but why is it
// necessary? I want to be able to dump cyclic restrictors. (bk)
//assert(dump_a.val < dump_index);
dump_arc(f, &dump_a);
arc = arc->next;
}
}
}
bool dag_dump(dumper *f, struct dag_node *dag)
{
if(dag == 0 || dag == FAIL)
{
return false;
}
dump_index = 0;
dag_dump_total_nodes = 1; dag_dump_total_arcs = 0;
dag_mark_dump_nodes(dag);
--dag_dump_total_nodes;
f->dump_int(dag_dump_total_nodes);
f->dump_int(dag_dump_total_arcs);
dag_dump_rec(f, dag);
dag_invalidate_visited();
dag_dump_grand_total_nodes += dag_dump_total_nodes;
dag_dump_grand_total_arcs += dag_dump_total_arcs;
return true;
}
struct dag_node *dag_undump(dumper *f)
{
struct dag_node *undumped_nodes;
struct dag_arc *undumped_arcs = NULL;
struct dag_node_dump dump_n;
struct dag_arc_dump dump_a;
dag_dump_total_nodes = f->undump_int();
dag_dump_total_arcs = f->undump_int();
if((undumped_nodes = (struct dag_node *) dag_alloc_p_nodes(dag_dump_total_nodes)) == 0)
return FAIL;
if(dag_dump_total_arcs > 0)
if((undumped_arcs = (struct dag_arc *) dag_alloc_p_arcs(dag_dump_total_arcs)) == 0)
return FAIL;
int current_arc = 0;
for(int i = 0; i < dag_dump_total_nodes; ++i)
{
undump_node(f, &dump_n);
if(dump_n.type < 0)
dump_n.type = -dump_n.type; // node is not expanded
dag_init(undumped_nodes+i, dump_n.type);
if(dump_n.nattrs > 0)
undumped_nodes[i].arcs = undumped_arcs+current_arc;
for(int j = 0; j < dump_n.nattrs; ++j)
{
undump_arc(f, &dump_a);
undumped_arcs[current_arc].attr = dump_a.attr;
undumped_arcs[current_arc].val = undumped_nodes + dump_a.val;
undumped_arcs[current_arc].next =
(j == dump_n.nattrs - 1) ? 0 : undumped_arcs + current_arc+1;
++current_arc;
}
}
return undumped_nodes + dag_dump_total_nodes - 1;
}