]> granicus.if.org Git - graphviz/commitdiff
Fix cycle to list the directed edges
authorEmden R. Gansner <erg@alum.mit.edu>
Tue, 8 Oct 2013 14:45:15 +0000 (10:45 -0400)
committerEmden R. Gansner <erg@alum.mit.edu>
Tue, 8 Oct 2013 14:45:15 +0000 (10:45 -0400)
cmd/gvpr/lib/cycle [new file with mode: 0644]

diff --git a/cmd/gvpr/lib/cycle b/cmd/gvpr/lib/cycle
new file mode 100644 (file)
index 0000000..8ea8c4a
--- /dev/null
@@ -0,0 +1,33 @@
+/* Detect directed cycle and print one if found */
+BEG_G{
+  node_t tp, hp;
+  node_t stk[node_t];
+  $tvtype = TV_prepostfwd;
+  $tvroot = fstnode($);
+}
+
+N {
+  if (stk[$]) {
+      stk[$] = NULL;
+  }
+  else if ($tvedge == NULL) {  /* current root */
+    stk[$] = $;
+  }
+  else {
+    stk[$] = $tvedge.tail;
+  }
+}
+E {
+  if (stk[$.head]) {
+     tp = $.tail;
+     hp = $.head;
+     while (tp != $.head) {
+        printf ("%s -> %s\n", tp.name, hp.name);
+        hp = tp;
+        tp = stk[tp];
+      }
+     printf ("%s -> %s\n", tp.name, hp.name);
+     exit(0);
+  }
+}
+