Global: collectd → libsortnetwork
[sort-networks.git] / src / sn-info.c
1 /**
2  * libsortnetwork - src/sn-info.c
3  * Copyright (C) 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 #include "config.h"
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <strings.h>
27
28 #include "sn_network.h"
29
30 static _Bool is_normalized (sn_network_t *n) /* {{{ */
31 {
32   int i;
33
34   for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
35   {
36     sn_stage_t *s = SN_NETWORK_STAGE_GET (n, i);
37     int j;
38
39     for (j = 0; j < SN_STAGE_COMP_NUM (s); j++)
40     {
41       sn_comparator_t *c = SN_STAGE_COMP_GET (s, j);
42
43       if (c->min > c->max)
44         return (0);
45     }
46   }
47
48   return (1);
49 } /* }}} _Bool is_normalized */
50
51 int main (int argc, char **argv)
52 {
53   sn_network_t *n;
54
55   int inputs_num;
56   int stages_num;
57   int comparators_num;
58   _Bool normalized;
59   _Bool sorts;
60
61   int stages_num_compressed;
62
63   if (argc >= 2)
64     n = sn_network_read_file (argv[1]);
65   else
66     n = sn_network_read (stdin);
67   if (n == NULL)
68   {
69     fprintf (stderr, "Unable to read network.\n");
70     exit (EXIT_FAILURE);
71   }
72
73   inputs_num = SN_NETWORK_INPUT_NUM (n);
74   stages_num = SN_NETWORK_STAGE_NUM (n);
75   comparators_num = sn_network_get_comparator_num (n);
76
77   normalized = is_normalized (n);
78
79   if (!normalized)
80     sn_network_normalize (n);
81   sn_network_compress (n);
82
83   stages_num_compressed = SN_NETWORK_STAGE_NUM (n);
84
85   if (inputs_num <= 16)
86   {
87     if (sn_network_brute_force_check (n) == 0)
88       sorts = 1;
89     else
90       sorts = 0;
91   }
92   else
93   {
94     sorts = 0;
95   }
96
97   printf ("%s %s network:\n"
98       "\n"
99       "  Inputs:      %4i\n",
100       (normalized ? "Standard" : "Non-standard"),
101       (sorts ? "sorting" : "comparator"),
102       inputs_num);
103
104   if (stages_num_compressed == stages_num)
105     printf ("  Stages:      %4i\n",
106         stages_num);
107   else
108     printf ("  Stages:      %4i (compressed: %i)\n",
109         stages_num, stages_num_compressed);
110
111   printf ("  Comparators: %4i\n"
112       "  Normalized:  %4s\n"
113       "  Sorts:    %7s\n"
114       "  Rating:      %4i\n"
115       "\n",
116       comparators_num,
117       (normalized ? "yes" : "no"),
118       ((inputs_num > 16) ? "unknown" : (sorts ? "yes" : "no")),
119       (stages_num_compressed * inputs_num) + comparators_num);
120
121   exit (EXIT_SUCCESS);
122 } /* int main */
123
124 /* vim: set shiftwidth=2 softtabstop=2 : */