From f7efe02b57557b50c14d82356c789fd0736b22a9 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 7 May 2008 17:04:36 +0200 Subject: [PATCH] src/sn-tex.c: Add a program to format sort networks as tikz source. --- src/sn-tex.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/sn-tex.c diff --git a/src/sn-tex.c b/src/sn-tex.c new file mode 100644 index 0000000..deb55b4 --- /dev/null +++ b/src/sn-tex.c @@ -0,0 +1,102 @@ +#include +#include + +#define SCALE 0.7 +#define INNER_SPACING 0.35 +#define OUTER_SPACING 1.5 + +#include "sn_network.h" + +static double x_offset = OUTER_SPACING; +static int next_vertex_number = 0; + +static int tex_show_stage (sn_stage_t *s) +{ + int lines[s->comparators_num]; + int right[s->comparators_num]; + int lines_used = 0; + int i; + + for (i = 0; i < s->comparators_num; i++) + { + lines[i] = -1; + right[i] = -1; + } + + for (i = 0; i < s->comparators_num; i++) + { + int j; + sn_comparator_t *c = s->comparators + i; + + int min_num; + int max_num; + + min_num = next_vertex_number; + next_vertex_number++; + + max_num = next_vertex_number; + next_vertex_number++; + + for (j = 0; j < lines_used; j++) + if (SN_COMP_LEFT (c) > right[j]) + break; + + lines[i] = j; + right[j] = SN_COMP_RIGHT (c); + if (j >= lines_used) + lines_used = j + 1; + + printf ("\\node[vertex] (v%i) at (%.2f,%i) {};\n" + "\\node[vertex] (v%i) at (%.2f,%i) {};\n" + "\\path[comp] (v%i) -- (v%i);\n" + "\n", + min_num, x_offset + (j * INNER_SPACING), c->min, + max_num, x_offset + (j * INNER_SPACING), c->max, + min_num, max_num); + } + + x_offset = x_offset + ((lines_used - 1) * INNER_SPACING) + OUTER_SPACING; + + return (0); +} /* int tex_show_stage */ + +int main (int argc, char **argv) +{ + sn_network_t *n; + FILE *fh = NULL; + int i; + + if (argc == 1) + fh = stdin; + else if (argc == 2) + fh = fopen (argv[1], "r"); + + if (fh == NULL) + { + printf ("fh == NULL!\n"); + return (1); + } + + n = sn_network_read (fh); + + if (n == NULL) + { + printf ("n == NULL!\n"); + return (1); + } + + printf ("\\begin{tikzpicture}[scale=%.2f,auto]\n", SCALE); + + 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,%i) -- (%.2f,%i);\n", + i, x_offset, i); + + printf ("\\end{tikzpicture}\n"); + + return (0); +} /* int main */ + +/* vim: set shiftwidth=2 softtabstop=2 : */ -- 2.11.0