11 #include "graph-data.h"
13 #define MMALLOC(sz) mmalloc(sz,__FILE__,__LINE__)
14 #define MREALLOC(p,sz) mrealloc(p,sz,__FILE__,__LINE__)
16 static void *mmalloccheck(void *p, const char *op, size_t sz,
17 const char *file, int line) {
19 fprintf(stderr,"%s:%d: %s of %lu failed: %s\n",
20 file, line, op, (unsigned long)sz, strerror(errno));
24 static void *mmalloc(size_t sz, const char *file, int line) {
28 return mmalloccheck(p,"malloc",sz,file,line);
31 static void *mrealloc(void *p, size_t sz, const char *file, int line) {
33 return mmalloccheck(p,"realloc",sz,file,line);
36 static char *mvasprintf(const char *fmt, va_list al)
37 __attribute__((format(printf,1,0)));
38 static char *mvasprintf(const char *fmt, va_list al) {
42 r= vasprintf(&p,fmt,al);
44 fprintf(stderr,"vasprintf(\"%s\",...) failed: %s\n",
45 fmt, strerror(errno));
51 static char *masprintf(const char *fmt, ...)
52 __attribute__((format(printf,1,2)));
53 static char *masprintf(const char *fmt, ...) {
57 p= mvasprintf(fmt,al);
62 /*---------- output processing ----------*/
64 static void voutputstuff(int trace, const char *fmt, va_list al)
65 __attribute__((format(printf,2,0)));
66 static void voutputstuff(int trace, const char *fmt, va_list al) {
67 static struct OutputBuf {
73 char *s, *p, *newline;
77 s= mvasprintf(fmt,al);
81 newline= strchr(p,'\n');
82 if (newline) { *newline= 0; }
84 if (l+b->used > b->allocd) {
86 b->buf= MREALLOC(b->buf, b->allocd);
88 memcpy(b->buf+b->used, p, l);
93 if (trace) { fputs("# ",stdout); }
94 fwrite(b->buf, 1,b->used, stdout);
96 if (ferror(stdout) || fflush(stdout)) {
97 perror("stdout voutputstuff"); exit(12);
107 static void v##fn(const char *fmt, va_list al) \
108 __attribute__((format(printf,1,0))); \
109 static void v##fn(const char *fmt, va_list al) { \
110 voutputstuff(tr,fmt,al); \
112 static void fn(const char *fmt, ...) __attribute__((format(printf,1,2))); \
113 static void fn(const char *fmt, ...) { \
122 /*---------- traceing of various things ----------*/
124 static void trace_node(Node *node) {
125 trace("n%s",node->pname);
128 static void trace_nodeside(NodeSide *node) {
129 trace("n%s %c",node->node->pname,"OI"[node->side]);
132 static void trace_edge(Edge *edge) {
133 trace("e%s",edge->pname);
136 static void trace_edgeend(EdgeEnd *edge) {
137 trace("e%s %c",edge->edge->pname,"OI"[edge->end]);
140 static void trace_combpos(int combpos, Segment *seg) {
146 FOR_SEGMENT_MOVFEATS(i,f,seg) {
147 if (!f->movfeat) continue;
148 trace("%s%d", f->movfeat, (combpos / weight) % f->n_positions);
149 weight *= f->n_positions;
153 /*---------- general graph-manipulation tools ----------*/
155 static EdgeEnd *edgeend_otherend(EdgeEnd *thisend) {
156 return &thisend->edge->ends[!thisend->end];
159 static int count_edges(const NodeSide *node) {
163 for (count=0, edge=node->head;
165 count++, edge=edge->next);
169 static Edge *makeedge(const char *pname, double distance,
170 MovFeat *subseg, int movpos) {
174 e= MMALLOC(sizeof(*e));
176 e->distance= distance;
180 e->ends[end].edge= e;
181 e->ends[end].end= end;
183 trace("[new:"); trace_edge(e); trace("]");
188 static void edgeend_detach(EdgeEnd *toremove) {
189 trace("[detach:"); trace_edgeend(toremove);
190 trace("-"); trace_nodeside(toremove->node); trace("]");
191 LIST_UNLINK(*toremove->node, toremove);
193 static void edgeend_attach(EdgeEnd *toattach, NodeSide *newtarget) {
194 trace("[attach:"); trace_edgeend(toattach);
195 trace("-"); trace_nodeside(newtarget); trace("]");
196 LIST_LINK_TAIL(*newtarget, toattach);
197 toattach->node= newtarget;
199 static void edgeend_replumb(EdgeEnd *tomove, NodeSide *newtarget) {
200 edgeend_detach(tomove);
201 edgeend_attach(tomove,newtarget);
204 static void edge_delete(Edge *edge) {
205 edgeend_detach(&edge->ends[0]);
206 edgeend_detach(&edge->ends[1]);
207 trace("[delete:"); trace_edge(edge); trace("]");
210 static void node_surely_orphaned(Node *node) {
211 trace("[nodeorphan:"); trace_node(node); trace("]");
212 assert(!node->sides[0].head);
213 assert(!node->sides[1].head);
214 LIST_UNLINK(all_nodes, node);
217 /*---------- operations ----------*/
219 /* We combine the individual moveable features in each segment into
220 * a single `feature' with the cross-product of all of the
221 * positions. The new feature does not appear in
222 * Segment->[n_]movfeats, but Edge->subseg and Edge->movpos are
223 * updated (ie Edge->subseg->segment->movfeats is the original array
224 * containing only the actual features, and so does not contain
225 * Edge->subseg). It is often printed as if its name was `*' (which
226 * is not legal in input files etc.). The positions of the
227 * *-feature are numbered according to the obvious arithmetic coding
228 * of the individual actual feature positions. See also
231 * The conversion has two stages:
233 * FIRST STAGE (movfeatmultedges):
235 * We multiply up each edge of any segment with moveable features to
236 * be an edge which is a position of the *-feature.
238 * For example, if segment A1 has feature P with states 0 and 1
239 * and feature J with states 0 and 1, then the edges are translated
242 * A1 A1/P0J0 A1/P0J1 A1/P1J0 A1/P1J1
243 * A1/P0 A1/P0J0 A1/P0J1
244 * A1/P1 A1/P1J0 A1/P1J1
245 * A1/J0 A1/P0J0 A1/P1J0
246 * A1/J1 A1/P0J1 A1/P1J1
248 * *----A1/P0----* becomes *----A1/P0J0----*
253 * We split nodes: every node which has (only) edges which are part
254 * of the same segment is split into one node for each position and
255 * direction (directions are end0-to-side0+end1-to-side1 and
256 * end0-to-side1+end1-to-side0), to which the corresponding edges
257 * are connected. Any resulting node which has only one edge is
258 * removed together with the edge.
260 * For example, when we start with:
263 * ...*----A1/P0J1----*---A1/P0J1---*...
264 * `---A1/P1J0---' `--A1/P1J1--'
267 * the middle node is split like this (neglecting directions for
271 * ...*----A1/P0J1----*---A1/P0J1---*...
273 * `--A1/P1J1----*---A1/P1J1-'
275 * and then the stub edges and nodes are removed, leaving this:
277 * ...*----A1/P0J1----*---A1/P0J1---*...
279 * `--A1/P1J1----*---A1/P1J1-'
281 * The actual implementation of the second stage simply considers
282 * whether to create each node as it goes along, rather than
283 * inventing nodes merely to delete them.
286 static void movfeatmultedges(void) {
288 int side, l, i, weight, combpos, end;
290 Edge *edge, *newedge;
292 MovFeat *subseg, *starfeature, *loopfeat;
295 trace("movfeatmultedges ...\n");
296 FOR_ALL_NODES(node) {
297 FOR_EDGES(edge, node,side,edgeend) {
298 subseg= edge->subseg;
299 segment= subseg->segment;
301 trace("movfeatmultedges "); trace_edge(edge);
302 trace(" segment %s ", segment->segname);
303 if (segment->n_movfeats<=1) { trace("no moveables\n"); continue; }
304 if (subseg == segment->starfeature) { trace("*-feature\n"); continue; }
306 starfeature= segment->starfeature;
311 starfeature= MMALLOC(sizeof(*starfeature));
312 starfeature->segment= segment;
314 l=2; starfeature->n_positions=1;
315 FOR_SEGMENT_MOVFEATS(i,loopfeat, segment) {
316 if (!loopfeat->movfeat) continue;
317 l += strlen(loopfeat->movfeat);
318 starfeature->n_positions *= loopfeat->n_positions;
321 starfeature->movfeat= featstr= MMALLOC(l);
322 strcpy(featstr, "*");
324 FOR_SEGMENT_MOVFEATS(i,loopfeat, segment) {
325 if (!loopfeat->movfeat) continue;
326 strcat(featstr, loopfeat->movfeat);
329 segment->starfeature= starfeature;
331 trace(" %s(%d) ", starfeature->movfeat, starfeature->n_positions);
333 if (subseg->movfeat) {
335 FOR_SEGMENT_MOVFEATS(i,loopfeat, segment) {
336 if (loopfeat == subseg) goto found;
337 weight *= loopfeat->n_positions;
341 trace("%s(x%d)",subseg->movfeat,weight);
349 for (combpos=0; combpos<starfeature->n_positions; combpos++) {
350 trace("movfeatmultedges "); trace_combpos(combpos,segment);
351 if ((combpos / weight) % subseg->n_positions != edge->movpos) {
352 trace(" excluded\n");
355 newedge= makeedge(masprintf("%s/*%d",edge->pname,combpos),
360 edgeend_attach(&newedge->ends[end], edge->ends[end].node);
363 /* edge_delete doesn't destroy or modify this edge node,
364 * so we can safely carry on with this loop, which will
365 * now carry on with the next edge - either one we've just
366 * added, or the one which was next before we started messing
371 trace("movfeatmultedges complete.\n");
374 static void elimtrivial(void) {
375 /* Eliminate trivial nodes: ones which have only two edges, which
376 * come in on opposite sides, have the same subsegspec, and with the
377 * two ends identically aligned.
379 EdgeEnd *leftedge, *rightedge;
380 Node *node, *next_node;
382 FOR_ALL_NODES(node) {
383 next_node=node->next;
384 trace("elimtrivial node "); trace_node(node); trace(" ");
385 if (!(count_edges(&node->sides[0])==1 &&
386 count_edges(&node->sides[1])==1)) {
387 trace("no, a non-unitary side\n");
390 leftedge= node->sides[0].head;
391 rightedge= node->sides[1].head;
392 if (leftedge->edge->subseg != rightedge->edge->subseg) {
393 trace("no, (sub)segments differ\n");
396 if (leftedge->edge->movpos != rightedge->edge->movpos) {
397 trace("no, movpos's differ\n");
400 if (leftedge->end == rightedge->end) {
401 trace("no, opposite alignment\n");
405 rightedge->edge->distance += leftedge->edge->distance;
406 edgeend_replumb(rightedge, edgeend_otherend(leftedge)->node);
407 edge_delete(leftedge->edge);
408 node_surely_orphaned(node);
409 trace(" elimtrivial operation complete\n");
413 static void check(const char *why) {
415 int alloc_edone, used_edone;
416 typedef struct { Edge *edge; int endsdone; } EdgeDone;
417 EdgeDone *edone, *search_edone;
423 alloc_edone= used_edone= 0;
425 trace("consistency check (%s) ...\n",why);
426 FOR_ALL_NODES(node) {
427 trace(" consistency "); trace_node(node); trace("\n");
428 LIST_CHECKNODE(all_nodes,node);
430 trace(" consistency "); trace_nodeside(&node->sides[side]); trace("\n");
431 assert(node->sides[side].node == node);
432 assert(node->sides[side].side == side);
433 for (edgeend=node->sides[side].head; edgeend; edgeend=edgeend->next) {
434 trace(" consistency "); trace_nodeside(&node->sides[side]);
435 trace(" "); trace_edgeend(edgeend); trace("\n");
438 LIST_CHECKNODE(node->sides[side], edgeend);
439 assert(edgeend == &edge->ends[edgeend->end]);
440 assert(edgeend->node == &node->sides[side]);
441 if (used_edone==alloc_edone) {
442 alloc_edone += 2; alloc_edone *= 2;
443 edone= MREALLOC(edone, alloc_edone * sizeof(*edone));
445 for (search_edone= edone;
446 search_edone < edone + used_edone && search_edone->edge != edge;
448 if (search_edone >= edone + used_edone) {
449 search_edone->edge= edge;
450 search_edone->endsdone= 0;
453 search_edone->endsdone++;
454 assert(search_edone->endsdone <= 2);
458 for (search_edone= edone;
459 search_edone < edone + used_edone;
461 trace("consistency "); trace_edge(search_edone->edge);
462 trace(" count=%d\n", search_edone->endsdone);
463 assert(search_edone->endsdone == 2);
465 trace("consistency check (%s) ok\n",why);
468 static void consistency(void) {
469 check("command-line request");
472 static void printforneato(void) {
478 output("digraph L {\n");
480 FOR_ALL_NODES(node) {
481 output(" n%sO [label=\"%s\"];\n"
482 " n%sI [label=\"+\"];\n",
483 node->pname, node->pname, node->pname);
484 output(" n%sO -> n%sI [len=0.25 arrowsize=0];\n",
485 node->pname, node->pname);
486 FOR_EDGES(edge, node,side,edgeend) {
487 output(" n%s%c -> n%s%c [label=\"%s:",
488 edge->ends[0].node->node->pname,
489 "OI"[edge->ends[0].node->side],
490 edge->ends[1].node->node->pname,
491 "OI"[edge->ends[1].node->side],
493 if (!edge->subseg->segment->segname) {
496 output(edge->subseg->segment->segname);
497 if (edge->subseg->movfeat) {
498 output("/%s%d",edge->subseg->movfeat,edge->movpos);
501 output(":%.2f\"];\n",edge->distance);
512 static const OpInfo opinfos[]= {
513 #define OI(x) { #x, x },
521 static void usage(void) {
525 "usage: .../redactgraph <operation> [<operation>...]\n"
527 for (oi=opinfos; oi->name; oi++) {
528 fprintf(stderr," %s\n",oi->name);
534 int main(int argc, const char *const *argv) {
538 if (!argv[0] || !argv[1]) usage();
540 while ((arg= *++argv)) {
541 for (oi=opinfos; oi->name && strcmp(arg,oi->name); oi++);
542 if (!oi->name) usage();
547 if (ferror(stdout) || fflush(stdout)) { perror("stdout"); exit(12); }