Global: collectd → libsortnetwork
[sort-networks.git] / src / sn-tex.c
1 /**
2  * libsortnetwork - src/sn-tex.c
3  * Copyright (C) 2008-2010  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 <ff at octo.it>
20  **/
21
22 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
24 #endif
25 #ifndef _POSIX_C_SOURCE
26 # define _POSIX_C_SOURCE 200112L
27 #endif
28
29 #include <stdlib.h>
30 #include <stdio.h>
31
32 #define SCALE 0.7
33 #define INNER_SPACING 0.35
34 #define OUTER_SPACING 1.5
35
36 #include "sn_network.h"
37
38 static double x_offset = OUTER_SPACING;
39 static int next_vertex_number = 0;
40
41 static int tex_show_stage (sn_stage_t *s)
42 {
43   int lines[s->comparators_num];
44   int right[s->comparators_num];
45   int lines_used = 0;
46   int i;
47
48   for (i = 0; i < s->comparators_num; i++)
49   {
50     lines[i] = -1;
51     right[i] = -1;
52   }
53
54   for (i = 0; i < s->comparators_num; i++)
55   {
56     int j;
57     sn_comparator_t *c = s->comparators + i;
58
59     int min_num;
60     int max_num;
61
62     min_num = next_vertex_number;
63     next_vertex_number++;
64
65     max_num = next_vertex_number;
66     next_vertex_number++;
67
68     for (j = 0; j < lines_used; j++)
69       if (SN_COMP_LEFT (c) > right[j])
70         break;
71
72     lines[i] = j;
73     right[j] = SN_COMP_RIGHT (c);
74     if (j >= lines_used)
75       lines_used = j + 1;
76
77     printf ("\\node[vertex] (v%i) at (%.2f,%i) {};\n"
78         "\\node[vertex] (v%i) at (%.2f,%i) {};\n"
79         "\\path[comp] (v%i) -- (v%i);\n"
80         "\n",
81         min_num, x_offset + (j * INNER_SPACING), c->min,
82         max_num, x_offset + (j * INNER_SPACING), c->max,
83         min_num, max_num);
84   }
85
86   x_offset = x_offset + ((lines_used - 1) * INNER_SPACING) + OUTER_SPACING;
87
88   return (0);
89 } /* int tex_show_stage */
90
91 int main (int argc, char **argv)
92 {
93   sn_network_t *n;
94   FILE *fh = NULL;
95   int i;
96
97   if (argc == 1)
98     fh = stdin;
99   else if (argc == 2)
100     fh = fopen (argv[1], "r");
101
102   if (fh == NULL)
103   {
104     printf ("fh == NULL!\n");
105     return (1);
106   }
107
108   n = sn_network_read (fh);
109
110   if (n == NULL)
111   {
112     printf ("n == NULL!\n");
113     return (1);
114   }
115
116   printf ("\\begin{tikzpicture}[scale=%.2f,auto]\n", SCALE);
117
118   for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
119      tex_show_stage (SN_NETWORK_STAGE_GET (n, i));
120
121   for (i = 0; i < SN_NETWORK_INPUT_NUM (n); i++)
122     printf ("\\path[edge] (0,%i) -- (%.2f,%i);\n",
123         i, x_offset, i);
124
125   printf ("\\end{tikzpicture}\n");
126
127   return (0);
128 } /* int main */
129
130 /* vim: set shiftwidth=2 softtabstop=2 : */