src/*.[ch]: Added GPLv2 license information.
[sort-networks.git] / src / sn-tex.c
1 /**
2  * collectd - src/sn-tex.c
3  * Copyright (C) 2008  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include <stdlib.h>
23 #include <stdio.h>
24
25 #define SCALE 0.7
26 #define INNER_SPACING 0.35
27 #define OUTER_SPACING 1.5
28
29 #include "sn_network.h"
30
31 static double x_offset = OUTER_SPACING;
32 static int next_vertex_number = 0;
33
34 static int tex_show_stage (sn_stage_t *s)
35 {
36   int lines[s->comparators_num];
37   int right[s->comparators_num];
38   int lines_used = 0;
39   int i;
40
41   for (i = 0; i < s->comparators_num; i++)
42   {
43     lines[i] = -1;
44     right[i] = -1;
45   }
46
47   for (i = 0; i < s->comparators_num; i++)
48   {
49     int j;
50     sn_comparator_t *c = s->comparators + i;
51
52     int min_num;
53     int max_num;
54
55     min_num = next_vertex_number;
56     next_vertex_number++;
57
58     max_num = next_vertex_number;
59     next_vertex_number++;
60
61     for (j = 0; j < lines_used; j++)
62       if (SN_COMP_LEFT (c) > right[j])
63         break;
64
65     lines[i] = j;
66     right[j] = SN_COMP_RIGHT (c);
67     if (j >= lines_used)
68       lines_used = j + 1;
69
70     printf ("\\node[vertex] (v%i) at (%.2f,%i) {};\n"
71         "\\node[vertex] (v%i) at (%.2f,%i) {};\n"
72         "\\path[comp] (v%i) -- (v%i);\n"
73         "\n",
74         min_num, x_offset + (j * INNER_SPACING), c->min,
75         max_num, x_offset + (j * INNER_SPACING), c->max,
76         min_num, max_num);
77   }
78
79   x_offset = x_offset + ((lines_used - 1) * INNER_SPACING) + OUTER_SPACING;
80
81   return (0);
82 } /* int tex_show_stage */
83
84 int main (int argc, char **argv)
85 {
86   sn_network_t *n;
87   FILE *fh = NULL;
88   int i;
89
90   if (argc == 1)
91     fh = stdin;
92   else if (argc == 2)
93     fh = fopen (argv[1], "r");
94
95   if (fh == NULL)
96   {
97     printf ("fh == NULL!\n");
98     return (1);
99   }
100
101   n = sn_network_read (fh);
102
103   if (n == NULL)
104   {
105     printf ("n == NULL!\n");
106     return (1);
107   }
108
109   printf ("\\begin{tikzpicture}[scale=%.2f,auto]\n", SCALE);
110
111   for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
112      tex_show_stage (SN_NETWORK_STAGE_GET (n, i));
113
114   for (i = 0; i < SN_NETWORK_INPUT_NUM (n); i++)
115     printf ("\\path[edge] (0,%i) -- (%.2f,%i);\n",
116         i, x_offset, i);
117
118   printf ("\\end{tikzpicture}\n");
119
120   return (0);
121 } /* int main */
122
123 /* vim: set shiftwidth=2 softtabstop=2 : */