X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fsn-bitonicsort.c;h=31191230487f9fc2be4639444b07c3f9bf8f729b;hb=8d745e97bdb8371c42dff7304d55511fabfa6b6c;hp=9a69dc2f863632c07016d7d10d769009acd5e9e9;hpb=d602a8356687728b0973eeb6d4b6da881238f89c;p=sort-networks.git diff --git a/src/sn-bitonicsort.c b/src/sn-bitonicsort.c index 9a69dc2..3119123 100644 --- a/src/sn-bitonicsort.c +++ b/src/sn-bitonicsort.c @@ -31,88 +31,30 @@ #include "sn_network.h" -static sn_network_t *create_batcher_sort (size_t inputs_num) -{ - sn_network_t *n; - sn_network_t *n_small; - - if (inputs_num == 2) - { - sn_stage_t *s; - sn_comparator_t c; - - n = sn_network_create (2); - if (n == NULL) - { - fprintf (stderr, "create_batcher_sort: sn_network_create failed.\n"); - return (NULL); - } - - s = sn_stage_create (0); - if (s == NULL) - { - sn_network_destroy (n); - fprintf (stderr, "create_batcher_sort: sn_stage_create failed.\n"); - return (NULL); - } - - c.min = 0; - c.max = 1; - - sn_stage_comparator_add (s, &c); - sn_network_stage_add (n, s); - - return (n); - } - else if ((inputs_num < 2) || ((inputs_num % 2) != 0)) - { - fprintf (stderr, "create_batcher_sort: Inputs must be a power of two, " - "sorry.\n"); - return (NULL); - } - - n_small = create_batcher_sort (inputs_num / 2); - if (n_small == NULL) - return (NULL); - - n = sn_network_combine_bitonic_merge (n_small, n_small); - if (n == NULL) - { - sn_network_destroy (n_small); - fprintf (stderr, "create_batcher_sort: sn_network_combine_bitonic_merge " - "failed.\n"); - return (NULL); - } - - sn_network_destroy (n_small); - sn_network_compress (n); - - return (n); -} /* sn_network_t *create_batcher_sort */ - int main (int argc, char **argv) { sn_network_t *n; - size_t inputs_num; + int inputs_num; if (argc != 2) { - printf ("Usage: %s \n", argv[0]); - return (0); + printf ("Usage: sn-bitonicsort \n"); + exit (EXIT_SUCCESS); } - inputs_num = (size_t) atoi (argv[1]); + inputs_num = atoi (argv[1]); if (inputs_num < 2) { - fprintf (stderr, "Invalid number of inputs: %zu\n", inputs_num); - return (1); + fprintf (stderr, "Invalid number of inputs: %i\n", inputs_num); + exit (EXIT_FAILURE); } - n = create_batcher_sort (inputs_num); + n = sn_network_create_bitonic_mergesort (inputs_num); if (n == NULL) { - printf ("n == NULL!\n"); - return (1); + fprintf (stderr, "Creating bitonic mergesort network with %i inputs failed.\n", + inputs_num); + exit (EXIT_FAILURE); } sn_network_write (n, stdout);