Implement the bitonic sort in src/sn_network.c.
[sort-networks.git] / src / sn-bitonicsort.c
index 9a69dc2..3119123 100644 (file)
 
 #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 <num inputs>\n", argv[0]);
-    return (0);
+    printf ("Usage: sn-bitonicsort <num inputs>\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);