77c242913e62712a8db0ef98b74b25cc3d2b3a7f
[sort-networks.git] / src / sn-merge.c
1 /**
2  * libsortnetwork - src/sn-merge.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 #include "config.h"
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include "sn_network.h"
30
31 static _Bool use_bitonic = 0;
32 static const char *file0 = NULL;
33 static const char *file1 = NULL;
34
35 static void exit_usage (void) /* {{{ */
36 {
37   printf ("sn-merge [options] <file0> <file1>\n"
38       "\n"
39       "Options:\n"
40       "  -b        Use the bitonic merger.\n"
41       "  -o        Use the odd-even merger. (default)\n"
42       "  -h        Display this help and exit.\n"
43       "\n");
44   exit (EXIT_FAILURE);
45 } /* }}} void exit_usage */
46
47 static int read_options (int argc, char **argv) /* {{{ */
48 {
49   int option;
50
51   while ((option = getopt (argc, argv, "boh")) != -1)
52   {
53     switch (option)
54     {
55       case 'b':
56         use_bitonic = 1;
57         break;
58
59       case 'o':
60         use_bitonic = 0;
61         break;
62
63       case 'h':
64       default:
65         exit_usage ();
66     }
67   }
68
69   if ((argc - optind) != 2)
70     exit_usage ();
71
72   file0 = argv[optind];
73   file1 = argv[optind + 1];
74
75   if ((file0 == NULL) || (file1 == NULL))
76     exit_usage ();
77
78   return (0);
79 } /* }}} int read_options */
80
81 static _Bool is_power_of_two (int n)
82 {
83   if (n < 1)
84     return (0);
85   else if ((n == 1) || (n == 2))
86     return (1);
87   else if ((n % 2) != 0)
88     return (0);
89   else
90     return (is_power_of_two (n >> 1));
91 } /* _Bool is_power_of_two */
92
93 int main (int argc, char **argv)
94 {
95   sn_network_t *n0;
96   sn_network_t *n1;
97   sn_network_t *n;
98
99   read_options (argc, argv);
100
101   if (strcmp ("-", file0) == 0)
102     n0 = sn_network_read (stdin);
103   else
104     n0 = sn_network_read_file (file0);
105   if (n0 == NULL)
106   {
107     fprintf (stderr, "Unable to read first network.\n");
108     exit (EXIT_FAILURE);
109   }
110
111   if (strcmp ("-", file1) == 0)
112   {
113     if (strcmp ("-", file0) == 0)
114       n1 = sn_network_clone (n0);
115     else
116       n1 = sn_network_read (stdin);
117   }
118   else
119     n1 = sn_network_read_file (file1);
120   if (n1 == NULL)
121   {
122     fprintf (stderr, "Unable to read second network.\n");
123     exit (EXIT_FAILURE);
124   }
125
126   if (use_bitonic)
127     n = sn_network_combine_bitonic_merge (n0, n1);
128   else
129     n = sn_network_combine_odd_even_merge (n0, n1);
130
131   if (n == NULL)
132   {
133     fprintf (stderr, "Combining the networks faild.\n");
134     exit (EXIT_FAILURE);
135   }
136
137   sn_network_destroy (n0);
138   sn_network_destroy (n1);
139
140   sn_network_write (n, stdout);
141
142   return (0);
143 } /* int main */
144
145 /* vim: set shiftwidth=2 softtabstop=2 : */