2 * libsortnetwork - src/sn-merge.c
3 * Copyright (C) 2008-2010 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <ff at octo.it>
29 #include "sn_network.h"
31 static _Bool use_bitonic = 0;
32 static const char *file0 = NULL;
33 static const char *file1 = NULL;
35 static void exit_usage (void) /* {{{ */
37 printf ("sn-merge [options] <file0> <file1>\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"
45 } /* }}} void exit_usage */
47 static int read_options (int argc, char **argv) /* {{{ */
51 while ((option = getopt (argc, argv, "boh")) != -1)
69 if ((argc - optind) != 2)
73 file1 = argv[optind + 1];
75 if ((file0 == NULL) || (file1 == NULL))
79 } /* }}} int read_options */
81 static _Bool is_power_of_two (int n)
85 else if ((n == 1) || (n == 2))
87 else if ((n % 2) != 0)
90 return (is_power_of_two (n >> 1));
91 } /* _Bool is_power_of_two */
93 int main (int argc, char **argv)
99 read_options (argc, argv);
101 if (strcmp ("-", file0) == 0)
102 n0 = sn_network_read (stdin);
104 n0 = sn_network_read_file (file0);
107 fprintf (stderr, "Unable to read first network.\n");
111 if (strcmp ("-", file1) == 0)
113 if (strcmp ("-", file0) == 0)
114 n1 = sn_network_clone (n0);
116 n1 = sn_network_read (stdin);
119 n1 = sn_network_read_file (file1);
122 fprintf (stderr, "Unable to read second network.\n");
127 n = sn_network_combine_bitonic_merge (n0, n1);
129 n = sn_network_combine_odd_even_merge (n0, n1);
133 fprintf (stderr, "Combining the networks faild.\n");
137 sn_network_destroy (n0);
138 sn_network_destroy (n1);
140 sn_network_write (n, stdout);
145 /* vim: set shiftwidth=2 softtabstop=2 : */