sn-tex: Fix indentation (remove tabs).
[sort-networks.git] / src / sn-tex.c
index 95bf61d..f93cd0c 100644 (file)
@@ -33,7 +33,7 @@
 
 #include "sn_network.h"
 
-/* 21cm (DIN-A4) - 2x3cm Rand = 15cm
+/* 21cm (DIN-A4) - 2x3cm Rand = 15cm */
 static double output_width = 15.0;
 static double scale = 1.0;
 static double inner_spacing = 0.3;
@@ -42,6 +42,7 @@ static double vertical_spacing = 0.8;
 
 static double x_offset;
 static int next_vertex_number = 0;
+static int flip = 0;
 
 #define INPUT_TO_Y(i) (((double) (i)) * vertical_spacing)
 
@@ -51,6 +52,7 @@ static void exit_usage (void) /* {{{ */
       "\n"
       "Valid options are:\n"
       "  -w <width>   Specify the width of the graph (in TikZ default units).\n" 
+      "  -f           Flip the sorting network (min wire at the top, max wire at the bottom).\n"
       "\n");
   exit (EXIT_FAILURE);
 } /* }}} void exit_usage */
@@ -59,26 +61,30 @@ static int read_options (int argc, char **argv) /* {{{ */
 {
   int option;
 
-  while ((option = getopt (argc, argv, "w:h?")) != -1)
+  while ((option = getopt (argc, argv, "w:fh?")) != -1)
   {
     switch (option)
     {
       case 'w':
       {
-       double width = atof (optarg);
-       if (width <= 0.0)
-       {
-         fprintf (stderr, "Invalid width argument: %s\n", optarg);
-         exit (EXIT_FAILURE);
-       }
-       output_width = width;
-       break;
+        double width = atof (optarg);
+        if (width <= 0.0)
+        {
+          fprintf (stderr, "Invalid width argument: %s\n", optarg);
+          exit (EXIT_FAILURE);
+        }
+        output_width = width;
+        break;
       }
 
+      case 'f':
+        flip = 1;
+        break;
+
       case 'h':
       case '?':
       default:
-       exit_usage ();
+        exit_usage ();
     }
   }
 
@@ -108,7 +114,7 @@ static double determine_stage_width (sn_stage_t *s) /* {{{ */
 
     for (j = 0; j < lines_used; j++)
       if (SN_COMP_LEFT (c) > right[j])
-       break;
+        break;
 
     lines[i] = j;
     right[j] = SN_COMP_RIGHT (c);
@@ -164,7 +170,7 @@ static int tex_show_stage (sn_stage_t *s) /* {{{ */
 
     for (j = 0; j < lines_used; j++)
       if (SN_COMP_LEFT (c) > right[j])
-       break;
+        break;
 
     lines[i] = j;
     right[j] = SN_COMP_RIGHT (c);
@@ -172,12 +178,12 @@ static int tex_show_stage (sn_stage_t *s) /* {{{ */
       lines_used = j + 1;
 
     printf ("\\node[vertex] (v%i) at (%.2f,%.2f) {};\n"
-       "\\node[vertex] (v%i) at (%.2f,%.2f) {};\n"
-       "\\path[comp] (v%i) -- (v%i);\n"
-       "\n",
-       min_num, x_offset + (j * inner_spacing), INPUT_TO_Y (c->min),
-       max_num, x_offset + (j * inner_spacing), INPUT_TO_Y (c->max),
-       min_num, max_num);
+        "\\node[vertex] (v%i) at (%.2f,%.2f) {};\n"
+        "\\path[comp] (v%i) -- (v%i);\n"
+        "\n",
+        min_num, x_offset + (j * inner_spacing), INPUT_TO_Y (c->min),
+        max_num, x_offset + (j * inner_spacing), INPUT_TO_Y (c->max),
+        min_num, max_num);
   }
 
   x_offset = x_offset + ((lines_used - 1) * inner_spacing) + outer_spacing;
@@ -213,7 +219,7 @@ int main (int argc, char **argv) /* {{{ */
   if (orig_width <= 0.0)
   {
     fprintf (stderr, "determine_network_width returned invalid value %g.\n",
-       orig_width);
+        orig_width);
     exit (EXIT_FAILURE);
   }
 
@@ -224,14 +230,17 @@ int main (int argc, char **argv) /* {{{ */
 
   x_offset = outer_spacing;
 
-  printf ("\\begin{tikzpicture}[auto]\n");
+  if (flip)
+    printf ("\\begin{tikzpicture}[auto,yscale=-1]\n");
+  else
+    printf ("\\begin{tikzpicture}[auto]\n");
 
   for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
      tex_show_stage (SN_NETWORK_STAGE_GET (n, i));
 
   for (i = 0; i < SN_NETWORK_INPUT_NUM (n); i++)
     printf ("\\path[edge] (0,%.2f) -- (%.2f,%.2f);\n",
-       INPUT_TO_Y (i), x_offset, INPUT_TO_Y (i));
+        INPUT_TO_Y (i), x_offset, INPUT_TO_Y (i));
 
   printf ("\\end{tikzpicture}\n");